Vampyre Imaging Library Forum

Imaging Category => Bugs And Other Insects => Topic started by: Patman on 30 November 2018, 17:32:25

Title: Premultiply and Unpremultiply
Post by: Patman on 30 November 2018, 17:32:25
ImagingCanvases.pas:

function TransformPremultiplyAlpha(const Pixel: TColorFPRec; P1, P2, P3: Single): TColorFPRec;
begin
  Result.A := Pixel.A;
  Result.R := Result.R * Pixel.A;
  Result.G := Result.G * Pixel.A;
  Result.B := Result.B * Pixel.A;
end;

function TransformUnPremultiplyAlpha(const Pixel: TColorFPRec; P1, P2, P3: Single): TColorFPRec;
begin
  Result.A := Pixel.A;
  if Pixel.A <> 0.0 then
  begin
    Result.R := Result.R / Pixel.A;
    Result.G := Result.G / Pixel.A;
    Result.B := Result.B / Pixel.A;
  end
  else
  begin
    Result.R := 0;
    Result.G := 0;
    Result.B := 0;
  end;
end;


IMHO the alpha operations on Result.R, Result.G and Result.B should be on Pixel.R, Pixel.G and Pixel.B:

function TransformPremultiplyAlpha(const Pixel: TColorFPRec; P1, P2, P3: Single): TColorFPRec;
begin
  Result.A := Pixel.A;
  Result.R := Pixel.R * Pixel.A;
  Result.G := Pixel.G * Pixel.A;
  Result.B := Pixel.B * Pixel.A;
end;

function TransformUnPremultiplyAlpha(const Pixel: TColorFPRec; P1, P2, P3: Single): TColorFPRec;
begin
  Result.A := Pixel.A;
  if Pixel.A <> 0.0 then
  begin
    Result.R := Pixel.R / Pixel.A;
    Result.G := Pixel.G / Pixel.A;
    Result.B := Pixel.B / Pixel.A;
  end
  else
  begin
    Result.R := 0;
    Result.G := 0;
    Result.B := 0;
  end;
end;


Title: Re: Premultiply and Unpremultiply
Post by: Galfar on 30 November 2018, 23:42:30
Thanks, that's indeed a bug.
Just pushed your fix to the repo.