PGMLoader.cpp 4.6 KB

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