From 533780aadf1ba1b553ef69f191171f6a528ae894 Mon Sep 17 00:00:00 2001 From: Ray Date: Tue, 3 Apr 2018 12:42:22 +0200 Subject: [PATCH] Review ImageDraw() alpha blending Not sure if math is ok... just left a commented piece of code that uses pre-multiplied alpha. --- src/textures.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/textures.c b/src/textures.c index 54055ce03..8e68df7e1 100644 --- a/src/textures.c +++ b/src/textures.c @@ -1337,11 +1337,30 @@ void ImageDraw(Image *dst, Image src, Rectangle srcRec, Rectangle dstRec) // Alpha blending implementation dstCol = dstPixels[j*dst->width + i]; srcCol = srcPixels[(j - dstRec.y)*dstRec.width + (i - dstRec.x)]; + + /* + // Pre-multiply alpha + Vector3 dstColf = { (float)dstCol.r/255.0f, (float)dstCol.g/255.0f, (float)dstCol.b/255.0f }; + dstColf = Vector3Multiply(dstColf, (float)dstCol.a/255.0f); + Vector3 srcColf = { (float)srcCol.r/255.0f, (float)srcCol.g/255.0f, (float)srcCol.b/255.0f }; + srcColf = Vector3Multiply(srcColf, (float)srcCol.a/255.0f); + + dstColf = Vector3Add(dstColf, srcColf); + + if (dstColf.x > 1.0f) dstColf.x = 1.0f; + if (dstColf.y > 1.0f) dstColf.y = 1.0f; + if (dstColf.z > 1.0f) dstColf.z = 1.0f; + + dstCol.r = (unsigned char)(dstColf.x*255.0f); + dstCol.g = (unsigned char)(dstColf.y*255.0f); + dstCol.b = (unsigned char)(dstColf.z*255.0f); + dstCol.a = srcCol.a; + */ dstCol.r = ((srcCol.a*(srcCol.r - dstCol.r)) >> 8) + dstCol.r; dstCol.g = ((srcCol.a*(srcCol.g - dstCol.g)) >> 8) + dstCol.g; dstCol.b = ((srcCol.a*(srcCol.b - dstCol.b)) >> 8) + dstCol.b; - dstCol.a = ((srcCol.a*(srcCol.a - dstCol.a)) >> 8) + dstCol.a; + dstCol.a = srcCol.a; dstPixels[j*dst->width + i] = dstCol;