Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

95 righe
2.4 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2009—2014 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
  5. // © 2010—2018 Sam Hocevar <sam@hocevar.net>
  6. //
  7. // Lol Engine is free software. It comes without any warranty, to
  8. // the extent permitted by applicable law. You can redistribute it
  9. // and/or modify it under the terms of the Do What the Fuck You Want
  10. // to Public License, Version 2, as published by the WTFPL Task Force.
  11. // See http://www.wtfpl.net/ for more details.
  12. //
  13. #include <lol/engine-internal.h>
  14. #include <string>
  15. #include "../../image/resource-private.h"
  16. namespace lol
  17. {
  18. /*
  19. * Image implementation class
  20. */
  21. class ZedPaletteImageCodec : public ResourceCodec
  22. {
  23. public:
  24. virtual std::string GetName() { return "<ZedPaletteImageCodec>"; }
  25. virtual ResourceCodecData* Load(std::string const &path);
  26. virtual bool Save(std::string const &path, ResourceCodecData* data);
  27. };
  28. DECLARE_IMAGE_CODEC(ZedPaletteImageCodec, 10)
  29. /*
  30. * Public Image class
  31. */
  32. ResourceCodecData* ZedPaletteImageCodec::Load(std::string const &path)
  33. {
  34. if (!ends_with(path, ".pal"))
  35. return nullptr;
  36. File file;
  37. file.Open(path, FileAccess::Read, true);
  38. // Put file in memory
  39. long file_size = file.size();
  40. array<uint8_t> file_buffer;
  41. file_buffer.resize(file_size);
  42. file.Read((uint8_t*)&file_buffer[0], file_size);
  43. file.Close();
  44. #if 0 //2D PALETTE
  45. int32_t tex_sqrt = (int32_t)lol::sqrt((float)file_size / 3);
  46. int32_t tex_size = 2;
  47. while (tex_size < tex_sqrt)
  48. tex_size <<= 1;
  49. auto data = new ResourceImageData(new image(ivec2(tex_size)));
  50. auto image = data->m_image;
  51. #else
  52. int32_t tex_sqrt = file_size / 3;
  53. int32_t tex_size = 2;
  54. while (tex_size < tex_sqrt)
  55. tex_size <<= 1;
  56. auto data = new ResourceImageData(new image(ivec2(tex_size, 1)));
  57. auto image = data->m_image;
  58. #endif
  59. u8vec4 *pixels = image->lock<PixelFormat::RGBA_8>();
  60. for (int i = 0; i < file_buffer.count();)
  61. {
  62. pixels->r = file_buffer[i++];
  63. pixels->g = file_buffer[i++];
  64. pixels->b = file_buffer[i++];
  65. pixels->a = (i == 0) ? 0 : 255;
  66. ++pixels;
  67. }
  68. image->unlock(pixels);
  69. return data;
  70. }
  71. bool ZedPaletteImageCodec::Save(std::string const &path, ResourceCodecData* data)
  72. {
  73. UNUSED(path, data);
  74. /* FIXME: do we need to implement this? */
  75. return true;
  76. }
  77. } /* namespace lol */