graphics-magick.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 = CloneImageInfo(NULL);
  15. ExceptionInfo *exception = AcquireExceptionInfo();
  16. Image *image;
  17. strcpy(info->filename, file);
  18. image = ReadImage(info, exception);
  19. if (image) {
  20. size_t cx = image->magick_columns;
  21. size_t cy = image->magick_rows;
  22. uint8_t *data = malloc(cx * cy * 4);
  23. ExportImagePixels(image, 0, 0, cx, cy, "BGRA", CharPixel,
  24. data, exception);
  25. if (exception->severity == UndefinedException)
  26. tex = gs_create_texture(cx, cy, GS_BGRA, 1,
  27. (const uint8_t**)&data, 0);
  28. else
  29. blog(LOG_WARNING, "magickcore warning/error getting "
  30. "pixels from file '%s': %s", file,
  31. exception->reason);
  32. free(data);
  33. DestroyImage(image);
  34. } else if (exception->severity != UndefinedException) {
  35. blog(LOG_WARNING, "magickcore warning/error reading file "
  36. "'%s': %s", file, exception->reason);
  37. }
  38. DestroyImageInfo(info);
  39. DestroyExceptionInfo(exception);
  40. return tex;
  41. }