PBMLoader.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2020, Hüseyin ASLITÜRK <asliturk@hotmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "PBMLoader.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/StringBuilder.h>
  13. #include <string.h>
  14. namespace Gfx {
  15. struct PBMLoadingContext {
  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. Bitmap,
  28. Decoded
  29. };
  30. static constexpr auto ascii_magic_number = '1';
  31. static constexpr auto binary_magic_number = '4';
  32. static constexpr auto image_type = "PBM";
  33. Type type { Type::Unknown };
  34. State state { State::NotDecoded };
  35. const u8* data { nullptr };
  36. size_t data_size { 0 };
  37. size_t width { 0 };
  38. size_t height { 0 };
  39. RefPtr<Gfx::Bitmap> bitmap;
  40. };
  41. static bool read_image_data(PBMLoadingContext& context, Streamer& streamer)
  42. {
  43. u8 byte;
  44. Vector<Gfx::Color> color_data;
  45. if (context.type == PBMLoadingContext::ASCII) {
  46. while (streamer.read(byte)) {
  47. if (byte == '0') {
  48. color_data.append(Color::White);
  49. } else if (byte == '1') {
  50. color_data.append(Color::Black);
  51. }
  52. }
  53. } else if (context.type == PBMLoadingContext::RAWBITS) {
  54. size_t color_index = 0;
  55. while (streamer.read(byte)) {
  56. for (int i = 0; i < 8; i++) {
  57. int val = byte & 0x80;
  58. if (val == 0) {
  59. color_data.append(Color::White);
  60. } else {
  61. color_data.append(Color::Black);
  62. }
  63. byte = byte << 1;
  64. color_index++;
  65. if (color_index % context.width == 0) {
  66. break;
  67. }
  68. }
  69. }
  70. }
  71. size_t context_size = (u32)context.width * (u32)context.height;
  72. if (context_size != color_data.size()) {
  73. dbgln("Not enough color data in image.");
  74. return false;
  75. }
  76. if (!create_bitmap(context)) {
  77. return false;
  78. }
  79. set_pixels(context, color_data);
  80. context.state = PBMLoadingContext::State::Bitmap;
  81. return true;
  82. }
  83. RefPtr<Gfx::Bitmap> load_pbm(const StringView& path)
  84. {
  85. return load<PBMLoadingContext>(path);
  86. }
  87. RefPtr<Gfx::Bitmap> load_pbm_from_memory(const u8* data, size_t length)
  88. {
  89. auto bitmap = load_impl<PBMLoadingContext>(data, length);
  90. if (bitmap)
  91. bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded PBM: <memory>", bitmap->size()));
  92. return bitmap;
  93. }
  94. PBMImageDecoderPlugin::PBMImageDecoderPlugin(const u8* data, size_t size)
  95. {
  96. m_context = make<PBMLoadingContext>();
  97. m_context->data = data;
  98. m_context->data_size = size;
  99. }
  100. PBMImageDecoderPlugin::~PBMImageDecoderPlugin()
  101. {
  102. }
  103. IntSize PBMImageDecoderPlugin::size()
  104. {
  105. if (m_context->state == PBMLoadingContext::State::Error)
  106. return {};
  107. if (m_context->state < PBMLoadingContext::State::Decoded) {
  108. bool success = decode(*m_context);
  109. if (!success)
  110. return {};
  111. }
  112. return { m_context->width, m_context->height };
  113. }
  114. RefPtr<Gfx::Bitmap> PBMImageDecoderPlugin::bitmap()
  115. {
  116. if (m_context->state == PBMLoadingContext::State::Error)
  117. return nullptr;
  118. if (m_context->state < PBMLoadingContext::State::Decoded) {
  119. bool success = decode(*m_context);
  120. if (!success)
  121. return nullptr;
  122. }
  123. VERIFY(m_context->bitmap);
  124. return m_context->bitmap;
  125. }
  126. void PBMImageDecoderPlugin::set_volatile()
  127. {
  128. if (m_context->bitmap)
  129. m_context->bitmap->set_volatile();
  130. }
  131. bool PBMImageDecoderPlugin::set_nonvolatile()
  132. {
  133. if (!m_context->bitmap)
  134. return false;
  135. return m_context->bitmap->set_nonvolatile();
  136. }
  137. bool PBMImageDecoderPlugin::sniff()
  138. {
  139. if (m_context->data_size < 2)
  140. return false;
  141. if (m_context->data[0] == 'P' && m_context->data[1] == '1')
  142. return true;
  143. if (m_context->data[0] == 'P' && m_context->data[1] == '4')
  144. return true;
  145. return false;
  146. }
  147. bool PBMImageDecoderPlugin::is_animated()
  148. {
  149. return false;
  150. }
  151. size_t PBMImageDecoderPlugin::loop_count()
  152. {
  153. return 0;
  154. }
  155. size_t PBMImageDecoderPlugin::frame_count()
  156. {
  157. return 1;
  158. }
  159. ImageFrameDescriptor PBMImageDecoderPlugin::frame(size_t i)
  160. {
  161. if (i > 0) {
  162. return { bitmap(), 0 };
  163. }
  164. return {};
  165. }
  166. }