Core Image

WebCore.Frameworkの中にCore Image Kernel

Posted in Core Image, Quartz/OS X Graphics on 11月 24th, 2007 by Norihisa Nagano – Be the first to comment

最近,WebKit周りを調べているのですが

WebCore.Frameworkの中にCore Image Kernelのファイル群を発見.
WKArithmeticFilter.cikernel
WKTableTransferFilter.cikernel
WKComponentMergeFilter.cikernel
WKDiffuseLightingFilter.cikernel
WKDiscreteTransferFilter.cikernel
WKDisplacementMapFilter.cikernel
WKDistantLightFilter.cikernel
WKGammaTransferFilter.cikernel
WKLinearTransferFilter.cikernel
WKNormalMapFilter.cikernel
WKPointLightFilter.cikernel
WKSpecularLightingFilter.cikernel
WKSpotLightFilter.cikernel

何をやってるのかは分かりませんが,名前から推測するに検索の時のちょいEffectか.
試しに一個QCで使ってみたが,よく分からず.(セピア?)
webcorecikernel.png
他にも入っているかも.

このエントリーをはてなブックマークに追加

2D Graphics Release Notes for Mac OS X v10.5

Posted in Core Image, Quartz/OS X Graphics on 11月 7th, 2007 by Norihisa Nagano – Be the first to comment

http://theocacao.com/

を見ていたら,10.5の2D Graphicsについての面白い記事が.

2D Graphics Release Notes for Mac OS X v10.5

に書いてあるのですが

The Core Graphics APIs (Quartz 2D) see an approximately 25% reduction in drawing performance for applications compiled to use garbage collection.

てなことで,GC使うとだいたい25%パフォーマンスダウンだそう.
あと,Core Imageを使うCore Videoの場合,frameメモリがGCできないから,GCは使わないほうがいいよ
ということらしい.
面白い.

Quartz・Core Image / Core Videoを使う僕としてはGCは常にoffなのが決定.
GCって楽なのかなぁ.

このエントリーをはてなブックマークに追加

Export CGImageRef to PNG

Posted in Core Image, Quartz/OS X Graphics on 10月 2nd, 2007 by Norihisa Nagano – Be the first to comment

CGImageRefはよく使います.
bitmapレベルから作れて,それからすぐにCIImageが作れてImage Unitで使えるからです.
で,この元になるCGImageをexportしておいて,QCにImage Importerで読み込んで,Kernelを書いてテストして
Image Unitにしたいときがよくあります.(いや無いですね)

というわけで,CGImageRefのexport.
CGImageDestinationRefを使います.
Programming With Quartzに載ってたコードはこちら.

void exportCGImage2PNGFileWithDestination(CGImageRef image,CFURLRef url){
    float resolution = 144.0;
    CFTypeRef keys[2];
    CFTypeRef values[2];
    CFDictionaryRef options = NULL;
    CGImageDestinationRef imageDestination
          = CGImageDestinationCreateWithURL(url,kUTTypePNG,1,NULL);
    if(imageDestination == NULL){
        return;
    }
    keys[0] = kCGImagePropertyDPIWidth;
    keys[1] = kCGImagePropertyDPIHeight;

    values[0] = CFNumberCreate(NULL,kCFNumberFloatType,&resolution);
    values[1] = values[0];

    options = CFDictionaryCreate(NULL,
                                 keys,
                                 values,
                                 2,
                                 &kCFTypeDictionaryKeyCallBacks,
                                 &kCFTypeDictionaryValueCallBacks);
    CFRelease(values[0]);

    CGImageDestinationAddImage(imageDestination,image,options);
    CFRelease(options);

    CGImageDestinationFinalize(imageDestination);
    CFRelease(imageDestination);
}

DPIの設定をやっていて丁寧.

CGImageDestinationFinalizeは,書き込み成功かどうかをboolで返すので追加して
DPIの設定なんかをとっぱらって簡単にしちゃうと

BOOL exportCGImage2PNGFileWithDestination(CGImageRef image,CFURLRef url){
    CGImageDestinationRef imageDestination
       = CGImageDestinationCreateWithURL(url,kUTTypePNG,1,NULL);
    if(imageDestination == NULL){
        return NO;
    }

    CGImageDestinationAddImage(imageDestination,image,NULL);

    BOOL flag = CGImageDestinationFinalize(imageDestination);
    CFRelease(imageDestination);
    return flag;
}

こうなります.

    CGImageRef *mCGImage; //が作成済みとして
	NSURL *url = [NSURL URLWithString:@"file://localhost/Users/hogehoge/hoge.png"];
	if(exportCGImage2PNGFileWithDestination(mCGImage,url)){
		NSLog(@"success");
	}

こういう感じで使います.
引数のCFURLは,NSURLでOK.(toll-free bridge最高)

NSImageの書き出しの時にBUGるとCGImageDestinationがどうのというのが出ますが
裏ではたぶん彼が働いているのだと思われます.

kUTTypePNGを他のに変えるとそのフォーマットで書き出せます.(たぶん)

このエントリーをはてなブックマークに追加

Quartz Composer Tips 2

Posted in Core Image, Quartz/OS X Graphics on 9月 27th, 2007 by Norihisa Nagano – Be the first to comment

Quartz ComposerでInput Sourceを左右反転しようとしたけど,そんなFilterありません.
ということで,Core Image Kernel Language初挑戦.5分でできた.
RGBAだとかARGBAだとか気にしなくていいからラクチンです.

samplerCoordで今のpixelのpositionを取得.
samplerSizeで画像サイズ取得
画像サイズ.x – 今のposition.xは,反対側のpixel.x.
というだけの話.

Shaderはやっぱり処理が速いなぁ.
たぶん,これを書かなくてもできる方法があるはず.
誰か教えて下さい.
意外と使うからImage Unitにしてもいいかも.

kernel vec4 mirror(sampler image)
{
	vec2 currentPosition = samplerCoord(image);
	vec2 size = samplerSize(image);

	vec2 point;
	point.x = size.x  - currentPosition.x;
	point.y = currentPosition.y;

	vec4 result = sample(image,point);

	return result;
}
このエントリーをはてなブックマークに追加

PDFからCIImageを作る

Posted in Cocoa/Carbon, Core Image, Quartz/OS X Graphics on 11月 7th, 2006 by Norihisa Nagano – Be the first to comment

PDF(のpage)からCIImageを作るとしたら一番最初に思いつくのはbitmapから作るという方法ですが,bitmapを取得するにはCGBitmapContextCreateを使うし,CGBitmapContextCreateで作ったCGContextRefからはCGBitmapContextCreateImageでCGImageRefが作れるので
CIImageの+ (CIImage *)imageWithCGImage:(CGImageRef)imageを使って作る.

NSString *path = @"/Users/nagano/hoge.pdf";
NSURL *url = [NSURL fileURLWithPath:path];
CGPDFDocumentRef doc = CGPDFDocumentCreateWithURL(url);

int page = 1;
CGRect pdfRect = CGPDFDocumentGetMediaBox(doc,page);
pdfRect = CGRectIntegral(pdfRect);

int width = CGRectGetWidth(pdfRect);
int height = CGRectGetHeight(pdfRect);
unsigned char *bitmap = malloc(width * height * sizeof(unsigned char) * 4);
CGContextRef bitmapContext;
bitmapContext = CGBitmapContextCreate(bitmap,
					width,
					height,
					8,
					width * 4,
					CGColorSpaceCreateDeviceRGB(),
					kCGImageAlphaPremultipliedFirst);

CGContextDrawPDFDocument(bitmapContext, pdfRect, doc, page);

CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext);
CIImage* aCIImage =  [CIImage imageWithCGImage:cgImage];

free(bitmap);
CGContextRelease(bitmapContext);
CGImageRelease(cgImage);
CGPDFDocumentRelease(doc);
このエントリーをはてなブックマークに追加