| 1 |
/* |
| 2 |
* Copyright (c) 1988-1997 Sam Leffler |
| 3 |
* Copyright (c) 1991-1997 Silicon Graphics, Inc. |
| 4 |
* |
| 5 |
* Permission to use, copy, modify, distribute, and sell this software and |
| 6 |
* its documentation for any purpose is hereby granted without fee, provided |
| 7 |
* that (i) the above copyright notices and this permission notice appear in |
| 8 |
* all copies of the software and related documentation, and (ii) the names of |
| 9 |
* Sam Leffler and Silicon Graphics may not be used in any advertising or |
| 10 |
* publicity relating to the software without the specific, prior written |
| 11 |
* permission of Sam Leffler and Silicon Graphics. |
| 12 |
* |
| 13 |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 14 |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 15 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 16 |
* |
| 17 |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
| 18 |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 19 |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 20 |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 21 |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 22 |
* OF THIS SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "libport.h" |
| 26 |
#include "tif_config.h" |
| 27 |
|
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
|
| 32 |
#ifdef HAVE_UNISTD_H |
| 33 |
#include <unistd.h> |
| 34 |
#endif |
| 35 |
|
| 36 |
#include "tiffio.h" |
| 37 |
#include "tiffiop.h" |
| 38 |
|
| 39 |
#ifndef EXIT_SUCCESS |
| 40 |
#define EXIT_SUCCESS 0 |
| 41 |
#endif |
| 42 |
#ifndef EXIT_FAILURE |
| 43 |
#define EXIT_FAILURE 1 |
| 44 |
#endif |
| 45 |
|
| 46 |
#define streq(a, b) (strcmp(a, b) == 0) |
| 47 |
#define strneq(a, b, n) (strncmp(a, b, n) == 0) |
| 48 |
|
| 49 |
#define CopyField(tag, v) \ |
| 50 |
if (TIFFGetField(in, tag, &v)) \ |
| 51 |
TIFFSetField(out, tag, v) |
| 52 |
|
| 53 |
uint32_t imagewidth; |
| 54 |
uint32_t imagelength; |
| 55 |
int threshold = 128; |
| 56 |
|
| 57 |
static void usage(int code); |
| 58 |
|
| 59 |
/* |
| 60 |
* Floyd-Steinberg error propragation with threshold. |
| 61 |
* This code is stolen from tiffmedian. |
| 62 |
*/ |
| 63 |
static int fsdither(TIFF *in, TIFF *out) |
| 64 |
{ |
| 65 |
unsigned char *outline, *inputline, *inptr; |
| 66 |
short *thisline, *nextline, *tmpptr; |
| 67 |
register unsigned char *outptr; |
| 68 |
register short *thisptr, *nextptr; |
| 69 |
register uint32_t i, j; |
| 70 |
uint32_t imax, jmax; |
| 71 |
int lastline, lastpixel; |
| 72 |
int bit; |
| 73 |
tsize_t outlinesize; |
| 74 |
int errcode = 0; |
| 75 |
|
| 76 |
imax = imagelength - 1; |
| 77 |
jmax = imagewidth - 1; |
| 78 |
inputline = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(in)); |
| 79 |
thisline = (short *)_TIFFmalloc( |
| 80 |
TIFFSafeMultiply(tmsize_t, imagewidth, sizeof(short))); |
| 81 |
nextline = (short *)_TIFFmalloc( |
| 82 |
TIFFSafeMultiply(tmsize_t, imagewidth, sizeof(short))); |
| 83 |
outlinesize = TIFFScanlineSize(out); |
| 84 |
outline = (unsigned char *)_TIFFmalloc(outlinesize); |
| 85 |
if (!(inputline && thisline && nextline && outline)) |
| 86 |
{ |
| 87 |
fprintf(stderr, "Out of memory.\n"); |
| 88 |
goto skip_on_error; |
| 89 |
} |
| 90 |
|
| 91 |
/* |
| 92 |
* Get first line |
| 93 |
*/ |
| 94 |
if (TIFFReadScanline(in, inputline, 0, 0) <= 0) |
| 95 |
goto skip_on_error; |
| 96 |
|
| 97 |
inptr = inputline; |
| 98 |
nextptr = nextline; |
| 99 |
for (j = 0; j < imagewidth; ++j) |
| 100 |
*nextptr++ = *inptr++; |
| 101 |
for (i = 1; i < imagelength; ++i) |
| 102 |
{ |
| 103 |
tmpptr = thisline; |
| 104 |
thisline = nextline; |
| 105 |
nextline = tmpptr; |
| 106 |
lastline = (i == imax); |
| 107 |
if (TIFFReadScanline(in, inputline, i, 0) <= 0) |
| 108 |
goto skip_on_error; |
| 109 |
inptr = inputline; |
| 110 |
nextptr = nextline; |
| 111 |
for (j = 0; j < imagewidth; ++j) |
| 112 |
*nextptr++ = *inptr++; |
| 113 |
thisptr = thisline; |
| 114 |
nextptr = nextline; |
| 115 |
_TIFFmemset(outptr = outline, 0, outlinesize); |
| 116 |
bit = 0x80; |
| 117 |
for (j = 0; j < imagewidth; ++j) |
| 118 |
{ |
| 119 |
register int v; |
| 120 |
|
| 121 |
lastpixel = (j == jmax); |
| 122 |
v = *thisptr++; |
| 123 |
if (v < 0) |
| 124 |
v = 0; |
| 125 |
else if (v > 255) |
| 126 |
v = 255; |
| 127 |
if (v > threshold) |
| 128 |
{ |
| 129 |
*outptr |= bit; |
| 130 |
v -= 255; |
| 131 |
} |
| 132 |
bit >>= 1; |
| 133 |
if (bit == 0) |
| 134 |
{ |
| 135 |
outptr++; |
| 136 |
bit = 0x80; |
| 137 |
} |
| 138 |
if (!lastpixel) |
| 139 |
thisptr[0] += v * 7 / 16; |
| 140 |
if (!lastline) |
| 141 |
{ |
| 142 |
if (j != 0) |
| 143 |
nextptr[-1] += v * 3 / 16; |
| 144 |
*nextptr++ += v * 5 / 16; |
| 145 |
if (!lastpixel) |
| 146 |
nextptr[0] += v / 16; |
| 147 |
} |
| 148 |
} |
| 149 |
if (TIFFWriteScanline(out, outline, i - 1, 0) < 0) |
| 150 |
goto skip_on_error; |
| 151 |
} |
| 152 |
goto exit_label; |
| 153 |
|
| 154 |
skip_on_error: |
| 155 |
errcode = 1; |
| 156 |
exit_label: |
| 157 |
_TIFFfree(inputline); |
| 158 |
_TIFFfree(thisline); |
| 159 |
_TIFFfree(nextline); |
| 160 |
_TIFFfree(outline); |
| 161 |
return errcode; |
| 162 |
} |
| 163 |
|
| 164 |
static uint16_t compression = COMPRESSION_PACKBITS; |
| 165 |
static uint16_t predictor = 0; |
| 166 |
static uint32_t group3options = 0; |
| 167 |
|
| 168 |
static void processG3Options(char *cp) |
| 169 |
{ |
| 170 |
if ((cp = strchr(cp, ':'))) |
| 171 |
{ |
| 172 |
do |
| 173 |
{ |
| 174 |
cp++; |
| 175 |
if (strneq(cp, "1d", 2)) |
| 176 |
group3options &= ~GROUP3OPT_2DENCODING; |
| 177 |
else if (strneq(cp, "2d", 2)) |
| 178 |
group3options |= GROUP3OPT_2DENCODING; |
| 179 |
else if (strneq(cp, "fill", 4)) |
| 180 |
group3options |= GROUP3OPT_FILLBITS; |
| 181 |
else |
| 182 |
usage(EXIT_FAILURE); |
| 183 |
} while ((cp = strchr(cp, ':'))); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
static int processCompressOptions(char *opt) |
| 188 |
{ |
| 189 |
if (streq(opt, "none")) |
| 190 |
compression = COMPRESSION_NONE; |
| 191 |
else if (streq(opt, "packbits")) |
| 192 |
compression = COMPRESSION_PACKBITS; |
| 193 |
else if (strneq(opt, "g3", 2)) |
| 194 |
{ |
| 195 |
processG3Options(opt); |
| 196 |
compression = COMPRESSION_CCITTFAX3; |
| 197 |
} |
| 198 |
else if (streq(opt, "g4")) |
| 199 |
compression = COMPRESSION_CCITTFAX4; |
| 200 |
else if (strneq(opt, "lzw", 3)) |
| 201 |
{ |
| 202 |
char *cp = strchr(opt, ':'); |
| 203 |
if (cp) |
| 204 |
predictor = atoi(cp + 1); |
| 205 |
compression = COMPRESSION_LZW; |
| 206 |
} |
| 207 |
else if (strneq(opt, "zip", 3)) |
| 208 |
{ |
| 209 |
char *cp = strchr(opt, ':'); |
| 210 |
if (cp) |
| 211 |
predictor = atoi(cp + 1); |
| 212 |
compression = COMPRESSION_ADOBE_DEFLATE; |
| 213 |
} |
| 214 |
else |
| 215 |
return (0); |
| 216 |
return (1); |
| 217 |
} |
| 218 |
|
| 219 |
int main(int argc, char *argv[]) |
| 220 |
{ |
| 221 |
TIFF *in, *out; |
| 222 |
uint16_t samplesperpixel, bitspersample = 1, shortv; |
| 223 |
float floatv; |
| 224 |
char thing[1024]; |
| 225 |
uint32_t rowsperstrip = (uint32_t)-1; |
| 226 |
uint16_t fillorder = 0; |
| 227 |
int c; |
| 228 |
#if !HAVE_DECL_OPTARG |
| 229 |
extern int optind; |
| 230 |
extern char *optarg; |
| 231 |
#endif |
| 232 |
|
| 233 |
while ((c = getopt(argc, argv, "c:f:r:t:h")) != -1) |
| 234 |
switch (c) |
| 235 |
{ |
| 236 |
case 'c': /* compression scheme */ |
| 237 |
if (!processCompressOptions(optarg)) |
| 238 |
usage(EXIT_FAILURE); |
| 239 |
break; |
| 240 |
case 'f': /* fill order */ |
| 241 |
if (streq(optarg, "lsb2msb")) |
| 242 |
fillorder = FILLORDER_LSB2MSB; |
| 243 |
else if (streq(optarg, "msb2lsb")) |
| 244 |
fillorder = FILLORDER_MSB2LSB; |
| 245 |
else |
| 246 |
usage(EXIT_FAILURE); |
| 247 |
break; |
| 248 |
case 'r': /* rows/strip */ |
| 249 |
rowsperstrip = atoi(optarg); |
| 250 |
break; |
| 251 |
case 't': |
| 252 |
threshold = atoi(optarg); |
| 253 |
if (threshold < 0) |
| 254 |
threshold = 0; |
| 255 |
else if (threshold > 255) |
| 256 |
threshold = 255; |
| 257 |
break; |
| 258 |
case 'h': |
| 259 |
usage(EXIT_SUCCESS); |
| 260 |
/*NOTREACHED*/ |
| 261 |
break; |
| 262 |
case '?': |
| 263 |
usage(EXIT_FAILURE); |
| 264 |
/*NOTREACHED*/ |
| 265 |
break; |
| 266 |
} |
| 267 |
if (argc - optind < 2) |
| 268 |
usage(EXIT_FAILURE); |
| 269 |
in = TIFFOpen(argv[optind], "r"); |
| 270 |
if (in == NULL) |
| 271 |
return (EXIT_FAILURE); |
| 272 |
TIFFGetField(in, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel); |
| 273 |
if (samplesperpixel != 1) |
| 274 |
{ |
| 275 |
fprintf(stderr, "%s: Not a b&w image.\n", argv[0]); |
| 276 |
TIFFClose(in); |
| 277 |
return (EXIT_FAILURE); |
| 278 |
} |
| 279 |
TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bitspersample); |
| 280 |
if (bitspersample != 8) |
| 281 |
{ |
| 282 |
fprintf(stderr, " %s: Sorry, only handle 8-bit samples.\n", argv[0]); |
| 283 |
TIFFClose(in); |
| 284 |
return (EXIT_FAILURE); |
| 285 |
} |
| 286 |
out = TIFFOpen(argv[optind + 1], "w"); |
| 287 |
if (out == NULL) |
| 288 |
{ |
| 289 |
TIFFClose(in); |
| 290 |
return (EXIT_FAILURE); |
| 291 |
} |
| 292 |
CopyField(TIFFTAG_IMAGEWIDTH, imagewidth); |
| 293 |
TIFFGetField(in, TIFFTAG_IMAGELENGTH, &imagelength); |
| 294 |
TIFFSetField(out, TIFFTAG_IMAGELENGTH, imagelength - 1); |
| 295 |
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 1); |
| 296 |
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 1); |
| 297 |
TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); |
| 298 |
TIFFSetField(out, TIFFTAG_COMPRESSION, compression); |
| 299 |
if (fillorder) |
| 300 |
TIFFSetField(out, TIFFTAG_FILLORDER, fillorder); |
| 301 |
else |
| 302 |
CopyField(TIFFTAG_FILLORDER, shortv); |
| 303 |
snprintf(thing, sizeof(thing), "Dithered B&W version of %s", argv[optind]); |
| 304 |
TIFFSetField(out, TIFFTAG_IMAGEDESCRIPTION, thing); |
| 305 |
CopyField(TIFFTAG_PHOTOMETRIC, shortv); |
| 306 |
CopyField(TIFFTAG_ORIENTATION, shortv); |
| 307 |
CopyField(TIFFTAG_XRESOLUTION, floatv); |
| 308 |
CopyField(TIFFTAG_YRESOLUTION, floatv); |
| 309 |
CopyField(TIFFTAG_RESOLUTIONUNIT, shortv); |
| 310 |
rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip); |
| 311 |
TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip); |
| 312 |
switch (compression) |
| 313 |
{ |
| 314 |
case COMPRESSION_CCITTFAX3: |
| 315 |
TIFFSetField(out, TIFFTAG_GROUP3OPTIONS, group3options); |
| 316 |
break; |
| 317 |
case COMPRESSION_LZW: |
| 318 |
case COMPRESSION_ADOBE_DEFLATE: |
| 319 |
case COMPRESSION_DEFLATE: |
| 320 |
if (predictor) |
| 321 |
TIFFSetField(out, TIFFTAG_PREDICTOR, predictor); |
| 322 |
break; |
| 323 |
} |
| 324 |
fsdither(in, out); |
| 325 |
TIFFClose(in); |
| 326 |
TIFFClose(out); |
| 327 |
return (EXIT_SUCCESS); |
| 328 |
} |
| 329 |
|
| 330 |
static const char usage_info[] = |
| 331 |
"Convert a greyscale image to bilevel using dithering\n\n" |
| 332 |
"usage: tiffdither [options] input.tif output.tif\n" |
| 333 |
"where options are:\n" |
| 334 |
" -r # make each strip have no more than # rows\n" |
| 335 |
" -t # set the threshold value for dithering (default 128)\n" |
| 336 |
" -f lsb2msb force lsb-to-msb FillOrder for output\n" |
| 337 |
" -f msb2lsb force msb-to-lsb FillOrder for output\n" |
| 338 |
"\n" |
| 339 |
#ifdef LZW_SUPPORT |
| 340 |
" -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding\n" |
| 341 |
" # set predictor value\n" |
| 342 |
" For example, -c lzw:2 for LZW-encoded data with horizontal " |
| 343 |
"differencing\n" |
| 344 |
#endif |
| 345 |
#ifdef ZIP_SUPPORT |
| 346 |
" -c zip[:opts] compress output with deflate encoding\n" |
| 347 |
" # set predictor value\n" |
| 348 |
#endif |
| 349 |
#ifdef PACKBITS_SUPPORT |
| 350 |
" -c packbits compress output with packbits encoding\n" |
| 351 |
#endif |
| 352 |
#ifdef CCITT_SUPPORT |
| 353 |
" -c g3[:opts] compress output with CCITT Group 3 encoding\n" |
| 354 |
" Group 3 options:\n" |
| 355 |
" 1d use default CCITT Group 3 1D-encoding\n" |
| 356 |
" 2d use optional CCITT Group 3 2D-encoding\n" |
| 357 |
" fill byte-align EOL codes\n" |
| 358 |
" For example, -c g3:2d:fill for G3-2D-encoded data with byte-aligned " |
| 359 |
"EOLs\n" |
| 360 |
" -c g4 compress output with CCITT Group 4 encoding\n" |
| 361 |
#endif |
| 362 |
#if defined(LZW_SUPPORT) || defined(ZIP_SUPPORT) || \ |
| 363 |
defined(PACKBITS_SUPPORT) || defined(CCITT_SUPPORT) |
| 364 |
" -c none use no compression algorithm on output\n" |
| 365 |
#endif |
| 366 |
"\n"; |
| 367 |
|
| 368 |
static void usage(int code) |
| 369 |
{ |
| 370 |
FILE *out = (code == EXIT_SUCCESS) ? stdout : stderr; |
| 371 |
|
| 372 |
fprintf(out, "%s\n\n", TIFFGetVersion()); |
| 373 |
fprintf(out, "%s", usage_info); |
| 374 |
exit(code); |
| 375 |
} |