Created
October 5, 2014 20:52
-
-
Save philippec/fa4e3083804ef4bc2fc0 to your computer and use it in GitHub Desktop.
strip-alpha.m -- takes an input file (image) and rewrites it so it does not have an alpha channel
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
// | |
// strip-alpha.m -- takes an input file (image) and rewrites it so it does not have an alpha channel | |
// Created by Philippe Casgrain on 2014-10-05 | |
// (C) 2014 by Philippe Casgraina | |
// | |
// Inspired by https://github.com/bpolat/Alpha-Channel-Remover | |
// | |
// License: Public Domain. See http://unlicense.org for more info | |
// | |
// To build: | |
// clang -g -Weverything -Wno-unused-parameter -Wno-gnu -framework Foundation -framework Cocoa -o strip-alpha strip-alpha.m | |
// | |
// To use: | |
// ./strip-alpha <filename> | |
// | |
// To use on a series of images (for example) | |
// find . -name "*.png" -exec ./strip-alpha {} \; | |
// | |
#import <Foundation/Foundation.h> | |
#import <Cocoa/Cocoa.h> | |
void stripAlpha(NSString *name); | |
void stripAlpha(NSString *name) | |
{ | |
NSURL *url = [NSURL fileURLWithPath:name]; | |
// Load the image into a CGImage | |
NSImage *srcImage = [[[NSImage alloc] initWithContentsOfURL:url] autorelease]; | |
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)[srcImage TIFFRepresentation], NULL); | |
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, 0, NULL); | |
// Draw the image in a CGContext with no alpha | |
CGRect rect = CGRectMake(0.0, 0.0, CGImageGetWidth(imageRef), CGImageGetHeight(imageRef)); | |
CGContextRef bitmapContext = CGBitmapContextCreate(NULL, | |
(size_t)rect.size.width, | |
(size_t)rect.size.height, | |
CGImageGetBitsPerComponent(imageRef), | |
CGImageGetBytesPerRow(imageRef), | |
CGImageGetColorSpace(imageRef), | |
(CGBitmapInfo)(kCGImageAlphaNoneSkipLast | kCGBitmapByteOrder32Little)); | |
CGContextDrawImage(bitmapContext, rect, imageRef); | |
// Re-create an image from the CGContext | |
CGImageRef decompressedImageRef = CGBitmapContextCreateImage(bitmapContext); | |
NSImage *finalImage = [[[NSImage alloc] initWithCGImage:decompressedImageRef size:NSZeroSize] autorelease]; | |
NSData *imageData = [finalImage TIFFRepresentation]; | |
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData]; | |
// Convert the image to a PNG, which is now alpha-free | |
NSDictionary *imageProps = @{ NSImageCompressionFactor : @0.9 }; | |
imageData = [imageRep representationUsingType:NSPNGFileType properties:imageProps]; | |
// Write back to the same file | |
[imageData writeToFile:name atomically:YES]; | |
CGImageRelease(decompressedImageRef); | |
CGContextRelease(bitmapContext); | |
} | |
int main (int argc, char *argv[]) | |
{ | |
if (argc != 2) | |
{ | |
fprintf(stderr, "Usage: strip-alpha filename\n"); | |
return EXIT_FAILURE; | |
} | |
@autoreleasepool | |
{ | |
NSString *filename = [NSString stringWithCString:argv[1] encoding:NSUTF8StringEncoding]; | |
stripAlpha(filename); | |
} | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment