graphics-magick.c 1.3 KB

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