GIFLoader.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Array.h>
  8. #include <AK/Debug.h>
  9. #include <AK/Endian.h>
  10. #include <AK/Error.h>
  11. #include <AK/IntegralMath.h>
  12. #include <AK/Memory.h>
  13. #include <AK/MemoryStream.h>
  14. #include <AK/Try.h>
  15. #include <LibGfx/ImageFormats/GIFLoader.h>
  16. #include <string.h>
  17. namespace Gfx {
  18. // Row strides and offsets for each interlace pass.
  19. static constexpr Array<int, 4> INTERLACE_ROW_STRIDES = { 8, 8, 4, 2 };
  20. static constexpr Array<int, 4> INTERLACE_ROW_OFFSETS = { 0, 4, 2, 1 };
  21. struct GIFImageDescriptor {
  22. u16 x { 0 };
  23. u16 y { 0 };
  24. u16 width { 0 };
  25. u16 height { 0 };
  26. bool use_global_color_map { true };
  27. bool interlaced { false };
  28. Color color_map[256];
  29. u8 lzw_min_code_size { 0 };
  30. Vector<u8> lzw_encoded_bytes;
  31. // Fields from optional graphic control extension block
  32. enum DisposalMethod : u8 {
  33. None = 0,
  34. InPlace = 1,
  35. RestoreBackground = 2,
  36. RestorePrevious = 3,
  37. };
  38. DisposalMethod disposal_method { None };
  39. u8 transparency_index { 0 };
  40. u16 duration { 0 };
  41. bool transparent { false };
  42. bool user_input { false };
  43. const IntRect rect() const
  44. {
  45. return { this->x, this->y, this->width, this->height };
  46. }
  47. };
  48. struct LogicalScreen {
  49. u16 width;
  50. u16 height;
  51. Color color_map[256];
  52. };
  53. struct GIFLoadingContext {
  54. GIFLoadingContext(FixedMemoryStream stream)
  55. : stream(move(stream))
  56. {
  57. }
  58. enum State {
  59. NotDecoded = 0,
  60. FrameDescriptorsLoaded,
  61. FrameComplete,
  62. };
  63. State state { NotDecoded };
  64. enum ErrorState {
  65. NoError = 0,
  66. FailedToDecodeAllFrames,
  67. FailedToDecodeAnyFrame,
  68. FailedToLoadFrameDescriptors,
  69. };
  70. ErrorState error_state { NoError };
  71. FixedMemoryStream stream;
  72. LogicalScreen logical_screen {};
  73. u8 background_color_index { 0 };
  74. Vector<NonnullOwnPtr<GIFImageDescriptor>> images {};
  75. size_t loops { 1 };
  76. RefPtr<Gfx::Bitmap> frame_buffer;
  77. size_t current_frame { 0 };
  78. RefPtr<Gfx::Bitmap> prev_frame_buffer;
  79. };
  80. enum class GIFFormat {
  81. GIF87a,
  82. GIF89a,
  83. };
  84. static ErrorOr<GIFFormat> decode_gif_header(Stream& stream)
  85. {
  86. static auto valid_header_87 = "GIF87a"sv;
  87. static auto valid_header_89 = "GIF89a"sv;
  88. Array<u8, 6> header;
  89. TRY(stream.read_until_filled(header));
  90. if (header.span() == valid_header_87.bytes())
  91. return GIFFormat::GIF87a;
  92. if (header.span() == valid_header_89.bytes())
  93. return GIFFormat::GIF89a;
  94. return Error::from_string_literal("GIF header unknown");
  95. }
  96. class LZWDecoder {
  97. private:
  98. static constexpr int max_code_size = 12;
  99. public:
  100. explicit LZWDecoder(Vector<u8> const& lzw_bytes, u8 min_code_size)
  101. : m_lzw_bytes(lzw_bytes)
  102. , m_code_size(min_code_size)
  103. , m_original_code_size(min_code_size)
  104. , m_table_capacity(AK::exp2<u32>(min_code_size))
  105. {
  106. init_code_table();
  107. }
  108. u16 add_control_code()
  109. {
  110. const u16 control_code = m_code_table.size();
  111. m_code_table.append(Vector<u8> {});
  112. m_original_code_table.append(Vector<u8> {});
  113. if (m_code_table.size() >= m_table_capacity && m_code_size < max_code_size) {
  114. ++m_code_size;
  115. ++m_original_code_size;
  116. m_table_capacity *= 2;
  117. }
  118. return control_code;
  119. }
  120. void reset()
  121. {
  122. m_code_table.clear();
  123. m_code_table.extend(m_original_code_table);
  124. m_code_size = m_original_code_size;
  125. m_table_capacity = AK::exp2<u32>(m_code_size);
  126. m_output.clear();
  127. }
  128. ErrorOr<u16> next_code()
  129. {
  130. size_t current_byte_index = m_current_bit_index / 8;
  131. if (current_byte_index >= m_lzw_bytes.size()) {
  132. return Error::from_string_literal("LZWDecoder tries to read ouf of bounds");
  133. }
  134. // Extract the code bits using a 32-bit mask to cover the possibility that if
  135. // the current code size > 9 bits then the code can span 3 bytes.
  136. u8 current_bit_offset = m_current_bit_index % 8;
  137. u32 mask = (u32)(m_table_capacity - 1) << current_bit_offset;
  138. // Make a padded copy of the final bytes in the data to ensure we don't read past the end.
  139. if (current_byte_index + sizeof(mask) > m_lzw_bytes.size()) {
  140. u8 padded_last_bytes[sizeof(mask)] = { 0 };
  141. for (int i = 0; current_byte_index + i < m_lzw_bytes.size(); ++i) {
  142. padded_last_bytes[i] = m_lzw_bytes[current_byte_index + i];
  143. }
  144. u32 const* addr = (u32 const*)&padded_last_bytes;
  145. m_current_code = (*addr & mask) >> current_bit_offset;
  146. } else {
  147. u32 tmp_word;
  148. memcpy(&tmp_word, &m_lzw_bytes.at(current_byte_index), sizeof(u32));
  149. m_current_code = (tmp_word & mask) >> current_bit_offset;
  150. }
  151. if (m_current_code > m_code_table.size()) {
  152. dbgln_if(GIF_DEBUG, "Corrupted LZW stream, invalid code: {} at bit index {}, code table size: {}",
  153. m_current_code,
  154. m_current_bit_index,
  155. m_code_table.size());
  156. return Error::from_string_literal("Corrupted LZW stream, invalid code");
  157. } else if (m_current_code == m_code_table.size() && m_output.is_empty()) {
  158. dbgln_if(GIF_DEBUG, "Corrupted LZW stream, valid new code but output buffer is empty: {} at bit index {}, code table size: {}",
  159. m_current_code,
  160. m_current_bit_index,
  161. m_code_table.size());
  162. return Error::from_string_literal("Corrupted LZW stream, valid new code but output buffer is empty");
  163. }
  164. m_current_bit_index += m_code_size;
  165. return m_current_code;
  166. }
  167. Vector<u8>& get_output()
  168. {
  169. VERIFY(m_current_code <= m_code_table.size());
  170. if (m_current_code < m_code_table.size()) {
  171. Vector<u8> new_entry = m_output;
  172. m_output = m_code_table.at(m_current_code);
  173. new_entry.append(m_output[0]);
  174. extend_code_table(new_entry);
  175. } else if (m_current_code == m_code_table.size()) {
  176. VERIFY(!m_output.is_empty());
  177. m_output.append(m_output[0]);
  178. extend_code_table(m_output);
  179. }
  180. return m_output;
  181. }
  182. private:
  183. void init_code_table()
  184. {
  185. m_code_table.ensure_capacity(m_table_capacity);
  186. for (u16 i = 0; i < m_table_capacity; ++i) {
  187. m_code_table.unchecked_append({ (u8)i });
  188. }
  189. m_original_code_table = m_code_table;
  190. }
  191. void extend_code_table(Vector<u8> const& entry)
  192. {
  193. if (entry.size() > 1 && m_code_table.size() < 4096) {
  194. m_code_table.append(entry);
  195. if (m_code_table.size() >= m_table_capacity && m_code_size < max_code_size) {
  196. ++m_code_size;
  197. m_table_capacity *= 2;
  198. }
  199. }
  200. }
  201. Vector<u8> const& m_lzw_bytes;
  202. int m_current_bit_index { 0 };
  203. Vector<Vector<u8>> m_code_table {};
  204. Vector<Vector<u8>> m_original_code_table {};
  205. u8 m_code_size { 0 };
  206. u8 m_original_code_size { 0 };
  207. u32 m_table_capacity { 0 };
  208. u16 m_current_code { 0 };
  209. Vector<u8> m_output {};
  210. };
  211. static void copy_frame_buffer(Bitmap& dest, Bitmap const& src)
  212. {
  213. VERIFY(dest.size_in_bytes() == src.size_in_bytes());
  214. memcpy(dest.scanline(0), src.scanline(0), dest.size_in_bytes());
  215. }
  216. static void clear_rect(Bitmap& bitmap, IntRect const& rect, Color color)
  217. {
  218. auto intersection_rect = rect.intersected(bitmap.rect());
  219. if (intersection_rect.is_empty())
  220. return;
  221. ARGB32* dst = bitmap.scanline(intersection_rect.top()) + intersection_rect.left();
  222. const size_t dst_skip = bitmap.pitch() / sizeof(ARGB32);
  223. for (int i = intersection_rect.height() - 1; i >= 0; --i) {
  224. fast_u32_fill(dst, color.value(), intersection_rect.width());
  225. dst += dst_skip;
  226. }
  227. }
  228. static ErrorOr<void> decode_frame(GIFLoadingContext& context, size_t frame_index)
  229. {
  230. if (frame_index >= context.images.size()) {
  231. return Error::from_string_literal("frame_index size too high");
  232. }
  233. if (context.state >= GIFLoadingContext::State::FrameComplete && frame_index == context.current_frame) {
  234. return {};
  235. }
  236. size_t start_frame = context.current_frame + 1;
  237. if (context.state < GIFLoadingContext::State::FrameComplete) {
  238. start_frame = 0;
  239. context.frame_buffer = TRY(Bitmap::create(BitmapFormat::BGRA8888, { context.logical_screen.width, context.logical_screen.height }));
  240. context.prev_frame_buffer = TRY(Bitmap::create(BitmapFormat::BGRA8888, { context.logical_screen.width, context.logical_screen.height }));
  241. } else if (frame_index < context.current_frame) {
  242. start_frame = 0;
  243. }
  244. for (size_t i = start_frame; i <= frame_index; ++i) {
  245. auto& image = context.images.at(i);
  246. auto const previous_image_disposal_method = i > 0 ? context.images.at(i - 1)->disposal_method : GIFImageDescriptor::DisposalMethod::None;
  247. if (i == 0) {
  248. context.frame_buffer->fill(Color::Transparent);
  249. } else if (i > 0 && image->disposal_method == GIFImageDescriptor::DisposalMethod::RestorePrevious
  250. && previous_image_disposal_method != GIFImageDescriptor::DisposalMethod::RestorePrevious) {
  251. // This marks the start of a run of frames that once disposed should be restored to the
  252. // previous underlying image contents. Therefore we make a copy of the current frame
  253. // buffer so that it can be restored later.
  254. copy_frame_buffer(*context.prev_frame_buffer, *context.frame_buffer);
  255. }
  256. if (previous_image_disposal_method == GIFImageDescriptor::DisposalMethod::RestoreBackground) {
  257. // Note: RestoreBackground could be interpreted either as restoring the underlying
  258. // background of the entire image (e.g. container element's background-color), or the
  259. // background color of the GIF itself. It appears that all major browsers and most other
  260. // GIF decoders adhere to the former interpretation, therefore we will do the same by
  261. // clearing the entire frame buffer to transparent.
  262. clear_rect(*context.frame_buffer, context.images[i - 1]->rect(), Color::Transparent);
  263. } else if (i > 0 && previous_image_disposal_method == GIFImageDescriptor::DisposalMethod::RestorePrevious) {
  264. // Previous frame indicated that once disposed, it should be restored to *its* previous
  265. // underlying image contents, therefore we restore the saved previous frame buffer.
  266. copy_frame_buffer(*context.frame_buffer, *context.prev_frame_buffer);
  267. }
  268. if (image->lzw_min_code_size > 8)
  269. return Error::from_string_literal("LZW minimum code size is greater than 8");
  270. LZWDecoder decoder(image->lzw_encoded_bytes, image->lzw_min_code_size);
  271. // Add GIF-specific control codes
  272. int const clear_code = decoder.add_control_code();
  273. int const end_of_information_code = decoder.add_control_code();
  274. auto const& color_map = image->use_global_color_map ? context.logical_screen.color_map : image->color_map;
  275. int pixel_index = 0;
  276. int row = 0;
  277. int interlace_pass = 0;
  278. while (true) {
  279. ErrorOr<u16> code = decoder.next_code();
  280. if (code.is_error()) {
  281. dbgln_if(GIF_DEBUG, "Unexpectedly reached end of gif frame data");
  282. return code.release_error();
  283. }
  284. if (code.value() == clear_code) {
  285. decoder.reset();
  286. continue;
  287. }
  288. if (code.value() == end_of_information_code)
  289. break;
  290. if (!image->width)
  291. continue;
  292. auto colors = decoder.get_output();
  293. for (auto const& color : colors) {
  294. auto c = color_map[color];
  295. int x = pixel_index % image->width + image->x;
  296. int y = row + image->y;
  297. if (context.frame_buffer->rect().contains(x, y) && (!image->transparent || color != image->transparency_index)) {
  298. context.frame_buffer->set_pixel(x, y, c);
  299. }
  300. ++pixel_index;
  301. if (pixel_index % image->width == 0) {
  302. if (image->interlaced) {
  303. if (interlace_pass < 4) {
  304. if (row + INTERLACE_ROW_STRIDES[interlace_pass] >= image->height) {
  305. ++interlace_pass;
  306. if (interlace_pass < 4)
  307. row = INTERLACE_ROW_OFFSETS[interlace_pass];
  308. } else {
  309. row += INTERLACE_ROW_STRIDES[interlace_pass];
  310. }
  311. }
  312. } else {
  313. ++row;
  314. }
  315. }
  316. }
  317. }
  318. context.current_frame = i;
  319. context.state = GIFLoadingContext::State::FrameComplete;
  320. }
  321. return {};
  322. }
  323. static ErrorOr<void> load_header_and_logical_screen(GIFLoadingContext& context)
  324. {
  325. if (TRY(context.stream.size()) < 32)
  326. return Error::from_string_literal("Size too short for GIF frame descriptors");
  327. TRY(decode_gif_header(context.stream));
  328. context.logical_screen.width = TRY(context.stream.read_value<LittleEndian<u16>>());
  329. context.logical_screen.height = TRY(context.stream.read_value<LittleEndian<u16>>());
  330. if (context.logical_screen.width > maximum_width_for_decoded_images || context.logical_screen.height > maximum_height_for_decoded_images) {
  331. dbgln("This GIF is too large for comfort: {}x{}", context.logical_screen.width, context.logical_screen.height);
  332. return Error::from_string_literal("This GIF is too large for comfort");
  333. }
  334. auto gcm_info = TRY(context.stream.read_value<u8>());
  335. context.background_color_index = TRY(context.stream.read_value<u8>());
  336. [[maybe_unused]] auto pixel_aspect_ratio = TRY(context.stream.read_value<u8>());
  337. u8 bits_per_pixel = (gcm_info & 7) + 1;
  338. int color_map_entry_count = 1;
  339. for (int i = 0; i < bits_per_pixel; ++i)
  340. color_map_entry_count *= 2;
  341. for (int i = 0; i < color_map_entry_count; ++i) {
  342. u8 r = TRY(context.stream.read_value<u8>());
  343. u8 g = TRY(context.stream.read_value<u8>());
  344. u8 b = TRY(context.stream.read_value<u8>());
  345. context.logical_screen.color_map[i] = { r, g, b };
  346. }
  347. return {};
  348. }
  349. static ErrorOr<void> load_gif_frame_descriptors(GIFLoadingContext& context)
  350. {
  351. NonnullOwnPtr<GIFImageDescriptor> current_image = make<GIFImageDescriptor>();
  352. for (;;) {
  353. u8 sentinel = TRY(context.stream.read_value<u8>());
  354. if (sentinel == '!') {
  355. u8 extension_type = TRY(context.stream.read_value<u8>());
  356. u8 sub_block_length = 0;
  357. Vector<u8> sub_block {};
  358. for (;;) {
  359. sub_block_length = TRY(context.stream.read_value<u8>());
  360. if (sub_block_length == 0)
  361. break;
  362. TRY(sub_block.try_resize(sub_block.size() + sub_block_length));
  363. TRY(context.stream.read_until_filled(sub_block.span().slice_from_end(sub_block_length)));
  364. }
  365. if (extension_type == 0xF9) {
  366. if (sub_block.size() != 4) {
  367. dbgln_if(GIF_DEBUG, "Unexpected graphic control size");
  368. continue;
  369. }
  370. u8 disposal_method = (sub_block[0] & 0x1C) >> 2;
  371. current_image->disposal_method = (GIFImageDescriptor::DisposalMethod)disposal_method;
  372. u8 user_input = (sub_block[0] & 0x2) >> 1;
  373. current_image->user_input = user_input == 1;
  374. u8 transparent = sub_block[0] & 1;
  375. current_image->transparent = transparent == 1;
  376. u16 duration = sub_block[1] + ((u16)sub_block[2] << 8);
  377. current_image->duration = duration;
  378. current_image->transparency_index = sub_block[3];
  379. }
  380. if (extension_type == 0xFF) {
  381. if (sub_block.size() != 14) {
  382. dbgln_if(GIF_DEBUG, "Unexpected application extension size: {}", sub_block.size());
  383. continue;
  384. }
  385. if (sub_block[11] != 1) {
  386. dbgln_if(GIF_DEBUG, "Unexpected application extension format");
  387. continue;
  388. }
  389. u16 loops = sub_block[12] + (sub_block[13] << 8);
  390. context.loops = loops;
  391. }
  392. continue;
  393. }
  394. if (sentinel == ',') {
  395. context.images.append(move(current_image));
  396. auto& image = context.images.last();
  397. image->x = TRY(context.stream.read_value<LittleEndian<u16>>());
  398. image->y = TRY(context.stream.read_value<LittleEndian<u16>>());
  399. image->width = TRY(context.stream.read_value<LittleEndian<u16>>());
  400. image->height = TRY(context.stream.read_value<LittleEndian<u16>>());
  401. auto packed_fields = TRY(context.stream.read_value<u8>());
  402. image->use_global_color_map = !(packed_fields & 0x80);
  403. image->interlaced = (packed_fields & 0x40) != 0;
  404. if (!image->use_global_color_map) {
  405. size_t local_color_table_size = AK::exp2<size_t>((packed_fields & 7) + 1);
  406. for (size_t i = 0; i < local_color_table_size; ++i) {
  407. u8 r = TRY(context.stream.read_value<u8>());
  408. u8 g = TRY(context.stream.read_value<u8>());
  409. u8 b = TRY(context.stream.read_value<u8>());
  410. image->color_map[i] = { r, g, b };
  411. }
  412. }
  413. image->lzw_min_code_size = TRY(context.stream.read_value<u8>());
  414. u8 lzw_encoded_bytes_expected = 0;
  415. for (;;) {
  416. lzw_encoded_bytes_expected = TRY(context.stream.read_value<u8>());
  417. if (lzw_encoded_bytes_expected == 0)
  418. break;
  419. Array<u8, 256> buffer;
  420. TRY(context.stream.read_until_filled(buffer.span().trim(lzw_encoded_bytes_expected)));
  421. for (int i = 0; i < lzw_encoded_bytes_expected; ++i) {
  422. image->lzw_encoded_bytes.append(buffer[i]);
  423. }
  424. }
  425. current_image = make<GIFImageDescriptor>();
  426. continue;
  427. }
  428. if (sentinel == ';') {
  429. break;
  430. }
  431. return Error::from_string_literal("Unexpected sentinel");
  432. }
  433. context.state = GIFLoadingContext::State::FrameDescriptorsLoaded;
  434. return {};
  435. }
  436. GIFImageDecoderPlugin::GIFImageDecoderPlugin(FixedMemoryStream stream)
  437. {
  438. m_context = make<GIFLoadingContext>(move(stream));
  439. }
  440. GIFImageDecoderPlugin::~GIFImageDecoderPlugin() = default;
  441. IntSize GIFImageDecoderPlugin::size()
  442. {
  443. return { m_context->logical_screen.width, m_context->logical_screen.height };
  444. }
  445. bool GIFImageDecoderPlugin::sniff(ReadonlyBytes data)
  446. {
  447. FixedMemoryStream stream { data };
  448. return !decode_gif_header(stream).is_error();
  449. }
  450. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> GIFImageDecoderPlugin::create(ReadonlyBytes data)
  451. {
  452. FixedMemoryStream stream { data };
  453. auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) GIFImageDecoderPlugin(move(stream))));
  454. TRY(load_header_and_logical_screen(*plugin->m_context));
  455. return plugin;
  456. }
  457. bool GIFImageDecoderPlugin::is_animated()
  458. {
  459. if (m_context->error_state != GIFLoadingContext::ErrorState::NoError) {
  460. return false;
  461. }
  462. if (m_context->state < GIFLoadingContext::State::FrameDescriptorsLoaded) {
  463. if (load_gif_frame_descriptors(*m_context).is_error()) {
  464. m_context->error_state = GIFLoadingContext::ErrorState::FailedToLoadFrameDescriptors;
  465. return false;
  466. }
  467. }
  468. return m_context->images.size() > 1;
  469. }
  470. size_t GIFImageDecoderPlugin::loop_count()
  471. {
  472. if (m_context->error_state != GIFLoadingContext::ErrorState::NoError) {
  473. return 0;
  474. }
  475. if (m_context->state < GIFLoadingContext::State::FrameDescriptorsLoaded) {
  476. if (load_gif_frame_descriptors(*m_context).is_error()) {
  477. m_context->error_state = GIFLoadingContext::ErrorState::FailedToLoadFrameDescriptors;
  478. return 0;
  479. }
  480. }
  481. return m_context->loops;
  482. }
  483. size_t GIFImageDecoderPlugin::frame_count()
  484. {
  485. if (m_context->error_state != GIFLoadingContext::ErrorState::NoError) {
  486. return 1;
  487. }
  488. if (m_context->state < GIFLoadingContext::State::FrameDescriptorsLoaded) {
  489. if (load_gif_frame_descriptors(*m_context).is_error()) {
  490. m_context->error_state = GIFLoadingContext::ErrorState::FailedToLoadFrameDescriptors;
  491. return 1;
  492. }
  493. }
  494. return m_context->images.size();
  495. }
  496. size_t GIFImageDecoderPlugin::first_animated_frame_index()
  497. {
  498. return 0;
  499. }
  500. ErrorOr<ImageFrameDescriptor> GIFImageDecoderPlugin::frame(size_t index, Optional<IntSize>)
  501. {
  502. if (m_context->error_state >= GIFLoadingContext::ErrorState::FailedToDecodeAnyFrame) {
  503. return Error::from_string_literal("GIFImageDecoderPlugin: Decoding failed");
  504. }
  505. if (m_context->state < GIFLoadingContext::State::FrameDescriptorsLoaded) {
  506. if (auto result = load_gif_frame_descriptors(*m_context); result.is_error()) {
  507. m_context->error_state = GIFLoadingContext::ErrorState::FailedToLoadFrameDescriptors;
  508. return result.release_error();
  509. }
  510. }
  511. if (m_context->error_state == GIFLoadingContext::ErrorState::NoError) {
  512. if (auto result = decode_frame(*m_context, index); result.is_error()) {
  513. if (m_context->state < GIFLoadingContext::State::FrameComplete) {
  514. m_context->error_state = GIFLoadingContext::ErrorState::FailedToDecodeAnyFrame;
  515. return result.release_error();
  516. }
  517. if (auto result = decode_frame(*m_context, 0); result.is_error()) {
  518. m_context->error_state = GIFLoadingContext::ErrorState::FailedToDecodeAnyFrame;
  519. return result.release_error();
  520. }
  521. m_context->error_state = GIFLoadingContext::ErrorState::FailedToDecodeAllFrames;
  522. }
  523. }
  524. ImageFrameDescriptor frame {};
  525. frame.image = TRY(m_context->frame_buffer->clone());
  526. frame.duration = m_context->images[index]->duration * 10;
  527. if (frame.duration <= 10) {
  528. frame.duration = 100;
  529. }
  530. return frame;
  531. }
  532. ErrorOr<Optional<ReadonlyBytes>> GIFImageDecoderPlugin::icc_data()
  533. {
  534. return OptionalNone {};
  535. }
  536. }