Add alpha premultiplication.

This commit is contained in:
H. Utku Maden 2023-07-02 11:39:02 +03:00
parent 1676184118
commit 3906222506

@ -4,6 +4,56 @@ namespace Quik.Media.Color
{ {
public static class FormatConvert public static class FormatConvert
{ {
public static void Premultiply(QImageLock image)
{
switch (image.Format)
{
case QImageFormat.RaF:
case QImageFormat.RaU8:
case QImageFormat.RgbF:
case QImageFormat.RgbU8:
case QImageFormat.RgbaF:
case QImageFormat.RgbaU8:
break;
default:
return;
}
int count = image.Width * image.Height * image.Depth;
if (image.Format.IsFloat())
{
LockIOF io = new LockIOF(image);
for (int i = 0; i < count; i++)
{
QColorF color = io[i];
color.R *= color.A;
color.G *= color.A;
color.G *= color.A;
io[i] = color;
}
}
else
{
LockIO io = new LockIO(image);
for (int i = 0; i < count; i++)
{
QColor color = io[i];
float a = color.A/255.0f;
color.R = (byte)(color.R * a);
color.G = (byte)(color.G * a);
color.B = (byte)(color.B * a);
io[i] = color;
}
}
}
public static void Convert(QImageLock dst, QImageLock src) public static void Convert(QImageLock dst, QImageLock src)
{ {
if (dst.Format.IsU8() && src.Format.IsU8()) if (dst.Format.IsU8() && src.Format.IsU8())