10class BitmapRegionDecoderExample {
11 BitmapRegionDecoder? _decoder;
12 Uint8List? _imageData;
14 BitmapRegionDecoderExample() {
15 _loadSampleImageData();
21 void _loadSampleImageData() {
23 _imageData = Uint8List.fromList([
24 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,
27 print(
"Sample image data loaded (${_imageData!.length} bytes)");
33 void demonstrateBitmapRegionDecoderMethods() {
34 if (_imageData ==
null) {
35 print(
"No image data available");
41 _decoder = BitmapRegionDecoder.newInstance(_imageData!);
42 print(
"Created BitmapRegionDecoder instance");
46 _demonstrateRectangleUsage();
49 _demonstrateRegionDecoding();
52 _demonstrateSampleSizes();
55 _demonstrateMultipleRegions();
61 void _demonstrateRectangleUsage() {
64 Rectangle rect1 = Rectangle(10, 20, 100, 150);
66 "Created rectangle: x=${rect1.x}, y=${rect1.y}, width=${rect1.width}, height=${rect1.height}",
73 print(
"Rectangle X coordinate: $x");
79 print(
"Rectangle Y coordinate: $y");
84 int width = rect1.width;
85 print(
"Rectangle width: $width");
90 int height = rect1.height;
91 print(
"Rectangle height: $height");
95 Rectangle fullImage = Rectangle(0, 0, 1024, 768);
96 Rectangle topLeft = Rectangle(0, 0, 512, 384);
97 Rectangle center = Rectangle(256, 192, 512, 384);
98 Rectangle bottomRight = Rectangle(512, 384, 512, 384);
100 print(
"Created different rectangles for different regions");
106 void _demonstrateRegionDecoding() {
107 if (_decoder ==
null) {
108 print(
"Decoder not initialized");
113 Rectangle sampleRect = Rectangle(50, 50, 200, 200);
117 ImageWrapper decodedImage = _decoder!.decodeRegion(sampleRect, 1);
119 "Decoded region: ${sampleRect.width}x${sampleRect.height} at sample size 1",
124 _demonstrateDecodedImage(decodedImage,
"Sample Region");
130 void _demonstrateSampleSizes() {
131 if (_decoder ==
null) {
135 Rectangle testRect = Rectangle(0, 0, 400, 300);
138 List<double> sampleSizes = [1.0, 2.0, 4.0, 8.0];
140 for (
double sampleSize in sampleSizes) {
143 ImageWrapper decodedImage = _decoder!.decodeRegion(testRect, sampleSize);
145 "Decoded region with sample size $sampleSize: ${(testRect.width / sampleSize).round()}x${(testRect.height / sampleSize).round()}",
149 _demonstrateDecodedImage(decodedImage,
"Sample Size $sampleSize");
156 void _demonstrateMultipleRegions() {
157 if (_decoder ==
null) {
162 List<Rectangle> regions = [
163 Rectangle(0, 0, 256, 256),
164 Rectangle(256, 0, 256, 256),
165 Rectangle(0, 256, 256, 256),
166 Rectangle(256, 256, 256, 256),
169 print(
"=== Decoding Multiple Regions ===");
170 for (
int i = 0; i < regions.length; i++) {
171 Rectangle region = regions[i];
175 ImageWrapper decodedImage = _decoder!.decodeRegion(region, 1);
177 "Region ${i + 1}: ${region.width}x${region.height} at (${region.x}, ${region.y})",
181 _demonstrateDecodedImage(decodedImage,
"Region ${i + 1}");
188 void _demonstrateDecodedImage(ImageWrapper image, String description) {
189 print(
"--- $description ---");
190 print(
"Image dimensions: ${image.width}x${image.height}");
191 print(
"Image format: ${image.format}");
192 print(
"Image data size: ${image.data.length} bytes");
199 Future<void> demonstrateAdvancedFeatures() async {
200 print(
"=== Advanced BitmapRegionDecoder Features ===");
202 if (_imageData ==
null) {
207 List<Uint8List> imageDataList = [
209 Uint8List.fromList([0xFF, 0xD8, 0xFF, 0xE0]),
210 Uint8List.fromList([0x47, 0x49, 0x46, 0x38]),
213 for (
int i = 0; i < imageDataList.length; i++) {
217 BitmapRegionDecoder
decoder = BitmapRegionDecoder.newInstance(
220 print(
"Created decoder for image type ${i + 1}");
224 Rectangle testRect = Rectangle(0, 0, 100, 100);
225 ImageWrapper decodedImage =
decoder.decodeRegion(testRect, 1);
226 print(
"Successfully decoded region from image type ${i + 1}");
228 print(
"Failed to decode image type ${i + 1}: $e");
236 void demonstrateErrorHandling() {
237 print(
"=== Error Handling ===");
240 Uint8List invalidData = Uint8List.fromList([0x00, 0x01, 0x02, 0x03]);
245 BitmapRegionDecoder
decoder = BitmapRegionDecoder.newInstance(
248 print(
"Created decoder with invalid data");
252 Rectangle rect = Rectangle(0, 0, 50, 50);
253 ImageWrapper image =
decoder.decodeRegion(rect, 1);
254 print(
"Successfully decoded region from invalid data");
256 print(
"Expected error when creating decoder with invalid data: $e");
260 if (_decoder !=
null) {
262 Rectangle invalidRect = Rectangle(-10, -10, 100, 100);
263 ImageWrapper image = _decoder!.decodeRegion(invalidRect, 1);
264 print(
"Successfully decoded region with negative coordinates");
266 print(
"Expected error with invalid rectangle: $e");
274 void demonstratePerformanceOptimization() {
275 print(
"=== Performance Optimization ===");
277 if (_decoder ==
null) {
282 Rectangle largeRect = Rectangle(0, 0, 800, 600);
283 List<double> sampleSizes = [1.0, 2.0, 4.0, 8.0, 16.0];
285 for (
double sampleSize in sampleSizes) {
286 final stopwatch = Stopwatch()..start();
288 ImageWrapper image = _decoder!.decodeRegion(largeRect, sampleSize);
292 "Sample size $sampleSize: ${stopwatch.elapsedMilliseconds}ms, "
293 "Output size: ${image.width}x${image.height}",
301 void demonstrateRectangleManipulation() {
302 print(
"=== Rectangle Manipulation ===");
305 Rectangle baseRect = Rectangle(100, 100, 200, 150);
307 "Base rectangle: ${baseRect.x}, ${baseRect.y}, ${baseRect.width}x${baseRect.height}",
311 List<Rectangle> variations = [
322 baseRect.height ~/ 2,
327 baseRect.width - 100,
328 baseRect.height - 50,
330 Rectangle(0, 0, baseRect.width, baseRect.height),
333 for (
int i = 0; i < variations.length; i++) {
334 Rectangle rect = variations[i];
336 "Variation ${i + 1}: ${rect.x}, ${rect.y}, ${rect.width}x${rect.height}",
344 Future<void> runExample() async {
345 print(
"=== BitmapRegionDecoder Example ===");
347 demonstrateBitmapRegionDecoderMethods();
348 demonstrateRectangleManipulation();
349 await demonstrateAdvancedFeatures();
350 demonstrateErrorHandling();
351 demonstratePerformanceOptimization();
354 await Future.delayed(Duration(seconds: 2));
356 print(
"=== Example completed ===");