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

  • Home
  • SEARCH
  • 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 8200371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:25:33+00:00 2026-06-07T06:25:33+00:00

I am trying to to pixel-by-pixel image filters using Core Graphics (breaking a CGImage

  • 0

I am trying to to pixel-by-pixel image filters using Core Graphics (breaking a CGImage into unsigned integers using CFData)

When I try to create an imaged with the processed data, however, the resulting image comes out with significantly different colors.

I commented out the entire loop where I actually alter the pixels’ rgb values and nothing changes, either.

When I initialize the UIImage I am using in the filter; I do a resize using drawInRect with UIGraphicsBeginContext(); on an image taken from the camera.

When I remove the resize step and set my image directly from the camera; the filters seem to work just fine. Here’s the code where I initialize the image I am using (from inside didFinishPickingImage)

self.editingImage is a UIImageView and self.editingUIImage is a UIImage

-(void)imagePickerController:(UIImagePickerController *)picker
  didFinishPickingImage : (UIImage *)image
             editingInfo:(NSDictionary *)editingInfo
{

self.didAskForImage = YES;

UIGraphicsBeginImageContext(self.editingImage.frame.size);

float prop = image.size.width / image.size.height;

float left, top, width, height;

if(prop < 1){

    height = self.editingImage.frame.size.height;

    width = (height / image.size.height) * image.size.width;

    left = (self.editingImage.frame.size.width - width)/2;

    top = 0;

}else{

    width = self.editingImage.frame.size.width;

    height = (width / image.size.width) * image.size.height;

    top = (self.editingImage.frame.size.height - height)/2;

    left = 0;

}

[image drawInRect:CGRectMake(left, top, width, height)];

self.editingUIImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();



self.editingImage.image = self.editingUIImage;

[self.contrastSlider addTarget:self action:@selector(doImageFilter:) forControlEvents:UIControlEventValueChanged];

[self.brightnessSlider addTarget:self action:@selector(doImageFilter:) forControlEvents:UIControlEventValueChanged];

[picker dismissModalViewControllerAnimated:YES];
picker = nil;
}

The resizes the image just the way I need it as far as position;

Here’s the image filtering function, I’ve taken the actual loop contents out because they’re irrelevant.

- (void) doImageFilter:(id)sender{


CGImageRef src = self.editingUIImage.CGImage;

CFDataRef dta;
dta = CGDataProviderCopyData(CGImageGetDataProvider(src));

UInt8 *pixData = (UInt8 *) CFDataGetBytePtr(dta);

int dtaLen = CFDataGetLength(dta);

for (int i = 0; i < dtaLen; i += 3) {
    //the loop
}

CGContextRef ctx;

ctx = CGBitmapContextCreate(pixData, CGImageGetWidth(src), CGImageGetHeight(src), 8, CGImageGetBytesPerRow(src), CGImageGetColorSpace(src), kCGImageAlphaPremultipliedLast);

CGImageRef newCG = CGBitmapContextCreateImage(ctx);
UIImage *new = [UIImage imageWithCGImage:newCG];

CGContextRelease(ctx);
CFRelease(dta);
CGImageRelease(newCG);

self.editingImage.image = new;

}

The image looks like this at first

http://picsilk.com/media/before.png

and then after doing doImageFilter…

http://picsilk.com/media/before.png

As mentioned before, this only happens when I use the resize method shown above.

Really stumped on this one, been researching it all day… any help very appreciated!

Cheers

Update: I’ve examined all the image objects’ color spaces and they’re all kCGColorSpaceDeviceRGB. Pretty stumped on this one guys, I’m pretty some something is going wrong when I break the image into unsigned integers, but I’m not sure what.. Anyone?

  • 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-06-07T06:25:35+00:00Added an answer on June 7, 2026 at 6:25 am

    Your problem is on the last line:

    ctx = CGBitmapContextCreate(pixData, 
                                CGImageGetWidth(src), 
                                CGImageGetHeight(src), 
                                8, 
                                CGImageGetBytesPerRow(src), 
                                CGImageGetColorSpace(src), 
                                kCGImageAlphaPremultipliedLast);
    

    You’re making an assumption about the alpha and the component ordering of the data of the source image, which is apparently not correct. You should get that from the source image via CGImageGetBitmapInfo(src).

    To avoid issues like this one, if you’re starting with an arbitrary CGImage and you want to manipulate the bytes of the bitmap directly, it is best to make a CGBitmapContext in a format that you specify yourself (not directly taken from the source image). Then, draw your source image into the bitmap context; CG will convert the image’s data into your bitmap context’s format, if necessary. Then get the data from the bitmap context and manipulate it.

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

Sidebar

Related Questions

I am trying to create a binary image in C#/WPF using the WriteableBitmap class,
I'm trying to create a mapping for tile/pixel positions in one tiled image dataset
I am trying to sort pixel values of an image (example 80x20) from lowest
I am trying to get the current pixel in an image by a OnMouseMove
I'm trying to save the value of each pixel in a gray scale image
I am trying to draw a simple 256x256 pixel RGBA square using python's png
I am trying to display an image using OpenGL ES through android NDK.. What
I am trying to display a bitmap image pixel by pixel (which explicitly means
I am trying to make a terrain using a height map image in OpenGL.
I am trying to get the pixel colour from an image displayed by the

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.