GIFLoader.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  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 <AK/BufferStream.h>
  27. #include <AK/ByteBuffer.h>
  28. #include <AK/FileSystemPath.h>
  29. #include <AK/NonnullOwnPtrVector.h>
  30. #include <LibGfx/GIFLoader.h>
  31. #include <stdio.h>
  32. namespace Gfx {
  33. static RefPtr<Gfx::Bitmap> load_gif_impl(const u8*, size_t);
  34. RefPtr<Gfx::Bitmap> load_gif(const StringView& path)
  35. {
  36. MappedFile mapped_file(path);
  37. if (!mapped_file.is_valid())
  38. return nullptr;
  39. auto bitmap = load_gif_impl((const u8*)mapped_file.data(), mapped_file.size());
  40. if (bitmap)
  41. bitmap->set_mmap_name(String::format("Gfx::Bitmap [%dx%d] - Decoded GIF: %s", bitmap->width(), bitmap->height(), canonicalized_path(path).characters()));
  42. return bitmap;
  43. }
  44. RefPtr<Gfx::Bitmap> load_gif_from_memory(const u8* data, size_t length)
  45. {
  46. auto bitmap = load_gif_impl(data, length);
  47. if (bitmap)
  48. bitmap->set_mmap_name(String::format("Gfx::Bitmap [%dx%d] - Decoded GIF: <memory>", bitmap->width(), bitmap->height()));
  49. return bitmap;
  50. }
  51. enum class GIFFormat {
  52. GIF87a,
  53. GIF89a,
  54. };
  55. struct RGB {
  56. u8 r;
  57. u8 g;
  58. u8 b;
  59. };
  60. struct LogicalScreen {
  61. u16 width;
  62. u16 height;
  63. RGB color_map[256];
  64. };
  65. struct ImageDescriptor {
  66. u16 x;
  67. u16 y;
  68. u16 width;
  69. u16 height;
  70. bool use_global_color_map;
  71. RGB color_map[256];
  72. u8 lzw_min_code_size;
  73. Vector<u8> lzw_encoded_bytes;
  74. };
  75. RefPtr<Gfx::Bitmap> load_gif_impl(const u8* data, size_t data_size)
  76. {
  77. if (data_size < 32)
  78. return nullptr;
  79. auto buffer = ByteBuffer::wrap(data, data_size);
  80. BufferStream stream(buffer);
  81. static const char valid_header_87[] = "GIF87a";
  82. static const char valid_header_89[] = "GIF89a";
  83. char header[6];
  84. for (int i = 0; i < 6; ++i)
  85. stream >> header[i];
  86. GIFFormat format;
  87. if (!memcmp(header, valid_header_87, sizeof(header)))
  88. format = GIFFormat::GIF87a;
  89. else if (!memcmp(header, valid_header_89, sizeof(header)))
  90. format = GIFFormat::GIF89a;
  91. else
  92. return nullptr;
  93. printf("Format is %s\n", format == GIFFormat::GIF89a ? "GIF89a" : "GIF87a");
  94. LogicalScreen logical_screen;
  95. stream >> logical_screen.width;
  96. stream >> logical_screen.height;
  97. if (stream.handle_read_failure())
  98. return nullptr;
  99. u8 gcm_info = 0;
  100. stream >> gcm_info;
  101. if (stream.handle_read_failure())
  102. return nullptr;
  103. bool global_color_map_follows_descriptor = gcm_info & 0x80;
  104. u8 bits_per_pixel = (gcm_info & 7) + 1;
  105. u8 bits_of_color_resolution = (gcm_info >> 4) & 7;
  106. printf("LogicalScreen: %dx%d\n", logical_screen.width, logical_screen.height);
  107. printf("global_color_map_follows_descriptor: %u\n", global_color_map_follows_descriptor);
  108. printf("bits_per_pixel: %u\n", bits_per_pixel);
  109. printf("bits_of_color_resolution: %u\n", bits_of_color_resolution);
  110. u8 background_color = 0;
  111. stream >> background_color;
  112. if (stream.handle_read_failure())
  113. return nullptr;
  114. printf("background_color: %u\n", background_color);
  115. u8 pixel_aspect_ratio = 0;
  116. stream >> pixel_aspect_ratio;
  117. if (stream.handle_read_failure())
  118. return nullptr;
  119. int color_map_entry_count = 1;
  120. for (int i = 0; i < bits_per_pixel; ++i)
  121. color_map_entry_count *= 2;
  122. printf("color_map_entry_count: %d\n", color_map_entry_count);
  123. for (int i = 0; i < color_map_entry_count; ++i) {
  124. stream >> logical_screen.color_map[i].r;
  125. stream >> logical_screen.color_map[i].g;
  126. stream >> logical_screen.color_map[i].b;
  127. }
  128. if (stream.handle_read_failure())
  129. return nullptr;
  130. for (int i = 0; i < color_map_entry_count; ++i) {
  131. auto& rgb = logical_screen.color_map[i];
  132. printf("[%02x]: %s\n", i, Color(rgb.r, rgb.g, rgb.b).to_string().characters());
  133. }
  134. NonnullOwnPtrVector<ImageDescriptor> images;
  135. for (;;) {
  136. u8 sentinel = 0;
  137. stream >> sentinel;
  138. printf("Sentinel: %02x\n", sentinel);
  139. if (sentinel == 0x21) {
  140. u8 extension_type = 0;
  141. stream >> extension_type;
  142. if (stream.handle_read_failure())
  143. return nullptr;
  144. printf("Extension block of type %02x\n", extension_type);
  145. u8 sub_block_length = 0;
  146. for (;;) {
  147. stream >> sub_block_length;
  148. if (stream.handle_read_failure())
  149. return nullptr;
  150. if (sub_block_length == 0)
  151. break;
  152. u8 dummy;
  153. for (u16 i = 0; i < sub_block_length; ++i)
  154. stream >> dummy;
  155. if (stream.handle_read_failure())
  156. return nullptr;
  157. }
  158. continue;
  159. }
  160. if (sentinel == 0x2c) {
  161. images.append(make<ImageDescriptor>());
  162. auto& image = images.last();
  163. u8 packed_fields;
  164. stream >> image.x;
  165. stream >> image.y;
  166. stream >> image.width;
  167. stream >> image.height;
  168. stream >> packed_fields;
  169. if (stream.handle_read_failure())
  170. return nullptr;
  171. printf("Image descriptor: %d,%d %dx%d, %02x\n", image.x, image.y, image.width, image.height, packed_fields);
  172. stream >> image.lzw_min_code_size;
  173. printf("min code size: %u\n", image.lzw_min_code_size);
  174. u8 lzw_encoded_bytes_expected = 0;
  175. for (;;) {
  176. stream >> lzw_encoded_bytes_expected;
  177. if (stream.handle_read_failure())
  178. return nullptr;
  179. if (lzw_encoded_bytes_expected == 0)
  180. break;
  181. u8 buffer[256];
  182. for (int i = 0; i < lzw_encoded_bytes_expected; ++i) {
  183. stream >> buffer[i];
  184. }
  185. if (stream.handle_read_failure())
  186. return nullptr;
  187. for (int i = 0; i < lzw_encoded_bytes_expected; ++i) {
  188. image.lzw_encoded_bytes.append(buffer[i]);
  189. }
  190. }
  191. continue;
  192. }
  193. if (sentinel == 0x3b) {
  194. printf("Trailer! Awesome :)\n");
  195. break;
  196. }
  197. return nullptr;
  198. }
  199. // We exited the block loop after finding a trailer. We should have everything needed.
  200. printf("Image count: %d\n", images.size());
  201. if (images.is_empty())
  202. return nullptr;
  203. for (int i = 0; i < images.size(); ++i) {
  204. auto& image = images.at(i);
  205. printf("Image %d: %d,%d %dx%d %d bytes LZW-encoded\n", i, image.x, image.y, image.width, image.height, image.lzw_encoded_bytes.size());
  206. // FIXME: Decode the LZW-encoded bytes and turn them into an image.
  207. }
  208. return nullptr;
  209. }
  210. }