TinyVGLoader.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*
  2. * Copyright (c) 2023, MacDue <macdue@dueutil.tech>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Array.h>
  7. #include <AK/Endian.h>
  8. #include <AK/FixedArray.h>
  9. #include <AK/LEB128.h>
  10. #include <AK/MemoryStream.h>
  11. #include <AK/Variant.h>
  12. #include <LibCore/File.h>
  13. #include <LibGfx/AntiAliasingPainter.h>
  14. #include <LibGfx/ImageFormats/TinyVGLoader.h>
  15. #include <LibGfx/Line.h>
  16. #include <LibGfx/Painter.h>
  17. #include <LibGfx/Point.h>
  18. namespace Gfx {
  19. using VarUInt = LEB128<u32>;
  20. static constexpr Array<u8, 2> TVG_MAGIC { 0x72, 0x56 };
  21. enum class ColorEncoding : u8 {
  22. RGBA8888 = 0,
  23. RGB565 = 1,
  24. RGBAF32 = 2,
  25. Custom = 3
  26. };
  27. enum class CoordinateRange : u8 {
  28. Default = 0,
  29. Reduced = 1,
  30. Enhanced = 2
  31. };
  32. enum class StyleType : u8 {
  33. FlatColored = 0,
  34. LinearGradient = 1,
  35. RadialGradinet = 2
  36. };
  37. enum class Command : u8 {
  38. EndOfDocument = 0,
  39. FillPolygon = 1,
  40. FillRectangles = 2,
  41. FillPath = 3,
  42. DrawLines = 4,
  43. DrawLineLoop = 5,
  44. DrawLineStrip = 6,
  45. DrawLinePath = 7,
  46. OutlineFillPolygon = 8,
  47. OutlineFillRectangles = 9,
  48. OutLineFillPath = 10
  49. };
  50. struct FillCommandHeader {
  51. u32 count;
  52. TinyVGDecodedImageData::Style style;
  53. };
  54. struct DrawCommandHeader {
  55. u32 count;
  56. TinyVGDecodedImageData::Style line_style;
  57. float line_width;
  58. };
  59. struct OutlineFillCommandHeader {
  60. u32 count;
  61. TinyVGDecodedImageData::Style fill_style;
  62. TinyVGDecodedImageData::Style line_style;
  63. float line_width;
  64. };
  65. enum class PathCommand : u8 {
  66. Line = 0,
  67. HorizontalLine = 1,
  68. VerticalLine = 2,
  69. CubicBezier = 3,
  70. ArcCircle = 4,
  71. ArcEllipse = 5,
  72. ClosePath = 6,
  73. QuadraticBezier = 7
  74. };
  75. struct TinyVGHeader {
  76. u8 version;
  77. u8 scale;
  78. ColorEncoding color_encoding;
  79. CoordinateRange coordinate_range;
  80. u32 width;
  81. u32 height;
  82. u32 color_count;
  83. };
  84. static ErrorOr<TinyVGHeader> decode_tinyvg_header(Stream& stream)
  85. {
  86. TinyVGHeader header {};
  87. Array<u8, 2> magic_bytes;
  88. TRY(stream.read_until_filled(magic_bytes));
  89. if (magic_bytes != TVG_MAGIC)
  90. return Error::from_string_literal("Invalid TinyVG: Incorrect header magic");
  91. header.version = TRY(stream.read_value<u8>());
  92. u8 properties = TRY(stream.read_value<u8>());
  93. header.scale = properties & 0xF;
  94. header.color_encoding = static_cast<ColorEncoding>((properties >> 4) & 0x3);
  95. header.coordinate_range = static_cast<CoordinateRange>((properties >> 6) & 0x3);
  96. switch (header.coordinate_range) {
  97. case CoordinateRange::Default:
  98. header.width = TRY(stream.read_value<LittleEndian<u16>>());
  99. header.height = TRY(stream.read_value<LittleEndian<u16>>());
  100. break;
  101. case CoordinateRange::Reduced:
  102. header.width = TRY(stream.read_value<u8>());
  103. header.height = TRY(stream.read_value<u8>());
  104. break;
  105. case CoordinateRange::Enhanced:
  106. header.width = TRY(stream.read_value<LittleEndian<u32>>());
  107. header.height = TRY(stream.read_value<LittleEndian<u32>>());
  108. break;
  109. default:
  110. return Error::from_string_literal("Invalid TinyVG: Bad coordinate range");
  111. }
  112. header.color_count = TRY(stream.read_value<VarUInt>());
  113. return header;
  114. }
  115. static ErrorOr<FixedArray<Color>> decode_color_table(Stream& stream, ColorEncoding encoding, u32 color_count)
  116. {
  117. if (encoding == ColorEncoding::Custom)
  118. return Error::from_string_literal("Invalid TinyVG: Unsupported color encoding");
  119. auto color_table = TRY(FixedArray<Color>::create(color_count));
  120. auto parse_color = [&]() -> ErrorOr<Color> {
  121. switch (encoding) {
  122. case ColorEncoding::RGBA8888: {
  123. Array<u8, 4> rgba;
  124. TRY(stream.read_until_filled(rgba));
  125. return Color(rgba[0], rgba[1], rgba[2], rgba[3]);
  126. }
  127. case ColorEncoding::RGB565: {
  128. u16 color = TRY(stream.read_value<LittleEndian<u16>>());
  129. auto red = (color >> (6 + 5)) & 0x1f;
  130. auto green = (color >> 5) & 0x3f;
  131. auto blue = (color >> 0) & 0x1f;
  132. return Color((red * 255 + 15) / 31, (green * 255 + 31), (blue * 255 + 15) / 31);
  133. }
  134. case ColorEncoding::RGBAF32: {
  135. auto red = TRY(stream.read_value<LittleEndian<f32>>());
  136. auto green = TRY(stream.read_value<LittleEndian<f32>>());
  137. auto blue = TRY(stream.read_value<LittleEndian<f32>>());
  138. auto alpha = TRY(stream.read_value<LittleEndian<f32>>());
  139. return Color(red * 255, green * 255, blue * 255, alpha * 255);
  140. }
  141. default:
  142. return Error::from_string_literal("Invalid TinyVG: Bad color encoding");
  143. }
  144. };
  145. for (auto& color : color_table) {
  146. color = TRY(parse_color());
  147. }
  148. return color_table;
  149. }
  150. class TinyVGReader {
  151. public:
  152. TinyVGReader(Stream& stream, TinyVGHeader const& header, ReadonlySpan<Color> color_table)
  153. : m_stream(stream)
  154. , m_scale(powf(0.5, header.scale))
  155. , m_coordinate_range(header.coordinate_range)
  156. , m_color_table(color_table)
  157. {
  158. }
  159. ErrorOr<float> read_unit()
  160. {
  161. auto read_value = [&]() -> ErrorOr<i32> {
  162. switch (m_coordinate_range) {
  163. case CoordinateRange::Default:
  164. return TRY(m_stream.read_value<LittleEndian<i16>>());
  165. case CoordinateRange::Reduced:
  166. return TRY(m_stream.read_value<i8>());
  167. case CoordinateRange::Enhanced:
  168. return TRY(m_stream.read_value<LittleEndian<i32>>());
  169. default:
  170. // Note: Already checked while reading the header.
  171. VERIFY_NOT_REACHED();
  172. }
  173. };
  174. return TRY(read_value()) * m_scale;
  175. }
  176. ErrorOr<u32> read_var_uint()
  177. {
  178. return TRY(m_stream.read_value<VarUInt>());
  179. }
  180. ErrorOr<FloatPoint> read_point()
  181. {
  182. return FloatPoint { TRY(read_unit()), TRY(read_unit()) };
  183. }
  184. ErrorOr<TinyVGDecodedImageData::Style> read_style(StyleType type)
  185. {
  186. auto read_color = [&]() -> ErrorOr<Color> {
  187. auto color_index = TRY(m_stream.read_value<VarUInt>());
  188. return m_color_table[color_index];
  189. };
  190. switch (type) {
  191. case StyleType::FlatColored: {
  192. return TRY(read_color());
  193. }
  194. case StyleType::LinearGradient:
  195. case StyleType::RadialGradinet: {
  196. // TODO: Make PaintStyle (for these ultra basic gradients)
  197. [[maybe_unused]] auto point_0 = TRY(read_point());
  198. [[maybe_unused]] auto point_1 = TRY(read_point());
  199. [[maybe_unused]] auto color_0 = TRY(read_color());
  200. [[maybe_unused]] auto color_1 = TRY(read_color());
  201. return Color(Color::Black);
  202. }
  203. }
  204. return Error::from_string_literal("Invalid TinyVG: Bad style data");
  205. }
  206. ErrorOr<FloatRect> read_rectangle()
  207. {
  208. return FloatRect { TRY(read_unit()), TRY(read_unit()), TRY(read_unit()), TRY(read_unit()) };
  209. }
  210. ErrorOr<FloatLine> read_line()
  211. {
  212. return FloatLine { TRY(read_point()), TRY(read_point()) };
  213. }
  214. ErrorOr<Path> read_path(u32 segment_count)
  215. {
  216. Path path;
  217. auto segment_lengths = TRY(FixedArray<u32>::create(segment_count));
  218. for (auto& command_count : segment_lengths) {
  219. command_count = TRY(read_var_uint()) + 1;
  220. }
  221. for (auto command_count : segment_lengths) {
  222. auto start_point = TRY(read_point());
  223. path.move_to(start_point);
  224. for (u32 i = 0; i < command_count; i++) {
  225. u8 command_tag = TRY(m_stream.read_value<u8>());
  226. auto path_command = static_cast<PathCommand>(command_tag & 0x7);
  227. bool has_line_width = (command_tag >> 4) & 0b1;
  228. if (has_line_width) {
  229. // FIXME: TinyVG allows changing the line width within a path.
  230. // This is not supported in LibGfx, so we currently ignore this.
  231. (void)TRY(read_unit());
  232. }
  233. switch (path_command) {
  234. case PathCommand::Line:
  235. path.line_to(TRY(read_point()));
  236. break;
  237. case PathCommand::HorizontalLine:
  238. path.line_to({ TRY(read_unit()), path.segments().last()->point().y() });
  239. break;
  240. case PathCommand::VerticalLine:
  241. path.line_to({ path.segments().last()->point().x(), TRY(read_unit()) });
  242. break;
  243. case PathCommand::CubicBezier: {
  244. auto control_0 = TRY(read_point());
  245. auto control_1 = TRY(read_point());
  246. auto point_1 = TRY(read_point());
  247. path.cubic_bezier_curve_to(control_0, control_1, point_1);
  248. break;
  249. }
  250. case PathCommand::ArcCircle: {
  251. u8 flags = TRY(m_stream.read_value<u8>());
  252. bool large_arc = (flags >> 0) & 0b1;
  253. bool sweep = (flags >> 1) & 0b1;
  254. auto radius = TRY(read_unit());
  255. auto target = TRY(read_point());
  256. path.arc_to(target, radius, large_arc, !sweep);
  257. break;
  258. }
  259. case PathCommand::ArcEllipse: {
  260. u8 flags = TRY(m_stream.read_value<u8>());
  261. bool large_arc = (flags >> 0) & 0b1;
  262. bool sweep = (flags >> 1) & 0b1;
  263. auto radius_x = TRY(read_unit());
  264. auto radius_y = TRY(read_unit());
  265. auto rotation = TRY(read_unit());
  266. auto target = TRY(read_point());
  267. path.elliptical_arc_to(target, { radius_x, radius_y }, rotation, large_arc, !sweep);
  268. break;
  269. }
  270. case PathCommand::ClosePath: {
  271. path.close();
  272. break;
  273. }
  274. case PathCommand::QuadraticBezier: {
  275. auto control = TRY(read_point());
  276. auto point_1 = TRY(read_point());
  277. path.quadratic_bezier_curve_to(control, point_1);
  278. break;
  279. }
  280. default:
  281. return Error::from_string_literal("Invalid TinyVG: Bad path command");
  282. }
  283. }
  284. }
  285. return path;
  286. }
  287. ErrorOr<FillCommandHeader> read_fill_command_header(StyleType style_type)
  288. {
  289. return FillCommandHeader { TRY(read_var_uint()) + 1, TRY(read_style(style_type)) };
  290. }
  291. ErrorOr<DrawCommandHeader> read_draw_command_header(StyleType style_type)
  292. {
  293. return DrawCommandHeader { TRY(read_var_uint()) + 1, TRY(read_style(style_type)), TRY(read_unit()) };
  294. }
  295. ErrorOr<OutlineFillCommandHeader> read_outline_fill_command_header(StyleType style_type)
  296. {
  297. u8 header = TRY(m_stream.read_value<u8>());
  298. u8 count = (header & 0x3f) + 1;
  299. auto stroke_type = static_cast<StyleType>((header >> 6) & 0x3);
  300. return OutlineFillCommandHeader { count, TRY(read_style(style_type)), TRY(read_style(stroke_type)), TRY(read_unit()) };
  301. }
  302. private:
  303. Stream& m_stream;
  304. float m_scale {};
  305. CoordinateRange m_coordinate_range;
  306. ReadonlySpan<Color> m_color_table;
  307. };
  308. ErrorOr<NonnullRefPtr<TinyVGDecodedImageData>> TinyVGDecodedImageData::decode(Stream& stream)
  309. {
  310. return decode(stream, TRY(decode_tinyvg_header(stream)));
  311. }
  312. ErrorOr<NonnullRefPtr<TinyVGDecodedImageData>> TinyVGDecodedImageData::decode(Stream& stream, TinyVGHeader const& header)
  313. {
  314. if (header.version != 1)
  315. return Error::from_string_literal("Invalid TinyVG: Unsupported version");
  316. auto color_table = TRY(decode_color_table(stream, header.color_encoding, header.color_count));
  317. TinyVGReader reader { stream, header, color_table.span() };
  318. auto rectangle_to_path = [](FloatRect const& rect) -> Path {
  319. Path path;
  320. path.move_to({ rect.x(), rect.y() });
  321. path.line_to({ rect.x() + rect.width(), rect.y() });
  322. path.line_to({ rect.x() + rect.width(), rect.y() + rect.height() });
  323. path.line_to({ rect.x(), rect.y() + rect.height() });
  324. path.close();
  325. return path;
  326. };
  327. Vector<DrawCommand> draw_commands;
  328. bool at_end = false;
  329. while (!at_end) {
  330. u8 command_info = TRY(stream.read_value<u8>());
  331. auto command = static_cast<Command>(command_info & 0x3f);
  332. auto style_type = static_cast<StyleType>((command_info >> 6) & 0x3);
  333. switch (command) {
  334. case Command::EndOfDocument:
  335. at_end = true;
  336. break;
  337. case Command::FillPolygon: {
  338. auto header = TRY(reader.read_fill_command_header(style_type));
  339. Path polygon;
  340. polygon.move_to(TRY(reader.read_point()));
  341. for (u32 i = 0; i < header.count - 1; i++)
  342. polygon.line_to(TRY(reader.read_point()));
  343. TRY(draw_commands.try_append(DrawCommand { move(polygon), move(header.style) }));
  344. break;
  345. }
  346. case Command::FillRectangles: {
  347. auto header = TRY(reader.read_fill_command_header(style_type));
  348. for (u32 i = 0; i < header.count; i++) {
  349. TRY(draw_commands.try_append(DrawCommand {
  350. rectangle_to_path(TRY(reader.read_rectangle())), move(header.style) }));
  351. }
  352. break;
  353. }
  354. case Command::FillPath: {
  355. auto header = TRY(reader.read_fill_command_header(style_type));
  356. auto path = TRY(reader.read_path(header.count));
  357. TRY(draw_commands.try_append(DrawCommand { move(path), move(header.style) }));
  358. break;
  359. }
  360. case Command::DrawLines: {
  361. auto header = TRY(reader.read_draw_command_header(style_type));
  362. Path path;
  363. for (u32 i = 0; i < header.count; i++) {
  364. auto line = TRY(reader.read_line());
  365. path.move_to(line.a());
  366. path.line_to(line.b());
  367. }
  368. TRY(draw_commands.try_append(DrawCommand { move(path), {}, move(header.line_style), header.line_width }));
  369. break;
  370. }
  371. case Command::DrawLineStrip:
  372. case Command::DrawLineLoop: {
  373. auto header = TRY(reader.read_draw_command_header(style_type));
  374. Path path;
  375. path.move_to(TRY(reader.read_point()));
  376. for (u32 i = 0; i < header.count - 1; i++)
  377. path.line_to(TRY(reader.read_point()));
  378. if (command == Command::DrawLineLoop)
  379. path.close();
  380. TRY(draw_commands.try_append(DrawCommand { move(path), {}, move(header.line_style), header.line_width }));
  381. break;
  382. }
  383. case Command::DrawLinePath: {
  384. auto header = TRY(reader.read_draw_command_header(style_type));
  385. auto path = TRY(reader.read_path(header.count));
  386. TRY(draw_commands.try_append(DrawCommand { move(path), {}, move(header.line_style), header.line_width }));
  387. break;
  388. }
  389. case Command::OutlineFillPolygon: {
  390. auto header = TRY(reader.read_outline_fill_command_header(style_type));
  391. Path polygon;
  392. polygon.move_to(TRY(reader.read_point()));
  393. for (u32 i = 0; i < header.count - 1; i++)
  394. polygon.line_to(TRY(reader.read_point()));
  395. TRY(draw_commands.try_append(DrawCommand { move(polygon), move(header.fill_style), move(header.line_style), header.line_width }));
  396. break;
  397. }
  398. case Command::OutlineFillRectangles: {
  399. auto header = TRY(reader.read_outline_fill_command_header(style_type));
  400. for (u32 i = 0; i < header.count - 1; i++) {
  401. TRY(draw_commands.try_append(DrawCommand {
  402. rectangle_to_path(TRY(reader.read_rectangle())), move(header.fill_style), move(header.line_style), header.line_width }));
  403. }
  404. break;
  405. }
  406. case Command::OutLineFillPath: {
  407. auto header = TRY(reader.read_outline_fill_command_header(style_type));
  408. auto path = TRY(reader.read_path(header.count));
  409. TRY(draw_commands.try_append(DrawCommand { move(path), move(header.fill_style), move(header.line_style), header.line_width }));
  410. break;
  411. }
  412. default:
  413. return Error::from_string_literal("Invalid TinyVG: Bad command");
  414. }
  415. }
  416. return TRY(adopt_nonnull_ref_or_enomem(new (nothrow) TinyVGDecodedImageData({ header.width, header.height }, move(draw_commands))));
  417. }
  418. void TinyVGDecodedImageData::draw_transformed(Painter& painter, AffineTransform transform) const
  419. {
  420. // FIXME: Correctly handle non-uniform scales.
  421. auto scale = max(transform.x_scale(), transform.y_scale());
  422. AntiAliasingPainter aa_painter { painter };
  423. for (auto const& command : draw_commands()) {
  424. auto draw_path = command.path.copy_transformed(transform);
  425. if (command.fill.has_value()) {
  426. auto fill_path = draw_path;
  427. fill_path.close_all_subpaths();
  428. command.fill->visit(
  429. [&](Color color) { aa_painter.fill_path(fill_path, color, Painter::WindingRule::EvenOdd); },
  430. [&](NonnullRefPtr<SVGGradientPaintStyle> style) {
  431. const_cast<SVGGradientPaintStyle&>(*style).set_gradient_transform(transform);
  432. aa_painter.fill_path(fill_path, style, 1.0f, Painter::WindingRule::EvenOdd);
  433. });
  434. }
  435. if (command.stroke.has_value()) {
  436. command.stroke->visit(
  437. [&](Color color) { aa_painter.stroke_path(draw_path, color, command.stroke_width * scale); },
  438. [&](NonnullRefPtr<SVGGradientPaintStyle> style) {
  439. const_cast<SVGGradientPaintStyle&>(*style).set_gradient_transform(transform);
  440. aa_painter.stroke_path(draw_path, style, command.stroke_width * scale);
  441. });
  442. }
  443. }
  444. }
  445. struct TinyVGLoadingContext {
  446. FixedMemoryStream stream;
  447. TinyVGHeader header {};
  448. RefPtr<TinyVGDecodedImageData> decoded_image {};
  449. RefPtr<Bitmap> bitmap {};
  450. enum class State {
  451. NotDecoded = 0,
  452. HeaderDecoded,
  453. ImageDecoded,
  454. Error,
  455. };
  456. State state { State::NotDecoded };
  457. };
  458. static ErrorOr<void> decode_header_and_update_context(TinyVGLoadingContext& context)
  459. {
  460. VERIFY(context.state == TinyVGLoadingContext::State::NotDecoded);
  461. auto header_or_error = decode_tinyvg_header(context.stream);
  462. if (header_or_error.is_error()) {
  463. context.state = TinyVGLoadingContext::State::Error;
  464. return header_or_error.release_error();
  465. }
  466. context.state = TinyVGLoadingContext::State::HeaderDecoded;
  467. context.header = header_or_error.release_value();
  468. return {};
  469. }
  470. static ErrorOr<void> decode_image_data_and_update_context(TinyVGLoadingContext& context)
  471. {
  472. VERIFY(context.state == TinyVGLoadingContext::State::HeaderDecoded);
  473. auto image_data_or_error = TinyVGDecodedImageData::decode(context.stream, context.header);
  474. if (image_data_or_error.is_error()) {
  475. context.state = TinyVGLoadingContext::State::Error;
  476. return image_data_or_error.release_error();
  477. }
  478. context.state = TinyVGLoadingContext::State::ImageDecoded;
  479. context.decoded_image = image_data_or_error.release_value();
  480. return {};
  481. }
  482. static ErrorOr<void> ensure_fully_decoded(TinyVGLoadingContext& context)
  483. {
  484. if (context.state == TinyVGLoadingContext::State::Error)
  485. return Error::from_string_literal("TinyVGImageDecoderPlugin: Decoding failed!");
  486. if (context.state == TinyVGLoadingContext::State::NotDecoded)
  487. TRY(decode_header_and_update_context(context));
  488. if (context.state == TinyVGLoadingContext::State::HeaderDecoded)
  489. TRY(decode_image_data_and_update_context(context));
  490. VERIFY(context.state == TinyVGLoadingContext::State::ImageDecoded);
  491. return {};
  492. }
  493. TinyVGImageDecoderPlugin::TinyVGImageDecoderPlugin(ReadonlyBytes bytes)
  494. : m_context { make<TinyVGLoadingContext>(FixedMemoryStream { bytes }) }
  495. {
  496. }
  497. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> TinyVGImageDecoderPlugin::create(ReadonlyBytes bytes)
  498. {
  499. return adopt_nonnull_own_or_enomem(new (nothrow) TinyVGImageDecoderPlugin(bytes));
  500. }
  501. bool TinyVGImageDecoderPlugin::sniff(ReadonlyBytes bytes)
  502. {
  503. FixedMemoryStream stream { { bytes.data(), bytes.size() } };
  504. return !decode_tinyvg_header(stream).is_error();
  505. }
  506. IntSize TinyVGImageDecoderPlugin::size()
  507. {
  508. if (m_context->state == TinyVGLoadingContext::State::NotDecoded)
  509. (void)decode_header_and_update_context(*m_context);
  510. if (m_context->state == TinyVGLoadingContext::State::Error)
  511. return {};
  512. return { m_context->header.width, m_context->header.height };
  513. }
  514. ErrorOr<void> TinyVGImageDecoderPlugin::initialize()
  515. {
  516. return decode_header_and_update_context(*m_context);
  517. }
  518. ErrorOr<ImageFrameDescriptor> TinyVGImageDecoderPlugin::frame(size_t, Optional<IntSize> ideal_size)
  519. {
  520. TRY(ensure_fully_decoded(*m_context));
  521. auto target_size = ideal_size.value_or(m_context->decoded_image->size());
  522. if (!m_context->bitmap || m_context->bitmap->size() != target_size)
  523. m_context->bitmap = TRY(m_context->decoded_image->bitmap(target_size));
  524. return ImageFrameDescriptor { m_context->bitmap };
  525. }
  526. ErrorOr<VectorImageFrameDescriptor> TinyVGImageDecoderPlugin::vector_frame(size_t)
  527. {
  528. TRY(ensure_fully_decoded(*m_context));
  529. return VectorImageFrameDescriptor { m_context->decoded_image, 0 };
  530. }
  531. }