Loading...
Searching...
No Matches
BitmapRegionDecoderExample.m
Go to the documentation of this file.
2
7@implementation BitmapRegionDecoderExample
8
9- (instancetype)init {
10 self = [super init];
11 if (self) {
12 [self loadSampleImageData];
13 }
14 return self;
15}
16
20- (void)loadSampleImageData {
21 // Simulate loading image data (in real app, this would be from file or network)
22 uint8_t pngHeader[] = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }; // PNG header
23 self.imageData = [NSData dataWithBytes:pngHeader length:sizeof(pngHeader)];
24 NSLog(@"Sample image data loaded (%lu bytes)", (unsigned long)self.imageData.length);
25}
26
31 if (self.imageData == nil) {
32 NSLog(@"No image data available");
33 return;
34 }
35
36 // [objc_BitmapRegionDecoder_newInstance]
37 // Create new instance of BitmapRegionDecoder
38 self.decoder = [NCBitmapRegionDecoder newInstanceWithData:self.imageData];
39 NSLog(@"Created BitmapRegionDecoder instance");
40 // [objc_BitmapRegionDecoder_newInstance]
41
42 // [objc_BitmapRegionDecoder_newInstanceFromImage]
43 // When you already have an Image (e.g. from SDK), create decoder without passing raw bytes again
44 NCImage *imageForDecoder = nil; // set from your pipeline when available
45 NCBitmapRegionDecoder *decoderFromImage = [NCBitmapRegionDecoder newInstanceFromImageWithData:imageForDecoder];
46 NSLog(@"Decoder from Image: %@", decoderFromImage != nil ? @"yes" : @"no");
47 // [objc_BitmapRegionDecoder_newInstanceFromImage]
48
49 // [objc_BitmapRegionDecoder_getWidth]
50 int32_t sourceWidth = self.decoder.width;
51 NSLog(@"Source image width: %d", sourceWidth);
52 // [objc_BitmapRegionDecoder_getWidth]
53
54 // [objc_BitmapRegionDecoder_getHeight]
55 int32_t sourceHeight = self.decoder.height;
56 NSLog(@"Source image height: %d", sourceHeight);
57 // [objc_BitmapRegionDecoder_getHeight]
58
59 // Demonstrate rectangle creation and usage
61
62 // Demonstrate region decoding
63 [self demonstrateRegionDecoding];
64
65 // Demonstrate different sample sizes
66 [self demonstrateSampleSizes];
67
68 // Demonstrate multiple regions
69 [self demonstrateMultipleRegions];
70}
71
76 // [objc_Rectangle_constructor]
77 // Create rectangle with x, y, width, height
78 NCRectangle *rect1 = [[NCRectangle alloc] initWithX:10 y:20 width:100 height:150];
79 NSLog(@"Created rectangle: x=%d, y=%d, width=%d, height=%d", rect1.x, rect1.y, rect1.width, rect1.height);
80 // [objc_Rectangle_constructor]
81
82 // [objc_Rectangle_getX]
83 // Get X coordinate
84 int32_t x = rect1.x;
85 NSLog(@"Rectangle X coordinate: %d", x);
86 // [objc_Rectangle_getX]
87
88 // [objc_Rectangle_getY]
89 // Get Y coordinate
90 int32_t y = rect1.y;
91 NSLog(@"Rectangle Y coordinate: %d", y);
92 // [objc_Rectangle_getY]
93
94 // [objc_Rectangle_getWidth]
95 // Get width
96 int32_t width = rect1.width;
97 NSLog(@"Rectangle width: %d", width);
98 // [objc_Rectangle_getWidth]
99
100 // [objc_Rectangle_getHeight]
101 // Get height
102 int32_t height = rect1.height;
103 NSLog(@"Rectangle height: %d", height);
104 // [objc_Rectangle_getHeight]
105
106 // Create different rectangles for different use cases
107 NCRectangle *fullImage = [[NCRectangle alloc] initWithX:0 y:0 width:1024 height:768];
108 NCRectangle *topLeft = [[NCRectangle alloc] initWithX:0 y:0 width:512 height:384];
109 NCRectangle *center = [[NCRectangle alloc] initWithX:256 y:192 width:512 height:384];
110 NCRectangle *bottomRight = [[NCRectangle alloc] initWithX:512 y:384 width:512 height:384];
111
112 NSLog(@"Created different rectangles for different regions");
113}
114
118- (void)demonstrateRegionDecoding {
119 if (self.decoder == nil) {
120 NSLog(@"Decoder not initialized");
121 return;
122 }
123
124 // Create a sample rectangle
125 NCRectangle *sampleRect = [[NCRectangle alloc] initWithX:50 y:50 width:200 height:200];
126
127 // [objc_BitmapRegionDecoder_decodeRegion]
128 // Decode region with sample size 1 (full resolution)
129 id decodedImage = [self.decoder decodeRegionWithRect:sampleRect sampleSize:1];
130 NSLog(@"Decoded region: %dx%d at sample size 1", sampleRect.width, sampleRect.height);
131 // [objc_BitmapRegionDecoder_decodeRegion]
132
133 // Demonstrate the decoded image
134 [self demonstrateDecodedImage:decodedImage withDescription:@"Sample Region"];
135}
136
140- (void)demonstrateSampleSizes {
141 if (self.decoder == nil) {
142 return;
143 }
144
145 NCRectangle *testRect = [[NCRectangle alloc] initWithX:0 y:0 width:400 height:300];
146
147 // Decode with different sample sizes
148 NSArray<NSNumber *> *sampleSizes = @[@1.0f, @2.0f, @4.0f, @8.0f];
149
150 for (NSNumber *sampleSizeNumber in sampleSizes) {
151 float sampleSize = sampleSizeNumber.floatValue;
152
153 // [objc_BitmapRegionDecoder_decodeRegion_sampleSize]
154 // Decode region with specific sample size
155 id decodedImage = [self.decoder decodeRegionWithRect:testRect sampleSize:sampleSize];
156 NSLog(@"Decoded region with sample size %.1f: %dx%d", sampleSize,
157 (int)(testRect.width / sampleSize), (int)(testRect.height / sampleSize));
158 // [objc_BitmapRegionDecoder_decodeRegion_sampleSize]
159
160 [self demonstrateDecodedImage:decodedImage withDescription:[NSString stringWithFormat:@"Sample Size %.1f", sampleSize]];
161 }
162}
163
167- (void)demonstrateMultipleRegions {
168 if (self.decoder == nil) {
169 return;
170 }
171
172 // Define multiple regions to decode
173 NSArray<NCRectangle *> *regions = @[
174 [[NCRectangle alloc] initWithX:0 y:0 width:256 height:256], // Top-left quadrant
175 [[NCRectangle alloc] initWithX:256 y:0 width:256 height:256], // Top-right quadrant
176 [[NCRectangle alloc] initWithX:0 y:256 width:256 height:256], // Bottom-left quadrant
177 [[NCRectangle alloc] initWithX:256 y:256 width:256 height:256] // Bottom-right quadrant
178 ];
179
180 NSLog(@"=== Decoding Multiple Regions ===");
181 for (int i = 0; i < regions.count; i++) {
182 NCRectangle *region = regions[i];
183
184 // [objc_BitmapRegionDecoder_decodeRegion_multiple]
185 // Decode multiple regions
186 id decodedImage = [self.decoder decodeRegionWithRect:region sampleSize:1];
187 NSLog(@"Region %d: %dx%d at (%d, %d)", i + 1, region.width, region.height, region.x, region.y);
188 // [objc_BitmapRegionDecoder_decodeRegion_multiple]
189
190 [self demonstrateDecodedImage:decodedImage withDescription:[NSString stringWithFormat:@"Region %d", i + 1]];
191 }
192}
193
197- (void)demonstrateDecodedImage:(id)image withDescription:(NSString *)description {
198 NSLog(@"--- %@ ---", description);
199 // Note: In real implementation, you would access image properties here
200 NSLog(@"Image decoded successfully");
201 NSLog(@"---");
202}
203
207- (void)demonstrateAdvancedFeatures {
208 NSLog(@"=== Advanced BitmapRegionDecoder Features ===");
209
210 if (self.imageData == nil) {
211 return;
212 }
213
214 // Create multiple decoders for different images
215 NSArray<NSData *> *imageDataList = @[
216 self.imageData,
217 [NSData dataWithBytes:(uint8_t[]){0xFF, 0xD8, 0xFF, 0xE0} length:4], // JPEG header
218 [NSData dataWithBytes:(uint8_t[]){0x47, 0x49, 0x46, 0x38} length:4] // GIF header
219 ];
220
221 for (int i = 0; i < imageDataList.count; i++) {
222 @try {
223 // [objc_BitmapRegionDecoder_newInstance_advanced]
224 // Create decoder for different image types
225 NCBitmapRegionDecoder *decoder = [NCBitmapRegionDecoder newInstanceWithData:imageDataList[i]];
226 NSLog(@"Created decoder for image type %d", i + 1);
227 // [objc_BitmapRegionDecoder_newInstance_advanced]
228
229 // Test decoding with different regions
230 NCRectangle *testRect = [[NCRectangle alloc] initWithX:0 y:0 width:100 height:100];
231 id decodedImage = [decoder decodeRegionWithRect:testRect sampleSize:1];
232 NSLog(@"Successfully decoded region from image type %d", i + 1);
233
234 } @catch (NSException *exception) {
235 NSLog(@"Failed to decode image type %d: %@", i + 1, exception.reason);
236 }
237 }
238}
239
244 NSLog(@"=== Error Handling ===");
245
246 // Test with invalid image data
247 NSData *invalidData = [NSData dataWithBytes:(uint8_t[]){0x00, 0x01, 0x02, 0x03} length:4];
248
249 @try {
250 // [objc_BitmapRegionDecoder_newInstance_error]
251 // Create decoder with invalid data
252 NCBitmapRegionDecoder *decoder = [NCBitmapRegionDecoder newInstanceWithData:invalidData];
253 NSLog(@"Created decoder with invalid data");
254 // [objc_BitmapRegionDecoder_newInstance_error]
255
256 // Try to decode region
257 NCRectangle *rect = [[NCRectangle alloc] initWithX:0 y:0 width:50 height:50];
258 id image = [decoder decodeRegionWithRect:rect sampleSize:1];
259 NSLog(@"Successfully decoded region from invalid data");
260
261 } @catch (NSException *exception) {
262 NSLog(@"Expected error when creating decoder with invalid data: %@", exception.reason);
263 }
264
265 // Test with invalid rectangle
266 if (self.decoder != nil) {
267 @try {
268 NCRectangle *invalidRect = [[NCRectangle alloc] initWithX:-10 y:-10 width:100 height:100];
269 id image = [self.decoder decodeRegionWithRect:invalidRect sampleSize:1];
270 NSLog(@"Successfully decoded region with negative coordinates");
271
272 } @catch (NSException *exception) {
273 NSLog(@"Expected error with invalid rectangle: %@", exception.reason);
274 }
275 }
276}
277
282 NSLog(@"=== Performance Optimization ===");
283
284 if (self.decoder == nil) {
285 return;
286 }
287
288 // Test different sample sizes for performance
289 NCRectangle *largeRect = [[NCRectangle alloc] initWithX:0 y:0 width:800 height:600];
290 NSArray<NSNumber *> *sampleSizes = @[@1.0f, @2.0f, @4.0f, @8.0f, @16.0f];
291
292 for (NSNumber *sampleSizeNumber in sampleSizes) {
293 float sampleSize = sampleSizeNumber.floatValue;
294
295 NSDate *startTime = [NSDate date];
296
297 id image = [self.decoder decodeRegionWithRect:largeRect sampleSize:sampleSize];
298
299 NSTimeInterval elapsedTime = [[NSDate date] timeIntervalSinceDate:startTime] * 1000;
300 NSLog(@"Sample size %.1f: %.0fms", sampleSize, elapsedTime);
301 }
302}
303
308 NSLog(@"=== Rectangle Manipulation ===");
309
310 // Create base rectangle
311 NCRectangle *baseRect = [[NCRectangle alloc] initWithX:100 y:100 width:200 height:150];
312 NSLog(@"Base rectangle: %d, %d, %dx%d", baseRect.x, baseRect.y, baseRect.width, baseRect.height);
313
314 // Create different variations
315 NSArray<NCRectangle *> *variations = @[
316 [[NCRectangle alloc] initWithX:baseRect.x y:baseRect.y width:baseRect.width / 2 height:baseRect.height], // Half width
317 [[NCRectangle alloc] initWithX:baseRect.x y:baseRect.y width:baseRect.width height:baseRect.height / 2], // Half height
318 [[NCRectangle alloc] initWithX:baseRect.x + 50 y:baseRect.y + 25 width:baseRect.width - 100 height:baseRect.height - 50], // Centered smaller
319 [[NCRectangle alloc] initWithX:0 y:0 width:baseRect.width height:baseRect.height] // Top-left aligned
320 ];
321
322 for (int i = 0; i < variations.count; i++) {
323 NCRectangle *rect = variations[i];
324 NSLog(@"Variation %d: %d, %d, %dx%d", i + 1, rect.x, rect.y, rect.width, rect.height);
325 }
326}
327
331- (void)runExample {
332 NSLog(@"=== BitmapRegionDecoder Example ===");
333
334 [self demonstrateBitmapRegionDecoderMethods];
335 [self demonstrateRectangleManipulation];
336 [self demonstrateAdvancedFeatures];
337 [self demonstrateErrorHandling];
338 [self demonstratePerformanceOptimization];
339
340 // Wait a bit for processing
341 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
342 NSLog(@"=== Example completed ===");
343 });
344}
345
346@end
347
351int main(int argc, const char * argv[]) {
352 @autoreleasepool {
354 [example runExample];
355
356 // Keep the app running for a while to see the output
357 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:10.0]];
358 }
359 return 0;
360}