Image decoding without dart:ui: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: jpeg-source (line 22), png (line 41), jpeg (line 58), hdr (line 61), report (line 89), check (line 116).
1/// PNG, JPEG and Radiance `.hdr`, decoded with no `dart:ui` in sight.
2///
3/// Quoted by `image_decode.md` and shown whole in the Source tab.
4library;
6import 'dart:convert';
7import 'dart:typed_data';
9import 'package:flutter/widgets.dart';
10import 'package:flutter3d/flutter3d.dart';
11import 'package:flutter3d_core/flutter3d_core.dart' show encodeCompressedPng;
12import 'package:flutter3d_showcase/src/demo/demo.dart';
14/// The `image-decode` page: a block textured from images decoded in Dart.
15/// Its check round-trips a PNG, decodes a JPEG close to its fill, and reads
16/// an HDR pixel.
17final class ImageDecodeDemo extends ShowcaseDemo {
18 late final DecodedImage _png;
19 late final DecodedImage _jpeg;
20 late final HdrImage _hdr;
22 // A real JPEG, one 8x8 MCU encoded at quality 100 from a flat
23 // (200, 100, 50) fill, the same fixture the decoder's own test checks
24 // against that exact colour.
25 static const String _jpegBase64 =
26 '/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB'
27 'AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEB'
28 'AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAAR'
29 'CAAIAAgDAREAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAA'
30 'AgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkK'
31 'FhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWG'
32 'h4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl'
33 '5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREA'
34 'AgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYk'
35 'NOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOE'
36 'hYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk'
37 '5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDn6/yvP9sD/9k=';
40 Scene build(DemoContext context) {
41 // A tiny checkerboard, encoded here and decoded straight back: a real
42 // compressed PNG, not a fixture on disk.
43 final width = 4, height = 4;
44 final rgba = Uint8List(width * height * 4);
45 for (var y = 0; y < height; y++) {
46 for (var x = 0; x < width; x++) {
47 final lit = (x + y).isEven;
48 final o = (y * width + x) * 4;
49 rgba[o] = lit ? 240 : 20;
50 rgba[o + 1] = lit ? 240 : 20;
51 rgba[o + 2] = lit ? 240 : 20;
52 rgba[o + 3] = 255;
53 }
54 }
55 final pngBytes = encodeCompressedPng(width, height, rgba);
56 _png = decodePng(pngBytes);
58 final jpegBytes = base64Decode(_jpegBase64);
59 _jpeg = decodeJpeg(jpegBytes);
61 // Radiance's old, flat scanline encoding: a header, a resolution line,
62 // then every pixel as four raw bytes (R, G, B, a shared exponent). Two
63 // by two is under the width the new run-length form requires, so this
64 // is read the simple way.
65 final hdrText = '#?RADIANCE\nFORMAT=32-bit_rle_rgbe\n\n-Y 2 +X 2\n';
66 final hdrBytes = Uint8List.fromList(<int>[
67 ...utf8.encode(hdrText),
68 // Top row: a dim red pixel, a bright red pixel.
69 255, 0, 0, 120, 255, 0, 0, 128,
70 // Bottom row: two pixels at half that brightness again.
71 255, 0, 0, 119, 255, 0, 0, 127,
72 ]);
73 _hdr = readHdr(hdrBytes);
75 return Scene()
76 ..add(
77 MeshNode(
78 DeviceMesh.upload(context.device, CuboidShape().build()),
79 RenderMaterial(baseColor: LinearColor.fromSrgb(0.6, 0.6, 0.6, 1.0)),
80 name: 'block',
81 ),
82 )
83 ..add(
84 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
85 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
86 );
87 }
89 String _report() =>
90 'PNG: ${_png.width}x${_png.height}, top-left pixel '
91 '${_png.rgba[0]},${_png.rgba[1]},${_png.rgba[2]}\n'
92 'JPEG: ${_jpeg.width}x${_jpeg.height}, decodes back to about '
93 '${_jpeg.rgba[0]},${_jpeg.rgba[1]},${_jpeg.rgba[2]} (encoded from '
94 '200,100,50)\n'
95 'HDR: ${_hdr.width}x${_hdr.height}, top-left red channel '
96 '${_hdr.rgb[0].toStringAsFixed(3)}';
99 Widget? customBody(BuildContext buildContext, DemoContext context) =>
100 Container(
101 color: const Color(0xFF14161A),
102 padding: const EdgeInsets.all(24),
103 alignment: Alignment.topLeft,
104 child: DefaultTextStyle(
105 style: const TextStyle(
106 color: Color(0xFFE8E8EC),
107 fontSize: 16,
108 fontFamily: 'monospace',
109 ),
110 child: Text(_report()),
111 ),
112 );
115 void verify(Scene scene, FrameResult frame) {
116 if (_png.width != 4 || _png.height != 4) {
117 throw StateError('the PNG did not round-trip its own size');
118 }
119 if (_png.rgba[0] < 200) {
120 throw StateError('the PNG round trip lost its lit corner');
121 }
122 if ((_jpeg.rgba[0] - 200).abs() > 5 || (_jpeg.rgba[1] - 100).abs() > 5) {
123 throw StateError('the JPEG did not decode close to its own fill');
124 }
125 if (_hdr.rgb[0] <= 0.0) {
126 throw StateError('the HDR pixel decoded to nothing');
127 }
128 if (frame.drawCalls < 1) {
129 throw StateError('the block was not drawn');
130 }
131 }
132}