Skip to content

Instantly share code, notes, and snippets.

@katotetsuro
Created October 19, 2012 08:34
Show Gist options
  • Select an option

  • Save katotetsuro/3916970 to your computer and use it in GitHub Desktop.

Select an option

Save katotetsuro/3916970 to your computer and use it in GitHub Desktop.
CIImageとofImageの変換の練習
// CIImageとofImageのコンバートの練習 ===================
void saveCIImage(CIImage *ciImage) {
NSBitmapImageRep* rep = [[NSBitmapImageRep alloc]
initWithCIImage:ciImage];
NSData* PNGData = [rep representationUsingType:NSPNGFileType
properties:nil];
NSString *filepath = [NSString stringWithCString:ofToDataPath(ofGetTimestampString() + ".png").c_str() encoding:NSUTF8StringEncoding];
[PNGData writeToFile:filepath atomically:NO];
}
void convertTest(ofImage &src, ofImage &dst) {
// ofImage -> CIImage
unsigned char *pixels = src.getPixels();
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:&pixels pixelsWide:src.width pixelsHigh:src.height bitsPerSample:8 samplesPerPixel:3 hasAlpha:NO isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace bitmapFormat:NSAlphaNonpremultipliedBitmapFormat bytesPerRow:3*src.width bitsPerPixel:24];
CIImage *ciImage = [[CIImage alloc] initWithBitmapImageRep:rep];
saveCIImage(ciImage);
// CIImage -> ofImage
CIContext *ciContext = [[CIContext alloc] init];
CGImageRef cgimg = [ciContext createCGImage:ciImage fromRect:[ciImage extent]];
NSUInteger width = CGImageGetWidth(cgimg);
NSUInteger height = CGImageGetHeight(cgimg);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
dst.setImageType(OF_IMAGE_COLOR_ALPHA); // なんか4チャンネルになっちゃうみたい
unsigned char *rawData = dst.getPixels();//(unsigned char*)calloc(height*width*4, sizeof(unsigned char));
NSUInteger bpp = 4;
NSUInteger bpr = bpp * width;
NSUInteger bpc = 8;
CGContextRef cgContext = CGBitmapContextCreate(rawData, width, height, bpc, bpr, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(cgContext, CGRectMake(0, 0, width, height), cgimg);
CGContextRelease(cgContext);
CGImageRelease(cgimg);
dst.update();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment