Mega Code Archive

 
Categories / Delphi / Graphic
 

Png2bmp

//www.dronymc.cjb.net //drony@mynet.com //icq:266148308 function TdtpPngFormat.LoadImageFromStream(S: TStream; ADIB: TBitmap32; AMaxSize: TPoint; Page: integer): integer; var i, j: integer; APng: TPngObject; P: PColor32; A: PByte; ABmp: TBitmap; Canvas: TCanvas; begin if Page > 0 then begin Result := creIllegalPageNum; exit; end; APng := TPngObject.Create; ABmp := TBitmap.Create; try {Load and assign to the a bitmap} APng.LoadFromStream(S); APng.AssignTo(ABmp); {We now draw the bitmap to our bitmap. This will clear alpha in places where drawn, so we can use it later} ADib.SetSize(APng.Width, APng.Height); ADib.Clear(clBlack32); Canvas := TCanvas.Create; try Canvas.Handle := ADib.Handle; TGraphicAccess(ABmp).Draw(Canvas, Rect(0, 0, ADib.Width, ADib.Height)); finally Canvas.Free; end; {Flip the alpha channel} P := @ADib.Bits[0]; for i := 0 to ADib.Width * ADib.Height - 1 do begin P^ := P^ XOR $FF000000; inc(P); end; {The previous doesn't handle bitwise alpha info, so we do that here} for i := 0 to APng.Height - 1 do begin A := PByte(APng.AlphaScanLine[i]); if assigned(A) then begin P := @ADib.Bits[i * ADib.Width]; for j := 0 to APng.Width - 1 do begin P^ := SetAlpha(P^, A^); inc(P); inc(A); end; end else break; end; {Arriving here means "all ok"} Result := creAllOK; finally APng.Free; ABmp.Free; end; end;