-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrwpcx.pas
959 lines (859 loc) · 22 KB
/
rwpcx.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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
{$mode TP}
Unit rwpcx;
Interface
uses
bits,RMCore,Dialogs,SysUtils;
Function ReadPcxImg(x,y,x2,y2,Pal,pm : Word;Filename : string): word;
Function SavePcxImg(x,y,x2,y2 : Word;Filename : String): Word;
Implementation
Type
PaletteT = Array[0..255,0..2] of Byte;
PcxPalette = Array[0..255,0..2] of Byte;
PCXHeaderRec = Record
Manufacturer: byte; (* Always 10 for PCX file *)
Version: byte; (* 2 - old PCX - no palette (not used anymore),
3 - no palette,
4 - Microsoft Windows - no palette (only in
old files, new Windows version uses 3),
5 - with palette *)
Encoding: byte; (* 1 is PCX, it is possible that we may add
additional encoding methods in the future *)
Bits_per_pixel: byte; (* Number of bits to represent a pixel
(per plane) - 1, 2, 4, or 8 *)
Xmin: integer; (* Image window dimensions (inclusive) *)
Ymin: integer; (* Xmin, Ymin are usually zero (not always) *)
Xmax: integer;
Ymax: integer;
Hdpi: integer; (* Resolution of image (dots per inch) *)
Vdpi: integer; (* Set to scanner resolution - 300 is default *)
ColorMap: array [0..15, 0..2] of byte;
(* RGB palette data (16 colors or less)
256 color palette is appended to end of file *)
Reserved: byte; (* (used to contain video mode)
now it is ignored - just set to zero *)
Nplanes: byte; (* Number of planes *)
Bytes_per_line_per_plane: integer; (* Number of bytes to allocate
for a scanline plane.
MUST be an an EVEN number!
Do NOT calculate from Xmax-Xmin! *)
PaletteInfo: integer; (* 1 = black & white or color image,
2 = grayscale image - ignored in PB4, PB4+
palette must also be set to shades of gray! *)
HscreenSize: Integer; (* added for PC Paintbrush IV Plus ver 1.0, *)
VscreenSize: Integer; (* PC Paintbrush IV ver 1.02 (and later) *)
(* I know it is tempting to use these fields
to determine what video mode should be used
to display the image - but it is NOT
recommended since the fields will probably
just contain garbage. It is better to have
the user install for the graphics mode he
wants to use... *)
Filler: array [74..127] of Byte;
end;
Const
MaxWidth = 2048; (* arbitrary - maximum width (in bytes) of a PCX image *)
CompressNum = $C0; (* this is the upper two bits that indicate a count *)
MaxBlock = 4096;
type
BlockArray = Array [0..MaxBlock] of Byte;
UncpLine = Array [0..MaxWidth] of Byte;
CompressedLine = Array[0..MaxWidth*2] of Byte;
var
BlockData: ^BlockArray; (* 4k data buffer *)
NextByte : Integer; (* index into file buffer in ReadByte *)
Index : Integer; (* PCXline index - where to put Data *)
Data : byte; (* PCX compressed data byte *)
myPcx : PcxHeaderRec;
myPal : PcxPalette;
Function BytesPerRow(width : word) : Word;
begin
BytesPerRow :=(width+7) div 8;
end;
Procedure GetPcxInfo(Filename : string; Var PcxH : PcxHeaderRec;Var Pal : PaletteT);
Var
F : File;
Error : Word;
Begin
{$I-}
Assign(F,Filename);
Reset(F,1);
BlockRead(F,PcxH,sizeof(PcxH));
If (PcxH.Bits_Per_Pixel = 8) and (PcxH.Version=5) then
begin
Seek(F,FileSize(F)-768);
BlockRead(F,Pal,SizeOf(Pal));
end
else
begin
Move(PcxH.ColorMap,Pal,48);
end;
Close(F);
Error:=IORESULT;
{$I+}
end;
Function ValidPcxFile(Filename : string) : Boolean;
Var
MyPcxHeader : PcxHeaderRec;
F : File;
Error : Word;
Begin
{$I-}
Assign(F,Filename);
Reset(F,1);
BlockRead(F,MyPcxHeader,sizeof(MyPcxHeader));
Close(F);
ValidPcxFile:=false;
If MyPcxHeader.Manufacturer = 10 then
begin
ValidPcxFile:=true;
end;
Error:=IORESULT;
{$I+}
end;
Function GetNPcxColors(Var PcxH : PcxHeaderRec) : Word;
begin
GetNPcxColors:=0;
if (PcxH.Bits_Per_Pixel=1) And (pcxH.nplanes=1) then
begin
GetNPcxColors:=2;
end
Else if (PcxH.Bits_Per_Pixel=2) And (pcxH.nplanes=1) then
begin
GetNPcxColors:=4;
end
Else if (PcxH.Bits_Per_Pixel=1) And (pcxH.nplanes=3) then
begin
GetNPcxColors:=8;
end
Else if (PcxH.Bits_Per_Pixel=1) And (pcxH.nplanes=4) then
begin
GetNPcxColors:=16;
end
Else if (PcxH.Bits_Per_Pixel=8) And (pcxH.nplanes=1) then
begin
GetNPcxColors:=256;
end;
end;
Function GetPcxWidth(Var PcxH : PcxHeaderRec) : Word;
begin
GetPcxWidth:=PcxH.Xmax-PcxH.Xmin+1;
end;
Function GetPcxHeight(Var PcxH : PcxHeaderRec) : Word;
begin
GetPcxHeight:=PcxH.Ymax-PcxH.Ymin+1;
end;
Function PcxPaletteExists(Var PcxH : PcxHeaderRec) : Boolean;
begin
PcxPaletteExists:=false;
if PcxH.Version > 4 then
begin
PcxPaletteExists:=true;
end;
end;
Procedure ReadHeader(Var F : File;Var Header : PcxHeaderRec);
begin
{$I-}
BlockRead (F, Header, 128);
Index := 0;
NextByte := MaxBlock;
{$I+}
end;
Function Compressline(Var Uline:UncpLine;Var CLine : CompressedLine;BytesPerLine : Word) : Word;
Var
cp : Word;
count : Word;
repeatNum : Word;
begin
Cp:=0;
Count:=0;
Repeat
repeatNum:=0;
repeat
inc(repeatNum);
until (repeatNum+count=BytesPerLine) OR
(uline[count+repeatNum-1] <> uline[count+repeatNum]) OR (repeatNum>62);
if (repeatNum > 1) then
begin
Cline[Cp]:=repeatNum;
inc(cline[Cp],192);
inc(Cp);
Cline[Cp]:=Uline[count];
inc(count,repeatNum);
inc(Cp);
end
else if (uline[count] > 127) then
begin
Cline[Cp]:=repeatNum;
inc(cline[Cp],192);
inc(Cp);
Cline[Cp]:=uline[count];
inc(count,repeatNum);
inc(Cp);
end
else
begin
Cline[Cp]:=uline[count];
inc(Cp);
inc(count,repeatNum);
end;
Until (count=BytesPerLine) OR (count=BytesPerLine-1);
if count=BytesPerLine-1 then
begin
repeatNum:=1;
if uline[count] > 127 then
begin
Cline[Cp]:=repeatNum;
inc(Cline[Cp],192);
inc(Cp);
Cline[Cp]:=uline[count];
inc(count,repeatNum);
inc(Cp);
end
else
begin
Cline[Cp]:=uline[count];
inc(Cp);
inc(count,repeatNum);
end;
end;
compressline:=Cp;
end;
Procedure ReadByte(Var F : File);
var
NumBlocksRead : Integer;
begin
if NextByte = MaxBlock then
begin
{$I-}
BlockRead (F, BlockData^,MaxBlock,NumBlocksRead);
{$I+}
NextByte:=0;
end;
Data:=BlockData^[NextByte];
Inc(NextByte);
End;
Procedure ReadPCXLine(Var F : File;Var Uline : Uncpline;BytesPerLine : Word);
Var
Count : Word;
begin
If Index <> 0 then
begin
FillChar(Uline [0], Index, data);
end;
While (Index < BytesPerLine) do
begin
ReadByte(F);
If (Data and $C0) = CompressNum then
begin
Count:=(Data AND $3F);
ReadByte(F);
FillChar(Uline [Index],Count,Data);
Inc(Index,Count);
end
else
begin
Uline[Index]:=Data;
Inc(Index);
end;
end;
Dec(Index,BytesPerLine);
end;
Procedure MonoTOsp(Var pPlane : UncpLine;Var splane : uncpLine;
BPL : Word);
var
cb,i,j : Word;
begin
cb:=0;
FillChar(splane,sizeof(splane),0);
For i:=0 to BPL-1 do
begin
For j:=7 downto 0 do
begin
if Biton(j,pplane[i]) then splane[cb+(7-j)]:=1;
end;
inc(cb,8);
end;
end;
Procedure spToMono(Var splane : uncpLine;Var monoPlane : UncpLine;
BPL : Word);
var
cb,i,j : Word;
x : integer;
begin
cb:=0;
x:=0;
FillChar(monoplane,sizeof(monoplane),0);
For i:=0 to BPL-1 do
begin
For j:=7 downto 0 do
begin
//if Biton(j,pplane[i]) then splane[cb+(7-j)]:=1;
if splane[x] = 1 then SetBit(j,1,monoplane[i]);
inc(x);
end;
// monoplane[i]:=254;
inc(cb,8);
end;
end;
Procedure packTOsp(Var pPlane : UncpLine;Var splane : uncpLine;
BPL : Word);
var
cb,i : Word;
begin
cb:=0;
For i:=0 to BPL-1 do
begin
splane[cb]:=pplane[i] shr 6;
splane[cb+1]:=pplane[i] shl 2;
splane[cb+1]:=splane[cb+1] shr 6;
splane[cb+2]:=pplane[i] shl 4;
splane[cb+2]:=splane[cb+2] shr 6;
splane[cb+3]:=pplane[i] shl 6;
splane[cb+3]:=splane[cb+3] shr 6;
inc(cb,4);
end;
end;
Procedure spTOpack(Var splane : uncpLine;var pPlane : UncpLine;
BPL : Word);
var
cb,i : Word;
begin
cb:=0;
For i:=0 to BPL-1 do
begin
pplane[i] := (splane[cb] shl 6) + (splane[cb+1] shl 4) + (splane[cb+2] shl 2) + (splane[cb+3]);
inc(cb,4);
end;
end;
Procedure mpTOsp(Var mPlane : UncpLine;Var splane : uncpLine;
ImgOff2,ImgOff3,ImgOff4 : Word);
Var
i,j : Word;
xpos : Word;
Col : Word;
begin
xpos:=0;
FillChar(splane,SizeOf(sPlane),0);
For i:=0 to ImgOff2-1 do
begin
For j:=7 downto 0 do
begin
Col:=0;
if biton(j,mPlane[i]) then
begin
Inc(Col,1);
end;
if biton(j,mPlane[i+ImgOff2]) then
begin
Inc(Col,2);
end;
if biton(j,mPlane[i+ImgOff3]) then
begin
Inc(Col,4);
end;
if biton(j,mPlane[i+ImgOff4]) then
begin
Inc(Col,8);
end;
Splane[xpos]:=Col;
Inc(xpos);
end;
end;
end;
Procedure spTOmp(Var sPlane : UncpLine;Var mplane : uncpLine;
ImgOff2,ImgOff3,ImgOff4 : Word);
Var
xp,cp,cl : Word;
i,j,x : Word;
begin
xp:=0;
cp:=0;
FillChar(mPlane,SizeOf(mplane),0);
for x:=0 to ImgOff2-1 do
begin
for j:=0 to 7 do
begin
cl:=sPlane[xp+j];
if biton(3,cl) then setbit((7-j),1,mplane[ImgOff4+cp]);
if biton(2,cl) then setbit((7-j),1,mplane[ImgOff3+cp]);
if biton(1,cl) then setbit((7-j),1,mplane[ImgOff2+cp]);
if biton(0,cl) then setbit((7-j),1,mplane[cp]);
end;
inc(cp);
inc(xp,8);
end;
end;
Procedure spTOmp2(Var sPlane : UncpLine;Var mplane : uncpLine;
ImgOff2 : Word);
Var
xp,cp,cl : Word;
i,j,x : Word;
begin
xp:=0;
cp:=0;
FillChar(mPlane,SizeOf(mplane),0);
for x:=0 to ImgOff2-1 do
begin
for j:=0 to 7 do
begin
cl:=sPlane[xp+j];
// if biton(3,cl) then setbit((7-j),1,mplane[ImgOff4+cp]);
// if biton(2,cl) then setbit((7-j),1,mplane[ImgOff3+cp]);
if biton(1,cl) then setbit((7-j),1,mplane[ImgOff2+cp]);
if biton(0,cl) then setbit((7-j),1,mplane[cp]);
end;
inc(cp);
inc(xp,8);
end;
end;
function GetMaxColor : integer;
begin
//GetMaxColor:=15;
GetMaxColor:=RMCoreBase.Palette.GetColorCount -1;
end;
Function ReadPcxImg(x,y,x2,y2,Pal,pm : Word;Filename : string): word;
var
PcxPal : PaletteT;
PcxFile : File;
Header : PcxHeaderRec;
Uline : UncpLine;
BitPlane2 : Word;
BitPlane3 : Word;
BitPlane4 : Word;
BytesPerLine : Word;
i,j,Ln : Word;
myWidth : Word;
myHeight : Word;
Fwidth : Word;
FHeight : Word;
Error : Word;
Colors : Word;
Temp : Byte;
uncomLine : uncpLine;
cr : TRMColorRec;
Begin
ReadPcxImg:=0;
{$I-}
Assign(PcxFile, Filename);
Reset(PcxFile, 1);
BlockRead(PcxFile, Header, 128);
Error:=IORESULT;
If Error <> 0 Then
begin
ReadPcxImg:=Error;
Exit;
end;
Index:=0;
NextByte:=MaxBlock;
Colors:=GetNPcxColors(Header);
FHeight:=GetPcxHeight(Header);
FWidth :=GetPCxWidth(Header);
myWidth:=(x2-x+1);
myHeight:=(y2-y+1);
if Fwidth < myWidth then myWidth:=FWidth;
if FHeight < myHeight then myHeight:=Fheight;
If (Colors<>2) AND (Colors<>4) AND (Colors<>16) AND (Colors<>256) then
begin
ReadPcxImg:=1000;
Close(PcxFile);
Error:=IORESULT;
Exit;
end
Else if (Header.Manufacturer<>10) then
begin
ReadPcxImg:=1000;
Close(PcxFile);
Error:=IORESULT;
Exit;
end;
GetMem(BlockData,SizeOf(BlockArray));
if Colors=2 then
begin
BytesPerLine:=Header.Bytes_per_line_per_plane;
// ShowMessage('Bytes_per_line_per_plane = '+IntToStr(Header.Bytes_per_line_per_plane)+'Bits_per_pixel= '+intTostr( Header.Bits_per_pixel)+' nplanes = '+intTostr( Header.Nplanes));
Header.Bits_per_pixel:=1;
Header.Nplanes:=1;
For Ln:=1 to myHeight do
begin
ReadPCXLine(PcxFile,Uline,BytesPerLine);
Error:=IORESULT;
If Error <> 0 then
begin
ReadPcxImg:=Error;
Close(PcxFile);
Error:=IORESULT;
FreeMem(BlockData,SizeOf(BlockArray));
Exit;
end;
MonoTOsp(Uline,uncomline,BytesPerLine);
For i:=1 to myWidth do
begin
// IconImage[x+i-1,Ln+y-1]:=Uncomline[i-1];
RMCoreBase.PutPixel(x+i-1,Ln+y-1,Uncomline[i-1]);
end;
end;
end
else if Colors=4 then
begin
BytesPerLine:=Header.Bytes_per_line_per_plane;
// ShowMessage('Header.Bytes_per_line_per_plane = '+IntToStr(Header.Bytes_per_line_per_plane));
For Ln:=1 to myHeight do
begin
ReadPCXLine(PcxFile,Uline,BytesPerLine);
Error:=IORESULT;
If Error <> 0 then
begin
ReadPcxImg:=Error;
Close(PcxFile);
Error:=IORESULT;
FreeMem(BlockData,SizeOf(BlockArray));
Exit;
end;
PackTOsp(Uline,uncomline,BytesPerLine);
For i:=1 to myWidth do
begin
// IconImage[x+i-1,Ln+y-1]:=Uncomline[i-1];
RMCoreBase.PutPixel(x+i-1,Ln+y-1,Uncomline[i-1]);
end;
end;
end
else If Colors = 16 then
begin
BytesPerLine:=(Header.Bytes_per_line_per_plane SHL 2);
BitPlane2:=(BytesPerLine SHR 2);
BitPlane3:=(BytesPerLine SHR 1);
BitPlane4:=(BytesPerLine SHR 2) * 3;
For Ln:=1 to myHeight do
begin
ReadPCXLine(PcxFile,Uline,BytesPerLine);
Error:=IORESULT;
If Error <> 0 then
begin
REadPcxImg:=Error;
Close(PcxFile);
Error:=IORESULT;
FreeMem(BlockData,SizeOf(BlockArray));
Exit;
end;
mpTosp(Uline,uncomLine,BitPlane2,BitPlane3,BitPlane4);
For i:=1 to myWidth do
begin
//IconImage[x+i-1,Ln+y-1]:=Uncomline[i-1];
RMCoreBase.PutPixel(x+i-1,Ln+y-1,Uncomline[i-1]);
end;
end;
end
else if (Colors=256) then
begin
BytesPerLine:=Header.Bytes_per_line_per_plane;
For Ln:=1 to myHeight do
begin
ReadPCXLine(PcxFile,Uline,BytesPerLine);
Error:=IORESULT;
If Error <> 0 then
begin
ReadPcxImg:=Error;
Close(PcxFile);
Error:=IORESULT;
FreeMem(BlockData,SizeOf(BlockArray));
Exit;
end;
For i:=1 to myWidth do
begin
//IconImage[x+i-1,Ln+y-1]:=Uline[i-1];
RMCoreBase.PutPixel(x+i-1,Ln+y-1,Uline[i-1]);
end;
end;
If GetMaxColor=15 then
begin
// ReduceTo16;
end;
end;
Close(PcxFile);
FreeMem(BlockData,SizeOf(BlockArray));
{$I+}
GetPcxInfo(filename,Header,PcxPal);
Colors:=GetNPcxColors(Header);
If (Pal=1) and (CanLoadPaletteFile(pm)) and (Colors > 0) then //we do not load palette for mono and cga or if we are select/clip open mode pal=0
begin
if pm=PaletteModeEGA then //if we are in ega palette mode we need to be able to remap rgb color ega64 palette
begin //if not we skip setting that color
for i:=0 to Colors-1 do
begin
cr.r:=PcxPal[i,0];
cr.g:=PcxPal[i,1];
cr.b:=PcxPal[i,2];
MakeRGBToEGACompatible(cr.r,cr.g,cr.b,cr.r,cr.g,cr.b);
RMCoreBase.Palette.SetColor(i,cr);
end;
end
else if isAmigaPaletteMode(pm) then
begin
for i:=0 to Colors-1 do
begin
cr.r:=FourToEightBit(EightToFourBit(PcxPal[i,0]));
cr.g:=FourToEightBit(EightToFourBit(PcxPal[i,1]));
cr.b:=FourToEightBit(EightToFourBit(PcxPal[i,2]));
RMCoreBase.Palette.SetColor(i,cr);
end;
end
else if (pm=PaletteModeVGA) or (pm=PaletteModeVGA256) then
begin
for i:=0 to Colors-1 do
begin
cr.r:=SixToEightBit(EightToSixBit(PcxPal[i,0])); //we bitshift because if palette was saved when PaletteModeXga or PaletteModeXga256
cr.g:=SixToEightBit(EightToSixBit(PcxPal[i,1])); //we will have invalid values
cr.b:=SixToEightBit(EightToSixBit(PcxPal[i,2]));
RMCoreBase.Palette.SetColor(i,cr);
// SetColor(i,cr);
end;
end
else if (pm=PaletteModeXGA) or (pm=PaletteModeXGA256) then
begin
for i:=0 to Colors-1 do
begin
cr.r:=PcxPal[i,0];
cr.g:=PcxPal[i,1];
cr.b:=PcxPal[i,2];
RMCoreBase.Palette.SetColor(i,cr);
// SetColor(i,cr);
end;
end;
end;
end;
//Function BytesPerRow(width : word) : Word;
//begin
// BytesPerRow :=(width+7) div 8;
//end;
Function ImageSize(x,y,x2,y2 : word) : word;
var
width , height : integer;
bpl : integer;
colorcount : integer;
begin
width:=x2-x+1;
height:=y2-y+1;
bpl:=(width+7) div 8;
//bpl:=width;
colorcount:=RMCoreBase.Palette.GetColorCount;
if colorcount = 8 then
begin
colorcount:=16;
end
else if colorcount=32 then
begin
colorcount:=256;
end;
if colorcount= 16 then
begin
ImageSize:=(bpl*4)*height+6;
end
else if colorcount = 256 then
begin
ImageSize:=width + 6;
end
else if colorcount = 4 then
begin
ImageSize:=bpl div 4 + 6;
end
else if colorcount = 2 then
begin
if odd(bpl) then inc(bpl);
ImageSize:=bpl+6;
end;
end;
Function SavePcxImg(x,y,x2,y2 : Word;Filename : String): Word;
var
PcxPal : PaletteT;
PcxFile : File;
Header : PcxHeaderRec;
ULine : UncpLine;
CLine : CompressedLine;
Cbytes : Word;
BitPlane2 : Word;
BitPlane3 : Word;
BitPlane4 : Word;
BytesPerLine : Word;
i,j,Ln : Word;
myWidth : Word;
myHeight : Word;
Error : Word;
NColors : Word;
Temp : Byte;
splane : uncpLine;
Begin
SavePcxImg:=0;
NColors:=GetMaxColor+1;
//NColors:=16;
if NColors=8 then (* promote amiga color modes to next available formats *)
begin
NColors:=16;
end
else if NColors = 32 then
begin
NColors:=256;
end;
If (Ncolors<>16) AND (Ncolors<>256) ANd (NColors<>4) AND (NColors<>2) Then
begin
SavePcxImg:=3;
Exit;
end;
myWidth:=x2-x+1;
myHeight:=y2-y+1;
BytesPerLine:=ImageSize(1,1,myWidth,1)-6;
FillChar(Header,SizeOf(Header),0);
Header.Manufacturer:=10;
Header.Version:=5;
Header.Encoding:=1;
Header.PaletteInfo:=1;
Header.Xmax:=myWidth-1;
Header.Ymax:=myHeight-1;
If NColors=16 Then
begin
Header.Bits_per_pixel:=1;
Header.Nplanes:=4;
Header.Bytes_per_line_per_plane:=BytesPerLine SHR 2;
end
else if NColors=256 then
begin
Header.Bits_per_pixel:=8;
Header.Nplanes:=1;
Header.Bytes_per_line_per_plane:=BytesPerLine;
end
else if NColors = 4 then
begin
Header.Bits_per_pixel:=2;
Header.Nplanes:=1;
Header.Bytes_per_line_per_plane:=(mywidth+3) div 4;
BytesPerLine:=(mywidth+3) div 4;
end
else if NColors = 2 then
begin
BytesPerLine:=(mywidth+7) div 8;
if odd(BytesPerLine) then inc(BytesPerLine);
Header.Bits_per_pixel:=1;
Header.Nplanes:=1;
Header.Bytes_per_line_per_plane:=BytesPerLine;
end;
//GrabPaletteList(PcxPal,GetMaxColor+1);
For i:=0 to GetMaxColor do
begin
PcxPal[i,0]:=RMCoreBase.Palette.GetRed(i);
PcxPal[i,1]:=RMCoreBase.Palette.GetGreen(i);
PcxPal[i,2]:=RMCoreBase.Palette.GetBlue(i);
end;
Move(PcxPal,header.colormap,16*3);
{$I-}
Assign(PcxFile, Filename);
Rewrite(PcxFile, 1);
BlockWrite(PcxFile, Header, 128);
Error:=IORESULT;
If Error <> 0 Then
begin
SavePcxImg:=Error;
Exit;
end;
If NColors=16 then
begin
BitPlane2:=(BytesPerLine SHR 2);
BitPlane3:=(BytesPerLine SHR 1);
BitPlane4:=(BytesPerLine SHR 2) * 3;
end;
If NColors=16 then
begin
For Ln:=y to y2 do
begin
For i:=1 to myWidth do
begin
// splane[i-1]:=IconImage[i+x-1,Ln];
splane[i-1]:= RMCoreBase.GetPixel(i+x-1,Ln);
end;
spTOmp(splane,uline,BitPlane2,BitPlane3,BitPlane4);
Cbytes:=CompressLine(Uline,Cline,BytesPerLine);
BlockWrite(PcxFile,Cline,Cbytes);
Error:=IORESULT;
If Error <> 0 then
begin
SavePcxImg:=Error;
Close(PcxFile);
Error:=IORESULT;
Exit;
end;
end;
end
else if NColors = 256 then
begin
For Ln:=y to y2 do
begin
For i:=1 to myWidth do
begin
// splane[i-1]:=IconImage[i+x-1,Ln];
splane[i-1]:=RMCoreBase.GetPixel(i+x-1,Ln);
end;
Cbytes:=CompressLine(Splane,Cline,BytesPerLine);
BlockWrite(PcxFile,Cline,Cbytes);
Error:=IORESULT;
If Error<>0 then
begin
SavePcxImg:=Error;
Close(PcxFile);
Error:=IORESULT;
Exit;
end;
end;
Temp:=$C;
BlockWrite(PcxFile,Temp,1);
BlockWrite(PcxFile,PcxPal,Sizeof(PcxPal));
end
else if NColors = 4 then
begin
For Ln:=y to y2 do
begin
For i:=1 to myWidth do
begin
// splane[i-1]:=IconImage[i+x-1,Ln];
splane[i-1]:=RMCoreBase.GetPixel(i+x-1,Ln);
end;
spTOpack(splane,uline,bytesperline);
Cbytes:=CompressLine(uline,Cline,BytesPerLine);
BlockWrite(PcxFile,Cline,Cbytes);
Error:=IORESULT;
If Error<>0 then
begin
SavePcxImg:=Error;
Close(PcxFile);
Error:=IORESULT;
Exit;
end;
end;
end
else if NColors = 2 then
begin
For Ln:=y to y2 do
begin
For i:=1 to myWidth do
begin
// splane[i-1]:=IconImage[i+x-1,Ln];
splane[i-1]:=RMCoreBase.GetPixel(i+x-1,Ln);
end;
spTOMono(splane,uline,bytesperline);
Cbytes:=CompressLine(uline,Cline,BytesPerLine);
BlockWrite(PcxFile,Cline,Cbytes);
Error:=IORESULT;
If Error<>0 then
begin
SavePcxImg:=Error;
Close(PcxFile);
Error:=IORESULT;
Exit;
end;
end;
Temp:=$C;
BlockWrite(PcxFile,Temp,1);
BlockWrite(PcxFile,PcxPal,Sizeof(PcxPal));
end;
Close(PcxFile);
{$I+}
end;
begin
end.