Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

106 wiersze
2.2 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. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #if defined USE_IMLIB2
  14. #include <Imlib2.h>
  15. #include "core.h"
  16. #include "../../image/image-private.h"
  17. using namespace std;
  18. namespace lol
  19. {
  20. /*
  21. * Imlib2 image codec
  22. */
  23. class Imlib2ImageCodec : public ImageCodec
  24. {
  25. public:
  26. virtual bool Load(Image *image, char const *path);
  27. virtual bool Save(Image *image, char const *path);
  28. };
  29. /* Set priority higher than SDL because we can save in many formats. */
  30. DECLARE_IMAGE_CODEC(Imlib2ImageCodec, 70)
  31. bool Imlib2ImageCodec::Load(Image *image, char const *path)
  32. {
  33. Imlib_Image priv = nullptr;
  34. Array<String> pathlist = System::GetPathList(path);
  35. for (int i = 0; i < pathlist.Count(); i++)
  36. {
  37. priv = imlib_load_image(pathlist[i].C());
  38. if (priv)
  39. break;
  40. }
  41. if (!priv)
  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(priv);
  49. if (!imlib_image_get_data())
  50. {
  51. imlib_free_image();
  52. #if !LOL_BUILD_RELEASE
  53. Log::Error("could not get image data for %s\n", path);
  54. #endif
  55. return false;
  56. }
  57. ivec2 size(imlib_image_get_width(), imlib_image_get_height());
  58. image->SetSize(size);
  59. u8vec4 *data = image->Lock<PixelFormat::RGBA_8>();
  60. memcpy(data, imlib_image_get_data(), 4 * size.x * size.y);
  61. image->Unlock(data);
  62. imlib_free_image();
  63. return true;
  64. }
  65. bool Imlib2ImageCodec::Save(Image *image, char const *path)
  66. {
  67. ivec2 size = image->GetSize();
  68. Imlib_Image priv = imlib_create_image(size.x, size.y);
  69. imlib_context_set_image(priv);
  70. imlib_image_set_has_alpha(1);
  71. u8vec4 *data = image->Lock<PixelFormat::RGBA_8>();
  72. memcpy(imlib_image_get_data(), data, 4 * size.x * size.y);
  73. image->Unlock(data);
  74. imlib_save_image(path);
  75. imlib_free_image();
  76. return true;
  77. }
  78. } /* namespace lol */
  79. #endif /* defined USE_IMLIB2 */