2017-01-14 08:12:33 -05:00
|
|
|
/* $Id: raw2tiff.c,v 1.29 2017-01-14 13:12:33 erouault Exp $
|
2002-08-10 11:25:54 -04:00
|
|
|
*
|
|
|
|
* Project: libtiff tools
|
|
|
|
* Purpose: Convert raw byte sequences in TIFF images
|
2006-03-23 09:54:00 -05:00
|
|
|
* Author: Andrey Kiselev, dron@ak4719.spb.edu
|
2002-08-10 11:25:54 -04:00
|
|
|
*
|
|
|
|
******************************************************************************
|
2006-03-23 09:54:00 -05:00
|
|
|
* Copyright (c) 2002, Andrey Kiselev <dron@ak4719.spb.edu>
|
2002-08-10 11:25:54 -04:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, distribute, and sell this software and
|
|
|
|
* its documentation for any purpose is hereby granted without fee, provided
|
|
|
|
* that (i) the above copyright notices and this permission notice appear in
|
|
|
|
* all copies of the software and related documentation, and (ii) the names of
|
|
|
|
* Sam Leffler and Silicon Graphics may not be used in any advertising or
|
|
|
|
* publicity relating to the software without the specific, prior written
|
|
|
|
* permission of Sam Leffler and Silicon Graphics.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
|
|
|
|
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*
|
|
|
|
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
|
|
|
|
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
|
|
|
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
|
|
|
|
* OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2004-09-03 03:55:34 -04:00
|
|
|
#include "tif_config.h"
|
|
|
|
|
2002-08-10 11:25:54 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
2004-06-29 15:19:17 -04:00
|
|
|
#include <sys/types.h>
|
2003-11-11 10:39:33 -05:00
|
|
|
#include <math.h>
|
|
|
|
#include <ctype.h>
|
2002-08-10 11:25:54 -04:00
|
|
|
|
2004-09-03 03:55:34 -04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2004-10-28 10:35:50 -04:00
|
|
|
#if HAVE_FCNTL_H
|
|
|
|
# include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAVE_SYS_TYPES_H
|
|
|
|
# include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAVE_IO_H
|
|
|
|
# include <io.h>
|
|
|
|
#endif
|
|
|
|
|
2009-01-22 15:53:07 -05:00
|
|
|
#ifdef NEED_LIBPORT
|
|
|
|
# include "libport.h"
|
|
|
|
#endif
|
|
|
|
|
2015-08-18 22:31:04 -04:00
|
|
|
#include "tiffiop.h"
|
2002-08-10 11:25:54 -04:00
|
|
|
#include "tiffio.h"
|
|
|
|
|
2004-11-28 09:44:31 -05:00
|
|
|
#ifndef HAVE_GETOPT
|
|
|
|
extern int getopt(int, char**, char*);
|
|
|
|
#endif
|
|
|
|
|
2004-10-28 10:35:50 -04:00
|
|
|
#ifndef O_BINARY
|
|
|
|
# define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
2003-11-11 10:39:33 -05:00
|
|
|
typedef enum {
|
|
|
|
PIXEL,
|
|
|
|
BAND
|
|
|
|
} InterleavingType;
|
|
|
|
|
2002-08-10 11:25:54 -04:00
|
|
|
static uint16 compression = (uint16) -1;
|
|
|
|
static int jpegcolormode = JPEGCOLORMODE_RGB;
|
|
|
|
static int quality = 75; /* JPEG quality */
|
|
|
|
static uint16 predictor = 0;
|
|
|
|
|
2003-11-11 10:39:33 -05:00
|
|
|
static void swapBytesInScanline(void *, uint32, TIFFDataType);
|
2015-08-18 22:31:04 -04:00
|
|
|
static int guessSize(int, TIFFDataType, _TIFF_off_t, uint32, int,
|
2003-11-12 04:19:53 -05:00
|
|
|
uint32 *, uint32 *);
|
2003-11-11 10:39:33 -05:00
|
|
|
static double correlation(void *, void *, uint32, TIFFDataType);
|
2002-08-10 11:25:54 -04:00
|
|
|
static void usage(void);
|
|
|
|
static int processCompressOptions(char*);
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
2004-06-04 09:46:25 -04:00
|
|
|
uint32 width = 0, length = 0, linebytes, bufsize;
|
|
|
|
uint32 nbands = 1; /* number of bands in input image*/
|
2015-08-18 22:31:04 -04:00
|
|
|
_TIFF_off_t hdr_size = 0; /* size of the header to skip */
|
2002-08-10 11:25:54 -04:00
|
|
|
TIFFDataType dtype = TIFF_BYTE;
|
2004-10-28 10:35:50 -04:00
|
|
|
int16 depth = 1; /* bytes per pixel in input image */
|
2003-11-11 10:39:33 -05:00
|
|
|
int swab = 0; /* byte swapping flag */
|
|
|
|
InterleavingType interleaving = 0; /* interleaving type flag */
|
2004-06-29 15:19:17 -04:00
|
|
|
uint32 rowsperstrip = (uint32) -1;
|
2002-08-10 11:25:54 -04:00
|
|
|
uint16 photometric = PHOTOMETRIC_MINISBLACK;
|
|
|
|
uint16 config = PLANARCONFIG_CONTIG;
|
|
|
|
uint16 fillorder = FILLORDER_LSB2MSB;
|
2004-10-28 10:35:50 -04:00
|
|
|
int fd;
|
2002-08-10 11:25:54 -04:00
|
|
|
char *outfilename = NULL;
|
|
|
|
TIFF *out;
|
2004-06-08 05:56:35 -04:00
|
|
|
|
2002-08-12 11:32:37 -04:00
|
|
|
uint32 row, col, band;
|
2002-08-10 11:25:54 -04:00
|
|
|
int c;
|
2003-02-04 03:51:38 -05:00
|
|
|
unsigned char *buf = NULL, *buf1 = NULL;
|
2015-06-20 21:09:09 -04:00
|
|
|
#if !HAVE_DECL_OPTARG
|
2002-08-10 11:25:54 -04:00
|
|
|
extern int optind;
|
|
|
|
extern char* optarg;
|
2015-06-20 21:09:09 -04:00
|
|
|
#endif
|
2002-08-10 11:25:54 -04:00
|
|
|
|
2004-06-08 05:56:35 -04:00
|
|
|
while ((c = getopt(argc, argv, "c:r:H:w:l:b:d:LMp:si:o:h")) != -1) {
|
2002-08-10 11:25:54 -04:00
|
|
|
switch (c) {
|
|
|
|
case 'c': /* compression scheme */
|
|
|
|
if (!processCompressOptions(optarg))
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
case 'r': /* rows/strip */
|
|
|
|
rowsperstrip = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'H': /* size of input image file header */
|
|
|
|
hdr_size = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'w': /* input image width */
|
|
|
|
width = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'l': /* input image length */
|
|
|
|
length = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'b': /* number of bands in input image */
|
|
|
|
nbands = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'd': /* type of samples in input image */
|
|
|
|
if (strncmp(optarg, "byte", 4) == 0)
|
|
|
|
dtype = TIFF_BYTE;
|
|
|
|
else if (strncmp(optarg, "short", 5) == 0)
|
|
|
|
dtype = TIFF_SHORT;
|
|
|
|
else if (strncmp(optarg, "long", 4) == 0)
|
|
|
|
dtype = TIFF_LONG;
|
|
|
|
else if (strncmp(optarg, "sbyte", 5) == 0)
|
|
|
|
dtype = TIFF_SBYTE;
|
|
|
|
else if (strncmp(optarg, "sshort", 6) == 0)
|
|
|
|
dtype = TIFF_SSHORT;
|
|
|
|
else if (strncmp(optarg, "slong", 5) == 0)
|
|
|
|
dtype = TIFF_SLONG;
|
|
|
|
else if (strncmp(optarg, "float", 5) == 0)
|
|
|
|
dtype = TIFF_FLOAT;
|
|
|
|
else if (strncmp(optarg, "double", 6) == 0)
|
|
|
|
dtype = TIFF_DOUBLE;
|
|
|
|
else
|
|
|
|
dtype = TIFF_BYTE;
|
|
|
|
depth = TIFFDataWidth(dtype);
|
|
|
|
break;
|
|
|
|
case 'L': /* input has lsb-to-msb fillorder */
|
|
|
|
fillorder = FILLORDER_LSB2MSB;
|
|
|
|
break;
|
|
|
|
case 'M': /* input has msb-to-lsb fillorder */
|
|
|
|
fillorder = FILLORDER_MSB2LSB;
|
2003-07-10 16:04:42 -04:00
|
|
|
break;
|
|
|
|
case 'p': /* photometric interpretation */
|
|
|
|
if (strncmp(optarg, "miniswhite", 10) == 0)
|
|
|
|
photometric = PHOTOMETRIC_MINISWHITE;
|
|
|
|
else if (strncmp(optarg, "minisblack", 10) == 0)
|
|
|
|
photometric = PHOTOMETRIC_MINISBLACK;
|
|
|
|
else if (strncmp(optarg, "rgb", 3) == 0)
|
|
|
|
photometric = PHOTOMETRIC_RGB;
|
|
|
|
else if (strncmp(optarg, "cmyk", 4) == 0)
|
|
|
|
photometric = PHOTOMETRIC_SEPARATED;
|
|
|
|
else if (strncmp(optarg, "ycbcr", 5) == 0)
|
|
|
|
photometric = PHOTOMETRIC_YCBCR;
|
|
|
|
else if (strncmp(optarg, "cielab", 6) == 0)
|
|
|
|
photometric = PHOTOMETRIC_CIELAB;
|
|
|
|
else if (strncmp(optarg, "icclab", 6) == 0)
|
|
|
|
photometric = PHOTOMETRIC_ICCLAB;
|
|
|
|
else if (strncmp(optarg, "itulab", 6) == 0)
|
|
|
|
photometric = PHOTOMETRIC_ITULAB;
|
|
|
|
else
|
|
|
|
photometric = PHOTOMETRIC_MINISBLACK;
|
|
|
|
break;
|
2002-08-10 11:25:54 -04:00
|
|
|
case 's': /* do we need to swap bytes? */
|
|
|
|
swab = 1;
|
|
|
|
break;
|
2002-08-12 11:32:37 -04:00
|
|
|
case 'i': /* type of interleaving */
|
|
|
|
if (strncmp(optarg, "pixel", 4) == 0)
|
2003-11-11 10:39:33 -05:00
|
|
|
interleaving = PIXEL;
|
2002-08-12 11:32:37 -04:00
|
|
|
else if (strncmp(optarg, "band", 6) == 0)
|
2003-11-11 10:39:33 -05:00
|
|
|
interleaving = BAND;
|
2002-08-12 11:32:37 -04:00
|
|
|
else
|
|
|
|
interleaving = 0;
|
|
|
|
break;
|
2002-08-10 11:25:54 -04:00
|
|
|
case 'o':
|
|
|
|
outfilename = optarg;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
usage();
|
2002-08-12 11:32:37 -04:00
|
|
|
default:
|
|
|
|
break;
|
2002-08-10 11:25:54 -04:00
|
|
|
}
|
2004-06-08 05:56:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argc - optind < 2)
|
2002-08-10 11:25:54 -04:00
|
|
|
usage();
|
2004-06-08 05:56:35 -04:00
|
|
|
|
2004-10-28 10:35:50 -04:00
|
|
|
fd = open(argv[optind], O_RDONLY|O_BINARY, 0);
|
|
|
|
if (fd < 0) {
|
2002-08-10 11:25:54 -04:00
|
|
|
fprintf(stderr, "%s: %s: Cannot open input file.\n",
|
|
|
|
argv[0], argv[optind]);
|
|
|
|
return (-1);
|
|
|
|
}
|
2003-11-11 10:39:33 -05:00
|
|
|
|
2004-10-28 10:35:50 -04:00
|
|
|
if (guessSize(fd, dtype, hdr_size, nbands, swab, &width, &length) < 0)
|
2003-11-11 10:39:33 -05:00
|
|
|
return 1;
|
|
|
|
|
2002-08-10 11:25:54 -04:00
|
|
|
if (outfilename == NULL)
|
|
|
|
outfilename = argv[optind+1];
|
|
|
|
out = TIFFOpen(outfilename, "w");
|
|
|
|
if (out == NULL) {
|
2002-08-12 11:32:37 -04:00
|
|
|
fprintf(stderr, "%s: %s: Cannot open file for output.\n",
|
2002-08-10 11:25:54 -04:00
|
|
|
argv[0], outfilename);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);
|
|
|
|
TIFFSetField(out, TIFFTAG_IMAGELENGTH, length);
|
|
|
|
TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
|
|
|
|
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, nbands);
|
|
|
|
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, depth * 8);
|
|
|
|
TIFFSetField(out, TIFFTAG_FILLORDER, fillorder);
|
|
|
|
TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);
|
2003-07-10 16:04:42 -04:00
|
|
|
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric);
|
2002-08-12 11:32:37 -04:00
|
|
|
switch (dtype) {
|
2002-08-10 11:25:54 -04:00
|
|
|
case TIFF_BYTE:
|
|
|
|
case TIFF_SHORT:
|
|
|
|
case TIFF_LONG:
|
|
|
|
TIFFSetField(out, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
|
|
|
|
break;
|
|
|
|
case TIFF_SBYTE:
|
|
|
|
case TIFF_SSHORT:
|
|
|
|
case TIFF_SLONG:
|
|
|
|
TIFFSetField(out, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
|
|
|
|
break;
|
|
|
|
case TIFF_FLOAT:
|
|
|
|
case TIFF_DOUBLE:
|
|
|
|
TIFFSetField(out, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
TIFFSetField(out, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_VOID);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (compression == (uint16) -1)
|
|
|
|
compression = COMPRESSION_PACKBITS;
|
|
|
|
TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
|
|
|
|
switch (compression) {
|
|
|
|
case COMPRESSION_JPEG:
|
2003-11-11 10:39:33 -05:00
|
|
|
if (photometric == PHOTOMETRIC_RGB
|
|
|
|
&& jpegcolormode == JPEGCOLORMODE_RGB)
|
2002-08-10 11:25:54 -04:00
|
|
|
photometric = PHOTOMETRIC_YCBCR;
|
|
|
|
TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality);
|
|
|
|
TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode);
|
|
|
|
break;
|
|
|
|
case COMPRESSION_LZW:
|
|
|
|
case COMPRESSION_DEFLATE:
|
|
|
|
if (predictor != 0)
|
|
|
|
TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);
|
|
|
|
break;
|
|
|
|
}
|
2002-08-12 11:32:37 -04:00
|
|
|
switch(interleaving) {
|
2003-11-11 10:39:33 -05:00
|
|
|
case BAND: /* band interleaved data */
|
2002-08-12 11:32:37 -04:00
|
|
|
linebytes = width * depth;
|
|
|
|
buf = (unsigned char *)_TIFFmalloc(linebytes);
|
|
|
|
break;
|
2003-11-11 10:39:33 -05:00
|
|
|
case PIXEL: /* pixel interleaved data */
|
2002-08-12 11:32:37 -04:00
|
|
|
default:
|
|
|
|
linebytes = width * nbands * depth;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bufsize = width * nbands * depth;
|
|
|
|
buf1 = (unsigned char *)_TIFFmalloc(bufsize);
|
2006-03-15 07:48:24 -05:00
|
|
|
|
|
|
|
rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip);
|
|
|
|
if (rowsperstrip > length) {
|
|
|
|
rowsperstrip = length;
|
|
|
|
}
|
|
|
|
TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip );
|
|
|
|
|
2015-08-18 22:31:04 -04:00
|
|
|
_TIFF_lseek_f(fd, hdr_size, SEEK_SET); /* Skip the file header */
|
2002-08-10 11:25:54 -04:00
|
|
|
for (row = 0; row < length; row++) {
|
2002-08-12 11:32:37 -04:00
|
|
|
switch(interleaving) {
|
2003-11-11 10:39:33 -05:00
|
|
|
case BAND: /* band interleaved data */
|
2002-08-12 11:32:37 -04:00
|
|
|
for (band = 0; band < nbands; band++) {
|
2015-08-18 22:31:04 -04:00
|
|
|
if (_TIFF_lseek_f(fd,
|
2015-05-30 12:13:29 -04:00
|
|
|
hdr_size + (length*band+row)*linebytes,
|
2015-08-18 22:31:04 -04:00
|
|
|
SEEK_SET) == (_TIFF_off_t)-1) {
|
2015-05-30 12:13:29 -04:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: %s: scanline %lu: seek error.\n",
|
|
|
|
argv[0], argv[optind],
|
|
|
|
(unsigned long) row);
|
|
|
|
break;
|
|
|
|
}
|
2004-11-28 09:44:31 -05:00
|
|
|
if (read(fd, buf, linebytes) < 0) {
|
2002-08-12 11:32:37 -04:00
|
|
|
fprintf(stderr,
|
2015-05-30 12:13:29 -04:00
|
|
|
"%s: %s: scanline %lu: Read error.\n",
|
|
|
|
argv[0], argv[optind],
|
|
|
|
(unsigned long) row);
|
|
|
|
break;
|
2002-08-12 11:32:37 -04:00
|
|
|
}
|
|
|
|
if (swab) /* Swap bytes if needed */
|
|
|
|
swapBytesInScanline(buf, width, dtype);
|
|
|
|
for (col = 0; col < width; col++)
|
2003-11-11 10:39:33 -05:00
|
|
|
memcpy(buf1 + (col*nbands+band)*depth,
|
|
|
|
buf + col * depth, depth);
|
2002-08-12 11:32:37 -04:00
|
|
|
}
|
|
|
|
break;
|
2003-11-11 10:39:33 -05:00
|
|
|
case PIXEL: /* pixel interleaved data */
|
2002-08-12 11:32:37 -04:00
|
|
|
default:
|
2004-11-28 09:44:31 -05:00
|
|
|
if (read(fd, buf1, bufsize) < 0) {
|
2003-11-11 10:39:33 -05:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: %s: scanline %lu: Read error.\n",
|
|
|
|
argv[0], argv[optind],
|
|
|
|
(unsigned long) row);
|
2002-08-10 11:25:54 -04:00
|
|
|
break;
|
|
|
|
}
|
2002-08-12 11:32:37 -04:00
|
|
|
if (swab) /* Swap bytes if needed */
|
|
|
|
swapBytesInScanline(buf1, width, dtype);
|
|
|
|
break;
|
|
|
|
}
|
2002-08-10 11:25:54 -04:00
|
|
|
|
2002-08-12 11:32:37 -04:00
|
|
|
if (TIFFWriteScanline(out, buf1, row, 0) < 0) {
|
|
|
|
fprintf(stderr, "%s: %s: scanline %lu: Write error.\n",
|
2003-11-11 10:39:33 -05:00
|
|
|
argv[0], outfilename, (unsigned long) row);
|
2002-08-10 11:25:54 -04:00
|
|
|
break;
|
2002-08-12 11:32:37 -04:00
|
|
|
}
|
2002-08-10 11:25:54 -04:00
|
|
|
}
|
2003-02-04 03:51:38 -05:00
|
|
|
if (buf)
|
|
|
|
_TIFFfree(buf);
|
|
|
|
if (buf1)
|
|
|
|
_TIFFfree(buf1);
|
|
|
|
TIFFClose(out);
|
2002-08-10 11:25:54 -04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2002-08-12 11:32:37 -04:00
|
|
|
static void
|
2003-11-11 10:39:33 -05:00
|
|
|
swapBytesInScanline(void *buf, uint32 width, TIFFDataType dtype)
|
2002-08-12 11:32:37 -04:00
|
|
|
{
|
2003-11-11 10:39:33 -05:00
|
|
|
switch (dtype) {
|
|
|
|
case TIFF_SHORT:
|
|
|
|
case TIFF_SSHORT:
|
2004-06-08 05:56:35 -04:00
|
|
|
TIFFSwabArrayOfShort((uint16*)buf,
|
|
|
|
(unsigned long)width);
|
2003-11-11 10:39:33 -05:00
|
|
|
break;
|
|
|
|
case TIFF_LONG:
|
|
|
|
case TIFF_SLONG:
|
2004-06-08 05:56:35 -04:00
|
|
|
TIFFSwabArrayOfLong((uint32*)buf,
|
|
|
|
(unsigned long)width);
|
2003-11-11 10:39:33 -05:00
|
|
|
break;
|
|
|
|
/* case TIFF_FLOAT: */ /* FIXME */
|
|
|
|
case TIFF_DOUBLE:
|
2004-06-08 05:56:35 -04:00
|
|
|
TIFFSwabArrayOfDouble((double*)buf,
|
|
|
|
(unsigned long)width);
|
2003-11-11 10:39:33 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-08-18 22:31:04 -04:00
|
|
|
guessSize(int fd, TIFFDataType dtype, _TIFF_off_t hdr_size, uint32 nbands,
|
2004-06-04 09:46:25 -04:00
|
|
|
int swab, uint32 *width, uint32 *length)
|
2003-11-11 10:39:33 -05:00
|
|
|
{
|
|
|
|
const float longt = 40.0; /* maximum possible height/width ratio */
|
|
|
|
char *buf1, *buf2;
|
2015-08-18 22:31:04 -04:00
|
|
|
_TIFF_stat_s filestat;
|
2003-11-11 10:39:33 -05:00
|
|
|
uint32 w, h, scanlinesize, imagesize;
|
2004-06-04 09:46:25 -04:00
|
|
|
uint32 depth = TIFFDataWidth(dtype);
|
2003-11-11 10:39:33 -05:00
|
|
|
float cor_coef = 0, tmp;
|
|
|
|
|
2015-08-18 22:31:04 -04:00
|
|
|
if (_TIFF_fstat_f(fd, &filestat) == -1) {
|
2015-05-30 12:13:29 -04:00
|
|
|
fprintf(stderr, "Failed to obtain file size.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2003-11-11 10:39:33 -05:00
|
|
|
|
|
|
|
if (filestat.st_size < hdr_size) {
|
|
|
|
fprintf(stderr, "Too large header size specified.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
imagesize = (filestat.st_size - hdr_size) / nbands / depth;
|
|
|
|
|
|
|
|
if (*width != 0 && *length == 0) {
|
|
|
|
fprintf(stderr, "Image height is not specified.\n");
|
|
|
|
|
|
|
|
*length = imagesize / *width;
|
|
|
|
|
2005-09-13 09:17:57 -04:00
|
|
|
fprintf(stderr, "Height is guessed as %lu.\n",
|
|
|
|
(unsigned long)*length);
|
2003-11-11 10:39:33 -05:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
} else if (*width == 0 && *length != 0) {
|
|
|
|
fprintf(stderr, "Image width is not specified.\n");
|
|
|
|
|
|
|
|
*width = imagesize / *length;
|
|
|
|
|
2005-09-13 09:17:57 -04:00
|
|
|
fprintf(stderr, "Width is guessed as %lu.\n",
|
|
|
|
(unsigned long)*width);
|
2003-11-11 10:39:33 -05:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
} else if (*width == 0 && *length == 0) {
|
2015-05-30 12:13:29 -04:00
|
|
|
unsigned int fail = 0;
|
2003-11-11 10:39:33 -05:00
|
|
|
fprintf(stderr, "Image width and height are not specified.\n");
|
2017-01-14 08:12:33 -05:00
|
|
|
w = (uint32) sqrt(imagesize / longt);
|
|
|
|
if( w == 0 )
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Too small image size.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;
|
2003-11-22 03:42:06 -05:00
|
|
|
w < sqrt(imagesize * longt);
|
2003-11-11 10:39:33 -05:00
|
|
|
w++) {
|
|
|
|
if (imagesize % w == 0) {
|
|
|
|
scanlinesize = w * depth;
|
|
|
|
buf1 = _TIFFmalloc(scanlinesize);
|
|
|
|
buf2 = _TIFFmalloc(scanlinesize);
|
|
|
|
h = imagesize / w;
|
2015-05-30 12:13:29 -04:00
|
|
|
do {
|
2015-08-18 22:31:04 -04:00
|
|
|
if (_TIFF_lseek_f(fd, hdr_size + (int)(h/2)*scanlinesize,
|
|
|
|
SEEK_SET) == (_TIFF_off_t)-1) {
|
2015-05-30 12:13:29 -04:00
|
|
|
fprintf(stderr, "seek error.\n");
|
|
|
|
fail=1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (read(fd, buf1, scanlinesize) !=
|
|
|
|
(long) scanlinesize) {
|
|
|
|
fprintf(stderr, "read error.\n");
|
|
|
|
fail=1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (read(fd, buf2, scanlinesize) !=
|
|
|
|
(long) scanlinesize) {
|
|
|
|
fprintf(stderr, "read error.\n");
|
|
|
|
fail=1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (swab) {
|
|
|
|
swapBytesInScanline(buf1, w, dtype);
|
|
|
|
swapBytesInScanline(buf2, w, dtype);
|
|
|
|
}
|
|
|
|
tmp = (float) fabs(correlation(buf1, buf2,
|
|
|
|
w, dtype));
|
|
|
|
if (tmp > cor_coef) {
|
|
|
|
cor_coef = tmp;
|
|
|
|
*width = w, *length = h;
|
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
_TIFFfree(buf1);
|
2003-11-11 10:39:33 -05:00
|
|
|
_TIFFfree(buf2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-30 12:13:29 -04:00
|
|
|
if (fail) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-11-11 10:39:33 -05:00
|
|
|
fprintf(stderr,
|
2004-09-03 03:55:34 -04:00
|
|
|
"Width is guessed as %lu, height is guessed as %lu.\n",
|
2005-09-13 09:17:57 -04:00
|
|
|
(unsigned long)*width, (unsigned long)*length);
|
2003-11-11 10:39:33 -05:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
} else {
|
2015-08-18 22:31:04 -04:00
|
|
|
if (filestat.st_size<(_TIFF_off_t)(hdr_size+(*width)*(*length)*nbands*depth)) {
|
2003-11-11 10:39:33 -05:00
|
|
|
fprintf(stderr, "Input file too small.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate correlation coefficient between two numeric vectors */
|
|
|
|
static double
|
|
|
|
correlation(void *buf1, void *buf2, uint32 n_elem, TIFFDataType dtype)
|
|
|
|
{
|
2004-06-04 09:46:25 -04:00
|
|
|
double X, Y, M1 = 0.0, M2 = 0.0, D1 = 0.0, D2 = 0.0, K = 0.0;
|
|
|
|
uint32 i;
|
2003-11-11 10:39:33 -05:00
|
|
|
|
|
|
|
switch (dtype) {
|
|
|
|
case TIFF_BYTE:
|
|
|
|
default:
|
|
|
|
for (i = 0; i < n_elem; i++) {
|
2004-06-29 15:19:17 -04:00
|
|
|
X = ((unsigned char *)buf1)[i];
|
|
|
|
Y = ((unsigned char *)buf2)[i];
|
2003-11-12 04:19:53 -05:00
|
|
|
M1 += X, M2 += Y;
|
|
|
|
D1 += X * X, D2 += Y * Y;
|
|
|
|
K += X * Y;
|
2003-11-11 10:39:33 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TIFF_SBYTE:
|
|
|
|
for (i = 0; i < n_elem; i++) {
|
2003-11-12 04:19:53 -05:00
|
|
|
X = ((signed char *)buf1)[i];
|
|
|
|
Y = ((signed char *)buf2)[i];
|
|
|
|
M1 += X, M2 += Y;
|
|
|
|
D1 += X * X, D2 += Y * Y;
|
|
|
|
K += X * Y;
|
2003-11-11 10:39:33 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TIFF_SHORT:
|
|
|
|
for (i = 0; i < n_elem; i++) {
|
2003-11-12 04:19:53 -05:00
|
|
|
X = ((uint16 *)buf1)[i];
|
|
|
|
Y = ((uint16 *)buf2)[i];
|
|
|
|
M1 += X, M2 += Y;
|
|
|
|
D1 += X * X, D2 += Y * Y;
|
|
|
|
K += X * Y;
|
2003-11-11 10:39:33 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TIFF_SSHORT:
|
|
|
|
for (i = 0; i < n_elem; i++) {
|
2003-11-12 04:19:53 -05:00
|
|
|
X = ((int16 *)buf1)[i];
|
|
|
|
Y = ((int16 *)buf2)[i];
|
|
|
|
M1 += X, M2 += Y;
|
|
|
|
D1 += X * X, D2 += Y * Y;
|
|
|
|
K += X * Y;
|
2003-11-11 10:39:33 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TIFF_LONG:
|
|
|
|
for (i = 0; i < n_elem; i++) {
|
2003-11-12 04:19:53 -05:00
|
|
|
X = ((uint32 *)buf1)[i];
|
|
|
|
Y = ((uint32 *)buf2)[i];
|
|
|
|
M1 += X, M2 += Y;
|
|
|
|
D1 += X * X, D2 += Y * Y;
|
|
|
|
K += X * Y;
|
2003-11-11 10:39:33 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TIFF_SLONG:
|
|
|
|
for (i = 0; i < n_elem; i++) {
|
2003-11-12 04:19:53 -05:00
|
|
|
X = ((int32 *)buf1)[i];
|
|
|
|
Y = ((int32 *)buf2)[i];
|
|
|
|
M1 += X, M2 += Y;
|
|
|
|
D1 += X * X, D2 += Y * Y;
|
|
|
|
K += X * Y;
|
2003-11-11 10:39:33 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TIFF_FLOAT:
|
|
|
|
for (i = 0; i < n_elem; i++) {
|
2003-11-12 04:19:53 -05:00
|
|
|
X = ((float *)buf1)[i];
|
|
|
|
Y = ((float *)buf2)[i];
|
|
|
|
M1 += X, M2 += Y;
|
|
|
|
D1 += X * X, D2 += Y * Y;
|
|
|
|
K += X * Y;
|
2003-11-11 10:39:33 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TIFF_DOUBLE:
|
|
|
|
for (i = 0; i < n_elem; i++) {
|
2003-11-12 04:19:53 -05:00
|
|
|
X = ((double *)buf1)[i];
|
|
|
|
Y = ((double *)buf2)[i];
|
|
|
|
M1 += X, M2 += Y;
|
|
|
|
D1 += X * X, D2 += Y * Y;
|
|
|
|
K += X * Y;
|
2003-11-11 10:39:33 -05:00
|
|
|
}
|
|
|
|
break;
|
2002-08-12 11:32:37 -04:00
|
|
|
}
|
2003-11-11 10:39:33 -05:00
|
|
|
|
2003-11-12 04:19:53 -05:00
|
|
|
M1 /= n_elem;
|
|
|
|
M2 /= n_elem;
|
|
|
|
D1 -= M1 * M1 * n_elem;
|
|
|
|
D2 -= M2 * M2 * n_elem;
|
|
|
|
K = (K - M1 * M2 * n_elem) / sqrt(D1 * D2);
|
|
|
|
|
2003-11-11 10:39:33 -05:00
|
|
|
return K;
|
2002-08-12 11:32:37 -04:00
|
|
|
}
|
|
|
|
|
2002-08-10 11:25:54 -04:00
|
|
|
static int
|
|
|
|
processCompressOptions(char* opt)
|
|
|
|
{
|
|
|
|
if (strcmp(opt, "none") == 0)
|
|
|
|
compression = COMPRESSION_NONE;
|
|
|
|
else if (strcmp(opt, "packbits") == 0)
|
|
|
|
compression = COMPRESSION_PACKBITS;
|
|
|
|
else if (strncmp(opt, "jpeg", 4) == 0) {
|
|
|
|
char* cp = strchr(opt, ':');
|
2006-01-11 11:59:35 -05:00
|
|
|
|
2006-01-11 12:03:43 -05:00
|
|
|
compression = COMPRESSION_JPEG;
|
2006-01-11 11:59:35 -05:00
|
|
|
while( cp )
|
|
|
|
{
|
|
|
|
if (isdigit((int)cp[1]))
|
2002-08-10 11:25:54 -04:00
|
|
|
quality = atoi(cp+1);
|
2006-01-11 11:59:35 -05:00
|
|
|
else if (cp[1] == 'r' )
|
2002-08-10 11:25:54 -04:00
|
|
|
jpegcolormode = JPEGCOLORMODE_RAW;
|
2006-01-11 11:59:35 -05:00
|
|
|
else
|
|
|
|
usage();
|
|
|
|
|
|
|
|
cp = strchr(cp+1,':');
|
|
|
|
}
|
2002-08-10 11:25:54 -04:00
|
|
|
} else if (strncmp(opt, "lzw", 3) == 0) {
|
|
|
|
char* cp = strchr(opt, ':');
|
|
|
|
if (cp)
|
|
|
|
predictor = atoi(cp+1);
|
|
|
|
compression = COMPRESSION_LZW;
|
|
|
|
} else if (strncmp(opt, "zip", 3) == 0) {
|
|
|
|
char* cp = strchr(opt, ':');
|
|
|
|
if (cp)
|
|
|
|
predictor = atoi(cp+1);
|
|
|
|
compression = COMPRESSION_DEFLATE;
|
|
|
|
} else
|
|
|
|
return (0);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2004-06-29 15:19:17 -04:00
|
|
|
static char* stuff[] = {
|
|
|
|
"raw2tiff --- tool for converting raw byte sequences in TIFF images",
|
2002-08-10 11:25:54 -04:00
|
|
|
"usage: raw2tiff [options] input.raw output.tif",
|
|
|
|
"where options are:",
|
|
|
|
" -L input data has LSB2MSB bit order (default)",
|
|
|
|
" -M input data has MSB2LSB bit order",
|
|
|
|
" -r # make each strip have no more than # rows",
|
2002-08-12 11:32:37 -04:00
|
|
|
" -H # size of input image file header in bytes (0 by default)",
|
2003-11-11 10:39:33 -05:00
|
|
|
" -w # width of input image in pixels",
|
2002-08-10 11:25:54 -04:00
|
|
|
" -l # length of input image in lines",
|
2002-08-12 11:32:37 -04:00
|
|
|
" -b # number of bands in input image (1 by default)",
|
2002-08-10 11:25:54 -04:00
|
|
|
"",
|
|
|
|
" -d data_type type of samples in input image",
|
|
|
|
"where data_type may be:",
|
|
|
|
" byte 8-bit unsigned integer (default)",
|
|
|
|
" short 16-bit unsigned integer",
|
|
|
|
" long 32-bit unsigned integer",
|
|
|
|
" sbyte 8-bit signed integer",
|
|
|
|
" sshort 16-bit signed integer",
|
|
|
|
" slong 32-bit signed integer",
|
|
|
|
" float 32-bit IEEE floating point",
|
|
|
|
" double 64-bit IEEE floating point",
|
|
|
|
"",
|
2003-07-10 16:04:42 -04:00
|
|
|
" -p photo photometric interpretation (color space) of the input image",
|
|
|
|
"where photo may be:",
|
|
|
|
" miniswhite white color represented with 0 value",
|
|
|
|
" minisblack black color represented with 0 value (default)",
|
|
|
|
" rgb image has RGB color model",
|
|
|
|
" cmyk image has CMYK (separated) color model",
|
|
|
|
" ycbcr image has YCbCr color model",
|
|
|
|
" cielab image has CIE L*a*b color model",
|
|
|
|
" icclab image has ICC L*a*b color model",
|
|
|
|
" itulab image has ITU L*a*b color model",
|
|
|
|
"",
|
2002-08-10 11:25:54 -04:00
|
|
|
" -s swap bytes fetched from input file",
|
|
|
|
"",
|
2002-08-12 11:32:37 -04:00
|
|
|
" -i config type of samples interleaving in input image",
|
|
|
|
"where config may be:",
|
|
|
|
" pixel pixel interleaved data (default)",
|
|
|
|
" band band interleaved data",
|
|
|
|
"",
|
2002-08-10 11:25:54 -04:00
|
|
|
" -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding",
|
|
|
|
" -c zip[:opts] compress output with deflate encoding",
|
2006-02-07 06:08:31 -05:00
|
|
|
" -c jpeg[:opts] compress output with JPEG encoding",
|
2002-08-10 11:25:54 -04:00
|
|
|
" -c packbits compress output with packbits encoding",
|
|
|
|
" -c none use no compression algorithm on output",
|
|
|
|
"",
|
|
|
|
"JPEG options:",
|
|
|
|
" # set compression quality level (0-100, default 75)",
|
|
|
|
" r output color image as RGB rather than YCbCr",
|
|
|
|
"For example, -c jpeg:r:50 to get JPEG-encoded RGB data with 50% comp. quality",
|
|
|
|
"",
|
|
|
|
"LZW and deflate options:",
|
|
|
|
" # set predictor value",
|
|
|
|
"For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
|
|
|
|
" -o out.tif write output to out.tif",
|
|
|
|
" -h this help message",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
setbuf(stderr, buf);
|
2003-03-12 09:05:05 -05:00
|
|
|
fprintf(stderr, "%s\n\n", TIFFGetVersion());
|
2002-08-10 11:25:54 -04:00
|
|
|
for (i = 0; stuff[i] != NULL; i++)
|
|
|
|
fprintf(stderr, "%s\n", stuff[i]);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2004-09-03 03:55:34 -04:00
|
|
|
/* vim: set ts=8 sts=8 sw=8 noet: */
|
2010-03-10 13:56:48 -05:00
|
|
|
/*
|
|
|
|
* Local Variables:
|
|
|
|
* mode: c
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* fill-column: 78
|
|
|
|
* End:
|
|
|
|
*/
|