CoreGraphics on the iPhone. What is the correct way to load a PNG file with pre-multiplied alpha?

For my iPhone 3D applications, I am currently using CoreGraphics to download png files that have pre-multiplied alpha. Here is the basic information:

// load a 4-channel rgba png file with pre-multiplied alpha. 
CGContextRef context =  
CGBitmapContextCreate(data, width, height, 8, num_channels * width, colorSpace, 
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 

// Draw the image into the context
CGRect rect =  CGRectMake(0, 0, width, height); 
CGContextDrawImage(context, rect, image); 
CGContextRelease(context); 

Then I continue to use the data as a texture in OpenGL.

My question is: by specifying pre-multiplied alpha - kCGImageAlphaPremultipliedLast - I - inadvertently - tell CoreGraphics to multiply rgb channels by alpha, or - what I assumed - I just indicate that the input image format has a preliminary format, multiplied alpha?

Thanks Doug

+3
source share
2 answers

pre-multiplied alpha - kCGImageAlphaPremultipliedLast - - CoreGraphics , rgb - - , ?

. (), . - - , , , , .

, - , , ( , , 't, ).

OpenGL, , , , , : Mac, , .

.

+4

, , , -:

    /////// .... 
    /////// load the image into imageRef up here
    /////// e.g. with CGImageSourceCreateWithURL(); and CGImageSourceCreateImageAtIndex(); on OS X
    /////// or, by accessing CGImage property of UIImage 
    ///////  
    /////// then extract bitsPerComponent, bytesPerRow and color space 
    /////// .... 


    // draw CG image to a byte array
    CGContextRef bitmapContext = CGBitmapContextCreate(img->data,
                                                       img->w, img->h,
                                                       bitsPerComponent, 
                                                       bytesPerRow,
                                                       space,
                                                       kCGImageAlphaPremultipliedLast);

    CGContextDrawImage(bitmapContext,CGRectMake(0.0, 0.0, (float)img->w, (float)img->h),imageRef);
    CGContextRelease(bitmapContext);

    // let undo premultiplication
    for(int i=0; i < bytesPerRow * img->h; i+=4)
    {
        for(int j=0; j<3; j++)
        {
            img->data[i+j] = img->data[i+j] / (img->data[i+3]/255.);
        }
    }
+2

Source: https://habr.com/ru/post/1738964/


All Articles