ICOLoader.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 GenericTraits<Gfx::ICONDIR> {
  36. public:
  37. static constexpr bool is_trivially_serializable() { return true; }
  38. };
  39. template<>
  40. class AK::Traits<Gfx::ICONDIRENTRY> : public GenericTraits<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, Optional<size_t> index)
  122. {
  123. if (context.state < ICOLoadingContext::State::DirectoryDecoded)
  124. TRY(load_ico_directory(context));
  125. size_t real_index = context.largest_index;
  126. if (index.has_value())
  127. real_index = index.value();
  128. if (real_index >= context.images.size())
  129. return Error::from_string_literal("Index out of bounds");
  130. ICOImageDescriptor& desc = context.images[real_index];
  131. if (PNGImageDecoderPlugin::sniff({ context.data + desc.offset, desc.size })) {
  132. auto png_decoder = TRY(PNGImageDecoderPlugin::create({ context.data + desc.offset, desc.size }));
  133. TRY(png_decoder->initialize());
  134. auto decoded_png_frame = TRY(png_decoder->frame(0));
  135. if (!decoded_png_frame.image) {
  136. dbgln_if(ICO_DEBUG, "load_ico_bitmap: failed to load PNG encoded image index: {}", real_index);
  137. return Error::from_string_literal("Encoded image not null");
  138. }
  139. desc.bitmap = decoded_png_frame.image;
  140. return {};
  141. } else {
  142. auto bmp_decoder = TRY(BMPImageDecoderPlugin::create_as_included_in_ico({}, { context.data + desc.offset, desc.size }));
  143. // NOTE: We don't initialize a BMP decoder in the usual way, but rather
  144. // we just create an object and try to sniff for a frame when it's included
  145. // inside an ICO image.
  146. if (bmp_decoder->sniff_dib()) {
  147. auto decoded_bmp_frame = TRY(bmp_decoder->frame(0));
  148. if (!decoded_bmp_frame.image) {
  149. dbgln_if(ICO_DEBUG, "load_ico_bitmap: failed to load BMP encoded image index: {}", real_index);
  150. return Error::from_string_literal("Encoded image not null");
  151. }
  152. desc.bitmap = decoded_bmp_frame.image;
  153. } else {
  154. dbgln_if(ICO_DEBUG, "load_ico_bitmap: encoded image not supported at index: {}", real_index);
  155. return Error::from_string_literal("Encoded image not supported");
  156. }
  157. return {};
  158. }
  159. }
  160. bool ICOImageDecoderPlugin::sniff(ReadonlyBytes data)
  161. {
  162. FixedMemoryStream stream { data };
  163. return !decode_ico_header(stream).is_error();
  164. }
  165. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> ICOImageDecoderPlugin::create(ReadonlyBytes data)
  166. {
  167. return adopt_nonnull_own_or_enomem(new (nothrow) ICOImageDecoderPlugin(data.data(), data.size()));
  168. }
  169. ICOImageDecoderPlugin::ICOImageDecoderPlugin(u8 const* data, size_t size)
  170. {
  171. m_context = make<ICOLoadingContext>();
  172. m_context->data = data;
  173. m_context->data_size = size;
  174. }
  175. ICOImageDecoderPlugin::~ICOImageDecoderPlugin() = default;
  176. IntSize ICOImageDecoderPlugin::size()
  177. {
  178. if (m_context->state == ICOLoadingContext::State::Error) {
  179. return {};
  180. }
  181. if (m_context->state < ICOLoadingContext::State::DirectoryDecoded) {
  182. if (!load_ico_directory(*m_context).is_error()) {
  183. m_context->state = ICOLoadingContext::State::Error;
  184. return {};
  185. }
  186. m_context->state = ICOLoadingContext::State::DirectoryDecoded;
  187. }
  188. return { m_context->images[m_context->largest_index].width, m_context->images[m_context->largest_index].height };
  189. }
  190. ErrorOr<void> ICOImageDecoderPlugin::initialize()
  191. {
  192. FixedMemoryStream stream { { m_context->data, m_context->data_size } };
  193. TRY(decode_ico_header(stream));
  194. return {};
  195. }
  196. bool ICOImageDecoderPlugin::is_animated()
  197. {
  198. return false;
  199. }
  200. size_t ICOImageDecoderPlugin::loop_count()
  201. {
  202. return 0;
  203. }
  204. size_t ICOImageDecoderPlugin::frame_count()
  205. {
  206. return 1;
  207. }
  208. size_t ICOImageDecoderPlugin::first_animated_frame_index()
  209. {
  210. return 0;
  211. }
  212. ErrorOr<ImageFrameDescriptor> ICOImageDecoderPlugin::frame(size_t index, Optional<IntSize>)
  213. {
  214. if (index > 0)
  215. return Error::from_string_literal("ICOImageDecoderPlugin: Invalid frame index");
  216. if (m_context->state == ICOLoadingContext::State::Error)
  217. return Error::from_string_literal("ICOImageDecoderPlugin: Decoding failed");
  218. if (m_context->state < ICOLoadingContext::State::BitmapDecoded) {
  219. // NOTE: This forces the chunk decoding to happen.
  220. auto maybe_error = load_ico_bitmap(*m_context, {});
  221. if (maybe_error.is_error()) {
  222. m_context->state = ICOLoadingContext::State::Error;
  223. return Error::from_string_literal("ICOImageDecoderPlugin: Decoding failed");
  224. }
  225. m_context->state = ICOLoadingContext::State::BitmapDecoded;
  226. }
  227. VERIFY(m_context->images[m_context->largest_index].bitmap);
  228. return ImageFrameDescriptor { m_context->images[m_context->largest_index].bitmap, 0 };
  229. }
  230. ErrorOr<Optional<ReadonlyBytes>> ICOImageDecoderPlugin::icc_data()
  231. {
  232. return OptionalNone {};
  233. }
  234. }