PBMLoader.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) 2020, Hüseyin ASLITÜRK <asliturk@hotmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "PBMLoader.h"
  27. #include "PortableImageLoaderCommon.h"
  28. #include "Streamer.h"
  29. #include <AK/Endian.h>
  30. #include <AK/LexicalPath.h>
  31. #include <AK/MappedFile.h>
  32. #include <AK/StringBuilder.h>
  33. #include <string.h>
  34. namespace Gfx {
  35. struct PBMLoadingContext {
  36. enum Type {
  37. Unknown,
  38. ASCII,
  39. RAWBITS
  40. };
  41. enum State {
  42. NotDecoded = 0,
  43. Error,
  44. MagicNumber,
  45. Width,
  46. Height,
  47. Bitmap,
  48. Decoded
  49. };
  50. static constexpr auto ascii_magic_number = '1';
  51. static constexpr auto binary_magic_number = '4';
  52. static constexpr auto image_type = "PBM";
  53. Type type { Type::Unknown };
  54. State state { State::NotDecoded };
  55. const u8* data { nullptr };
  56. size_t data_size { 0 };
  57. size_t width { 0 };
  58. size_t height { 0 };
  59. RefPtr<Gfx::Bitmap> bitmap;
  60. };
  61. static bool read_image_data(PBMLoadingContext& context, Streamer& streamer)
  62. {
  63. u8 byte;
  64. Vector<Gfx::Color> color_data;
  65. if (context.type == PBMLoadingContext::ASCII) {
  66. while (streamer.read(byte)) {
  67. if (byte == '0') {
  68. color_data.append(Color::White);
  69. } else if (byte == '1') {
  70. color_data.append(Color::Black);
  71. }
  72. }
  73. } else if (context.type == PBMLoadingContext::RAWBITS) {
  74. size_t color_index = 0;
  75. while (streamer.read(byte)) {
  76. for (int i = 0; i < 8; i++) {
  77. int val = byte & 0x80;
  78. if (val == 0) {
  79. color_data.append(Color::White);
  80. } else {
  81. color_data.append(Color::Black);
  82. }
  83. byte = byte << 1;
  84. color_index++;
  85. if (color_index % context.width == 0) {
  86. break;
  87. }
  88. }
  89. }
  90. }
  91. if (!create_bitmap(context)) {
  92. return false;
  93. }
  94. set_pixels(context, color_data);
  95. context.state = PBMLoadingContext::State::Bitmap;
  96. return true;
  97. }
  98. RefPtr<Gfx::Bitmap> load_pbm(const StringView& path)
  99. {
  100. return load<PBMLoadingContext>(path);
  101. }
  102. RefPtr<Gfx::Bitmap> load_pbm_from_memory(const u8* data, size_t length)
  103. {
  104. auto bitmap = load_impl<PBMLoadingContext>(data, length);
  105. if (bitmap)
  106. bitmap->set_mmap_name(String::format("Gfx::Bitmap [%dx%d] - Decoded PBM: <memory>", bitmap->width(), bitmap->height()));
  107. return bitmap;
  108. }
  109. PBMImageDecoderPlugin::PBMImageDecoderPlugin(const u8* data, size_t size)
  110. {
  111. m_context = make<PBMLoadingContext>();
  112. m_context->data = data;
  113. m_context->data_size = size;
  114. }
  115. PBMImageDecoderPlugin::~PBMImageDecoderPlugin()
  116. {
  117. }
  118. IntSize PBMImageDecoderPlugin::size()
  119. {
  120. if (m_context->state == PBMLoadingContext::State::Error)
  121. return {};
  122. if (m_context->state < PBMLoadingContext::State::Decoded) {
  123. bool success = decode(*m_context);
  124. if (!success)
  125. return {};
  126. }
  127. return { m_context->width, m_context->height };
  128. }
  129. RefPtr<Gfx::Bitmap> PBMImageDecoderPlugin::bitmap()
  130. {
  131. if (m_context->state == PBMLoadingContext::State::Error)
  132. return nullptr;
  133. if (m_context->state < PBMLoadingContext::State::Decoded) {
  134. bool success = decode(*m_context);
  135. if (!success)
  136. return nullptr;
  137. }
  138. ASSERT(m_context->bitmap);
  139. return m_context->bitmap;
  140. }
  141. void PBMImageDecoderPlugin::set_volatile()
  142. {
  143. if (m_context->bitmap)
  144. m_context->bitmap->set_volatile();
  145. }
  146. bool PBMImageDecoderPlugin::set_nonvolatile()
  147. {
  148. if (!m_context->bitmap)
  149. return false;
  150. return m_context->bitmap->set_nonvolatile();
  151. }
  152. bool PBMImageDecoderPlugin::sniff()
  153. {
  154. if (m_context->data_size < 2)
  155. return false;
  156. if (m_context->data[0] == 'P' && m_context->data[1] == '1')
  157. return true;
  158. if (m_context->data[0] == 'P' && m_context->data[1] == '4')
  159. return true;
  160. return false;
  161. }
  162. bool PBMImageDecoderPlugin::is_animated()
  163. {
  164. return false;
  165. }
  166. size_t PBMImageDecoderPlugin::loop_count()
  167. {
  168. return 0;
  169. }
  170. size_t PBMImageDecoderPlugin::frame_count()
  171. {
  172. return 1;
  173. }
  174. ImageFrameDescriptor PBMImageDecoderPlugin::frame(size_t i)
  175. {
  176. if (i > 0) {
  177. return { bitmap(), 0 };
  178. }
  179. return {};
  180. }
  181. }