graphics-magick.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "graphics.h"
  2. #define MAGICKCORE_QUANTUM_DEPTH 16
  3. #define MAGICKCORE_HDRI_ENABLE 0
  4. #include <magick/MagickCore.h>
  5. void gs_init_image_deps()
  6. {
  7. MagickCoreGenesis(NULL, MagickTrue);
  8. }
  9. void gs_free_image_deps()
  10. {
  11. MagickCoreTerminus();
  12. }
  13. gs_texture_t *gs_texture_create_from_file(const char *file)
  14. {
  15. gs_texture_t *tex = NULL;
  16. ImageInfo *info;
  17. ExceptionInfo *exception;
  18. Image *image;
  19. if (!file || !*file)
  20. return NULL;
  21. info = CloneImageInfo(NULL);
  22. exception = AcquireExceptionInfo();
  23. strcpy(info->filename, file);
  24. image = ReadImage(info, exception);
  25. if (image) {
  26. size_t cx = image->magick_columns;
  27. size_t cy = image->magick_rows;
  28. uint8_t *data = malloc(cx * cy * 4);
  29. ExportImagePixels(image, 0, 0, cx, cy, "BGRA", CharPixel,
  30. data, exception);
  31. if (exception->severity == UndefinedException)
  32. tex = gs_texture_create(cx, cy, GS_BGRA, 1,
  33. (const uint8_t**)&data, 0);
  34. else
  35. blog(LOG_WARNING, "magickcore warning/error getting "
  36. "pixels from file '%s': %s", file,
  37. exception->reason);
  38. free(data);
  39. DestroyImage(image);
  40. } else if (exception->severity != UndefinedException) {
  41. blog(LOG_WARNING, "magickcore warning/error reading file "
  42. "'%s': %s", file, exception->reason);
  43. }
  44. DestroyImageInfo(info);
  45. DestroyExceptionInfo(exception);
  46. return tex;
  47. }