PBMLoader.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 <AK/Endian.h>
  28. #include <AK/LexicalPath.h>
  29. #include <AK/MappedFile.h>
  30. #include <AK/StringBuilder.h>
  31. #include <string.h>
  32. namespace Gfx {
  33. struct PBMLoadingContext {
  34. enum Type {
  35. Unknown,
  36. P1_ASCII,
  37. P4_RAWBITS
  38. };
  39. enum State {
  40. NotDecoded = 0,
  41. Error,
  42. MagicNumber,
  43. Width,
  44. Height,
  45. Bitmap,
  46. Decoded
  47. };
  48. Type type { Type::Unknown };
  49. State state { State::NotDecoded };
  50. const u8* data { nullptr };
  51. size_t data_size { 0 };
  52. int width { -1 };
  53. int height { -1 };
  54. RefPtr<Gfx::Bitmap> bitmap;
  55. };
  56. class Streamer {
  57. public:
  58. Streamer(const u8* data, size_t size)
  59. : m_data_ptr(data)
  60. , m_size_remaining(size)
  61. {
  62. }
  63. template<typename T>
  64. bool read(T& value)
  65. {
  66. if (m_size_remaining < sizeof(T))
  67. return false;
  68. value = *((const NetworkOrdered<T>*)m_data_ptr);
  69. m_data_ptr += sizeof(T);
  70. m_size_remaining -= sizeof(T);
  71. return true;
  72. }
  73. bool read_bytes(u8* buffer, size_t count)
  74. {
  75. if (m_size_remaining < count)
  76. return false;
  77. memcpy(buffer, m_data_ptr, count);
  78. m_data_ptr += count;
  79. m_size_remaining -= count;
  80. return true;
  81. }
  82. bool at_end() const { return !m_size_remaining; }
  83. void step_back()
  84. {
  85. m_data_ptr -= 1;
  86. m_size_remaining += 1;
  87. }
  88. private:
  89. const u8* m_data_ptr { nullptr };
  90. size_t m_size_remaining { 0 };
  91. };
  92. static int read_number(Streamer& streamer)
  93. {
  94. u8 byte;
  95. StringBuilder sb;
  96. while (streamer.read(byte)) {
  97. if (byte == ' ' || byte == '\t' || byte == '\n' || byte == '\r') {
  98. streamer.step_back();
  99. break;
  100. }
  101. sb.append(byte);
  102. }
  103. return sb.to_string().to_uint().value_or(0);
  104. }
  105. static bool read_comment(PBMLoadingContext& context, Streamer& streamer)
  106. {
  107. (void)context;
  108. bool exist = false;
  109. u8 byte;
  110. while (streamer.read(byte)) {
  111. switch (byte) {
  112. case '#': {
  113. exist = true;
  114. break;
  115. }
  116. case '\t':
  117. case '\n': {
  118. return exist;
  119. }
  120. default:
  121. break;
  122. }
  123. }
  124. return exist;
  125. }
  126. static bool read_magic_number(PBMLoadingContext& context, Streamer& streamer)
  127. {
  128. if (context.state >= PBMLoadingContext::MagicNumber)
  129. return true;
  130. if (!context.data || context.data_size < 2) {
  131. context.state = PBMLoadingContext::State::Error;
  132. dbg() << "There is no enough data.";
  133. return false;
  134. }
  135. u8 magic_number[2];
  136. if (!streamer.read_bytes(magic_number, 2)) {
  137. context.state = PBMLoadingContext::State::Error;
  138. dbg() << "We can't read magic number.";
  139. return false;
  140. }
  141. if (magic_number[0] == 'P' && magic_number[1] == '1') {
  142. context.type = PBMLoadingContext::P1_ASCII;
  143. context.state = PBMLoadingContext::MagicNumber;
  144. return true;
  145. }
  146. if (magic_number[0] == 'P' && magic_number[1] == '4') {
  147. context.type = PBMLoadingContext::P4_RAWBITS;
  148. context.state = PBMLoadingContext::MagicNumber;
  149. return true;
  150. }
  151. context.state = PBMLoadingContext::State::Error;
  152. dbg() << "Magic number is not valid." << (char)magic_number[0] << (char)magic_number[1];
  153. return false;
  154. }
  155. static bool read_white_space(PBMLoadingContext& context, Streamer& streamer)
  156. {
  157. bool exist = false;
  158. u8 byte;
  159. while (streamer.read(byte)) {
  160. switch (byte) {
  161. case ' ':
  162. case '\t':
  163. case '\n':
  164. case '\r': {
  165. exist = true;
  166. break;
  167. }
  168. case '#': {
  169. streamer.step_back();
  170. read_comment(context, streamer);
  171. break;
  172. }
  173. default: {
  174. streamer.step_back();
  175. return exist;
  176. }
  177. }
  178. }
  179. return exist;
  180. }
  181. static bool read_width(PBMLoadingContext& context, Streamer& streamer)
  182. {
  183. context.width = read_number(streamer);
  184. if (context.width == 0) {
  185. return false;
  186. }
  187. context.state = PBMLoadingContext::Width;
  188. return true;
  189. }
  190. static bool read_height(PBMLoadingContext& context, Streamer& streamer)
  191. {
  192. context.height = read_number(streamer);
  193. if (context.height == 0) {
  194. return false;
  195. }
  196. context.state = PBMLoadingContext::Height;
  197. return true;
  198. }
  199. static bool read_image_data(PBMLoadingContext& context, Streamer& streamer)
  200. {
  201. u8 byte;
  202. Vector<Gfx::Color> color_data;
  203. if (context.type == PBMLoadingContext::P1_ASCII) {
  204. while (streamer.read(byte)) {
  205. if (byte == '0') {
  206. color_data.append(Color::White);
  207. } else if (byte == '1') {
  208. color_data.append(Color::Black);
  209. }
  210. }
  211. } else if (context.type == PBMLoadingContext::P4_RAWBITS) {
  212. size_t color_index = 0;
  213. while (streamer.read(byte)) {
  214. for (int i = 0; i < 8; i++) {
  215. int val = byte & 0x80;
  216. if (val == 0) {
  217. color_data.append(Color::White);
  218. } else {
  219. color_data.append(Color::Black);
  220. }
  221. byte = byte << 1;
  222. color_index++;
  223. if (color_index % context.width == 0) {
  224. break;
  225. }
  226. }
  227. }
  228. }
  229. context.bitmap = Bitmap::create_purgeable(BitmapFormat::RGB32, { context.width, context.height });
  230. size_t index = 0;
  231. for (int y = 0; y < context.height; ++y) {
  232. for (int x = 0; x < context.width; ++x) {
  233. context.bitmap->set_pixel(x, y, color_data.at(index));
  234. index++;
  235. }
  236. }
  237. context.state = PBMLoadingContext::State::Bitmap;
  238. return true;
  239. }
  240. static bool decode_pbm(PBMLoadingContext& context)
  241. {
  242. if (context.state >= PBMLoadingContext::State::Decoded)
  243. return true;
  244. Streamer streamer(context.data, context.data_size);
  245. if (!read_magic_number(context, streamer))
  246. return false;
  247. if (!read_white_space(context, streamer))
  248. return false;
  249. if (!read_width(context, streamer))
  250. return false;
  251. if (!read_white_space(context, streamer))
  252. return false;
  253. if (!read_height(context, streamer))
  254. return false;
  255. if (!read_white_space(context, streamer))
  256. return false;
  257. if (!read_image_data(context, streamer))
  258. return false;
  259. context.state = PBMLoadingContext::State::Decoded;
  260. return true;
  261. }
  262. static RefPtr<Gfx::Bitmap> load_pbm_impl(const u8* data, size_t data_size)
  263. {
  264. PBMLoadingContext context;
  265. context.data = data;
  266. context.data_size = data_size;
  267. if (!decode_pbm(context))
  268. return nullptr;
  269. return context.bitmap;
  270. }
  271. RefPtr<Gfx::Bitmap> load_pbm(const StringView& path)
  272. {
  273. MappedFile mapped_file(path);
  274. if (!mapped_file.is_valid()) {
  275. return nullptr;
  276. }
  277. auto bitmap = load_pbm_impl((const u8*)mapped_file.data(), mapped_file.size());
  278. if (bitmap)
  279. bitmap->set_mmap_name(String::format("Gfx::Bitmap [%dx%d] - Decoded PBM: %s", bitmap->width(), bitmap->height(), LexicalPath::canonicalized_path(path).characters()));
  280. return bitmap;
  281. }
  282. RefPtr<Gfx::Bitmap> load_pbm_from_memory(const u8* data, size_t length)
  283. {
  284. auto bitmap = load_pbm_impl(data, length);
  285. if (bitmap)
  286. bitmap->set_mmap_name(String::format("Gfx::Bitmap [%dx%d] - Decoded PBM: <memory>", bitmap->width(), bitmap->height()));
  287. return bitmap;
  288. }
  289. PBMImageDecoderPlugin::PBMImageDecoderPlugin(const u8* data, size_t size)
  290. {
  291. m_context = make<PBMLoadingContext>();
  292. m_context->data = data;
  293. m_context->data_size = size;
  294. }
  295. PBMImageDecoderPlugin::~PBMImageDecoderPlugin()
  296. {
  297. }
  298. IntSize PBMImageDecoderPlugin::size()
  299. {
  300. if (m_context->state == PBMLoadingContext::State::Error)
  301. return {};
  302. if (m_context->state < PBMLoadingContext::State::Decoded) {
  303. bool success = decode_pbm(*m_context);
  304. if (!success)
  305. return {};
  306. }
  307. return { m_context->width, m_context->height };
  308. }
  309. RefPtr<Gfx::Bitmap> PBMImageDecoderPlugin::bitmap()
  310. {
  311. if (m_context->state == PBMLoadingContext::State::Error)
  312. return nullptr;
  313. if (m_context->state < PBMLoadingContext::State::Decoded) {
  314. bool success = decode_pbm(*m_context);
  315. if (!success)
  316. return nullptr;
  317. }
  318. ASSERT(m_context->bitmap);
  319. return m_context->bitmap;
  320. }
  321. void PBMImageDecoderPlugin::set_volatile()
  322. {
  323. if (m_context->bitmap)
  324. m_context->bitmap->set_volatile();
  325. }
  326. bool PBMImageDecoderPlugin::set_nonvolatile()
  327. {
  328. if (!m_context->bitmap)
  329. return false;
  330. return m_context->bitmap->set_nonvolatile();
  331. }
  332. bool PBMImageDecoderPlugin::sniff()
  333. {
  334. if (m_context->data_size < 2)
  335. return false;
  336. if (m_context->data[0] == 'P' && m_context->data[1] == '1')
  337. return true;
  338. if (m_context->data[0] == 'P' && m_context->data[1] == '4')
  339. return true;
  340. return false;
  341. }
  342. bool PBMImageDecoderPlugin::is_animated()
  343. {
  344. return false;
  345. }
  346. size_t PBMImageDecoderPlugin::loop_count()
  347. {
  348. return 0;
  349. }
  350. size_t PBMImageDecoderPlugin::frame_count()
  351. {
  352. return 1;
  353. }
  354. ImageFrameDescriptor PBMImageDecoderPlugin::frame(size_t i)
  355. {
  356. if (i > 0) {
  357. return { bitmap(), 0 };
  358. }
  359. return {};
  360. }
  361. }