Changeset 494 for GraphicTest/Packages/bgrabitmap/bgrareadbmp.pas
- Timestamp:
- Dec 22, 2016, 8:49:19 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GraphicTest/Packages/bgrabitmap/bgrareadbmp.pas
r472 r494 36 36 37 37 type 38 TBMPTransparencyOption = (toAuto, toTransparent, toOpaque); 38 39 39 40 { TBGRAReaderBMP } … … 56 57 FOutputHeight: integer; 57 58 FOriginalHeight: Integer; 59 FTransparencyOption: TBMPTransparencyOption; 58 60 FBuffer: packed array of byte; 59 61 FBufferPos, FBufferSize: integer; 60 62 FBufferStream: TStream; 63 FHasAlphaValues: boolean; 61 64 // SetupRead will allocate the needed buffers, and read the colormap if needed. 62 65 procedure SetupRead(nPalette, nRowBits: Integer; Stream : TStream); virtual; … … 77 80 procedure CloseReadBuffer; 78 81 function GetNextBufferByte: byte; 82 procedure MakeOpaque(Img: TFPCustomImage); 79 83 public 80 84 MinifyHeight,WantedHeight: integer; … … 83 87 property OriginalHeight: integer read FOriginalHeight; 84 88 property OutputHeight: integer read FOutputHeight; 89 property TransparencyOption: TBMPTransparencyOption read FTransparencyOption write FTransparencyOption; 85 90 end; 86 91 87 92 implementation 88 89 uses dialogs;90 93 91 94 type … … 117 120 end; 118 121 119 Constructor TBGRAReaderBMP.create;122 constructor TBGRAReaderBMP.Create; 120 123 121 124 begin 122 125 inherited create; 123 end; 124 125 Destructor TBGRAReaderBMP.Destroy; 126 FTransparencyOption := toTransparent; 127 end; 128 129 destructor TBGRAReaderBMP.Destroy; 126 130 127 131 begin … … 130 134 end; 131 135 132 Procedure TBGRAReaderBMP.FreeBufs;136 procedure TBGRAReaderBMP.FreeBufs; 133 137 134 138 begin … … 405 409 PrevSourceRow := SourceRow-SourceRowDelta; 406 410 if (BFI.Compression=BI_RLE8) or(BFI.Compression=BI_RLE4) then InitReadBuffer(Stream,2048); 411 FHasAlphaValues:= false; 407 412 while SourceRow <> SourceLastRow+SourceRowDelta do 408 413 begin … … 434 439 if percent<>prevPercent then Progress(psRunning,percent,false,Rect,'',continue); 435 440 end; 441 if not FHasAlphaValues and (TransparencyOption = toAuto) and (BFI.BitCount = 32) then 442 MakeOpaque(Img); 436 443 Progress(psEnding,100,false,Rect,'',continue); 437 except438 on ex:exception do439 ShowMessage(ex.Message);444 finally 445 if (BFI.Compression=BI_RLE8) or(BFI.Compression=BI_RLE4) then CloseReadBuffer; 446 FreeBufs; 440 447 end; 441 if (BFI.Compression=BI_RLE8) or(BFI.Compression=BI_RLE4) then CloseReadBuffer;442 FreeBufs;443 448 end; 444 449 … … 602 607 Var 603 608 Column : Integer; 604 609 c: TFPColor; 605 610 begin 606 611 Case BFI.BitCount of … … 628 633 img.colors[Column,Row]:=ExpandColor(PLongWord(LineBuf)[Column]) 629 634 else 630 img.colors[Column,Row]:=RGBAToFPColor(PColorRGBA(LineBuf)[Column]); 635 begin 636 if FTransparencyOption = toOpaque then 637 img.colors[Column,Row]:=RGBToFPColor(PColorRGB(PColorRGBA(LineBuf)+Column)^) 638 else 639 begin 640 c := RGBAToFPColor(PColorRGBA(LineBuf)[Column]); 641 if c.alpha <> 0 then FHasAlphaValues:= true; 642 img.colors[Column,Row]:= c; 643 end; 644 end; 631 645 end; 632 646 end; … … 672 686 for Column:=0 to img.Width-1 do 673 687 begin 674 {$IFDEF ENDIAN_BIG} 675 PDWord(PDest)^ := (PWord(PSrc)^ shl 16) or ((Psrc+2)^ shl 8) or $000000ff; 676 {$ELSE} 677 PDWord(PDest)^ := PWord(PSrc)^ or ((Psrc+2)^ shl 16) or $ff000000; 678 {$ENDIF} 688 PDest^ := BGRA((Psrc+2)^,(Psrc+1)^,(Psrc)^); 679 689 inc(PDest); 680 690 inc(PSrc,3); … … 689 699 inc(PDest); 690 700 end; 691 end else Move(LineBuf^, PDest^, img.Width*SizeOf(TBGRAPixel)); 701 end else 702 if FTransparencyOption = toOpaque then 703 begin 704 if TBGRAPixel_RGBAOrder then 705 begin 706 PSrc := LineBuf; 707 for Column:=0 to img.Width-1 do 708 begin 709 PDest^:= BGRA((PSrc)^,(PSrc+1)^,(PSrc+2)^); 710 inc(PDest); 711 Inc(PSrc,4); 712 end; 713 end 714 else 715 begin 716 PSrc := LineBuf; 717 for Column:=0 to img.Width-1 do 718 begin 719 PDest^:= BGRA((PSrc+2)^,(PSrc+1)^,(PSrc+1)^); 720 inc(PDest); 721 Inc(PSrc,4); 722 end; 723 end; 724 end else 725 begin 726 if TBGRAPixel_RGBAOrder then 727 begin 728 PSrc := LineBuf; 729 for Column:=0 to img.Width-1 do 730 begin 731 PDest^:= BGRA((PSrc)^,(PSrc+1)^,(PSrc+2)^,(PSrc+3)^); 732 if PDest^.alpha <> 0 then FHasAlphaValues:= true; 733 inc(PDest); 734 Inc(PSrc,4); 735 end; 736 end 737 else 738 begin 739 PSrc := LineBuf; 740 for Column:=0 to img.Width-1 do 741 begin 742 PDest^ := PBGRAPixel(PSrc)^; 743 if PDest^.alpha <> 0 then FHasAlphaValues:= true; 744 inc(PDest); 745 Inc(PSrc,4); 746 end; 747 end; 748 end; 692 749 end; 693 750 end; … … 741 798 end; 742 799 800 procedure TBGRAReaderBMP.MakeOpaque(Img: TFPCustomImage); 801 var c: TFPColor; 802 x,y: NativeInt; 803 begin 804 if Img is TBGRACustomBitmap then 805 TBGRACustomBitmap(Img).AlphaFill(255) 806 else 807 for y := 0 to Img.Height-1 do 808 for x := 0 to Img.Width-1 do 809 begin 810 c := Img.Colors[x,y]; 811 c.alpha := alphaOpaque; 812 Img.Colors[x,y] := c; 813 end; 814 end; 815 743 816 744 817 initialization
Note:
See TracChangeset
for help on using the changeset viewer.