Clone of Bael'Zharon's Respite @ https://github.com/boardwalk/bzr

ImgColor.cpp 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Bael'Zharon's Respite
  3. * Copyright (C) 2014 Daniel Skorupski
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "resource/ImgColor.h"
  19. #include "resource/Palette.h"
  20. #include "BinReader.h"
  21. #include "Core.h"
  22. #include "ResourceCache.h"
  23. ImgColor::ImgColor(uint32_t id, const void* data, size_t size) : ResourceImpl{id}
  24. {
  25. BinReader reader(data, size);
  26. uint32_t resourceId = reader.readInt();
  27. assert(resourceId == id);
  28. UNUSED(resourceId);
  29. uint32_t unk1 = reader.readInt();
  30. assert(unk1 <= 0xA);
  31. UNUSED(unk1);
  32. uint32_t width = reader.readInt();
  33. assert(width <= 4096);
  34. uint32_t height = reader.readInt();
  35. assert(height <= 4096);
  36. PixelFormat format = static_cast<PixelFormat>(reader.readInt());
  37. if(format == PixelFormat::kCustomRawJPEG)
  38. {
  39. throw runtime_error("JPEG textures not supported");
  40. }
  41. uint32_t pixelsSize = reader.readInt();
  42. assert(pixelsSize * 8 == width * height * bitsPerPixel(format));
  43. const uint8_t* pixels = reader.readRaw(pixelsSize);
  44. if(isPaletted(format))
  45. {
  46. uint32_t paletteId = reader.readInt();
  47. palette = Core::get().resourceCache().get(paletteId);
  48. }
  49. assert(reader.remaining() == 0);
  50. image.init(format, width, height, pixels);
  51. if(palette)
  52. {
  53. image.applyPalette(palette->cast<Palette>());
  54. }
  55. }
  56. ImgColor::ImgColor(uint32_t bgra) : ResourceImpl{static_cast<uint32_t>(ResourceType::kImgColor) | 0xFFFF}
  57. {
  58. image.init(PixelFormat::kA8R8G8B8, 1, 1, &bgra);
  59. }