summaryrefslogtreecommitdiff
path: root/packages/ptc/src/dos/cga/cga.pp
blob: 9813ea642a1492ad00f2a784ee154bb28b17d7d7 (plain)
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
{
    This file is part of the PTCPas framebuffer library
    Copyright (C) 2001-2010 Nikolay Nikolov (nickysn@users.sourceforge.net)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version
    with the following modification:

    As a special exception, the copyright holders of this library give you
    permission to link this library with independent modules to produce an
    executable, regardless of the license terms of these independent modules,and
    to copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the terms
    and conditions of the license of that module. An independent module is a
    module which is not derived from or based on this library. If you modify
    this library, you may extend this exception to your version of the library,
    but you are not obligated to do so. If you do not wish to do so, delete this
    exception statement from your version.

    This library 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.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
}

unit CGA;

{$MODE objfpc}
{$ASMMODE intel}
{$INLINE on}

interface

procedure CGAText;
procedure CGA320;
procedure CGA640;
procedure CGADump(q: PByte);
procedure CGASetPalette(palette, border: Integer);
procedure CGAPrecalc;
procedure CGAFree;

implementation

uses
  go32, crt;

const
  palette: array[0..15, 0..2] of Byte = (
    ( 0, 0, 0), ( 0, 0,42), ( 0,42, 0), ( 0,42,42), (42, 0, 0), (42, 0,42), (42,21, 0), (42,42,42),
    (21,21,21), (21,21,63), (21,63,21), (21,63,63), (63,21,21), (63,21,63), (63,63,21), (63,63,63));
  cgaback: array[0..3, 0..12] of Integer = (
    (  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 11, 13, 15),
    (  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 12, 14),
    (  0,  1,  3,  5,  7,  8,  9, 10, 11, 12, 13, 14, 15),
    (  0,  1,  2,  4,  6,  8,  9, 10, 11, 12, 13, 14, 15));

type
  Float = Extended;
  TCGAVideoBuffer = array[0..16383] of Byte;
  PCGAPrecalc = ^TCGAPrecalc;
  TCGAPrecalc = array[0..15{r}, 0..15{g}, 0..15{b}, 0..3{y}, 0..3{x}] of Byte;
  PCGAPrecalcError = ^TCGAPrecalcError;
  TCGAPrecalcError = array[0..15{r}, 0..15{g}, 0..15{b}] of Integer;

var
  cgapal: array[0..3] of Integer;
  videobuf: TCGAVideoBuffer;
  precalcbuf: array[0..12, 0..3] of PCGAPrecalc; {3.25mb}
  precalcerror: array[0..12, 0..3] of PCGAPrecalcError; {0.8125mb}
  error: Integer;
  lastpalette, lastback: Integer;

procedure CGA320;

var
  regs: TRealRegs;

begin
  regs.ax := $0004;
  RealIntr($10, regs);
  lastpalette := -1;
  lastback := -1;
end;

procedure CGA640;

var
  regs: TRealRegs;

begin
  regs.ax := $0006;
  RealIntr($10, regs);
end;

procedure CGAText;

var
  regs: TRealRegs;

begin
  regs.ax := $0003;
  RealIntr($10, regs);
end;

procedure CGASetPalette(palette, border: Integer);

var
  regs: TRealRegs;

begin
  if (palette = lastpalette) and (border = lastback) then
    exit;
  lastpalette := palette;
  lastback := border;
  regs.ah := $0B;
  regs.bh := 1;
  regs.bl := palette and 1;
  RealIntr($10, regs);
  if (palette and 2) = 0 then
    Inc(border, 16);
  regs.ah := $0B;
  regs.bh := 0;
  regs.bl := border;
  RealIntr($10, regs);
end;

procedure CGABlitToScreen(p: Pointer); assembler; register;

asm
  mov esi, p
  mov edi, $B8000
  push es
  mov ax, fs
  mov es, ax
  mov ecx, 16192/4
  rep movsd
  pop es
end;

function ColorDistance(r1, g1, b1, r2, g2, b2: Integer): Integer;

var
  RMean: Integer;

begin
//  Result := Sqr(r1 - r2) + Sqr(g1 - g2) + Sqr(b1 - b2);

  { formula taken from: http://www.compuphase.com/cmetric.htm }
  RMean := (r1 + r2) div 2;
  Result := ((512 + RMean)*Sqr(r1 - r2) shr 8) + 4*Sqr(g1 - g2) + ((767 - RMean)*Sqr(b1 - b2) shr 8);
end;

function CGACalc2(r, g, b: Integer; dx, dy: Integer; back, pal: Integer): Integer; inline;

begin
  Result := precalcbuf[back, pal]^[r shr 4, g shr 4, b shr 4, dy, dx];
end;

procedure CGACalc(r, g, b: Integer; var dither, best1, best2: Integer);

var
  I, J: Integer;
  mindist: Float;
  dist: Float;
  r1, g1, b1: Integer;
  tmp: Integer;
{  dither: Integer;} {0-none; 1-50%; 2-25%; 3-12.5%; 4-37.5%}

begin
  r := Round(r*63 / 15);
  g := Round(g*63 / 15);
  b := Round(b*63 / 15);
  mindist := $7FFFFFFF;
  for I := 0 to 3 do
  begin
    dist := ColorDistance(r, g, b, palette[cgapal[I], 0], palette[cgapal[I], 1], palette[cgapal[I], 2]);
    if dist < mindist then
    begin
      mindist := dist;
      best1 := I;
      dither := 0;
    end;
  end;

  for J := 0 to 3 do
  begin
    r1 := palette[cgapal[J], 0];
    g1 := palette[cgapal[J], 1];
    b1 := palette[cgapal[J], 2];
    for I := 0 to 3 do
    begin
      if I = J then
        continue;
      dist := ColorDistance(r, g, b, (palette[cgapal[I], 0] + r1) div 2, (palette[cgapal[I], 1] + g1) div 2, (palette[cgapal[I], 2] + b1) div 2);
      if dist < mindist then
      begin
        mindist := dist;
        best1 := J;
        best2 := I;
        dither := 1;
      end;
      dist := ColorDistance(r, g, b, (palette[cgapal[I], 0] + 3*r1) div 4, (palette[cgapal[I], 1] + 3*g1) div 4, (palette[cgapal[I], 2] + 3*b1) div 4);
      if dist < mindist then
      begin
        mindist := dist;
        best1 := J;
        best2 := I;
        dither := 2;
      end;
      dist := ColorDistance(r, g, b, (palette[cgapal[I], 0] + 7*r1) div 8, (palette[cgapal[I], 1] + 7*g1) div 8, (palette[cgapal[I], 2] + 7*b1) div 8);
      if dist < mindist then
      begin
        mindist := dist;
        best1 := J;
        best2 := I;
        dither := 3;
      end;
      dist := ColorDistance(r, g, b, (3*palette[cgapal[I], 0] + 5*r1) div 8, (3*palette[cgapal[I], 1] + 5*g1) div 8, (3*palette[cgapal[I], 2] + 5*b1) div 8);
      if dist < mindist then
      begin
        mindist := dist;
        best1 := J;
        best2 := I;
        dither := 4;
      end;
    end;
  end;

  error := error + Round(Sqrt(mindist) * {290}40);
  case dither of
    0: best2 := best1;
    1: begin
      if best1 > best2 then
      begin
        tmp := best1;
        best1 := best2;
        best2 := tmp;
      end;
    end;
  end;
end;

function CGACalcError(s: PByte; back, pal: Integer): Integer;

var
  X, Y: Integer;
  r, g, b: Integer;

begin
  Result := 0;
  for Y := 0 to 199 {div 4} do
  begin
    for X := 0 to 319 {div 4} do
    begin
      b := s[0];
      g := s[1];
      r := s[2];
      Inc(Result, precalcerror[back, pal]^[b shr 4, g shr 4, r shr 4]);
      Inc(s, 4 {+ 4 + 4 + 4});
    end;
    {Inc(s, 320*4*3);}
  end;
end;

procedure CGADump2(s, d: PByte; back, pal: Integer);

var
  I: Integer;
  src, dest: PByte;
  X, Y: Integer;
  r1, g1, b1: Integer;
  r2, g2, b2: Integer;
  r3, g3, b3: Integer;
  r4, g4, b4: Integer;

begin
  error := 0;
  src := s;
  dest := d;
  for Y := 0 to 99 do
  begin
    for X := 0 to 79 do
    begin
      b1 := src[0];
      g1 := src[1];
      r1 := src[2];
      b2 := src[4];
      g2 := src[5];
      r2 := src[6];
      b3 := src[8];
      g3 := src[9];
      r3 := src[10];
      b4 := src[12];
      g4 := src[13];
      r4 := src[14];
      dest^ := (CGACalc2(r1, g1, b1, 0, (Y and 1) shl 1, back, pal) shl 6) or
               (CGACalc2(r2, g2, b2, 1, (Y and 1) shl 1, back, pal) shl 4) or
               (CGACalc2(r3, g3, b3, 2, (Y and 1) shl 1, back, pal) shl 2) or
               (CGACalc2(r4, g4, b4, 3, (Y and 1) shl 1, back, pal));

      Inc(src, 4*4);
      Inc(dest);
    end;
    Inc(src, 320*4);
  end;
  src := s + 320*4;
  dest := d + 8192;
  for Y := 0 to 99 do
  begin
    for X := 0 to 79 do
    begin
      b1 := src[0];
      g1 := src[1];
      r1 := src[2];
      b2 := src[4];
      g2 := src[5];
      r2 := src[6];
      b3 := src[8];
      g3 := src[9];
      r3 := src[10];
      b4 := src[12];
      g4 := src[13];
      r4 := src[14];
      dest^ := (CGACalc2(r1, g1, b1, 0, ((Y and 1) shl 1) + 1, back, pal) shl 6) or
               (CGACalc2(r2, g2, b2, 1, ((Y and 1) shl 1) + 1, back, pal) shl 4) or
               (CGACalc2(r3, g3, b3, 2, ((Y and 1) shl 1) + 1, back, pal) shl 2) or
               (CGACalc2(r4, g4, b4, 3, ((Y and 1) shl 1) + 1, back, pal));

      Inc(src, 4*4);
      Inc(dest);
    end;
    Inc(src, 320*4);
  end;
end;

procedure CGADump(q: PByte);

var
  pal, back: Integer;
  bestpal, bestback: Integer;
  besterror: Integer;

begin
  besterror := $7FFFFFFF;
  for pal := 0 to 3 do
  begin
    for back := 0 to 12 do
    begin
      error := CGACalcError(q, back, pal);
      if error < besterror then
      begin
        besterror := error;
        bestpal := pal;
        bestback := back;
      end;
    end;
  end;

  CGADump2(q, videobuf, bestback, bestpal);

  CGASetPalette(bestpal, cgaback[bestpal, bestback]);
  CGABlitToScreen(@videobuf);
end;

procedure CGAPrecalc;

var
  pal, back: Integer;
  r, g, b: Integer;
  x, y: Integer;
  dither: Integer;
  best1, best2: Integer;
  res: Integer;

begin
  Writeln('Precalculating CGA lookup tables, please wait...');
  for pal := 0 to 3 do
  begin
    case pal of
      0: begin
        cgapal[1] := 10;
        cgapal[2] := 12;
        cgapal[3] := 14;
      end;
      1: begin
        cgapal[1] := 11;
        cgapal[2] := 13;
        cgapal[3] := 15;
      end;
      2: begin
        cgapal[1] := 2;
        cgapal[2] := 4;
        cgapal[3] := 6;
      end;
      3: begin
        cgapal[1] := 3;
        cgapal[2] := 5;
        cgapal[3] := 7;
      end;
    end;
    for back := 0 to 12 do
    begin
      if (precalcbuf[back, pal] = nil) and (precalcerror[back, pal] = nil) then
      begin
        new(precalcbuf[back, pal]);
        new(precalcerror[back, pal]);
      end
      else
        continue;

      cgapal[0] := cgaback[pal, back];
      error := 0;
      Write('  (');
      TextAttr := cgapal[0];
      Write('*');
      TextAttr := cgapal[1];
      Write('*');
      TextAttr := cgapal[2];
      Write('*');
      TextAttr := cgapal[3];
      Write('*');
      TextAttr := 7;
      Write(')');
      for r := 0 to 15 do
        for g := 0 to 15 do
          for b := 0 to 15 do
          begin
            error := 0;
            CGACalc(r, g, b, dither, best1, best2);
            precalcerror[back, pal]^[r, g, b] := error;
            for y := 0 to 3 do
              for x := 0 to 3 do
              begin
                case dither of
                  0: res := best1;
                  1: begin
                    if ((x + y) and 1) <> 0 then
                      res := best1
                    else
                      res := best2;
                  end;
                  2: begin
                    if ((x and 1) = 0) and ((y and 1) = 0) then
                      res := best2
                    else
                      res := best1;
                  end;
                  3: begin
                    if (x = y) and ((x and 1) = 0) then
                      res := best2
                    else
                      res := best1;
                  end;
                  4: begin
                    if (((x and 1) = 0) and ((y and 1) = 0)) or (x = y) then
                      res := best2
                    else
                      res := best1;
                  end;
                end;
                precalcbuf[back, pal]^[r, g, b, y, x] := res;
              end;
          end;
    end;
  end;
end;

procedure CGAFree;

var
  pal, back: Integer;

begin
  for pal := 0 to 3 do
  begin
    for back := 0 to 12 do
    begin
      if precalcbuf[back, pal] <> nil then
      begin
        dispose(precalcbuf[back, pal]);
	precalcbuf[back, pal] := nil;
      end;
      
      if precalcerror[back, pal] <> nil then
      begin
        dispose(precalcerror[back, pal]);
	precalcerror[back, pal] := nil;
      end;
    end;
  end;
end;

initialization
  FillChar(precalcbuf, SizeOf(precalcbuf), 0);
  FillChar(precalcerror, SizeOf(precalcerror), 0);

finalization
  CGAFree;

end.