PBMLoader.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. PBMImageDecoderPlugin::PBMImageDecoderPlugin(const u8* data, size_t size)
  81. {
  82. m_context = make<PBMLoadingContext>();
  83. m_context->data = data;
  84. m_context->data_size = size;
  85. }
  86. PBMImageDecoderPlugin::~PBMImageDecoderPlugin()
  87. {
  88. }
  89. IntSize PBMImageDecoderPlugin::size()
  90. {
  91. if (m_context->state == PBMLoadingContext::State::Error)
  92. return {};
  93. if (m_context->state < PBMLoadingContext::State::Decoded) {
  94. bool success = decode(*m_context);
  95. if (!success)
  96. return {};
  97. }
  98. return { m_context->width, m_context->height };
  99. }
  100. void PBMImageDecoderPlugin::set_volatile()
  101. {
  102. if (m_context->bitmap)
  103. m_context->bitmap->set_volatile();
  104. }
  105. bool PBMImageDecoderPlugin::set_nonvolatile(bool& was_purged)
  106. {
  107. if (!m_context->bitmap)
  108. return false;
  109. return m_context->bitmap->set_nonvolatile(was_purged);
  110. }
  111. bool PBMImageDecoderPlugin::sniff()
  112. {
  113. if (m_context->data_size < 2)
  114. return false;
  115. if (m_context->data[0] == 'P' && m_context->data[1] == '1')
  116. return true;
  117. if (m_context->data[0] == 'P' && m_context->data[1] == '4')
  118. return true;
  119. return false;
  120. }
  121. bool PBMImageDecoderPlugin::is_animated()
  122. {
  123. return false;
  124. }
  125. size_t PBMImageDecoderPlugin::loop_count()
  126. {
  127. return 0;
  128. }
  129. size_t PBMImageDecoderPlugin::frame_count()
  130. {
  131. return 1;
  132. }
  133. ErrorOr<ImageFrameDescriptor> PBMImageDecoderPlugin::frame(size_t index)
  134. {
  135. if (index > 0)
  136. return Error::from_string_literal("PBMImageDecoderPlugin: Invalid frame index"sv);
  137. if (m_context->state == PBMLoadingContext::State::Error)
  138. return Error::from_string_literal("PBMImageDecoderPlugin: Decoding failed"sv);
  139. if (m_context->state < PBMLoadingContext::State::Decoded) {
  140. bool success = decode(*m_context);
  141. if (!success)
  142. return Error::from_string_literal("PBMImageDecoderPlugin: Decoding failed"sv);
  143. }
  144. VERIFY(m_context->bitmap);
  145. return ImageFrameDescriptor { m_context->bitmap, 0 };
  146. }
  147. }