10class BitmapRegionDecoderExample {
14 BitmapRegionDecoderExample() {
15 _loadSampleImageData();
21 void _loadSampleImageData() {
24 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,
27 print(
"Sample image data loaded (${_imageData!.length} bytes)");
35 print(
"No image data available");
42 print(
"Created BitmapRegionDecoder instance");
47 final Image? imageForDecoder =
null;
48 final BitmapRegionDecoder? decoderFromImage =
49 BitmapRegionDecoder.newInstanceFromImage(imageForDecoder);
50 print(
'Decoder from Image: ${decoderFromImage != null}');
55 print(
'Source image width: $sourceWidth');
59 final sourceHeight =
_decoder!.height;
60 print(
'Source image height: $sourceHeight');
64 _demonstrateRectangleUsage();
67 _demonstrateRegionDecoding();
70 _demonstrateSampleSizes();
73 _demonstrateMultipleRegions();
79 void _demonstrateRectangleUsage() {
82 Rectangle rect1 = Rectangle(10, 20, 100, 150);
84 "Created rectangle: x=${rect1.x}, y=${rect1.y}, width=${rect1.width}, height=${rect1.height}",
91 print(
"Rectangle X coordinate: $x");
97 print(
"Rectangle Y coordinate: $y");
102 int width = rect1.
width;
103 print(
"Rectangle width: $width");
108 int height = rect1.
height;
109 print(
"Rectangle height: $height");
113 Rectangle fullImage = Rectangle(0, 0, 1024, 768);
114 Rectangle topLeft = Rectangle(0, 0, 512, 384);
115 Rectangle center = Rectangle(256, 192, 512, 384);
116 Rectangle bottomRight = Rectangle(512, 384, 512, 384);
118 print(
"Created different rectangles for different regions");
124 void _demonstrateRegionDecoding() {
126 print(
"Decoder not initialized");
131 Rectangle sampleRect = Rectangle(50, 50, 200, 200);
135 ImageWrapper decodedImage =
_decoder!.decodeRegion(sampleRect, 1);
137 "Decoded region: ${sampleRect.width}x${sampleRect.height} at sample size 1",
142 _demonstrateDecodedImage(decodedImage,
"Sample Region");
148 void _demonstrateSampleSizes() {
153 Rectangle testRect = Rectangle(0, 0, 400, 300);
156 List<double> sampleSizes = [1.0, 2.0, 4.0, 8.0];
158 for (
double sampleSize in sampleSizes) {
161 ImageWrapper decodedImage =
_decoder!.decodeRegion(testRect, sampleSize);
163 "Decoded region with sample size $sampleSize: ${(testRect.width / sampleSize).round()}x${(testRect.height / sampleSize).round()}",
167 _demonstrateDecodedImage(decodedImage,
"Sample Size $sampleSize");
174 void _demonstrateMultipleRegions() {
180 List<Rectangle> regions = [
181 Rectangle(0, 0, 256, 256),
182 Rectangle(256, 0, 256, 256),
183 Rectangle(0, 256, 256, 256),
184 Rectangle(256, 256, 256, 256),
187 print(
"=== Decoding Multiple Regions ===");
188 for (
int i = 0; i < regions.length; i++) {
189 Rectangle region = regions[i];
193 ImageWrapper decodedImage =
_decoder!.decodeRegion(region, 1);
195 "Region ${i + 1}: ${region.width}x${region.height} at (${region.x}, ${region.y})",
199 _demonstrateDecodedImage(decodedImage,
"Region ${i + 1}");
206 void _demonstrateDecodedImage(ImageWrapper image, String description) {
207 print(
"--- $description ---");
208 print(
"Image dimensions: ${image.width}x${image.height}");
209 print(
"Image format: ${image.format}");
210 print(
"Image data size: ${image.data.length} bytes");
217 Future<void> demonstrateAdvancedFeatures() async {
218 print(
"=== Advanced BitmapRegionDecoder Features ===");
225 List<Uint8List> imageDataList = [
227 Uint8List.fromList([0xFF, 0xD8, 0xFF, 0xE0]),
228 Uint8List.fromList([0x47, 0x49, 0x46, 0x38]),
231 for (
int i = 0; i < imageDataList.length; i++) {
235 BitmapRegionDecoder
decoder = BitmapRegionDecoder.newInstance(
238 print(
"Created decoder for image type ${i + 1}");
242 Rectangle testRect = Rectangle(0, 0, 100, 100);
243 ImageWrapper decodedImage =
decoder.decodeRegion(testRect, 1);
244 print(
"Successfully decoded region from image type ${i + 1}");
246 print(
"Failed to decode image type ${i + 1}: $e");
255 print(
"=== Error Handling ===");
258 Uint8List invalidData = Uint8List.fromList([0x00, 0x01, 0x02, 0x03]);
263 BitmapRegionDecoder
decoder = BitmapRegionDecoder.newInstance(
266 print(
"Created decoder with invalid data");
270 Rectangle rect = Rectangle(0, 0, 50, 50);
271 ImageWrapper image =
decoder.decodeRegion(rect, 1);
272 print(
"Successfully decoded region from invalid data");
274 print(
"Expected error when creating decoder with invalid data: $e");
280 Rectangle invalidRect = Rectangle(-10, -10, 100, 100);
281 ImageWrapper image =
_decoder!.decodeRegion(invalidRect, 1);
282 print(
"Successfully decoded region with negative coordinates");
284 print(
"Expected error with invalid rectangle: $e");
293 print(
"=== Performance Optimization ===");
300 Rectangle largeRect = Rectangle(0, 0, 800, 600);
301 List<double> sampleSizes = [1.0, 2.0, 4.0, 8.0, 16.0];
303 for (
double sampleSize in sampleSizes) {
304 final stopwatch = Stopwatch()..start();
306 ImageWrapper image =
_decoder!.decodeRegion(largeRect, sampleSize);
310 "Sample size $sampleSize: ${stopwatch.elapsedMilliseconds}ms, "
311 "Output size: ${image.width}x${image.height}",
320 print(
"=== Rectangle Manipulation ===");
323 Rectangle baseRect = Rectangle(100, 100, 200, 150);
325 "Base rectangle: ${baseRect.x}, ${baseRect.y}, ${baseRect.width}x${baseRect.height}",
329 List<Rectangle> variations = [
345 baseRect.
width - 100,
351 for (
int i = 0; i < variations.length; i++) {
352 Rectangle rect = variations[i];
354 "Variation ${i + 1}: ${rect.x}, ${rect.y}, ${rect.width}x${rect.height}",
362 Future<void> runExample() async {
363 print(
"=== BitmapRegionDecoder Example ===");
367 await demonstrateAdvancedFeatures();
372 await Future.delayed(Duration(seconds: 2));
374 print(
"=== Example completed ===");