]>
git.saurik.com Git - wxWidgets.git/blob - src/tiff/tools/tiffdither.c
3 * Copyright (c) 1988-1997 Sam Leffler
4 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that (i) the above copyright notices and this permission notice appear in
9 * all copies of the software and related documentation, and (ii) the names of
10 * Sam Leffler and Silicon Graphics may not be used in any advertising or
11 * publicity relating to the software without the specific, prior written
12 * permission of Sam Leffler and Silicon Graphics.
14 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
16 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
19 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
20 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
22 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
26 #include "tif_config.h"
42 #define streq(a,b) (strcmp(a,b) == 0)
43 #define strneq(a,b,n) (strncmp(a,b,n) == 0)
45 #define CopyField(tag, v) \
46 if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
52 static void usage(void);
55 * Floyd-Steinberg error propragation with threshold.
56 * This code is stolen from tiffmedian.
59 fsdither(TIFF
* in
, TIFF
* out
)
61 unsigned char *outline
, *inputline
, *inptr
;
62 short *thisline
, *nextline
, *tmpptr
;
63 register unsigned char *outptr
;
64 register short *thisptr
, *nextptr
;
67 int lastline
, lastpixel
;
71 imax
= imagelength
- 1;
72 jmax
= imagewidth
- 1;
73 inputline
= (unsigned char *)_TIFFmalloc(TIFFScanlineSize(in
));
74 thisline
= (short *)_TIFFmalloc(imagewidth
* sizeof (short));
75 nextline
= (short *)_TIFFmalloc(imagewidth
* sizeof (short));
76 outlinesize
= TIFFScanlineSize(out
);
77 outline
= (unsigned char *) _TIFFmalloc(outlinesize
);
82 if (TIFFReadScanline(in
, inputline
, 0, 0) <= 0)
87 for (j
= 0; j
< imagewidth
; ++j
)
88 *nextptr
++ = *inptr
++;
89 for (i
= 1; i
< imagelength
; ++i
) {
93 lastline
= (i
== imax
);
94 if (TIFFReadScanline(in
, inputline
, i
, 0) <= 0)
98 for (j
= 0; j
< imagewidth
; ++j
)
99 *nextptr
++ = *inptr
++;
102 _TIFFmemset(outptr
= outline
, 0, outlinesize
);
104 for (j
= 0; j
< imagewidth
; ++j
) {
107 lastpixel
= (j
== jmax
);
123 thisptr
[0] += v
* 7 / 16;
126 nextptr
[-1] += v
* 3 / 16;
127 *nextptr
++ += v
* 5 / 16;
129 nextptr
[0] += v
/ 16;
132 if (TIFFWriteScanline(out
, outline
, i
-1, 0) < 0)
136 _TIFFfree(inputline
);
142 static uint16 compression
= COMPRESSION_PACKBITS
;
143 static uint16 predictor
= 0;
144 static uint32 group3options
= 0;
147 processG3Options(char* cp
)
149 if ((cp
= strchr(cp
, ':'))) {
152 if (strneq(cp
, "1d", 2))
153 group3options
&= ~GROUP3OPT_2DENCODING
;
154 else if (strneq(cp
, "2d", 2))
155 group3options
|= GROUP3OPT_2DENCODING
;
156 else if (strneq(cp
, "fill", 4))
157 group3options
|= GROUP3OPT_FILLBITS
;
160 } while ((cp
= strchr(cp
, ':')));
165 processCompressOptions(char* opt
)
167 if (streq(opt
, "none"))
168 compression
= COMPRESSION_NONE
;
169 else if (streq(opt
, "packbits"))
170 compression
= COMPRESSION_PACKBITS
;
171 else if (strneq(opt
, "g3", 2)) {
172 processG3Options(opt
);
173 compression
= COMPRESSION_CCITTFAX3
;
174 } else if (streq(opt
, "g4"))
175 compression
= COMPRESSION_CCITTFAX4
;
176 else if (strneq(opt
, "lzw", 3)) {
177 char* cp
= strchr(opt
, ':');
179 predictor
= atoi(cp
+1);
180 compression
= COMPRESSION_LZW
;
181 } else if (strneq(opt
, "zip", 3)) {
182 char* cp
= strchr(opt
, ':');
184 predictor
= atoi(cp
+1);
185 compression
= COMPRESSION_DEFLATE
;
192 main(int argc
, char* argv
[])
195 uint16 samplesperpixel
, bitspersample
= 1, shortv
;
198 uint32 rowsperstrip
= (uint32
) -1;
199 uint16 fillorder
= 0;
204 while ((c
= getopt(argc
, argv
, "c:f:r:t:")) != -1)
206 case 'c': /* compression scheme */
207 if (!processCompressOptions(optarg
))
210 case 'f': /* fill order */
211 if (streq(optarg
, "lsb2msb"))
212 fillorder
= FILLORDER_LSB2MSB
;
213 else if (streq(optarg
, "msb2lsb"))
214 fillorder
= FILLORDER_MSB2LSB
;
218 case 'r': /* rows/strip */
219 rowsperstrip
= atoi(optarg
);
222 threshold
= atoi(optarg
);
225 else if (threshold
> 255)
232 if (argc
- optind
< 2)
234 in
= TIFFOpen(argv
[optind
], "r");
237 TIFFGetField(in
, TIFFTAG_SAMPLESPERPIXEL
, &samplesperpixel
);
238 if (samplesperpixel
!= 1) {
239 fprintf(stderr
, "%s: Not a b&w image.\n", argv
[0]);
242 TIFFGetField(in
, TIFFTAG_BITSPERSAMPLE
, &bitspersample
);
243 if (bitspersample
!= 8) {
245 " %s: Sorry, only handle 8-bit samples.\n", argv
[0]);
248 out
= TIFFOpen(argv
[optind
+1], "w");
251 CopyField(TIFFTAG_IMAGEWIDTH
, imagewidth
);
252 TIFFGetField(in
, TIFFTAG_IMAGELENGTH
, &imagelength
);
253 TIFFSetField(out
, TIFFTAG_IMAGELENGTH
, imagelength
-1);
254 TIFFSetField(out
, TIFFTAG_BITSPERSAMPLE
, 1);
255 TIFFSetField(out
, TIFFTAG_SAMPLESPERPIXEL
, 1);
256 TIFFSetField(out
, TIFFTAG_PLANARCONFIG
, PLANARCONFIG_CONTIG
);
257 TIFFSetField(out
, TIFFTAG_COMPRESSION
, compression
);
259 TIFFSetField(out
, TIFFTAG_FILLORDER
, fillorder
);
261 CopyField(TIFFTAG_FILLORDER
, shortv
);
262 sprintf(thing
, "Dithered B&W version of %s", argv
[optind
]);
263 TIFFSetField(out
, TIFFTAG_IMAGEDESCRIPTION
, thing
);
264 CopyField(TIFFTAG_PHOTOMETRIC
, shortv
);
265 CopyField(TIFFTAG_ORIENTATION
, shortv
);
266 CopyField(TIFFTAG_XRESOLUTION
, floatv
);
267 CopyField(TIFFTAG_YRESOLUTION
, floatv
);
268 CopyField(TIFFTAG_RESOLUTIONUNIT
, shortv
);
269 rowsperstrip
= TIFFDefaultStripSize(out
, rowsperstrip
);
270 TIFFSetField(out
, TIFFTAG_ROWSPERSTRIP
, rowsperstrip
);
271 switch (compression
) {
272 case COMPRESSION_CCITTFAX3
:
273 TIFFSetField(out
, TIFFTAG_GROUP3OPTIONS
, group3options
);
275 case COMPRESSION_LZW
:
276 case COMPRESSION_DEFLATE
:
278 TIFFSetField(out
, TIFFTAG_PREDICTOR
, predictor
);
288 "usage: tiffdither [options] input.tif output.tif",
289 "where options are:",
290 " -r # make each strip have no more than # rows",
291 " -f lsb2msb force lsb-to-msb FillOrder for output",
292 " -f msb2lsb force msb-to-lsb FillOrder for output",
293 " -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding",
294 " -c zip[:opts] compress output with deflate encoding",
295 " -c packbits compress output with packbits encoding",
296 " -c g3[:opts] compress output with CCITT Group 3 encoding",
297 " -c g4 compress output with CCITT Group 4 encoding",
298 " -c none use no compression algorithm on output",
301 " 1d use default CCITT Group 3 1D-encoding",
302 " 2d use optional CCITT Group 3 2D-encoding",
303 " fill byte-align EOL codes",
304 "For example, -c g3:2d:fill to get G3-2D-encoded data with byte-aligned EOLs",
306 "LZW and deflate options:",
307 " # set predictor value",
308 "For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
319 fprintf(stderr
, "%s\n\n", TIFFGetVersion());
320 for (i
= 0; stuff
[i
] != NULL
; i
++)
321 fprintf(stderr
, "%s\n", stuff
[i
]);
325 /* vim: set ts=8 sts=8 sw=8 noet: */