You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

124 line
2.9 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #include <lol/engine-internal.h>
  11. #if defined USE_IMLIB2
  12. #include <Imlib2.h>
  13. /* Check that the Imlib2 types are safe */
  14. static_assert(sizeof(DATA64) == sizeof(uint64_t), "Imlib2 type DATA64 is broken");
  15. static_assert(sizeof(DATA32) == sizeof(uint32_t), "Imlib2 type DATA32 is broken");
  16. static_assert(sizeof(DATA16) == sizeof(uint16_t), "Imlib2 type DATA16 is broken");
  17. static_assert(sizeof(DATA8) == sizeof(uint8_t), "Imlib2 type DATA8 is broken");
  18. #include "../../image/image-private.h"
  19. namespace lol
  20. {
  21. /*
  22. * Imlib2 image codec
  23. */
  24. class Imlib2ImageCodec : public ImageCodec
  25. {
  26. public:
  27. virtual bool Load(Image *image, char const *path);
  28. virtual bool Save(Image *image, char const *path);
  29. };
  30. /* Set priority higher than SDL because we can save in many formats. */
  31. DECLARE_IMAGE_CODEC(Imlib2ImageCodec, 70)
  32. bool Imlib2ImageCodec::Load(Image *image, char const *path)
  33. {
  34. Imlib_Image im = nullptr;
  35. for (auto candidate : System::GetPathList(path))
  36. {
  37. im = imlib_load_image(candidate.C());
  38. if (im)
  39. break;
  40. }
  41. if (!im)
  42. {
  43. #if !LOL_BUILD_RELEASE
  44. Log::Error("could not load image %s\n", path);
  45. #endif
  46. return false;
  47. }
  48. imlib_context_set_image(im);
  49. u8vec4 const *srcdata = (u8vec4 *)imlib_image_get_data_for_reading_only();
  50. if (!srcdata)
  51. {
  52. imlib_free_image();
  53. #if !LOL_BUILD_RELEASE
  54. Log::Error("could not get image data for %s\n", path);
  55. #endif
  56. return false;
  57. }
  58. ivec2 size(imlib_image_get_width(), imlib_image_get_height());
  59. image->SetSize(size);
  60. u8vec4 *dstdata = image->Lock<PixelFormat::RGBA_8>();
  61. for (int i = 0; i < size.x * size.y; i++)
  62. {
  63. if (is_big_endian())
  64. dstdata[i] = srcdata[i].argb;
  65. else
  66. dstdata[i] = srcdata[i].bgra;
  67. }
  68. image->Unlock(dstdata);
  69. imlib_free_image();
  70. return true;
  71. }
  72. bool Imlib2ImageCodec::Save(Image *image, char const *path)
  73. {
  74. ivec2 size = image->GetSize();
  75. Imlib_Image priv = imlib_create_image(size.x, size.y);
  76. imlib_context_set_image(priv);
  77. imlib_image_set_has_alpha(1);
  78. u8vec4 const *srcdata = image->Lock<PixelFormat::RGBA_8>();
  79. u8vec4 *dstdata = (u8vec4 *)imlib_image_get_data();
  80. for (int i = 0; i < size.x * size.y; i++)
  81. {
  82. if (is_big_endian())
  83. dstdata[i] = srcdata[i].argb;
  84. else
  85. dstdata[i] = srcdata[i].bgra;
  86. }
  87. imlib_image_put_back_data((DATA32 *)dstdata);
  88. image->Unlock(srcdata);
  89. imlib_save_image(path);
  90. imlib_free_image();
  91. return true;
  92. }
  93. } /* namespace lol */
  94. #endif /* defined USE_IMLIB2 */