We can blend two images on mac easily by following these steps.
1. First, make sure that dimension of both images should be equal. if they are not then you should scale source image in order to equalize them.
2. Get the opacity of source image.
3. Calculate the opacity of target image simply by reducing this value from 100.
4. Get the context reference using CGBitmapContextCreate() function. Then get the data of both images using CGBitmapContextGetData() function.
Here is the code how to blend two images on mac.
void *srcData = CGBitmapContextGetData (context); void *dstData = CGBitmapContextGetData (context); uint8* dstPtr = (uint8*) dstData; uint8* srcPtr = (uint8*) srcData; int32 bytesPerRow = CGBitmapContextGetBytesPerRow(context); int32 planeBits = CGBitmapContextGetBitsPerComponent(context); float width = CGImageGetWidth(srcImage); float height = CGImageGetHeight(srcImage); short totalRows = height; short totalColumns = width; short totalPlanes = 4; //No of planes per pixel i.e. R, G, B & alpha. short planeBytes = planeBits/8; short loopRows, loopColumns, plane; for(loopRows = 0; loopRows < totalRows; loopRows++) { srcPtr = (uint8 *) srcData + (loopRows * bytesPerRow); dstPtr = (uint8 *) dstData + (loopRows * bytesPerRow); for(loopColumns = 0; loopColumns < totalColumns; loopColumns++) { for (plane = 0; plane < totalPlanes; plane++) { switch (plane) { case 0: case 1: case 2: srcByte = srcPtr [plane * planeBytes]; dstByte = dstPtr[plane * planeBytes]; newSrcByte = (srcImageOpacity*srcByte)/100; newDstByte = (dstImageOpacity*dstByte)/100; dstPtr [plane * planeBytes] = newDstByte + newSrcByte; break; default: break; } // copy the second byte also if there r two bytes per plane if(planeBytes == 2) dstPtr [plane * planeBytes + 1] = srcPtr [plane * planeBytes + 1]; } dstPtr += totalPlanes * planeBytes; srcPtr += totalPlanes * planeBytes; } }