PGMLoader.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2020, Hüseyin ASLITÜRK <asliturk@hotmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "PGMLoader.h"
  7. #include "PortableImageLoaderCommon.h"
  8. #include "Streamer.h"
  9. #include <AK/Endian.h>
  10. #include <string.h>
  11. namespace Gfx {
  12. struct PGMLoadingContext {
  13. enum Type {
  14. Unknown,
  15. ASCII,
  16. RAWBITS
  17. };
  18. enum State {
  19. NotDecoded = 0,
  20. Error,
  21. MagicNumber,
  22. Width,
  23. Height,
  24. Maxval,
  25. Bitmap,
  26. Decoded
  27. };
  28. static constexpr auto ascii_magic_number = '2';
  29. static constexpr auto binary_magic_number = '5';
  30. static constexpr auto image_type = "PGM";
  31. Type type { Type::Unknown };
  32. State state { State::NotDecoded };
  33. const u8* data { nullptr };
  34. size_t data_size { 0 };
  35. u16 width { 0 };
  36. u16 height { 0 };
  37. u16 max_val { 0 };
  38. RefPtr<Gfx::Bitmap> bitmap;
  39. };
  40. static void set_adjusted_pixels(PGMLoadingContext& context, const Vector<Gfx::Color>& color_data)
  41. {
  42. size_t index = 0;
  43. for (size_t y = 0; y < context.height; ++y) {
  44. for (size_t x = 0; x < context.width; ++x) {
  45. Color color = color_data.at(index);
  46. if (context.max_val < 255) {
  47. color = adjust_color(context.max_val, color);
  48. }
  49. context.bitmap->set_pixel(x, y, color);
  50. ++index;
  51. }
  52. }
  53. }
  54. static bool read_image_data(PGMLoadingContext& context, Streamer& streamer)
  55. {
  56. Vector<Gfx::Color> color_data;
  57. if (context.type == PGMLoadingContext::ASCII) {
  58. u16 value;
  59. while (true) {
  60. if (!read_number(streamer, &value))
  61. break;
  62. if (!read_whitespace(context, streamer))
  63. break;
  64. color_data.append({ (u8)value, (u8)value, (u8)value });
  65. }
  66. } else if (context.type == PGMLoadingContext::RAWBITS) {
  67. u8 pixel;
  68. while (streamer.read(pixel)) {
  69. color_data.append({ pixel, pixel, pixel });
  70. }
  71. }
  72. size_t context_size = (u32)context.width * (u32)context.height;
  73. if (context_size != color_data.size()) {
  74. dbgln("Not enough color data in image.");
  75. return false;
  76. }
  77. if (!create_bitmap(context))
  78. return false;
  79. set_adjusted_pixels(context, color_data);
  80. context.state = PGMLoadingContext::State::Bitmap;
  81. return true;
  82. }
  83. RefPtr<Gfx::Bitmap> load_pgm_from_memory(u8 const* data, size_t length, String const& mmap_name)
  84. {
  85. return load_from_memory<PGMLoadingContext>(data, length, mmap_name);
  86. }
  87. PGMImageDecoderPlugin::PGMImageDecoderPlugin(const u8* data, size_t size)
  88. {
  89. m_context = make<PGMLoadingContext>();
  90. m_context->data = data;
  91. m_context->data_size = size;
  92. }
  93. PGMImageDecoderPlugin::~PGMImageDecoderPlugin()
  94. {
  95. }
  96. IntSize PGMImageDecoderPlugin::size()
  97. {
  98. if (m_context->state == PGMLoadingContext::State::Error)
  99. return {};
  100. if (m_context->state < PGMLoadingContext::State::Decoded) {
  101. bool success = decode(*m_context);
  102. if (!success)
  103. return {};
  104. }
  105. return { m_context->width, m_context->height };
  106. }
  107. RefPtr<Gfx::Bitmap> PGMImageDecoderPlugin::bitmap()
  108. {
  109. if (m_context->state == PGMLoadingContext::State::Error)
  110. return nullptr;
  111. if (m_context->state < PGMLoadingContext::State::Decoded) {
  112. bool success = decode(*m_context);
  113. if (!success)
  114. return nullptr;
  115. }
  116. VERIFY(m_context->bitmap);
  117. return m_context->bitmap;
  118. }
  119. void PGMImageDecoderPlugin::set_volatile()
  120. {
  121. if (m_context->bitmap)
  122. m_context->bitmap->set_volatile();
  123. }
  124. bool PGMImageDecoderPlugin::set_nonvolatile(bool& was_purged)
  125. {
  126. if (!m_context->bitmap)
  127. return false;
  128. return m_context->bitmap->set_nonvolatile(was_purged);
  129. }
  130. bool PGMImageDecoderPlugin::sniff()
  131. {
  132. if (m_context->data_size < 2)
  133. return false;
  134. if (m_context->data[0] == 'P' && m_context->data[1] == '2')
  135. return true;
  136. if (m_context->data[0] == 'P' && m_context->data[1] == '5')
  137. return true;
  138. return false;
  139. }
  140. bool PGMImageDecoderPlugin::is_animated()
  141. {
  142. return false;
  143. }
  144. size_t PGMImageDecoderPlugin::loop_count()
  145. {
  146. return 0;
  147. }
  148. size_t PGMImageDecoderPlugin::frame_count()
  149. {
  150. return 1;
  151. }
  152. ImageFrameDescriptor PGMImageDecoderPlugin::frame(size_t i)
  153. {
  154. if (i > 0)
  155. return {};
  156. return { bitmap(), 0 };
  157. }
  158. }