Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6173209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:31:54+00:00 2026-05-23T23:31:54+00:00

I am taking an image, loading it via the screen context, and changing it

  • 0

I am taking an image, loading it via the screen context, and changing it pixel by pixel. I have a number of different filters that I am applying to the images, but the last thing I need to do is shift the color balance (similar to Photoshop) to make the red more cyan.

The code below shows how I am taking the image, getting the data, and going through the r/g/b values pixel by pixel:

CGImageRef sourceImage = theImage.image.CGImage;

CFDataRef theData;
theData = CGDataProviderCopyData(CGImageGetDataProvider(sourceImage));

UInt8 *pixelData = (UInt8 *) CFDataGetBytePtr(theData);

int dataLength = CFDataGetLength(theData);


int red = 0;
int green = 1;
int blue = 2;


for (int index = 0; index < dataLength; index += 4) {

    int r = pixelData[index + red];
    int g = pixelData[index + green];
    int b = pixelData[index + blue];

    // the color balancing would go here...

    if (r < 0) r = 0;
    if (g < 0) g = 0;
    if (b < 0) b = 0;

    if (r > 255) r = 255;
    if (g > 255) g = 255;
    if (b > 255) b = 255;

    pixelData[index + red] = r;
    pixelData[index + green] = g;
    pixelData[index + blue] = b;

}



CGContextRef context;
context = CGBitmapContextCreate(pixelData,
                                CGImageGetWidth(sourceImage),
                                CGImageGetHeight(sourceImage),
                                8,
                                CGImageGetBytesPerRow(sourceImage),
                                CGImageGetColorSpace(sourceImage),
                                kCGImageAlphaPremultipliedLast);

CGImageRef newCGImage = CGBitmapContextCreateImage(context);
UIImage *newImage = [UIImage imageWithCGImage:newCGImage];

CGContextRelease(context);
CFRelease(theData);
CGImageRelease(newCGImage);

theImage.image = newImage;

I am doing a number of other things to the pixel data (setting the levels, desaturating) but I need to shift the red value toward cyan.

In Photoshop, I am able to do it via:

Image: Adjustment: Color Balance
and setting it to -30 0 0

I have not been able to find an algorithm or an example explaining how this color shift is performed. I have tried to subtract 30 from every red value, or setting a hard maximum at 255 – 30 (225), but those seem to clip the colors and not shift the values… Right now, I’m just hacking around on it, but pointers to a reference would help out a lot.

(NB: I am not able to use an OpenGL solution because I have to take the same algorithm and convert it to PHP/gd for a web server version of this application)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:31:56+00:00Added an answer on May 23, 2026 at 11:31 pm

    What you need to do is subtract the red and then correct the “lightness” to match the old color, for whatever measure of lightness you choose. Something like this should work:

    // First, calculate the current lightness.
    float oldL = r * 0.30 + g * 0.59 + b * 0.11;
    
    // Adjust the color components. This changes lightness.
    r = r - 30;
    if (r < 0) r = 0;
    
    // Now correct the color back to the old lightness.
    float newL = r * 0.30 + g * 0.59 + b * 0.11;
    if (newL > 0) {
        r = r * oldL / newL;
        g = g * oldL / newL;
        b = b * oldL / newL;
    }
    

    Note this behaves somewhat oddly when given pure red (it will leave it unchanged). OTOH, GIMP (version 2.6.11) does the same thing in its color balance tool so I’m not too worried about it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app that show a list of images. The image source is
I want to show the image that mobile phone's camera its taking on a
I have a custom handler that is returning an image to the browser. The
I have a (many) .dgp files, which is an image file that I can't
I have a problem on taking the snapshot of html document running on different
I am taking image through camera, and after that cropping the image, when I
I have created a simple list of images that are to be reloaded every
In my application i have Implement to take image from Gallery. In that gallery
Taking over some code from my predecessor and I found a query that uses
I'm interested in loading indexed-color PNG images in my iPhone application. Once they're loaded,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.