Sepia Color Effect

已查看 523 次
跳至第一个未读帖子

Asad Ullah

未读,
2011年1月3日 23:32:502011/1/3
收件人 iphonesdkd...@googlegroups.com
Hey All

Need to get Sepia Effect in iPhone Application.
I have converted the RGB image to Grayscale image
but  can't get the Sepia conversion. There is some
transformation method to convert from Grayscale
to Sepia but i don't know that. I have searched in
google but didn't get any effective transformation
method for the sepia conversion. If anyone have
please share with me.

Thanks in advance.

Eko Mirhard

未读,
2011年1月4日 05:24:472011/1/4
收件人 iphonesdkd...@googlegroups.com
Based on this : http://blogs.techrepublic.com.com/howdoi/?p=120

I wrote a method long time ago, here you go :

+ (UIImage*)convertToSephia:(UIImage*)source{
CGSize size = [source size];
int width = size.width;
int height = size.height;

// the pixels will be painted to this array
uint32_t *pixels = (uint32_t *) malloc(width * height * sizeof(uint32_t));

// clear the pixels so any transparency is preserved
memset(pixels, 0, width * height * sizeof(uint32_t));

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

// create a context with RGBA pixels
CGContextRef context = CGBitmapContextCreate(pixels, width, height,
8, width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little |
kCGImageAlphaPremultipliedLast);

// paint the bitmap to our context which will fill in the pixels array
CGContextDrawImage(context, CGRectMake(0, 0, width, height), source.CGImage);

for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
uint8_t *rgbaPixel = (uint8_t *) &pixels[y * width + x];

// Based on : http://blogs.techrepublic.com.com/howdoi/?p=120

uint32_t oRed = rgbaPixel[RED] * 0.393 + rgbaPixel[GREEN] * 0.769 +
rgbaPixel[BLUE] * 0.189;
if(oRed > 255) oRed = 255;
uint32_t oGreen = rgbaPixel[RED] * 0.349 + rgbaPixel[GREEN] * 0.686
+ rgbaPixel[BLUE] * 0.168;
if(oGreen > 255) oGreen = 255;
uint32_t oBlue = rgbaPixel[RED] * 0.272 + rgbaPixel[GREEN] * 0.534
+ rgbaPixel[BLUE] * 0.131;
if(oBlue > 255) oBlue = 255;

// set the pixels to gray
rgbaPixel[RED] = oRed;
rgbaPixel[GREEN] = oGreen;
rgbaPixel[BLUE] = oBlue;
}
}

// create a new CGImageRef from our context with the modified pixels
CGImageRef image = CGBitmapContextCreateImage(context);

// we're done with the context, color space, and pixels
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
free(pixels);

// make a new UIImage to return
UIImage *resultUIImage = [UIImage imageWithCGImage:image];

// we're done with image now too
CGImageRelease(image);

return resultUIImage;
}

> --
> You received this message because you are subscribed to the Google Groups
> "iPhone SDK Development" group.
> To post to this group, send email to iphonesdkd...@googlegroups.com.
> To unsubscribe from this group, send email to
> iphonesdkdevelop...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/iphonesdkdevelopment?hl=en.
>
>

Till Toenshoff

未读,
2011年1月4日 04:15:192011/1/4
收件人 iphonesdkd...@googlegroups.com

Options…

 

A: You might want to check the ImageMagick sources, specifically the function SepiaToneImage in fx.c. It is a really straight forward tone transformation. You will possibly have to look up a few more functions to get the entire concept (PixelIntensityToQuantum,…) but I would guess within less than an hour you should have a working copy within your iPhone app.

 

B: Check the aforge C# sources, understand the concept and apply it within C or ObjectiveC  http://www.google.com/codesearch/p?hl=en#0S8DoZCfio4/Sources/Imaging/Filters/Color%20Filters/Sepia.cs

 

C: Use a trivial, but possibly good enough solution; Convert the source-image to a grey-tone image. Fetch the RGB-values (0-255) from that grey-tone image. Add 40 to the red-component and 20 to the green-component of every source-pixel. Clamp them to 255 and write them to the destination image.

 

--

Asad Ullah

未读,
2011年1月4日 12:52:522011/1/4
收件人 iphonesdkd...@googlegroups.com
Hey Thanks for the quick response.
回复全部
回复作者
转发
0 个新帖子