TinyVGLoader.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. auto read_gradient = [&]() -> ErrorOr<NonnullRefPtr<SVGGradientPaintStyle>> {
  191. auto point_0 = TRY(read_point());
  192. auto point_1 = TRY(read_point());
  193. auto color_0 = TRY(read_color());
  194. auto color_1 = TRY(read_color());
  195. // Map TinyVG gradients to SVG gradients (since we already have those).
  196. // This is not entirely consistent with the spec, which uses gamma sRGB for gradients
  197. // (but this matches the TVG -> SVG renderings).
  198. auto svg_gradient = TRY([&]() -> ErrorOr<NonnullRefPtr<SVGGradientPaintStyle>> {
  199. if (type == StyleType::LinearGradient)
  200. return TRY(SVGLinearGradientPaintStyle::create(point_0, point_1));
  201. auto radius = point_1.distance_from(point_0);
  202. return TRY(SVGRadialGradientPaintStyle::create(point_0, 0, point_0, radius));
  203. }());
  204. TRY(svg_gradient->add_color_stop(0, color_0));
  205. TRY(svg_gradient->add_color_stop(1, color_1));
  206. return svg_gradient;
  207. };
  208. switch (type) {
  209. case StyleType::FlatColored:
  210. return TRY(read_color());
  211. case StyleType::LinearGradient:
  212. case StyleType::RadialGradinet:
  213. return TRY(read_gradient());
  214. }
  215. return Error::from_string_literal("Invalid TinyVG: Bad style data");
  216. }
  217. ErrorOr<FloatRect> read_rectangle()
  218. {
  219. return FloatRect { TRY(read_unit()), TRY(read_unit()), TRY(read_unit()), TRY(read_unit()) };
  220. }
  221. ErrorOr<FloatLine> read_line()
  222. {
  223. return FloatLine { TRY(read_point()), TRY(read_point()) };
  224. }
  225. ErrorOr<Path> read_path(u32 segment_count)
  226. {
  227. Path path;
  228. auto segment_lengths = TRY(FixedArray<u32>::create(segment_count));
  229. for (auto& command_count : segment_lengths) {
  230. command_count = TRY(read_var_uint()) + 1;
  231. }
  232. for (auto command_count : segment_lengths) {
  233. auto start_point = TRY(read_point());
  234. path.move_to(start_point);
  235. for (u32 i = 0; i < command_count; i++) {
  236. u8 command_tag = TRY(m_stream.read_value<u8>());
  237. auto path_command = static_cast<PathCommand>(command_tag & 0x7);
  238. bool has_line_width = (command_tag >> 4) & 0b1;
  239. if (has_line_width) {
  240. // FIXME: TinyVG allows changing the line width within a path.
  241. // This is not supported in LibGfx, so we currently ignore this.
  242. (void)TRY(read_unit());
  243. }
  244. switch (path_command) {
  245. case PathCommand::Line:
  246. path.line_to(TRY(read_point()));
  247. break;
  248. case PathCommand::HorizontalLine:
  249. path.line_to({ TRY(read_unit()), path.segments().last()->point().y() });
  250. break;
  251. case PathCommand::VerticalLine:
  252. path.line_to({ path.segments().last()->point().x(), TRY(read_unit()) });
  253. break;
  254. case PathCommand::CubicBezier: {
  255. auto control_0 = TRY(read_point());
  256. auto control_1 = TRY(read_point());
  257. auto point_1 = TRY(read_point());
  258. path.cubic_bezier_curve_to(control_0, control_1, point_1);
  259. break;
  260. }
  261. case PathCommand::ArcCircle: {
  262. u8 flags = TRY(m_stream.read_value<u8>());
  263. bool large_arc = (flags >> 0) & 0b1;
  264. bool sweep = (flags >> 1) & 0b1;
  265. auto radius = TRY(read_unit());
  266. auto target = TRY(read_point());
  267. path.arc_to(target, radius, large_arc, !sweep);
  268. break;
  269. }
  270. case PathCommand::ArcEllipse: {
  271. u8 flags = TRY(m_stream.read_value<u8>());
  272. bool large_arc = (flags >> 0) & 0b1;
  273. bool sweep = (flags >> 1) & 0b1;
  274. auto radius_x = TRY(read_unit());
  275. auto radius_y = TRY(read_unit());
  276. auto rotation = TRY(read_unit());
  277. auto target = TRY(read_point());
  278. path.elliptical_arc_to(target, { radius_x, radius_y }, rotation, large_arc, !sweep);
  279. break;
  280. }
  281. case PathCommand::ClosePath: {
  282. path.close();
  283. break;
  284. }
  285. case PathCommand::QuadraticBezier: {
  286. auto control = TRY(read_point());
  287. auto point_1 = TRY(read_point());
  288. path.quadratic_bezier_curve_to(control, point_1);
  289. break;
  290. }
  291. default:
  292. return Error::from_string_literal("Invalid TinyVG: Bad path command");
  293. }
  294. }
  295. }
  296. return path;
  297. }
  298. ErrorOr<FillCommandHeader> read_fill_command_header(StyleType style_type)
  299. {
  300. return FillCommandHeader { TRY(read_var_uint()) + 1, TRY(read_style(style_type)) };
  301. }
  302. ErrorOr<DrawCommandHeader> read_draw_command_header(StyleType style_type)
  303. {
  304. return DrawCommandHeader { TRY(read_var_uint()) + 1, TRY(read_style(style_type)), TRY(read_unit()) };
  305. }
  306. ErrorOr<OutlineFillCommandHeader> read_outline_fill_command_header(StyleType style_type)
  307. {
  308. u8 header = TRY(m_stream.read_value<u8>());
  309. u8 count = (header & 0x3f) + 1;
  310. auto stroke_type = static_cast<StyleType>((header >> 6) & 0x3);
  311. return OutlineFillCommandHeader { count, TRY(read_style(style_type)), TRY(read_style(stroke_type)), TRY(read_unit()) };
  312. }
  313. private:
  314. Stream& m_stream;
  315. float m_scale {};
  316. CoordinateRange m_coordinate_range;
  317. ReadonlySpan<Color> m_color_table;
  318. };
  319. ErrorOr<NonnullRefPtr<TinyVGDecodedImageData>> TinyVGDecodedImageData::decode(Stream& stream)
  320. {
  321. return decode(stream, TRY(decode_tinyvg_header(stream)));
  322. }
  323. ErrorOr<NonnullRefPtr<TinyVGDecodedImageData>> TinyVGDecodedImageData::decode(Stream& stream, TinyVGHeader const& header)
  324. {
  325. if (header.version != 1)
  326. return Error::from_string_literal("Invalid TinyVG: Unsupported version");
  327. auto color_table = TRY(decode_color_table(stream, header.color_encoding, header.color_count));
  328. TinyVGReader reader { stream, header, color_table.span() };
  329. auto rectangle_to_path = [](FloatRect const& rect) -> Path {
  330. Path path;
  331. path.move_to({ rect.x(), rect.y() });
  332. path.line_to({ rect.x() + rect.width(), rect.y() });
  333. path.line_to({ rect.x() + rect.width(), rect.y() + rect.height() });
  334. path.line_to({ rect.x(), rect.y() + rect.height() });
  335. path.close();
  336. return path;
  337. };
  338. Vector<DrawCommand> draw_commands;
  339. bool at_end = false;
  340. while (!at_end) {
  341. u8 command_info = TRY(stream.read_value<u8>());
  342. auto command = static_cast<Command>(command_info & 0x3f);
  343. auto style_type = static_cast<StyleType>((command_info >> 6) & 0x3);
  344. switch (command) {
  345. case Command::EndOfDocument:
  346. at_end = true;
  347. break;
  348. case Command::FillPolygon: {
  349. auto header = TRY(reader.read_fill_command_header(style_type));
  350. Path polygon;
  351. polygon.move_to(TRY(reader.read_point()));
  352. for (u32 i = 0; i < header.count - 1; i++)
  353. polygon.line_to(TRY(reader.read_point()));
  354. TRY(draw_commands.try_append(DrawCommand { move(polygon), move(header.style) }));
  355. break;
  356. }
  357. case Command::FillRectangles: {
  358. auto header = TRY(reader.read_fill_command_header(style_type));
  359. for (u32 i = 0; i < header.count; i++) {
  360. TRY(draw_commands.try_append(DrawCommand {
  361. rectangle_to_path(TRY(reader.read_rectangle())), header.style }));
  362. }
  363. break;
  364. }
  365. case Command::FillPath: {
  366. auto header = TRY(reader.read_fill_command_header(style_type));
  367. auto path = TRY(reader.read_path(header.count));
  368. TRY(draw_commands.try_append(DrawCommand { move(path), move(header.style) }));
  369. break;
  370. }
  371. case Command::DrawLines: {
  372. auto header = TRY(reader.read_draw_command_header(style_type));
  373. Path path;
  374. for (u32 i = 0; i < header.count; i++) {
  375. auto line = TRY(reader.read_line());
  376. path.move_to(line.a());
  377. path.line_to(line.b());
  378. }
  379. TRY(draw_commands.try_append(DrawCommand { move(path), {}, move(header.line_style), header.line_width }));
  380. break;
  381. }
  382. case Command::DrawLineStrip:
  383. case Command::DrawLineLoop: {
  384. auto header = TRY(reader.read_draw_command_header(style_type));
  385. Path path;
  386. path.move_to(TRY(reader.read_point()));
  387. for (u32 i = 0; i < header.count - 1; i++)
  388. path.line_to(TRY(reader.read_point()));
  389. if (command == Command::DrawLineLoop)
  390. path.close();
  391. TRY(draw_commands.try_append(DrawCommand { move(path), {}, move(header.line_style), header.line_width }));
  392. break;
  393. }
  394. case Command::DrawLinePath: {
  395. auto header = TRY(reader.read_draw_command_header(style_type));
  396. auto path = TRY(reader.read_path(header.count));
  397. TRY(draw_commands.try_append(DrawCommand { move(path), {}, move(header.line_style), header.line_width }));
  398. break;
  399. }
  400. case Command::OutlineFillPolygon: {
  401. auto header = TRY(reader.read_outline_fill_command_header(style_type));
  402. Path polygon;
  403. polygon.move_to(TRY(reader.read_point()));
  404. for (u32 i = 0; i < header.count - 1; i++)
  405. polygon.line_to(TRY(reader.read_point()));
  406. polygon.close();
  407. TRY(draw_commands.try_append(DrawCommand { move(polygon), move(header.fill_style), move(header.line_style), header.line_width }));
  408. break;
  409. }
  410. case Command::OutlineFillRectangles: {
  411. auto header = TRY(reader.read_outline_fill_command_header(style_type));
  412. for (u32 i = 0; i < header.count; i++) {
  413. TRY(draw_commands.try_append(DrawCommand {
  414. rectangle_to_path(TRY(reader.read_rectangle())), header.fill_style, header.line_style, header.line_width }));
  415. }
  416. break;
  417. }
  418. case Command::OutLineFillPath: {
  419. auto header = TRY(reader.read_outline_fill_command_header(style_type));
  420. auto path = TRY(reader.read_path(header.count));
  421. TRY(draw_commands.try_append(DrawCommand { move(path), move(header.fill_style), move(header.line_style), header.line_width }));
  422. break;
  423. }
  424. default:
  425. return Error::from_string_literal("Invalid TinyVG: Bad command");
  426. }
  427. }
  428. return TRY(adopt_nonnull_ref_or_enomem(new (nothrow) TinyVGDecodedImageData({ header.width, header.height }, move(draw_commands))));
  429. }
  430. void TinyVGDecodedImageData::draw_transformed(Painter& painter, AffineTransform transform) const
  431. {
  432. // FIXME: Correctly handle non-uniform scales.
  433. auto scale = max(transform.x_scale(), transform.y_scale());
  434. AntiAliasingPainter aa_painter { painter };
  435. for (auto const& command : draw_commands()) {
  436. auto draw_path = command.path.copy_transformed(transform);
  437. if (command.fill.has_value()) {
  438. auto fill_path = draw_path;
  439. fill_path.close_all_subpaths();
  440. command.fill->visit(
  441. [&](Color color) { aa_painter.fill_path(fill_path, color, Painter::WindingRule::EvenOdd); },
  442. [&](NonnullRefPtr<SVGGradientPaintStyle> style) {
  443. const_cast<SVGGradientPaintStyle&>(*style).set_gradient_transform(transform);
  444. aa_painter.fill_path(fill_path, style, 1.0f, Painter::WindingRule::EvenOdd);
  445. });
  446. }
  447. if (command.stroke.has_value()) {
  448. command.stroke->visit(
  449. [&](Color color) { aa_painter.stroke_path(draw_path, color, command.stroke_width * scale); },
  450. [&](NonnullRefPtr<SVGGradientPaintStyle> style) {
  451. const_cast<SVGGradientPaintStyle&>(*style).set_gradient_transform(transform);
  452. aa_painter.stroke_path(draw_path, style, command.stroke_width * scale);
  453. });
  454. }
  455. }
  456. }
  457. struct TinyVGLoadingContext {
  458. FixedMemoryStream stream;
  459. TinyVGHeader header {};
  460. RefPtr<TinyVGDecodedImageData> decoded_image {};
  461. RefPtr<Bitmap> bitmap {};
  462. enum class State {
  463. NotDecoded = 0,
  464. HeaderDecoded,
  465. ImageDecoded,
  466. Error,
  467. };
  468. State state { State::NotDecoded };
  469. };
  470. static ErrorOr<void> decode_header_and_update_context(TinyVGLoadingContext& context)
  471. {
  472. VERIFY(context.state == TinyVGLoadingContext::State::NotDecoded);
  473. context.header = TRY(decode_tinyvg_header(context.stream));
  474. context.state = TinyVGLoadingContext::State::HeaderDecoded;
  475. return {};
  476. }
  477. static ErrorOr<void> decode_image_data_and_update_context(TinyVGLoadingContext& context)
  478. {
  479. VERIFY(context.state == TinyVGLoadingContext::State::HeaderDecoded);
  480. auto image_data_or_error = TinyVGDecodedImageData::decode(context.stream, context.header);
  481. if (image_data_or_error.is_error()) {
  482. context.state = TinyVGLoadingContext::State::Error;
  483. return image_data_or_error.release_error();
  484. }
  485. context.state = TinyVGLoadingContext::State::ImageDecoded;
  486. context.decoded_image = image_data_or_error.release_value();
  487. return {};
  488. }
  489. static ErrorOr<void> ensure_fully_decoded(TinyVGLoadingContext& context)
  490. {
  491. if (context.state == TinyVGLoadingContext::State::Error)
  492. return Error::from_string_literal("TinyVGImageDecoderPlugin: Decoding failed!");
  493. if (context.state == TinyVGLoadingContext::State::HeaderDecoded)
  494. TRY(decode_image_data_and_update_context(context));
  495. VERIFY(context.state == TinyVGLoadingContext::State::ImageDecoded);
  496. return {};
  497. }
  498. TinyVGImageDecoderPlugin::TinyVGImageDecoderPlugin(ReadonlyBytes bytes)
  499. : m_context { make<TinyVGLoadingContext>(FixedMemoryStream { bytes }) }
  500. {
  501. }
  502. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> TinyVGImageDecoderPlugin::create(ReadonlyBytes bytes)
  503. {
  504. auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) TinyVGImageDecoderPlugin(bytes)));
  505. TRY(decode_header_and_update_context(*plugin->m_context));
  506. return plugin;
  507. }
  508. bool TinyVGImageDecoderPlugin::sniff(ReadonlyBytes bytes)
  509. {
  510. FixedMemoryStream stream { { bytes.data(), bytes.size() } };
  511. return !decode_tinyvg_header(stream).is_error();
  512. }
  513. IntSize TinyVGImageDecoderPlugin::size()
  514. {
  515. return { m_context->header.width, m_context->header.height };
  516. }
  517. ErrorOr<ImageFrameDescriptor> TinyVGImageDecoderPlugin::frame(size_t, Optional<IntSize> ideal_size)
  518. {
  519. TRY(ensure_fully_decoded(*m_context));
  520. auto target_size = ideal_size.value_or(m_context->decoded_image->size());
  521. if (!m_context->bitmap || m_context->bitmap->size() != target_size)
  522. m_context->bitmap = TRY(m_context->decoded_image->bitmap(target_size));
  523. return ImageFrameDescriptor { m_context->bitmap };
  524. }
  525. ErrorOr<VectorImageFrameDescriptor> TinyVGImageDecoderPlugin::vector_frame(size_t)
  526. {
  527. TRY(ensure_fully_decoded(*m_context));
  528. return VectorImageFrameDescriptor { m_context->decoded_image, 0 };
  529. }
  530. }