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
|
/* pbmreduce.c - read a portable bitmap and reduce it N times
**
** Copyright (C) 1989 by Jef Poskanzer.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation. This software is provided "as is" without express or
** implied warranty.
*/
#include "pbm.h"
#include "mallocvar.h"
int
main( argc, argv )
int argc;
char* argv[];
{
FILE* ifp;
register bit** bitslice;
register bit* newbitrow;
register bit* nbP;
int argn, n, rows, cols, format, newrows, newcols;
int row, col, limitcol, subrow, subcol, count, direction;
const char* const usage = "[-floyd|-fs | -threshold] [-value <val>] N [pbmfile]";
int halftone;
#define QT_FS 1
#define QT_THRESH 2
#define SCALE 1024
#define HALFSCALE 512
long threshval, sum;
long* thiserr; /* used for Floyd-Steinberg stuff */
long* nexterr; /* used for Floyd-Steinberg stuff */
long* temperr;
pbm_init( &argc, argv );
argn = 1;
halftone = QT_FS;
threshval = HALFSCALE;
while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
{
if ( pm_keymatch( argv[argn], "-fs", 2 ) ||
pm_keymatch( argv[argn], "-floyd", 2 ) )
halftone = QT_FS;
else if ( pm_keymatch( argv[argn], "-threshold", 2 ) )
halftone = QT_THRESH;
else if ( pm_keymatch( argv[argn], "-value", 2 ) )
{
float f;
++argn;
if ( argn == argc || sscanf( argv[argn], "%f", &f ) != 1 ||
f < 0.0 || f > 1.0 )
pm_usage( usage );
threshval = f * SCALE;
}
else
pm_usage( usage );
++argn;
}
if ( argn == argc )
pm_usage( usage );
if ( sscanf( argv[argn], "%d", &n ) != 1 )
pm_usage( usage );
if ( n < 2 )
pm_error( "N must be greater than 1" );
++argn;
if ( argn == argc )
ifp = stdin;
else
{
ifp = pm_openr( argv[argn] );
++argn;
}
if ( argn != argc )
pm_usage( usage );
pbm_readpbminit( ifp, &cols, &rows, &format );
bitslice = pbm_allocarray( cols, n );
newrows = rows / n;
newcols = cols / n;
pbm_writepbminit( stdout, newcols, newrows, 0 );
newbitrow = pbm_allocrow( newcols );
if ( halftone == QT_FS ) {
/* Initialize Floyd-Steinberg. */
MALLOCARRAY(thiserr, newcols + 2);
MALLOCARRAY(nexterr, newcols + 2);
if ( thiserr == NULL || nexterr == NULL )
pm_error( "out of memory" );
srand( (int) ( time( 0 ) ^ getpid( ) ) );
for ( col = 0; col < newcols + 2; ++col )
thiserr[col] = ( rand( ) % SCALE - HALFSCALE ) / 4;
/* (random errors in [-SCALE/8 .. SCALE/8]) */
} else {
/* These variables are meaningless in this case, and the values
should never be used.
*/
thiserr = NULL;
nexterr = NULL;
}
direction = 1;
for ( row = 0; row < newrows; ++row )
{
for ( subrow = 0; subrow < n; ++subrow )
pbm_readpbmrow( ifp, bitslice[subrow], cols, format );
if ( halftone == QT_FS )
for ( col = 0; col < newcols + 2; ++col )
nexterr[col] = 0;
if ( direction )
{
col = 0;
limitcol = newcols;
nbP = newbitrow;
}
else
{
col = newcols - 1;
limitcol = -1;
nbP = &(newbitrow[col]);
}
do
{
sum = 0;
count = 0;
for ( subrow = 0; subrow < n; ++subrow )
for ( subcol = 0; subcol < n; ++subcol )
if ( row * n + subrow < rows && col * n + subcol < cols )
{
count += 1;
if ( bitslice[subrow][col * n + subcol] == PBM_WHITE )
sum += 1;
}
sum = ( sum * SCALE ) / count;
if ( halftone == QT_FS )
sum += thiserr[col + 1];
if ( sum >= threshval )
{
*nbP = PBM_WHITE;
if ( halftone == QT_FS )
sum = sum - threshval - HALFSCALE;
}
else
*nbP = PBM_BLACK;
if ( halftone == QT_FS )
{
if ( direction )
{
thiserr[col + 2] += ( sum * 7 ) / 16;
nexterr[col ] += ( sum * 3 ) / 16;
nexterr[col + 1] += ( sum * 5 ) / 16;
nexterr[col + 2] += ( sum ) / 16;
}
else
{
thiserr[col ] += ( sum * 7 ) / 16;
nexterr[col + 2] += ( sum * 3 ) / 16;
nexterr[col + 1] += ( sum * 5 ) / 16;
nexterr[col ] += ( sum ) / 16;
}
}
if ( direction )
{
++col;
++nbP;
}
else
{
--col;
--nbP;
}
}
while ( col != limitcol );
pbm_writepbmrow( stdout, newbitrow, newcols, 0 );
if ( halftone == QT_FS )
{
temperr = thiserr;
thiserr = nexterr;
nexterr = temperr;
direction = ! direction;
}
}
pm_close( ifp );
pm_close( stdout );
exit( 0 );
}
|