GIFLoader.cpp 6.6 KB

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