|
roentgen |
b75cab |
/* $Id: raw2tiff.c,v 1.25 2010-03-10 18:56:49 bfriesen Exp $
|
|
roentgen |
b75cab |
*
|
|
roentgen |
b75cab |
* Project: libtiff tools
|
|
roentgen |
b75cab |
* Purpose: Convert raw byte sequences in TIFF images
|
|
roentgen |
b75cab |
* Author: Andrey Kiselev, dron@ak4719.spb.edu
|
|
roentgen |
b75cab |
*
|
|
roentgen |
b75cab |
******************************************************************************
|
|
roentgen |
b75cab |
* Copyright (c) 2002, Andrey Kiselev <dron@ak4719.spb.edu></dron@ak4719.spb.edu>
|
|
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 "tif_config.h"
|
|
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 |
#include <sys stat.h=""></sys>
|
|
roentgen |
b75cab |
#include <sys types.h=""></sys>
|
|
roentgen |
b75cab |
#include <math.h></math.h>
|
|
roentgen |
b75cab |
#include <ctype.h></ctype.h>
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
#ifdef HAVE_UNISTD_H
|
|
roentgen |
b75cab |
# include <unistd.h></unistd.h>
|
|
roentgen |
b75cab |
#endif
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
#if HAVE_FCNTL_H
|
|
roentgen |
b75cab |
# include <fcntl.h></fcntl.h>
|
|
roentgen |
b75cab |
#endif
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
#if HAVE_SYS_TYPES_H
|
|
roentgen |
b75cab |
# include <sys types.h=""></sys>
|
|
roentgen |
b75cab |
#endif
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
#if HAVE_IO_H
|
|
roentgen |
b75cab |
# include <io.h></io.h>
|
|
roentgen |
b75cab |
#endif
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
#ifdef NEED_LIBPORT
|
|
roentgen |
b75cab |
# include "libport.h"
|
|
roentgen |
b75cab |
#endif
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
#include "tiffio.h"
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
#ifndef HAVE_GETOPT
|
|
roentgen |
b75cab |
extern int getopt(int, char**, char*);
|
|
roentgen |
b75cab |
#endif
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
#ifndef O_BINARY
|
|
roentgen |
b75cab |
# define O_BINARY 0
|
|
roentgen |
b75cab |
#endif
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
typedef enum {
|
|
roentgen |
b75cab |
PIXEL,
|
|
roentgen |
b75cab |
BAND
|
|
roentgen |
b75cab |
} InterleavingType;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
static uint16 compression = (uint16) -1;
|
|
roentgen |
b75cab |
static int jpegcolormode = JPEGCOLORMODE_RGB;
|
|
roentgen |
b75cab |
static int quality = 75; /* JPEG quality */
|
|
roentgen |
b75cab |
static uint16 predictor = 0;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
static void swapBytesInScanline(void *, uint32, TIFFDataType);
|
|
roentgen |
b75cab |
static int guessSize(int, TIFFDataType, off_t, uint32, int,
|
|
roentgen |
b75cab |
uint32 *, uint32 *);
|
|
roentgen |
b75cab |
static double correlation(void *, void *, uint32, TIFFDataType);
|
|
roentgen |
b75cab |
static void usage(void);
|
|
roentgen |
b75cab |
static int processCompressOptions(char*);
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
int
|
|
roentgen |
b75cab |
main(int argc, char* argv[])
|
|
roentgen |
b75cab |
{
|
|
roentgen |
b75cab |
uint32 width = 0, length = 0, linebytes, bufsize;
|
|
roentgen |
b75cab |
uint32 nbands = 1; /* number of bands in input image*/
|
|
roentgen |
b75cab |
off_t hdr_size = 0; /* size of the header to skip */
|
|
roentgen |
b75cab |
TIFFDataType dtype = TIFF_BYTE;
|
|
roentgen |
b75cab |
int16 depth = 1; /* bytes per pixel in input image */
|
|
roentgen |
b75cab |
int swab = 0; /* byte swapping flag */
|
|
roentgen |
b75cab |
InterleavingType interleaving = 0; /* interleaving type flag */
|
|
roentgen |
b75cab |
uint32 rowsperstrip = (uint32) -1;
|
|
roentgen |
b75cab |
uint16 photometric = PHOTOMETRIC_MINISBLACK;
|
|
roentgen |
b75cab |
uint16 config = PLANARCONFIG_CONTIG;
|
|
roentgen |
b75cab |
uint16 fillorder = FILLORDER_LSB2MSB;
|
|
roentgen |
b75cab |
int fd;
|
|
roentgen |
b75cab |
char *outfilename = NULL;
|
|
roentgen |
b75cab |
TIFF *out;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
uint32 row, col, band;
|
|
roentgen |
b75cab |
int c;
|
|
roentgen |
b75cab |
unsigned char *buf = NULL, *buf1 = NULL;
|
|
roentgen |
b75cab |
extern int optind;
|
|
roentgen |
b75cab |
extern char* optarg;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
while ((c = getopt(argc, argv, "c:r:H:w:l:b:d:LMp:si:o:h")) != -1) {
|
|
roentgen |
b75cab |
switch (c) {
|
|
roentgen |
b75cab |
case 'c': /* compression scheme */
|
|
roentgen |
b75cab |
if (!processCompressOptions(optarg))
|
|
roentgen |
b75cab |
usage();
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case 'r': /* rows/strip */
|
|
roentgen |
b75cab |
rowsperstrip = atoi(optarg);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case 'H': /* size of input image file header */
|
|
roentgen |
b75cab |
hdr_size = atoi(optarg);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case 'w': /* input image width */
|
|
roentgen |
b75cab |
width = atoi(optarg);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case 'l': /* input image length */
|
|
roentgen |
b75cab |
length = atoi(optarg);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case 'b': /* number of bands in input image */
|
|
roentgen |
b75cab |
nbands = atoi(optarg);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case 'd': /* type of samples in input image */
|
|
roentgen |
b75cab |
if (strncmp(optarg, "byte", 4) == 0)
|
|
roentgen |
b75cab |
dtype = TIFF_BYTE;
|
|
roentgen |
b75cab |
else if (strncmp(optarg, "short", 5) == 0)
|
|
roentgen |
b75cab |
dtype = TIFF_SHORT;
|
|
roentgen |
b75cab |
else if (strncmp(optarg, "long", 4) == 0)
|
|
roentgen |
b75cab |
dtype = TIFF_LONG;
|
|
roentgen |
b75cab |
else if (strncmp(optarg, "sbyte", 5) == 0)
|
|
roentgen |
b75cab |
dtype = TIFF_SBYTE;
|
|
roentgen |
b75cab |
else if (strncmp(optarg, "sshort", 6) == 0)
|
|
roentgen |
b75cab |
dtype = TIFF_SSHORT;
|
|
roentgen |
b75cab |
else if (strncmp(optarg, "slong", 5) == 0)
|
|
roentgen |
b75cab |
dtype = TIFF_SLONG;
|
|
roentgen |
b75cab |
else if (strncmp(optarg, "float", 5) == 0)
|
|
roentgen |
b75cab |
dtype = TIFF_FLOAT;
|
|
roentgen |
b75cab |
else if (strncmp(optarg, "double", 6) == 0)
|
|
roentgen |
b75cab |
dtype = TIFF_DOUBLE;
|
|
roentgen |
b75cab |
else
|
|
roentgen |
b75cab |
dtype = TIFF_BYTE;
|
|
roentgen |
b75cab |
depth = TIFFDataWidth(dtype);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case 'L': /* input has lsb-to-msb fillorder */
|
|
roentgen |
b75cab |
fillorder = FILLORDER_LSB2MSB;
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case 'M': /* input has msb-to-lsb fillorder */
|
|
roentgen |
b75cab |
fillorder = FILLORDER_MSB2LSB;
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case 'p': /* photometric interpretation */
|
|
roentgen |
b75cab |
if (strncmp(optarg, "miniswhite", 10) == 0)
|
|
roentgen |
b75cab |
photometric = PHOTOMETRIC_MINISWHITE;
|
|
roentgen |
b75cab |
else if (strncmp(optarg, "minisblack", 10) == 0)
|
|
roentgen |
b75cab |
photometric = PHOTOMETRIC_MINISBLACK;
|
|
roentgen |
b75cab |
else if (strncmp(optarg, "rgb", 3) == 0)
|
|
roentgen |
b75cab |
photometric = PHOTOMETRIC_RGB;
|
|
roentgen |
b75cab |
else if (strncmp(optarg, "cmyk", 4) == 0)
|
|
roentgen |
b75cab |
photometric = PHOTOMETRIC_SEPARATED;
|
|
roentgen |
b75cab |
else if (strncmp(optarg, "ycbcr", 5) == 0)
|
|
roentgen |
b75cab |
photometric = PHOTOMETRIC_YCBCR;
|
|
roentgen |
b75cab |
else if (strncmp(optarg, "cielab", 6) == 0)
|
|
roentgen |
b75cab |
photometric = PHOTOMETRIC_CIELAB;
|
|
roentgen |
b75cab |
else if (strncmp(optarg, "icclab", 6) == 0)
|
|
roentgen |
b75cab |
photometric = PHOTOMETRIC_ICCLAB;
|
|
roentgen |
b75cab |
else if (strncmp(optarg, "itulab", 6) == 0)
|
|
roentgen |
b75cab |
photometric = PHOTOMETRIC_ITULAB;
|
|
roentgen |
b75cab |
else
|
|
roentgen |
b75cab |
photometric = PHOTOMETRIC_MINISBLACK;
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case 's': /* do we need to swap bytes? */
|
|
roentgen |
b75cab |
swab = 1;
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case 'i': /* type of interleaving */
|
|
roentgen |
b75cab |
if (strncmp(optarg, "pixel", 4) == 0)
|
|
roentgen |
b75cab |
interleaving = PIXEL;
|
|
roentgen |
b75cab |
else if (strncmp(optarg, "band", 6) == 0)
|
|
roentgen |
b75cab |
interleaving = BAND;
|
|
roentgen |
b75cab |
else
|
|
roentgen |
b75cab |
interleaving = 0;
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case 'o':
|
|
roentgen |
b75cab |
outfilename = optarg;
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case 'h':
|
|
roentgen |
b75cab |
usage();
|
|
roentgen |
b75cab |
default:
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
if (argc - optind < 2)
|
|
roentgen |
b75cab |
usage();
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
fd = open(argv[optind], O_RDONLY|O_BINARY, 0);
|
|
roentgen |
b75cab |
if (fd < 0) {
|
|
roentgen |
b75cab |
fprintf(stderr, "%s: %s: Cannot open input file.\n",
|
|
roentgen |
b75cab |
argv[0], argv[optind]);
|
|
roentgen |
b75cab |
return (-1);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
if (guessSize(fd, dtype, hdr_size, nbands, swab, &width, &length) < 0)
|
|
roentgen |
b75cab |
return 1;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
if (outfilename == NULL)
|
|
roentgen |
b75cab |
outfilename = argv[optind+1];
|
|
roentgen |
b75cab |
out = TIFFOpen(outfilename, "w");
|
|
roentgen |
b75cab |
if (out == NULL) {
|
|
roentgen |
b75cab |
fprintf(stderr, "%s: %s: Cannot open file for output.\n",
|
|
roentgen |
b75cab |
argv[0], outfilename);
|
|
roentgen |
b75cab |
return (-1);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_IMAGELENGTH, length);
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, nbands);
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, depth * 8);
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_FILLORDER, fillorder);
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric);
|
|
roentgen |
b75cab |
switch (dtype) {
|
|
roentgen |
b75cab |
case TIFF_BYTE:
|
|
roentgen |
b75cab |
case TIFF_SHORT:
|
|
roentgen |
b75cab |
case TIFF_LONG:
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case TIFF_SBYTE:
|
|
roentgen |
b75cab |
case TIFF_SSHORT:
|
|
roentgen |
b75cab |
case TIFF_SLONG:
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case TIFF_FLOAT:
|
|
roentgen |
b75cab |
case TIFF_DOUBLE:
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
default:
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_VOID);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
if (compression == (uint16) -1)
|
|
roentgen |
b75cab |
compression = COMPRESSION_PACKBITS;
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
|
|
roentgen |
b75cab |
switch (compression) {
|
|
roentgen |
b75cab |
case COMPRESSION_JPEG:
|
|
roentgen |
b75cab |
if (photometric == PHOTOMETRIC_RGB
|
|
roentgen |
b75cab |
&& jpegcolormode == JPEGCOLORMODE_RGB)
|
|
roentgen |
b75cab |
photometric = PHOTOMETRIC_YCBCR;
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality);
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case COMPRESSION_LZW:
|
|
roentgen |
b75cab |
case COMPRESSION_DEFLATE:
|
|
roentgen |
b75cab |
if (predictor != 0)
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
switch(interleaving) {
|
|
roentgen |
b75cab |
case BAND: /* band interleaved data */
|
|
roentgen |
b75cab |
linebytes = width * depth;
|
|
roentgen |
b75cab |
buf = (unsigned char *)_TIFFmalloc(linebytes);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case PIXEL: /* pixel interleaved data */
|
|
roentgen |
b75cab |
default:
|
|
roentgen |
b75cab |
linebytes = width * nbands * depth;
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
bufsize = width * nbands * depth;
|
|
roentgen |
b75cab |
buf1 = (unsigned char *)_TIFFmalloc(bufsize);
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip);
|
|
roentgen |
b75cab |
if (rowsperstrip > length) {
|
|
roentgen |
b75cab |
rowsperstrip = length;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip );
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
lseek(fd, hdr_size, SEEK_SET); /* Skip the file header */
|
|
roentgen |
b75cab |
for (row = 0; row < length; row++) {
|
|
roentgen |
b75cab |
switch(interleaving) {
|
|
roentgen |
b75cab |
case BAND: /* band interleaved data */
|
|
roentgen |
b75cab |
for (band = 0; band < nbands; band++) {
|
|
roentgen |
b75cab |
lseek(fd,
|
|
roentgen |
b75cab |
hdr_size + (length*band+row)*linebytes,
|
|
roentgen |
b75cab |
SEEK_SET);
|
|
roentgen |
b75cab |
if (read(fd, buf, linebytes) < 0) {
|
|
roentgen |
b75cab |
fprintf(stderr,
|
|
roentgen |
b75cab |
"%s: %s: scanline %lu: Read error.\n",
|
|
roentgen |
b75cab |
argv[0], argv[optind],
|
|
roentgen |
b75cab |
(unsigned long) row);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
if (swab) /* Swap bytes if needed */
|
|
roentgen |
b75cab |
swapBytesInScanline(buf, width, dtype);
|
|
roentgen |
b75cab |
for (col = 0; col < width; col++)
|
|
roentgen |
b75cab |
memcpy(buf1 + (col*nbands+band)*depth,
|
|
roentgen |
b75cab |
buf + col * depth, depth);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case PIXEL: /* pixel interleaved data */
|
|
roentgen |
b75cab |
default:
|
|
roentgen |
b75cab |
if (read(fd, buf1, bufsize) < 0) {
|
|
roentgen |
b75cab |
fprintf(stderr,
|
|
roentgen |
b75cab |
"%s: %s: scanline %lu: Read error.\n",
|
|
roentgen |
b75cab |
argv[0], argv[optind],
|
|
roentgen |
b75cab |
(unsigned long) row);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
if (swab) /* Swap bytes if needed */
|
|
roentgen |
b75cab |
swapBytesInScanline(buf1, width, dtype);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
if (TIFFWriteScanline(out, buf1, row, 0) < 0) {
|
|
roentgen |
b75cab |
fprintf(stderr, "%s: %s: scanline %lu: Write error.\n",
|
|
roentgen |
b75cab |
argv[0], outfilename, (unsigned long) row);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
if (buf)
|
|
roentgen |
b75cab |
_TIFFfree(buf);
|
|
roentgen |
b75cab |
if (buf1)
|
|
roentgen |
b75cab |
_TIFFfree(buf1);
|
|
roentgen |
b75cab |
TIFFClose(out);
|
|
roentgen |
b75cab |
return (0);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
static void
|
|
roentgen |
b75cab |
swapBytesInScanline(void *buf, uint32 width, TIFFDataType dtype)
|
|
roentgen |
b75cab |
{
|
|
roentgen |
b75cab |
switch (dtype) {
|
|
roentgen |
b75cab |
case TIFF_SHORT:
|
|
roentgen |
b75cab |
case TIFF_SSHORT:
|
|
roentgen |
b75cab |
TIFFSwabArrayOfShort((uint16*)buf,
|
|
roentgen |
b75cab |
(unsigned long)width);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case TIFF_LONG:
|
|
roentgen |
b75cab |
case TIFF_SLONG:
|
|
roentgen |
b75cab |
TIFFSwabArrayOfLong((uint32*)buf,
|
|
roentgen |
b75cab |
(unsigned long)width);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
/* case TIFF_FLOAT: */ /* FIXME */
|
|
roentgen |
b75cab |
case TIFF_DOUBLE:
|
|
roentgen |
b75cab |
TIFFSwabArrayOfDouble((double*)buf,
|
|
roentgen |
b75cab |
(unsigned long)width);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
default:
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
static int
|
|
roentgen |
b75cab |
guessSize(int fd, TIFFDataType dtype, off_t hdr_size, uint32 nbands,
|
|
roentgen |
b75cab |
int swab, uint32 *width, uint32 *length)
|
|
roentgen |
b75cab |
{
|
|
roentgen |
b75cab |
const float longt = 40.0; /* maximum possible height/width ratio */
|
|
roentgen |
b75cab |
char *buf1, *buf2;
|
|
roentgen |
b75cab |
struct stat filestat;
|
|
roentgen |
b75cab |
uint32 w, h, scanlinesize, imagesize;
|
|
roentgen |
b75cab |
uint32 depth = TIFFDataWidth(dtype);
|
|
roentgen |
b75cab |
float cor_coef = 0, tmp;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
fstat(fd, &filestat);
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
if (filestat.st_size < hdr_size) {
|
|
roentgen |
b75cab |
fprintf(stderr, "Too large header size specified.\n");
|
|
roentgen |
b75cab |
return -1;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
imagesize = (filestat.st_size - hdr_size) / nbands / depth;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
if (*width != 0 && *length == 0) {
|
|
roentgen |
b75cab |
fprintf(stderr, "Image height is not specified.\n");
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
*length = imagesize / *width;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
fprintf(stderr, "Height is guessed as %lu.\n",
|
|
roentgen |
b75cab |
(unsigned long)*length);
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
return 1;
|
|
roentgen |
b75cab |
} else if (*width == 0 && *length != 0) {
|
|
roentgen |
b75cab |
fprintf(stderr, "Image width is not specified.\n");
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
*width = imagesize / *length;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
fprintf(stderr, "Width is guessed as %lu.\n",
|
|
roentgen |
b75cab |
(unsigned long)*width);
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
return 1;
|
|
roentgen |
b75cab |
} else if (*width == 0 && *length == 0) {
|
|
roentgen |
b75cab |
fprintf(stderr, "Image width and height are not specified.\n");
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
for (w = (uint32) sqrt(imagesize / longt);
|
|
roentgen |
b75cab |
w < sqrt(imagesize * longt);
|
|
roentgen |
b75cab |
w++) {
|
|
roentgen |
b75cab |
if (imagesize % w == 0) {
|
|
roentgen |
b75cab |
scanlinesize = w * depth;
|
|
roentgen |
b75cab |
buf1 = _TIFFmalloc(scanlinesize);
|
|
roentgen |
b75cab |
buf2 = _TIFFmalloc(scanlinesize);
|
|
roentgen |
b75cab |
h = imagesize / w;
|
|
roentgen |
b75cab |
lseek(fd, hdr_size + (int)(h/2)*scanlinesize,
|
|
roentgen |
b75cab |
SEEK_SET);
|
|
roentgen |
b75cab |
read(fd, buf1, scanlinesize);
|
|
roentgen |
b75cab |
read(fd, buf2, scanlinesize);
|
|
roentgen |
b75cab |
if (swab) {
|
|
roentgen |
b75cab |
swapBytesInScanline(buf1, w, dtype);
|
|
roentgen |
b75cab |
swapBytesInScanline(buf2, w, dtype);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
tmp = (float) fabs(correlation(buf1, buf2,
|
|
roentgen |
b75cab |
w, dtype));
|
|
roentgen |
b75cab |
if (tmp > cor_coef) {
|
|
roentgen |
b75cab |
cor_coef = tmp;
|
|
roentgen |
b75cab |
*width = w, *length = h;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
_TIFFfree(buf1);
|
|
roentgen |
b75cab |
_TIFFfree(buf2);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
fprintf(stderr,
|
|
roentgen |
b75cab |
"Width is guessed as %lu, height is guessed as %lu.\n",
|
|
roentgen |
b75cab |
(unsigned long)*width, (unsigned long)*length);
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
return 1;
|
|
roentgen |
b75cab |
} else {
|
|
roentgen |
b75cab |
if (filestat.st_size<(off_t)(hdr_size+(*width)*(*length)*nbands*depth)) {
|
|
roentgen |
b75cab |
fprintf(stderr, "Input file too small.\n");
|
|
roentgen |
b75cab |
return -1;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
return 1;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
/* Calculate correlation coefficient between two numeric vectors */
|
|
roentgen |
b75cab |
static double
|
|
roentgen |
b75cab |
correlation(void *buf1, void *buf2, uint32 n_elem, TIFFDataType dtype)
|
|
roentgen |
b75cab |
{
|
|
roentgen |
b75cab |
double X, Y, M1 = 0.0, M2 = 0.0, D1 = 0.0, D2 = 0.0, K = 0.0;
|
|
roentgen |
b75cab |
uint32 i;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
switch (dtype) {
|
|
roentgen |
b75cab |
case TIFF_BYTE:
|
|
roentgen |
b75cab |
default:
|
|
roentgen |
b75cab |
for (i = 0; i < n_elem; i++) {
|
|
roentgen |
b75cab |
X = ((unsigned char *)buf1)[i];
|
|
roentgen |
b75cab |
Y = ((unsigned char *)buf2)[i];
|
|
roentgen |
b75cab |
M1 += X, M2 += Y;
|
|
roentgen |
b75cab |
D1 += X * X, D2 += Y * Y;
|
|
roentgen |
b75cab |
K += X * Y;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case TIFF_SBYTE:
|
|
roentgen |
b75cab |
for (i = 0; i < n_elem; i++) {
|
|
roentgen |
b75cab |
X = ((signed char *)buf1)[i];
|
|
roentgen |
b75cab |
Y = ((signed char *)buf2)[i];
|
|
roentgen |
b75cab |
M1 += X, M2 += Y;
|
|
roentgen |
b75cab |
D1 += X * X, D2 += Y * Y;
|
|
roentgen |
b75cab |
K += X * Y;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case TIFF_SHORT:
|
|
roentgen |
b75cab |
for (i = 0; i < n_elem; i++) {
|
|
roentgen |
b75cab |
X = ((uint16 *)buf1)[i];
|
|
roentgen |
b75cab |
Y = ((uint16 *)buf2)[i];
|
|
roentgen |
b75cab |
M1 += X, M2 += Y;
|
|
roentgen |
b75cab |
D1 += X * X, D2 += Y * Y;
|
|
roentgen |
b75cab |
K += X * Y;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case TIFF_SSHORT:
|
|
roentgen |
b75cab |
for (i = 0; i < n_elem; i++) {
|
|
roentgen |
b75cab |
X = ((int16 *)buf1)[i];
|
|
roentgen |
b75cab |
Y = ((int16 *)buf2)[i];
|
|
roentgen |
b75cab |
M1 += X, M2 += Y;
|
|
roentgen |
b75cab |
D1 += X * X, D2 += Y * Y;
|
|
roentgen |
b75cab |
K += X * Y;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case TIFF_LONG:
|
|
roentgen |
b75cab |
for (i = 0; i < n_elem; i++) {
|
|
roentgen |
b75cab |
X = ((uint32 *)buf1)[i];
|
|
roentgen |
b75cab |
Y = ((uint32 *)buf2)[i];
|
|
roentgen |
b75cab |
M1 += X, M2 += Y;
|
|
roentgen |
b75cab |
D1 += X * X, D2 += Y * Y;
|
|
roentgen |
b75cab |
K += X * Y;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case TIFF_SLONG:
|
|
roentgen |
b75cab |
for (i = 0; i < n_elem; i++) {
|
|
roentgen |
b75cab |
X = ((int32 *)buf1)[i];
|
|
roentgen |
b75cab |
Y = ((int32 *)buf2)[i];
|
|
roentgen |
b75cab |
M1 += X, M2 += Y;
|
|
roentgen |
b75cab |
D1 += X * X, D2 += Y * Y;
|
|
roentgen |
b75cab |
K += X * Y;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case TIFF_FLOAT:
|
|
roentgen |
b75cab |
for (i = 0; i < n_elem; i++) {
|
|
roentgen |
b75cab |
X = ((float *)buf1)[i];
|
|
roentgen |
b75cab |
Y = ((float *)buf2)[i];
|
|
roentgen |
b75cab |
M1 += X, M2 += Y;
|
|
roentgen |
b75cab |
D1 += X * X, D2 += Y * Y;
|
|
roentgen |
b75cab |
K += X * Y;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case TIFF_DOUBLE:
|
|
roentgen |
b75cab |
for (i = 0; i < n_elem; i++) {
|
|
roentgen |
b75cab |
X = ((double *)buf1)[i];
|
|
roentgen |
b75cab |
Y = ((double *)buf2)[i];
|
|
roentgen |
b75cab |
M1 += X, M2 += Y;
|
|
roentgen |
b75cab |
D1 += X * X, D2 += Y * Y;
|
|
roentgen |
b75cab |
K += X * Y;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
M1 /= n_elem;
|
|
roentgen |
b75cab |
M2 /= n_elem;
|
|
roentgen |
b75cab |
D1 -= M1 * M1 * n_elem;
|
|
roentgen |
b75cab |
D2 -= M2 * M2 * n_elem;
|
|
roentgen |
b75cab |
K = (K - M1 * M2 * n_elem) / sqrt(D1 * D2);
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
return K;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
static int
|
|
roentgen |
b75cab |
processCompressOptions(char* opt)
|
|
roentgen |
b75cab |
{
|
|
roentgen |
b75cab |
if (strcmp(opt, "none") == 0)
|
|
roentgen |
b75cab |
compression = COMPRESSION_NONE;
|
|
roentgen |
b75cab |
else if (strcmp(opt, "packbits") == 0)
|
|
roentgen |
b75cab |
compression = COMPRESSION_PACKBITS;
|
|
roentgen |
b75cab |
else if (strncmp(opt, "jpeg", 4) == 0) {
|
|
roentgen |
b75cab |
char* cp = strchr(opt, ':');
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
compression = COMPRESSION_JPEG;
|
|
roentgen |
b75cab |
while( cp )
|
|
roentgen |
b75cab |
{
|
|
roentgen |
b75cab |
if (isdigit((int)cp[1]))
|
|
roentgen |
b75cab |
quality = atoi(cp+1);
|
|
roentgen |
b75cab |
else if (cp[1] == 'r' )
|
|
roentgen |
b75cab |
jpegcolormode = JPEGCOLORMODE_RAW;
|
|
roentgen |
b75cab |
else
|
|
roentgen |
b75cab |
usage();
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
cp = strchr(cp+1,':');
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
} else if (strncmp(opt, "lzw", 3) == 0) {
|
|
roentgen |
b75cab |
char* cp = strchr(opt, ':');
|
|
roentgen |
b75cab |
if (cp)
|
|
roentgen |
b75cab |
predictor = atoi(cp+1);
|
|
roentgen |
b75cab |
compression = COMPRESSION_LZW;
|
|
roentgen |
b75cab |
} else if (strncmp(opt, "zip", 3) == 0) {
|
|
roentgen |
b75cab |
char* cp = strchr(opt, ':');
|
|
roentgen |
b75cab |
if (cp)
|
|
roentgen |
b75cab |
predictor = atoi(cp+1);
|
|
roentgen |
b75cab |
compression = COMPRESSION_DEFLATE;
|
|
roentgen |
b75cab |
} else
|
|
roentgen |
b75cab |
return (0);
|
|
roentgen |
b75cab |
return (1);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
static char* stuff[] = {
|
|
roentgen |
b75cab |
"raw2tiff --- tool for converting raw byte sequences in TIFF images",
|
|
roentgen |
b75cab |
"usage: raw2tiff [options] input.raw output.tif",
|
|
roentgen |
b75cab |
"where options are:",
|
|
roentgen |
b75cab |
" -L input data has LSB2MSB bit order (default)",
|
|
roentgen |
b75cab |
" -M input data has MSB2LSB bit order",
|
|
roentgen |
b75cab |
" -r # make each strip have no more than # rows",
|
|
roentgen |
b75cab |
" -H # size of input image file header in bytes (0 by default)",
|
|
roentgen |
b75cab |
" -w # width of input image in pixels",
|
|
roentgen |
b75cab |
" -l # length of input image in lines",
|
|
roentgen |
b75cab |
" -b # number of bands in input image (1 by default)",
|
|
roentgen |
b75cab |
"",
|
|
roentgen |
b75cab |
" -d data_type type of samples in input image",
|
|
roentgen |
b75cab |
"where data_type may be:",
|
|
roentgen |
b75cab |
" byte 8-bit unsigned integer (default)",
|
|
roentgen |
b75cab |
" short 16-bit unsigned integer",
|
|
roentgen |
b75cab |
" long 32-bit unsigned integer",
|
|
roentgen |
b75cab |
" sbyte 8-bit signed integer",
|
|
roentgen |
b75cab |
" sshort 16-bit signed integer",
|
|
roentgen |
b75cab |
" slong 32-bit signed integer",
|
|
roentgen |
b75cab |
" float 32-bit IEEE floating point",
|
|
roentgen |
b75cab |
" double 64-bit IEEE floating point",
|
|
roentgen |
b75cab |
"",
|
|
roentgen |
b75cab |
" -p photo photometric interpretation (color space) of the input image",
|
|
roentgen |
b75cab |
"where photo may be:",
|
|
roentgen |
b75cab |
" miniswhite white color represented with 0 value",
|
|
roentgen |
b75cab |
" minisblack black color represented with 0 value (default)",
|
|
roentgen |
b75cab |
" rgb image has RGB color model",
|
|
roentgen |
b75cab |
" cmyk image has CMYK (separated) color model",
|
|
roentgen |
b75cab |
" ycbcr image has YCbCr color model",
|
|
roentgen |
b75cab |
" cielab image has CIE L*a*b color model",
|
|
roentgen |
b75cab |
" icclab image has ICC L*a*b color model",
|
|
roentgen |
b75cab |
" itulab image has ITU L*a*b color model",
|
|
roentgen |
b75cab |
"",
|
|
roentgen |
b75cab |
" -s swap bytes fetched from input file",
|
|
roentgen |
b75cab |
"",
|
|
roentgen |
b75cab |
" -i config type of samples interleaving in input image",
|
|
roentgen |
b75cab |
"where config may be:",
|
|
roentgen |
b75cab |
" pixel pixel interleaved data (default)",
|
|
roentgen |
b75cab |
" band band interleaved data",
|
|
roentgen |
b75cab |
"",
|
|
roentgen |
b75cab |
" -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding",
|
|
roentgen |
b75cab |
" -c zip[:opts] compress output with deflate 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 |
"For example, -c jpeg:r:50 to get JPEG-encoded RGB data with 50% comp. quality",
|
|
roentgen |
b75cab |
"",
|
|
roentgen |
b75cab |
"LZW and deflate options:",
|
|
roentgen |
b75cab |
" # set predictor value",
|
|
roentgen |
b75cab |
"For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
|
|
roentgen |
b75cab |
" -o out.tif write output to out.tif",
|
|
roentgen |
b75cab |
" -h this help message",
|
|
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 |
fprintf(stderr, "%s\n\n", TIFFGetVersion());
|
|
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 |
/* vim: set ts=8 sts=8 sw=8 noet: */
|
|
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 |
*/
|