-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDITHER.PAS
97 lines (80 loc) · 2.58 KB
/
DITHER.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
{ DITHER.PAS: Test drive for BAYER.PAS }
{ Compatible: Turbo/TMT/Free Pascal }
{ enable the symbol to below display debugging message }
(* {$define __DEBUG__} *)
{$i include\common.inc} { common definitions }
{$i include\matrix.inc} { generated using BAYER.EXE }
{$i include\bitmap.inc} { bitmap handlers }
const DTH_GAUGE : array[0..9] of string[10] =
('> ',
'=> ',
'==> ',
'===> ',
'====> ',
'=====> ',
'======> ',
'=======> ',
'========> ',
'=========>');
function gauge_text(current, total: uint): string;
begin
gauge_text := DTH_GAUGE[10 * current div total];
end;
function dither_bayer(const bmp: bitmap): bitmap;
var out : bitmap;
x, y, c : uint;
pixel : double;
begin
dither_bayer := nil;
if bmp = nil then exit;
out := bitmap_create(bm_mono, bmp^.w, bmp^.h);
if out = nil then exit;
for y := 0 to bmp^.h-1 do
begin
write('. Dithering image: [', gauge_text(y, bmp^.h), ']'#13);
for x := 0 to bmp^.w-1 do
begin
pixel := bitmap_getpix(bmp, x, y) / 255.0;
if pixel > bayerf(x, y) then
c := 255
else c := 0;
bitmap_setpix(out, x, y, c);
end;
end;
writeln;
dither_bayer := out;
end;
var bmp, out: bitmap;
infile,
outfile : string;
begin
writeln('Bayer dithering demo v1.0a (', COMPILER, ')');
writeln('Coded by Trinh D.D. Nguyen');
writeln;
if paramcount = 0 then error('USAGE', 'dither <graymap> [outfile]');
infile := paramstr(1);
outfile := paramstr(2);
if pos('.', infile) = 0 then infile := infile + '.pgm';
if outfile = '' then outfile := replace_ext(infile, '.pbm');
write('. Loading graymap: ');
bmp := bitmap_load(infile);
if bmp <> nil then
begin
writeln('ok [', bmp^.w, 'x', bmp^.h, '], ',
bmp^.size, ' bytes (', bmp^.size div 1024, ' KB)');
out := dither_bayer(bmp);
if out <> nil then
begin
writeln('. Dithered bitmap: ok [', out^.w, 'x', out^.h, '], ',
out^.size, ' bytes (', out^.size div 1024, ' KB)');
write ('. Saving bitmap : ');
if bitmap_save(outfile, out) then
writeln('ok [', outfile, ']')
else writeln('failed');
bitmap_destroy(out);
end
else writeln('failed');
bitmap_destroy(bmp);
end
else writeln('failed');
end.