-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpcximage.pas
817 lines (743 loc) · 21.1 KB
/
pcximage.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
unit pcximage;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
pRGBArray = ^TRGBArray;
TRGBArray = array[0..$FFFF] of TRGBTriple;
type
TPCXImage = class(TGraphic)
private
FBitmap: TBitmap;
buf: array[0..128] of byte;
ibuf2: pRGBArray;
ibuf: array[0..$FFFF] of byte;
ibuftmp: PByteArray;
rpal: TMaxLogPalette;
wpal: array[0..255] of PALETTEENTRY;
function RLECompress(Stream: TStream): TStream;
function RLEDecompress(Stream: TStream): TMemoryStream;
public
procedure Assign(Source: TPersistent); override;
procedure AssignTo(Dest: TPersistent); override;
constructor Create; override;
destructor Destroy; override;
procedure Draw(ACanvas: TCanvas; const ARect: TRect); override;
function Equals(Graphic: TGraphic): Boolean; override;
function GetBitmap: TBitmap;
function GetEmpty: Boolean; override;
function GetHeight: Integer; override;
function GetPalette: HPALETTE; override;
function GetPixelFormat: TPixelFormat;
function GetTransparent: Boolean; override;
function GetWidth: Integer; override;
function HasBitmap: boolean;
procedure LoadFromClipboardFormat(AFormat: Word; AData: THandle; APalette: HPALETTE); override;
procedure LoadFromFile(const FileName: string); override;
procedure LoadFromResourceID(Instance: THandle; ResID: Integer; ResType: PChar);
procedure LoadFromResourceName(Instance: THandle; const ResName: string; ResType: PChar);
procedure LoadFromStream(Stream: TStream); override;
procedure SaveToClipboardFormat(var AFormat: Word; var AData: THandle; var APalette: HPALETTE); override;
procedure SaveToFile(const FileName: string); override;
procedure SaveToStream(Stream: TStream); override;
procedure SetHeight(Value: integer); override;
procedure SetWidth(Value: integer); override;
procedure WriteData(Stream: TStream); override;
end;
EPcxError = class(Exception)
end;
implementation
resourcestring
RC_PcxUnknowFormat = 'PCX : unknown format';
RC_PcxPaletteProblem = 'PCX : Unable to retrieve palette !';
RC_PcxInvalid = 'PCX : This isn''t a valid PCX image';
RC_PcxExtension = 'pcx';
RC_PcxFilterName = 'PCX Image';
procedure TPCXImage.Assign(Source: TPersistent);
var
ts: TStream;
begin
if Source = nil then
begin
if FBitmap <> nil then
FreeAndNil(FBitmap);
end
else if (Source is TPCXImage) and (Source <> Self) then
begin
if FBitmap <> nil then
FreeAndNil(FBitmap);
FBitmap := TBitmap.Create;
ts := TMemoryStream.Create;
try
TPCXImage(Source).SaveToStream(ts);
ts.Position := 0;
LoadFromStream(ts);
finally
ts.Free;
end;
end
else if Source is TBitmap then
begin
if FBitmap <> nil then
FreeAndNil(FBitmap);
FBitmap := TBitmap.Create;
FBitmap.Assign(TBitmap(Source));
end
else
inherited Assign(Source);
end;
procedure TPCXImage.AssignTo(Dest: TPersistent);
begin
if (Dest is TPCXImage) then
Dest.Assign(Self)
else if (Dest is TGraphic) then
begin
if Empty then
Dest.assign(nil)
else
(Dest as TGraphic).Assign(FBitmap)
end
else
inherited AssignTo(Dest);
end;
constructor TPCXImage.Create;
begin
inherited Create;
FBitmap := TBitmap.Create;
FBitmap.width := 0;
FBitmap.height := 0;
FBitmap.Palette := 0;
end;
destructor TPCXImage.Destroy;
begin
if FBitmap <> nil then
begin
if FBitmap.Palette <> 0 then
DeleteObject(FBitmap.Palette);
FreeAndNil(FBitmap);
end;
inherited Destroy;
end;
procedure TPCXImage.Draw(ACanvas: TCanvas; const ARect: TRect);
begin
if FBitmap <> nil then
ACanvas.StretchDraw(ARect, FBitmap);
end;
function TPCXImage.Equals(Graphic: TGraphic): Boolean;
begin
if FBitmap = nil then
Result := Graphic = nil
else
Result := (Graphic is TPCXImage) and (FBitmap = TPCXImage(TPCXImage).FBitmap)
end;
function TPCXImage.GetBitmap: TBitmap;
begin
Result := FBitmap;
end;
function TPCXImage.GetEmpty: Boolean;
begin
Result := FBitmap = nil;
end;
function TPCXImage.GetHeight: Integer;
begin
if FBitmap <> nil then
Result := FBitmap.height
else
Result := -1;
end;
function TPCXImage.GetPalette: HPALETTE;
begin
if FBitmap <> nil then
Result := FBitmap.Palette
else
Result := 0;
end;
function TPCXImage.GetPixelFormat: TPixelFormat;
begin
if FBitmap <> nil then
Result := FBitmap.PixelFormat
else
Result := pfCustom;
end;
function TPCXImage.GetTransparent: Boolean;
begin
if FBitmap <> nil then
Result := FBitmap.Transparent
else
Result := False;
end;
function TPCXImage.GetWidth: Integer;
begin
if FBitmap <> nil then
Result := FBitmap.width
else
Result := -1;
end;
function TPCXImage.HasBitmap: boolean;
begin
Result := FBItmap <> nil;
end;
procedure TPCXImage.LoadFromClipboardFormat(AFormat: Word; AData: THandle; APalette: HPALETTE);
begin
if FBitmap <> nil then
FBitmap.LoadFromClipboardFormat(AFormat, AData, APalette);
end;
procedure TPCXImage.LoadFromFile(const FileName: string);
var
fs: TFileStream;
begin
fs := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
Self.LoadFromStream(fs);
finally
fs.Free;
end;
end;
procedure TPCXImage.LoadFromResourceID(Instance: THandle; ResID: Integer; ResType: PChar);
var
Stream: TStream;
begin
Stream := TResourceStream.CreateFromID(Instance, ResId, ResType);
try
Self.LoadFromStream(Stream);
finally
Stream.Free;
end;
end;
procedure TPCXImage.LoadFromResourceName(Instance: THandle;
const ResName: string; ResType: PChar);
var
Stream: TStream;
begin
Stream := TResourceStream.Create(Instance, ResName, ResType);
try
Self.LoadFromStream(Stream);
finally
Stream.Free;
end;
end;
function TPCXImage.RLECompress(Stream: TStream): TStream;
var
count, count2, count3, i: integer;
buf: array[0..1024] of byte;
buf2: array[0..64000] of byte;
b: byte;
begin
Result := TMemoryStream.Create;
count := 1024;
while count = 1024 do
begin
count := Stream.read(buf, 1024);
count2 := 0;
i := 0;
while i < count do
begin
b := buf[i];
count3 := 0;
while (buf[i] = b) and (i < count) and (count3 < $30) do
begin
inc(i);
inc(count3);
end;
if (i = count) and (count3 in [2..$2F]) and (count = 1024) then
Stream.position := Stream.position - count3
else
begin
if count3 = 1 then
begin
if (b and $C0) = $C0 then
begin
buf2[count2] := $C1;
buf2[count2 + 1] := b;
inc(count2, 2);
end
else
begin
buf2[count2] := b;
inc(count2);
end;
end
else
begin
buf2[count2] := count3 or $C0;
buf2[count2 + 1] := b;
inc(count2, 2);
end;
end;
end;
Result.Write(buf2, count2);
end;
Result.position := 0;
end;
function TPCXImage.RLEDecompress(Stream: TStream): TMemoryStream;
var
count, count2, count3, i: integer;
buf: array[0..1024] of byte;
buf2: array[0..64000] of byte;
b: byte;
begin
Result := TMemoryStream.Create;
count := 1024;
while count = 1024 do
begin
count := Stream.Read(buf, 1024);
count2 := 0;
i := 0;
while i < count do
begin
if (buf[i] and $C0) = $C0 then
begin
if (i = count - 1) then
Stream.Position := Stream.Position - 1
else
begin
b := buf[i] and $3F;
inc(i);
for count3 := count2 to count2 + b - 1 do
buf2[count3] := buf[i];
count2 := count2 + b;
end;
end
else
begin
buf2[count2] := buf[i];
inc(count2);
end;
inc(i);
end;
Result.Write(buf2, count2);
end;
Result.Position := 0;
end;
procedure TPCXImage.LoadFromStream(Stream: TStream);
var
i, j, k, l, m: Integer;
w1, w2, w3, w4: word;
BytesPerLine: Integer;
bpp, planes: Integer;
compressed: TMemoryStream;
decompressed: TMemoryStream;
begin
if FBitmap = nil then
FBitmap := TBitmap.Create;
i := Stream.Read(buf, 128);
FBitmap.width := 0;
FBitmap.height := 0;
if i = 128 then
begin
if buf[0] = 10 then
begin
w1 := buf[4] + buf[5] * 256;
w2 := buf[6] + buf[7] * 256;
w3 := buf[8] + buf[9] * 256;
w4 := buf[10] + buf[11] * 256;
FBitmap.Width := (w3 - w1) + 1;
FBitmap.Height := (w4 - w2) + 1;
FBItmap.Canvas.FloodFill(0, 0, clWhite, fsSurface);
BytesPerLine := (Buf[66] + Buf[67] * 256) * buf[65];
l := FBitmap.width * 2;
m := FBitmap.width;
bpp := buf[3];
planes := buf[65];
case bpp of
1: case planes of
1:
begin
FBitmap.PixelFormat := pf1bit;
FBitmap.Monochrome := True;
FBitmap.IgnorePalette := True;
FBitmap.Palette := 0;
end;
4:
begin
FBitmap.PixelFormat := pf4bit;
FBitmap.Monochrome := False;
FBitmap.IgnorePalette := False;
FBitmap.Palette := 0;
end;
end;
8: case planes of
1:
begin //256 colors
FBitmap.PixelFormat := pf8bit;
FBitmap.Monochrome := False;
FBitmap.IgnorePalette := False;
FBitmap.Palette := 0;
end;
3:
begin //16 millions colors
FBitmap.PixelFormat := pf24bit;
FBitmap.Monochrome := False;
FBitmap.IgnorePalette := True;
FBitmap.Palette := 0;
end;
else
raise EPcxError.Create(RC_PcxUnknowFormat);
end;
else
raise EPcxError.Create(RC_PcxUnknowFormat);
end;
compressed := TMemoryStream.Create;
compressed.CopyFrom(stream, stream.Size - stream.position);
compressed.Position := 0;
decompressed := RLEDecompress(compressed);
compressed.Free;
if (bpp = 1) and (planes = 1) then
begin
//monochrome okey
for i := 0 to FBitmap.height - 1 do
begin
ibuftmp := FBitmap.ScanLine[i];
decompressed.read(ibuf, BytesPerLine);
CopyMemory(ibuftmp, @ibuf, BytesPerLine);
end;
end
else if (bpp = 1) and (planes = 4) then
begin
//16 color palette
stream.position := 16;
if stream.read(ibuf, 48) <> 48 then
raise EPcxError.Create(RC_PcxPaletteProblem)
else
begin
rpal.palVersion := $300;
rpal.palNumEntries := 16;
for m := 0 to 15 do
begin
i := m * 3;
rpal.palPalEntry[m].peRed := ibuf[i];
rpal.palPalEntry[m].peGreen := ibuf[i + 1];
rpal.palPalEntry[m].peBlue := ibuf[i + 2];
rpal.palPalEntry[m].peFlags := 0;
end;
FBitmap.Palette := CreatePalette(tagLogPalette((@rpal)^));
PaletteModified := True;
end;
//Reading data
for i := 0 to FBitmap.height - 1 do
begin
decompressed.read(ibuf, BytesPerLine);
ibuftmp := FBitmap.scanline[i];
fillChar(ibuftmp^, (FBitmap.Width div 2) + 1, 0);
l := 0;
for k := 0 to FBitmap.Width - 1 do
if ((ibuf[l + k div 8]) and (1 shl (7 - (k mod 8)))) <> 0 then
if k mod 2 <> 0 then
ibuftmp^[k div 2] := ibuftmp^[k div 2] + $01
else
ibuftmp^[k div 2] := ibuftmp^[k div 2] + $10;
l := BytesPerLine div 4;
for k := 0 to FBitmap.Width - 1 do
if ((ibuf[l + k div 8]) and (1 shl (7 - (k mod 8)))) <> 0 then
if k mod 2 <> 0 then
ibuftmp^[k div 2] := ibuftmp^[k div 2] + $02
else
ibuftmp^[k div 2] := ibuftmp^[k div 2] + $20;
l := (BytesPerLine div 4) * 2;
for k := 0 to FBitmap.Width - 1 do
if ((ibuf[l + k div 8]) and (1 shl (7 - (k mod 8)))) <> 0 then
if k mod 2 <> 0 then
ibuftmp^[k div 2] := ibuftmp^[k div 2] + $04
else
ibuftmp^[k div 2] := ibuftmp^[k div 2] + $40;
l := (BytesPerLine div 4) * 3;
for k := 0 to FBitmap.Width - 1 do
if ((ibuf[l + k div 8]) and (1 shl (7 - (k mod 8)))) <> 0 then
if k mod 2 <> 0 then
ibuftmp^[k div 2] := ibuftmp^[k div 2] + $08
else
ibuftmp^[k div 2] := ibuftmp^[k div 2] + $80;
end;
end
else if (bpp = 8) and (Planes = 1) then
begin
//256 Colors. Okey
stream.Position := stream.Size - (256 * 3 + 1);
stream.Read(ibuf, 1000);
if ibuf[0] = 12 then
begin
rpal.palVersion := $300;
rpal.palNumEntries := 256;
for m := 0 to 255 do
begin
i := m * 3 + 1;
rpal.palPalEntry[m].peRed := ibuf[i];
rpal.palPalEntry[m].peGreen := ibuf[i + 1];
rpal.palPalEntry[m].peBlue := ibuf[i + 2];
rpal.palPalEntry[m].peFlags := 0;
end;
FBitmap.Palette := CreatePalette(tagLogPalette((@rpal)^));
PaletteModified := True;
FBitmap.PaletteModified := True;
Changed(self);
end;
for i := 0 to FBitmap.height - 1 do
begin
ibuftmp := FBitmap.ScanLine[i];
decompressed.read(ibuf, BytesPerLine);
CopyMemory(ibuftmp, @ibuf, BytesPerLine);
end;
end
else if (bpp = 8) and (Planes = 3) then
begin
//24 bit. Okey
for i := 0 to FBitmap.height - 1 do
begin
ibuf2 := FBitmap.ScanLine[i];
decompressed.read(ibuf, BytesPerLine);
for j := 0 to FBitmap.width - 1 do
begin
ibuf2[j].rgbtRed := ibuf[j];
ibuf2[j].rgbtGreen := ibuf[j + m];
ibuf2[j].rgbtBlue := ibuf[j + l];
end;
end;
end;
decompressed.Free;
end
else
raise EPcxError.Create(RC_PcxInvalid);
end
else
raise EPcxError.Create(RC_PcxInvalid);
PaletteModified := True;
Changed(self);
end;
procedure TPCXImage.SaveToClipboardFormat(var AFormat: Word;
var AData: THandle; var APalette: HPALETTE);
begin
if FBitmap <> nil then
FBitmap.SaveToClipboardFormat(AFormat, Adata, APalette);
end;
procedure TPCXImage.SaveToFile(const FileName: string);
var
stream: TFileStream;
begin
Stream := TFileStream.Create(FileName, fmCreate or fmShareExclusive);
SaveToStream(Stream);
Stream.Free;
end;
procedure TPCXImage.SaveToStream(Stream: TStream);
var
i, j, k, l: Integer;
bytesperline: word;
st: TStream;
b: byte;
begin
if FBitmap = nil then
Exit;
if (FBitmap.PixelFormat <> pf1Bit) and (FBitmap.PixelFormat <> pf4Bit) and
(FBitmap.PixelFormat <> pf8Bit) and (FBitmap.PixelFormat <> pf24Bit) then
FBItmap.pixelformat := pf24bit;
//St -> temp stream to put the data
st := TMemoryStream.Create;
// header
FillChar(ibuf, 128, 0);
ibuf[0] := $A;
ibuf[1] := 5;
ibuf[2] := 1;
ibuf[8] := lo(FBitmap.Width) - 1;
ibuf[9] := hi(FBitmap.width);
ibuf[10] := lo(FBitmap.height) - 1;
ibuf[11] := hi(FBitmap.height);
ibuf[12] := 1;
ibuf[13] := 300 - 256;
ibuf[14] := 1;
ibuf[15] := 300 - 256;
ibuf[69] := 1;
ibuf[70] := hi(screen.height);
ibuf[71] := lo(screen.height);
ibuf[72] := hi(screen.Width);
ibuf[73] := lo(screen.Width);
case FBitmap.PixelFormat of
pf1bit:
begin
ibuf[3] := 1;
ibuf[65] := 1;
BytesPerLine := FBitmap.width div 8;
if (FBitmap.Width mod 8 <> 0) then
inc(BytesPerLine);
ibuf[66] := lo(BytesPerLine);
ibuf[67] := hi(BytesPerLine);
stream.Write(ibuf, 128);
//Write data
for i := 0 to FBitmap.height - 1 do
begin
ibuftmp := FBitmap.ScanLine[i];
CopyMemory(@ibuf, ibuftmp, BytesPerLine);
st.write(ibuf, BytesPerLine);
end;
end;
pf4bit:
begin
ibuf[3] := 1;
ibuf[65] := 4;
BytesPerLine := FBitmap.width div 8;
if (FBitmap.Width mod 8 <> 0) then
inc(BytesPerLine);
i := BytesPerLine;
BytesPerLine := BytesPerLine * 4;
ibuf[66] := lo(i);
ibuf[67] := hi(i);
//Write palette
if (FBitmap.Palette <> 0) then
begin
GetPaletteEntries(FBitmap.Palette, 0, 16, wpal);
for i := 0 to 15 do
begin
ibuf[16 + i * 3] := wpal[i].peRed;
ibuf[16 + i * 3 + 1] := wpal[i].peGreen;
ibuf[16 + i * 3 + 2] := wpal[i].peBlue;
end;
end;
stream.Write(ibuf, 128);
//Write data
for i := 0 to FBitmap.height - 1 do
begin
ibuftmp := FBitmap.Scanline[i];
fillchar(ibuf, BytesPerLine, 0);
//Red
l := 0;
for j := 0 to FBitmap.Width - 1 do
if (j mod 2) = 0 then
begin
if (ibuftmp^[j div 2] and $10) <> 0 then
ibuf[l + j div 8] := ibuf[l + j div 8] + (1 shl (7 - (j mod 8)));
end
else
begin
if (ibuftmp^[j div 2] and $01) <> 0 then
ibuf[l + j div 8] := ibuf[l + j div 8] + (1 shl (7 - (j mod 8)));
end;
//Green
l := BytesPerLine div 4;
for j := 0 to FBitmap.Width - 1 do
if (j mod 2) = 0 then
begin
if (ibuftmp^[j div 2] and $20) <> 0 then
ibuf[l + j div 8] := ibuf[l + j div 8] + (1 shl (7 - (j mod 8)));
end
else
begin
if (ibuftmp^[j div 2] and $02) <> 0 then
ibuf[l + j div 8] := ibuf[l + j div 8] + (1 shl (7 - (j mod 8)));
end;
//Blue
l := BytesPerLine div 2;
for j := 0 to FBitmap.Width - 1 do
if (j mod 2) = 0 then
begin
if (ibuftmp^[j div 2] and $40) <> 0 then
ibuf[l + j div 8] := ibuf[l + j div 8] + (1 shl (7 - (j mod 8)));
end
else
begin
if (ibuftmp^[j div 2] and $04) <> 0 then
ibuf[l + j div 8] := ibuf[l + j div 8] + (1 shl (7 - (j mod 8)));
end;
//Intensity
l := (BytesPerLine div 4) * 3;
for j := 0 to FBitmap.Width - 1 do
if (j mod 2) = 0 then
begin
if (ibuftmp^[j div 2] and $80) <> 0 then
ibuf[l + j div 8] := ibuf[l + j div 8] + (1 shl (7 - (j mod 8)));
end
else
begin
if (ibuftmp^[j div 2] and $08) <> 0 then
ibuf[l + j div 8] := ibuf[l + j div 8] + (1 shl (7 - (j mod 8)));
end;
st.Write(ibuf, BytesPerLine);
end;
end;
pf8bit:
begin
ibuf[3] := 8;
ibuf[65] := 1;
BytesPerLine := FBitmap.width;
ibuf[66] := lo(BytesPerLine);
ibuf[67] := hi(BytesPerLine);
stream.Write(ibuf, 128);
//Write Data
for i := 0 to FBitmap.height - 1 do
begin
ibuftmp := FBitmap.ScanLine[i];
CopyMemory(@ibuf, ibuftmp, BytesPerLine);
st.write(ibuf, BytesPerLine);
end;
end;
pf24bit:
begin
ibuf[3] := 8;
ibuf[65] := 3;
BytesPerLine := FBitmap.width * 3;
i := FBitmap.width;
ibuf[66] := lo(i);
ibuf[67] := hi(i);
stream.Write(ibuf, 128);
//Write data
for i := 0 to FBitmap.height - 1 do
begin
ibuf2 := FBitmap.ScanLine[i];
for j := 0 to FBitmap.Width - 1 do
ibuf[j] := ibuf2[j].rgbtRed;
k := FBitmap.Width;
for j := 0 to FBitmap.Width - 1 do
ibuf[j + k] := ibuf2[j].rgbtGreen;
k := FBitmap.Width * 2;
for j := 0 to FBitmap.Width - 1 do
ibuf[j + k] := ibuf2[j].rgbtBlue;
st.write(ibuf, BytesPerLine);
end;
end;
end;
//RLE Compress temporary stream
st.Position := 0;
st := RLECompress(st);
//Copy temporary stream to final stream
st.Position := 0;
stream.CopyFrom(st, st.size);
//Write palette if mode 256 color.
if (FBitmap.PixelFormat = pf8bit) and (FBitmap.Palette <> 0) then
begin
GetPaletteEntries(FBitmap.Palette, 0, 256, wpal);
for i := 0 to 255 do
begin
ibuf[i * 3] := wpal[i].peRed;
ibuf[i * 3 + 1] := wpal[i].peGreen;
ibuf[i * 3 + 2] := wpal[i].peBlue;
end;
b := 12;
stream.write(b, 1);
stream.write(ibuf, 256 * 3);
end;
st.Free;
end;
procedure TPCXImage.SetHeight(Value: integer);
begin
if FBitmap <> nil then
begin
FBitmap.Height := Value;
Changed(self);
end;
end;
procedure TPCXImage.SetWidth(Value: integer);
begin
if FBitmap <> nil then
begin
FBitmap.Width := Value;
Changed(self);
end;
end;
procedure TPCXImage.WriteData(Stream: TStream);
begin
SaveToStream(Stream);
end;
initialization
Registerclass(TPCXImage);
TPicture.RegisterFileFormat(RC_PcxExtension, RC_PcxFilterName, TPCXImage);
finalization
TPicture.UnRegisterGraphicclass(TPCXImage);
end.