PBMLoader.cpp 4.3 KB

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