This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* * complie | |
gcc --std=c99 -Wall -g -o pdf2png pdf2png.m -framework Cocoa | |
*/ | |
#import <Cocoa/Cocoa.h> | |
static NSString* dumpPNG(CGPDFPageRef page, NSURL* outDir) { | |
size_t num = CGPDFPageGetPageNumber(page); | |
assert(num > 0); | |
CGRect r = CGPDFPageGetBoxRect(page, kCGPDFTrimBox); | |
// 解像度を72*4, 288dpiに設定 | |
size_t dpi = 72 * 4; | |
size_t scale = dpi / 72; | |
size_t w = r.size.width * scale; | |
size_t h = r.size.height * scale; | |
CGColorSpaceRef colorspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); | |
CGContextRef ctx = CGBitmapContextCreate( | |
NULL, // 10.6+ | |
w, h, | |
8, w * 4, | |
colorspace, | |
kCGImageAlphaNoneSkipFirst | |
); | |
// CGColorSpaceRelease(colorspace); | |
CGAffineTransform trans = CGAffineTransformMakeScale(scale, scale); | |
CGContextConcatCTM(ctx, trans); | |
//カラー作成 | |
CGFloat col[4]; | |
col[0] = 1.0; | |
col[1] = 1.0; | |
col[2] = 1.0; | |
col[3] = 1.0; | |
CGColorRef color = CGColorCreate(colorspace, col); | |
//色空間の解放 | |
CGColorSpaceRelease(colorspace); | |
CGContextSetFillColorWithColor(ctx, color); | |
CGContextAddRect(ctx, CGRectMake(0.0, 0.0, w, h)); | |
CGContextFillPath(ctx); | |
CGContextDrawPDFPage(ctx, page); | |
CGImageRef img = CGBitmapContextCreateImage(ctx); | |
assert(NULL != img); | |
NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] initWithCGImage:img]; | |
NSData* data = [rep representationUsingType:NSBitmapImageFileTypePNG properties:[NSDictionary dictionary]]; | |
[rep release]; | |
NSString* outFile = [[outDir path] stringByAppendingPathComponent:[NSString stringWithFormat:@"%04zu.png", num]]; | |
BOOL success = [data writeToFile:outFile atomically:NO]; | |
assert(YES == success); | |
CGImageRelease(img); | |
CGContextRelease(ctx); | |
return outFile; | |
} | |
int main(int argc, char** argv) { | |
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; | |
if (argc < 3) { | |
NSLog(@"Usage: %s input.pdf output_dir\n", argv[0]); | |
goto exit; | |
} | |
NSURL* inputFile = [NSURL fileURLWithPath:[NSString stringWithUTF8String:argv[1]]]; | |
NSURL* outputDir = [NSURL fileURLWithPath:[NSString stringWithUTF8String:argv[2]] isDirectory:YES]; | |
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)inputFile); | |
assert(NULL != pdf); | |
size_t pages = CGPDFDocumentGetNumberOfPages(pdf); | |
size_t p; | |
for (p = 1; p <= pages; ++p) { | |
NSAutoreleasePool* poolin = [[NSAutoreleasePool alloc] init]; | |
CGPDFPageRef page = CGPDFDocumentGetPage(pdf, p); | |
assert(NULL != page); | |
NSString* wrotePath = dumpPNG(page, outputDir); | |
NSLog(@"wrote: %@", wrotePath); | |
[poolin drain]; | |
} | |
CGPDFDocumentRelease(pdf); | |
exit: | |
[pool drain]; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment