-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbgrareadbmpmiomap.pas
300 lines (264 loc) · 8.76 KB
/
bgrareadbmpmiomap.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
{ ***************************************************************************
* *
* This file is part of BGRABitmap library which is distributed under the *
* modified LGPL. *
* *
* See the file COPYING.modifiedLGPL.txt, included in this distribution, *
* for details about the copyright. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* *
************************* BGRABitmap library ******************************
- Drawing routines with transparency and antialiasing with Lazarus.
Offers also various transforms.
- These routines allow to manipulate 32bit images in BGRA format or RGBA
format (depending on the platform).
- This code is under modified LGPL (see COPYING.modifiedLGPL.txt).
This means that you can link this library inside your programs for any purpose.
Only the included part of the code must remain LGPL.
- If you make some improvements to this library, please notify here:
http://www.lazarus.freepascal.org/index.php/topic,12037.0.html
********************* Contact : Circular at operamail.com *******************
******************************* CONTRIBUTOR(S) ******************************
- Edivando S. Santos Brasil | [email protected]
(Compatibility with FPC ($Mode objfpc/delphi) and delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
Unit BGRAReadBmpMioMap;
{$i bgrabitmap.inc}{$H+}
interface
uses
Classes, SysUtils, BGRATypes,{$IFNDEF FPC}Types, GraphType, BGRAGraphics,{$ENDIF} FPImage, BGRABitmapTypes;
const
MioMapMagicValue = 'RL';
MioMapTransparentColor = $F81F;
type
TMioHeader = packed record
magic: packed array[1..2] of {$IFDEF FPC}Char{$ELSE}AnsiChar{$ENDIF};
format: BGRAWord;
width,height,nbColors,nbChunks: BGRAWord;
end;
TPixelArray = array of TBGRAPixel;
{ TBGRAReaderBmpMioMap }
TBGRAReaderBmpMioMap = class(TFPCustomImageReader)
private
function ReadHeader(Stream: TStream; out header: TMioHeader): boolean;
function ReadPalette(Stream: TStream; nbColors: integer; alphaChannel: boolean): TPixelArray;
procedure UncompressChunks(Stream: TStream; nbChunks: integer; palette: TPixelArray; img: TFPCustomImage);
public
procedure InternalRead (Stream:TStream; Img:TFPCustomImage); override;
function InternalCheck (Stream:TStream) : boolean; override;
end;
function MioMapToBGRA(AColor: BGRAWord): TBGRAPixel;
function BGRAToMioMap(const AColor: TBGRAPixel): BGRAWord;
function MioMapToAlpha(AValue: Byte): Byte;
function AlphaToMioMap(AValue: Byte): Byte;
implementation
{$IFDEF FPC}//#
uses bufstream;
{$ENDIF}
function MioMapToBGRA(AColor: BGRAWord): TBGRAPixel;
begin
if AColor = MioMapTransparentColor then
result := BGRAPixelTransparent
else
result := Color16BitToBGRA(AColor);
end;
function BGRAToMioMap(const AColor: TBGRAPixel): BGRAWord;
begin
if AColor.alpha < 7 then
result := MioMapTransparentColor
else
begin
result := BGRAToColor16Bit(AColor);
if result = MioMapTransparentColor then dec(result);
end;
end;
function MioMapToAlpha(AValue: Byte): Byte;
begin
result := AValue*255 div 32;
end;
function AlphaToMioMap(AValue: Byte): Byte;
begin
result := (AValue*32 + 64) div 255;
end;
{ TBGRAReaderBmpMioMap }
function TBGRAReaderBmpMioMap.ReadHeader(Stream: TStream; out header: TMioHeader
): boolean;
begin
result := false;
fillchar({%H-}header,sizeof(header),0);
if stream.Read(header, sizeof(header))<> sizeof(header) then exit;
if header.magic <> MioMapMagicValue then exit;
{$IFNDEF BDS}header.format:= LEtoN(header.format);{$ENDIF}
{$IFNDEF BDS}header.width:= LEtoN(header.width);{$ENDIF}
{$IFNDEF BDS}header.height:= LEtoN(header.height);{$ENDIF}
{$IFNDEF BDS}header.nbColors:= LEtoN(header.nbColors);{$ENDIF}
{$IFNDEF BDS}header.nbChunks:= LEtoN(header.nbChunks);{$ENDIF}
if header.format > 1 then exit;
result := true;
end;
function TBGRAReaderBmpMioMap.ReadPalette(Stream: TStream; nbColors: integer;
alphaChannel: boolean): TPixelArray;
var mioPalette: packed array of BGRAWord;
nbColorsRead,i: integer;
colorValue: BGRAWord;
alphaPalette: packed array of byte;
begin
setlength(mioPalette, nbColors);
setlength(result,nbColors);
nbColorsRead:= Stream.Read({%H-}mioPalette[0], nbColors*2) div 2;
for i := 0 to nbColorsRead-1 do
begin
colorValue := {$IFNDEF BDS}LEtoN{$ENDIF}(mioPalette[i]);
result[i] := MioMapToBGRA(colorValue);
end;
for i := nbColorsRead to nbColors-1 do
result[i] := BGRAPixelTransparent;
if alphaChannel then
begin
setlength(alphaPalette,nbColors);
Stream.Read(alphaPalette[0],nbColors);
for i := 0 to nbColors-1 do
if mioPalette[i] <> MioMapTransparentColor then
result[i].alpha := MioMapToAlpha(alphaPalette[i]);
end;
end;
procedure TBGRAReaderBmpMioMap.UncompressChunks(Stream: TStream; nbChunks: integer;
palette: TPixelArray; img: TFPCustomImage);
var i,maxChunkSize: integer;
chunkSizes: array of integer;
chunkData: packed array of byte;
pos,bytesRead: integer;
palLen: integer;
x,y: integer;
p: PBGRAPixel;
colorOffset: integer;
b: byte;
w,h: integer;
procedure UncompressPixel(colorNumber, repeatCount: integer);
var
c: TBGRAPixel;
begin
if colorNumber >= palLen then
c := BGRAPixelTransparent
else
c := palette[colorNumber];
while (repeatCount > 0) and (y < h) do
begin
if p <> nil then
begin
p^ := c;
inc(p);
end else
img.Colors[x,y] := BGRAToFPColor(c);
inc(x);
if x = w then
begin
x := 0;
inc(y);
if p <> nil then
begin
if y >= h then p := nil
else
p := TBGRACustomBitmap(Img).ScanLine[y];
end;
end;
dec(repeatCount);
end;
end;
begin
palLen := length(palette);
if (img.Width = 0) or (img.Height = 0) or (palLen = 0) then exit;
maxChunkSize := 1;
setlength(chunkSizes, nbChunks);
for i := 0 to nbChunks-1 do
begin
if stream.read({%H-}b,1)=0 then b := 0;
if b < 255 then
begin
chunkSizes[i] := b;
end else
begin
if stream.read(b,1)=0 then b := 0;
chunkSizes[i] := b shl 8;
if stream.read(b,1)=0 then b := 0;
chunkSizes[i] := chunkSizes[i] +b;
end;
if chunkSizes[i]>maxChunkSize then
maxChunkSize := chunkSizes[i];
end;
setlength(chunkData, maxChunkSize);
x := 0;
y := 0;
w := img.Width;
h := img.Height;
colorOffset:= 0;
if Img is TBGRACustomBitmap then
begin
p := TBGRACustomBitmap(Img).ScanLine[y];
TBGRACustomBitmap(Img).FillTransparent;
end
else
p := nil;
for i:= 0 to nbChunks-1 do
begin
bytesRead := Stream.Read(chunkData[0], chunkSizes[i]);
pos := 0;
while pos < bytesRead do
begin
if (chunkData[pos] = $FE) and (pos+2 < bytesRead) then
begin
UncompressPixel(chunkData[pos+1]+colorOffset,chunkData[pos+2]);
inc(pos,3);
end else
if (chunkData[pos] = $ff) and (pos+1 < bytesRead) then
begin
UncompressPixel(0,chunkData[pos+1]);
inc(pos,2);
end else
if (chunkData[pos] = $fd) and (pos+2 < bytesRead) then
begin
colorOffset:= chunkData[pos+1] + (chunkData[pos+2] shl 8);
inc(pos,3);
end else
if chunkData[pos] = 0 then
begin
UncompressPixel(0,1);
inc(pos);
end else
begin
UncompressPixel(chunkData[pos]+colorOffset,1);
inc(pos);
end;
end;
end;
end;
procedure TBGRAReaderBmpMioMap.InternalRead(Stream: TStream; Img: TFPCustomImage);
{$IFDEF FPC}//#
var header: TMioHeader;
palette: TPixelArray;
buf: TReadBufStream;
{$ENDIF}
begin
{$IFDEF FPC}//#
if not ReadHeader(stream, header) then exit;
buf := TReadBufStream.Create(Stream,1024);
Img.SetSize(header.width,header.height);
palette := ReadPalette(stream, header.nbColors, header.format = 1);
UncompressChunks(stream,header.nbChunks, palette, Img);
buf.Free;
{$ENDIF}
end;
function TBGRAReaderBmpMioMap.InternalCheck(Stream: TStream): boolean;
var OldPosition : BGRAInt64;
dummy: TMioHeader;
begin
OldPosition:= stream.Position;
result := ReadHeader(stream, dummy);
stream.Position:= OldPosition;
end;
initialization
DefaultBGRAImageReader[ifBmpMioMap] := TBGRAReaderBmpMioMap;
end.