PPMLoader.cpp 4.5 KB

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