12 [
self loadSampleImageData];
20- (void)loadSampleImageData {
22 uint8_t pngHeader[] = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
23 self.imageData = [NSData dataWithBytes:pngHeader length:sizeof(pngHeader)];
24 NSLog(
@"Sample image data loaded (%lu bytes)", (
unsigned long)
self.
imageData.length);
30- (void)demonstrateBitmapRegionDecoderMethods {
32 NSLog(
@"No image data available");
38 self.decoder = [NCBitmapRegionDecoder newInstanceWithData:
self.imageData];
39 NSLog(
@"Created BitmapRegionDecoder instance");
43 [
self demonstrateRectangleUsage];
46 [
self demonstrateRegionDecoding];
49 [
self demonstrateSampleSizes];
52 [
self demonstrateMultipleRegions];
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);
68 NSLog(
@"Rectangle X coordinate: %d", x);
74 NSLog(
@"Rectangle Y coordinate: %d", y);
79 int32_t width = rect1.width;
80 NSLog(
@"Rectangle width: %d", width);
85 int32_t height = rect1.height;
86 NSLog(
@"Rectangle height: %d", height);
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];
95 NSLog(
@"Created different rectangles for different regions");
101- (void)demonstrateRegionDecoding {
103 NSLog(
@"Decoder not initialized");
108 NCRectangle *sampleRect = [[NCRectangle alloc] initWithX:50 y:50 width:200 height:200];
112 id decodedImage = [
self.decoder decodeRegionWithRect:sampleRect sampleSize:1];
113 NSLog(
@"Decoded region: %dx%d at sample size 1", sampleRect.width, sampleRect.height);
117 [
self demonstrateDecodedImage:decodedImage withDescription:@"Sample Region"];
123- (void)demonstrateSampleSizes {
128 NCRectangle *testRect = [[NCRectangle alloc] initWithX:0 y:0 width:400 height:300];
131 NSArray<NSNumber *> *sampleSizes = @[@1.0f, @2.0f, @4.0f, @8.0f];
133 for (NSNumber *sampleSizeNumber in sampleSizes) {
134 float sampleSize = sampleSizeNumber.floatValue;
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));
143 [
self demonstrateDecodedImage:decodedImage withDescription:[NSString stringWithFormat:@"Sample Size %.1f", sampleSize]];
150- (void)demonstrateMultipleRegions {
156 NSArray<NCRectangle *> *regions = @[
157 [[NCRectangle alloc] initWithX:0 y:0 width:256 height:256],
158 [[NCRectangle alloc] initWithX:256 y:0 width:256 height:256],
159 [[NCRectangle alloc] initWithX:0 y:256 width:256 height:256],
160 [[NCRectangle alloc] initWithX:256 y:256 width:256 height:256]
163 NSLog(
@"=== Decoding Multiple Regions ===");
164 for (
int i = 0; i < regions.count; i++) {
165 NCRectangle *region = regions[i];
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);
173 [
self demonstrateDecodedImage:decodedImage withDescription:[NSString stringWithFormat:@"Region %d", i + 1]];
180- (void)demonstrateDecodedImage:(
id)image withDescription:(NSString *)description {
181 NSLog(
@"--- %@ ---", description);
183 NSLog(
@"Image decoded successfully");
190- (void)demonstrateAdvancedFeatures {
191 NSLog(
@"=== Advanced BitmapRegionDecoder Features ===");
198 NSArray<NSData *> *imageDataList = @[
200 [NSData dataWithBytes:(uint8_t[]){0xFF, 0xD8, 0xFF, 0xE0} length:4],
201 [NSData dataWithBytes:(uint8_t[]){0x47, 0x49, 0x46, 0x38} length:4]
204 for (
int i = 0; i < imageDataList.count; i++) {
208 NCBitmapRegionDecoder *
decoder = [NCBitmapRegionDecoder newInstanceWithData:imageDataList[i]];
209 NSLog(
@"Created decoder for image type %d", i + 1);
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);
217 }
@catch (NSException *exception) {
218 NSLog(
@"Failed to decode image type %d: %@", i + 1, exception.reason);
226- (void)demonstrateErrorHandling {
227 NSLog(
@"=== Error Handling ===");
230 NSData *invalidData = [NSData dataWithBytes:(uint8_t[]){0x00, 0x01, 0x02, 0x03} length:4];
235 NCBitmapRegionDecoder *
decoder = [NCBitmapRegionDecoder newInstanceWithData:invalidData];
236 NSLog(
@"Created decoder with invalid data");
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");
244 }
@catch (NSException *exception) {
245 NSLog(
@"Expected error when creating decoder with invalid data: %@", exception.reason);
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");
255 }
@catch (NSException *exception) {
256 NSLog(
@"Expected error with invalid rectangle: %@", exception.reason);
264- (void)demonstratePerformanceOptimization {
265 NSLog(
@"=== Performance Optimization ===");
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];
275 for (NSNumber *sampleSizeNumber in sampleSizes) {
276 float sampleSize = sampleSizeNumber.floatValue;
278 NSDate *startTime = [NSDate date];
280 id image = [
self.decoder decodeRegionWithRect:largeRect sampleSize:sampleSize];
282 NSTimeInterval elapsedTime = [[NSDate date] timeIntervalSinceDate:startTime] * 1000;
283 NSLog(
@"Sample size %.1f: %.0fms", sampleSize, elapsedTime);
290- (void)demonstrateRectangleManipulation {
291 NSLog(
@"=== Rectangle Manipulation ===");
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);
298 NSArray<NCRectangle *> *variations = @[
299 [[NCRectangle alloc] initWithX:baseRect.x y:baseRect.y width:baseRect.width / 2 height:baseRect.height],
300 [[NCRectangle alloc] initWithX:baseRect.x y:baseRect.y width:baseRect.width height:baseRect.height / 2],
301 [[NCRectangle alloc] initWithX:baseRect.x + 50 y:baseRect.y + 25 width:baseRect.width - 100 height:baseRect.height - 50],
302 [[NCRectangle alloc] initWithX:0 y:0 width:baseRect.width height:baseRect.height]
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);
315 NSLog(
@"=== BitmapRegionDecoder Example ===");
317 [
self demonstrateBitmapRegionDecoderMethods];
318 [
self demonstrateRectangleManipulation];
319 [
self demonstrateAdvancedFeatures];
320 [
self demonstrateErrorHandling];
321 [
self demonstratePerformanceOptimization];
324 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
325 NSLog(
@"=== Example completed ===");
334int main(
int argc,
const char * argv[]) {
337 [example runExample];
340 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:10.0]];