ICOLoader.cpp 8.9 KB

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