PPMLoader.cpp 4.6 KB

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