PGMLoader.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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(u8 const* data, size_t length, String const& mmap_name)
  90. {
  91. return load_from_memory<PGMLoadingContext>(data, length, mmap_name);
  92. }
  93. PGMImageDecoderPlugin::PGMImageDecoderPlugin(const u8* data, size_t size)
  94. {
  95. m_context = make<PGMLoadingContext>();
  96. m_context->data = data;
  97. m_context->data_size = size;
  98. }
  99. PGMImageDecoderPlugin::~PGMImageDecoderPlugin()
  100. {
  101. }
  102. IntSize PGMImageDecoderPlugin::size()
  103. {
  104. if (m_context->state == PGMLoadingContext::State::Error)
  105. return {};
  106. if (m_context->state < PGMLoadingContext::State::Decoded) {
  107. bool success = decode(*m_context);
  108. if (!success)
  109. return {};
  110. }
  111. return { m_context->width, m_context->height };
  112. }
  113. RefPtr<Gfx::Bitmap> PGMImageDecoderPlugin::bitmap()
  114. {
  115. if (m_context->state == PGMLoadingContext::State::Error)
  116. return nullptr;
  117. if (m_context->state < PGMLoadingContext::State::Decoded) {
  118. bool success = decode(*m_context);
  119. if (!success)
  120. return nullptr;
  121. }
  122. VERIFY(m_context->bitmap);
  123. return m_context->bitmap;
  124. }
  125. void PGMImageDecoderPlugin::set_volatile()
  126. {
  127. if (m_context->bitmap)
  128. m_context->bitmap->set_volatile();
  129. }
  130. bool PGMImageDecoderPlugin::set_nonvolatile(bool& was_purged)
  131. {
  132. if (!m_context->bitmap)
  133. return false;
  134. return m_context->bitmap->set_nonvolatile(was_purged);
  135. }
  136. bool PGMImageDecoderPlugin::sniff()
  137. {
  138. if (m_context->data_size < 2)
  139. return false;
  140. if (m_context->data[0] == 'P' && m_context->data[1] == '2')
  141. return true;
  142. if (m_context->data[0] == 'P' && m_context->data[1] == '5')
  143. return true;
  144. return false;
  145. }
  146. bool PGMImageDecoderPlugin::is_animated()
  147. {
  148. return false;
  149. }
  150. size_t PGMImageDecoderPlugin::loop_count()
  151. {
  152. return 0;
  153. }
  154. size_t PGMImageDecoderPlugin::frame_count()
  155. {
  156. return 1;
  157. }
  158. ImageFrameDescriptor PGMImageDecoderPlugin::frame(size_t i)
  159. {
  160. if (i > 0)
  161. return {};
  162. return { bitmap(), 0 };
  163. }
  164. }