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