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
30- (void)demonstrateBitmapRegionDecoderMethods {
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 // Demonstrate rectangle creation and usage
43 [self demonstrateRectangleUsage];
44
45 // Demonstrate region decoding
46 [self demonstrateRegionDecoding];
47
48 // Demonstrate different sample sizes
49 [self demonstrateSampleSizes];
50
51 // Demonstrate multiple regions
52 [self demonstrateMultipleRegions];
53}
54
59 // [objc_Rectangle_constructor]
60 // Create rectangle with x, y, width, height
61 NCRectangle *rect1 = [[NCRectangle alloc] initWithX:10 y:20 width:100 height:150];
62 NSLog(@"Created rectangle: x=%d, y=%d, width=%d, height=%d", rect1.x, rect1.y, rect1.width, rect1.height);
63 // [objc_Rectangle_constructor]
64
65 // [objc_Rectangle_getX]
66 // Get X coordinate
67 int32_t x = rect1.x;
68 NSLog(@"Rectangle X coordinate: %d", x);
69 // [objc_Rectangle_getX]
70
71 // [objc_Rectangle_getY]
72 // Get Y coordinate
73 int32_t y = rect1.y;
74 NSLog(@"Rectangle Y coordinate: %d", y);
75 // [objc_Rectangle_getY]
76
77 // [objc_Rectangle_getWidth]
78 // Get width
79 int32_t width = rect1.width;
80 NSLog(@"Rectangle width: %d", width);
81 // [objc_Rectangle_getWidth]
82
83 // [objc_Rectangle_getHeight]
84 // Get height
85 int32_t height = rect1.height;
86 NSLog(@"Rectangle height: %d", height);
87 // [objc_Rectangle_getHeight]
88
89 // Create different rectangles for different use cases
90 NCRectangle *fullImage = [[NCRectangle alloc] initWithX:0 y:0 width:1024 height:768];
91 NCRectangle *topLeft = [[NCRectangle alloc] initWithX:0 y:0 width:512 height:384];
92 NCRectangle *center = [[NCRectangle alloc] initWithX:256 y:192 width:512 height:384];
93 NCRectangle *bottomRight = [[NCRectangle alloc] initWithX:512 y:384 width:512 height:384];
94
95 NSLog(@"Created different rectangles for different regions");
96}
97
101- (void)demonstrateRegionDecoding {
102 if (self.decoder == nil) {
103 NSLog(@"Decoder not initialized");
104 return;
105 }
106
107 // Create a sample rectangle
108 NCRectangle *sampleRect = [[NCRectangle alloc] initWithX:50 y:50 width:200 height:200];
109
110 // [objc_BitmapRegionDecoder_decodeRegion]
111 // Decode region with sample size 1 (full resolution)
112 id decodedImage = [self.decoder decodeRegionWithRect:sampleRect sampleSize:1];
113 NSLog(@"Decoded region: %dx%d at sample size 1", sampleRect.width, sampleRect.height);
114 // [objc_BitmapRegionDecoder_decodeRegion]
115
116 // Demonstrate the decoded image
117 [self demonstrateDecodedImage:decodedImage withDescription:@"Sample Region"];
118}
119
123- (void)demonstrateSampleSizes {
124 if (self.decoder == nil) {
125 return;
126 }
127
128 NCRectangle *testRect = [[NCRectangle alloc] initWithX:0 y:0 width:400 height:300];
129
130 // Decode with different sample sizes
131 NSArray<NSNumber *> *sampleSizes = @[@1.0f, @2.0f, @4.0f, @8.0f];
132
133 for (NSNumber *sampleSizeNumber in sampleSizes) {
134 float sampleSize = sampleSizeNumber.floatValue;
135
136 // [objc_BitmapRegionDecoder_decodeRegion_sampleSize]
137 // Decode region with specific sample size
138 id decodedImage = [self.decoder decodeRegionWithRect:testRect sampleSize:sampleSize];
139 NSLog(@"Decoded region with sample size %.1f: %dx%d", sampleSize,
140 (int)(testRect.width / sampleSize), (int)(testRect.height / sampleSize));
141 // [objc_BitmapRegionDecoder_decodeRegion_sampleSize]
142
143 [self demonstrateDecodedImage:decodedImage withDescription:[NSString stringWithFormat:@"Sample Size %.1f", sampleSize]];
144 }
145}
146
150- (void)demonstrateMultipleRegions {
151 if (self.decoder == nil) {
152 return;
153 }
154
155 // Define multiple regions to decode
156 NSArray<NCRectangle *> *regions = @[
157 [[NCRectangle alloc] initWithX:0 y:0 width:256 height:256], // Top-left quadrant
158 [[NCRectangle alloc] initWithX:256 y:0 width:256 height:256], // Top-right quadrant
159 [[NCRectangle alloc] initWithX:0 y:256 width:256 height:256], // Bottom-left quadrant
160 [[NCRectangle alloc] initWithX:256 y:256 width:256 height:256] // Bottom-right quadrant
161 ];
162
163 NSLog(@"=== Decoding Multiple Regions ===");
164 for (int i = 0; i < regions.count; i++) {
165 NCRectangle *region = regions[i];
166
167 // [objc_BitmapRegionDecoder_decodeRegion_multiple]
168 // Decode multiple regions
169 id decodedImage = [self.decoder decodeRegionWithRect:region sampleSize:1];
170 NSLog(@"Region %d: %dx%d at (%d, %d)", i + 1, region.width, region.height, region.x, region.y);
171 // [objc_BitmapRegionDecoder_decodeRegion_multiple]
172
173 [self demonstrateDecodedImage:decodedImage withDescription:[NSString stringWithFormat:@"Region %d", i + 1]];
174 }
175}
176
180- (void)demonstrateDecodedImage:(id)image withDescription:(NSString *)description {
181 NSLog(@"--- %@ ---", description);
182 // Note: In real implementation, you would access image properties here
183 NSLog(@"Image decoded successfully");
184 NSLog(@"---");
185}
186
190- (void)demonstrateAdvancedFeatures {
191 NSLog(@"=== Advanced BitmapRegionDecoder Features ===");
192
193 if (self.imageData == nil) {
194 return;
195 }
196
197 // Create multiple decoders for different images
198 NSArray<NSData *> *imageDataList = @[
199 self.imageData,
200 [NSData dataWithBytes:(uint8_t[]){0xFF, 0xD8, 0xFF, 0xE0} length:4], // JPEG header
201 [NSData dataWithBytes:(uint8_t[]){0x47, 0x49, 0x46, 0x38} length:4] // GIF header
202 ];
203
204 for (int i = 0; i < imageDataList.count; i++) {
205 @try {
206 // [objc_BitmapRegionDecoder_newInstance_advanced]
207 // Create decoder for different image types
208 NCBitmapRegionDecoder *decoder = [NCBitmapRegionDecoder newInstanceWithData:imageDataList[i]];
209 NSLog(@"Created decoder for image type %d", i + 1);
210 // [objc_BitmapRegionDecoder_newInstance_advanced]
211
212 // Test decoding with different regions
213 NCRectangle *testRect = [[NCRectangle alloc] initWithX:0 y:0 width:100 height:100];
214 id decodedImage = [decoder decodeRegionWithRect:testRect sampleSize:1];
215 NSLog(@"Successfully decoded region from image type %d", i + 1);
216
217 } @catch (NSException *exception) {
218 NSLog(@"Failed to decode image type %d: %@", i + 1, exception.reason);
219 }
220 }
221}
222
226- (void)demonstrateErrorHandling {
227 NSLog(@"=== Error Handling ===");
228
229 // Test with invalid image data
230 NSData *invalidData = [NSData dataWithBytes:(uint8_t[]){0x00, 0x01, 0x02, 0x03} length:4];
231
232 @try {
233 // [objc_BitmapRegionDecoder_newInstance_error]
234 // Create decoder with invalid data
235 NCBitmapRegionDecoder *decoder = [NCBitmapRegionDecoder newInstanceWithData:invalidData];
236 NSLog(@"Created decoder with invalid data");
237 // [objc_BitmapRegionDecoder_newInstance_error]
238
239 // Try to decode region
240 NCRectangle *rect = [[NCRectangle alloc] initWithX:0 y:0 width:50 height:50];
241 id image = [decoder decodeRegionWithRect:rect sampleSize:1];
242 NSLog(@"Successfully decoded region from invalid data");
243
244 } @catch (NSException *exception) {
245 NSLog(@"Expected error when creating decoder with invalid data: %@", exception.reason);
246 }
247
248 // Test with invalid rectangle
249 if (self.decoder != nil) {
250 @try {
251 NCRectangle *invalidRect = [[NCRectangle alloc] initWithX:-10 y:-10 width:100 height:100];
252 id image = [self.decoder decodeRegionWithRect:invalidRect sampleSize:1];
253 NSLog(@"Successfully decoded region with negative coordinates");
254
255 } @catch (NSException *exception) {
256 NSLog(@"Expected error with invalid rectangle: %@", exception.reason);
257 }
258 }
259}
260
264- (void)demonstratePerformanceOptimization {
265 NSLog(@"=== Performance Optimization ===");
266
267 if (self.decoder == nil) {
268 return;
269 }
270
271 // Test different sample sizes for performance
272 NCRectangle *largeRect = [[NCRectangle alloc] initWithX:0 y:0 width:800 height:600];
273 NSArray<NSNumber *> *sampleSizes = @[@1.0f, @2.0f, @4.0f, @8.0f, @16.0f];
274
275 for (NSNumber *sampleSizeNumber in sampleSizes) {
276 float sampleSize = sampleSizeNumber.floatValue;
277
278 NSDate *startTime = [NSDate date];
279
280 id image = [self.decoder decodeRegionWithRect:largeRect sampleSize:sampleSize];
281
282 NSTimeInterval elapsedTime = [[NSDate date] timeIntervalSinceDate:startTime] * 1000;
283 NSLog(@"Sample size %.1f: %.0fms", sampleSize, elapsedTime);
284 }
285}
286
290- (void)demonstrateRectangleManipulation {
291 NSLog(@"=== Rectangle Manipulation ===");
292
293 // Create base rectangle
294 NCRectangle *baseRect = [[NCRectangle alloc] initWithX:100 y:100 width:200 height:150];
295 NSLog(@"Base rectangle: %d, %d, %dx%d", baseRect.x, baseRect.y, baseRect.width, baseRect.height);
296
297 // Create different variations
298 NSArray<NCRectangle *> *variations = @[
299 [[NCRectangle alloc] initWithX:baseRect.x y:baseRect.y width:baseRect.width / 2 height:baseRect.height], // Half width
300 [[NCRectangle alloc] initWithX:baseRect.x y:baseRect.y width:baseRect.width height:baseRect.height / 2], // Half height
301 [[NCRectangle alloc] initWithX:baseRect.x + 50 y:baseRect.y + 25 width:baseRect.width - 100 height:baseRect.height - 50], // Centered smaller
302 [[NCRectangle alloc] initWithX:0 y:0 width:baseRect.width height:baseRect.height] // Top-left aligned
303 ];
304
305 for (int i = 0; i < variations.count; i++) {
306 NCRectangle *rect = variations[i];
307 NSLog(@"Variation %d: %d, %d, %dx%d", i + 1, rect.x, rect.y, rect.width, rect.height);
308 }
309}
310
314- (void)runExample {
315 NSLog(@"=== BitmapRegionDecoder Example ===");
316
317 [self demonstrateBitmapRegionDecoderMethods];
318 [self demonstrateRectangleManipulation];
319 [self demonstrateAdvancedFeatures];
320 [self demonstrateErrorHandling];
321 [self demonstratePerformanceOptimization];
322
323 // Wait a bit for processing
324 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
325 NSLog(@"=== Example completed ===");
326 });
327}
328
329@end
330
334int main(int argc, const char * argv[]) {
335 @autoreleasepool {
337 [example runExample];
338
339 // Keep the app running for a while to see the output
340 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:10.0]];
341 }
342 return 0;
343}