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);
32 NSLog(
@"No image data available");
39 NSLog(
@"Created BitmapRegionDecoder instance");
46 NSLog(
@"Decoder from Image: %@", decoderFromImage != nil ?
@"yes" :
@"no");
50 int32_t sourceWidth =
self.decoder.width;
51 NSLog(
@"Source image width: %d", sourceWidth);
55 int32_t sourceHeight =
self.decoder.height;
56 NSLog(
@"Source image height: %d", sourceHeight);
63 [
self demonstrateRegionDecoding];
66 [
self demonstrateSampleSizes];
69 [
self demonstrateMultipleRegions];
79 NSLog(
@"Created rectangle: x=%d, y=%d, width=%d, height=%d", rect1.
x, rect1.
y, rect1.
width, rect1.
height);
85 NSLog(
@"Rectangle X coordinate: %d", x);
91 NSLog(
@"Rectangle Y coordinate: %d", y);
96 int32_t width = rect1.
width;
97 NSLog(
@"Rectangle width: %d", width);
102 int32_t height = rect1.
height;
103 NSLog(
@"Rectangle height: %d", height);
112 NSLog(
@"Created different rectangles for different regions");
118- (void)demonstrateRegionDecoding {
120 NSLog(
@"Decoder not initialized");
125 NCRectangle *sampleRect = [[
NCRectangle alloc] initWithX:50 y:50 width:200 height:200];
129 id decodedImage = [
self.decoder decodeRegionWithRect:sampleRect sampleSize:1];
130 NSLog(
@"Decoded region: %dx%d at sample size 1", sampleRect.
width, sampleRect.
height);
134 [
self demonstrateDecodedImage:decodedImage withDescription:@"Sample Region"];
140- (void)demonstrateSampleSizes {
145 NCRectangle *testRect = [[
NCRectangle alloc] initWithX:0 y:0 width:400 height:300];
148 NSArray<NSNumber *> *sampleSizes = @[@1.0f, @2.0f, @4.0f, @8.0f];
150 for (NSNumber *sampleSizeNumber in sampleSizes) {
151 float sampleSize = sampleSizeNumber.floatValue;
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));
160 [
self demonstrateDecodedImage:decodedImage withDescription:[NSString stringWithFormat:@"Sample Size %.1f", sampleSize]];
167- (void)demonstrateMultipleRegions {
173 NSArray<NCRectangle *> *regions = @[
174 [[
NCRectangle alloc] initWithX:0 y:0 width:256 height:256],
175 [[
NCRectangle alloc] initWithX:256 y:0 width:256 height:256],
176 [[
NCRectangle alloc] initWithX:0 y:256 width:256 height:256],
177 [[
NCRectangle alloc] initWithX:256 y:256 width:256 height:256]
180 NSLog(
@"=== Decoding Multiple Regions ===");
181 for (
int i = 0; i < regions.count; i++) {
182 NCRectangle *region = regions[i];
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);
190 [
self demonstrateDecodedImage:decodedImage withDescription:[NSString stringWithFormat:@"Region %d", i + 1]];
197- (void)demonstrateDecodedImage:(
id)image withDescription:(NSString *)description {
198 NSLog(
@"--- %@ ---", description);
200 NSLog(
@"Image decoded successfully");
207- (void)demonstrateAdvancedFeatures {
208 NSLog(
@"=== Advanced BitmapRegionDecoder Features ===");
215 NSArray<NSData *> *imageDataList = @[
217 [NSData dataWithBytes:(uint8_t[]){0xFF, 0xD8, 0xFF, 0xE0} length:4],
218 [NSData dataWithBytes:(uint8_t[]){0x47, 0x49, 0x46, 0x38} length:4]
221 for (
int i = 0; i < imageDataList.count; i++) {
226 NSLog(
@"Created decoder for image type %d", i + 1);
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);
234 }
@catch (NSException *exception) {
235 NSLog(
@"Failed to decode image type %d: %@", i + 1, exception.reason);
244 NSLog(
@"=== Error Handling ===");
247 NSData *invalidData = [
NSData dataWithBytes:(uint8_t[]){0
x00, 0x01, 0x02, 0x03} length:4];
253 NSLog(
@"Created decoder with invalid data");
258 id image = [
decoder decodeRegionWithRect:rect sampleSize:1];
259 NSLog(
@"Successfully decoded region from invalid data");
261 }
@catch (NSException *exception) {
262 NSLog(
@"Expected error when creating decoder with invalid data: %@", exception.reason);
269 id image = [
self.
decoder decodeRegionWithRect:invalidRect sampleSize:1];
270 NSLog(
@"Successfully decoded region with negative coordinates");
272 }
@catch (NSException *exception) {
273 NSLog(
@"Expected error with invalid rectangle: %@", exception.reason);
282 NSLog(
@"=== Performance Optimization ===");
290 NSArray<NSNumber *> *sampleSizes = @[@1.0
f, @2.0f, @4.0f, @8.0f, @16.0f];
292 for (NSNumber *sampleSizeNumber in sampleSizes) {
293 float sampleSize = sampleSizeNumber.floatValue;
295 NSDate *startTime = [
NSDate date];
297 id image = [
self.
decoder decodeRegionWithRect:largeRect sampleSize:sampleSize];
299 NSTimeInterval elapsedTime = [[
NSDate date] timeIntervalSinceDate:startTime] * 1000;
300 NSLog(
@"Sample size %.1f: %.0fms", sampleSize, elapsedTime);
308 NSLog(
@"=== Rectangle Manipulation ===");
312 NSLog(
@"Base rectangle: %d, %d, %dx%d", baseRect.
x, baseRect.
y, baseRect.
width, baseRect.
height);
315 NSArray<NCRectangle *> *variations = @[
316 [[
NCRectangle alloc] initWithX:baseRect.x y:baseRect.y width:baseRect.width / 2 height:baseRect.height],
317 [[
NCRectangle alloc] initWithX:baseRect.x y:baseRect.y width:baseRect.width height:baseRect.height / 2],
318 [[
NCRectangle alloc] initWithX:baseRect.x + 50 y:baseRect.y + 25 width:baseRect.width - 100 height:baseRect.height - 50],
319 [[
NCRectangle alloc] initWithX:0 y:0 width:baseRect.width height:baseRect.height]
322 for (
int i = 0; i < variations.count; i++) {
324 NSLog(
@"Variation %d: %d, %d, %dx%d", i + 1, rect.
x, rect.
y, rect.
width, rect.
height);
332 NSLog(
@"=== BitmapRegionDecoder Example ===");
334 [
self demonstrateBitmapRegionDecoderMethods];
335 [
self demonstrateRectangleManipulation];
336 [
self demonstrateAdvancedFeatures];
337 [
self demonstrateErrorHandling];
338 [
self demonstratePerformanceOptimization];
341 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
342 NSLog(
@"=== Example completed ===");
351int main(
int argc,
const char * argv[]) {
354 [example runExample];
357 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:10.0]];