Skip to content

Instantly share code, notes, and snippets.

@landaire
Created March 29, 2016 23:11
Show Gist options
  • Save landaire/63e9a94f197c345335d165a16cea6a64 to your computer and use it in GitHub Desktop.
Save landaire/63e9a94f197c345335d165a16cea6a64 to your computer and use it in GitHub Desktop.
//
// main.m
// imageio-fuzz
//
// Created by Lander Brandt on 12/20/15.
// Copyright © 2015 Lander Brandt. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <ImageIO/ImageIO.h>
CGImageSourceRef MyCreateCGImageFromFile (NSString* path)
{
// Get the URL for the pathname passed to the function.
NSURL *url = [NSURL fileURLWithPath:path];
CGImageRef myImage = NULL;
CGImageSourceRef myImageSource;
CFDictionaryRef myOptions = NULL;
CFStringRef myKeys[2];
CFTypeRef myValues[2];
// Set up options if you want them. The options here are for
// caching the image in a decoded form and for using floating-point
// values if the image format supports them.
myKeys[0] = kCGImageSourceShouldCache;
myValues[0] = (CFTypeRef)kCFBooleanTrue;
myKeys[1] = kCGImageSourceShouldAllowFloat;
myValues[1] = (CFTypeRef)kCFBooleanTrue;
// Create the dictionary
myOptions = CFDictionaryCreate(NULL, (const void **) myKeys,
(const void **) myValues, 2,
&kCFTypeDictionaryKeyCallBacks,
& kCFTypeDictionaryValueCallBacks);
// Create an image source from the URL.
myImageSource = CGImageSourceCreateWithURL((CFURLRef)url, myOptions);
CFRelease(myOptions);
// Make sure the image source exists before continuing
if (myImageSource == NULL){
fprintf(stderr, "Image source is NULL.");
return NULL;
}
// Create an image from the first item in the image source.
myImage = CGImageSourceCreateImageAtIndex(myImageSource,
0,
NULL);
// Make sure the image exists before continuing
if (myImage == NULL){
fprintf(stderr, "Image not created from image source.");
return NULL;
}
return myImageSource;
}
CGImageRef MyCreateThumbnailImageFromData (CGImageSourceRef myImageSource, int imageSize)
{
CGImageRef myThumbnailImage;
CFDictionaryRef myOptions = NULL;
CFStringRef myKeys[3];
CFTypeRef myValues[3];
CFNumberRef thumbnailSize;
// Package the integer as a CFNumber object. Using CFTypes allows you
// to more easily create the options dictionary later.
thumbnailSize = CFNumberCreate(NULL, kCFNumberIntType, &imageSize);
// Set up the thumbnail options.
myKeys[0] = kCGImageSourceCreateThumbnailWithTransform;
myValues[0] = (CFTypeRef)kCFBooleanTrue;
myKeys[1] = kCGImageSourceCreateThumbnailFromImageIfAbsent;
myValues[1] = (CFTypeRef)kCFBooleanTrue;
myKeys[2] = kCGImageSourceThumbnailMaxPixelSize;
myValues[2] = (CFTypeRef)thumbnailSize;
myOptions = CFDictionaryCreate(NULL, (const void **) myKeys,
(const void **) myValues, 2,
&kCFTypeDictionaryKeyCallBacks,
& kCFTypeDictionaryValueCallBacks);
// Create the thumbnail image using the specified options.
myThumbnailImage = CGImageSourceCreateThumbnailAtIndex(myImageSource,
0,
myOptions);
// Release the options dictionary and the image source
// when you no longer need them.
CFRelease(thumbnailSize);
CFRelease(myOptions);
// Make sure the thumbnail image exists before continuing.
if (myThumbnailImage == NULL){
fprintf(stderr, "Thumbnail image not created from image source.");
return NULL;
}
return myThumbnailImage;
}
void ReadExif(CGImageSourceRef source)
{
NSDictionary* props =
(NSDictionary*) CGImageSourceCopyPropertiesAtIndex(source, 0, NULL); // 2
NSDictionary* thumbOpts = [NSDictionary dictionaryWithObjectsAndKeys:
(id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform,
(id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageIfAbsent,
[NSNumber numberWithInt:128], (id)kCGImageSourceThumbnailMaxPixelSize,
nil]; // 4
NSString* uti = (NSString*)CGImageSourceGetType(source); // 9
CFDictionaryRef fileProps = CGImageSourceCopyProperties(source, nil); // 11
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *imageFilePath = [NSString stringWithCString:argv[1] encoding:NSUTF8StringEncoding];
// Correct the CRC if it's a PNG. This application checks if it is a PNG
NSTask *task = [[NSTask alloc] init];
task.launchPath = @"/Users/lander/go/bin/png-crc-fix";
task.arguments = @[imageFilePath];
[task launch];
CGImageSourceRef image = MyCreateCGImageFromFile(imageFilePath);
if (image != NULL) {
MyCreateThumbnailImageFromData(image, 200);
ReadExif(image);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment