PBMLoader.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 <string.h>
  11. namespace Gfx {
  12. struct PBMLoadingContext {
  13. enum Type {
  14. Unknown,
  15. ASCII,
  16. RAWBITS
  17. };
  18. enum State {
  19. NotDecoded = 0,
  20. Error,
  21. MagicNumber,
  22. Width,
  23. Height,
  24. Bitmap,
  25. Decoded
  26. };
  27. static constexpr auto ascii_magic_number = '1';
  28. static constexpr auto binary_magic_number = '4';
  29. static constexpr auto image_type = "PBM";
  30. Type type { Type::Unknown };
  31. State state { State::NotDecoded };
  32. const u8* data { nullptr };
  33. size_t data_size { 0 };
  34. size_t width { 0 };
  35. size_t height { 0 };
  36. RefPtr<Gfx::Bitmap> bitmap;
  37. };
  38. static bool read_image_data(PBMLoadingContext& context, Streamer& streamer)
  39. {
  40. u8 byte;
  41. Vector<Gfx::Color> color_data;
  42. if (context.type == PBMLoadingContext::ASCII) {
  43. while (streamer.read(byte)) {
  44. if (byte == '0') {
  45. color_data.append(Color::White);
  46. } else if (byte == '1') {
  47. color_data.append(Color::Black);
  48. }
  49. }
  50. } else if (context.type == PBMLoadingContext::RAWBITS) {
  51. size_t color_index = 0;
  52. while (streamer.read(byte)) {
  53. for (int i = 0; i < 8; i++) {
  54. int val = byte & 0x80;
  55. if (val == 0) {
  56. color_data.append(Color::White);
  57. } else {
  58. color_data.append(Color::Black);
  59. }
  60. byte = byte << 1;
  61. color_index++;
  62. if (color_index % context.width == 0) {
  63. break;
  64. }
  65. }
  66. }
  67. }
  68. size_t context_size = (u32)context.width * (u32)context.height;
  69. if (context_size != color_data.size()) {
  70. dbgln("Not enough color data in image.");
  71. return false;
  72. }
  73. if (!create_bitmap(context)) {
  74. return false;
  75. }
  76. set_pixels(context, color_data);
  77. context.state = PBMLoadingContext::State::Bitmap;
  78. return true;
  79. }
  80. RefPtr<Gfx::Bitmap> load_pbm_from_memory(u8 const* data, size_t length, String const& mmap_name)
  81. {
  82. return load_from_memory<PBMLoadingContext>(data, length, mmap_name);
  83. }
  84. PBMImageDecoderPlugin::PBMImageDecoderPlugin(const u8* data, size_t size)
  85. {
  86. m_context = make<PBMLoadingContext>();
  87. m_context->data = data;
  88. m_context->data_size = size;
  89. }
  90. PBMImageDecoderPlugin::~PBMImageDecoderPlugin()
  91. {
  92. }
  93. IntSize PBMImageDecoderPlugin::size()
  94. {
  95. if (m_context->state == PBMLoadingContext::State::Error)
  96. return {};
  97. if (m_context->state < PBMLoadingContext::State::Decoded) {
  98. bool success = decode(*m_context);
  99. if (!success)
  100. return {};
  101. }
  102. return { m_context->width, m_context->height };
  103. }
  104. RefPtr<Gfx::Bitmap> PBMImageDecoderPlugin::bitmap()
  105. {
  106. if (m_context->state == PBMLoadingContext::State::Error)
  107. return nullptr;
  108. if (m_context->state < PBMLoadingContext::State::Decoded) {
  109. bool success = decode(*m_context);
  110. if (!success)
  111. return nullptr;
  112. }
  113. VERIFY(m_context->bitmap);
  114. return m_context->bitmap;
  115. }
  116. void PBMImageDecoderPlugin::set_volatile()
  117. {
  118. if (m_context->bitmap)
  119. m_context->bitmap->set_volatile();
  120. }
  121. bool PBMImageDecoderPlugin::set_nonvolatile(bool& was_purged)
  122. {
  123. if (!m_context->bitmap)
  124. return false;
  125. return m_context->bitmap->set_nonvolatile(was_purged);
  126. }
  127. bool PBMImageDecoderPlugin::sniff()
  128. {
  129. if (m_context->data_size < 2)
  130. return false;
  131. if (m_context->data[0] == 'P' && m_context->data[1] == '1')
  132. return true;
  133. if (m_context->data[0] == 'P' && m_context->data[1] == '4')
  134. return true;
  135. return false;
  136. }
  137. bool PBMImageDecoderPlugin::is_animated()
  138. {
  139. return false;
  140. }
  141. size_t PBMImageDecoderPlugin::loop_count()
  142. {
  143. return 0;
  144. }
  145. size_t PBMImageDecoderPlugin::frame_count()
  146. {
  147. return 1;
  148. }
  149. ImageFrameDescriptor PBMImageDecoderPlugin::frame(size_t i)
  150. {
  151. if (i > 0)
  152. return {};
  153. return { bitmap(), 0 };
  154. }
  155. }