GIFLoader.cpp 8.0 KB

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