ICOLoader.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. struct ICOImageDescriptor {
  35. u16 width;
  36. u16 height;
  37. u16 bits_per_pixel;
  38. size_t offset;
  39. size_t size;
  40. RefPtr<Gfx::Bitmap> bitmap;
  41. };
  42. struct ICOLoadingContext {
  43. enum State {
  44. NotDecoded = 0,
  45. Error,
  46. DirectoryDecoded,
  47. BitmapDecoded
  48. };
  49. State state { NotDecoded };
  50. u8 const* data { nullptr };
  51. size_t data_size { 0 };
  52. Vector<ICOImageDescriptor> images;
  53. size_t largest_index;
  54. };
  55. static Optional<size_t> decode_ico_header(InputMemoryStream& stream)
  56. {
  57. ICONDIR header;
  58. stream >> Bytes { &header, sizeof(header) };
  59. if (stream.handle_any_error())
  60. return {};
  61. if (header.must_be_0 != 0 || header.must_be_1 != 1)
  62. return {};
  63. return { header.image_count };
  64. }
  65. static Optional<ICOImageDescriptor> decode_ico_direntry(InputMemoryStream& stream)
  66. {
  67. ICONDIRENTRY entry;
  68. stream >> Bytes { &entry, sizeof(entry) };
  69. if (stream.handle_any_error())
  70. return {};
  71. ICOImageDescriptor desc = { entry.width, entry.height, entry.bits_per_pixel, entry.offset, entry.size, nullptr };
  72. if (desc.width == 0)
  73. desc.width = 256;
  74. if (desc.height == 0)
  75. desc.height = 256;
  76. return { desc };
  77. }
  78. static size_t find_largest_image(ICOLoadingContext const& context)
  79. {
  80. size_t max_area = 0;
  81. size_t index = 0;
  82. size_t largest_index = 0;
  83. u16 max_bits_per_pixel = 0;
  84. for (auto const& desc : context.images) {
  85. if (static_cast<size_t>(desc.width) * static_cast<size_t>(desc.height) >= max_area) {
  86. if (desc.bits_per_pixel > max_bits_per_pixel) {
  87. max_area = desc.width * desc.height;
  88. largest_index = index;
  89. max_bits_per_pixel = desc.bits_per_pixel;
  90. }
  91. }
  92. ++index;
  93. }
  94. return largest_index;
  95. }
  96. static bool load_ico_directory(ICOLoadingContext& context)
  97. {
  98. InputMemoryStream stream { { context.data, context.data_size } };
  99. auto image_count = decode_ico_header(stream);
  100. if (!image_count.has_value() || image_count.value() == 0) {
  101. return false;
  102. }
  103. for (size_t i = 0; i < image_count.value(); ++i) {
  104. auto maybe_desc = decode_ico_direntry(stream);
  105. if (!maybe_desc.has_value()) {
  106. dbgln_if(ICO_DEBUG, "load_ico_directory: error loading entry: {}", i);
  107. return false;
  108. }
  109. auto& desc = maybe_desc.value();
  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 false;
  114. }
  115. dbgln_if(ICO_DEBUG, "load_ico_directory: index {} width: {} height: {} offset: {} size: {}", i, desc.width, desc.height, desc.offset, desc.size);
  116. context.images.append(desc);
  117. }
  118. context.largest_index = find_largest_image(context);
  119. context.state = ICOLoadingContext::State::DirectoryDecoded;
  120. return true;
  121. }
  122. bool ICOImageDecoderPlugin::load_ico_bitmap(ICOLoadingContext& context, Optional<size_t> index)
  123. {
  124. if (context.state < ICOLoadingContext::State::DirectoryDecoded) {
  125. if (!load_ico_directory(context)) {
  126. context.state = ICOLoadingContext::State::Error;
  127. return false;
  128. }
  129. context.state = ICOLoadingContext::State::DirectoryDecoded;
  130. }
  131. size_t real_index = context.largest_index;
  132. if (index.has_value())
  133. real_index = index.value();
  134. if (real_index >= context.images.size()) {
  135. return false;
  136. }
  137. ICOImageDescriptor& desc = context.images[real_index];
  138. if (PNGImageDecoderPlugin::sniff({ context.data + desc.offset, desc.size }).release_value_but_fixme_should_propagate_errors()) {
  139. auto png_decoder = PNGImageDecoderPlugin::create({ context.data + desc.offset, desc.size }).release_value_but_fixme_should_propagate_errors();
  140. if (png_decoder->initialize()) {
  141. auto decoded_png_frame = png_decoder->frame(0);
  142. if (decoded_png_frame.is_error() || !decoded_png_frame.value().image) {
  143. dbgln_if(ICO_DEBUG, "load_ico_bitmap: failed to load PNG encoded image index: {}", real_index);
  144. return false;
  145. }
  146. desc.bitmap = decoded_png_frame.value().image;
  147. return true;
  148. }
  149. return false;
  150. } else {
  151. auto bmp_decoder = BMPImageDecoderPlugin::create_as_included_in_ico({}, { context.data + desc.offset, desc.size }).release_value_but_fixme_should_propagate_errors();
  152. // NOTE: We don't initialize a BMP decoder in the usual way, but rather
  153. // we just create an object and try to sniff for a frame when it's included
  154. // inside an ICO image.
  155. if (bmp_decoder->sniff_dib()) {
  156. auto decoded_bmp_frame = bmp_decoder->frame(0);
  157. if (decoded_bmp_frame.is_error() || !decoded_bmp_frame.value().image) {
  158. dbgln_if(ICO_DEBUG, "load_ico_bitmap: failed to load BMP encoded image index: {}", real_index);
  159. return false;
  160. }
  161. desc.bitmap = decoded_bmp_frame.value().image;
  162. } else {
  163. dbgln_if(ICO_DEBUG, "load_ico_bitmap: encoded image not supported at index: {}", real_index);
  164. return false;
  165. }
  166. return true;
  167. }
  168. }
  169. ErrorOr<bool> ICOImageDecoderPlugin::sniff(ReadonlyBytes data)
  170. {
  171. InputMemoryStream stream { { data.data(), data.size() } };
  172. return decode_ico_header(stream).has_value();
  173. }
  174. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> ICOImageDecoderPlugin::create(ReadonlyBytes data)
  175. {
  176. return adopt_nonnull_own_or_enomem(new (nothrow) ICOImageDecoderPlugin(data.data(), data.size()));
  177. }
  178. ICOImageDecoderPlugin::ICOImageDecoderPlugin(u8 const* data, size_t size)
  179. {
  180. m_context = make<ICOLoadingContext>();
  181. m_context->data = data;
  182. m_context->data_size = size;
  183. }
  184. ICOImageDecoderPlugin::~ICOImageDecoderPlugin() = default;
  185. IntSize ICOImageDecoderPlugin::size()
  186. {
  187. if (m_context->state == ICOLoadingContext::State::Error) {
  188. return {};
  189. }
  190. if (m_context->state < ICOLoadingContext::State::DirectoryDecoded) {
  191. if (!load_ico_directory(*m_context)) {
  192. m_context->state = ICOLoadingContext::State::Error;
  193. return {};
  194. }
  195. m_context->state = ICOLoadingContext::State::DirectoryDecoded;
  196. }
  197. return { m_context->images[m_context->largest_index].width, m_context->images[m_context->largest_index].height };
  198. }
  199. void ICOImageDecoderPlugin::set_volatile()
  200. {
  201. if (m_context->images[0].bitmap)
  202. m_context->images[0].bitmap->set_volatile();
  203. }
  204. bool ICOImageDecoderPlugin::set_nonvolatile(bool& was_purged)
  205. {
  206. if (!m_context->images[0].bitmap)
  207. return false;
  208. return m_context->images[0].bitmap->set_nonvolatile(was_purged);
  209. }
  210. bool ICOImageDecoderPlugin::initialize()
  211. {
  212. InputMemoryStream stream { { m_context->data, m_context->data_size } };
  213. return decode_ico_header(stream).has_value();
  214. }
  215. bool ICOImageDecoderPlugin::is_animated()
  216. {
  217. return false;
  218. }
  219. size_t ICOImageDecoderPlugin::loop_count()
  220. {
  221. return 0;
  222. }
  223. size_t ICOImageDecoderPlugin::frame_count()
  224. {
  225. return 1;
  226. }
  227. ErrorOr<ImageFrameDescriptor> ICOImageDecoderPlugin::frame(size_t index)
  228. {
  229. if (index > 0)
  230. return Error::from_string_literal("ICOImageDecoderPlugin: Invalid frame index");
  231. if (m_context->state == ICOLoadingContext::State::Error)
  232. return Error::from_string_literal("ICOImageDecoderPlugin: Decoding failed");
  233. if (m_context->state < ICOLoadingContext::State::BitmapDecoded) {
  234. // NOTE: This forces the chunk decoding to happen.
  235. bool success = load_ico_bitmap(*m_context, {});
  236. if (!success) {
  237. m_context->state = ICOLoadingContext::State::Error;
  238. return Error::from_string_literal("ICOImageDecoderPlugin: Decoding failed");
  239. }
  240. m_context->state = ICOLoadingContext::State::BitmapDecoded;
  241. }
  242. VERIFY(m_context->images[m_context->largest_index].bitmap);
  243. return ImageFrameDescriptor { m_context->images[m_context->largest_index].bitmap, 0 };
  244. }
  245. }