Sepia Color Effect

523 views
Skip to first unread message

Asad Ullah

unread,
Jan 3, 2011, 11:32:50 PM1/3/11
to 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

unread,
Jan 4, 2011, 5:24:47 AM1/4/11
to 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

unread,
Jan 4, 2011, 4:15:19 AM1/4/11
to 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

unread,
Jan 4, 2011, 12:52:52 PM1/4/11
to iphonesdkd...@googlegroups.com
Hey Thanks for the quick response.
Reply all
Reply to author
Forward
0 new messages