1999-07-27 17:50:26 -04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1988-1997 Sam Leffler
|
|
|
|
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
|
|
|
|
*
|
2001-02-19 10:18:19 -05:00
|
|
|
* Revised: 2/18/01 BAR -- added syntax for extracting single images from
|
|
|
|
* multi-image TIFF files.
|
|
|
|
*
|
|
|
|
* New syntax is: sourceFileName,image#
|
|
|
|
*
|
|
|
|
* image# ranges from 0..<n-1> where n is the # of images in the file.
|
|
|
|
* There may be no white space between the comma and the filename or
|
|
|
|
* image number.
|
|
|
|
*
|
|
|
|
* Example: tiffcp source.tif,1 destination.tif
|
|
|
|
*
|
|
|
|
* Copies the 2nd image in source.tif to the destination.
|
|
|
|
*
|
|
|
|
*****
|
1999-07-27 17:50:26 -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-02 10:46:27 -04:00
|
|
|
#include "tif_config.h"
|
|
|
|
|
1999-07-27 17:50:26 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2019-02-11 15:42:03 -05:00
|
|
|
#include <limits.h>
|
1999-07-27 17:50:26 -04:00
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2004-09-02 10:46:27 -04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
1999-07-27 17:50:26 -04:00
|
|
|
#include "tiffio.h"
|
|
|
|
|
2020-03-07 07:40:20 -05:00
|
|
|
#ifndef EXIT_SUCCESS
|
|
|
|
#define EXIT_SUCCESS 0
|
|
|
|
#endif
|
|
|
|
#ifndef EXIT_FAILURE
|
|
|
|
#define EXIT_FAILURE 1
|
|
|
|
#endif
|
|
|
|
|
2006-06-07 12:34:32 -04:00
|
|
|
#ifndef HAVE_GETOPT
|
2019-11-03 12:21:26 -05:00
|
|
|
extern int getopt(int argc, char * const argv[], const char *optstring);
|
2006-06-07 12:34:32 -04:00
|
|
|
#endif
|
|
|
|
|
1999-07-27 17:50:26 -04:00
|
|
|
#if defined(VMS)
|
2006-06-07 12:34:32 -04:00
|
|
|
# define unlink delete
|
1999-07-27 17:50:26 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define streq(a,b) (strcmp(a,b) == 0)
|
|
|
|
#define strneq(a,b,n) (strncmp(a,b,n) == 0)
|
|
|
|
|
|
|
|
#define TRUE 1
|
|
|
|
#define FALSE 0
|
|
|
|
|
2020-03-21 06:07:02 -04:00
|
|
|
#define DEFAULT_MAX_MALLOC (256 * 1024 * 1024)
|
|
|
|
|
|
|
|
/* malloc size limit (in bytes)
|
|
|
|
* disabled when set to 0 */
|
|
|
|
static tmsize_t maxMalloc = DEFAULT_MAX_MALLOC;
|
|
|
|
|
2007-06-20 04:35:57 -04:00
|
|
|
static int outtiled = -1;
|
|
|
|
static uint32 tilewidth;
|
|
|
|
static uint32 tilelength;
|
|
|
|
|
|
|
|
static uint16 config;
|
|
|
|
static uint16 compression;
|
|
|
|
static uint16 predictor;
|
2010-12-23 08:38:47 -05:00
|
|
|
static int preset;
|
2007-06-20 04:35:57 -04:00
|
|
|
static uint16 fillorder;
|
|
|
|
static uint16 orientation;
|
|
|
|
static uint32 rowsperstrip;
|
|
|
|
static uint32 g3opts;
|
|
|
|
static int ignore = FALSE; /* if true, ignore read errors */
|
|
|
|
static uint32 defg3opts = (uint32) -1;
|
|
|
|
static int quality = 75; /* JPEG quality */
|
|
|
|
static int jpegcolormode = JPEGCOLORMODE_RGB;
|
|
|
|
static uint16 defcompression = (uint16) -1;
|
|
|
|
static uint16 defpredictor = (uint16) -1;
|
2010-12-23 08:38:47 -05:00
|
|
|
static int defpreset = -1;
|
2020-10-03 12:56:46 -04:00
|
|
|
static int subcodec = -1;
|
2007-06-20 04:35:57 -04:00
|
|
|
|
|
|
|
static int tiffcp(TIFF*, TIFF*);
|
|
|
|
static int processCompressOptions(char*);
|
2020-03-07 07:40:20 -05:00
|
|
|
static void usage(int code);
|
1999-07-27 17:50:26 -04:00
|
|
|
|
2001-02-19 10:18:19 -05:00
|
|
|
static char comma = ','; /* (default) comma separator character */
|
|
|
|
static TIFF* bias = NULL;
|
2003-06-04 06:36:54 -04:00
|
|
|
static int pageNum = 0;
|
2010-06-03 13:01:01 -04:00
|
|
|
static int pageInSeq = 0;
|
2001-02-19 10:18:19 -05:00
|
|
|
|
2020-03-21 06:07:02 -04:00
|
|
|
/**
|
|
|
|
* This custom malloc function enforce a maximum allocation size
|
|
|
|
*/
|
|
|
|
static void* limitMalloc(tmsize_t s)
|
|
|
|
{
|
|
|
|
if (maxMalloc && (s > maxMalloc)) {
|
|
|
|
fprintf(stderr, "MemoryLimitError: allocation of " TIFF_UINT64_FORMAT " bytes is forbidden. Limit is " TIFF_UINT64_FORMAT ".\n",
|
|
|
|
(uint64)s, (uint64)maxMalloc);
|
|
|
|
fprintf(stderr, " use -m option to change limit.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return _TIFFmalloc(s);
|
|
|
|
}
|
|
|
|
|
2001-02-19 10:18:19 -05:00
|
|
|
static int nextSrcImage (TIFF *tif, char **imageSpec)
|
|
|
|
/*
|
|
|
|
seek to the next image specified in *imageSpec
|
|
|
|
returns 1 if success, 0 if no more images to process
|
|
|
|
*imageSpec=NULL if subsequent images should be processed in sequence
|
|
|
|
*/
|
|
|
|
{
|
2007-06-20 04:35:57 -04:00
|
|
|
if (**imageSpec == comma) { /* if not @comma, we've done all images */
|
|
|
|
char *start = *imageSpec + 1;
|
|
|
|
tdir_t nextImage = (tdir_t)strtol(start, imageSpec, 0);
|
|
|
|
if (start == *imageSpec) nextImage = TIFFCurrentDirectory (tif);
|
|
|
|
if (**imageSpec)
|
|
|
|
{
|
|
|
|
if (**imageSpec == comma) {
|
|
|
|
/* a trailing comma denotes remaining images in sequence */
|
|
|
|
if ((*imageSpec)[1] == '\0') *imageSpec = NULL;
|
|
|
|
}else{
|
|
|
|
fprintf (stderr,
|
|
|
|
"Expected a %c separated image # list after %s\n",
|
|
|
|
comma, TIFFFileName (tif));
|
2020-03-07 07:40:20 -05:00
|
|
|
exit (EXIT_FAILURE); /* syntax error */
|
2007-06-20 04:35:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (TIFFSetDirectory (tif, nextImage)) return 1;
|
|
|
|
fprintf (stderr, "%s%c%d not found!\n",
|
|
|
|
TIFFFileName(tif), comma, (int) nextImage);
|
|
|
|
}
|
|
|
|
return 0;
|
2001-02-19 10:18:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static TIFF* openSrcImage (char **imageSpec)
|
|
|
|
/*
|
|
|
|
imageSpec points to a pointer to a filename followed by optional ,image#'s
|
|
|
|
Open the TIFF file and assign *imageSpec to either NULL if there are
|
|
|
|
no images specified, or a pointer to the next image number text
|
|
|
|
*/
|
|
|
|
{
|
2020-04-01 19:18:10 -04:00
|
|
|
/* disable strip shopping when using jbig compression */
|
|
|
|
const char *mode = (defcompression == COMPRESSION_JBIG) ? "rc" : "r";
|
2007-06-20 04:35:57 -04:00
|
|
|
TIFF *tif;
|
|
|
|
char *fn = *imageSpec;
|
|
|
|
*imageSpec = strchr (fn, comma);
|
|
|
|
if (*imageSpec) { /* there is at least one image number specifier */
|
|
|
|
**imageSpec = '\0';
|
2020-04-01 19:18:10 -04:00
|
|
|
tif = TIFFOpen (fn, mode);
|
2007-06-20 04:35:57 -04:00
|
|
|
/* but, ignore any single trailing comma */
|
|
|
|
if (!(*imageSpec)[1]) {*imageSpec = NULL; return tif;}
|
|
|
|
if (tif) {
|
|
|
|
**imageSpec = comma; /* replace the comma */
|
|
|
|
if (!nextSrcImage(tif, imageSpec)) {
|
|
|
|
TIFFClose (tif);
|
|
|
|
tif = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else
|
2020-04-01 19:18:10 -04:00
|
|
|
tif = TIFFOpen (fn, mode);
|
2007-06-20 04:35:57 -04:00
|
|
|
return tif;
|
2001-02-19 10:18:19 -05:00
|
|
|
}
|
|
|
|
|
1999-07-27 17:50:26 -04:00
|
|
|
int
|
2007-06-20 04:36:42 -04:00
|
|
|
main(int argc, char* argv[])
|
1999-07-27 17:50:26 -04:00
|
|
|
{
|
|
|
|
uint16 defconfig = (uint16) -1;
|
|
|
|
uint16 deffillorder = 0;
|
|
|
|
uint32 deftilewidth = (uint32) -1;
|
|
|
|
uint32 deftilelength = (uint32) -1;
|
2003-06-10 02:15:36 -04:00
|
|
|
uint32 defrowsperstrip = (uint32) 0;
|
2007-06-20 04:35:57 -04:00
|
|
|
uint64 diroff = 0;
|
1999-07-27 17:50:26 -04:00
|
|
|
TIFF* in;
|
|
|
|
TIFF* out;
|
|
|
|
char mode[10];
|
|
|
|
char* mp = mode;
|
|
|
|
int c;
|
2015-06-20 21:09:09 -04:00
|
|
|
#if !HAVE_DECL_OPTARG
|
1999-07-27 17:50:26 -04:00
|
|
|
extern int optind;
|
|
|
|
extern char* optarg;
|
2015-06-20 21:09:09 -04:00
|
|
|
#endif
|
1999-07-27 17:50:26 -04:00
|
|
|
|
|
|
|
*mp++ = 'w';
|
|
|
|
*mp = '\0';
|
2020-03-07 07:40:20 -05:00
|
|
|
while ((c = getopt(argc, argv, "m:,:b:c:f:l:o:p:r:w:aistBLMC8xh")) != -1)
|
1999-07-27 17:50:26 -04:00
|
|
|
switch (c) {
|
2020-03-21 06:07:02 -04:00
|
|
|
case 'm':
|
|
|
|
maxMalloc = (tmsize_t)strtoul(optarg, NULL, 0) << 20;
|
|
|
|
break;
|
2007-06-20 04:35:57 -04:00
|
|
|
case ',':
|
2020-03-07 07:40:20 -05:00
|
|
|
if (optarg[0] != '=') usage(EXIT_FAILURE);
|
2007-06-20 04:35:57 -04:00
|
|
|
comma = optarg[1];
|
|
|
|
break;
|
|
|
|
case 'b': /* this file is bias image subtracted from others */
|
|
|
|
if (bias) {
|
|
|
|
fputs ("Only 1 bias image may be specified\n", stderr);
|
2020-03-07 07:40:20 -05:00
|
|
|
exit (EXIT_FAILURE);
|
2007-06-20 04:35:57 -04:00
|
|
|
}
|
|
|
|
{
|
|
|
|
uint16 samples = (uint16) -1;
|
|
|
|
char **biasFn = &optarg;
|
|
|
|
bias = openSrcImage (biasFn);
|
2020-03-07 07:40:20 -05:00
|
|
|
if (!bias) exit (EXIT_FAILURE);
|
2007-06-20 04:35:57 -04:00
|
|
|
if (TIFFIsTiled (bias)) {
|
|
|
|
fputs ("Bias image must be organized in strips\n", stderr);
|
2020-03-07 07:40:20 -05:00
|
|
|
exit (EXIT_FAILURE);
|
2007-06-20 04:35:57 -04:00
|
|
|
}
|
|
|
|
TIFFGetField(bias, TIFFTAG_SAMPLESPERPIXEL, &samples);
|
|
|
|
if (samples != 1) {
|
|
|
|
fputs ("Bias image must be monochrome\n", stderr);
|
2020-03-07 07:40:20 -05:00
|
|
|
exit (EXIT_FAILURE);
|
2007-06-20 04:35:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'a': /* append to output */
|
1999-07-27 17:50:26 -04:00
|
|
|
mode[0] = 'a';
|
|
|
|
break;
|
2007-06-20 04:35:57 -04:00
|
|
|
case 'c': /* compression scheme */
|
1999-07-27 17:50:26 -04:00
|
|
|
if (!processCompressOptions(optarg))
|
2020-03-07 07:40:20 -05:00
|
|
|
usage(EXIT_FAILURE);
|
1999-07-27 17:50:26 -04:00
|
|
|
break;
|
2007-06-20 04:35:57 -04:00
|
|
|
case 'f': /* fill order */
|
1999-07-27 17:50:26 -04:00
|
|
|
if (streq(optarg, "lsb2msb"))
|
|
|
|
deffillorder = FILLORDER_LSB2MSB;
|
|
|
|
else if (streq(optarg, "msb2lsb"))
|
|
|
|
deffillorder = FILLORDER_MSB2LSB;
|
|
|
|
else
|
2020-03-07 07:40:20 -05:00
|
|
|
usage(EXIT_FAILURE);
|
1999-07-27 17:50:26 -04:00
|
|
|
break;
|
2007-06-20 04:35:57 -04:00
|
|
|
case 'i': /* ignore errors */
|
1999-07-27 17:50:26 -04:00
|
|
|
ignore = TRUE;
|
|
|
|
break;
|
2007-06-20 04:35:57 -04:00
|
|
|
case 'l': /* tile length */
|
1999-07-27 17:50:26 -04:00
|
|
|
outtiled = TRUE;
|
|
|
|
deftilelength = atoi(optarg);
|
|
|
|
break;
|
2007-06-20 04:35:57 -04:00
|
|
|
case 'o': /* initial directory offset */
|
1999-07-27 17:50:26 -04:00
|
|
|
diroff = strtoul(optarg, NULL, 0);
|
|
|
|
break;
|
2007-06-20 04:35:57 -04:00
|
|
|
case 'p': /* planar configuration */
|
1999-07-27 17:50:26 -04:00
|
|
|
if (streq(optarg, "separate"))
|
|
|
|
defconfig = PLANARCONFIG_SEPARATE;
|
|
|
|
else if (streq(optarg, "contig"))
|
|
|
|
defconfig = PLANARCONFIG_CONTIG;
|
|
|
|
else
|
2020-03-07 07:40:20 -05:00
|
|
|
usage(EXIT_FAILURE);
|
1999-07-27 17:50:26 -04:00
|
|
|
break;
|
2007-06-20 04:35:57 -04:00
|
|
|
case 'r': /* rows/strip */
|
2003-06-04 06:36:54 -04:00
|
|
|
defrowsperstrip = atol(optarg);
|
1999-07-27 17:50:26 -04:00
|
|
|
break;
|
2007-06-20 04:35:57 -04:00
|
|
|
case 's': /* generate stripped output */
|
1999-07-27 17:50:26 -04:00
|
|
|
outtiled = FALSE;
|
|
|
|
break;
|
2007-06-20 04:35:57 -04:00
|
|
|
case 't': /* generate tiled output */
|
1999-07-27 17:50:26 -04:00
|
|
|
outtiled = TRUE;
|
|
|
|
break;
|
2007-06-20 04:35:57 -04:00
|
|
|
case 'w': /* tile width */
|
1999-07-27 17:50:26 -04:00
|
|
|
outtiled = TRUE;
|
|
|
|
deftilewidth = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
*mp++ = 'b'; *mp = '\0';
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
*mp++ = 'l'; *mp = '\0';
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
*mp++ = 'm'; *mp = '\0';
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
*mp++ = 'c'; *mp = '\0';
|
|
|
|
break;
|
2007-06-20 04:35:57 -04:00
|
|
|
case '8':
|
|
|
|
*mp++ = '8'; *mp = '\0';
|
|
|
|
break;
|
2010-06-03 13:01:01 -04:00
|
|
|
case 'x':
|
|
|
|
pageInSeq = 1;
|
|
|
|
break;
|
2020-03-07 07:40:20 -05:00
|
|
|
case 'h':
|
|
|
|
usage(EXIT_SUCCESS);
|
|
|
|
/*NOTREACHED*/
|
1999-07-27 17:50:26 -04:00
|
|
|
case '?':
|
2020-03-07 07:40:20 -05:00
|
|
|
usage(EXIT_FAILURE);
|
1999-07-27 17:50:26 -04:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
if (argc - optind < 2)
|
2020-03-07 07:40:20 -05:00
|
|
|
usage(EXIT_FAILURE);
|
1999-07-27 17:50:26 -04:00
|
|
|
out = TIFFOpen(argv[argc-1], mode);
|
|
|
|
if (out == NULL)
|
2020-03-07 07:40:20 -05:00
|
|
|
return (EXIT_FAILURE);
|
2003-06-04 06:36:54 -04:00
|
|
|
if ((argc - optind) == 2)
|
2007-06-20 04:35:57 -04:00
|
|
|
pageNum = -1;
|
1999-07-27 17:50:26 -04:00
|
|
|
for (; optind < argc-1 ; optind++) {
|
2007-06-20 04:35:57 -04:00
|
|
|
char *imageCursor = argv[optind];
|
2001-02-19 10:18:19 -05:00
|
|
|
in = openSrcImage (&imageCursor);
|
2010-06-09 13:17:13 -04:00
|
|
|
if (in == NULL) {
|
|
|
|
(void) TIFFClose(out);
|
2020-03-07 07:40:20 -05:00
|
|
|
return (EXIT_FAILURE);
|
2010-06-09 13:17:13 -04:00
|
|
|
}
|
1999-07-27 17:50:26 -04:00
|
|
|
if (diroff != 0 && !TIFFSetSubDirectory(in, diroff)) {
|
|
|
|
TIFFError(TIFFFileName(in),
|
2008-12-31 18:48:01 -05:00
|
|
|
"Error, setting subdirectory at " TIFF_UINT64_FORMAT, diroff);
|
2010-06-09 13:17:13 -04:00
|
|
|
(void) TIFFClose(in);
|
1999-07-27 17:50:26 -04:00
|
|
|
(void) TIFFClose(out);
|
2020-03-07 07:40:20 -05:00
|
|
|
return (EXIT_FAILURE);
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
2007-06-20 04:35:57 -04:00
|
|
|
for (;;) {
|
|
|
|
config = defconfig;
|
|
|
|
compression = defcompression;
|
|
|
|
predictor = defpredictor;
|
2010-12-23 08:38:47 -05:00
|
|
|
preset = defpreset;
|
2007-06-20 04:35:57 -04:00
|
|
|
fillorder = deffillorder;
|
|
|
|
rowsperstrip = defrowsperstrip;
|
|
|
|
tilewidth = deftilewidth;
|
|
|
|
tilelength = deftilelength;
|
|
|
|
g3opts = defg3opts;
|
|
|
|
if (!tiffcp(in, out) || !TIFFWriteDirectory(out)) {
|
2010-06-09 13:17:13 -04:00
|
|
|
(void) TIFFClose(in);
|
|
|
|
(void) TIFFClose(out);
|
2020-03-07 07:40:20 -05:00
|
|
|
return (EXIT_FAILURE);
|
2007-06-20 04:35:57 -04:00
|
|
|
}
|
|
|
|
if (imageCursor) { /* seek next image directory */
|
|
|
|
if (!nextSrcImage(in, &imageCursor)) break;
|
|
|
|
}else
|
|
|
|
if (!TIFFReadDirectory(in)) break;
|
1999-08-16 14:27:51 -04:00
|
|
|
}
|
2010-06-09 13:17:13 -04:00
|
|
|
(void) TIFFClose(in);
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
2002-01-16 12:48:16 -05:00
|
|
|
|
2010-06-09 13:17:13 -04:00
|
|
|
(void) TIFFClose(out);
|
2020-03-07 07:40:20 -05:00
|
|
|
return (EXIT_SUCCESS);
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
|
2010-12-23 08:38:47 -05:00
|
|
|
static void
|
|
|
|
processZIPOptions(char* cp)
|
|
|
|
{
|
|
|
|
if ( (cp = strchr(cp, ':')) ) {
|
|
|
|
do {
|
|
|
|
cp++;
|
|
|
|
if (isdigit((int)*cp))
|
|
|
|
defpredictor = atoi(cp);
|
|
|
|
else if (*cp == 'p')
|
|
|
|
defpreset = atoi(++cp);
|
2020-10-03 12:56:46 -04:00
|
|
|
else if (*cp == 's')
|
|
|
|
subcodec = atoi(++cp);
|
2010-12-23 08:38:47 -05:00
|
|
|
else
|
2020-03-07 07:40:20 -05:00
|
|
|
usage(EXIT_FAILURE);
|
2010-12-23 08:38:47 -05:00
|
|
|
} while( (cp = strchr(cp, ':')) );
|
|
|
|
}
|
|
|
|
}
|
2001-02-19 10:18:19 -05:00
|
|
|
|
1999-07-27 17:50:26 -04:00
|
|
|
static void
|
|
|
|
processG3Options(char* cp)
|
|
|
|
{
|
2002-01-16 12:48:16 -05:00
|
|
|
if( (cp = strchr(cp, ':')) ) {
|
1999-07-27 17:50:26 -04:00
|
|
|
if (defg3opts == (uint32) -1)
|
|
|
|
defg3opts = 0;
|
|
|
|
do {
|
|
|
|
cp++;
|
|
|
|
if (strneq(cp, "1d", 2))
|
|
|
|
defg3opts &= ~GROUP3OPT_2DENCODING;
|
|
|
|
else if (strneq(cp, "2d", 2))
|
|
|
|
defg3opts |= GROUP3OPT_2DENCODING;
|
|
|
|
else if (strneq(cp, "fill", 4))
|
|
|
|
defg3opts |= GROUP3OPT_FILLBITS;
|
|
|
|
else
|
2020-03-07 07:40:20 -05:00
|
|
|
usage(EXIT_FAILURE);
|
2002-01-16 12:48:16 -05:00
|
|
|
} while( (cp = strchr(cp, ':')) );
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
processCompressOptions(char* opt)
|
|
|
|
{
|
|
|
|
if (streq(opt, "none")) {
|
|
|
|
defcompression = COMPRESSION_NONE;
|
|
|
|
} else if (streq(opt, "packbits")) {
|
|
|
|
defcompression = COMPRESSION_PACKBITS;
|
|
|
|
} else if (strneq(opt, "jpeg", 4)) {
|
|
|
|
char* cp = strchr(opt, ':');
|
2006-01-11 11:59:35 -05:00
|
|
|
|
2007-06-20 04:35:57 -04:00
|
|
|
defcompression = COMPRESSION_JPEG;
|
|
|
|
while( cp )
|
|
|
|
{
|
|
|
|
if (isdigit((int)cp[1]))
|
|
|
|
quality = atoi(cp+1);
|
|
|
|
else if (cp[1] == 'r' )
|
|
|
|
jpegcolormode = JPEGCOLORMODE_RAW;
|
|
|
|
else
|
2020-03-07 07:40:20 -05:00
|
|
|
usage(EXIT_FAILURE);
|
2007-06-20 04:35:57 -04:00
|
|
|
|
|
|
|
cp = strchr(cp+1,':');
|
|
|
|
}
|
1999-07-27 17:50:26 -04:00
|
|
|
} else if (strneq(opt, "g3", 2)) {
|
|
|
|
processG3Options(opt);
|
|
|
|
defcompression = COMPRESSION_CCITTFAX3;
|
|
|
|
} else if (streq(opt, "g4")) {
|
|
|
|
defcompression = COMPRESSION_CCITTFAX4;
|
|
|
|
} else if (strneq(opt, "lzw", 3)) {
|
|
|
|
char* cp = strchr(opt, ':');
|
|
|
|
if (cp)
|
|
|
|
defpredictor = atoi(cp+1);
|
|
|
|
defcompression = COMPRESSION_LZW;
|
|
|
|
} else if (strneq(opt, "zip", 3)) {
|
2010-12-23 08:38:47 -05:00
|
|
|
processZIPOptions(opt);
|
2005-04-15 13:50:09 -04:00
|
|
|
defcompression = COMPRESSION_ADOBE_DEFLATE;
|
2010-12-14 07:52:59 -05:00
|
|
|
} else if (strneq(opt, "lzma", 4)) {
|
2010-12-23 08:38:47 -05:00
|
|
|
processZIPOptions(opt);
|
2010-12-14 07:52:59 -05:00
|
|
|
defcompression = COMPRESSION_LZMA;
|
Add ZSTD compression codec
From https://github.com/facebook/zstd
"Zstandard, or zstd as short version, is a fast lossless compression
algorithm, targeting real-time compression scenarios at zlib-level
and better compression ratios. It's backed by a very fast entropy stage,
provided by Huff0 and FSE library."
We require libzstd >= 1.0.0 so as to be able to use streaming compression
and decompression methods.
The default compression level we have selected is 9 (range goes from 1 to 22),
which experimentally offers equivalent or better compression ratio than
the default deflate/ZIP level of 6, and much faster compression.
For example on a 6600x4400 16bit image, tiffcp -c zip runs in 10.7 seconds,
while tiffcp -c zstd runs in 5.3 seconds. Decompression time for zip is
840 ms, and for zstd 650 ms. File size is 42735936 for zip, and
42586822 for zstd. Similar findings on other images.
On a 25894x16701 16bit image,
Compression time Decompression time File size
ZSTD 35 s 3.2 s 399 700 498
ZIP/Deflate 1m 20 s 4.9 s 419 622 336
2017-12-21 07:32:02 -05:00
|
|
|
} else if (strneq(opt, "zstd", 4)) {
|
|
|
|
processZIPOptions(opt);
|
|
|
|
defcompression = COMPRESSION_ZSTD;
|
2018-09-05 19:49:28 -04:00
|
|
|
} else if (strneq(opt, "webp", 4)) {
|
|
|
|
processZIPOptions(opt);
|
|
|
|
defcompression = COMPRESSION_WEBP;
|
2006-06-07 11:47:43 -04:00
|
|
|
} else if (strneq(opt, "jbig", 4)) {
|
|
|
|
defcompression = COMPRESSION_JBIG;
|
2009-06-30 13:25:26 -04:00
|
|
|
} else if (strneq(opt, "sgilog", 6)) {
|
|
|
|
defcompression = COMPRESSION_SGILOG;
|
1999-07-27 17:50:26 -04:00
|
|
|
} else
|
|
|
|
return (0);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2020-03-07 07:40:20 -05:00
|
|
|
static const char* stuff[] = {
|
1999-07-27 17:50:26 -04:00
|
|
|
"usage: tiffcp [options] input... output",
|
|
|
|
"where options are:",
|
2007-06-20 04:35:57 -04:00
|
|
|
" -a append to output instead of overwriting",
|
|
|
|
" -o offset set initial directory offset",
|
|
|
|
" -p contig pack samples contiguously (e.g. RGBRGB...)",
|
|
|
|
" -p separate store samples separately (e.g. RRR...GGG...BBB...)",
|
|
|
|
" -s write output in strips",
|
|
|
|
" -t write output in tiles",
|
2013-03-05 22:35:09 -05:00
|
|
|
" -x force the merged tiff pages in sequence",
|
2007-06-20 04:35:57 -04:00
|
|
|
" -8 write BigTIFF instead of default ClassicTIFF",
|
2013-03-05 22:35:09 -05:00
|
|
|
" -B write big-endian instead of native byte order",
|
|
|
|
" -L write little-endian instead of native byte order",
|
|
|
|
" -M disable use of memory-mapped files",
|
|
|
|
" -C disable strip chopping",
|
2007-06-20 04:35:57 -04:00
|
|
|
" -i ignore read errors",
|
|
|
|
" -b file[,#] bias (dark) monochrome image to be subtracted from all others",
|
|
|
|
" -,=% use % rather than , to separate image #'s (per Note below)",
|
2020-03-21 06:07:02 -04:00
|
|
|
" -m size set maximum memory allocation size (MiB). 0 to disable limit.",
|
1999-07-27 17:50:26 -04:00
|
|
|
"",
|
2007-06-20 04:35:57 -04:00
|
|
|
" -r # make each strip have no more than # rows",
|
|
|
|
" -w # set output tile width (pixels)",
|
|
|
|
" -l # set output tile length (pixels)",
|
1999-07-27 17:50:26 -04:00
|
|
|
"",
|
2007-06-20 04:35:57 -04:00
|
|
|
" -f lsb2msb force lsb-to-msb FillOrder for output",
|
|
|
|
" -f msb2lsb force msb-to-lsb FillOrder for output",
|
1999-07-27 17:50:26 -04:00
|
|
|
"",
|
2007-06-20 04:35:57 -04:00
|
|
|
" -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding",
|
|
|
|
" -c zip[:opts] compress output with deflate encoding",
|
2010-12-23 08:38:47 -05:00
|
|
|
" -c lzma[:opts] compress output with LZMA2 encoding",
|
Add ZSTD compression codec
From https://github.com/facebook/zstd
"Zstandard, or zstd as short version, is a fast lossless compression
algorithm, targeting real-time compression scenarios at zlib-level
and better compression ratios. It's backed by a very fast entropy stage,
provided by Huff0 and FSE library."
We require libzstd >= 1.0.0 so as to be able to use streaming compression
and decompression methods.
The default compression level we have selected is 9 (range goes from 1 to 22),
which experimentally offers equivalent or better compression ratio than
the default deflate/ZIP level of 6, and much faster compression.
For example on a 6600x4400 16bit image, tiffcp -c zip runs in 10.7 seconds,
while tiffcp -c zstd runs in 5.3 seconds. Decompression time for zip is
840 ms, and for zstd 650 ms. File size is 42735936 for zip, and
42586822 for zstd. Similar findings on other images.
On a 25894x16701 16bit image,
Compression time Decompression time File size
ZSTD 35 s 3.2 s 399 700 498
ZIP/Deflate 1m 20 s 4.9 s 419 622 336
2017-12-21 07:32:02 -05:00
|
|
|
" -c zstd[:opts] compress output with ZSTD encoding",
|
2018-09-05 19:49:28 -04:00
|
|
|
" -c webp[:opts] compress output with WEBP encoding",
|
2007-06-20 04:35:57 -04:00
|
|
|
" -c jpeg[:opts] compress output with JPEG encoding",
|
|
|
|
" -c jbig compress output with ISO JBIG encoding",
|
|
|
|
" -c packbits compress output with packbits encoding",
|
|
|
|
" -c g3[:opts] compress output with CCITT Group 3 encoding",
|
|
|
|
" -c g4 compress output with CCITT Group 4 encoding",
|
2009-06-30 13:25:26 -04:00
|
|
|
" -c sgilog compress output with SGILOG encoding",
|
2007-06-20 04:35:57 -04:00
|
|
|
" -c none use no compression algorithm on output",
|
1999-07-27 17:50:26 -04:00
|
|
|
"",
|
|
|
|
"Group 3 options:",
|
2007-06-20 04:35:57 -04:00
|
|
|
" 1d use default CCITT Group 3 1D-encoding",
|
|
|
|
" 2d use optional CCITT Group 3 2D-encoding",
|
|
|
|
" fill byte-align EOL codes",
|
1999-07-27 17:50:26 -04:00
|
|
|
"For example, -c g3:2d:fill to get G3-2D-encoded data with byte-aligned EOLs",
|
|
|
|
"",
|
|
|
|
"JPEG options:",
|
2007-06-20 04:35:57 -04:00
|
|
|
" # set compression quality level (0-100, default 75)",
|
|
|
|
" r output color image as RGB rather than YCbCr",
|
1999-07-27 17:50:26 -04:00
|
|
|
"For example, -c jpeg:r:50 to get JPEG-encoded RGB data with 50% comp. quality",
|
|
|
|
"",
|
2018-09-05 19:49:28 -04:00
|
|
|
"LZW, Deflate (ZIP), LZMA2, ZSTD and WEBP options:",
|
2007-06-20 04:35:57 -04:00
|
|
|
" # set predictor value",
|
2010-12-23 08:38:47 -05:00
|
|
|
" p# set compression level (preset)",
|
2020-10-03 12:56:46 -04:00
|
|
|
#if LIBDEFLATE_SUPPORT
|
|
|
|
" s# set subcodec (0=zlib, 1=libdeflate) (only for Deflate/ZIP)",
|
|
|
|
#endif
|
2010-12-23 08:38:47 -05:00
|
|
|
"For example, -c lzw:2 to get LZW-encoded data with horizontal differencing,",
|
|
|
|
"-c zip:3:p9 for Deflate encoding with maximum compression level and floating",
|
|
|
|
"point predictor.",
|
2001-02-19 10:18:19 -05:00
|
|
|
"",
|
|
|
|
"Note that input filenames may be of the form filename,x,y,z",
|
|
|
|
"where x, y, and z specify image numbers in the filename to copy.",
|
|
|
|
"example: tiffcp -c none -b esp.tif,1 esp.tif,0 test.tif",
|
|
|
|
" subtract 2nd image in esp.tif from 1st yielding uncompressed result test.tif",
|
1999-07-27 17:50:26 -04:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2020-03-07 07:40:20 -05:00
|
|
|
usage(int code)
|
1999-07-27 17:50:26 -04:00
|
|
|
{
|
|
|
|
int i;
|
2020-03-07 07:40:20 -05:00
|
|
|
FILE * out = (code == EXIT_SUCCESS) ? stdout : stderr;
|
1999-07-27 17:50:26 -04:00
|
|
|
|
2020-03-07 07:40:20 -05:00
|
|
|
fprintf(out, "%s\n\n", TIFFGetVersion());
|
1999-07-27 17:50:26 -04:00
|
|
|
for (i = 0; stuff[i] != NULL; i++)
|
2020-03-07 07:40:20 -05:00
|
|
|
fprintf(out, "%s\n", stuff[i]);
|
|
|
|
exit(code);
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define CopyField(tag, v) \
|
|
|
|
if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
|
|
|
|
#define CopyField2(tag, v1, v2) \
|
|
|
|
if (TIFFGetField(in, tag, &v1, &v2)) TIFFSetField(out, tag, v1, v2)
|
|
|
|
#define CopyField3(tag, v1, v2, v3) \
|
|
|
|
if (TIFFGetField(in, tag, &v1, &v2, &v3)) TIFFSetField(out, tag, v1, v2, v3)
|
|
|
|
#define CopyField4(tag, v1, v2, v3, v4) \
|
|
|
|
if (TIFFGetField(in, tag, &v1, &v2, &v3, &v4)) TIFFSetField(out, tag, v1, v2, v3, v4)
|
|
|
|
|
|
|
|
static void
|
|
|
|
cpTag(TIFF* in, TIFF* out, uint16 tag, uint16 count, TIFFDataType type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case TIFF_SHORT:
|
|
|
|
if (count == 1) {
|
|
|
|
uint16 shortv;
|
|
|
|
CopyField(tag, shortv);
|
|
|
|
} else if (count == 2) {
|
|
|
|
uint16 shortv1, shortv2;
|
|
|
|
CopyField2(tag, shortv1, shortv2);
|
|
|
|
} else if (count == 4) {
|
|
|
|
uint16 *tr, *tg, *tb, *ta;
|
|
|
|
CopyField4(tag, tr, tg, tb, ta);
|
|
|
|
} else if (count == (uint16) -1) {
|
|
|
|
uint16 shortv1;
|
|
|
|
uint16* shortav;
|
|
|
|
CopyField2(tag, shortv1, shortav);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TIFF_LONG:
|
|
|
|
{ uint32 longv;
|
|
|
|
CopyField(tag, longv);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TIFF_RATIONAL:
|
|
|
|
if (count == 1) {
|
|
|
|
float floatv;
|
|
|
|
CopyField(tag, floatv);
|
|
|
|
} else if (count == (uint16) -1) {
|
|
|
|
float* floatav;
|
|
|
|
CopyField(tag, floatav);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TIFF_ASCII:
|
|
|
|
{ char* stringv;
|
|
|
|
CopyField(tag, stringv);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TIFF_DOUBLE:
|
|
|
|
if (count == 1) {
|
|
|
|
double doublev;
|
|
|
|
CopyField(tag, doublev);
|
|
|
|
} else if (count == (uint16) -1) {
|
|
|
|
double* doubleav;
|
|
|
|
CopyField(tag, doubleav);
|
|
|
|
}
|
|
|
|
break;
|
2007-06-20 04:35:57 -04:00
|
|
|
default:
|
|
|
|
TIFFError(TIFFFileName(in),
|
|
|
|
"Data type %d is not supported, tag %d skipped.",
|
|
|
|
tag, type);
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct cpTag {
|
2007-06-20 04:35:57 -04:00
|
|
|
uint16 tag;
|
|
|
|
uint16 count;
|
1999-07-27 17:50:26 -04:00
|
|
|
TIFFDataType type;
|
|
|
|
} tags[] = {
|
|
|
|
{ TIFFTAG_SUBFILETYPE, 1, TIFF_LONG },
|
|
|
|
{ TIFFTAG_THRESHHOLDING, 1, TIFF_SHORT },
|
|
|
|
{ TIFFTAG_DOCUMENTNAME, 1, TIFF_ASCII },
|
|
|
|
{ TIFFTAG_IMAGEDESCRIPTION, 1, TIFF_ASCII },
|
|
|
|
{ TIFFTAG_MAKE, 1, TIFF_ASCII },
|
|
|
|
{ TIFFTAG_MODEL, 1, TIFF_ASCII },
|
|
|
|
{ TIFFTAG_MINSAMPLEVALUE, 1, TIFF_SHORT },
|
|
|
|
{ TIFFTAG_MAXSAMPLEVALUE, 1, TIFF_SHORT },
|
|
|
|
{ TIFFTAG_XRESOLUTION, 1, TIFF_RATIONAL },
|
|
|
|
{ TIFFTAG_YRESOLUTION, 1, TIFF_RATIONAL },
|
|
|
|
{ TIFFTAG_PAGENAME, 1, TIFF_ASCII },
|
|
|
|
{ TIFFTAG_XPOSITION, 1, TIFF_RATIONAL },
|
|
|
|
{ TIFFTAG_YPOSITION, 1, TIFF_RATIONAL },
|
|
|
|
{ TIFFTAG_RESOLUTIONUNIT, 1, TIFF_SHORT },
|
|
|
|
{ TIFFTAG_SOFTWARE, 1, TIFF_ASCII },
|
|
|
|
{ TIFFTAG_DATETIME, 1, TIFF_ASCII },
|
|
|
|
{ TIFFTAG_ARTIST, 1, TIFF_ASCII },
|
|
|
|
{ TIFFTAG_HOSTCOMPUTER, 1, TIFF_ASCII },
|
2005-07-06 07:38:17 -04:00
|
|
|
{ TIFFTAG_WHITEPOINT, (uint16) -1, TIFF_RATIONAL },
|
1999-07-27 17:50:26 -04:00
|
|
|
{ TIFFTAG_PRIMARYCHROMATICITIES,(uint16) -1,TIFF_RATIONAL },
|
|
|
|
{ TIFFTAG_HALFTONEHINTS, 2, TIFF_SHORT },
|
|
|
|
{ TIFFTAG_INKSET, 1, TIFF_SHORT },
|
|
|
|
{ TIFFTAG_DOTRANGE, 2, TIFF_SHORT },
|
|
|
|
{ TIFFTAG_TARGETPRINTER, 1, TIFF_ASCII },
|
|
|
|
{ TIFFTAG_SAMPLEFORMAT, 1, TIFF_SHORT },
|
|
|
|
{ TIFFTAG_YCBCRCOEFFICIENTS, (uint16) -1,TIFF_RATIONAL },
|
|
|
|
{ TIFFTAG_YCBCRSUBSAMPLING, 2, TIFF_SHORT },
|
|
|
|
{ TIFFTAG_YCBCRPOSITIONING, 1, TIFF_SHORT },
|
|
|
|
{ TIFFTAG_REFERENCEBLACKWHITE, (uint16) -1,TIFF_RATIONAL },
|
|
|
|
{ TIFFTAG_EXTRASAMPLES, (uint16) -1, TIFF_SHORT },
|
|
|
|
{ TIFFTAG_SMINSAMPLEVALUE, 1, TIFF_DOUBLE },
|
|
|
|
{ TIFFTAG_SMAXSAMPLEVALUE, 1, TIFF_DOUBLE },
|
|
|
|
{ TIFFTAG_STONITS, 1, TIFF_DOUBLE },
|
|
|
|
};
|
|
|
|
#define NTAGS (sizeof (tags) / sizeof (tags[0]))
|
|
|
|
|
|
|
|
#define CopyTag(tag, count, type) cpTag(in, out, tag, count, type)
|
|
|
|
|
|
|
|
typedef int (*copyFunc)
|
|
|
|
(TIFF* in, TIFF* out, uint32 l, uint32 w, uint16 samplesperpixel);
|
|
|
|
static copyFunc pickCopyFunc(TIFF*, TIFF*, uint16, uint16);
|
|
|
|
|
2007-06-20 04:35:57 -04:00
|
|
|
/* PODD */
|
|
|
|
|
1999-07-27 17:50:26 -04:00
|
|
|
static int
|
|
|
|
tiffcp(TIFF* in, TIFF* out)
|
|
|
|
{
|
2017-01-11 14:25:44 -05:00
|
|
|
uint16 bitspersample = 1, samplesperpixel = 1;
|
2016-10-08 11:04:31 -04:00
|
|
|
uint16 input_compression, input_photometric = PHOTOMETRIC_MINISBLACK;
|
1999-07-27 17:50:26 -04:00
|
|
|
copyFunc cf;
|
2003-06-10 02:15:36 -04:00
|
|
|
uint32 width, length;
|
1999-07-27 17:50:26 -04:00
|
|
|
struct cpTag* p;
|
|
|
|
|
2003-06-10 02:15:36 -04:00
|
|
|
CopyField(TIFFTAG_IMAGEWIDTH, width);
|
|
|
|
CopyField(TIFFTAG_IMAGELENGTH, length);
|
1999-07-27 17:50:26 -04:00
|
|
|
CopyField(TIFFTAG_BITSPERSAMPLE, bitspersample);
|
|
|
|
CopyField(TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
|
|
|
|
if (compression != (uint16)-1)
|
|
|
|
TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
|
|
|
|
else
|
|
|
|
CopyField(TIFFTAG_COMPRESSION, compression);
|
2010-06-11 17:23:12 -04:00
|
|
|
TIFFGetFieldDefaulted(in, TIFFTAG_COMPRESSION, &input_compression);
|
|
|
|
TIFFGetFieldDefaulted(in, TIFFTAG_PHOTOMETRIC, &input_photometric);
|
|
|
|
if (input_compression == COMPRESSION_JPEG) {
|
|
|
|
/* Force conversion to RGB */
|
|
|
|
TIFFSetField(in, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
|
|
|
|
} else if (input_photometric == PHOTOMETRIC_YCBCR) {
|
|
|
|
/* Otherwise, can't handle subsampled input */
|
|
|
|
uint16 subsamplinghor,subsamplingver;
|
|
|
|
|
|
|
|
TIFFGetFieldDefaulted(in, TIFFTAG_YCBCRSUBSAMPLING,
|
|
|
|
&subsamplinghor, &subsamplingver);
|
|
|
|
if (subsamplinghor!=1 || subsamplingver!=1) {
|
|
|
|
fprintf(stderr, "tiffcp: %s: Can't copy/convert subsampled image.\n",
|
|
|
|
TIFFFileName(in));
|
|
|
|
return FALSE;
|
2007-06-20 04:35:57 -04:00
|
|
|
}
|
|
|
|
}
|
2010-06-11 17:23:12 -04:00
|
|
|
if (compression == COMPRESSION_JPEG) {
|
|
|
|
if (input_photometric == PHOTOMETRIC_RGB &&
|
|
|
|
jpegcolormode == JPEGCOLORMODE_RGB)
|
|
|
|
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);
|
|
|
|
else
|
|
|
|
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, input_photometric);
|
|
|
|
}
|
2004-01-02 05:02:14 -05:00
|
|
|
else if (compression == COMPRESSION_SGILOG
|
2007-06-20 04:35:57 -04:00
|
|
|
|| compression == COMPRESSION_SGILOG24)
|
1999-07-27 17:50:26 -04:00
|
|
|
TIFFSetField(out, TIFFTAG_PHOTOMETRIC,
|
|
|
|
samplesperpixel == 1 ?
|
2007-06-20 04:35:57 -04:00
|
|
|
PHOTOMETRIC_LOGL : PHOTOMETRIC_LOGLUV);
|
2014-12-21 11:28:37 -05:00
|
|
|
else if (input_compression == COMPRESSION_JPEG &&
|
|
|
|
samplesperpixel == 3 ) {
|
|
|
|
/* RGB conversion was forced above
|
|
|
|
hence the output will be of the same type */
|
|
|
|
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
|
|
|
|
}
|
1999-07-27 17:50:26 -04:00
|
|
|
else
|
|
|
|
CopyTag(TIFFTAG_PHOTOMETRIC, 1, TIFF_SHORT);
|
|
|
|
if (fillorder != 0)
|
|
|
|
TIFFSetField(out, TIFFTAG_FILLORDER, fillorder);
|
|
|
|
else
|
|
|
|
CopyTag(TIFFTAG_FILLORDER, 1, TIFF_SHORT);
|
2002-03-31 17:57:19 -05:00
|
|
|
/*
|
|
|
|
* Will copy `Orientation' tag from input image
|
|
|
|
*/
|
2004-03-26 06:39:31 -05:00
|
|
|
TIFFGetFieldDefaulted(in, TIFFTAG_ORIENTATION, &orientation);
|
2002-03-31 17:57:19 -05:00
|
|
|
switch (orientation) {
|
|
|
|
case ORIENTATION_BOTRIGHT:
|
|
|
|
case ORIENTATION_RIGHTBOT: /* XXX */
|
|
|
|
TIFFWarning(TIFFFileName(in), "using bottom-left orientation");
|
|
|
|
orientation = ORIENTATION_BOTLEFT;
|
2018-02-24 15:47:52 -05:00
|
|
|
/* fall through... */
|
2004-03-26 06:39:31 -05:00
|
|
|
case ORIENTATION_LEFTBOT: /* XXX */
|
2002-03-31 17:57:19 -05:00
|
|
|
case ORIENTATION_BOTLEFT:
|
|
|
|
break;
|
|
|
|
case ORIENTATION_TOPRIGHT:
|
|
|
|
case ORIENTATION_RIGHTTOP: /* XXX */
|
|
|
|
default:
|
|
|
|
TIFFWarning(TIFFFileName(in), "using top-left orientation");
|
|
|
|
orientation = ORIENTATION_TOPLEFT;
|
2018-02-24 15:47:52 -05:00
|
|
|
/* fall through... */
|
2004-03-26 06:39:31 -05:00
|
|
|
case ORIENTATION_LEFTTOP: /* XXX */
|
2002-03-31 17:57:19 -05:00
|
|
|
case ORIENTATION_TOPLEFT:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
TIFFSetField(out, TIFFTAG_ORIENTATION, orientation);
|
1999-07-27 17:50:26 -04:00
|
|
|
/*
|
|
|
|
* Choose tiles/strip for the output image according to
|
|
|
|
* the command line arguments (-tiles, -strips) and the
|
|
|
|
* structure of the input image.
|
|
|
|
*/
|
|
|
|
if (outtiled == -1)
|
|
|
|
outtiled = TIFFIsTiled(in);
|
|
|
|
if (outtiled) {
|
|
|
|
/*
|
|
|
|
* Setup output file's tile width&height. If either
|
|
|
|
* is not specified, use either the value from the
|
|
|
|
* input image or, if nothing is defined, use the
|
|
|
|
* library default.
|
|
|
|
*/
|
|
|
|
if (tilewidth == (uint32) -1)
|
|
|
|
TIFFGetField(in, TIFFTAG_TILEWIDTH, &tilewidth);
|
|
|
|
if (tilelength == (uint32) -1)
|
|
|
|
TIFFGetField(in, TIFFTAG_TILELENGTH, &tilelength);
|
|
|
|
TIFFDefaultTileSize(out, &tilewidth, &tilelength);
|
|
|
|
TIFFSetField(out, TIFFTAG_TILEWIDTH, tilewidth);
|
|
|
|
TIFFSetField(out, TIFFTAG_TILELENGTH, tilelength);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* RowsPerStrip is left unspecified: use either the
|
|
|
|
* value from the input image or, if nothing is defined,
|
|
|
|
* use the library default.
|
|
|
|
*/
|
2003-06-10 02:15:36 -04:00
|
|
|
if (rowsperstrip == (uint32) 0) {
|
2006-03-21 11:24:33 -05:00
|
|
|
if (!TIFFGetField(in, TIFFTAG_ROWSPERSTRIP,
|
2007-06-20 04:35:57 -04:00
|
|
|
&rowsperstrip)) {
|
2003-06-04 06:36:54 -04:00
|
|
|
rowsperstrip =
|
2007-06-20 04:35:57 -04:00
|
|
|
TIFFDefaultStripSize(out, rowsperstrip);
|
2006-03-21 11:24:33 -05:00
|
|
|
}
|
2007-02-24 10:57:01 -05:00
|
|
|
if (rowsperstrip > length && rowsperstrip != (uint32)-1)
|
2006-03-21 11:24:33 -05:00
|
|
|
rowsperstrip = length;
|
2003-06-04 06:36:54 -04:00
|
|
|
}
|
2003-06-10 02:15:36 -04:00
|
|
|
else if (rowsperstrip == (uint32) -1)
|
|
|
|
rowsperstrip = length;
|
1999-07-27 17:50:26 -04:00
|
|
|
TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
|
|
|
|
}
|
|
|
|
if (config != (uint16) -1)
|
|
|
|
TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);
|
|
|
|
else
|
|
|
|
CopyField(TIFFTAG_PLANARCONFIG, config);
|
|
|
|
if (samplesperpixel <= 4)
|
|
|
|
CopyTag(TIFFTAG_TRANSFERFUNCTION, 4, TIFF_SHORT);
|
|
|
|
CopyTag(TIFFTAG_COLORMAP, 4, TIFF_SHORT);
|
|
|
|
/* SMinSampleValue & SMaxSampleValue */
|
|
|
|
switch (compression) {
|
2007-06-20 04:35:57 -04:00
|
|
|
case COMPRESSION_JPEG:
|
|
|
|
TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality);
|
|
|
|
TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode);
|
|
|
|
break;
|
|
|
|
case COMPRESSION_JBIG:
|
|
|
|
CopyTag(TIFFTAG_FAXRECVPARAMS, 1, TIFF_LONG);
|
|
|
|
CopyTag(TIFFTAG_FAXRECVTIME, 1, TIFF_LONG);
|
|
|
|
CopyTag(TIFFTAG_FAXSUBADDRESS, 1, TIFF_ASCII);
|
|
|
|
CopyTag(TIFFTAG_FAXDCS, 1, TIFF_ASCII);
|
|
|
|
break;
|
|
|
|
case COMPRESSION_LZW:
|
|
|
|
case COMPRESSION_ADOBE_DEFLATE:
|
|
|
|
case COMPRESSION_DEFLATE:
|
2010-12-14 07:52:59 -05:00
|
|
|
case COMPRESSION_LZMA:
|
Add ZSTD compression codec
From https://github.com/facebook/zstd
"Zstandard, or zstd as short version, is a fast lossless compression
algorithm, targeting real-time compression scenarios at zlib-level
and better compression ratios. It's backed by a very fast entropy stage,
provided by Huff0 and FSE library."
We require libzstd >= 1.0.0 so as to be able to use streaming compression
and decompression methods.
The default compression level we have selected is 9 (range goes from 1 to 22),
which experimentally offers equivalent or better compression ratio than
the default deflate/ZIP level of 6, and much faster compression.
For example on a 6600x4400 16bit image, tiffcp -c zip runs in 10.7 seconds,
while tiffcp -c zstd runs in 5.3 seconds. Decompression time for zip is
840 ms, and for zstd 650 ms. File size is 42735936 for zip, and
42586822 for zstd. Similar findings on other images.
On a 25894x16701 16bit image,
Compression time Decompression time File size
ZSTD 35 s 3.2 s 399 700 498
ZIP/Deflate 1m 20 s 4.9 s 419 622 336
2017-12-21 07:32:02 -05:00
|
|
|
case COMPRESSION_ZSTD:
|
2007-06-20 04:35:57 -04:00
|
|
|
if (predictor != (uint16)-1)
|
|
|
|
TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);
|
1999-07-27 17:50:26 -04:00
|
|
|
else
|
2007-06-20 04:35:57 -04:00
|
|
|
CopyField(TIFFTAG_PREDICTOR, predictor);
|
2020-10-03 12:56:46 -04:00
|
|
|
if( compression == COMPRESSION_ADOBE_DEFLATE ||
|
|
|
|
compression == COMPRESSION_DEFLATE )
|
|
|
|
{
|
|
|
|
if( subcodec != -1 )
|
|
|
|
{
|
|
|
|
if( TIFFSetField(out, TIFFTAG_DEFLATE_SUBCODEC, subcodec) != 1 )
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 13:21:56 -04:00
|
|
|
/*fallthrough*/
|
|
|
|
case COMPRESSION_WEBP:
|
2010-12-23 08:38:47 -05:00
|
|
|
if (preset != -1) {
|
|
|
|
if (compression == COMPRESSION_ADOBE_DEFLATE
|
|
|
|
|| compression == COMPRESSION_DEFLATE)
|
|
|
|
TIFFSetField(out, TIFFTAG_ZIPQUALITY, preset);
|
|
|
|
else if (compression == COMPRESSION_LZMA)
|
|
|
|
TIFFSetField(out, TIFFTAG_LZMAPRESET, preset);
|
Add ZSTD compression codec
From https://github.com/facebook/zstd
"Zstandard, or zstd as short version, is a fast lossless compression
algorithm, targeting real-time compression scenarios at zlib-level
and better compression ratios. It's backed by a very fast entropy stage,
provided by Huff0 and FSE library."
We require libzstd >= 1.0.0 so as to be able to use streaming compression
and decompression methods.
The default compression level we have selected is 9 (range goes from 1 to 22),
which experimentally offers equivalent or better compression ratio than
the default deflate/ZIP level of 6, and much faster compression.
For example on a 6600x4400 16bit image, tiffcp -c zip runs in 10.7 seconds,
while tiffcp -c zstd runs in 5.3 seconds. Decompression time for zip is
840 ms, and for zstd 650 ms. File size is 42735936 for zip, and
42586822 for zstd. Similar findings on other images.
On a 25894x16701 16bit image,
Compression time Decompression time File size
ZSTD 35 s 3.2 s 399 700 498
ZIP/Deflate 1m 20 s 4.9 s 419 622 336
2017-12-21 07:32:02 -05:00
|
|
|
else if (compression == COMPRESSION_ZSTD)
|
|
|
|
TIFFSetField(out, TIFFTAG_ZSTD_LEVEL, preset);
|
2018-09-05 19:49:28 -04:00
|
|
|
else if (compression == COMPRESSION_WEBP) {
|
|
|
|
if (preset == 100) {
|
|
|
|
TIFFSetField(out, TIFFTAG_WEBP_LOSSLESS, TRUE);
|
|
|
|
} else {
|
|
|
|
TIFFSetField(out, TIFFTAG_WEBP_LEVEL, preset);
|
|
|
|
}
|
|
|
|
}
|
2010-12-23 08:38:47 -05:00
|
|
|
}
|
2007-06-20 04:35:57 -04:00
|
|
|
break;
|
|
|
|
case COMPRESSION_CCITTFAX3:
|
|
|
|
case COMPRESSION_CCITTFAX4:
|
|
|
|
if (compression == COMPRESSION_CCITTFAX3) {
|
|
|
|
if (g3opts != (uint32) -1)
|
|
|
|
TIFFSetField(out, TIFFTAG_GROUP3OPTIONS,
|
|
|
|
g3opts);
|
|
|
|
else
|
|
|
|
CopyField(TIFFTAG_GROUP3OPTIONS, g3opts);
|
|
|
|
} else
|
|
|
|
CopyTag(TIFFTAG_GROUP4OPTIONS, 1, TIFF_LONG);
|
|
|
|
CopyTag(TIFFTAG_BADFAXLINES, 1, TIFF_LONG);
|
|
|
|
CopyTag(TIFFTAG_CLEANFAXDATA, 1, TIFF_LONG);
|
|
|
|
CopyTag(TIFFTAG_CONSECUTIVEBADFAXLINES, 1, TIFF_LONG);
|
|
|
|
CopyTag(TIFFTAG_FAXRECVPARAMS, 1, TIFF_LONG);
|
|
|
|
CopyTag(TIFFTAG_FAXRECVTIME, 1, TIFF_LONG);
|
|
|
|
CopyTag(TIFFTAG_FAXSUBADDRESS, 1, TIFF_ASCII);
|
|
|
|
break;
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
2007-06-20 04:35:57 -04:00
|
|
|
{
|
|
|
|
uint32 len32;
|
|
|
|
void** data;
|
|
|
|
if (TIFFGetField(in, TIFFTAG_ICCPROFILE, &len32, &data))
|
|
|
|
TIFFSetField(out, TIFFTAG_ICCPROFILE, len32, data);
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
2007-06-20 04:35:57 -04:00
|
|
|
{
|
|
|
|
uint16 ninks;
|
|
|
|
const char* inknames;
|
|
|
|
if (TIFFGetField(in, TIFFTAG_NUMBEROFINKS, &ninks)) {
|
|
|
|
TIFFSetField(out, TIFFTAG_NUMBEROFINKS, ninks);
|
|
|
|
if (TIFFGetField(in, TIFFTAG_INKNAMES, &inknames)) {
|
|
|
|
int inknameslen = strlen(inknames) + 1;
|
|
|
|
const char* cp = inknames;
|
|
|
|
while (ninks > 1) {
|
|
|
|
cp = strchr(cp, '\0');
|
2008-05-23 13:24:41 -04:00
|
|
|
cp++;
|
|
|
|
inknameslen += (strlen(cp) + 1);
|
2007-06-20 04:35:57 -04:00
|
|
|
ninks--;
|
|
|
|
}
|
|
|
|
TIFFSetField(out, TIFFTAG_INKNAMES, inknameslen, inknames);
|
|
|
|
}
|
2004-01-28 07:12:01 -05:00
|
|
|
}
|
|
|
|
}
|
2003-06-04 06:36:54 -04:00
|
|
|
{
|
2007-06-20 04:35:57 -04:00
|
|
|
unsigned short pg0, pg1;
|
2010-06-03 13:01:01 -04:00
|
|
|
|
|
|
|
if (pageInSeq == 1) {
|
|
|
|
if (pageNum < 0) /* only one input file */ {
|
|
|
|
if (TIFFGetField(in, TIFFTAG_PAGENUMBER, &pg0, &pg1))
|
|
|
|
TIFFSetField(out, TIFFTAG_PAGENUMBER, pg0, pg1);
|
|
|
|
} else
|
2007-06-20 04:35:57 -04:00
|
|
|
TIFFSetField(out, TIFFTAG_PAGENUMBER, pageNum++, 0);
|
2010-06-03 13:01:01 -04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
if (TIFFGetField(in, TIFFTAG_PAGENUMBER, &pg0, &pg1)) {
|
|
|
|
if (pageNum < 0) /* only one input file */
|
|
|
|
TIFFSetField(out, TIFFTAG_PAGENUMBER, pg0, pg1);
|
|
|
|
else
|
|
|
|
TIFFSetField(out, TIFFTAG_PAGENUMBER, pageNum++, 0);
|
|
|
|
}
|
2007-06-20 04:35:57 -04:00
|
|
|
}
|
2003-06-04 06:36:54 -04:00
|
|
|
}
|
2004-01-28 07:12:01 -05:00
|
|
|
|
|
|
|
for (p = tags; p < &tags[NTAGS]; p++)
|
|
|
|
CopyTag(p->tag, p->count, p->type);
|
1999-07-27 17:50:26 -04:00
|
|
|
|
|
|
|
cf = pickCopyFunc(in, out, bitspersample, samplesperpixel);
|
2003-06-10 02:15:36 -04:00
|
|
|
return (cf ? (*cf)(in, out, length, width, samplesperpixel) : FALSE);
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy Functions.
|
|
|
|
*/
|
|
|
|
#define DECLAREcpFunc(x) \
|
|
|
|
static int x(TIFF* in, TIFF* out, \
|
|
|
|
uint32 imagelength, uint32 imagewidth, tsample_t spp)
|
|
|
|
|
|
|
|
#define DECLAREreadFunc(x) \
|
2005-12-13 11:42:39 -05:00
|
|
|
static int x(TIFF* in, \
|
1999-07-27 17:50:26 -04:00
|
|
|
uint8* buf, uint32 imagelength, uint32 imagewidth, tsample_t spp)
|
2005-12-13 11:42:39 -05:00
|
|
|
typedef int (*readFunc)(TIFF*, uint8*, uint32, uint32, tsample_t);
|
1999-07-27 17:50:26 -04:00
|
|
|
|
|
|
|
#define DECLAREwriteFunc(x) \
|
|
|
|
static int x(TIFF* out, \
|
|
|
|
uint8* buf, uint32 imagelength, uint32 imagewidth, tsample_t spp)
|
|
|
|
typedef int (*writeFunc)(TIFF*, uint8*, uint32, uint32, tsample_t);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Contig -> contig by scanline for rows/strip change.
|
|
|
|
*/
|
|
|
|
DECLAREcpFunc(cpContig2ContigByRow)
|
|
|
|
{
|
2010-06-25 08:24:13 -04:00
|
|
|
tsize_t scanlinesize = TIFFScanlineSize(in);
|
|
|
|
tdata_t buf;
|
1999-07-27 17:50:26 -04:00
|
|
|
uint32 row;
|
|
|
|
|
2020-03-21 08:19:16 -04:00
|
|
|
buf = limitMalloc(scanlinesize);
|
2010-06-25 08:24:13 -04:00
|
|
|
if (!buf)
|
|
|
|
return 0;
|
|
|
|
_TIFFmemset(buf, 0, scanlinesize);
|
1999-07-27 17:50:26 -04:00
|
|
|
(void) imagewidth; (void) spp;
|
|
|
|
for (row = 0; row < imagelength; row++) {
|
2005-12-20 11:50:21 -05:00
|
|
|
if (TIFFReadScanline(in, buf, row, 0) < 0 && !ignore) {
|
|
|
|
TIFFError(TIFFFileName(in),
|
|
|
|
"Error, can't read scanline %lu",
|
|
|
|
(unsigned long) row);
|
2005-12-14 08:59:52 -05:00
|
|
|
goto bad;
|
2005-12-20 11:50:21 -05:00
|
|
|
}
|
|
|
|
if (TIFFWriteScanline(out, buf, row, 0) < 0) {
|
|
|
|
TIFFError(TIFFFileName(out),
|
|
|
|
"Error, can't write scanline %lu",
|
|
|
|
(unsigned long) row);
|
1999-07-27 17:50:26 -04:00
|
|
|
goto bad;
|
2005-12-20 11:50:21 -05:00
|
|
|
}
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
_TIFFfree(buf);
|
2005-12-14 08:59:52 -05:00
|
|
|
return 1;
|
1999-07-27 17:50:26 -04:00
|
|
|
bad:
|
|
|
|
_TIFFfree(buf);
|
2005-12-14 08:59:52 -05:00
|
|
|
return 0;
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
|
2001-02-19 10:18:19 -05:00
|
|
|
|
|
|
|
typedef void biasFn (void *image, void *bias, uint32 pixels);
|
|
|
|
|
|
|
|
#define subtract(bits) \
|
|
|
|
static void subtract##bits (void *i, void *b, uint32 pixels)\
|
|
|
|
{\
|
|
|
|
uint##bits *image = i;\
|
|
|
|
uint##bits *bias = b;\
|
|
|
|
while (pixels--) {\
|
|
|
|
*image = *image > *bias ? *image-*bias : 0;\
|
|
|
|
image++, bias++; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
subtract(8)
|
|
|
|
subtract(16)
|
|
|
|
subtract(32)
|
|
|
|
|
|
|
|
static biasFn *lineSubtractFn (unsigned bits)
|
|
|
|
{
|
2007-06-20 04:35:57 -04:00
|
|
|
switch (bits) {
|
|
|
|
case 8: return subtract8;
|
|
|
|
case 16: return subtract16;
|
|
|
|
case 32: return subtract32;
|
|
|
|
}
|
|
|
|
return NULL;
|
2001-02-19 10:18:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Contig -> contig by scanline while subtracting a bias image.
|
|
|
|
*/
|
|
|
|
DECLAREcpFunc(cpBiasedContig2Contig)
|
|
|
|
{
|
2005-12-14 08:59:52 -05:00
|
|
|
if (spp == 1) {
|
2007-06-20 04:35:57 -04:00
|
|
|
tsize_t biasSize = TIFFScanlineSize(bias);
|
|
|
|
tsize_t bufSize = TIFFScanlineSize(in);
|
|
|
|
tdata_t buf, biasBuf;
|
|
|
|
uint32 biasWidth = 0, biasLength = 0;
|
|
|
|
TIFFGetField(bias, TIFFTAG_IMAGEWIDTH, &biasWidth);
|
|
|
|
TIFFGetField(bias, TIFFTAG_IMAGELENGTH, &biasLength);
|
|
|
|
if (biasSize == bufSize &&
|
|
|
|
imagelength == biasLength && imagewidth == biasWidth) {
|
|
|
|
uint16 sampleBits = 0;
|
|
|
|
biasFn *subtractLine;
|
|
|
|
TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &sampleBits);
|
|
|
|
subtractLine = lineSubtractFn (sampleBits);
|
|
|
|
if (subtractLine) {
|
|
|
|
uint32 row;
|
2020-03-21 08:19:16 -04:00
|
|
|
buf = limitMalloc(bufSize);
|
|
|
|
biasBuf = limitMalloc(bufSize);
|
2007-06-20 04:35:57 -04:00
|
|
|
for (row = 0; row < imagelength; row++) {
|
|
|
|
if (TIFFReadScanline(in, buf, row, 0) < 0
|
|
|
|
&& !ignore) {
|
|
|
|
TIFFError(TIFFFileName(in),
|
|
|
|
"Error, can't read scanline %lu",
|
|
|
|
(unsigned long) row);
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
if (TIFFReadScanline(bias, biasBuf, row, 0) < 0
|
|
|
|
&& !ignore) {
|
|
|
|
TIFFError(TIFFFileName(in),
|
|
|
|
"Error, can't read biased scanline %lu",
|
|
|
|
(unsigned long) row);
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
subtractLine (buf, biasBuf, imagewidth);
|
|
|
|
if (TIFFWriteScanline(out, buf, row, 0) < 0) {
|
|
|
|
TIFFError(TIFFFileName(out),
|
|
|
|
"Error, can't write scanline %lu",
|
|
|
|
(unsigned long) row);
|
|
|
|
goto bad;
|
|
|
|
}
|
2005-12-20 11:50:21 -05:00
|
|
|
}
|
2007-06-20 04:35:57 -04:00
|
|
|
|
|
|
|
_TIFFfree(buf);
|
|
|
|
_TIFFfree(biasBuf);
|
|
|
|
TIFFSetDirectory(bias,
|
|
|
|
TIFFCurrentDirectory(bias)); /* rewind */
|
|
|
|
return 1;
|
2005-12-14 08:59:52 -05:00
|
|
|
bad:
|
2007-06-20 04:35:57 -04:00
|
|
|
_TIFFfree(buf);
|
|
|
|
_TIFFfree(biasBuf);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
TIFFError(TIFFFileName(in),
|
|
|
|
"No support for biasing %d bit pixels\n",
|
|
|
|
sampleBits);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TIFFError(TIFFFileName(in),
|
2005-12-20 11:50:21 -05:00
|
|
|
"Bias image %s,%d\nis not the same size as %s,%d\n",
|
|
|
|
TIFFFileName(bias), TIFFCurrentDirectory(bias),
|
|
|
|
TIFFFileName(in), TIFFCurrentDirectory(in));
|
2007-06-20 04:35:57 -04:00
|
|
|
return 0;
|
2005-12-14 08:59:52 -05:00
|
|
|
} else {
|
2007-06-20 04:35:57 -04:00
|
|
|
TIFFError(TIFFFileName(in),
|
2005-12-20 11:50:21 -05:00
|
|
|
"Can't bias %s,%d as it has >1 Sample/Pixel\n",
|
|
|
|
TIFFFileName(in), TIFFCurrentDirectory(in));
|
2007-06-20 04:35:57 -04:00
|
|
|
return 0;
|
2001-02-19 10:18:19 -05:00
|
|
|
}
|
2005-12-14 08:59:52 -05:00
|
|
|
|
2001-02-19 10:18:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-07-27 17:50:26 -04:00
|
|
|
/*
|
|
|
|
* Strip -> strip for change in encoding.
|
|
|
|
*/
|
|
|
|
DECLAREcpFunc(cpDecodedStrips)
|
|
|
|
{
|
|
|
|
tsize_t stripsize = TIFFStripSize(in);
|
2020-03-21 08:19:16 -04:00
|
|
|
tdata_t buf = limitMalloc(stripsize);
|
1999-07-27 17:50:26 -04:00
|
|
|
|
|
|
|
(void) imagewidth; (void) spp;
|
|
|
|
if (buf) {
|
|
|
|
tstrip_t s, ns = TIFFNumberOfStrips(in);
|
|
|
|
uint32 row = 0;
|
2010-06-25 08:24:13 -04:00
|
|
|
_TIFFmemset(buf, 0, stripsize);
|
2016-12-02 17:13:32 -05:00
|
|
|
for (s = 0; s < ns && row < imagelength; s++) {
|
1999-07-27 17:50:26 -04:00
|
|
|
tsize_t cc = (row + rowsperstrip > imagelength) ?
|
|
|
|
TIFFVStripSize(in, imagelength - row) : stripsize;
|
2005-12-20 11:50:21 -05:00
|
|
|
if (TIFFReadEncodedStrip(in, s, buf, cc) < 0
|
|
|
|
&& !ignore) {
|
|
|
|
TIFFError(TIFFFileName(in),
|
2007-06-20 04:35:57 -04:00
|
|
|
"Error, can't read strip %lu",
|
|
|
|
(unsigned long) s);
|
2005-12-14 08:59:52 -05:00
|
|
|
goto bad;
|
2005-12-20 11:50:21 -05:00
|
|
|
}
|
|
|
|
if (TIFFWriteEncodedStrip(out, s, buf, cc) < 0) {
|
|
|
|
TIFFError(TIFFFileName(out),
|
2007-06-20 04:35:57 -04:00
|
|
|
"Error, can't write strip %lu",
|
|
|
|
(unsigned long) s);
|
2005-12-14 08:59:52 -05:00
|
|
|
goto bad;
|
2005-12-20 11:50:21 -05:00
|
|
|
}
|
1999-07-27 17:50:26 -04:00
|
|
|
row += rowsperstrip;
|
|
|
|
}
|
|
|
|
_TIFFfree(buf);
|
2005-12-14 08:59:52 -05:00
|
|
|
return 1;
|
2005-12-20 11:50:21 -05:00
|
|
|
} else {
|
|
|
|
TIFFError(TIFFFileName(in),
|
2007-06-20 04:35:57 -04:00
|
|
|
"Error, can't allocate memory buffer of size %lu "
|
|
|
|
"to read strips", (unsigned long) stripsize);
|
2005-12-20 11:50:21 -05:00
|
|
|
return 0;
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
2005-12-14 08:59:52 -05:00
|
|
|
|
|
|
|
bad:
|
|
|
|
_TIFFfree(buf);
|
|
|
|
return 0;
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Separate -> separate by row for rows/strip change.
|
|
|
|
*/
|
|
|
|
DECLAREcpFunc(cpSeparate2SeparateByRow)
|
|
|
|
{
|
2010-06-25 08:24:13 -04:00
|
|
|
tsize_t scanlinesize = TIFFScanlineSize(in);
|
|
|
|
tdata_t buf;
|
1999-07-27 17:50:26 -04:00
|
|
|
uint32 row;
|
|
|
|
tsample_t s;
|
|
|
|
|
|
|
|
(void) imagewidth;
|
2020-03-21 08:19:16 -04:00
|
|
|
buf = limitMalloc(scanlinesize);
|
2010-06-25 08:24:13 -04:00
|
|
|
if (!buf)
|
|
|
|
return 0;
|
|
|
|
_TIFFmemset(buf, 0, scanlinesize);
|
1999-07-27 17:50:26 -04:00
|
|
|
for (s = 0; s < spp; s++) {
|
|
|
|
for (row = 0; row < imagelength; row++) {
|
2005-12-20 11:50:21 -05:00
|
|
|
if (TIFFReadScanline(in, buf, row, s) < 0 && !ignore) {
|
|
|
|
TIFFError(TIFFFileName(in),
|
2007-06-20 04:35:57 -04:00
|
|
|
"Error, can't read scanline %lu",
|
|
|
|
(unsigned long) row);
|
2005-12-14 08:59:52 -05:00
|
|
|
goto bad;
|
2005-12-20 11:50:21 -05:00
|
|
|
}
|
|
|
|
if (TIFFWriteScanline(out, buf, row, s) < 0) {
|
|
|
|
TIFFError(TIFFFileName(out),
|
2007-06-20 04:35:57 -04:00
|
|
|
"Error, can't write scanline %lu",
|
|
|
|
(unsigned long) row);
|
1999-07-27 17:50:26 -04:00
|
|
|
goto bad;
|
2005-12-20 11:50:21 -05:00
|
|
|
}
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_TIFFfree(buf);
|
2005-12-14 08:59:52 -05:00
|
|
|
return 1;
|
1999-07-27 17:50:26 -04:00
|
|
|
bad:
|
|
|
|
_TIFFfree(buf);
|
2005-12-14 08:59:52 -05:00
|
|
|
return 0;
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Contig -> separate by row.
|
|
|
|
*/
|
|
|
|
DECLAREcpFunc(cpContig2SeparateByRow)
|
|
|
|
{
|
2010-06-25 08:24:13 -04:00
|
|
|
tsize_t scanlinesizein = TIFFScanlineSize(in);
|
|
|
|
tsize_t scanlinesizeout = TIFFScanlineSize(out);
|
|
|
|
tdata_t inbuf;
|
|
|
|
tdata_t outbuf;
|
1999-07-27 17:50:26 -04:00
|
|
|
register uint8 *inp, *outp;
|
|
|
|
register uint32 n;
|
|
|
|
uint32 row;
|
|
|
|
tsample_t s;
|
2017-01-11 14:25:44 -05:00
|
|
|
uint16 bps = 0;
|
|
|
|
|
|
|
|
(void) TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bps);
|
|
|
|
if( bps != 8 )
|
|
|
|
{
|
|
|
|
TIFFError(TIFFFileName(in),
|
|
|
|
"Error, can only handle BitsPerSample=8 in %s",
|
|
|
|
"cpContig2SeparateByRow");
|
|
|
|
return 0;
|
|
|
|
}
|
1999-07-27 17:50:26 -04:00
|
|
|
|
2020-03-21 08:19:16 -04:00
|
|
|
inbuf = limitMalloc(scanlinesizein);
|
|
|
|
outbuf = limitMalloc(scanlinesizeout);
|
2010-06-25 08:24:13 -04:00
|
|
|
if (!inbuf || !outbuf)
|
2015-05-28 09:17:35 -04:00
|
|
|
goto bad;
|
2010-06-25 08:24:13 -04:00
|
|
|
_TIFFmemset(inbuf, 0, scanlinesizein);
|
|
|
|
_TIFFmemset(outbuf, 0, scanlinesizeout);
|
1999-07-27 17:50:26 -04:00
|
|
|
/* unpack channels */
|
|
|
|
for (s = 0; s < spp; s++) {
|
|
|
|
for (row = 0; row < imagelength; row++) {
|
2005-12-20 11:50:21 -05:00
|
|
|
if (TIFFReadScanline(in, inbuf, row, 0) < 0
|
|
|
|
&& !ignore) {
|
|
|
|
TIFFError(TIFFFileName(in),
|
2007-06-20 04:35:57 -04:00
|
|
|
"Error, can't read scanline %lu",
|
|
|
|
(unsigned long) row);
|
2005-12-14 08:59:52 -05:00
|
|
|
goto bad;
|
2005-12-20 11:50:21 -05:00
|
|
|
}
|
1999-07-27 17:50:26 -04:00
|
|
|
inp = ((uint8*)inbuf) + s;
|
|
|
|
outp = (uint8*)outbuf;
|
|
|
|
for (n = imagewidth; n-- > 0;) {
|
|
|
|
*outp++ = *inp;
|
|
|
|
inp += spp;
|
|
|
|
}
|
2005-12-20 11:50:21 -05:00
|
|
|
if (TIFFWriteScanline(out, outbuf, row, s) < 0) {
|
|
|
|
TIFFError(TIFFFileName(out),
|
2007-06-20 04:35:57 -04:00
|
|
|
"Error, can't write scanline %lu",
|
|
|
|
(unsigned long) row);
|
1999-07-27 17:50:26 -04:00
|
|
|
goto bad;
|
2005-12-20 11:50:21 -05:00
|
|
|
}
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (inbuf) _TIFFfree(inbuf);
|
|
|
|
if (outbuf) _TIFFfree(outbuf);
|
2005-12-14 08:59:52 -05:00
|
|
|
return 1;
|
1999-07-27 17:50:26 -04:00
|
|
|
bad:
|
|
|
|
if (inbuf) _TIFFfree(inbuf);
|
|
|
|
if (outbuf) _TIFFfree(outbuf);
|
2005-12-14 08:59:52 -05:00
|
|
|
return 0;
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Separate -> contig by row.
|
|
|
|
*/
|
|
|
|
DECLAREcpFunc(cpSeparate2ContigByRow)
|
|
|
|
{
|
2010-06-25 08:24:13 -04:00
|
|
|
tsize_t scanlinesizein = TIFFScanlineSize(in);
|
|
|
|
tsize_t scanlinesizeout = TIFFScanlineSize(out);
|
|
|
|
tdata_t inbuf;
|
|
|
|
tdata_t outbuf;
|
1999-07-27 17:50:26 -04:00
|
|
|
register uint8 *inp, *outp;
|
|
|
|
register uint32 n;
|
|
|
|
uint32 row;
|
|
|
|
tsample_t s;
|
2017-01-11 14:25:44 -05:00
|
|
|
uint16 bps = 0;
|
|
|
|
|
|
|
|
(void) TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bps);
|
|
|
|
if( bps != 8 )
|
|
|
|
{
|
|
|
|
TIFFError(TIFFFileName(in),
|
|
|
|
"Error, can only handle BitsPerSample=8 in %s",
|
|
|
|
"cpSeparate2ContigByRow");
|
|
|
|
return 0;
|
|
|
|
}
|
1999-07-27 17:50:26 -04:00
|
|
|
|
2020-03-21 08:19:16 -04:00
|
|
|
inbuf = limitMalloc(scanlinesizein);
|
|
|
|
outbuf = limitMalloc(scanlinesizeout);
|
2010-06-25 08:24:13 -04:00
|
|
|
if (!inbuf || !outbuf)
|
2015-05-28 09:17:35 -04:00
|
|
|
goto bad;
|
2010-06-25 08:24:13 -04:00
|
|
|
_TIFFmemset(inbuf, 0, scanlinesizein);
|
|
|
|
_TIFFmemset(outbuf, 0, scanlinesizeout);
|
1999-07-27 17:50:26 -04:00
|
|
|
for (row = 0; row < imagelength; row++) {
|
|
|
|
/* merge channels */
|
|
|
|
for (s = 0; s < spp; s++) {
|
2005-12-20 11:50:21 -05:00
|
|
|
if (TIFFReadScanline(in, inbuf, row, s) < 0
|
|
|
|
&& !ignore) {
|
|
|
|
TIFFError(TIFFFileName(in),
|
2007-06-20 04:35:57 -04:00
|
|
|
"Error, can't read scanline %lu",
|
|
|
|
(unsigned long) row);
|
2005-12-14 08:59:52 -05:00
|
|
|
goto bad;
|
2005-12-20 11:50:21 -05:00
|
|
|
}
|
1999-07-27 17:50:26 -04:00
|
|
|
inp = (uint8*)inbuf;
|
|
|
|
outp = ((uint8*)outbuf) + s;
|
|
|
|
for (n = imagewidth; n-- > 0;) {
|
|
|
|
*outp = *inp++;
|
|
|
|
outp += spp;
|
|
|
|
}
|
|
|
|
}
|
2005-12-20 11:50:21 -05:00
|
|
|
if (TIFFWriteScanline(out, outbuf, row, 0) < 0) {
|
|
|
|
TIFFError(TIFFFileName(out),
|
2007-06-20 04:35:57 -04:00
|
|
|
"Error, can't write scanline %lu",
|
|
|
|
(unsigned long) row);
|
1999-07-27 17:50:26 -04:00
|
|
|
goto bad;
|
2005-12-20 11:50:21 -05:00
|
|
|
}
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
if (inbuf) _TIFFfree(inbuf);
|
|
|
|
if (outbuf) _TIFFfree(outbuf);
|
2005-12-20 11:50:21 -05:00
|
|
|
return 1;
|
1999-07-27 17:50:26 -04:00
|
|
|
bad:
|
|
|
|
if (inbuf) _TIFFfree(inbuf);
|
|
|
|
if (outbuf) _TIFFfree(outbuf);
|
2005-12-20 11:50:21 -05:00
|
|
|
return 0;
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cpStripToTile(uint8* out, uint8* in,
|
2016-12-03 11:40:01 -05:00
|
|
|
uint32 rows, uint32 cols, int outskew, int64 inskew)
|
1999-07-27 17:50:26 -04:00
|
|
|
{
|
|
|
|
while (rows-- > 0) {
|
|
|
|
uint32 j = cols;
|
|
|
|
while (j-- > 0)
|
|
|
|
*out++ = *in++;
|
|
|
|
out += outskew;
|
|
|
|
in += inskew;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cpContigBufToSeparateBuf(uint8* out, uint8* in,
|
2007-06-20 04:35:57 -04:00
|
|
|
uint32 rows, uint32 cols, int outskew, int inskew, tsample_t spp,
|
|
|
|
int bytes_per_sample )
|
1999-07-27 17:50:26 -04:00
|
|
|
{
|
|
|
|
while (rows-- > 0) {
|
|
|
|
uint32 j = cols;
|
|
|
|
while (j-- > 0)
|
2007-06-20 04:35:57 -04:00
|
|
|
{
|
|
|
|
int n = bytes_per_sample;
|
|
|
|
|
|
|
|
while( n-- ) {
|
|
|
|
*out++ = *in++;
|
|
|
|
}
|
|
|
|
in += (spp-1) * bytes_per_sample;
|
|
|
|
}
|
1999-07-27 17:50:26 -04:00
|
|
|
out += outskew;
|
|
|
|
in += inskew;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cpSeparateBufToContigBuf(uint8* out, uint8* in,
|
2007-06-20 04:35:57 -04:00
|
|
|
uint32 rows, uint32 cols, int outskew, int inskew, tsample_t spp,
|
|
|
|
int bytes_per_sample)
|
1999-07-27 17:50:26 -04:00
|
|
|
{
|
|
|
|
while (rows-- > 0) {
|
|
|
|
uint32 j = cols;
|
1999-08-16 14:27:51 -04:00
|
|
|
while (j-- > 0) {
|
2007-06-20 04:35:57 -04:00
|
|
|
int n = bytes_per_sample;
|
1999-08-16 14:27:51 -04:00
|
|
|
|
2007-06-20 04:35:57 -04:00
|
|
|
while( n-- ) {
|
|
|
|
*out++ = *in++;
|
|
|
|
}
|
|
|
|
out += (spp-1)*bytes_per_sample;
|
|
|
|
}
|
1999-07-27 17:50:26 -04:00
|
|
|
out += outskew;
|
|
|
|
in += inskew;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cpImage(TIFF* in, TIFF* out, readFunc fin, writeFunc fout,
|
|
|
|
uint32 imagelength, uint32 imagewidth, tsample_t spp)
|
|
|
|
{
|
2005-12-13 11:42:39 -05:00
|
|
|
int status = 0;
|
2005-12-20 11:50:21 -05:00
|
|
|
tdata_t buf = NULL;
|
|
|
|
tsize_t scanlinesize = TIFFRasterScanlineSize(in);
|
2007-06-20 04:35:57 -04:00
|
|
|
tsize_t bytes = scanlinesize * (tsize_t)imagelength;
|
|
|
|
/*
|
|
|
|
* XXX: Check for integer overflow.
|
|
|
|
*/
|
|
|
|
if (scanlinesize
|
2005-12-20 11:50:21 -05:00
|
|
|
&& imagelength
|
|
|
|
&& bytes / (tsize_t)imagelength == scanlinesize) {
|
2020-03-21 08:19:16 -04:00
|
|
|
buf = limitMalloc(bytes);
|
2005-12-20 11:50:21 -05:00
|
|
|
if (buf) {
|
2007-06-20 04:35:57 -04:00
|
|
|
if ((*fin)(in, (uint8*)buf, imagelength,
|
|
|
|
imagewidth, spp)) {
|
2005-12-20 11:50:21 -05:00
|
|
|
status = (*fout)(out, (uint8*)buf,
|
2007-06-20 04:35:57 -04:00
|
|
|
imagelength, imagewidth, spp);
|
2005-12-20 11:50:21 -05:00
|
|
|
}
|
|
|
|
_TIFFfree(buf);
|
|
|
|
} else {
|
|
|
|
TIFFError(TIFFFileName(in),
|
2007-06-20 04:35:57 -04:00
|
|
|
"Error, can't allocate space for image buffer");
|
2005-12-13 11:42:39 -05:00
|
|
|
}
|
2005-12-20 11:50:21 -05:00
|
|
|
} else {
|
|
|
|
TIFFError(TIFFFileName(in), "Error, no space for image buffer");
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
2005-12-20 11:50:21 -05:00
|
|
|
|
2005-12-13 11:42:39 -05:00
|
|
|
return status;
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
DECLAREreadFunc(readContigStripsIntoBuffer)
|
|
|
|
{
|
|
|
|
tsize_t scanlinesize = TIFFScanlineSize(in);
|
2004-01-28 07:12:01 -05:00
|
|
|
uint8* bufp = buf;
|
1999-07-27 17:50:26 -04:00
|
|
|
uint32 row;
|
|
|
|
|
|
|
|
(void) imagewidth; (void) spp;
|
|
|
|
for (row = 0; row < imagelength; row++) {
|
2005-12-20 11:50:21 -05:00
|
|
|
if (TIFFReadScanline(in, (tdata_t) bufp, row, 0) < 0
|
|
|
|
&& !ignore) {
|
|
|
|
TIFFError(TIFFFileName(in),
|
2007-06-20 04:35:57 -04:00
|
|
|
"Error, can't read scanline %lu",
|
|
|
|
(unsigned long) row);
|
2005-12-13 11:42:39 -05:00
|
|
|
return 0;
|
2005-12-20 11:50:21 -05:00
|
|
|
}
|
1999-07-27 17:50:26 -04:00
|
|
|
bufp += scanlinesize;
|
|
|
|
}
|
2005-12-13 11:42:39 -05:00
|
|
|
|
|
|
|
return 1;
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
DECLAREreadFunc(readSeparateStripsIntoBuffer)
|
|
|
|
{
|
2005-12-13 11:42:39 -05:00
|
|
|
int status = 1;
|
1999-07-27 17:50:26 -04:00
|
|
|
tsize_t scanlinesize = TIFFScanlineSize(in);
|
2008-05-23 13:24:41 -04:00
|
|
|
tdata_t scanline;
|
2005-12-13 11:42:39 -05:00
|
|
|
if (!scanlinesize)
|
|
|
|
return 0;
|
1999-07-27 17:50:26 -04:00
|
|
|
|
2020-03-21 08:19:16 -04:00
|
|
|
scanline = limitMalloc(scanlinesize);
|
2010-06-25 08:24:13 -04:00
|
|
|
if (!scanline)
|
|
|
|
return 0;
|
|
|
|
_TIFFmemset(scanline, 0, scanlinesize);
|
1999-07-27 17:50:26 -04:00
|
|
|
(void) imagewidth;
|
|
|
|
if (scanline) {
|
|
|
|
uint8* bufp = (uint8*) buf;
|
|
|
|
uint32 row;
|
|
|
|
tsample_t s;
|
2005-12-13 11:42:39 -05:00
|
|
|
for (row = 0; row < imagelength; row++) {
|
1999-07-27 17:50:26 -04:00
|
|
|
/* merge channels */
|
|
|
|
for (s = 0; s < spp; s++) {
|
|
|
|
uint8* bp = bufp + s;
|
|
|
|
tsize_t n = scanlinesize;
|
2007-06-20 04:35:57 -04:00
|
|
|
uint8* sbuf = scanline;
|
1999-07-27 17:50:26 -04:00
|
|
|
|
2005-12-13 11:42:39 -05:00
|
|
|
if (TIFFReadScanline(in, scanline, row, s) < 0
|
|
|
|
&& !ignore) {
|
2005-12-20 11:50:21 -05:00
|
|
|
TIFFError(TIFFFileName(in),
|
2007-06-20 04:35:57 -04:00
|
|
|
"Error, can't read scanline %lu",
|
|
|
|
(unsigned long) row);
|
|
|
|
status = 0;
|
1999-07-27 17:50:26 -04:00
|
|
|
goto done;
|
2005-12-13 11:42:39 -05:00
|
|
|
}
|
1999-07-27 17:50:26 -04:00
|
|
|
while (n-- > 0)
|
1999-08-16 14:27:51 -04:00
|
|
|
*bp = *sbuf++, bp += spp;
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
1999-08-16 14:27:51 -04:00
|
|
|
bufp += scanlinesize * spp;
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
2005-12-20 11:50:21 -05:00
|
|
|
}
|
1999-08-16 14:27:51 -04:00
|
|
|
|
1999-07-27 17:50:26 -04:00
|
|
|
done:
|
2005-12-20 11:50:21 -05:00
|
|
|
_TIFFfree(scanline);
|
|
|
|
return status;
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
DECLAREreadFunc(readContigTilesIntoBuffer)
|
|
|
|
{
|
2005-12-13 11:42:39 -05:00
|
|
|
int status = 1;
|
2010-06-25 08:24:13 -04:00
|
|
|
tsize_t tilesize = TIFFTileSize(in);
|
|
|
|
tdata_t tilebuf;
|
1999-07-27 17:50:26 -04:00
|
|
|
uint32 imagew = TIFFScanlineSize(in);
|
|
|
|
uint32 tilew = TIFFTileRowSize(in);
|
2016-12-03 11:40:01 -05:00
|
|
|
int64 iskew = (int64)imagew - (int64)tilew;
|
1999-07-27 17:50:26 -04:00
|
|
|
uint8* bufp = (uint8*) buf;
|
|
|
|
uint32 tw, tl;
|
|
|
|
uint32 row;
|
|
|
|
|
|
|
|
(void) spp;
|
2020-03-21 08:19:16 -04:00
|
|
|
tilebuf = limitMalloc(tilesize);
|
1999-07-27 17:50:26 -04:00
|
|
|
if (tilebuf == 0)
|
2005-12-13 11:42:39 -05:00
|
|
|
return 0;
|
2010-06-25 08:24:13 -04:00
|
|
|
_TIFFmemset(tilebuf, 0, tilesize);
|
1999-07-27 17:50:26 -04:00
|
|
|
(void) TIFFGetField(in, TIFFTAG_TILEWIDTH, &tw);
|
|
|
|
(void) TIFFGetField(in, TIFFTAG_TILELENGTH, &tl);
|
1999-08-16 14:27:51 -04:00
|
|
|
|
1999-07-27 17:50:26 -04:00
|
|
|
for (row = 0; row < imagelength; row += tl) {
|
|
|
|
uint32 nrow = (row+tl > imagelength) ? imagelength-row : tl;
|
|
|
|
uint32 colb = 0;
|
|
|
|
uint32 col;
|
|
|
|
|
2016-10-08 11:54:56 -04:00
|
|
|
for (col = 0; col < imagewidth && colb < imagew; col += tw) {
|
2005-12-13 11:42:39 -05:00
|
|
|
if (TIFFReadTile(in, tilebuf, col, row, 0, 0) < 0
|
|
|
|
&& !ignore) {
|
2005-12-20 11:50:21 -05:00
|
|
|
TIFFError(TIFFFileName(in),
|
2007-06-20 04:35:57 -04:00
|
|
|
"Error, can't read tile at %lu %lu",
|
|
|
|
(unsigned long) col,
|
|
|
|
(unsigned long) row);
|
2005-12-13 11:42:39 -05:00
|
|
|
status = 0;
|
1999-07-27 17:50:26 -04:00
|
|
|
goto done;
|
2005-12-13 11:42:39 -05:00
|
|
|
}
|
2016-12-03 11:40:01 -05:00
|
|
|
if (colb > iskew) {
|
1999-07-27 17:50:26 -04:00
|
|
|
uint32 width = imagew - colb;
|
|
|
|
uint32 oskew = tilew - width;
|
|
|
|
cpStripToTile(bufp + colb,
|
2007-06-20 04:35:57 -04:00
|
|
|
tilebuf, nrow, width,
|
|
|
|
oskew + iskew, oskew );
|
1999-07-27 17:50:26 -04:00
|
|
|
} else
|
|
|
|
cpStripToTile(bufp + colb,
|
2007-06-20 04:35:57 -04:00
|
|
|
tilebuf, nrow, tilew,
|
|
|
|
iskew, 0);
|
1999-07-27 17:50:26 -04:00
|
|
|
colb += tilew;
|
|
|
|
}
|
|
|
|
bufp += imagew * nrow;
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
_TIFFfree(tilebuf);
|
2005-12-13 11:42:39 -05:00
|
|
|
return status;
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
DECLAREreadFunc(readSeparateTilesIntoBuffer)
|
|
|
|
{
|
2005-12-13 11:42:39 -05:00
|
|
|
int status = 1;
|
1999-08-16 14:27:51 -04:00
|
|
|
uint32 imagew = TIFFRasterScanlineSize(in);
|
1999-07-27 17:50:26 -04:00
|
|
|
uint32 tilew = TIFFTileRowSize(in);
|
2019-02-11 04:05:33 -05:00
|
|
|
int iskew;
|
2010-06-25 08:24:13 -04:00
|
|
|
tsize_t tilesize = TIFFTileSize(in);
|
|
|
|
tdata_t tilebuf;
|
1999-07-27 17:50:26 -04:00
|
|
|
uint8* bufp = (uint8*) buf;
|
|
|
|
uint32 tw, tl;
|
|
|
|
uint32 row;
|
2016-12-03 09:42:40 -05:00
|
|
|
uint16 bps = 0, bytes_per_sample;
|
1999-07-27 17:50:26 -04:00
|
|
|
|
2019-06-12 06:23:33 -04:00
|
|
|
if (tilew && spp > (INT_MAX / tilew))
|
2019-02-11 04:05:33 -05:00
|
|
|
{
|
|
|
|
TIFFError(TIFFFileName(in), "Error, cannot handle that much samples per tile row (Tile Width * Samples/Pixel)");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
iskew = imagew - tilew*spp;
|
2020-03-21 08:19:16 -04:00
|
|
|
tilebuf = limitMalloc(tilesize);
|
1999-07-27 17:50:26 -04:00
|
|
|
if (tilebuf == 0)
|
2005-12-13 11:42:39 -05:00
|
|
|
return 0;
|
2010-06-25 08:24:13 -04:00
|
|
|
_TIFFmemset(tilebuf, 0, tilesize);
|
1999-07-27 17:50:26 -04:00
|
|
|
(void) TIFFGetField(in, TIFFTAG_TILEWIDTH, &tw);
|
|
|
|
(void) TIFFGetField(in, TIFFTAG_TILELENGTH, &tl);
|
1999-08-16 14:27:51 -04:00
|
|
|
(void) TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bps);
|
2016-12-03 09:42:40 -05:00
|
|
|
if( bps == 0 )
|
|
|
|
{
|
|
|
|
TIFFError(TIFFFileName(in), "Error, cannot read BitsPerSample");
|
|
|
|
status = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
2016-12-03 11:50:02 -05:00
|
|
|
if( (bps % 8) != 0 )
|
|
|
|
{
|
|
|
|
TIFFError(TIFFFileName(in), "Error, cannot handle BitsPerSample that is not a multiple of 8");
|
|
|
|
status = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
2007-06-20 04:35:57 -04:00
|
|
|
bytes_per_sample = bps/8;
|
1999-08-16 14:27:51 -04:00
|
|
|
|
1999-07-27 17:50:26 -04:00
|
|
|
for (row = 0; row < imagelength; row += tl) {
|
|
|
|
uint32 nrow = (row+tl > imagelength) ? imagelength-row : tl;
|
|
|
|
uint32 colb = 0;
|
|
|
|
uint32 col;
|
|
|
|
|
|
|
|
for (col = 0; col < imagewidth; col += tw) {
|
|
|
|
tsample_t s;
|
|
|
|
|
|
|
|
for (s = 0; s < spp; s++) {
|
2005-12-13 11:42:39 -05:00
|
|
|
if (TIFFReadTile(in, tilebuf, col, row, 0, s) < 0
|
|
|
|
&& !ignore) {
|
2005-12-20 11:50:21 -05:00
|
|
|
TIFFError(TIFFFileName(in),
|
2007-06-20 04:35:57 -04:00
|
|
|
"Error, can't read tile at %lu %lu, "
|
|
|
|
"sample %lu",
|
|
|
|
(unsigned long) col,
|
|
|
|
(unsigned long) row,
|
|
|
|
(unsigned long) s);
|
2005-12-13 11:42:39 -05:00
|
|
|
status = 0;
|
1999-07-27 17:50:26 -04:00
|
|
|
goto done;
|
2005-12-13 11:42:39 -05:00
|
|
|
}
|
1999-07-27 17:50:26 -04:00
|
|
|
/*
|
|
|
|
* Tile is clipped horizontally. Calculate
|
|
|
|
* visible portion and skewing factors.
|
|
|
|
*/
|
1999-08-16 14:27:51 -04:00
|
|
|
if (colb + tilew*spp > imagew) {
|
1999-07-27 17:50:26 -04:00
|
|
|
uint32 width = imagew - colb;
|
1999-08-16 14:27:51 -04:00
|
|
|
int oskew = tilew*spp - width;
|
|
|
|
cpSeparateBufToContigBuf(
|
2007-06-20 04:35:57 -04:00
|
|
|
bufp+colb+s*bytes_per_sample,
|
1999-08-16 14:27:51 -04:00
|
|
|
tilebuf, nrow,
|
2007-06-20 04:35:57 -04:00
|
|
|
width/(spp*bytes_per_sample),
|
1999-08-16 14:27:51 -04:00
|
|
|
oskew + iskew,
|
2007-06-20 04:35:57 -04:00
|
|
|
oskew/spp, spp,
|
|
|
|
bytes_per_sample);
|
1999-07-27 17:50:26 -04:00
|
|
|
} else
|
1999-08-16 14:27:51 -04:00
|
|
|
cpSeparateBufToContigBuf(
|
2007-06-20 04:35:57 -04:00
|
|
|
bufp+colb+s*bytes_per_sample,
|
1999-07-27 17:50:26 -04:00
|
|
|
tilebuf, nrow, tw,
|
1999-08-16 14:27:51 -04:00
|
|
|
iskew, 0, spp,
|
2007-06-20 04:35:57 -04:00
|
|
|
bytes_per_sample);
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
1999-08-16 14:27:51 -04:00
|
|
|
colb += tilew*spp;
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
bufp += imagew * nrow;
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
_TIFFfree(tilebuf);
|
2005-12-13 11:42:39 -05:00
|
|
|
return status;
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
DECLAREwriteFunc(writeBufferToContigStrips)
|
|
|
|
{
|
|
|
|
uint32 row, rowsperstrip;
|
|
|
|
tstrip_t strip = 0;
|
|
|
|
|
|
|
|
(void) imagewidth; (void) spp;
|
|
|
|
(void) TIFFGetFieldDefaulted(out, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
|
|
|
|
for (row = 0; row < imagelength; row += rowsperstrip) {
|
|
|
|
uint32 nrows = (row+rowsperstrip > imagelength) ?
|
|
|
|
imagelength-row : rowsperstrip;
|
|
|
|
tsize_t stripsize = TIFFVStripSize(out, nrows);
|
2005-12-20 11:50:21 -05:00
|
|
|
if (TIFFWriteEncodedStrip(out, strip++, buf, stripsize) < 0) {
|
|
|
|
TIFFError(TIFFFileName(out),
|
2008-12-31 18:48:01 -05:00
|
|
|
"Error, can't write strip %u", strip - 1);
|
2005-12-20 11:50:21 -05:00
|
|
|
return 0;
|
|
|
|
}
|
1999-08-16 14:27:51 -04:00
|
|
|
buf += stripsize;
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
2005-12-20 11:50:21 -05:00
|
|
|
return 1;
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
DECLAREwriteFunc(writeBufferToSeparateStrips)
|
|
|
|
{
|
|
|
|
uint32 rowsize = imagewidth * spp;
|
|
|
|
uint32 rowsperstrip;
|
2010-06-25 08:24:13 -04:00
|
|
|
tsize_t stripsize = TIFFStripSize(out);
|
|
|
|
tdata_t obuf;
|
1999-07-27 17:50:26 -04:00
|
|
|
tstrip_t strip = 0;
|
|
|
|
tsample_t s;
|
|
|
|
|
2020-03-21 08:19:16 -04:00
|
|
|
obuf = limitMalloc(stripsize);
|
1999-07-27 17:50:26 -04:00
|
|
|
if (obuf == NULL)
|
|
|
|
return (0);
|
2010-06-25 08:24:13 -04:00
|
|
|
_TIFFmemset(obuf, 0, stripsize);
|
1999-07-27 17:50:26 -04:00
|
|
|
(void) TIFFGetFieldDefaulted(out, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
|
|
|
|
for (s = 0; s < spp; s++) {
|
|
|
|
uint32 row;
|
|
|
|
for (row = 0; row < imagelength; row += rowsperstrip) {
|
|
|
|
uint32 nrows = (row+rowsperstrip > imagelength) ?
|
|
|
|
imagelength-row : rowsperstrip;
|
|
|
|
tsize_t stripsize = TIFFVStripSize(out, nrows);
|
|
|
|
|
|
|
|
cpContigBufToSeparateBuf(
|
2007-06-20 04:35:57 -04:00
|
|
|
obuf, (uint8*) buf + row*rowsize + s,
|
1999-08-16 14:27:51 -04:00
|
|
|
nrows, imagewidth, 0, 0, spp, 1);
|
1999-07-27 17:50:26 -04:00
|
|
|
if (TIFFWriteEncodedStrip(out, strip++, obuf, stripsize) < 0) {
|
2005-12-20 11:50:21 -05:00
|
|
|
TIFFError(TIFFFileName(out),
|
2008-12-31 18:48:01 -05:00
|
|
|
"Error, can't write strip %u",
|
2007-06-20 04:35:57 -04:00
|
|
|
strip - 1);
|
1999-07-27 17:50:26 -04:00
|
|
|
_TIFFfree(obuf);
|
2005-12-20 11:50:21 -05:00
|
|
|
return 0;
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_TIFFfree(obuf);
|
2005-12-20 11:50:21 -05:00
|
|
|
return 1;
|
1999-07-27 17:50:26 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
DECLAREwriteFunc(writeBufferToContigTiles)
|
|
|
|
{
|
|
|
|
uint32 imagew = TIFFScanlineSize(out);
|
|
|
|
uint32 tilew = TIFFTileRowSize(out);
|
|
|
|
int iskew = imagew - tilew;
|
2010-06-25 08:24:13 -04:00
|
|
|
tsize_t tilesize = TIFFTileSize(out);
|
|
|
|
tdata_t obuf;
|
1999-07-27 17:50:26 -04:00
|
|
|
uint8* bufp = (uint8*) buf;
|
|
|
|
uint32 tl, tw;
|
|
|
|
uint32 row;
|
|
|
|
|
|
|
|
(void) spp;
|
2010-06-25 08:24:13 -04:00
|
|
|
|
2020-03-21 08:19:16 -04:00
|
|
|
obuf = limitMalloc(TIFFTileSize(out));
|
1999-07-27 17:50:26 -04:00
|
|
|
if (obuf == NULL)
|
2005-12-20 11:50:21 -05:00
|
|
|
return 0;
|
2010-06-25 08:24:13 -04:00
|
|
|
_TIFFmemset(obuf, 0, tilesize);
|
1999-07-27 17:50:26 -04:00
|
|
|
(void) TIFFGetField(out, TIFFTAG_TILELENGTH, &tl);
|
|
|
|
(void) TIFFGetField(out, TIFFTAG_TILEWIDTH, &tw);
|
|
|
|
for (row = 0; row < imagelength; row += tilelength) {
|
|
|
|
uint32 nrow = (row+tl > imagelength) ? imagelength-row : tl;
|
|
|
|
uint32 colb = 0;
|
|
|
|
uint32 col;
|
|
|
|
|
2016-10-08 11:54:56 -04:00
|
|
|
for (col = 0; col < imagewidth && colb < imagew; col += tw) {
|
1999-07-27 17:50:26 -04:00
|
|
|
/*
|
|
|
|
* Tile is clipped horizontally. Calculate
|
|
|
|
* visible portion and skewing factors.
|
|
|
|
*/
|
|
|
|
if (colb + tilew > imagew) {
|
|
|
|
uint32 width = imagew - colb;
|
|
|
|
int oskew = tilew - width;
|
|
|
|
cpStripToTile(obuf, bufp + colb, nrow, width,
|
|
|
|
oskew, oskew + iskew);
|
|
|
|
} else
|
|
|
|
cpStripToTile(obuf, bufp + colb, nrow, tilew,
|
|
|
|
0, iskew);
|
|
|
|
if (TIFFWriteTile(out, obuf, col, row, 0, 0) < 0) {
|
2005-12-20 11:50:21 -05:00
|
|
|
TIFFError(TIFFFileName(out),
|
2007-06-20 04:35:57 -04:00
|
|
|
"Error, can't write tile at %lu %lu",
|
|
|
|
(unsigned long) col,
|
|
|
|
(unsigned long) row);
|
1999-07-27 17:50:26 -04:00
|
|
|
_TIFFfree(obuf);
|
2005-12-20 11:50:21 -05:00
|
|
|
return 0;
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
colb += tilew;
|
|
|
|
}
|
|
|
|
bufp += nrow * imagew;
|
|
|
|
}
|
|
|
|
_TIFFfree(obuf);
|
2005-12-20 11:50:21 -05:00
|
|
|
return 1;
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
DECLAREwriteFunc(writeBufferToSeparateTiles)
|
|
|
|
{
|
|
|
|
uint32 imagew = TIFFScanlineSize(out);
|
|
|
|
tsize_t tilew = TIFFTileRowSize(out);
|
|
|
|
uint32 iimagew = TIFFRasterScanlineSize(out);
|
|
|
|
int iskew = iimagew - tilew*spp;
|
2010-06-25 08:24:13 -04:00
|
|
|
tsize_t tilesize = TIFFTileSize(out);
|
|
|
|
tdata_t obuf;
|
1999-07-27 17:50:26 -04:00
|
|
|
uint8* bufp = (uint8*) buf;
|
|
|
|
uint32 tl, tw;
|
|
|
|
uint32 row;
|
2016-12-03 10:44:15 -05:00
|
|
|
uint16 bps = 0, bytes_per_sample;
|
1999-07-27 17:50:26 -04:00
|
|
|
|
2020-03-21 08:19:16 -04:00
|
|
|
obuf = limitMalloc(TIFFTileSize(out));
|
1999-07-27 17:50:26 -04:00
|
|
|
if (obuf == NULL)
|
2005-12-20 11:50:21 -05:00
|
|
|
return 0;
|
2010-06-25 08:24:13 -04:00
|
|
|
_TIFFmemset(obuf, 0, tilesize);
|
1999-07-27 17:50:26 -04:00
|
|
|
(void) TIFFGetField(out, TIFFTAG_TILELENGTH, &tl);
|
|
|
|
(void) TIFFGetField(out, TIFFTAG_TILEWIDTH, &tw);
|
1999-08-16 14:27:51 -04:00
|
|
|
(void) TIFFGetField(out, TIFFTAG_BITSPERSAMPLE, &bps);
|
2016-12-03 10:44:15 -05:00
|
|
|
if( bps == 0 )
|
|
|
|
{
|
|
|
|
TIFFError(TIFFFileName(out), "Error, cannot read BitsPerSample");
|
|
|
|
_TIFFfree(obuf);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-12-03 11:50:02 -05:00
|
|
|
if( (bps % 8) != 0 )
|
|
|
|
{
|
|
|
|
TIFFError(TIFFFileName(out), "Error, cannot handle BitsPerSample that is not a multiple of 8");
|
|
|
|
_TIFFfree(obuf);
|
|
|
|
return 0;
|
|
|
|
}
|
2007-06-20 04:35:57 -04:00
|
|
|
bytes_per_sample = bps/8;
|
|
|
|
|
1999-07-27 17:50:26 -04:00
|
|
|
for (row = 0; row < imagelength; row += tl) {
|
|
|
|
uint32 nrow = (row+tl > imagelength) ? imagelength-row : tl;
|
|
|
|
uint32 colb = 0;
|
|
|
|
uint32 col;
|
|
|
|
|
|
|
|
for (col = 0; col < imagewidth; col += tw) {
|
|
|
|
tsample_t s;
|
|
|
|
for (s = 0; s < spp; s++) {
|
|
|
|
/*
|
|
|
|
* Tile is clipped horizontally. Calculate
|
|
|
|
* visible portion and skewing factors.
|
|
|
|
*/
|
|
|
|
if (colb + tilew > imagew) {
|
1999-08-16 14:27:51 -04:00
|
|
|
uint32 width = (imagew - colb);
|
1999-07-27 17:50:26 -04:00
|
|
|
int oskew = tilew - width;
|
|
|
|
|
|
|
|
cpContigBufToSeparateBuf(obuf,
|
|
|
|
bufp + (colb*spp) + s,
|
1999-08-16 14:27:51 -04:00
|
|
|
nrow, width/bytes_per_sample,
|
|
|
|
oskew, (oskew*spp)+iskew, spp,
|
2007-06-20 04:35:57 -04:00
|
|
|
bytes_per_sample);
|
1999-07-27 17:50:26 -04:00
|
|
|
} else
|
|
|
|
cpContigBufToSeparateBuf(obuf,
|
|
|
|
bufp + (colb*spp) + s,
|
|
|
|
nrow, tilewidth,
|
1999-08-16 14:27:51 -04:00
|
|
|
0, iskew, spp,
|
2007-06-20 04:35:57 -04:00
|
|
|
bytes_per_sample);
|
1999-07-27 17:50:26 -04:00
|
|
|
if (TIFFWriteTile(out, obuf, col, row, 0, s) < 0) {
|
2005-12-20 11:50:21 -05:00
|
|
|
TIFFError(TIFFFileName(out),
|
2007-06-20 04:35:57 -04:00
|
|
|
"Error, can't write tile at %lu %lu "
|
|
|
|
"sample %lu",
|
|
|
|
(unsigned long) col,
|
|
|
|
(unsigned long) row,
|
|
|
|
(unsigned long) s);
|
1999-07-27 17:50:26 -04:00
|
|
|
_TIFFfree(obuf);
|
2005-12-20 11:50:21 -05:00
|
|
|
return 0;
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
colb += tilew;
|
|
|
|
}
|
|
|
|
bufp += nrow * iimagew;
|
|
|
|
}
|
|
|
|
_TIFFfree(obuf);
|
2005-12-20 11:50:21 -05:00
|
|
|
return 1;
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Contig strips -> contig tiles.
|
|
|
|
*/
|
|
|
|
DECLAREcpFunc(cpContigStrips2ContigTiles)
|
|
|
|
{
|
|
|
|
return cpImage(in, out,
|
|
|
|
readContigStripsIntoBuffer,
|
|
|
|
writeBufferToContigTiles,
|
|
|
|
imagelength, imagewidth, spp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Contig strips -> separate tiles.
|
|
|
|
*/
|
|
|
|
DECLAREcpFunc(cpContigStrips2SeparateTiles)
|
|
|
|
{
|
|
|
|
return cpImage(in, out,
|
|
|
|
readContigStripsIntoBuffer,
|
|
|
|
writeBufferToSeparateTiles,
|
|
|
|
imagelength, imagewidth, spp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Separate strips -> contig tiles.
|
|
|
|
*/
|
|
|
|
DECLAREcpFunc(cpSeparateStrips2ContigTiles)
|
|
|
|
{
|
|
|
|
return cpImage(in, out,
|
|
|
|
readSeparateStripsIntoBuffer,
|
|
|
|
writeBufferToContigTiles,
|
|
|
|
imagelength, imagewidth, spp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Separate strips -> separate tiles.
|
|
|
|
*/
|
|
|
|
DECLAREcpFunc(cpSeparateStrips2SeparateTiles)
|
|
|
|
{
|
|
|
|
return cpImage(in, out,
|
|
|
|
readSeparateStripsIntoBuffer,
|
|
|
|
writeBufferToSeparateTiles,
|
|
|
|
imagelength, imagewidth, spp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Contig strips -> contig tiles.
|
|
|
|
*/
|
|
|
|
DECLAREcpFunc(cpContigTiles2ContigTiles)
|
|
|
|
{
|
|
|
|
return cpImage(in, out,
|
|
|
|
readContigTilesIntoBuffer,
|
|
|
|
writeBufferToContigTiles,
|
|
|
|
imagelength, imagewidth, spp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Contig tiles -> separate tiles.
|
|
|
|
*/
|
|
|
|
DECLAREcpFunc(cpContigTiles2SeparateTiles)
|
|
|
|
{
|
|
|
|
return cpImage(in, out,
|
|
|
|
readContigTilesIntoBuffer,
|
|
|
|
writeBufferToSeparateTiles,
|
|
|
|
imagelength, imagewidth, spp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Separate tiles -> contig tiles.
|
|
|
|
*/
|
|
|
|
DECLAREcpFunc(cpSeparateTiles2ContigTiles)
|
|
|
|
{
|
|
|
|
return cpImage(in, out,
|
|
|
|
readSeparateTilesIntoBuffer,
|
|
|
|
writeBufferToContigTiles,
|
|
|
|
imagelength, imagewidth, spp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Separate tiles -> separate tiles (tile dimension change).
|
|
|
|
*/
|
|
|
|
DECLAREcpFunc(cpSeparateTiles2SeparateTiles)
|
|
|
|
{
|
|
|
|
return cpImage(in, out,
|
|
|
|
readSeparateTilesIntoBuffer,
|
|
|
|
writeBufferToSeparateTiles,
|
|
|
|
imagelength, imagewidth, spp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Contig tiles -> contig tiles (tile dimension change).
|
|
|
|
*/
|
|
|
|
DECLAREcpFunc(cpContigTiles2ContigStrips)
|
|
|
|
{
|
|
|
|
return cpImage(in, out,
|
|
|
|
readContigTilesIntoBuffer,
|
|
|
|
writeBufferToContigStrips,
|
|
|
|
imagelength, imagewidth, spp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Contig tiles -> separate strips.
|
|
|
|
*/
|
|
|
|
DECLAREcpFunc(cpContigTiles2SeparateStrips)
|
|
|
|
{
|
|
|
|
return cpImage(in, out,
|
|
|
|
readContigTilesIntoBuffer,
|
|
|
|
writeBufferToSeparateStrips,
|
|
|
|
imagelength, imagewidth, spp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Separate tiles -> contig strips.
|
|
|
|
*/
|
|
|
|
DECLAREcpFunc(cpSeparateTiles2ContigStrips)
|
|
|
|
{
|
|
|
|
return cpImage(in, out,
|
|
|
|
readSeparateTilesIntoBuffer,
|
|
|
|
writeBufferToContigStrips,
|
|
|
|
imagelength, imagewidth, spp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Separate tiles -> separate strips.
|
|
|
|
*/
|
|
|
|
DECLAREcpFunc(cpSeparateTiles2SeparateStrips)
|
|
|
|
{
|
|
|
|
return cpImage(in, out,
|
|
|
|
readSeparateTilesIntoBuffer,
|
|
|
|
writeBufferToSeparateStrips,
|
|
|
|
imagelength, imagewidth, spp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Select the appropriate copy function to use.
|
|
|
|
*/
|
|
|
|
static copyFunc
|
|
|
|
pickCopyFunc(TIFF* in, TIFF* out, uint16 bitspersample, uint16 samplesperpixel)
|
|
|
|
{
|
|
|
|
uint16 shortv;
|
|
|
|
uint32 w, l, tw, tl;
|
|
|
|
int bychunk;
|
|
|
|
|
2017-01-11 14:25:44 -05:00
|
|
|
(void) TIFFGetFieldDefaulted(in, TIFFTAG_PLANARCONFIG, &shortv);
|
1999-07-27 17:50:26 -04:00
|
|
|
if (shortv != config && bitspersample != 8 && samplesperpixel > 1) {
|
|
|
|
fprintf(stderr,
|
2007-06-20 04:35:57 -04:00
|
|
|
"%s: Cannot handle different planar configuration w/ bits/sample != 8\n",
|
1999-07-27 17:50:26 -04:00
|
|
|
TIFFFileName(in));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &w);
|
|
|
|
TIFFGetField(in, TIFFTAG_IMAGELENGTH, &l);
|
2007-06-20 04:35:57 -04:00
|
|
|
if (!(TIFFIsTiled(out) || TIFFIsTiled(in))) {
|
|
|
|
uint32 irps = (uint32) -1L;
|
|
|
|
TIFFGetField(in, TIFFTAG_ROWSPERSTRIP, &irps);
|
|
|
|
/* if biased, force decoded copying to allow image subtraction */
|
|
|
|
bychunk = !bias && (rowsperstrip == irps);
|
2001-02-19 10:18:19 -05:00
|
|
|
}else{ /* either in or out is tiled */
|
2007-06-20 04:35:57 -04:00
|
|
|
if (bias) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: Cannot handle tiled configuration w/bias image\n",
|
|
|
|
TIFFFileName(in));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if (TIFFIsTiled(out)) {
|
|
|
|
if (!TIFFGetField(in, TIFFTAG_TILEWIDTH, &tw))
|
|
|
|
tw = w;
|
|
|
|
if (!TIFFGetField(in, TIFFTAG_TILELENGTH, &tl))
|
|
|
|
tl = l;
|
|
|
|
bychunk = (tw == tilewidth && tl == tilelength);
|
|
|
|
} else { /* out's not, so in must be tiled */
|
|
|
|
TIFFGetField(in, TIFFTAG_TILEWIDTH, &tw);
|
|
|
|
TIFFGetField(in, TIFFTAG_TILELENGTH, &tl);
|
|
|
|
bychunk = (tw == w && tl == rowsperstrip);
|
|
|
|
}
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
#define T 1
|
|
|
|
#define F 0
|
|
|
|
#define pack(a,b,c,d,e) ((long)(((a)<<11)|((b)<<3)|((c)<<2)|((d)<<1)|(e)))
|
|
|
|
switch(pack(shortv,config,TIFFIsTiled(in),TIFFIsTiled(out),bychunk)) {
|
2007-06-20 04:35:57 -04:00
|
|
|
/* Strips -> Tiles */
|
|
|
|
case pack(PLANARCONFIG_CONTIG, PLANARCONFIG_CONTIG, F,T,F):
|
|
|
|
case pack(PLANARCONFIG_CONTIG, PLANARCONFIG_CONTIG, F,T,T):
|
|
|
|
return cpContigStrips2ContigTiles;
|
|
|
|
case pack(PLANARCONFIG_CONTIG, PLANARCONFIG_SEPARATE, F,T,F):
|
|
|
|
case pack(PLANARCONFIG_CONTIG, PLANARCONFIG_SEPARATE, F,T,T):
|
|
|
|
return cpContigStrips2SeparateTiles;
|
|
|
|
case pack(PLANARCONFIG_SEPARATE, PLANARCONFIG_CONTIG, F,T,F):
|
|
|
|
case pack(PLANARCONFIG_SEPARATE, PLANARCONFIG_CONTIG, F,T,T):
|
|
|
|
return cpSeparateStrips2ContigTiles;
|
|
|
|
case pack(PLANARCONFIG_SEPARATE, PLANARCONFIG_SEPARATE, F,T,F):
|
|
|
|
case pack(PLANARCONFIG_SEPARATE, PLANARCONFIG_SEPARATE, F,T,T):
|
|
|
|
return cpSeparateStrips2SeparateTiles;
|
|
|
|
/* Tiles -> Tiles */
|
|
|
|
case pack(PLANARCONFIG_CONTIG, PLANARCONFIG_CONTIG, T,T,F):
|
|
|
|
case pack(PLANARCONFIG_CONTIG, PLANARCONFIG_CONTIG, T,T,T):
|
|
|
|
return cpContigTiles2ContigTiles;
|
|
|
|
case pack(PLANARCONFIG_CONTIG, PLANARCONFIG_SEPARATE, T,T,F):
|
|
|
|
case pack(PLANARCONFIG_CONTIG, PLANARCONFIG_SEPARATE, T,T,T):
|
|
|
|
return cpContigTiles2SeparateTiles;
|
|
|
|
case pack(PLANARCONFIG_SEPARATE, PLANARCONFIG_CONTIG, T,T,F):
|
|
|
|
case pack(PLANARCONFIG_SEPARATE, PLANARCONFIG_CONTIG, T,T,T):
|
|
|
|
return cpSeparateTiles2ContigTiles;
|
|
|
|
case pack(PLANARCONFIG_SEPARATE, PLANARCONFIG_SEPARATE, T,T,F):
|
|
|
|
case pack(PLANARCONFIG_SEPARATE, PLANARCONFIG_SEPARATE, T,T,T):
|
|
|
|
return cpSeparateTiles2SeparateTiles;
|
|
|
|
/* Tiles -> Strips */
|
|
|
|
case pack(PLANARCONFIG_CONTIG, PLANARCONFIG_CONTIG, T,F,F):
|
|
|
|
case pack(PLANARCONFIG_CONTIG, PLANARCONFIG_CONTIG, T,F,T):
|
|
|
|
return cpContigTiles2ContigStrips;
|
|
|
|
case pack(PLANARCONFIG_CONTIG, PLANARCONFIG_SEPARATE, T,F,F):
|
|
|
|
case pack(PLANARCONFIG_CONTIG, PLANARCONFIG_SEPARATE, T,F,T):
|
|
|
|
return cpContigTiles2SeparateStrips;
|
|
|
|
case pack(PLANARCONFIG_SEPARATE, PLANARCONFIG_CONTIG, T,F,F):
|
|
|
|
case pack(PLANARCONFIG_SEPARATE, PLANARCONFIG_CONTIG, T,F,T):
|
|
|
|
return cpSeparateTiles2ContigStrips;
|
|
|
|
case pack(PLANARCONFIG_SEPARATE, PLANARCONFIG_SEPARATE, T,F,F):
|
|
|
|
case pack(PLANARCONFIG_SEPARATE, PLANARCONFIG_SEPARATE, T,F,T):
|
|
|
|
return cpSeparateTiles2SeparateStrips;
|
|
|
|
/* Strips -> Strips */
|
|
|
|
case pack(PLANARCONFIG_CONTIG, PLANARCONFIG_CONTIG, F,F,F):
|
|
|
|
return bias ? cpBiasedContig2Contig : cpContig2ContigByRow;
|
|
|
|
case pack(PLANARCONFIG_CONTIG, PLANARCONFIG_CONTIG, F,F,T):
|
|
|
|
return cpDecodedStrips;
|
|
|
|
case pack(PLANARCONFIG_CONTIG, PLANARCONFIG_SEPARATE, F,F,F):
|
|
|
|
case pack(PLANARCONFIG_CONTIG, PLANARCONFIG_SEPARATE, F,F,T):
|
|
|
|
return cpContig2SeparateByRow;
|
|
|
|
case pack(PLANARCONFIG_SEPARATE, PLANARCONFIG_CONTIG, F,F,F):
|
|
|
|
case pack(PLANARCONFIG_SEPARATE, PLANARCONFIG_CONTIG, F,F,T):
|
|
|
|
return cpSeparate2ContigByRow;
|
|
|
|
case pack(PLANARCONFIG_SEPARATE, PLANARCONFIG_SEPARATE, F,F,F):
|
|
|
|
case pack(PLANARCONFIG_SEPARATE, PLANARCONFIG_SEPARATE, F,F,T):
|
|
|
|
return cpSeparate2SeparateByRow;
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
#undef pack
|
|
|
|
#undef F
|
|
|
|
#undef T
|
|
|
|
fprintf(stderr, "tiffcp: %s: Don't know how to copy/convert image.\n",
|
|
|
|
TIFFFileName(in));
|
|
|
|
return (NULL);
|
|
|
|
}
|
2004-09-02 10:46:27 -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:
|
|
|
|
*/
|