graphics-magick.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. uint8_t *gs_create_texture_file_data(const char *file,
  14. enum gs_color_format *format,
  15. uint32_t *cx_out, uint32_t *cy_out)
  16. {
  17. uint8_t *data = NULL;
  18. ImageInfo *info;
  19. ExceptionInfo *exception;
  20. Image *image;
  21. if (!file || !*file)
  22. return NULL;
  23. info = CloneImageInfo(NULL);
  24. exception = AcquireExceptionInfo();
  25. strcpy(info->filename, file);
  26. image = ReadImage(info, exception);
  27. if (image) {
  28. size_t cx = image->magick_columns;
  29. size_t cy = image->magick_rows;
  30. data = bmalloc(cx * cy * 4);
  31. ExportImagePixels(image, 0, 0, cx, cy, "BGRA", CharPixel,
  32. data, exception);
  33. if (exception->severity != UndefinedException) {
  34. blog(LOG_WARNING, "magickcore warning/error getting "
  35. "pixels from file '%s': %s", file,
  36. exception->reason);
  37. bfree(data);
  38. data = NULL;
  39. }
  40. *format = GS_BGRA;
  41. *cx_out = (uint32_t)cx;
  42. *cy_out = (uint32_t)cy;
  43. DestroyImage(image);
  44. } else if (exception->severity != UndefinedException) {
  45. blog(LOG_WARNING, "magickcore warning/error reading file "
  46. "'%s': %s", file, exception->reason);
  47. }
  48. DestroyImageInfo(info);
  49. DestroyExceptionInfo(exception);
  50. return data;
  51. }