PPMLoader.cpp 4.3 KB

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