source: trunk/Packages/bgrabitmap/bgrareadico.pas

Last change on this file was 2, checked in by chronos, 5 years ago
File size: 3.9 KB
Line 
1unit BGRAReadIco;
2
3{$mode objfpc}{$H+}
4{$i bgrabitmap.inc}
5
6interface
7
8uses
9 Classes, SysUtils, FPimage{$IFDEF BGRABITMAP_USE_LCL}, Graphics{$ENDIF};
10
11type
12 {$IFDEF BGRABITMAP_USE_LCL}TCustomIconClass = class of TCustomIcon;{$ENDIF}
13 TByteSet = set of byte;
14
15 { TBGRAReaderIcoOrCur }
16
17 TBGRAReaderIcoOrCur = class(TFPCustomImageReader)
18 protected
19 procedure InternalRead({%H-}Str: TStream; {%H-}Img: TFPCustomImage); override;
20 function InternalCheck(Str: TStream): boolean; override;
21 function ExpectedMagic: TByteSet; virtual; abstract;
22 {$IFDEF BGRABITMAP_USE_LCL}function LazClass: TCustomIconClass; virtual; abstract;{$ENDIF}
23 public
24 WantedWidth, WantedHeight : integer;
25 end;
26
27 TBGRAReaderIco = class(TBGRAReaderIcoOrCur)
28 protected
29 function ExpectedMagic: TByteSet; override;
30 {$IFDEF BGRABITMAP_USE_LCL}function LazClass: TCustomIconClass; override;{$ENDIF}
31 end;
32
33 { TBGRAReaderCur }
34
35 TBGRAReaderCur = class(TBGRAReaderIcoOrCur)
36 protected
37 function ExpectedMagic: TByteSet; override;
38 {$IFDEF BGRABITMAP_USE_LCL}function LazClass: TCustomIconClass; override;{$ENDIF}
39 end;
40
41implementation
42
43uses BGRABitmapTypes{$IFNDEF BGRABITMAP_USE_LCL}, BGRAIconCursor{$ENDIF};
44
45{ TBGRAReaderCur }
46
47function TBGRAReaderCur.ExpectedMagic: TByteSet;
48begin
49 result := [2];
50end;
51
52{$IFDEF BGRABITMAP_USE_LCL}function TBGRAReaderCur.LazClass: TCustomIconClass;
53begin
54 result := TCursorImage;
55end;{$ENDIF}
56
57{ TBGRAReaderIco }
58
59function TBGRAReaderIco.ExpectedMagic: TByteSet;
60begin
61 result := [1,2];
62end;
63
64{$IFDEF BGRABITMAP_USE_LCL}function TBGRAReaderIco.LazClass: TCustomIconClass;
65begin
66 result := TIcon;
67end;{$ENDIF}
68
69{ TBGRAReaderIcoOrCur }
70
71procedure TBGRAReaderIcoOrCur.InternalRead(Str: TStream; Img: TFPCustomImage);
72{$IFDEF BGRABITMAP_USE_LCL}
73var ico: TCustomIcon; i,bestIdx: integer;
74 height,width: word; format:TPixelFormat;
75 bestHeight,bestWidth: integer; maxFormat: TPixelFormat;
76 compWidth,compHeight: integer;
77begin
78 if WantedWidth > 0 then compWidth:= WantedWidth else compWidth:= 65536;
79 if WantedHeight > 0 then compHeight:= WantedHeight else compHeight:= 65536;
80 ico := LazClass.Create;
81 try
82 ico.LoadFromStream(Str);
83 bestIdx := -1;
84 bestHeight := 0;
85 bestWidth := 0;
86 maxFormat := pfDevice;
87 for i := 0 to ico.Count-1 do
88 begin
89 ico.GetDescription(i,format,height,width);
90 if (bestIdx = -1) or (abs(height-compHeight)+abs(width-compWidth) < abs(bestHeight-compHeight)+abs(bestWidth-compWidth)) or
91 ((height = bestHeight) and (width = bestWidth) and (format > maxFormat)) then
92 begin
93 bestIdx := i;
94 bestHeight := height;
95 bestWidth := width;
96 maxFormat := format;
97 end;
98 end;
99 if (bestIdx = -1) or (bestWidth = 0) or (bestHeight = 0) then
100 raise exception.Create('No adequate icon found') else
101 begin
102 ico.Current := bestIdx;
103 Img.Assign(ico);
104 end;
105 finally
106 ico.free;
107 end;
108end;
109{$ELSE}
110var icoCur: TBGRAIconCursor;
111 compWidth,compHeight: integer;
112 bmp: TBGRACustomBitmap;
113begin
114 if WantedWidth > 0 then compWidth:= WantedWidth else compWidth:= 65536;
115 if WantedHeight > 0 then compHeight:= WantedHeight else compHeight:= 65536;
116 icoCur := TBGRAIconCursor.Create(Str);
117 try
118 bmp := icoCur.GetBestFitBitmap(compWidth,compHeight);
119 try
120 Img.Assign(bmp);
121 finally
122 bmp.Free;
123 end;
124 finally
125 icoCur.Free;
126 end;
127end;
128{$ENDIF}
129
130function TBGRAReaderIcoOrCur.InternalCheck(Str: TStream): boolean;
131var {%H-}magic: packed array[0..5] of byte;
132 oldPos: int64;
133begin
134 oldPos := str.Position;
135 result := (str.Read({%H-}magic,sizeof(magic)) = sizeof(magic));
136 str.Position:= oldPos;
137 if result then
138 result := (magic[0] = $00) and (magic[1] = $00) and (magic[2] in ExpectedMagic) and (magic[3] = $00) and
139 (magic[4] + (magic[5] shl 8) > 0);
140end;
141
142initialization
143
144 DefaultBGRAImageReader[ifIco] := TBGRAReaderIco;
145 DefaultBGRAImageReader[ifCur] := TBGRAReaderCur;
146
147end.
148
Note: See TracBrowser for help on using the repository browser.