|
kusano |
7d535a |
/* $Id: sgisv.c,v 1.6 2010-03-10 18:56:49 bfriesen Exp $ */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Copyright (c) 1990-1997 Sam Leffler
|
|
kusano |
7d535a |
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* Permission to use, copy, modify, distribute, and sell this software and
|
|
kusano |
7d535a |
* its documentation for any purpose is hereby granted without fee, provided
|
|
kusano |
7d535a |
* that (i) the above copyright notices and this permission notice appear in
|
|
kusano |
7d535a |
* all copies of the software and related documentation, and (ii) the names of
|
|
kusano |
7d535a |
* Sam Leffler and Silicon Graphics may not be used in any advertising or
|
|
kusano |
7d535a |
* publicity relating to the software without the specific, prior written
|
|
kusano |
7d535a |
* permission of Sam Leffler and Silicon Graphics.
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
|
|
kusano |
7d535a |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
|
|
kusano |
7d535a |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
|
|
kusano |
7d535a |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
|
|
kusano |
7d535a |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
|
kusano |
7d535a |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
|
|
kusano |
7d535a |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
|
|
kusano |
7d535a |
* OF THIS SOFTWARE.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#include <stdio.h></stdio.h>
|
|
kusano |
7d535a |
#include <stdlib.h></stdlib.h>
|
|
kusano |
7d535a |
#include <string.h></string.h>
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#include <gl.h></gl.h>
|
|
kusano |
7d535a |
#include <ctype.h></ctype.h>
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#include "tiffio.h"
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
typedef unsigned char unsigned char;
|
|
kusano |
7d535a |
typedef unsigned long uint32;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#define streq(a,b) (strcmp(a,b) == 0)
|
|
kusano |
7d535a |
#define strneq(a,b,n) (strncmp(a,b,n) == 0)
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
uint32 rowsperstrip = (uint32) -1;
|
|
kusano |
7d535a |
uint16 compression = COMPRESSION_PACKBITS;
|
|
kusano |
7d535a |
uint16 config = PLANARCONFIG_CONTIG;
|
|
kusano |
7d535a |
uint16 predictor = 0;
|
|
kusano |
7d535a |
int xmaxscreen;
|
|
kusano |
7d535a |
int ymaxscreen;
|
|
kusano |
7d535a |
uint16 photometric = PHOTOMETRIC_RGB;
|
|
kusano |
7d535a |
int jpegcolormode = JPEGCOLORMODE_RGB;
|
|
kusano |
7d535a |
int quality = 75; /* JPEG quality */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static void usage(void);
|
|
kusano |
7d535a |
static void tiffsv(char*, int, int, int, int);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
int
|
|
kusano |
7d535a |
main(int argc, char* argv[])
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int c;
|
|
kusano |
7d535a |
extern int optind;
|
|
kusano |
7d535a |
extern char* optarg;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
while ((c = getopt(argc, argv, "c:p:r:")) != -1)
|
|
kusano |
7d535a |
switch (c) {
|
|
kusano |
7d535a |
case 'b': /* save as b&w */
|
|
kusano |
7d535a |
photometric = PHOTOMETRIC_MINISBLACK;
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
case 'c': /* compression scheme */
|
|
kusano |
7d535a |
if (streq(optarg, "none"))
|
|
kusano |
7d535a |
compression = COMPRESSION_NONE;
|
|
kusano |
7d535a |
else if (streq(optarg, "packbits"))
|
|
kusano |
7d535a |
compression = COMPRESSION_PACKBITS;
|
|
kusano |
7d535a |
else if (strneq(optarg, "jpeg", 4)) {
|
|
kusano |
7d535a |
char* cp = strchr(optarg, ':');
|
|
kusano |
7d535a |
if (cp && isdigit(cp[1]))
|
|
kusano |
7d535a |
quality = atoi(cp+1);
|
|
kusano |
7d535a |
if (cp && strchr(cp, 'r'))
|
|
kusano |
7d535a |
jpegcolormode = JPEGCOLORMODE_RAW;
|
|
kusano |
7d535a |
compression = COMPRESSION_JPEG;
|
|
kusano |
7d535a |
} else if (strneq(optarg, "lzw", 3)) {
|
|
kusano |
7d535a |
char* cp = strchr(optarg, ':');
|
|
kusano |
7d535a |
if (cp)
|
|
kusano |
7d535a |
predictor = atoi(cp+1);
|
|
kusano |
7d535a |
compression = COMPRESSION_LZW;
|
|
kusano |
7d535a |
} else
|
|
kusano |
7d535a |
usage();
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
case 'p': /* planar configuration */
|
|
kusano |
7d535a |
if (streq(optarg, "separate"))
|
|
kusano |
7d535a |
config = PLANARCONFIG_SEPARATE;
|
|
kusano |
7d535a |
else if (streq(optarg, "contig"))
|
|
kusano |
7d535a |
config = PLANARCONFIG_CONTIG;
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
usage();
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
case 'r': /* rows/strip */
|
|
kusano |
7d535a |
rowsperstrip = atoi(optarg);
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
case '?':
|
|
kusano |
7d535a |
usage();
|
|
kusano |
7d535a |
/*NOTREACHED*/
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (argc - optind != 1 && argc - optind != 5)
|
|
kusano |
7d535a |
usage();
|
|
kusano |
7d535a |
xmaxscreen = getgdesc(GD_XPMAX)-1;
|
|
kusano |
7d535a |
ymaxscreen = getgdesc(GD_YPMAX)-1;
|
|
kusano |
7d535a |
foreground();
|
|
kusano |
7d535a |
noport();
|
|
kusano |
7d535a |
winopen("tiffsv");
|
|
kusano |
7d535a |
if (argc - optind == 5)
|
|
kusano |
7d535a |
tiffsv(argv[optind],
|
|
kusano |
7d535a |
atoi(argv[optind+1]), atoi(argv[optind+2]),
|
|
kusano |
7d535a |
atoi(argv[optind+3]), atoi(argv[optind+4]));
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
tiffsv(argv[optind], 0, xmaxscreen, 0, ymaxscreen);
|
|
kusano |
7d535a |
return (0);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
char* stuff[] = {
|
|
kusano |
7d535a |
"usage: tiffsv [options] outimage.tif [x1 x2 y1 y2] [-b]",
|
|
kusano |
7d535a |
"where options are:",
|
|
kusano |
7d535a |
" -p contig pack samples contiguously (e.g. RGBRGB...)",
|
|
kusano |
7d535a |
" -p separate store samples separately (e.g. RRR...GGG...BBB...)",
|
|
kusano |
7d535a |
"",
|
|
kusano |
7d535a |
" -r # make each strip have no more than # rows",
|
|
kusano |
7d535a |
"",
|
|
kusano |
7d535a |
" -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding",
|
|
kusano |
7d535a |
" -c jpeg[:opts]compress output with JPEG encoding",
|
|
kusano |
7d535a |
" -c packbits compress output with packbits encoding",
|
|
kusano |
7d535a |
" -c none use no compression algorithm on output",
|
|
kusano |
7d535a |
"",
|
|
kusano |
7d535a |
"JPEG options:",
|
|
kusano |
7d535a |
" # set compression quality level (0-100, default 75)",
|
|
kusano |
7d535a |
" r output color image as RGB rather than YCbCr",
|
|
kusano |
7d535a |
"",
|
|
kusano |
7d535a |
"LZW options:",
|
|
kusano |
7d535a |
" # set predictor value for Lempel-Ziv & Welch encoding",
|
|
kusano |
7d535a |
"For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
|
|
kusano |
7d535a |
NULL
|
|
kusano |
7d535a |
};
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static void
|
|
kusano |
7d535a |
usage(void)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
char buf[BUFSIZ];
|
|
kusano |
7d535a |
int i;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
setbuf(stderr, buf);
|
|
kusano |
7d535a |
for (i = 0; stuff[i] != NULL; i++)
|
|
kusano |
7d535a |
fprintf(stderr, "%s\n", stuff[i]);
|
|
kusano |
7d535a |
exit(-1);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static void
|
|
kusano |
7d535a |
svRGBSeparate(TIFF* tif, uint32* ss, int xsize, int ysize)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
tsize_t stripsize = TIFFStripSize(tif);
|
|
kusano |
7d535a |
unsigned char *rbuf = (unsigned char *)_TIFFmalloc(3*stripsize);
|
|
kusano |
7d535a |
unsigned char *gbuf = rbuf + stripsize;
|
|
kusano |
7d535a |
unsigned char *bbuf = gbuf + stripsize;
|
|
kusano |
7d535a |
register int y;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
for (y = 0; y <= ysize; y += rowsperstrip) {
|
|
kusano |
7d535a |
unsigned char *rp, *gp, *bp;
|
|
kusano |
7d535a |
register int x;
|
|
kusano |
7d535a |
register uint32 n;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
n = rowsperstrip;
|
|
kusano |
7d535a |
if (n > ysize-y+1)
|
|
kusano |
7d535a |
n = ysize-y+1;
|
|
kusano |
7d535a |
rp = rbuf; gp = gbuf; bp = bbuf;
|
|
kusano |
7d535a |
do {
|
|
kusano |
7d535a |
for (x = 0; x <= xsize; x++) {
|
|
kusano |
7d535a |
uint32 v = ss[x];
|
|
kusano |
7d535a |
rp[x] = v;
|
|
kusano |
7d535a |
gp[x] = v >> 8;
|
|
kusano |
7d535a |
bp[x] = v >> 16;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
rp += xsize+1, gp += xsize+1, bp += xsize+1;
|
|
kusano |
7d535a |
ss += xsize+1;
|
|
kusano |
7d535a |
} while (--n);
|
|
kusano |
7d535a |
if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,0),
|
|
kusano |
7d535a |
rbuf, stripsize) < 0)
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,1),
|
|
kusano |
7d535a |
gbuf, stripsize) < 0)
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,2),
|
|
kusano |
7d535a |
bbuf, stripsize) < 0)
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
_TIFFfree(rbuf);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static void
|
|
kusano |
7d535a |
svRGBContig(TIFF* tif, uint32* ss, int xsize, int ysize)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
register int x, y;
|
|
kusano |
7d535a |
tsize_t stripsize = TIFFStripSize(tif);
|
|
kusano |
7d535a |
unsigned char *strip = (unsigned char *)_TIFFmalloc(stripsize);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
for (y = 0; y <= ysize; y += rowsperstrip) {
|
|
kusano |
7d535a |
register unsigned char *pp = strip;
|
|
kusano |
7d535a |
register uint32 n;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
n = rowsperstrip;
|
|
kusano |
7d535a |
if (n > ysize-y+1)
|
|
kusano |
7d535a |
n = ysize-y+1;
|
|
kusano |
7d535a |
do {
|
|
kusano |
7d535a |
for (x = 0; x <= xsize; x++) {
|
|
kusano |
7d535a |
uint32 v = ss[x];
|
|
kusano |
7d535a |
pp[0] = v;
|
|
kusano |
7d535a |
pp[1] = v >> 8;
|
|
kusano |
7d535a |
pp[2] = v >> 16;
|
|
kusano |
7d535a |
pp += 3;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
ss += xsize+1;
|
|
kusano |
7d535a |
} while (--n);
|
|
kusano |
7d535a |
if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,0),
|
|
kusano |
7d535a |
strip, stripsize) < 0)
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
_TIFFfree(strip);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#undef RED
|
|
kusano |
7d535a |
#undef GREEN
|
|
kusano |
7d535a |
#undef BLUE
|
|
kusano |
7d535a |
#define CVT(x) (((x)*255)/100)
|
|
kusano |
7d535a |
#define RED CVT(28) /* 28% */
|
|
kusano |
7d535a |
#define GREEN CVT(59) /* 59% */
|
|
kusano |
7d535a |
#define BLUE CVT(11) /* 11% */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static void
|
|
kusano |
7d535a |
svGrey(TIFF* tif, uint32* ss, int xsize, int ysize)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
register int x, y;
|
|
kusano |
7d535a |
unsigned char *buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif));
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
for (y = 0; y <= ysize; y++) {
|
|
kusano |
7d535a |
for (x = 0; x <= xsize; x++) {
|
|
kusano |
7d535a |
unsigned char *cp = (unsigned char *)&ss[x];
|
|
kusano |
7d535a |
buf[x] = (RED*cp[3] + GREEN*cp[2] + BLUE*cp[1]) >> 8;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (TIFFWriteScanline(tif, buf, (uint32) y, 0) < 0)
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
ss += xsize+1;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
_TIFFfree(buf);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#define MIN(a,b) ((a)<(b)?(a):(b))
|
|
kusano |
7d535a |
#define ABS(x) ((x)<0?-(x):(x))
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static void
|
|
kusano |
7d535a |
tiffsv(char* name, int x1, int x2, int y1, int y2)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
TIFF *tif;
|
|
kusano |
7d535a |
int xsize, ysize;
|
|
kusano |
7d535a |
int xorg, yorg;
|
|
kusano |
7d535a |
uint32 *scrbuf;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
xorg = MIN(x1,x2);
|
|
kusano |
7d535a |
yorg = MIN(y1,y2);
|
|
kusano |
7d535a |
if (xorg<0)
|
|
kusano |
7d535a |
xorg = 0;
|
|
kusano |
7d535a |
if (yorg<0)
|
|
kusano |
7d535a |
yorg = 0;
|
|
kusano |
7d535a |
xsize = ABS(x2-x1);
|
|
kusano |
7d535a |
ysize = ABS(y2-y1);
|
|
kusano |
7d535a |
if (xorg+xsize > xmaxscreen)
|
|
kusano |
7d535a |
xsize = xmaxscreen-xorg;
|
|
kusano |
7d535a |
if (yorg+ysize > ymaxscreen)
|
|
kusano |
7d535a |
ysize = ymaxscreen-yorg;
|
|
kusano |
7d535a |
tif = TIFFOpen(name, "w");
|
|
kusano |
7d535a |
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32) (xsize+1));
|
|
kusano |
7d535a |
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32) (ysize+1));
|
|
kusano |
7d535a |
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
|
|
kusano |
7d535a |
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL,
|
|
kusano |
7d535a |
photometric == PHOTOMETRIC_RGB ? 3 : 1);
|
|
kusano |
7d535a |
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, config);
|
|
kusano |
7d535a |
TIFFSetField(tif, TIFFTAG_COMPRESSION, compression);
|
|
kusano |
7d535a |
switch (compression) {
|
|
kusano |
7d535a |
case COMPRESSION_JPEG:
|
|
kusano |
7d535a |
if (photometric == PHOTOMETRIC_RGB && jpegcolormode == JPEGCOLORMODE_RGB)
|
|
kusano |
7d535a |
photometric = PHOTOMETRIC_YCBCR;
|
|
kusano |
7d535a |
TIFFSetField(tif, TIFFTAG_JPEGQUALITY, quality);
|
|
kusano |
7d535a |
TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, jpegcolormode);
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
case COMPRESSION_LZW:
|
|
kusano |
7d535a |
if (predictor != 0)
|
|
kusano |
7d535a |
TIFFSetField(tif, TIFFTAG_PREDICTOR, predictor);
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric);
|
|
kusano |
7d535a |
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_BOTLEFT);
|
|
kusano |
7d535a |
rowsperstrip = TIFFDefaultStripSize(tif, rowsperstrip);
|
|
kusano |
7d535a |
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
|
|
kusano |
7d535a |
scrbuf = (uint32 *)_TIFFmalloc((xsize+1)*(ysize+1)*sizeof (uint32));
|
|
kusano |
7d535a |
readdisplay(xorg, yorg, xorg+xsize, yorg+ysize, scrbuf, RD_FREEZE);
|
|
kusano |
7d535a |
if (photometric == PHOTOMETRIC_RGB) {
|
|
kusano |
7d535a |
if (config == PLANARCONFIG_SEPARATE)
|
|
kusano |
7d535a |
svRGBSeparate(tif, scrbuf, xsize, ysize);
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
svRGBContig(tif, scrbuf, xsize, ysize);
|
|
kusano |
7d535a |
} else
|
|
kusano |
7d535a |
svGrey(tif, scrbuf, xsize, ysize);
|
|
kusano |
7d535a |
(void) TIFFClose(tif);
|
|
kusano |
7d535a |
_TIFFfree((char *)scrbuf);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Local Variables:
|
|
kusano |
7d535a |
* mode: c
|
|
kusano |
7d535a |
* c-basic-offset: 8
|
|
kusano |
7d535a |
* fill-column: 78
|
|
kusano |
7d535a |
* End:
|
|
kusano |
7d535a |
*/
|