-
Notifications
You must be signed in to change notification settings - Fork 12
/
bgrareadpcx.pas
262 lines (235 loc) · 7.55 KB
/
bgrareadpcx.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
{ ***************************************************************************
* *
* 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) *****************************}
// This Unit provides some optimisations of TFPReaderPCX: decompression using a read buffer.
// It also fixes the progress message and the InternalCheck. }
unit BGRAReadPCX;
{$i bgrabitmap.inc}{$H+}
interface
uses FPImage, Classes, SysUtils, BGRATypes,{$IFNDEF FPC}Types, GraphType, BGRAGraphics,{$ENDIF} FPReadPCX;
type
{ TBGRAReaderPCX }
TBGRAReaderPCX = class(TFPReaderPCX)
protected
FBuffer: packed array of byte;
FBufferPos, FBufferSize: integer;
FBufferStream: TStream;
function InternalCheck(Stream: TStream): boolean; override;
procedure InternalRead(Stream: TStream; Img: TFPCustomImage); override;
procedure ReadScanLine({%H-}Row: integer; Stream: TStream); override;
procedure WriteScanLine(Row: integer; Img: TFPCustomImage); override;
procedure InitReadBuffer(AStream: TStream; ASize: integer);
procedure CloseReadBuffer;
function GetNextBufferByte: byte;
end;
implementation
uses BGRABitmapTypes;
procedure TBGRAReaderPCX.ReadScanLine(Row: integer; Stream: TStream);
var
P: PByte;
B: BGRANativeUInt;
bytes, Count: BGRANativeUInt;
begin
if FLineSize <= 0 then exit;
P := FScanLine;
bytes := FLineSize;
if Compressed then
begin
while bytes > 0 do
begin
B := GetNextBufferByte;
if (B < $c0) then
Count := 1
else
begin
Count := B - $c0;
B := GetNextBufferByte;
end;
if Count = 0 then continue else
if Count = 1 then
begin
P^ := B;
Inc(P);
Dec(bytes);
end else
begin
if Count > bytes then Count := bytes;
fillchar(p^, count, B);
Inc(p, count);
dec(bytes, count);
end;
end;
end
else
Stream.ReadBuffer(FScanLine^, FLineSize);
end;
procedure TBGRAReaderPCX.InternalRead(Stream: TStream; Img: TFPCustomImage);
var
H, Row: integer;
continue: boolean;
emptyRect: TRect;
begin
emptyRect := rect(0,0,0,0);
continue := True;
Progress(psStarting, 0, False, emptyRect, '', continue);
Stream.Read(Header, SizeOf(Header));
AnalyzeHeader(Img);
case BytesPerPixel of
1: CreateBWPalette(Img);
4: CreatePalette16(Img);
8: ReadPalette(stream, Img);
else
if (Header.PaletteType = 2) then
CreateGrayPalette(Img);
end;
H := Img.Height;
if Compressed then InitReadBuffer(Stream,2048);
for Row := 0 to H - 1 do
begin
ReadScanLine(Row, Stream);
WriteScanLine(Row, Img);
Progress(psRunning, (Row+1) div H, False, emptyRect, '', continue);
end;
if Compressed then CloseReadBuffer;
Progress(psEnding, 100, False, emptyRect, '', continue);
freemem(FScanLine);
end;
procedure TBGRAReaderPCX.WriteScanLine(Row: integer; Img: TFPCustomImage);
var
Col: integer;
C: TFPColor;
P, P1, P2, P3: PByte;
Z2: BGRAWord;
color: byte;
begin
C.Alpha := AlphaOpaque;
P := FScanLine;
Z2 := Header.BytesPerLine;
begin
case BytesPerPixel of
1:
begin
for Col := 0 to Img.Width - 1 do
begin
if (P[col div 8] and (128 shr (col mod 8))) <> 0 then
Img.Colors[Col, Row] := Img.Palette[1]
else
Img.Colors[Col, Row] := Img.Palette[0];
end;
end;
4:
begin
P1 := P;
Inc(P1, Z2);
P2 := P;
Inc(P2, Z2 * 2);
P3 := P;
Inc(P3, Z2 * 3);
for Col := 0 to Img.Width - 1 do
begin
color := 0;
if (P[col div 8] and (128 shr (col mod 8))) <> 0 then
Inc(color, 1);
if (P1[col div 8] and (128 shr (col mod 8))) <> 0 then
Inc(color, 1 shl 1);
if (P2[col div 8] and (128 shr (col mod 8))) <> 0 then
Inc(color, 1 shl 2);
if (P3[col div 8] and (128 shr (col mod 8))) <> 0 then
Inc(color, 1 shl 3);
Img.Colors[Col, Row] := Img.Palette[color];
end;
end;
8:
begin
for Col := 0 to Img.Width - 1 do
begin
Img.Colors[Col, Row] := Img.Palette[P[Col]];
end;
end;
24:
begin
for Col := 0 to Img.Width - 1 do
begin
with C do
begin
Red := P[col] or (P[col] shl 8);
Blue := P[col + Z2 * 2] or (P[col + Z2 * 2] shl 8);
Green := P[col + Z2] or (P[col + Z2] shl 8);
Alpha := alphaOpaque;
end;
Img[col, row] := C;
end;
end;
end;
end;
end;
procedure TBGRAReaderPCX.InitReadBuffer(AStream: TStream; ASize: integer);
begin
setLength(FBuffer,ASize);
FBufferSize := AStream.Read(FBuffer[0],ASize);
FBufferPos := 0;
FBufferStream := AStream;
end;
procedure TBGRAReaderPCX.CloseReadBuffer;
begin
FBufferStream.Position:= FBufferStream.Position-FBufferSize+FBufferPos;
end;
function TBGRAReaderPCX.GetNextBufferByte: byte;
begin
if FBufferPos < FBufferSize then
begin
result := FBuffer[FBufferPos];
inc(FBufferPos);
end else
if FBufferSize = 0 then
result := 0
else
begin
FBufferSize := FBufferStream.Read(FBuffer[0],length(FBuffer));
FBufferPos := 0;
if FBufferPos < FBufferSize then
begin
result := FBuffer[FBufferPos];
inc(FBufferPos);
end else
result := 0;
end;
end;
function TBGRAReaderPCX.InternalCheck({%H-}Stream: TStream): boolean;
var
{%H-}magic: packed array[0..3] of byte;
oldPos: BGRAInt64;
begin
oldPos:= stream.Position;
result := stream.Read({%H-}magic,SizeOf(magic)) = sizeof(magic);
stream.Position:= oldPos;
if result then
result := (magic[0] in[$0a,$0c,$cd]) and (magic[1] in [0,2,3,4,5]) and (magic[2] in[0,1]) and (magic[3] in[1,2,4,8])
end;
initialization
DefaultBGRAImageReader[ifPcx] := TBGRAReaderPCX;
end.