Skip to content
Permalink
Browse files

Converted to ARC. Removed the test images.

  • Loading branch information...
hollance committed Dec 22, 2012
1 parent b9129ec commit 0b3a2003b1dcd57a0a74b4a099890932b17fa7c1
@@ -1,4 +1,4 @@
Copyright (c) 2011 Matthijs Hollemans
Copyright (c) 2011-2012 Matthijs Hollemans

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -104,7 +104,7 @@
7B26E641134A115C00C5BC50 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0420;
LastUpgradeCheck = 0450;
ORGANIZATIONNAME = Hollance;
};
buildConfigurationList = 7B26E644134A115C00C5BC50 /* Build configuration list for PBXProject "ShrinkPng" */;
@@ -172,27 +172,43 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD_64_BIT)";
CLANG_ENABLE_OBJC_ARC = YES;
COPY_PHASE_STRIP = NO;
GCC_DYNAMIC_NO_PIC = NO;
GCC_ENABLE_OBJC_EXCEPTIONS = YES;
GCC_PRECOMPILE_PREFIX_HEADER = NO;
GCC_PREFIX_HEADER = "";
MACOSX_DEPLOYMENT_TARGET = 10.6.8;
PRODUCT_NAME = "$(TARGET_NAME)";
WARNING_CFLAGS = "-Wall";
RUN_CLANG_STATIC_ANALYZER = YES;
WARNING_CFLAGS = (
"-Wall",
"-Wextra",
"-Wno-unused-parameter",
);
};
name = Debug;
};
7B26E65A134A115C00C5BC50 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD_64_BIT)";
CLANG_ENABLE_OBJC_ARC = YES;
COPY_PHASE_STRIP = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
GCC_ENABLE_OBJC_EXCEPTIONS = YES;
GCC_PRECOMPILE_PREFIX_HEADER = NO;
GCC_PREFIX_HEADER = "";
MACOSX_DEPLOYMENT_TARGET = 10.6.8;
PRODUCT_NAME = "$(TARGET_NAME)";
WARNING_CFLAGS = "-Wall";
RUN_CLANG_STATIC_ANALYZER = YES;
WARNING_CFLAGS = (
"-Wall",
"-Wextra",
"-Wno-unused-parameter",
);
};
name = Release;
};
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
94 main.m
@@ -18,7 +18,7 @@ CGImageRef CreateImageFromPNG(NSString *filename)
BOOL SaveImageToPNG(NSString *filename, CGImageRef image)
{
CGImageDestinationRef dest = CGImageDestinationCreateWithURL(
(CFURLRef)[NSURL fileURLWithPath:filename], kUTTypePNG, 1, NULL);
(__bridge CFURLRef)[NSURL fileURLWithPath:filename], kUTTypePNG, 1, NULL);

if (dest == NULL)
{
@@ -67,10 +67,9 @@ BOOL SaveImageToPNG(NSString *filename, CGImageRef image)

CGRect rect = CGRectMake(0.0f, 0.0f, width, height);
CGContextDrawImage(context, rect, image);
unsigned char *imageData = CGBitmapContextGetData(context);
CGContextRelease(context);

return imageData;
return contextData;
}

CGImageRef CreateImageFromBytes(unsigned char *data, size_t width, size_t height)
@@ -101,7 +100,7 @@ CGImageRef CreateImageFromBytes(unsigned char *data, size_t width, size_t height

unsigned char *ShrinkBitmapData(unsigned char *inData, size_t width, size_t height)
{
unsigned char *outData = (unsigned char *)calloc(width*height, 4);
unsigned char *outData = (unsigned char *)calloc(width * height, 4);
if (outData == NULL)
{
fprintf(stderr, "Could not allocate memory\n");
@@ -110,9 +109,9 @@ CGImageRef CreateImageFromBytes(unsigned char *data, size_t width, size_t height

unsigned char *ptr = outData;

for (int y = 0; y < height; y += 2)
for (unsigned int y = 0; y < height; y += 2)
{
for (int x = 0; x < width; x += 2)
for (unsigned int x = 0; x < width; x += 2)
{
size_t offset1 = (y*width + x)*4; // top left
size_t offset2 = offset1 + 4; // top right
@@ -139,7 +138,7 @@ CGImageRef CreateImageFromBytes(unsigned char *data, size_t width, size_t height
int g4 = inData[offset4 + 2];
int b4 = inData[offset4 + 3];

// We do +2 in order to round up if the remainder is 0.5 or more.
// We do + 2 in order to round up if the remainder is 0.5 or more.
int r = (r1 + r2 + r3 + r4 + 2) / 4;
int g = (g1 + g2 + g3 + g4 + 2) / 4;
int b = (b1 + b2 + b3 + b4 + 2) / 4;
@@ -157,61 +156,60 @@ CGImageRef CreateImageFromBytes(unsigned char *data, size_t width, size_t height

int main(int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

if (argc != 2)
@autoreleasepool
{
fprintf(stderr, "Usage: ShrinkPng <input@2x.png>\n");
return EXIT_FAILURE;
}
if (argc != 2)
{
fprintf(stderr, "Usage: ShrinkPng <input@2x.png>\n");
return EXIT_FAILURE;
}

NSString *inputFilename = [NSString stringWithCString:argv[1] encoding:NSUTF8StringEncoding];
inputFilename = [inputFilename stringByExpandingTildeInPath];
NSString *inputFilename = @(argv[1]);
inputFilename = [inputFilename stringByExpandingTildeInPath];

NSString *withoutExtension = [inputFilename stringByDeletingPathExtension];
if (![withoutExtension hasSuffix:@"@2x"])
{
fprintf(stderr, "Input file must be @2x\n");
return EXIT_FAILURE;
}

NSString *without2x = [withoutExtension substringToIndex:withoutExtension.length - 3];
if (without2x.length == 0)
{
fprintf(stderr, "Invalid input filename\n");
return EXIT_FAILURE;
}

NSString *outputFilename = [without2x stringByAppendingPathExtension:@"png"];
NSString *withoutExtension = [inputFilename stringByDeletingPathExtension];
if (![withoutExtension hasSuffix:@"@2x"])
{
fprintf(stderr, "Input file must be @2x\n");
return EXIT_FAILURE;
}

CGImageRef inImage = CreateImageFromPNG(inputFilename);
if (inImage != NULL)
{
size_t width = CGImageGetWidth(inImage);
size_t height = CGImageGetHeight(inImage);
NSString *without2x = [withoutExtension substringToIndex:[withoutExtension length] - 3];
if ([without2x length] == 0)
{
fprintf(stderr, "Invalid input filename\n");
return EXIT_FAILURE;
}

unsigned char *inData = CreateBytesFromImage(inImage);
CGImageRelease(inImage);
NSString *outputFilename = [without2x stringByAppendingPathExtension:@"png"];

if (inData != NULL)
CGImageRef inImage = CreateImageFromPNG(inputFilename);
if (inImage != NULL)
{
unsigned char *outData = ShrinkBitmapData(inData, width, height);
free(inData);
size_t width = CGImageGetWidth(inImage);
size_t height = CGImageGetHeight(inImage);

if (outData != NULL)
unsigned char *inData = CreateBytesFromImage(inImage);
CGImageRelease(inImage);

if (inData != NULL)
{
CGImageRef outImage = CreateImageFromBytes(outData, width / 2, height / 2);
free(outData);
unsigned char *outData = ShrinkBitmapData(inData, width, height);
free(inData);

if (outImage != NULL)
if (outData != NULL)
{
SaveImageToPNG(outputFilename, outImage);
CGImageRelease(outImage);
CGImageRef outImage = CreateImageFromBytes(outData, width / 2, height / 2);
free(outData);

if (outImage != NULL)
{
SaveImageToPNG(outputFilename, outImage);
CGImageRelease(outImage);
}
}
}
}
}

[pool drain];
return 0;
}

0 comments on commit 0b3a200

Please sign in to comment.
You can’t perform that action at this time.