-
Notifications
You must be signed in to change notification settings - Fork 0
/
PALETTES.PAS
608 lines (568 loc) · 15.1 KB
/
PALETTES.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
{$I COMPILER.INC}
unit Palettes;
interface
uses
AplObj,
AplTypes,
AplUtils,
Compress,
Streams,
AplConst,
Lists;
type
PRgbPalette = ^TRgbPalette;
PRgbColor = ^TRgbColor;
PRgbColors = ^TRgbColors;
TStandardColor =
(
scEga,
scWhite,
scRed,
scGreen,
scBlue,
scCyan,
scYellow,
scMagenta,
scOrange,
scBrown,
scSpringGreen,
scAzure,
scViolet,
scFleshTone
);
TColor = record
Color: TStandardColor;
Shade: byte;
end;
TPaletteFormat =
(
pfApl,
pfJasc,
pfGimp
);
TRgbColor = record
Red: byte;
Green: byte;
Blue: byte;
end;
TRgbColors = array[0..MaxVarSize div SizeOf(TRgbColor) - 1] of TRgbColor;
TRgbPalette = object(TStatusObject)
private
FColors: PRgbColors;
FColorCount: word;
FFormat: TPaletteFormat;
FCompression: TAplCompression;
FCompressionLevel: TCompressionLevel;
procedure LoadPalettesette(AStream: PStream);
procedure LoadJascPalette(AStream: PStream);
procedure LoadGimpPalette(AStream: PStream);
procedure SavePalettesette(AStream: PStream);
procedure SaveJascPalette(AStream: PStream);
procedure SaveGimpPalette(AStream: PStream);
function ReadLine(AStream: PStream): string;
procedure WriteLine(AStream: PStream; const ALine: string);
public
constructor CreateFromFile(AFilename: string; AFormat: TPaletteFormat);
constructor CreateFromStream(AStream: PStream; AFormat: TPaletteFormat);
constructor CreateVgaStandard;
function Colors: PRgbColors;
function Format: TPaletteFormat;
function GetPaletteDataVga: PRgbColors;
function ColorCount: word;
procedure SetFormat(AFormat: TPaletteFormat);
procedure SetPaletteData(AData: PRgbColors);
procedure SetColor(AIndex: word; var AColor: TRgbColor);
procedure SaveToFile(const AFilename: string);
procedure SaveToStream(AStream: PStream);
procedure LoadFromFile(AFilename: string);
procedure LoadFromStream(AStream: PStream);
procedure SetCompression(ACompression: TAplCompression;
ACompressionLevel: TCompressionLevel);
procedure Init; virtual;
destructor Free; virtual;
end;
const
egaBlack = 0;
egaBlue = 1;
egaGreen = 2;
egaCyan = 3;
egaRed = 4;
egaMagenta = 5;
egaBrown = 6;
egaLightGray = 7;
egaDarkGray = 8;
egaLightBlue = 9;
egaLightGreen = 10;
egaLightCyan = 11;
egaLightRed = 12;
egaLightMagenta = 13;
egaYellow = 14;
egaWhite = 15;
rgbEgaBlack: TRgbColor = (Red: $00; Green: $00; Blue: $00);
rgbEgaBlue: TRgbColor = (Red: $00; Green: $00; Blue: $AA);
rgbEgaGreen: TRgbColor = (Red: $00; Green: $AA; Blue: $00);
rgbEgaCyan: TRgbColor = (Red: $00; Green: $AA; Blue: $AA);
rgbEgaRed: TRgbColor = (Red: $AA; Green: $00; Blue: $00);
rgbEgaMagenta: TRgbColor = (Red: $AA; Green: $00; Blue: $AA);
rgbEgaBrown: TRgbColor = (Red: $AA; Green: $55; Blue: $00);
rgbEgaLightGray: TRgbColor = (Red: $AA; Green: $AA; Blue: $AA);
rgbEgaDarkGray: TRgbColor = (Red: $55; Green: $55; Blue: $55);
rgbEgaLightBlue: TRgbColor = (Red: $55; Green: $55; Blue: $FF);
rgbEgaLightGreen: TRgbColor = (Red: $55; Green: $FF; Blue: $55);
rgbEgaLightCyan: TRgbColor = (Red: $55; Green: $FF; Blue: $FF);
rgbEgaLightRed: TRgbColor = (Red: $FF; Green: $55; Blue: $55);
rgbEgaLightMagenta: TRgbColor = (Red: $FF; Green: $55; Blue: $FF);
rgbEgaYellow: TRgbColor = (Red: $FF; Green: $FF; Blue: $55);
rgbEgaWhite: TRgbColor = (Red: $FF; Green: $FF; Blue: $FF);
MinIntensity: byte = 0;
MaxIntensity: byte = 15;
Intensity50: byte = 7;
Intensity25: byte = 3;
Intensity75: byte = 11;
PaletteAnimationStart: byte = 224;
PaletteAnimationEnd: byte = 254;
TransparentColor: byte = 255;
implementation
procedure TRgbPalette.Init;
begin
inherited Init;
FColors := nil;
FColorCount := 0;
FFormat := pfApl;
FCompression := acLzw;
FCompressionLevel := clHigh;
end;
constructor TRgbPalette.CreateVgaStandard;
var
colorIndex: TStandardColor;
shadeIndex, shade: integer;
index: integer;
color: TRgbColor;
begin
inherited Create;
if Assigned(FColors) then
FreeMem(FColors, FColorCount * SizeOf(TRgbColor));
FColorCount := 256;
GetMem(FColors, FColorCount * SizeOf(TRgbColor));
if NilPtr(FColors, ecNotEnoughMemory) then
Exit;
{ Standard EGA colors }
FColors^[0] := rgbEgaBlack;
FColors^[1] := rgbEgaBlue;
FColors^[2] := rgbEgaGreen;
FColors^[3] := rgbEgaCyan;
FColors^[4] := rgbEgaRed;
FColors^[5] := rgbEgaMagenta;
FColors^[6] := rgbEgaBrown;
FColors^[7] := rgbEgaLightGray;
FColors^[8] := rgbEgaDarkGray;
FColors^[9] := rgbEgaLightBlue;
FColors^[10] := rgbEgaLightGreen;
FColors^[11] := rgbEgaLightCyan;
FColors^[12] := rgbEgaLightRed;
FColors^[13] := rgbEgaLightMagenta;
FColors^[14] := rgbEgaYellow;
FColors^[15] := rgbEgaWhite;
index := 16;
for colorIndex := scWhite to High(TStandardColor) do begin
for shadeIndex := 1 to 16 do begin
shade := shadeIndex * 16 - 1;
case colorIndex of
scWhite: begin
color.Red := shade;
color.Green := shade;
color.Blue := shade;
end;
scRed: begin
color.Red := shade;
color.Green := 0;
color.Blue := 0;
end;
scGreen: begin
color.Red := 0;
color.Green := shade;
color.Blue := 0;
end;
scBlue: begin
color.Red := 0;
color.Green := 0;
color.Blue := shade;
end;
scYellow: begin
color.Red := shade;
color.Green := shade;
color.Blue := 0;
end;
scMagenta: begin
color.Red := shade;
color.Green := 0;
color.Blue := shade;
end;
scCyan: begin
color.Red := 0;
color.Green := shade;
color.Blue := shade;
end;
scOrange: begin
color.Red := shade;
color.Green := shade div 2;
color.Blue := 0;
end;
scBrown: begin
color.Red := Round(shade / 1.7);
color.Green := shade div 2;
color.Blue := 0;
end;
scSpringGreen: begin
color.Red := shade div 2;
color.Green := shade;
color.Blue := shade div 2;
end;
scAzure: begin
color.Red := shade div 3;
color.Green := shade div 3;
color.Blue := shade;
end;
scViolet: begin
color.Red := shade div 2;
color.Green := 0;
color.Blue := shade;
end;
scFleshTone: begin
color.Red := 215 - Round((shadeIndex / 18) * 115);
color.Green := 190 - Round((shadeIndex / 18) * 135);
color.Blue := 140 - Round((shadeIndex / 18) * 135);
end;
end;
FColors^[index] := color;
Inc(index);
end;
end;
FillChar(color, SizeOf(TRgbColor), 0);
while index < 256 do begin
FColors^[index] := color;
Inc(index);
end;
end;
constructor TRgbPalette.CreateFromFile(AFilename: string; AFormat: TPaletteFormat);
var
stream: PFileStream;
begin
inherited Create;
FFormat := AFormat;
stream := New(PFileStream, Create(AFilename, fmRead));
if NilPtr(stream, ecNotEnoughMemory) then
exit;
LoadFromStream(stream);
CheckReraise(stream);
FreeAndNil(stream);
end;
constructor TRgbPalette.CreateFromStream(AStream: PStream; AFormat: TPaletteFormat);
begin
inherited Create;
FFormat := AFormat;
LoadFromStream(AStream);
CheckReraise(AStream);
end;
procedure TRgbPalette.SetCompression(ACompression: TAplCompression;
ACompressionLevel: TCompressionLevel);
begin
FCompression := ACompression;
FCompressionLevel := ACompressionLevel;
end;
function TRgbPalette.Colors: PRgbColors;
begin
Colors := FColors;
end;
function TRgbPalette.Format: TPaletteFormat;
begin
Format := FFormat;
end;
procedure TRgbPalette.SetPaletteData(AData: PRgbColors);
begin
if Assigned(FColors) and (FColorCount > 0) then
FreeMem(FColors, FColorCount * SizeOf(TRgbColor));
FColors := AData;
end;
procedure TRgbPalette.SetColor(AIndex: word; var AColor: TRgbColor);
begin
if (not Assigned(FColors)) or (AIndex >= FColorCount) then
exit;
FColors^[AIndex] := AColor;
end;
procedure TRgbPalette.WriteLine(AStream: PStream; const ALine: string);
var
buffer: string;
begin
buffer := ALine + #13#10;
AStream^.Write(@buffer[1], Length(buffer));
if CheckReraise(AStream) then
exit;
end;
function TRgbPalette.ReadLine(AStream: PStream): string;
var
buffer: string;
ch: char;
begin
buffer := '';
while not AStream^.EndOfStream do begin
ch := Char(AStream^.ReadByte);
if AStream^.HasException then
Raise(AStream^.Exception^.Code);
if ch = #13 then
continue;
if ch = #10 then
break;
buffer := buffer + ch;
end;
ReadLine := buffer;
end;
procedure TRgbPalette.LoadJascPalette(AStream: PStream);
var
line: string;
color: TRgbColor;
count: word;
index: word;
begin
line := ReadLine(AStream);
if CheckReraise(AStream) then
exit;
if line <> 'JASC-PAL' then begin
Raise(ecInvalidPalette);
exit;
end;
line := ReadLine(AStream);
if CheckReraise(AStream) then
exit;
if line <> '0100' then begin
Raise(ecInvalidPalette);
exit;
end;
line := ReadLine(AStream);
if CheckReraise(AStream) then
exit;
count := StrToInt(line, 0);
if CheckReraise(AStream) then
exit;
GetMem(FColors, count * SizeOf(TRgbColor));
if NilPtr(FColors, ecNotEnoughMemory) then
exit;
FColorCount := count;
index := 0;
while not AStream^.EndOfStream do begin
line := ReadLine(AStream);
if CheckReraise(AStream) then
exit;
{// Change this to not read padding for each color component}
color.Red := StrToInt(Trim(Copy(line, 1, 3)), 0);
color.Green := StrToInt(Trim(Copy(line, 5, 3)), 0);
color.Blue := StrToInt(Trim(Copy(line, 9, 3)), 0);
FColors^[index] := color;
if index > count then
break;
Inc(index);
end;
end;
procedure TRgbPalette.LoadGimpPalette(AStream: PStream);
var
line: string;
color: TRgbColor;
comment: boolean;
count: word;
index: word;
pos: longint;
begin
count := 0;
pos := AStream^.Position;
while not AStream^.EndOfStream do begin
line := ReadLine(AStream);
if StartsWith(line, '#') then
continue;
Inc(count);
end;
AStream^.Seek(pos, soFromBeginning);
GetMem(FColors, count * SizeOf(TRgbColor));
if NilPtr(FColors, ecNotEnoughMemory) then
exit;
index := 0;
FColorCount := count;
while not AStream^.EndOfStream do begin
line := ReadLine(AStream);
if StartsWith(line, '#') then
continue;
color.Red := StrToInt(Trim(Copy(line, 1, 3)), 0);
color.Green := StrToInt(Trim(Copy(line, 5, 3)), 0);
color.Blue := StrToInt(Trim(Copy(line, 9, 3)), 0);
FColors^[index] := color;
Inc(index);
end;
end;
procedure TRgbPalette.LoadPalettesette(AStream: PStream);
var
count: word;
compressor: PCompressor;
dest: PMemoryStream;
size: word;
begin
count := AStream^.ReadWord;
if CheckReraise(AStream) then
exit;
size := count * SizeOf(TRgbColor);
GetMem(FColors, size);
if NilPtr(FColors, ecNotEnoughMemory) then
exit;
FColorCount := count;
case FCompression of
acLzw: begin
compressor := TCompression.GetCompressor(FCompression, FCompressionLevel);
if NilPtr(compressor, ecNotEnoughMemory) then
exit;
dest := New(PMemoryStream, CreateBuffer(FColors, size));
compressor^.DecompressStream(AStream, dest, count * SizeOf(TRgbColor));
CheckReraise(dest);
FreeAndNil(compressor);
FreeAndNil(dest);
end;
else
AStream^.Read(FColors, size);
end;
end;
procedure TRgbPalette.SavePalettesette(AStream: PStream);
var
compressor: PCompressor;
source: PMemoryStream;
size: word;
begin
AStream^.WriteWord(FColorCount);
if CheckReraise(AStream) then
exit;
size := FColorCount * SizeOf(TRgbColor);
case FCompression of
acLzw: begin
compressor := TCompression.GetCompressor(FCompression, FCompressionLevel);
if NilPtr(compressor, ecNotEnoughMemory) then
exit;
source := New(PMemoryStream, CreateBuffer(FColors, size));
compressor^.CompressStream(source, AStream, size);
CheckReraise(source);
FreeAndNil(compressor);
FreeAndNil(source);
end;
else
AStream^.Write(FColors, size);
end;
end;
procedure TRgbPalette.SaveJascPalette(AStream: PStream);
var
index: word;
color: TRgbColor;
begin
WriteLine(AStream, 'JASC-PAL');
WriteLine(AStream, '0100');
WriteLine(AStream, IntToStr(FColorCount));
for index := 0 to FColorCount - 1 do begin
color := FColors^[index];
WriteLine(AStream,
IntToStr(color.Red) + ' '
+ IntToStr(color.Green) + ' '
+ IntToStr(color.Blue)
);
end;
end;
procedure TRgbPalette.SaveGimpPalette(AStream: PStream);
var
index: integer;
color: TRgbColor;
begin
WriteLine(aStream, 'GIMP Palette');
for index := 0 to FColorCount - 1 do begin
color := FColors^[index];
WriteLine(AStream,
LeftPad(IntToStr(color.Red), 3, ' ') + ' '
+ LeftPad(IntToStr(color.Green), 3, ' ') + ' '
+ LeftPad(IntToStr(color.Blue), 3, ' ') + ' '
+ ' Untitled'
);
end;
end;
procedure TRgbPalette.LoadFromStream(AStream: PStream);
begin
if Assigned(FColors) and (FColorCount > 0) then
FreeMem(FColors, FColorCount * SizeOf(TRgbColor));
FColorCount := 0;
FColors := nil;
case FFormat of
pfApl: LoadPalettesette(AStream);
pfJasc: LoadJascPalette(AStream);
pfGimp: LoadGimpPalette(AStream);
end;
CheckReraise(AStream);
end;
procedure TRgbPalette.LoadFromFile(AFilename: string);
var
stream: PFileStream;
begin
stream := New(PFileStream, Create(AFilename, fmRead));
if NilPtr(stream, ecNotEnoughMemory) then
exit;
LoadFromStream(stream);
CheckReraise(stream);
FreeAndNil(stream);
end;
procedure TRgbPalette.SetFormat(AFormat: TPaletteFormat);
begin
FFormat := AFormat;
end;
procedure TRgbPalette.SaveToFile(const AFilename: string);
var
stream: PFileStream;
begin
stream := New(PFileStream, Create(AFilename, fmWrite));
if NilPtr(stream, ecNotEnoughMemory) then
exit;
SaveToStream(stream);
CheckReraise(stream);
FreeAndNil(stream);
end;
procedure TRgbPalette.SaveToStream(AStream: PStream);
begin
case FFormat of
pfApl: SavePalettesette(AStream);
pfJasc: SaveJascPalette(AStream);
pfGimp: SaveGimpPalette(AStream);
end;
CheckReraise(AStream);
end;
function TRgbPalette.GetPaletteDataVga: PRgbColors;
var
index: integer;
result: PRgbColors;
color: TRgbColor;
begin
GetPaletteDataVga := nil;
if (not Assigned(FColors)) or (FColorCount <> 256) then
exit;
GetMem(result, 256 * SizeOf(TRgbColor));
if NilPtr(result, ecNotEnoughMemory) then
exit;
for index := 0 to 255 do begin
color := FColors^[index];
color.Red := color.Red shr 2;
color.Green := color.Green shr 2;
color.Blue := color.Blue shr 2;
result^[index] := color;
end;
GetPaletteDataVga := result;
end;
function TRgbPalette.ColorCount: word;
begin
ColorCount := FColorCount;
end;
destructor TRgbPalette.Free;
begin
if Assigned(FColors) and (FColorCount > 0) then
FreeMem(FColors, FColorCount * SizeOf(TRgbColor));
inherited Free;
end;
begin
end.