PGMLoader.cpp 4.1 KB

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