ICOLoader.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright (c) 2020, Paul Roukema <roukemap@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ByteBuffer.h>
  7. #include <AK/Debug.h>
  8. #include <AK/MemoryStream.h>
  9. #include <AK/Types.h>
  10. #include <LibGfx/ImageFormats/BMPLoader.h>
  11. #include <LibGfx/ImageFormats/ICOLoader.h>
  12. #include <LibGfx/ImageFormats/PNGLoader.h>
  13. #include <string.h>
  14. namespace Gfx {
  15. // FIXME: This is in little-endian order. Maybe need a NetworkOrdered<T> equivalent eventually.
  16. struct ICONDIR {
  17. u16 must_be_0 = 0;
  18. u16 must_be_1 = 0;
  19. u16 image_count = 0;
  20. };
  21. static_assert(AssertSize<ICONDIR, 6>());
  22. struct ICONDIRENTRY {
  23. u8 width;
  24. u8 height;
  25. u8 color_count;
  26. u8 reserved_0;
  27. u16 planes;
  28. u16 bits_per_pixel;
  29. u32 size;
  30. u32 offset;
  31. };
  32. static_assert(AssertSize<ICONDIRENTRY, 16>());
  33. };
  34. template<>
  35. class AK::Traits<Gfx::ICONDIR> : public DefaultTraits<Gfx::ICONDIR> {
  36. public:
  37. static constexpr bool is_trivially_serializable() { return true; }
  38. };
  39. template<>
  40. class AK::Traits<Gfx::ICONDIRENTRY> : public DefaultTraits<Gfx::ICONDIRENTRY> {
  41. public:
  42. static constexpr bool is_trivially_serializable() { return true; }
  43. };
  44. namespace Gfx {
  45. struct ICOImageDescriptor {
  46. u16 width;
  47. u16 height;
  48. u16 bits_per_pixel;
  49. size_t offset;
  50. size_t size;
  51. RefPtr<Gfx::Bitmap> bitmap;
  52. };
  53. struct ICOLoadingContext {
  54. enum State {
  55. NotDecoded = 0,
  56. Error,
  57. DirectoryDecoded,
  58. BitmapDecoded
  59. };
  60. State state { NotDecoded };
  61. u8 const* data { nullptr };
  62. size_t data_size { 0 };
  63. Vector<ICOImageDescriptor> images;
  64. size_t largest_index;
  65. };
  66. static ErrorOr<size_t> decode_ico_header(Stream& stream)
  67. {
  68. auto header = TRY(stream.read_value<ICONDIR>());
  69. if (header.must_be_0 != 0 || header.must_be_1 != 1)
  70. return Error::from_string_literal("Invalid ICO header");
  71. return { header.image_count };
  72. }
  73. static ErrorOr<ICOImageDescriptor> decode_ico_direntry(Stream& stream)
  74. {
  75. auto entry = TRY(stream.read_value<ICONDIRENTRY>());
  76. ICOImageDescriptor desc = { entry.width, entry.height, entry.bits_per_pixel, entry.offset, entry.size, nullptr };
  77. if (desc.width == 0)
  78. desc.width = 256;
  79. if (desc.height == 0)
  80. desc.height = 256;
  81. return { desc };
  82. }
  83. static size_t find_largest_image(ICOLoadingContext const& context)
  84. {
  85. size_t max_area = 0;
  86. size_t index = 0;
  87. size_t largest_index = 0;
  88. u16 max_bits_per_pixel = 0;
  89. for (auto const& desc : context.images) {
  90. if (static_cast<size_t>(desc.width) * static_cast<size_t>(desc.height) >= max_area) {
  91. if (desc.bits_per_pixel > max_bits_per_pixel) {
  92. max_area = desc.width * desc.height;
  93. largest_index = index;
  94. max_bits_per_pixel = desc.bits_per_pixel;
  95. }
  96. }
  97. ++index;
  98. }
  99. return largest_index;
  100. }
  101. static ErrorOr<void> load_ico_directory(ICOLoadingContext& context)
  102. {
  103. FixedMemoryStream stream { { context.data, context.data_size } };
  104. auto image_count = TRY(decode_ico_header(stream));
  105. if (image_count == 0)
  106. return Error::from_string_literal("ICO file has no images");
  107. for (size_t i = 0; i < image_count; ++i) {
  108. auto desc = TRY(decode_ico_direntry(stream));
  109. if (desc.offset + desc.size < desc.offset // detect integer overflow
  110. || (desc.offset + desc.size) > context.data_size) {
  111. dbgln_if(ICO_DEBUG, "load_ico_directory: offset: {} size: {} doesn't fit in ICO size: {}", desc.offset, desc.size, context.data_size);
  112. return Error::from_string_literal("ICO size too large");
  113. }
  114. dbgln_if(ICO_DEBUG, "load_ico_directory: index {} width: {} height: {} offset: {} size: {}", i, desc.width, desc.height, desc.offset, desc.size);
  115. TRY(context.images.try_append(desc));
  116. }
  117. context.largest_index = find_largest_image(context);
  118. context.state = ICOLoadingContext::State::DirectoryDecoded;
  119. return {};
  120. }
  121. ErrorOr<void> ICOImageDecoderPlugin::load_ico_bitmap(ICOLoadingContext& context)
  122. {
  123. VERIFY(context.state >= ICOLoadingContext::State::DirectoryDecoded);
  124. size_t const real_index = context.largest_index;
  125. if (real_index >= context.images.size())
  126. return Error::from_string_literal("Index out of bounds");
  127. ICOImageDescriptor& desc = context.images[real_index];
  128. if (PNGImageDecoderPlugin::sniff({ context.data + desc.offset, desc.size })) {
  129. auto png_decoder = TRY(PNGImageDecoderPlugin::create({ context.data + desc.offset, desc.size }));
  130. auto decoded_png_frame = TRY(png_decoder->frame(0));
  131. if (!decoded_png_frame.image) {
  132. dbgln_if(ICO_DEBUG, "load_ico_bitmap: failed to load PNG encoded image index: {}", real_index);
  133. return Error::from_string_literal("Encoded image not null");
  134. }
  135. desc.bitmap = decoded_png_frame.image;
  136. return {};
  137. } else {
  138. auto bmp_decoder = TRY(BMPImageDecoderPlugin::create_as_included_in_ico({}, { context.data + desc.offset, desc.size }));
  139. // NOTE: We don't initialize a BMP decoder in the usual way, but rather
  140. // we just create an object and try to sniff for a frame when it's included
  141. // inside an ICO image.
  142. if (bmp_decoder->sniff_dib()) {
  143. auto decoded_bmp_frame = TRY(bmp_decoder->frame(0));
  144. if (!decoded_bmp_frame.image) {
  145. dbgln_if(ICO_DEBUG, "load_ico_bitmap: failed to load BMP encoded image index: {}", real_index);
  146. return Error::from_string_literal("Encoded image not null");
  147. }
  148. desc.bitmap = decoded_bmp_frame.image;
  149. } else {
  150. dbgln_if(ICO_DEBUG, "load_ico_bitmap: encoded image not supported at index: {}", real_index);
  151. return Error::from_string_literal("Encoded image not supported");
  152. }
  153. return {};
  154. }
  155. }
  156. bool ICOImageDecoderPlugin::sniff(ReadonlyBytes data)
  157. {
  158. FixedMemoryStream stream { data };
  159. return !decode_ico_header(stream).is_error();
  160. }
  161. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> ICOImageDecoderPlugin::create(ReadonlyBytes data)
  162. {
  163. auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) ICOImageDecoderPlugin(data.data(), data.size())));
  164. TRY(load_ico_directory(*plugin->m_context));
  165. return plugin;
  166. }
  167. ICOImageDecoderPlugin::ICOImageDecoderPlugin(u8 const* data, size_t size)
  168. {
  169. m_context = make<ICOLoadingContext>();
  170. m_context->data = data;
  171. m_context->data_size = size;
  172. }
  173. ICOImageDecoderPlugin::~ICOImageDecoderPlugin() = default;
  174. IntSize ICOImageDecoderPlugin::size()
  175. {
  176. return { m_context->images[m_context->largest_index].width, m_context->images[m_context->largest_index].height };
  177. }
  178. ErrorOr<ImageFrameDescriptor> ICOImageDecoderPlugin::frame(size_t index, Optional<IntSize>)
  179. {
  180. if (index > 0)
  181. return Error::from_string_literal("ICOImageDecoderPlugin: Invalid frame index");
  182. if (m_context->state == ICOLoadingContext::State::Error)
  183. return Error::from_string_literal("ICOImageDecoderPlugin: Decoding failed");
  184. if (m_context->state < ICOLoadingContext::State::BitmapDecoded) {
  185. // NOTE: This forces the chunk decoding to happen.
  186. auto maybe_error = load_ico_bitmap(*m_context);
  187. if (maybe_error.is_error()) {
  188. m_context->state = ICOLoadingContext::State::Error;
  189. return Error::from_string_literal("ICOImageDecoderPlugin: Decoding failed");
  190. }
  191. m_context->state = ICOLoadingContext::State::BitmapDecoded;
  192. }
  193. VERIFY(m_context->images[m_context->largest_index].bitmap);
  194. return ImageFrameDescriptor { m_context->images[m_context->largest_index].bitmap, 0 };
  195. }
  196. }