Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-11-2010, 11:23 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 2
cutmaster is on a distinguished road
Angry Help with kCGImageAlphaPremultipliedLast raw data image access & change...

Hi,

I'm trying to create a method that must change (for this example) every black pixel with an alpha value > 0 to another color (lets say a blue one) but keeping the same alpha value.

Here's my code :

Code:
- (CGImageRef)ChangeColorOfImage:(CGImageRef)inImage {
    CGContextRef ctx;
    
    CFDataRef m_DataRef; 
    m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage)); 
    UInt8 *pixels = (UInt8 * ) CFDataGetBytePtr(m_DataRef); 
    int length = CFDataGetLength(m_DataRef); 

    int r,g,b,a;
    for (int index = 0; index < length; index += 4) 
    { 
        r = pixels[index+0];
        g = pixels[index+1];
        b = pixels[index+2];
        a = pixels[index+3];
        
        if (r==0 && g==0 && b==0 && a>0) {
            pixels[index+0] = 0;
            pixels[index+1] = 128;
            pixels[index+2] = 255;
        }
    } 
    
    
    
    ctx = CGBitmapContextCreate(pixels, 
                                CGImageGetWidth(inImage), 
                                CGImageGetHeight(inImage), 
                                8, 
                                CGImageGetBytesPerRow(inImage), 
                                CGImageGetColorSpace(inImage), 
                                kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 


    CGImageRef imageRef = CGBitmapContextCreateImage (ctx); 
    CGContextRelease(ctx);
    
    return imageRef;
}
My problem is that, the resulted image is totally randomized in terms of color for each pixels !!



I presume that the keyword : kCGImageAlphaPremultipliedLast presume that the destination values must be computed in another way but I lose more than 3 hours searching the web without success

Can someone help me ?

Thanks a lot...

Laurent.
cutmaster is offline   Reply With Quote
Old 07-11-2010, 12:05 PM   #2 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 2
cutmaster is on a distinguished road
Default Found it

Okay, if it can help someone, here is the reason and its solution.
Importing PNG from Photoshop make them automatically using AlphaPreMultiplied mode...
To convert an ARGB value to the good expected premultiplied values, here's the part of code to change :

after the line
int r,g,b,a;
add :
float multiplier;

Then change the content of the portion code running inside the "if" with this :

Code:
        if (r==0 && g==0 && b==0 && a>0) {
            multiplier = (float)a/255.0f;
            pixels[index+0] = MAX(0,MIN(255,(int)(toRed*multiplier)));
            pixels[index+1] = MAX(0,MIN(255,(int)(toGreen*multiplier)));
            pixels[index+2] = MAX(0,MIN(255,(int)(toBlue*multiplier)));
        }
So if you understand, the multiplier value is the alpha value divided by 255 (in float), then you must multiply your color with this value, and ensure it'll not goes beyond the limits (0-255).

Bests.

Laurent.
cutmaster is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 333
16 members and 317 guests
akphyo, alexP, appservice, cgokey, EXOPTENDAELAX, flamingliquid, guusleijsten, mariano_donati, ohmniac, Paul Slocum, PavelSea, SLIC, Sloshmonster, Sonuye857, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,653
Threads: 94,115
Posts: 402,888
Top Poster: BrianSlick (7,990)
Welcome to our newest member, ohmniac
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:34 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0