| 1 |
/* -*- mode: C; tab-width: 4; c-basic-offset: 4 -*- |
| 2 |
//C- ------------------------------------------------------------------- |
| 3 |
//C- DjView4 |
| 4 |
//C- Copyright (c) 2006 Leon Bottou |
| 5 |
//C- |
| 6 |
//C- This software is subject to, and may be distributed under, the |
| 7 |
//C- GNU General Public License, either version 2 of the license, |
| 8 |
//C- or (at your option) any later version. The license should have |
| 9 |
//C- accompanied the software or you may obtain a copy of the license |
| 10 |
//C- from the Free Software Foundation at http://www.fsf.org . |
| 11 |
//C- |
| 12 |
//C- This program is distributed in the hope that it will be useful, |
| 13 |
//C- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
//C- GNU General Public License for more details. |
| 16 |
//C- ------------------------------------------------------------------ |
| 17 |
// |
| 18 |
// The following code is derived from program "tiff2pdf" |
| 19 |
// whose copyright notice is reproduced below. |
| 20 |
// Changes were made to make it independent from the private |
| 21 |
// include file tiffiop.h. |
| 22 |
*/ |
| 23 |
|
| 24 |
/* $Id: tiff2pdf.c,v 1.103 2017-10-29 18:50:41 bfriesen Exp $ |
| 25 |
* |
| 26 |
* tiff2pdf - converts a TIFF image to a PDF document |
| 27 |
* |
| 28 |
* Copyright (c) 2003 Ross Finlayson |
| 29 |
* |
| 30 |
* Permission to use, copy, modify, distribute, and sell this software and |
| 31 |
* its documentation for any purpose is hereby granted without fee, provided |
| 32 |
* that (i) the above copyright notices and this permission notice appear in |
| 33 |
* all copies of the software and related documentation, and (ii) the name of |
| 34 |
* Ross Finlayson may not be used in any advertising or |
| 35 |
* publicity relating to the software without the specific, prior written |
| 36 |
* permission of Ross Finlayson. |
| 37 |
* |
| 38 |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 39 |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 40 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 41 |
* |
| 42 |
* IN NO EVENT SHALL ROSS FINLAYSON BE LIABLE FOR |
| 43 |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 44 |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 45 |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 46 |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 47 |
* OF THIS SOFTWARE. |
| 48 |
*/ |
| 49 |
|
| 50 |
#include "tiff2pdf.h" |
| 51 |
|
| 52 |
#ifdef HAVE_TIFF2PDF |
| 53 |
|
| 54 |
#include <stdio.h> |
| 55 |
#include <stdlib.h> |
| 56 |
#include <string.h> |
| 57 |
#include <ctype.h> |
| 58 |
#include <time.h> |
| 59 |
#include <errno.h> |
| 60 |
#include <limits.h> |
| 61 |
|
| 62 |
#if HAVE_UNISTD_H |
| 63 |
# include <unistd.h> |
| 64 |
#endif |
| 65 |
|
| 66 |
#ifdef HAVE_FCNTL_H |
| 67 |
# include <fcntl.h> |
| 68 |
#endif |
| 69 |
|
| 70 |
#if defined(__GNUC__) || defined(HAVE_LONG_LONG_INT) |
| 71 |
# define uint64 unsigned long long |
| 72 |
#elif defined(_MSC_VER) |
| 73 |
# define uint64 unsigned __int64 |
| 74 |
#else |
| 75 |
# define uint64 size_t |
| 76 |
#endif |
| 77 |
#ifndef TIFFSafeMultiply |
| 78 |
# define TIFFSafeMultiply(t,v,m) \ |
| 79 |
((((t)(m)!=(t)0)&&(((t)(((v)*(m))/(m)))==(t)(v)))?(t)((v)*(m)):(t)0) |
| 80 |
#endif |
| 81 |
#ifndef TIFF_SIZE_FORMAT |
| 82 |
# define TIFF_SIZE_T unsigned long |
| 83 |
# define TIFF_SIZE_FORMAT "%lu" |
| 84 |
#endif |
| 85 |
#ifndef TIFFmin |
| 86 |
# define TIFFmin(A,B) ((A)<(B)?(A):(B)) |
| 87 |
#endif |
| 88 |
#ifndef TIFFclip |
| 89 |
# define TIFFclip(A,B) ((A)<(0)?(0):((A)>(B)?(B):(A))) |
| 90 |
#endif |
| 91 |
|
| 92 |
#ifdef HAVE_GETOPT_H |
| 93 |
# include <getopt.h> |
| 94 |
#endif |
| 95 |
|
| 96 |
#if defined(_WIN32) && defined(_MSC_VER) |
| 97 |
# ifndef HAVE_SNPRINTF |
| 98 |
# define snprintf mysnprintf |
| 99 |
static int |
| 100 |
snprintf(char* str, size_t size, const char* format, ...) |
| 101 |
{ |
| 102 |
int count; |
| 103 |
va_list ap; |
| 104 |
va_start(ap, format); |
| 105 |
count = vsnprintf(str, size, format, ap); |
| 106 |
va_end(ap); |
| 107 |
return count; |
| 108 |
} |
| 109 |
# endif |
| 110 |
#endif |
| 111 |
|
| 112 |
|
| 113 |
#ifndef HAVE_GETOPT_H |
| 114 |
/* |
| 115 |
* Copyright (c) 1987, 1993, 1994 |
| 116 |
* The Regents of the University of California. All rights reserved. |
| 117 |
* |
| 118 |
* Redistribution and use in source and binary forms, with or without |
| 119 |
* modification, are permitted provided that the following conditions |
| 120 |
* are met: |
| 121 |
* 1. Redistributions of source code must retain the above copyright |
| 122 |
* notice, this list of conditions and the following disclaimer. |
| 123 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 124 |
* notice, this list of conditions and the following disclaimer in the |
| 125 |
* documentation and/or other materials provided with the distribution. |
| 126 |
* 3. Neither the name of the University nor the names of its contributors |
| 127 |
* may be used to endorse or promote products derived from this software |
| 128 |
* without specific prior written permission. |
| 129 |
* |
| 130 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 131 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 132 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 133 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 134 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 135 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 136 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 137 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 138 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 139 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 140 |
* SUCH DAMAGE. |
| 141 |
*/ |
| 142 |
|
| 143 |
|
| 144 |
static int opterr = 1, /* if error message should be printed */ |
| 145 |
optind = 1, /* index into parent argv vector */ |
| 146 |
optopt, /* character checked for validity */ |
| 147 |
optreset; /* reset getopt */ |
| 148 |
static char *optarg; /* argument associated with option */ |
| 149 |
|
| 150 |
#define BADCH (int)'?' |
| 151 |
#define BADARG (int)':' |
| 152 |
#define EMSG "" |
| 153 |
|
| 154 |
/* |
| 155 |
* getopt -- |
| 156 |
* Parse argc/argv argument vector. |
| 157 |
*/ |
| 158 |
static int |
| 159 |
getopt(int argc, char * const argv[], const char *optstring) |
| 160 |
{ |
| 161 |
static char *place = EMSG; /* option letter processing */ |
| 162 |
char *oli; /* option letter list index */ |
| 163 |
|
| 164 |
if (optreset || *place == 0) { /* update scanning pointer */ |
| 165 |
optreset = 0; |
| 166 |
place = argv[optind]; |
| 167 |
if (optind >= argc || *place++ != '-') { |
| 168 |
/* Argument is absent or is not an option */ |
| 169 |
place = EMSG; |
| 170 |
return (-1); |
| 171 |
} |
| 172 |
optopt = *place++; |
| 173 |
if (optopt == '-' && *place == 0) { |
| 174 |
/* "--" => end of options */ |
| 175 |
++optind; |
| 176 |
place = EMSG; |
| 177 |
return (-1); |
| 178 |
} |
| 179 |
if (optopt == 0) { |
| 180 |
/* Solitary '-', treat as a '-' option |
| 181 |
if the program (eg su) is looking for it. */ |
| 182 |
place = EMSG; |
| 183 |
if (strchr(optstring, '-') == NULL) |
| 184 |
return -1; |
| 185 |
optopt = '-'; |
| 186 |
} |
| 187 |
} else |
| 188 |
optopt = *place++; |
| 189 |
|
| 190 |
/* See if option letter is one the caller wanted... */ |
| 191 |
if (optopt == ':' || (oli = strchr(optstring, optopt)) == NULL) { |
| 192 |
if (*place == 0) |
| 193 |
++optind; |
| 194 |
if (opterr && *optstring != ':') |
| 195 |
(void)fprintf(stderr, |
| 196 |
"unknown option -- %c\n", optopt); |
| 197 |
return (BADCH); |
| 198 |
} |
| 199 |
|
| 200 |
/* Does this option need an argument? */ |
| 201 |
if (oli[1] != ':') { |
| 202 |
/* don't need argument */ |
| 203 |
optarg = NULL; |
| 204 |
if (*place == 0) |
| 205 |
++optind; |
| 206 |
} else { |
| 207 |
/* Option-argument is either the rest of this argument or the |
| 208 |
entire next argument. */ |
| 209 |
if (*place) |
| 210 |
optarg = place; |
| 211 |
else if (argc > ++optind) |
| 212 |
optarg = argv[optind]; |
| 213 |
else { |
| 214 |
/* option-argument absent */ |
| 215 |
place = EMSG; |
| 216 |
if (*optstring == ':') |
| 217 |
return (BADARG); |
| 218 |
if (opterr) |
| 219 |
(void)fprintf(stderr, |
| 220 |
"option requires an argument -- %c\n", |
| 221 |
optopt); |
| 222 |
return (BADCH); |
| 223 |
} |
| 224 |
place = EMSG; |
| 225 |
++optind; |
| 226 |
} |
| 227 |
return (optopt); /* return option letter */ |
| 228 |
} |
| 229 |
#endif |
| 230 |
|
| 231 |
#ifndef EXIT_SUCCESS |
| 232 |
# define EXIT_SUCCESS 0 |
| 233 |
#endif |
| 234 |
#ifndef EXIT_FAILURE |
| 235 |
# define EXIT_FAILURE 1 |
| 236 |
#endif |
| 237 |
|
| 238 |
#define TIFF2PDF_MODULE "tiff2pdf" |
| 239 |
|
| 240 |
#define PS_UNIT_SIZE 72.0F |
| 241 |
|
| 242 |
/* This type is of PDF color spaces. */ |
| 243 |
typedef enum { |
| 244 |
T2P_CS_BILEVEL = 0x01, /* Bilevel, black and white */ |
| 245 |
T2P_CS_GRAY = 0x02, /* Single channel */ |
| 246 |
T2P_CS_RGB = 0x04, /* Three channel tristimulus RGB */ |
| 247 |
T2P_CS_CMYK = 0x08, /* Four channel CMYK print inkset */ |
| 248 |
T2P_CS_LAB = 0x10, /* Three channel L*a*b* color space */ |
| 249 |
T2P_CS_PALETTE = 0x1000,/* One of the above with a color map */ |
| 250 |
T2P_CS_CALGRAY = 0x20, /* Calibrated single channel */ |
| 251 |
T2P_CS_CALRGB = 0x40, /* Calibrated three channel tristimulus RGB */ |
| 252 |
T2P_CS_ICCBASED = 0x80 /* ICC profile color specification */ |
| 253 |
} t2p_cs_t; |
| 254 |
|
| 255 |
/* This type is of PDF compression types. */ |
| 256 |
typedef enum{ |
| 257 |
T2P_COMPRESS_NONE=0x00 |
| 258 |
#ifdef CCITT_SUPPORT |
| 259 |
, T2P_COMPRESS_G4=0x01 |
| 260 |
#endif |
| 261 |
#if defined(JPEG_SUPPORT) || defined(OJPEG_SUPPORT) |
| 262 |
, T2P_COMPRESS_JPEG=0x02 |
| 263 |
#endif |
| 264 |
#ifdef ZIP_SUPPORT |
| 265 |
, T2P_COMPRESS_ZIP=0x04 |
| 266 |
#endif |
| 267 |
} t2p_compress_t; |
| 268 |
|
| 269 |
/* This type is whether TIFF image data can be used in PDF without transcoding. */ |
| 270 |
typedef enum{ |
| 271 |
T2P_TRANSCODE_RAW=0x01, /* The raw data from the input can be used without recompressing */ |
| 272 |
T2P_TRANSCODE_ENCODE=0x02 /* The data from the input is perhaps unencoded and reencoded */ |
| 273 |
} t2p_transcode_t; |
| 274 |
|
| 275 |
/* This type is of information about the data samples of the input image. */ |
| 276 |
typedef enum{ |
| 277 |
T2P_SAMPLE_NOTHING=0x0000, /* The unencoded samples are normal for the output colorspace */ |
| 278 |
T2P_SAMPLE_ABGR_TO_RGB=0x0001, /* The unencoded samples are the result of ReadRGBAImage */ |
| 279 |
T2P_SAMPLE_RGBA_TO_RGB=0x0002, /* The unencoded samples are contiguous RGBA */ |
| 280 |
T2P_SAMPLE_RGBAA_TO_RGB=0x0004, /* The unencoded samples are RGBA with premultiplied alpha */ |
| 281 |
T2P_SAMPLE_YCBCR_TO_RGB=0x0008, |
| 282 |
T2P_SAMPLE_YCBCR_TO_LAB=0x0010, |
| 283 |
T2P_SAMPLE_REALIZE_PALETTE=0x0020, /* The unencoded samples are indexes into the color map */ |
| 284 |
T2P_SAMPLE_SIGNED_TO_UNSIGNED=0x0040, /* The unencoded samples are signed instead of unsignd */ |
| 285 |
T2P_SAMPLE_LAB_SIGNED_TO_UNSIGNED=0x0040, /* The L*a*b* samples have a* and b* signed */ |
| 286 |
T2P_SAMPLE_PLANAR_SEPARATE_TO_CONTIG=0x0100 /* The unencoded samples are separate instead of contiguous */ |
| 287 |
} t2p_sample_t; |
| 288 |
|
| 289 |
/* This type is of error status of the T2P struct. */ |
| 290 |
typedef enum{ |
| 291 |
T2P_ERR_OK = 0, /* This is the value of t2p->t2p_error when there is no error */ |
| 292 |
T2P_ERR_ERROR = 1 /* This is the value of t2p->t2p_error when there was an error */ |
| 293 |
} t2p_err_t; |
| 294 |
|
| 295 |
/* This struct defines a logical page of a TIFF. */ |
| 296 |
typedef struct { |
| 297 |
tdir_t page_directory; |
| 298 |
uint32 page_number; |
| 299 |
ttile_t page_tilecount; |
| 300 |
uint32 page_extra; |
| 301 |
} T2P_PAGE; |
| 302 |
|
| 303 |
/* This struct defines a PDF rectangle's coordinates. */ |
| 304 |
typedef struct { |
| 305 |
float x1; |
| 306 |
float y1; |
| 307 |
float x2; |
| 308 |
float y2; |
| 309 |
float mat[9]; |
| 310 |
} T2P_BOX; |
| 311 |
|
| 312 |
/* This struct defines a tile of a PDF. */ |
| 313 |
typedef struct { |
| 314 |
T2P_BOX tile_box; |
| 315 |
} T2P_TILE; |
| 316 |
|
| 317 |
/* This struct defines information about the tiles on a PDF page. */ |
| 318 |
typedef struct { |
| 319 |
ttile_t tiles_tilecount; |
| 320 |
uint32 tiles_tilewidth; |
| 321 |
uint32 tiles_tilelength; |
| 322 |
uint32 tiles_tilecountx; |
| 323 |
uint32 tiles_tilecounty; |
| 324 |
uint32 tiles_edgetilewidth; |
| 325 |
uint32 tiles_edgetilelength; |
| 326 |
T2P_TILE* tiles_tiles; |
| 327 |
} T2P_TILES; |
| 328 |
|
| 329 |
/* This struct is the context of a function to generate PDF from a TIFF. */ |
| 330 |
typedef struct { |
| 331 |
t2p_err_t t2p_error; |
| 332 |
T2P_PAGE* tiff_pages; |
| 333 |
T2P_TILES* tiff_tiles; |
| 334 |
tdir_t tiff_pagecount; |
| 335 |
uint16 tiff_compression; |
| 336 |
uint16 tiff_photometric; |
| 337 |
uint16 tiff_fillorder; |
| 338 |
uint16 tiff_bitspersample; |
| 339 |
uint16 tiff_samplesperpixel; |
| 340 |
uint16 tiff_planar; |
| 341 |
uint32 tiff_width; |
| 342 |
uint32 tiff_length; |
| 343 |
float tiff_xres; |
| 344 |
float tiff_yres; |
| 345 |
uint16 tiff_orientation; |
| 346 |
toff_t tiff_dataoffset; |
| 347 |
tsize_t tiff_datasize; |
| 348 |
uint16 tiff_resunit; |
| 349 |
uint16 pdf_centimeters; |
| 350 |
uint16 pdf_overrideres; |
| 351 |
uint16 pdf_overridepagesize; |
| 352 |
float pdf_defaultxres; |
| 353 |
float pdf_defaultyres; |
| 354 |
float pdf_xres; |
| 355 |
float pdf_yres; |
| 356 |
float pdf_defaultpagewidth; |
| 357 |
float pdf_defaultpagelength; |
| 358 |
float pdf_pagewidth; |
| 359 |
float pdf_pagelength; |
| 360 |
float pdf_imagewidth; |
| 361 |
float pdf_imagelength; |
| 362 |
int pdf_image_fillpage; /* 0 (default: no scaling, 1:scale imagesize to pagesize */ |
| 363 |
T2P_BOX pdf_mediabox; |
| 364 |
T2P_BOX pdf_imagebox; |
| 365 |
uint16 pdf_majorversion; |
| 366 |
uint16 pdf_minorversion; |
| 367 |
uint32 pdf_catalog; |
| 368 |
uint32 pdf_pages; |
| 369 |
uint32 pdf_info; |
| 370 |
uint32 pdf_palettecs; |
| 371 |
uint16 pdf_fitwindow; |
| 372 |
uint32 pdf_startxref; |
| 373 |
#define TIFF2PDF_FILEID_SIZE 33 |
| 374 |
char pdf_fileid[TIFF2PDF_FILEID_SIZE]; |
| 375 |
#define TIFF2PDF_DATETIME_SIZE 17 |
| 376 |
char pdf_datetime[TIFF2PDF_DATETIME_SIZE]; |
| 377 |
#define TIFF2PDF_CREATOR_SIZE 512 |
| 378 |
char pdf_creator[TIFF2PDF_CREATOR_SIZE]; |
| 379 |
#define TIFF2PDF_AUTHOR_SIZE 512 |
| 380 |
char pdf_author[TIFF2PDF_AUTHOR_SIZE]; |
| 381 |
#define TIFF2PDF_TITLE_SIZE 512 |
| 382 |
char pdf_title[TIFF2PDF_TITLE_SIZE]; |
| 383 |
#define TIFF2PDF_SUBJECT_SIZE 512 |
| 384 |
char pdf_subject[TIFF2PDF_SUBJECT_SIZE]; |
| 385 |
#define TIFF2PDF_KEYWORDS_SIZE 512 |
| 386 |
char pdf_keywords[TIFF2PDF_KEYWORDS_SIZE]; |
| 387 |
t2p_cs_t pdf_colorspace; |
| 388 |
uint16 pdf_colorspace_invert; |
| 389 |
uint16 pdf_switchdecode; |
| 390 |
uint16 pdf_palettesize; |
| 391 |
unsigned char* pdf_palette; |
| 392 |
int pdf_labrange[4]; |
| 393 |
t2p_compress_t pdf_defaultcompression; |
| 394 |
uint16 pdf_defaultcompressionquality; |
| 395 |
t2p_compress_t pdf_compression; |
| 396 |
uint16 pdf_compressionquality; |
| 397 |
uint16 pdf_nopassthrough; |
| 398 |
t2p_transcode_t pdf_transcode; |
| 399 |
t2p_sample_t pdf_sample; |
| 400 |
uint32* pdf_xrefoffsets; |
| 401 |
uint32 pdf_xrefcount; |
| 402 |
tdir_t pdf_page; |
| 403 |
#ifdef OJPEG_SUPPORT |
| 404 |
tdata_t pdf_ojpegdata; |
| 405 |
uint32 pdf_ojpegdatalength; |
| 406 |
uint32 pdf_ojpegiflength; |
| 407 |
#endif |
| 408 |
float tiff_whitechromaticities[2]; |
| 409 |
float tiff_primarychromaticities[6]; |
| 410 |
float tiff_referenceblackwhite[2]; |
| 411 |
float* tiff_transferfunction[3]; |
| 412 |
int pdf_image_interpolate; /* 0 (default) : do not interpolate, |
| 413 |
1 : interpolate */ |
| 414 |
uint16 tiff_transferfunctioncount; |
| 415 |
uint32 pdf_icccs; |
| 416 |
uint32 tiff_iccprofilelength; |
| 417 |
tdata_t tiff_iccprofile; |
| 418 |
|
| 419 |
/* fields for custom read/write procedures */ |
| 420 |
FILE *outputfile; |
| 421 |
int outputdisable; |
| 422 |
tsize_t outputwritten; |
| 423 |
} T2P; |
| 424 |
|
| 425 |
/* These functions are called by main. */ |
| 426 |
|
| 427 |
static |
| 428 |
int tiff2pdf_match_paper_size(float*, float*, char*); |
| 429 |
|
| 430 |
/* These functions are used to generate a PDF from a TIFF. */ |
| 431 |
|
| 432 |
#ifdef __cplusplus |
| 433 |
extern "C" { |
| 434 |
#endif |
| 435 |
|
| 436 |
static |
| 437 |
T2P* t2p_init(void); |
| 438 |
static |
| 439 |
void t2p_validate(T2P*); |
| 440 |
static |
| 441 |
tsize_t t2p_write_pdf(T2P*, TIFF*, TIFF*); |
| 442 |
static |
| 443 |
void t2p_free(T2P*); |
| 444 |
|
| 445 |
#ifdef __cplusplus |
| 446 |
} |
| 447 |
#endif |
| 448 |
|
| 449 |
static |
| 450 |
void t2p_read_tiff_init(T2P*, TIFF*); |
| 451 |
static |
| 452 |
int t2p_cmp_t2p_page(const void*, const void*); |
| 453 |
static |
| 454 |
void t2p_read_tiff_data(T2P*, TIFF*); |
| 455 |
static |
| 456 |
void t2p_read_tiff_size(T2P*, TIFF*); |
| 457 |
static |
| 458 |
void t2p_read_tiff_size_tile(T2P*, TIFF*, ttile_t); |
| 459 |
static |
| 460 |
int t2p_tile_is_right_edge(T2P_TILES, ttile_t); |
| 461 |
static |
| 462 |
int t2p_tile_is_bottom_edge(T2P_TILES, ttile_t); |
| 463 |
/* static |
| 464 |
int t2p_tile_is_edge(T2P_TILES, ttile_t); */ |
| 465 |
/* static |
| 466 |
int t2p_tile_is_corner_edge(T2P_TILES, ttile_t); */ |
| 467 |
static |
| 468 |
tsize_t t2p_readwrite_pdf_image(T2P*, TIFF*, TIFF*); |
| 469 |
static |
| 470 |
tsize_t t2p_readwrite_pdf_image_tile(T2P*, TIFF*, TIFF*, ttile_t); |
| 471 |
#ifdef OJPEG_SUPPORT |
| 472 |
static |
| 473 |
int t2p_process_ojpeg_tables(T2P*, TIFF*); |
| 474 |
#endif |
| 475 |
#ifdef JPEG_SUPPORT |
| 476 |
static |
| 477 |
int t2p_process_jpeg_strip(unsigned char*, tsize_t*, unsigned char*, tsize_t, tsize_t*, tstrip_t, uint32); |
| 478 |
#endif |
| 479 |
static |
| 480 |
void t2p_tile_collapse_left(tdata_t, tsize_t, uint32, uint32, uint32); |
| 481 |
static |
| 482 |
void t2p_write_advance_directory(T2P*, TIFF*); |
| 483 |
static |
| 484 |
tsize_t t2p_sample_planar_separate_to_contig(T2P*, unsigned char*, unsigned char*, tsize_t); |
| 485 |
static |
| 486 |
tsize_t t2p_sample_realize_palette(T2P*, unsigned char*); |
| 487 |
static |
| 488 |
tsize_t t2p_sample_abgr_to_rgb(tdata_t, uint32); |
| 489 |
static |
| 490 |
tsize_t t2p_sample_rgba_to_rgb(tdata_t, uint32); |
| 491 |
static |
| 492 |
tsize_t t2p_sample_rgbaa_to_rgb(tdata_t, uint32); |
| 493 |
static |
| 494 |
tsize_t t2p_sample_lab_signed_to_unsigned(tdata_t, uint32); |
| 495 |
static |
| 496 |
tsize_t t2p_write_pdf_header(T2P*, TIFF*); |
| 497 |
static |
| 498 |
tsize_t t2p_write_pdf_obj_start(uint32, TIFF*); |
| 499 |
static |
| 500 |
tsize_t t2p_write_pdf_obj_end(TIFF*); |
| 501 |
/* static |
| 502 |
tsize_t t2p_write_pdf_name(unsigned char*, TIFF*); */ |
| 503 |
static |
| 504 |
tsize_t t2p_write_pdf_string(char*, TIFF*); |
| 505 |
static |
| 506 |
tsize_t t2p_write_pdf_stream(tdata_t, tsize_t, TIFF*); |
| 507 |
static |
| 508 |
tsize_t t2p_write_pdf_stream_start(TIFF*); |
| 509 |
static |
| 510 |
tsize_t t2p_write_pdf_stream_end(TIFF*); |
| 511 |
static |
| 512 |
tsize_t t2p_write_pdf_stream_dict(tsize_t, uint32, TIFF*); |
| 513 |
static |
| 514 |
tsize_t t2p_write_pdf_stream_dict_start(TIFF*); |
| 515 |
static |
| 516 |
tsize_t t2p_write_pdf_stream_dict_end(TIFF*); |
| 517 |
static |
| 518 |
tsize_t t2p_write_pdf_stream_length(tsize_t, TIFF*); |
| 519 |
static |
| 520 |
tsize_t t2p_write_pdf_catalog(T2P*, TIFF*); |
| 521 |
static |
| 522 |
tsize_t t2p_write_pdf_info(T2P*, TIFF*, TIFF*); |
| 523 |
static |
| 524 |
void t2p_pdf_currenttime(T2P*); |
| 525 |
static |
| 526 |
void t2p_pdf_tifftime(T2P*, TIFF*); |
| 527 |
static |
| 528 |
tsize_t t2p_write_pdf_pages(T2P*, TIFF*); |
| 529 |
static |
| 530 |
tsize_t t2p_write_pdf_page(uint32, T2P*, TIFF*); |
| 531 |
static |
| 532 |
void t2p_compose_pdf_page(T2P*); |
| 533 |
static |
| 534 |
void t2p_compose_pdf_page_orient(T2P_BOX*, uint16); |
| 535 |
static |
| 536 |
void t2p_compose_pdf_page_orient_flip(T2P_BOX*, uint16); |
| 537 |
/* static |
| 538 |
tsize_t t2p_write_pdf_page_content(T2P*, TIFF*); */ |
| 539 |
static |
| 540 |
tsize_t t2p_write_pdf_xobject_stream_dict(ttile_t, T2P*, TIFF*); |
| 541 |
static |
| 542 |
tsize_t t2p_write_pdf_xobject_cs(T2P*, TIFF*); |
| 543 |
static |
| 544 |
tsize_t t2p_write_pdf_transfer(T2P*, TIFF*); |
| 545 |
static |
| 546 |
tsize_t t2p_write_pdf_transfer_dict(T2P*, TIFF*, uint16); |
| 547 |
static |
| 548 |
tsize_t t2p_write_pdf_transfer_stream(T2P*, TIFF*, uint16); |
| 549 |
static |
| 550 |
tsize_t t2p_write_pdf_xobject_calcs(T2P*, TIFF*); |
| 551 |
static |
| 552 |
tsize_t t2p_write_pdf_xobject_icccs(T2P*, TIFF*); |
| 553 |
static |
| 554 |
tsize_t t2p_write_pdf_xobject_icccs_dict(T2P*, TIFF*); |
| 555 |
static |
| 556 |
tsize_t t2p_write_pdf_xobject_icccs_stream(T2P*, TIFF*); |
| 557 |
/* static tsize_t t2p_write_pdf_xobject_cs_stream(T2P*, TIFF*); */ |
| 558 |
static |
| 559 |
tsize_t t2p_write_pdf_xobject_decode(T2P*, TIFF*); |
| 560 |
static |
| 561 |
tsize_t t2p_write_pdf_xobject_stream_filter(ttile_t, T2P*, TIFF*); |
| 562 |
static |
| 563 |
tsize_t t2p_write_pdf_xreftable(T2P*, TIFF*); |
| 564 |
static |
| 565 |
tsize_t t2p_write_pdf_trailer(T2P*, TIFF*); |
| 566 |
|
| 567 |
#define check_snprintf_ret(t2p, rv, buf) do { \ |
| 568 |
if ((rv) < 0) rv = 0; \ |
| 569 |
else if((rv) >= (int)sizeof(buf)) (rv) = sizeof(buf) - 1; \ |
| 570 |
else break; \ |
| 571 |
if ((t2p) != NULL) (t2p)->t2p_error = T2P_ERR_ERROR; \ |
| 572 |
} while(0) |
| 573 |
|
| 574 |
static void |
| 575 |
t2p_disable(TIFF *tif) |
| 576 |
{ |
| 577 |
T2P *t2p = (T2P*) TIFFClientdata(tif); |
| 578 |
t2p->outputdisable = 1; |
| 579 |
} |
| 580 |
|
| 581 |
static void |
| 582 |
t2p_enable(TIFF *tif) |
| 583 |
{ |
| 584 |
T2P *t2p = (T2P*) TIFFClientdata(tif); |
| 585 |
t2p->outputdisable = 0; |
| 586 |
} |
| 587 |
|
| 588 |
/* |
| 589 |
* Procs for TIFFClientOpen |
| 590 |
*/ |
| 591 |
|
| 592 |
#ifdef OJPEG_SUPPORT |
| 593 |
static tmsize_t |
| 594 |
t2pReadFile(TIFF *tif, tdata_t data, tmsize_t size) |
| 595 |
{ |
| 596 |
thandle_t client = TIFFClientdata(tif); |
| 597 |
TIFFReadWriteProc proc = TIFFGetReadProc(tif); |
| 598 |
if (proc) |
| 599 |
return proc(client, data, size); |
| 600 |
return -1; |
| 601 |
} |
| 602 |
#endif /* OJPEG_SUPPORT */ |
| 603 |
|
| 604 |
static tmsize_t |
| 605 |
t2pWriteFile(TIFF *tif, tdata_t data, tmsize_t size) |
| 606 |
{ |
| 607 |
thandle_t client = TIFFClientdata(tif); |
| 608 |
TIFFReadWriteProc proc = TIFFGetWriteProc(tif); |
| 609 |
if (proc) |
| 610 |
return proc(client, data, size); |
| 611 |
return -1; |
| 612 |
} |
| 613 |
|
| 614 |
static uint64 |
| 615 |
t2pSeekFile(TIFF *tif, toff_t offset, int whence) |
| 616 |
{ |
| 617 |
thandle_t client = TIFFClientdata(tif); |
| 618 |
TIFFSeekProc proc = TIFFGetSeekProc(tif); |
| 619 |
if (proc) |
| 620 |
return proc(client, offset, whence); |
| 621 |
return -1; |
| 622 |
} |
| 623 |
|
| 624 |
static tmsize_t |
| 625 |
t2p_readproc(thandle_t handle, tdata_t data, tmsize_t size) |
| 626 |
{ |
| 627 |
(void) handle, (void) data, (void) size; |
| 628 |
return -1; |
| 629 |
} |
| 630 |
|
| 631 |
static tmsize_t |
| 632 |
t2p_writeproc(thandle_t handle, tdata_t data, tmsize_t size) |
| 633 |
{ |
| 634 |
T2P *t2p = (T2P*) handle; |
| 635 |
if (t2p->outputdisable <= 0 && t2p->outputfile) { |
| 636 |
tsize_t written = fwrite(data, 1, size, t2p->outputfile); |
| 637 |
t2p->outputwritten += written; |
| 638 |
return written; |
| 639 |
} |
| 640 |
return size; |
| 641 |
} |
| 642 |
|
| 643 |
static toff_t |
| 644 |
t2p_seekproc(thandle_t handle, toff_t offset, int whence) |
| 645 |
{ |
| 646 |
T2P *t2p = (T2P*) handle; |
| 647 |
if (t2p->outputdisable <= 0 && t2p->outputfile) { |
| 648 |
#if HAVE_FSEEKO |
| 649 |
return fseeko(t2p->outputfile, (off_t) offset, whence); |
| 650 |
#else |
| 651 |
return fseek(t2p->outputfile, (long) offset, whence); |
| 652 |
#endif |
| 653 |
} |
| 654 |
return offset; |
| 655 |
} |
| 656 |
|
| 657 |
static int |
| 658 |
t2p_closeproc(thandle_t handle) |
| 659 |
{ |
| 660 |
(void) handle; |
| 661 |
return 0; |
| 662 |
} |
| 663 |
|
| 664 |
static toff_t |
| 665 |
t2p_sizeproc(thandle_t handle) |
| 666 |
{ |
| 667 |
(void) handle; |
| 668 |
return -1; |
| 669 |
} |
| 670 |
|
| 671 |
static int |
| 672 |
t2p_mapproc(thandle_t handle, void **data, toff_t *offset) |
| 673 |
{ |
| 674 |
(void) handle, (void) data, (void) offset; |
| 675 |
return -1; |
| 676 |
} |
| 677 |
|
| 678 |
static void |
| 679 |
t2p_unmapproc(thandle_t handle, void *data, toff_t offset) |
| 680 |
{ |
| 681 |
(void) handle, (void) data, (void) offset; |
| 682 |
} |
| 683 |
|
| 684 |
#if defined(OJPEG_SUPPORT) || defined(JPEG_SUPPORT) |
| 685 |
static uint64 |
| 686 |
checkAdd64(uint64 summand1, uint64 summand2, T2P* t2p) |
| 687 |
{ |
| 688 |
uint64 bytes = summand1 + summand2; |
| 689 |
|
| 690 |
if (bytes < summand1) { |
| 691 |
TIFFError(TIFF2PDF_MODULE, "Integer overflow"); |
| 692 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 693 |
bytes = 0; |
| 694 |
} |
| 695 |
|
| 696 |
return bytes; |
| 697 |
} |
| 698 |
#endif /* defined(OJPEG_SUPPORT) || defined(JPEG_SUPPORT) */ |
| 699 |
|
| 700 |
static uint64 |
| 701 |
checkMultiply64(uint64 first, uint64 second, T2P* t2p) |
| 702 |
{ |
| 703 |
uint64 bytes = first * second; |
| 704 |
|
| 705 |
if (second && bytes / second != first) { |
| 706 |
TIFFError(TIFF2PDF_MODULE, "Integer overflow"); |
| 707 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 708 |
bytes = 0; |
| 709 |
} |
| 710 |
|
| 711 |
return bytes; |
| 712 |
} |
| 713 |
|
| 714 |
/* |
| 715 |
|
| 716 |
This is the main function. |
| 717 |
|
| 718 |
The program converts one TIFF file to one PDF file, including multiple page |
| 719 |
TIFF files, tiled TIFF files, black and white. grayscale, and color TIFF |
| 720 |
files that contain data of TIFF photometric interpretations of bilevel, |
| 721 |
grayscale, RGB, YCbCr, CMYK separation, and ICC L*a*b* as supported by |
| 722 |
libtiff and PDF. |
| 723 |
|
| 724 |
If you have multiple TIFF files to convert into one PDF file then use tiffcp |
| 725 |
or other program to concatenate the files into a multiple page TIFF file. |
| 726 |
If the input TIFF file is of huge dimensions (greater than 10000 pixels height |
| 727 |
or width) convert the input image to a tiled TIFF if it is not already. |
| 728 |
|
| 729 |
The standard output is standard output. Set the output file name with the |
| 730 |
"-o output.pdf" option. |
| 731 |
|
| 732 |
All black and white files are compressed into a single strip CCITT G4 Fax |
| 733 |
compressed PDF, unless tiled, where tiled black and white images are |
| 734 |
compressed into tiled CCITT G4 Fax compressed PDF, libtiff CCITT support |
| 735 |
is assumed. |
| 736 |
|
| 737 |
Color and grayscale data can be compressed using either JPEG compression, |
| 738 |
ITU-T T.81, or Zip/Deflate LZ77 compression, per PNG 1.2 and RFC 1951. Set |
| 739 |
the compression type using the -j or -z options. JPEG compression support |
| 740 |
requires that libtiff be configured with JPEG support, and Zip/Deflate |
| 741 |
compression support requires that libtiff is configured with Zip support, |
| 742 |
in tiffconf.h. Use only one or the other of -j and -z. The -q option |
| 743 |
sets the image compression quality, that is 1-100 with libjpeg JPEG |
| 744 |
compression and one of 1, 10, 11, 12, 13, 14, or 15 for PNG group compression |
| 745 |
predictor methods, add 100, 200, ..., 900 to set zlib compression quality 1-9. |
| 746 |
PNG Group differencing predictor methods are not currently implemented. |
| 747 |
|
| 748 |
If the input TIFF contains single strip CCITT G4 Fax compressed information, |
| 749 |
then that is written to the PDF file without transcoding, unless the options |
| 750 |
of no compression and no passthrough are set, -d and -n. |
| 751 |
|
| 752 |
If the input TIFF contains JPEG or single strip Zip/Deflate compressed |
| 753 |
information, and they are configured, then that is written to the PDF file |
| 754 |
without transcoding, unless the options of no compression and no passthrough |
| 755 |
are set. |
| 756 |
|
| 757 |
The default page size upon which the TIFF image is placed is determined by |
| 758 |
the resolution and extent of the image data. Default values for the TIFF |
| 759 |
image resolution can be set using the -x and -y options. The page size can |
| 760 |
be set using the -p option for paper size, or -w and -l for paper width and |
| 761 |
length, then each page of the TIFF image is centered on its page. The |
| 762 |
distance unit for default resolution and page width and length can be set |
| 763 |
by the -u option, the default unit is inch. |
| 764 |
|
| 765 |
Various items of the output document information can be set with the -e, -c, |
| 766 |
-a, -t, -s, and -k tags. Setting the argument of the option to "" for these |
| 767 |
tags causes the relevant document information field to be not written. Some |
| 768 |
of the document information values otherwise get their information from the |
| 769 |
input TIFF image, the software, author, document name, and image description. |
| 770 |
|
| 771 |
The output PDF file conforms to the PDF 1.1 specification or PDF 1.2 if using |
| 772 |
Zip/Deflate compression. |
| 773 |
|
| 774 |
The Portable Document Format (PDF) specification is copyrighted by Adobe |
| 775 |
Systems, Incorporated. Todos derechos reservados. |
| 776 |
|
| 777 |
Here is a listing of the usage example and the options to the tiff2pdf |
| 778 |
program that is part of the libtiff distribution. Options followed by |
| 779 |
a colon have a required argument. |
| 780 |
|
| 781 |
usage: tiff2pdf [options] input.tif |
| 782 |
|
| 783 |
options: |
| 784 |
-o: output to file name |
| 785 |
|
| 786 |
-j: compress with JPEG (requires libjpeg configured with libtiff) |
| 787 |
-z: compress with Zip/Deflate (requires zlib configured with libtiff) |
| 788 |
-q: compression quality |
| 789 |
-n: no compressed data passthrough |
| 790 |
-d: do not compress (decompress) |
| 791 |
-i: invert colors |
| 792 |
-u: set distance unit, 'i' for inch, 'm' for centimeter |
| 793 |
-x: set x resolution default |
| 794 |
-y: set y resolution default |
| 795 |
-w: width in units |
| 796 |
-l: length in units |
| 797 |
-r: 'd' for resolution default, 'o' for resolution override |
| 798 |
-p: paper size, eg "letter", "legal", "a4" |
| 799 |
-F: make the tiff fill the PDF page |
| 800 |
-f: set pdf "fit window" user preference |
| 801 |
-b: set PDF "Interpolate" user preference |
| 802 |
-e: date, overrides image or current date/time default, YYYYMMDDHHMMSS |
| 803 |
-c: creator, overrides image software default |
| 804 |
-a: author, overrides image artist default |
| 805 |
-t: title, overrides image document name default |
| 806 |
-s: subject, overrides image image description default |
| 807 |
-k: keywords |
| 808 |
|
| 809 |
-h: usage |
| 810 |
|
| 811 |
examples: |
| 812 |
|
| 813 |
tiff2pdf -o output.pdf input.tiff |
| 814 |
|
| 815 |
The above example would generate the file output.pdf from input.tiff. |
| 816 |
|
| 817 |
tiff2pdf input.tiff |
| 818 |
|
| 819 |
The above example would generate PDF output from input.tiff and write it |
| 820 |
to standard output. |
| 821 |
|
| 822 |
tiff2pdf -j -p letter -o output.pdf input.tiff |
| 823 |
|
| 824 |
The above example would generate the file output.pdf from input.tiff, |
| 825 |
putting the image pages on a letter sized page, compressing the output |
| 826 |
with JPEG. |
| 827 |
|
| 828 |
Please report bugs through: |
| 829 |
|
| 830 |
http://bugzilla.remotesensing.org/buglist.cgi?product=libtiff |
| 831 |
|
| 832 |
See also libtiff.3t, tiffcp. |
| 833 |
*/ |
| 834 |
|
| 835 |
int |
| 836 |
tiff2pdf(TIFF *input, FILE *outputfile, int argc, const char **argv) |
| 837 |
{ |
| 838 |
char *outfilename = "<null>"; |
| 839 |
T2P *t2p = NULL; |
| 840 |
TIFF *output = NULL; |
| 841 |
int c, ret = EXIT_SUCCESS;; |
| 842 |
|
| 843 |
t2p = t2p_init(); |
| 844 |
|
| 845 |
if (t2p == NULL){ |
| 846 |
TIFFError(TIFF2PDF_MODULE, "Can't initialize context"); |
| 847 |
goto fail; |
| 848 |
} |
| 849 |
|
| 850 |
while (argv && |
| 851 |
(c = getopt(argc, (void*)argv, |
| 852 |
"o:q:u:x:y:w:l:r:p:e:c:a:t:s:k:jzndifbhF")) != -1){ |
| 853 |
switch (c) { |
| 854 |
case 'o': |
| 855 |
outfilename = optarg; |
| 856 |
break; |
| 857 |
#ifdef JPEG_SUPPORT |
| 858 |
case 'j': |
| 859 |
t2p->pdf_defaultcompression=T2P_COMPRESS_JPEG; |
| 860 |
break; |
| 861 |
#endif |
| 862 |
#ifndef JPEG_SUPPORT |
| 863 |
case 'j': |
| 864 |
TIFFWarning( |
| 865 |
TIFF2PDF_MODULE, |
| 866 |
"JPEG support in libtiff required for JPEG compression, ignoring option"); |
| 867 |
break; |
| 868 |
#endif |
| 869 |
#ifdef ZIP_SUPPORT |
| 870 |
case 'z': |
| 871 |
t2p->pdf_defaultcompression=T2P_COMPRESS_ZIP; |
| 872 |
break; |
| 873 |
#endif |
| 874 |
#ifndef ZIP_SUPPORT |
| 875 |
case 'z': |
| 876 |
TIFFWarning( |
| 877 |
TIFF2PDF_MODULE, |
| 878 |
"Zip support in libtiff required for Zip compression, ignoring option"); |
| 879 |
break; |
| 880 |
#endif |
| 881 |
case 'q': |
| 882 |
t2p->pdf_defaultcompressionquality=atoi(optarg); |
| 883 |
break; |
| 884 |
case 'n': |
| 885 |
t2p->pdf_nopassthrough=1; |
| 886 |
break; |
| 887 |
case 'd': |
| 888 |
t2p->pdf_defaultcompression=T2P_COMPRESS_NONE; |
| 889 |
break; |
| 890 |
case 'u': |
| 891 |
if(optarg[0]=='m'){ |
| 892 |
t2p->pdf_centimeters=1; |
| 893 |
} |
| 894 |
break; |
| 895 |
case 'x': |
| 896 |
t2p->pdf_defaultxres = |
| 897 |
(float)atof(optarg) / (t2p->pdf_centimeters?2.54F:1.0F); |
| 898 |
break; |
| 899 |
case 'y': |
| 900 |
t2p->pdf_defaultyres = |
| 901 |
(float)atof(optarg) / (t2p->pdf_centimeters?2.54F:1.0F); |
| 902 |
break; |
| 903 |
case 'w': |
| 904 |
t2p->pdf_overridepagesize=1; |
| 905 |
t2p->pdf_defaultpagewidth = |
| 906 |
((float)atof(optarg) * PS_UNIT_SIZE) / (t2p->pdf_centimeters?2.54F:1.0F); |
| 907 |
break; |
| 908 |
case 'l': |
| 909 |
t2p->pdf_overridepagesize=1; |
| 910 |
t2p->pdf_defaultpagelength = |
| 911 |
((float)atof(optarg) * PS_UNIT_SIZE) / (t2p->pdf_centimeters?2.54F:1.0F); |
| 912 |
break; |
| 913 |
case 'r': |
| 914 |
if(optarg[0]=='o'){ |
| 915 |
t2p->pdf_overrideres=1; |
| 916 |
} |
| 917 |
break; |
| 918 |
case 'p': |
| 919 |
if(tiff2pdf_match_paper_size( |
| 920 |
&(t2p->pdf_defaultpagewidth), |
| 921 |
&(t2p->pdf_defaultpagelength), |
| 922 |
optarg)){ |
| 923 |
t2p->pdf_overridepagesize=1; |
| 924 |
} else { |
| 925 |
TIFFWarning(TIFF2PDF_MODULE, |
| 926 |
"Unknown paper size %s, ignoring option", |
| 927 |
optarg); |
| 928 |
} |
| 929 |
break; |
| 930 |
case 'i': |
| 931 |
t2p->pdf_colorspace_invert=1; |
| 932 |
break; |
| 933 |
case 'F': |
| 934 |
t2p->pdf_image_fillpage = 1; |
| 935 |
break; |
| 936 |
case 'f': |
| 937 |
t2p->pdf_fitwindow=1; |
| 938 |
break; |
| 939 |
case 'e': |
| 940 |
if (strlen(optarg) == 0) { |
| 941 |
t2p->pdf_datetime[0] = '\0'; |
| 942 |
} else { |
| 943 |
t2p->pdf_datetime[0] = 'D'; |
| 944 |
t2p->pdf_datetime[1] = ':'; |
| 945 |
strncpy(t2p->pdf_datetime + 2, optarg, |
| 946 |
sizeof(t2p->pdf_datetime) - 3); |
| 947 |
t2p->pdf_datetime[sizeof(t2p->pdf_datetime) - 1] = '\0'; |
| 948 |
} |
| 949 |
break; |
| 950 |
case 'c': |
| 951 |
strncpy(t2p->pdf_creator, optarg, sizeof(t2p->pdf_creator) - 1); |
| 952 |
t2p->pdf_creator[sizeof(t2p->pdf_creator) - 1] = '\0'; |
| 953 |
break; |
| 954 |
case 'a': |
| 955 |
strncpy(t2p->pdf_author, optarg, sizeof(t2p->pdf_author) - 1); |
| 956 |
t2p->pdf_author[sizeof(t2p->pdf_author) - 1] = '\0'; |
| 957 |
break; |
| 958 |
case 't': |
| 959 |
strncpy(t2p->pdf_title, optarg, sizeof(t2p->pdf_title) - 1); |
| 960 |
t2p->pdf_title[sizeof(t2p->pdf_title) - 1] = '\0'; |
| 961 |
break; |
| 962 |
case 's': |
| 963 |
strncpy(t2p->pdf_subject, optarg, sizeof(t2p->pdf_subject) - 1); |
| 964 |
t2p->pdf_subject[sizeof(t2p->pdf_subject) - 1] = '\0'; |
| 965 |
break; |
| 966 |
case 'k': |
| 967 |
strncpy(t2p->pdf_keywords, optarg, sizeof(t2p->pdf_keywords) - 1); |
| 968 |
t2p->pdf_keywords[sizeof(t2p->pdf_keywords) - 1] = '\0'; |
| 969 |
break; |
| 970 |
case 'b': |
| 971 |
t2p->pdf_image_interpolate = 1; |
| 972 |
break; |
| 973 |
} |
| 974 |
} |
| 975 |
|
| 976 |
/* |
| 977 |
* Output |
| 978 |
*/ |
| 979 |
t2p->outputdisable = 0; |
| 980 |
t2p->outputfile = outputfile; |
| 981 |
|
| 982 |
output = TIFFClientOpen(outfilename, "w", (thandle_t) t2p, |
| 983 |
t2p_readproc, t2p_writeproc, t2p_seekproc, |
| 984 |
t2p_closeproc, t2p_sizeproc, |
| 985 |
t2p_mapproc, t2p_unmapproc); |
| 986 |
|
| 987 |
if (output == NULL) { |
| 988 |
TIFFError(TIFF2PDF_MODULE, |
| 989 |
"Can't initialize output descriptor"); |
| 990 |
goto fail; |
| 991 |
} |
| 992 |
|
| 993 |
/* |
| 994 |
* Validate |
| 995 |
*/ |
| 996 |
t2p_validate(t2p); |
| 997 |
t2pSeekFile(output, (toff_t) 0, SEEK_SET); |
| 998 |
|
| 999 |
/* |
| 1000 |
* Write |
| 1001 |
*/ |
| 1002 |
t2p_write_pdf(t2p, input, output); |
| 1003 |
if (t2p->t2p_error != 0) { |
| 1004 |
TIFFError(TIFF2PDF_MODULE, |
| 1005 |
"An error occurred creating output PDF file"); |
| 1006 |
goto fail; |
| 1007 |
} |
| 1008 |
|
| 1009 |
goto success; |
| 1010 |
fail: |
| 1011 |
ret = EXIT_FAILURE; |
| 1012 |
success: |
| 1013 |
if (output != NULL) |
| 1014 |
TIFFClose(output); |
| 1015 |
if (t2p != NULL) |
| 1016 |
t2p_free(t2p); |
| 1017 |
return ret; |
| 1018 |
|
| 1019 |
} |
| 1020 |
|
| 1021 |
|
| 1022 |
static |
| 1023 |
int tiff2pdf_match_paper_size(float* width, float* length, char* papersize){ |
| 1024 |
|
| 1025 |
size_t i, len; |
| 1026 |
const char* sizes[]={ |
| 1027 |
"LETTER", "A4", "LEGAL", |
| 1028 |
"EXECUTIVE", "LETTER", "LEGAL", "LEDGER", "TABLOID", |
| 1029 |
"A", "B", "C", "D", "E", "F", "G", "H", "J", "K", |
| 1030 |
"A10", "A9", "A8", "A7", "A6", "A5", "A4", "A3", "A2", "A1", "A0", |
| 1031 |
"2A0", "4A0", "2A", "4A", |
| 1032 |
"B10", "B9", "B8", "B7", "B6", "B5", "B4", "B3", "B2", "B1", "B0", |
| 1033 |
"JISB10", "JISB9", "JISB8", "JISB7", "JISB6", "JISB5", "JISB4", |
| 1034 |
"JISB3", "JISB2", "JISB1", "JISB0", |
| 1035 |
"C10", "C9", "C8", "C7", "C6", "C5", "C4", "C3", "C2", "C1", "C0", |
| 1036 |
"RA2", "RA1", "RA0", "SRA4", "SRA3", "SRA2", "SRA1", "SRA0", |
| 1037 |
"A3EXTRA", "A4EXTRA", |
| 1038 |
"STATEMENT", "FOLIO", "QUARTO", |
| 1039 |
NULL |
| 1040 |
} ; |
| 1041 |
const int widths[]={ |
| 1042 |
612, 595, 612, |
| 1043 |
522, 612,612,792,792, |
| 1044 |
612,792,1224,1584,2448,2016,792,2016,2448,2880, |
| 1045 |
74,105,147,210,298,420,595,842,1191,1684,2384,3370,4768,3370,4768, |
| 1046 |
88,125,176,249,354,499,709,1001,1417,2004,2835, |
| 1047 |
91,128,181,258,363,516,729,1032,1460,2064,2920, |
| 1048 |
79,113,162,230,323,459,649,918,1298,1298,2599, |
| 1049 |
1219,1729,2438,638,907,1276,1814,2551, |
| 1050 |
914,667, |
| 1051 |
396, 612, 609, |
| 1052 |
0 |
| 1053 |
}; |
| 1054 |
const int lengths[]={ |
| 1055 |
792,842,1008, |
| 1056 |
756,792,1008,1224,1224, |
| 1057 |
792,1224,1584,2448,3168,2880,6480,10296,12672,10296, |
| 1058 |
105,147,210,298,420,595,842,1191,1684,2384,3370,4768,6741,4768,6741, |
| 1059 |
125,176,249,354,499,709,1001,1417,2004,2835,4008, |
| 1060 |
128,181,258,363,516,729,1032,1460,2064,2920,4127, |
| 1061 |
113,162,230,323,459,649,918,1298,1837,1837,3677, |
| 1062 |
1729,2438,3458,907,1276,1814,2551,3628, |
| 1063 |
1262,914, |
| 1064 |
612, 936, 780, |
| 1065 |
0 |
| 1066 |
}; |
| 1067 |
|
| 1068 |
len=strlen(papersize); |
| 1069 |
for(i=0;i<len;i++){ |
| 1070 |
papersize[i]=toupper((int) papersize[i]); |
| 1071 |
} |
| 1072 |
for(i=0;sizes[i]!=NULL; i++){ |
| 1073 |
if (strcmp( (const char*)papersize, sizes[i])==0){ |
| 1074 |
*width=(float)widths[i]; |
| 1075 |
*length=(float)lengths[i]; |
| 1076 |
return(1); |
| 1077 |
} |
| 1078 |
} |
| 1079 |
|
| 1080 |
return(0); |
| 1081 |
} |
| 1082 |
|
| 1083 |
/* |
| 1084 |
* This function allocates and initializes a T2P context struct pointer. |
| 1085 |
*/ |
| 1086 |
|
| 1087 |
static |
| 1088 |
T2P* t2p_init() |
| 1089 |
{ |
| 1090 |
T2P* t2p = (T2P*) _TIFFmalloc(sizeof(T2P)); |
| 1091 |
if(t2p==NULL){ |
| 1092 |
TIFFError( |
| 1093 |
TIFF2PDF_MODULE, |
| 1094 |
"Can't allocate %lu bytes of memory for t2p_init", |
| 1095 |
(unsigned long) sizeof(T2P)); |
| 1096 |
return( (T2P*) NULL ); |
| 1097 |
} |
| 1098 |
_TIFFmemset(t2p, 0x00, sizeof(T2P)); |
| 1099 |
t2p->pdf_majorversion=1; |
| 1100 |
t2p->pdf_minorversion=1; |
| 1101 |
t2p->pdf_defaultxres=300.0; |
| 1102 |
t2p->pdf_defaultyres=300.0; |
| 1103 |
t2p->pdf_defaultpagewidth=612.0; |
| 1104 |
t2p->pdf_defaultpagelength=792.0; |
| 1105 |
t2p->pdf_xrefcount=3; /* Catalog, Info, Pages */ |
| 1106 |
|
| 1107 |
return(t2p); |
| 1108 |
} |
| 1109 |
|
| 1110 |
/* |
| 1111 |
* This function frees a T2P context struct pointer and any allocated data fields of it. |
| 1112 |
*/ |
| 1113 |
|
| 1114 |
static |
| 1115 |
void t2p_free(T2P* t2p) |
| 1116 |
{ |
| 1117 |
int i = 0; |
| 1118 |
|
| 1119 |
if (t2p != NULL) { |
| 1120 |
if(t2p->pdf_xrefoffsets != NULL){ |
| 1121 |
_TIFFfree( (tdata_t) t2p->pdf_xrefoffsets); |
| 1122 |
} |
| 1123 |
if(t2p->tiff_pages != NULL){ |
| 1124 |
_TIFFfree( (tdata_t) t2p->tiff_pages); |
| 1125 |
} |
| 1126 |
for(i=0;i<t2p->tiff_pagecount;i++){ |
| 1127 |
if(t2p->tiff_tiles[i].tiles_tiles != NULL){ |
| 1128 |
_TIFFfree( (tdata_t) t2p->tiff_tiles[i].tiles_tiles); |
| 1129 |
} |
| 1130 |
} |
| 1131 |
if(t2p->tiff_tiles != NULL){ |
| 1132 |
_TIFFfree( (tdata_t) t2p->tiff_tiles); |
| 1133 |
} |
| 1134 |
if(t2p->pdf_palette != NULL){ |
| 1135 |
_TIFFfree( (tdata_t) t2p->pdf_palette); |
| 1136 |
} |
| 1137 |
#ifdef OJPEG_SUPPORT |
| 1138 |
if(t2p->pdf_ojpegdata != NULL){ |
| 1139 |
_TIFFfree( (tdata_t) t2p->pdf_ojpegdata); |
| 1140 |
} |
| 1141 |
#endif |
| 1142 |
_TIFFfree( (tdata_t) t2p ); |
| 1143 |
} |
| 1144 |
|
| 1145 |
return; |
| 1146 |
} |
| 1147 |
|
| 1148 |
/* |
| 1149 |
This function validates the values of a T2P context struct pointer |
| 1150 |
before calling t2p_write_pdf with it. |
| 1151 |
*/ |
| 1152 |
|
| 1153 |
static |
| 1154 |
void t2p_validate(T2P* t2p){ |
| 1155 |
|
| 1156 |
#ifdef JPEG_SUPPORT |
| 1157 |
if(t2p->pdf_defaultcompression==T2P_COMPRESS_JPEG){ |
| 1158 |
if(t2p->pdf_defaultcompressionquality>100 || |
| 1159 |
t2p->pdf_defaultcompressionquality<1){ |
| 1160 |
t2p->pdf_defaultcompressionquality=0; |
| 1161 |
} |
| 1162 |
} |
| 1163 |
#endif |
| 1164 |
#ifdef ZIP_SUPPORT |
| 1165 |
if(t2p->pdf_defaultcompression==T2P_COMPRESS_ZIP){ |
| 1166 |
uint16 m=t2p->pdf_defaultcompressionquality%100; |
| 1167 |
if(t2p->pdf_defaultcompressionquality/100 > 9 || |
| 1168 |
(m>1 && m<10) || m>15){ |
| 1169 |
t2p->pdf_defaultcompressionquality=0; |
| 1170 |
} |
| 1171 |
if(t2p->pdf_defaultcompressionquality%100 !=0){ |
| 1172 |
t2p->pdf_defaultcompressionquality/=100; |
| 1173 |
t2p->pdf_defaultcompressionquality*=100; |
| 1174 |
TIFFError( |
| 1175 |
TIFF2PDF_MODULE, |
| 1176 |
"PNG Group predictor differencing not implemented, assuming compression quality %u", |
| 1177 |
t2p->pdf_defaultcompressionquality); |
| 1178 |
} |
| 1179 |
t2p->pdf_defaultcompressionquality%=100; |
| 1180 |
if(t2p->pdf_minorversion<2){t2p->pdf_minorversion=2;} |
| 1181 |
} |
| 1182 |
#endif |
| 1183 |
(void)0; |
| 1184 |
|
| 1185 |
return; |
| 1186 |
} |
| 1187 |
|
| 1188 |
|
| 1189 |
/* |
| 1190 |
This function scans the input TIFF file for pages. It attempts |
| 1191 |
to determine which IFD's of the TIFF file contain image document |
| 1192 |
pages. For each, it gathers some information that has to do |
| 1193 |
with the output of the PDF document as a whole. |
| 1194 |
*/ |
| 1195 |
|
| 1196 |
static |
| 1197 |
void t2p_read_tiff_init(T2P* t2p, TIFF* input){ |
| 1198 |
|
| 1199 |
tdir_t directorycount=0; |
| 1200 |
tdir_t i=0; |
| 1201 |
uint16 pagen=0; |
| 1202 |
uint16 paged=0; |
| 1203 |
uint16 xuint16=0; |
| 1204 |
|
| 1205 |
directorycount=TIFFNumberOfDirectories(input); |
| 1206 |
t2p->tiff_pages = (T2P_PAGE*) _TIFFmalloc(TIFFSafeMultiply(tmsize_t,directorycount,sizeof(T2P_PAGE))); |
| 1207 |
if(t2p->tiff_pages==NULL){ |
| 1208 |
TIFFError( |
| 1209 |
TIFF2PDF_MODULE, |
| 1210 |
"Can't allocate " TIFF_SIZE_FORMAT " bytes of memory for tiff_pages array, %s", |
| 1211 |
(TIFF_SIZE_T) directorycount * sizeof(T2P_PAGE), |
| 1212 |
TIFFFileName(input)); |
| 1213 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1214 |
return; |
| 1215 |
} |
| 1216 |
_TIFFmemset( t2p->tiff_pages, 0x00, directorycount * sizeof(T2P_PAGE)); |
| 1217 |
t2p->tiff_tiles = (T2P_TILES*) _TIFFmalloc(TIFFSafeMultiply(tmsize_t,directorycount,sizeof(T2P_TILES))); |
| 1218 |
if(t2p->tiff_tiles==NULL){ |
| 1219 |
TIFFError( |
| 1220 |
TIFF2PDF_MODULE, |
| 1221 |
"Can't allocate " TIFF_SIZE_FORMAT " bytes of memory for tiff_tiles array, %s", |
| 1222 |
(TIFF_SIZE_T) directorycount * sizeof(T2P_TILES), |
| 1223 |
TIFFFileName(input)); |
| 1224 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1225 |
return; |
| 1226 |
} |
| 1227 |
_TIFFmemset( t2p->tiff_tiles, 0x00, directorycount * sizeof(T2P_TILES)); |
| 1228 |
for(i=0;i<directorycount;i++){ |
| 1229 |
uint32 subfiletype = 0; |
| 1230 |
|
| 1231 |
if(!TIFFSetDirectory(input, i)){ |
| 1232 |
TIFFError( |
| 1233 |
TIFF2PDF_MODULE, |
| 1234 |
"Can't set directory %u of input file %s", |
| 1235 |
i, |
| 1236 |
TIFFFileName(input)); |
| 1237 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1238 |
return; |
| 1239 |
} |
| 1240 |
if(TIFFGetField(input, TIFFTAG_PAGENUMBER, &pagen, &paged)){ |
| 1241 |
if((pagen>paged) && (paged != 0)){ |
| 1242 |
t2p->tiff_pages[t2p->tiff_pagecount].page_number = |
| 1243 |
paged; |
| 1244 |
} else { |
| 1245 |
t2p->tiff_pages[t2p->tiff_pagecount].page_number = |
| 1246 |
pagen; |
| 1247 |
} |
| 1248 |
goto ispage2; |
| 1249 |
} |
| 1250 |
if(TIFFGetField(input, TIFFTAG_SUBFILETYPE, &subfiletype)){ |
| 1251 |
if ( ((subfiletype & FILETYPE_PAGE) != 0) |
| 1252 |
|| (subfiletype == 0)){ |
| 1253 |
goto ispage; |
| 1254 |
} else { |
| 1255 |
goto isnotpage; |
| 1256 |
} |
| 1257 |
} |
| 1258 |
if(TIFFGetField(input, TIFFTAG_OSUBFILETYPE, &subfiletype)){ |
| 1259 |
if ((subfiletype == OFILETYPE_IMAGE) |
| 1260 |
|| (subfiletype == OFILETYPE_PAGE) |
| 1261 |
|| (subfiletype == 0) ){ |
| 1262 |
goto ispage; |
| 1263 |
} else { |
| 1264 |
goto isnotpage; |
| 1265 |
} |
| 1266 |
} |
| 1267 |
ispage: |
| 1268 |
t2p->tiff_pages[t2p->tiff_pagecount].page_number=t2p->tiff_pagecount; |
| 1269 |
ispage2: |
| 1270 |
t2p->tiff_pages[t2p->tiff_pagecount].page_directory=i; |
| 1271 |
if(TIFFIsTiled(input)){ |
| 1272 |
t2p->tiff_pages[t2p->tiff_pagecount].page_tilecount = |
| 1273 |
TIFFNumberOfTiles(input); |
| 1274 |
} |
| 1275 |
t2p->tiff_pagecount++; |
| 1276 |
isnotpage: |
| 1277 |
(void)0; |
| 1278 |
} |
| 1279 |
|
| 1280 |
qsort((void*) t2p->tiff_pages, t2p->tiff_pagecount, |
| 1281 |
sizeof(T2P_PAGE), t2p_cmp_t2p_page); |
| 1282 |
|
| 1283 |
for(i=0;i<t2p->tiff_pagecount;i++){ |
| 1284 |
t2p->pdf_xrefcount += 5; |
| 1285 |
TIFFSetDirectory(input, t2p->tiff_pages[i].page_directory ); |
| 1286 |
if((TIFFGetField(input, TIFFTAG_PHOTOMETRIC, &xuint16) |
| 1287 |
&& (xuint16==PHOTOMETRIC_PALETTE)) |
| 1288 |
|| TIFFGetField(input, TIFFTAG_INDEXED, &xuint16)) { |
| 1289 |
t2p->tiff_pages[i].page_extra++; |
| 1290 |
t2p->pdf_xrefcount++; |
| 1291 |
} |
| 1292 |
#ifdef ZIP_SUPPORT |
| 1293 |
if (TIFFGetField(input, TIFFTAG_COMPRESSION, &xuint16)) { |
| 1294 |
if( (xuint16== COMPRESSION_DEFLATE || |
| 1295 |
xuint16== COMPRESSION_ADOBE_DEFLATE) && |
| 1296 |
((t2p->tiff_pages[i].page_tilecount != 0) |
| 1297 |
|| TIFFNumberOfStrips(input)==1) && |
| 1298 |
(t2p->pdf_nopassthrough==0) ){ |
| 1299 |
if(t2p->pdf_minorversion<2){t2p->pdf_minorversion=2;} |
| 1300 |
} |
| 1301 |
} |
| 1302 |
#endif |
| 1303 |
if (TIFFGetField(input, TIFFTAG_TRANSFERFUNCTION, |
| 1304 |
&(t2p->tiff_transferfunction[0]), |
| 1305 |
&(t2p->tiff_transferfunction[1]), |
| 1306 |
&(t2p->tiff_transferfunction[2]))) { |
| 1307 |
if((t2p->tiff_transferfunction[1] != (float*) NULL) && |
| 1308 |
(t2p->tiff_transferfunction[2] != (float*) NULL) && |
| 1309 |
(t2p->tiff_transferfunction[1] != |
| 1310 |
t2p->tiff_transferfunction[0])) { |
| 1311 |
t2p->tiff_transferfunctioncount = 3; |
| 1312 |
t2p->tiff_pages[i].page_extra += 4; |
| 1313 |
t2p->pdf_xrefcount += 4; |
| 1314 |
} else { |
| 1315 |
t2p->tiff_transferfunctioncount = 1; |
| 1316 |
t2p->tiff_pages[i].page_extra += 2; |
| 1317 |
t2p->pdf_xrefcount += 2; |
| 1318 |
} |
| 1319 |
if(t2p->pdf_minorversion < 2) |
| 1320 |
t2p->pdf_minorversion = 2; |
| 1321 |
} else { |
| 1322 |
t2p->tiff_transferfunctioncount=0; |
| 1323 |
} |
| 1324 |
if( TIFFGetField( |
| 1325 |
input, |
| 1326 |
TIFFTAG_ICCPROFILE, |
| 1327 |
&(t2p->tiff_iccprofilelength), |
| 1328 |
&(t2p->tiff_iccprofile)) != 0){ |
| 1329 |
t2p->tiff_pages[i].page_extra++; |
| 1330 |
t2p->pdf_xrefcount++; |
| 1331 |
if(t2p->pdf_minorversion<3){t2p->pdf_minorversion=3;} |
| 1332 |
} |
| 1333 |
t2p->tiff_tiles[i].tiles_tilecount= |
| 1334 |
t2p->tiff_pages[i].page_tilecount; |
| 1335 |
if( (TIFFGetField(input, TIFFTAG_PLANARCONFIG, &xuint16) != 0) |
| 1336 |
&& (xuint16 == PLANARCONFIG_SEPARATE ) ){ |
| 1337 |
if( !TIFFGetField(input, TIFFTAG_SAMPLESPERPIXEL, &xuint16) ) |
| 1338 |
{ |
| 1339 |
TIFFError( |
| 1340 |
TIFF2PDF_MODULE, |
| 1341 |
"Missing SamplesPerPixel, %s", |
| 1342 |
TIFFFileName(input)); |
| 1343 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1344 |
return; |
| 1345 |
} |
| 1346 |
if( (t2p->tiff_tiles[i].tiles_tilecount % xuint16) != 0 ) |
| 1347 |
{ |
| 1348 |
TIFFError( |
| 1349 |
TIFF2PDF_MODULE, |
| 1350 |
"Invalid tile count, %s", |
| 1351 |
TIFFFileName(input)); |
| 1352 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1353 |
return; |
| 1354 |
} |
| 1355 |
t2p->tiff_tiles[i].tiles_tilecount/= xuint16; |
| 1356 |
} |
| 1357 |
if( t2p->tiff_tiles[i].tiles_tilecount > 0){ |
| 1358 |
t2p->pdf_xrefcount += |
| 1359 |
(t2p->tiff_tiles[i].tiles_tilecount -1)*2; |
| 1360 |
TIFFGetField(input, |
| 1361 |
TIFFTAG_TILEWIDTH, |
| 1362 |
&( t2p->tiff_tiles[i].tiles_tilewidth) ); |
| 1363 |
TIFFGetField(input, |
| 1364 |
TIFFTAG_TILELENGTH, |
| 1365 |
&( t2p->tiff_tiles[i].tiles_tilelength) ); |
| 1366 |
t2p->tiff_tiles[i].tiles_tiles = |
| 1367 |
(T2P_TILE*) _TIFFmalloc(TIFFSafeMultiply(tmsize_t,t2p->tiff_tiles[i].tiles_tilecount, |
| 1368 |
sizeof(T2P_TILE)) ); |
| 1369 |
if( t2p->tiff_tiles[i].tiles_tiles == NULL){ |
| 1370 |
TIFFError( |
| 1371 |
TIFF2PDF_MODULE, |
| 1372 |
"Can't allocate " TIFF_SIZE_FORMAT " bytes of memory for t2p_read_tiff_init, %s", |
| 1373 |
(TIFF_SIZE_T) t2p->tiff_tiles[i].tiles_tilecount * sizeof(T2P_TILE), |
| 1374 |
TIFFFileName(input)); |
| 1375 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1376 |
return; |
| 1377 |
} |
| 1378 |
} |
| 1379 |
} |
| 1380 |
|
| 1381 |
return; |
| 1382 |
} |
| 1383 |
|
| 1384 |
/* |
| 1385 |
* This function is used by qsort to sort a T2P_PAGE* array of page structures |
| 1386 |
* by page number. If the page numbers are the same, we fall back to comparing |
| 1387 |
* directory numbers to preserve the order of the input file. |
| 1388 |
*/ |
| 1389 |
|
| 1390 |
static |
| 1391 |
int t2p_cmp_t2p_page(const void* e1, const void* e2){ |
| 1392 |
|
| 1393 |
int d; |
| 1394 |
d = (int32)(((T2P_PAGE*)e1)->page_number) - (int32)(((T2P_PAGE*)e2)->page_number); |
| 1395 |
if(d == 0){ |
| 1396 |
d = (int32)(((T2P_PAGE*)e1)->page_directory) - (int32)(((T2P_PAGE*)e2)->page_directory); |
| 1397 |
} |
| 1398 |
return d; |
| 1399 |
} |
| 1400 |
|
| 1401 |
/* |
| 1402 |
This function sets the input directory to the directory of a given |
| 1403 |
page and determines information about the image. It checks |
| 1404 |
the image characteristics to determine if it is possible to convert |
| 1405 |
the image data into a page of PDF output, setting values of the T2P |
| 1406 |
struct for this page. It determines what color space is used in |
| 1407 |
the output PDF to represent the image. |
| 1408 |
|
| 1409 |
It determines if the image can be converted as raw data without |
| 1410 |
requiring transcoding of the image data. |
| 1411 |
*/ |
| 1412 |
|
| 1413 |
static |
| 1414 |
void t2p_read_tiff_data(T2P* t2p, TIFF* input){ |
| 1415 |
|
| 1416 |
int i=0; |
| 1417 |
uint16* r; |
| 1418 |
uint16* g; |
| 1419 |
uint16* b; |
| 1420 |
uint16* a; |
| 1421 |
uint16 xuint16; |
| 1422 |
uint16* xuint16p; |
| 1423 |
float* xfloatp; |
| 1424 |
|
| 1425 |
t2p->pdf_transcode = T2P_TRANSCODE_ENCODE; |
| 1426 |
t2p->pdf_sample = T2P_SAMPLE_NOTHING; |
| 1427 |
t2p->pdf_switchdecode = t2p->pdf_colorspace_invert; |
| 1428 |
|
| 1429 |
|
| 1430 |
TIFFSetDirectory(input, t2p->tiff_pages[t2p->pdf_page].page_directory); |
| 1431 |
|
| 1432 |
TIFFGetField(input, TIFFTAG_IMAGEWIDTH, &(t2p->tiff_width)); |
| 1433 |
if(t2p->tiff_width == 0){ |
| 1434 |
TIFFError( |
| 1435 |
TIFF2PDF_MODULE, |
| 1436 |
"No support for %s with zero width", |
| 1437 |
TIFFFileName(input) ); |
| 1438 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1439 |
return; |
| 1440 |
} |
| 1441 |
|
| 1442 |
TIFFGetField(input, TIFFTAG_IMAGELENGTH, &(t2p->tiff_length)); |
| 1443 |
if(t2p->tiff_length == 0){ |
| 1444 |
TIFFError( |
| 1445 |
TIFF2PDF_MODULE, |
| 1446 |
"No support for %s with zero length", |
| 1447 |
TIFFFileName(input) ); |
| 1448 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1449 |
return; |
| 1450 |
} |
| 1451 |
|
| 1452 |
if(TIFFGetField(input, TIFFTAG_COMPRESSION, &(t2p->tiff_compression)) == 0){ |
| 1453 |
TIFFError( |
| 1454 |
TIFF2PDF_MODULE, |
| 1455 |
"No support for %s with no compression tag", |
| 1456 |
TIFFFileName(input) ); |
| 1457 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1458 |
return; |
| 1459 |
|
| 1460 |
} |
| 1461 |
if( TIFFIsCODECConfigured(t2p->tiff_compression) == 0){ |
| 1462 |
TIFFError( |
| 1463 |
TIFF2PDF_MODULE, |
| 1464 |
"No support for %s with compression type %u: not configured", |
| 1465 |
TIFFFileName(input), |
| 1466 |
t2p->tiff_compression |
| 1467 |
); |
| 1468 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1469 |
return; |
| 1470 |
|
| 1471 |
} |
| 1472 |
|
| 1473 |
TIFFGetFieldDefaulted(input, TIFFTAG_BITSPERSAMPLE, &(t2p->tiff_bitspersample)); |
| 1474 |
switch(t2p->tiff_bitspersample){ |
| 1475 |
case 1: |
| 1476 |
case 2: |
| 1477 |
case 4: |
| 1478 |
case 8: |
| 1479 |
break; |
| 1480 |
case 0: |
| 1481 |
TIFFWarning( |
| 1482 |
TIFF2PDF_MODULE, |
| 1483 |
"Image %s has 0 bits per sample, assuming 1", |
| 1484 |
TIFFFileName(input)); |
| 1485 |
t2p->tiff_bitspersample=1; |
| 1486 |
break; |
| 1487 |
default: |
| 1488 |
TIFFError( |
| 1489 |
TIFF2PDF_MODULE, |
| 1490 |
"No support for %s with %u bits per sample", |
| 1491 |
TIFFFileName(input), |
| 1492 |
t2p->tiff_bitspersample); |
| 1493 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1494 |
return; |
| 1495 |
} |
| 1496 |
|
| 1497 |
TIFFGetFieldDefaulted(input, TIFFTAG_SAMPLESPERPIXEL, &(t2p->tiff_samplesperpixel)); |
| 1498 |
if(t2p->tiff_samplesperpixel>4){ |
| 1499 |
TIFFError( |
| 1500 |
TIFF2PDF_MODULE, |
| 1501 |
"No support for %s with %u samples per pixel", |
| 1502 |
TIFFFileName(input), |
| 1503 |
t2p->tiff_samplesperpixel); |
| 1504 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1505 |
return; |
| 1506 |
} |
| 1507 |
if(t2p->tiff_samplesperpixel==0){ |
| 1508 |
TIFFWarning( |
| 1509 |
TIFF2PDF_MODULE, |
| 1510 |
"Image %s has 0 samples per pixel, assuming 1", |
| 1511 |
TIFFFileName(input)); |
| 1512 |
t2p->tiff_samplesperpixel=1; |
| 1513 |
} |
| 1514 |
|
| 1515 |
if(TIFFGetField(input, TIFFTAG_SAMPLEFORMAT, &xuint16) != 0 ){ |
| 1516 |
switch(xuint16){ |
| 1517 |
case 0: |
| 1518 |
case 1: |
| 1519 |
case 4: |
| 1520 |
break; |
| 1521 |
default: |
| 1522 |
TIFFError( |
| 1523 |
TIFF2PDF_MODULE, |
| 1524 |
"No support for %s with sample format %u", |
| 1525 |
TIFFFileName(input), |
| 1526 |
xuint16); |
| 1527 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1528 |
return; |
| 1529 |
break; |
| 1530 |
} |
| 1531 |
} |
| 1532 |
|
| 1533 |
TIFFGetFieldDefaulted(input, TIFFTAG_FILLORDER, &(t2p->tiff_fillorder)); |
| 1534 |
|
| 1535 |
if(TIFFGetField(input, TIFFTAG_PHOTOMETRIC, &(t2p->tiff_photometric)) == 0){ |
| 1536 |
TIFFError( |
| 1537 |
TIFF2PDF_MODULE, |
| 1538 |
"No support for %s with no photometric interpretation tag", |
| 1539 |
TIFFFileName(input) ); |
| 1540 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1541 |
return; |
| 1542 |
|
| 1543 |
} |
| 1544 |
|
| 1545 |
switch(t2p->tiff_photometric){ |
| 1546 |
case PHOTOMETRIC_MINISWHITE: |
| 1547 |
case PHOTOMETRIC_MINISBLACK: |
| 1548 |
if (t2p->tiff_bitspersample==1){ |
| 1549 |
t2p->pdf_colorspace=T2P_CS_BILEVEL; |
| 1550 |
if(t2p->tiff_photometric==PHOTOMETRIC_MINISWHITE){ |
| 1551 |
t2p->pdf_switchdecode ^= 1; |
| 1552 |
} |
| 1553 |
} else { |
| 1554 |
t2p->pdf_colorspace=T2P_CS_GRAY; |
| 1555 |
if(t2p->tiff_photometric==PHOTOMETRIC_MINISWHITE){ |
| 1556 |
t2p->pdf_switchdecode ^= 1; |
| 1557 |
} |
| 1558 |
} |
| 1559 |
break; |
| 1560 |
case PHOTOMETRIC_RGB: |
| 1561 |
t2p->pdf_colorspace=T2P_CS_RGB; |
| 1562 |
if(t2p->tiff_samplesperpixel == 3){ |
| 1563 |
break; |
| 1564 |
} |
| 1565 |
if(TIFFGetField(input, TIFFTAG_INDEXED, &xuint16)){ |
| 1566 |
if(xuint16==1) |
| 1567 |
goto photometric_palette; |
| 1568 |
} |
| 1569 |
if(t2p->tiff_samplesperpixel > 3) { |
| 1570 |
if(t2p->tiff_samplesperpixel == 4) { |
| 1571 |
t2p->pdf_colorspace = T2P_CS_RGB; |
| 1572 |
if(TIFFGetField(input, |
| 1573 |
TIFFTAG_EXTRASAMPLES, |
| 1574 |
&xuint16, &xuint16p) |
| 1575 |
&& xuint16 == 1) { |
| 1576 |
if(xuint16p[0] == EXTRASAMPLE_ASSOCALPHA){ |
| 1577 |
if( t2p->tiff_bitspersample != 8 ) |
| 1578 |
{ |
| 1579 |
TIFFError( |
| 1580 |
TIFF2PDF_MODULE, |
| 1581 |
"No support for BitsPerSample=%d for RGBA", |
| 1582 |
t2p->tiff_bitspersample); |
| 1583 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1584 |
return; |
| 1585 |
} |
| 1586 |
t2p->pdf_sample=T2P_SAMPLE_RGBAA_TO_RGB; |
| 1587 |
break; |
| 1588 |
} |
| 1589 |
if(xuint16p[0] == EXTRASAMPLE_UNASSALPHA){ |
| 1590 |
if( t2p->tiff_bitspersample != 8 ) |
| 1591 |
{ |
| 1592 |
TIFFError( |
| 1593 |
TIFF2PDF_MODULE, |
| 1594 |
"No support for BitsPerSample=%d for RGBA", |
| 1595 |
t2p->tiff_bitspersample); |
| 1596 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1597 |
return; |
| 1598 |
} |
| 1599 |
t2p->pdf_sample=T2P_SAMPLE_RGBA_TO_RGB; |
| 1600 |
break; |
| 1601 |
} |
| 1602 |
TIFFWarning( |
| 1603 |
TIFF2PDF_MODULE, |
| 1604 |
"RGB image %s has 4 samples per pixel, assuming RGBA", |
| 1605 |
TIFFFileName(input)); |
| 1606 |
break; |
| 1607 |
} |
| 1608 |
t2p->pdf_colorspace=T2P_CS_CMYK; |
| 1609 |
t2p->pdf_switchdecode ^= 1; |
| 1610 |
TIFFWarning( |
| 1611 |
TIFF2PDF_MODULE, |
| 1612 |
"RGB image %s has 4 samples per pixel, assuming inverse CMYK", |
| 1613 |
TIFFFileName(input)); |
| 1614 |
break; |
| 1615 |
} else { |
| 1616 |
TIFFError( |
| 1617 |
TIFF2PDF_MODULE, |
| 1618 |
"No support for RGB image %s with %u samples per pixel", |
| 1619 |
TIFFFileName(input), |
| 1620 |
t2p->tiff_samplesperpixel); |
| 1621 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1622 |
break; |
| 1623 |
} |
| 1624 |
} else { |
| 1625 |
TIFFError( |
| 1626 |
TIFF2PDF_MODULE, |
| 1627 |
"No support for RGB image %s with %u samples per pixel", |
| 1628 |
TIFFFileName(input), |
| 1629 |
t2p->tiff_samplesperpixel); |
| 1630 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1631 |
break; |
| 1632 |
} |
| 1633 |
case PHOTOMETRIC_PALETTE: |
| 1634 |
photometric_palette: |
| 1635 |
if(t2p->tiff_samplesperpixel!=1){ |
| 1636 |
TIFFError( |
| 1637 |
TIFF2PDF_MODULE, |
| 1638 |
"No support for palettized image %s with not one sample per pixel", |
| 1639 |
TIFFFileName(input)); |
| 1640 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1641 |
return; |
| 1642 |
} |
| 1643 |
t2p->pdf_colorspace=T2P_CS_RGB | T2P_CS_PALETTE; |
| 1644 |
t2p->pdf_palettesize=0x0001<<t2p->tiff_bitspersample; |
| 1645 |
if(!TIFFGetField(input, TIFFTAG_COLORMAP, &r, &g, &b)){ |
| 1646 |
TIFFError( |
| 1647 |
TIFF2PDF_MODULE, |
| 1648 |
"Palettized image %s has no color map", |
| 1649 |
TIFFFileName(input)); |
| 1650 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1651 |
return; |
| 1652 |
} |
| 1653 |
if(t2p->pdf_palette != NULL){ |
| 1654 |
_TIFFfree(t2p->pdf_palette); |
| 1655 |
t2p->pdf_palette=NULL; |
| 1656 |
} |
| 1657 |
t2p->pdf_palette = (unsigned char*) |
| 1658 |
_TIFFmalloc(TIFFSafeMultiply(tmsize_t,t2p->pdf_palettesize,3)); |
| 1659 |
if(t2p->pdf_palette==NULL){ |
| 1660 |
TIFFError( |
| 1661 |
TIFF2PDF_MODULE, |
| 1662 |
"Can't allocate %u bytes of memory for t2p_read_tiff_image, %s", |
| 1663 |
t2p->pdf_palettesize, |
| 1664 |
TIFFFileName(input)); |
| 1665 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1666 |
return; |
| 1667 |
} |
| 1668 |
for(i=0;i<t2p->pdf_palettesize;i++){ |
| 1669 |
t2p->pdf_palette[(i*3)] = (unsigned char) (r[i]>>8); |
| 1670 |
t2p->pdf_palette[(i*3)+1]= (unsigned char) (g[i]>>8); |
| 1671 |
t2p->pdf_palette[(i*3)+2]= (unsigned char) (b[i]>>8); |
| 1672 |
} |
| 1673 |
t2p->pdf_palettesize *= 3; |
| 1674 |
break; |
| 1675 |
case PHOTOMETRIC_SEPARATED: |
| 1676 |
if(TIFFGetField(input, TIFFTAG_INDEXED, &xuint16)){ |
| 1677 |
if(xuint16==1){ |
| 1678 |
goto photometric_palette_cmyk; |
| 1679 |
} |
| 1680 |
} |
| 1681 |
if( TIFFGetField(input, TIFFTAG_INKSET, &xuint16) ){ |
| 1682 |
if(xuint16 != INKSET_CMYK){ |
| 1683 |
TIFFError( |
| 1684 |
TIFF2PDF_MODULE, |
| 1685 |
"No support for %s because its inkset is not CMYK", |
| 1686 |
TIFFFileName(input) ); |
| 1687 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1688 |
return; |
| 1689 |
} |
| 1690 |
} |
| 1691 |
if(t2p->tiff_samplesperpixel==4){ |
| 1692 |
t2p->pdf_colorspace=T2P_CS_CMYK; |
| 1693 |
} else { |
| 1694 |
TIFFError( |
| 1695 |
TIFF2PDF_MODULE, |
| 1696 |
"No support for %s because it has %u samples per pixel", |
| 1697 |
TIFFFileName(input), |
| 1698 |
t2p->tiff_samplesperpixel); |
| 1699 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1700 |
return; |
| 1701 |
} |
| 1702 |
break; |
| 1703 |
photometric_palette_cmyk: |
| 1704 |
if(t2p->tiff_samplesperpixel!=1){ |
| 1705 |
TIFFError( |
| 1706 |
TIFF2PDF_MODULE, |
| 1707 |
"No support for palettized CMYK image %s with not one sample per pixel", |
| 1708 |
TIFFFileName(input)); |
| 1709 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1710 |
return; |
| 1711 |
} |
| 1712 |
t2p->pdf_colorspace=T2P_CS_CMYK | T2P_CS_PALETTE; |
| 1713 |
t2p->pdf_palettesize=0x0001<<t2p->tiff_bitspersample; |
| 1714 |
if(!TIFFGetField(input, TIFFTAG_COLORMAP, &r, &g, &b, &a)){ |
| 1715 |
TIFFError( |
| 1716 |
TIFF2PDF_MODULE, |
| 1717 |
"Palettized image %s has no color map", |
| 1718 |
TIFFFileName(input)); |
| 1719 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1720 |
return; |
| 1721 |
} |
| 1722 |
if(t2p->pdf_palette != NULL){ |
| 1723 |
_TIFFfree(t2p->pdf_palette); |
| 1724 |
t2p->pdf_palette=NULL; |
| 1725 |
} |
| 1726 |
t2p->pdf_palette = (unsigned char*) |
| 1727 |
_TIFFmalloc(TIFFSafeMultiply(tmsize_t,t2p->pdf_palettesize,4)); |
| 1728 |
if(t2p->pdf_palette==NULL){ |
| 1729 |
TIFFError( |
| 1730 |
TIFF2PDF_MODULE, |
| 1731 |
"Can't allocate %u bytes of memory for t2p_read_tiff_image, %s", |
| 1732 |
t2p->pdf_palettesize, |
| 1733 |
TIFFFileName(input)); |
| 1734 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1735 |
return; |
| 1736 |
} |
| 1737 |
for(i=0;i<t2p->pdf_palettesize;i++){ |
| 1738 |
t2p->pdf_palette[(i*4)] = (unsigned char) (r[i]>>8); |
| 1739 |
t2p->pdf_palette[(i*4)+1]= (unsigned char) (g[i]>>8); |
| 1740 |
t2p->pdf_palette[(i*4)+2]= (unsigned char) (b[i]>>8); |
| 1741 |
t2p->pdf_palette[(i*4)+3]= (unsigned char) (a[i]>>8); |
| 1742 |
} |
| 1743 |
t2p->pdf_palettesize *= 4; |
| 1744 |
break; |
| 1745 |
case PHOTOMETRIC_YCBCR: |
| 1746 |
t2p->pdf_colorspace=T2P_CS_RGB; |
| 1747 |
if(t2p->tiff_samplesperpixel==1){ |
| 1748 |
t2p->pdf_colorspace=T2P_CS_GRAY; |
| 1749 |
t2p->tiff_photometric=PHOTOMETRIC_MINISBLACK; |
| 1750 |
break; |
| 1751 |
} |
| 1752 |
t2p->pdf_sample=T2P_SAMPLE_YCBCR_TO_RGB; |
| 1753 |
#ifdef JPEG_SUPPORT |
| 1754 |
if(t2p->pdf_defaultcompression==T2P_COMPRESS_JPEG){ |
| 1755 |
t2p->pdf_sample=T2P_SAMPLE_NOTHING; |
| 1756 |
} |
| 1757 |
#endif |
| 1758 |
break; |
| 1759 |
case PHOTOMETRIC_CIELAB: |
| 1760 |
if( t2p->tiff_samplesperpixel != 3){ |
| 1761 |
TIFFError( |
| 1762 |
TIFF2PDF_MODULE, |
| 1763 |
"Unsupported samplesperpixel = %d for CIELAB", |
| 1764 |
t2p->tiff_samplesperpixel); |
| 1765 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1766 |
return; |
| 1767 |
} |
| 1768 |
if( t2p->tiff_bitspersample != 8){ |
| 1769 |
TIFFError( |
| 1770 |
TIFF2PDF_MODULE, |
| 1771 |
"Invalid bitspersample = %d for CIELAB", |
| 1772 |
t2p->tiff_bitspersample); |
| 1773 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1774 |
return; |
| 1775 |
} |
| 1776 |
t2p->pdf_labrange[0]= -127; |
| 1777 |
t2p->pdf_labrange[1]= 127; |
| 1778 |
t2p->pdf_labrange[2]= -127; |
| 1779 |
t2p->pdf_labrange[3]= 127; |
| 1780 |
t2p->pdf_sample=T2P_SAMPLE_LAB_SIGNED_TO_UNSIGNED; |
| 1781 |
t2p->pdf_colorspace=T2P_CS_LAB; |
| 1782 |
break; |
| 1783 |
case PHOTOMETRIC_ICCLAB: |
| 1784 |
t2p->pdf_labrange[0]= 0; |
| 1785 |
t2p->pdf_labrange[1]= 255; |
| 1786 |
t2p->pdf_labrange[2]= 0; |
| 1787 |
t2p->pdf_labrange[3]= 255; |
| 1788 |
t2p->pdf_colorspace=T2P_CS_LAB; |
| 1789 |
break; |
| 1790 |
case PHOTOMETRIC_ITULAB: |
| 1791 |
if( t2p->tiff_samplesperpixel != 3){ |
| 1792 |
TIFFError( |
| 1793 |
TIFF2PDF_MODULE, |
| 1794 |
"Unsupported samplesperpixel = %d for ITULAB", |
| 1795 |
t2p->tiff_samplesperpixel); |
| 1796 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1797 |
return; |
| 1798 |
} |
| 1799 |
if( t2p->tiff_bitspersample != 8){ |
| 1800 |
TIFFError( |
| 1801 |
TIFF2PDF_MODULE, |
| 1802 |
"Invalid bitspersample = %d for ITULAB", |
| 1803 |
t2p->tiff_bitspersample); |
| 1804 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1805 |
return; |
| 1806 |
} |
| 1807 |
t2p->pdf_labrange[0]=-85; |
| 1808 |
t2p->pdf_labrange[1]=85; |
| 1809 |
t2p->pdf_labrange[2]=-75; |
| 1810 |
t2p->pdf_labrange[3]=124; |
| 1811 |
t2p->pdf_sample=T2P_SAMPLE_LAB_SIGNED_TO_UNSIGNED; |
| 1812 |
t2p->pdf_colorspace=T2P_CS_LAB; |
| 1813 |
break; |
| 1814 |
case PHOTOMETRIC_LOGL: |
| 1815 |
case PHOTOMETRIC_LOGLUV: |
| 1816 |
TIFFError( |
| 1817 |
TIFF2PDF_MODULE, |
| 1818 |
"No support for %s with photometric interpretation LogL/LogLuv", |
| 1819 |
TIFFFileName(input)); |
| 1820 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1821 |
return; |
| 1822 |
default: |
| 1823 |
TIFFError( |
| 1824 |
TIFF2PDF_MODULE, |
| 1825 |
"No support for %s with photometric interpretation %u", |
| 1826 |
TIFFFileName(input), |
| 1827 |
t2p->tiff_photometric); |
| 1828 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1829 |
return; |
| 1830 |
} |
| 1831 |
|
| 1832 |
if(TIFFGetField(input, TIFFTAG_PLANARCONFIG, &(t2p->tiff_planar))){ |
| 1833 |
switch(t2p->tiff_planar){ |
| 1834 |
case 0: |
| 1835 |
TIFFWarning( |
| 1836 |
TIFF2PDF_MODULE, |
| 1837 |
"Image %s has planar configuration 0, assuming 1", |
| 1838 |
TIFFFileName(input)); |
| 1839 |
t2p->tiff_planar=PLANARCONFIG_CONTIG; |
| 1840 |
case PLANARCONFIG_CONTIG: |
| 1841 |
break; |
| 1842 |
case PLANARCONFIG_SEPARATE: |
| 1843 |
t2p->pdf_sample=T2P_SAMPLE_PLANAR_SEPARATE_TO_CONTIG; |
| 1844 |
if(t2p->tiff_bitspersample!=8){ |
| 1845 |
TIFFError( |
| 1846 |
TIFF2PDF_MODULE, |
| 1847 |
"No support for %s with separated planar configuration and %u bits per sample", |
| 1848 |
TIFFFileName(input), |
| 1849 |
t2p->tiff_bitspersample); |
| 1850 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1851 |
return; |
| 1852 |
} |
| 1853 |
break; |
| 1854 |
default: |
| 1855 |
TIFFError( |
| 1856 |
TIFF2PDF_MODULE, |
| 1857 |
"No support for %s with planar configuration %u", |
| 1858 |
TIFFFileName(input), |
| 1859 |
t2p->tiff_planar); |
| 1860 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1861 |
return; |
| 1862 |
} |
| 1863 |
} |
| 1864 |
|
| 1865 |
TIFFGetFieldDefaulted(input, TIFFTAG_ORIENTATION, |
| 1866 |
&(t2p->tiff_orientation)); |
| 1867 |
if(t2p->tiff_orientation>8){ |
| 1868 |
TIFFWarning(TIFF2PDF_MODULE, |
| 1869 |
"Image %s has orientation %u, assuming 0", |
| 1870 |
TIFFFileName(input), t2p->tiff_orientation); |
| 1871 |
t2p->tiff_orientation=0; |
| 1872 |
} |
| 1873 |
|
| 1874 |
if(TIFFGetField(input, TIFFTAG_XRESOLUTION, &(t2p->tiff_xres) ) == 0){ |
| 1875 |
t2p->tiff_xres=0.0; |
| 1876 |
} |
| 1877 |
if(TIFFGetField(input, TIFFTAG_YRESOLUTION, &(t2p->tiff_yres) ) == 0){ |
| 1878 |
t2p->tiff_yres=0.0; |
| 1879 |
} |
| 1880 |
TIFFGetFieldDefaulted(input, TIFFTAG_RESOLUTIONUNIT, |
| 1881 |
&(t2p->tiff_resunit)); |
| 1882 |
if(t2p->tiff_resunit == RESUNIT_CENTIMETER) { |
| 1883 |
t2p->tiff_xres *= 2.54F; |
| 1884 |
t2p->tiff_yres *= 2.54F; |
| 1885 |
} else if (t2p->tiff_resunit != RESUNIT_INCH |
| 1886 |
&& t2p->pdf_centimeters != 0) { |
| 1887 |
t2p->tiff_xres *= 2.54F; |
| 1888 |
t2p->tiff_yres *= 2.54F; |
| 1889 |
} |
| 1890 |
|
| 1891 |
t2p_compose_pdf_page(t2p); |
| 1892 |
if( t2p->t2p_error == T2P_ERR_ERROR ) |
| 1893 |
return; |
| 1894 |
|
| 1895 |
t2p->pdf_transcode = T2P_TRANSCODE_ENCODE; |
| 1896 |
/* It seems that T2P_TRANSCODE_RAW mode doesn't support separate->contig */ |
| 1897 |
/* conversion. At least t2p_read_tiff_size and t2p_read_tiff_size_tile */ |
| 1898 |
/* do not take into account the number of samples, and thus */ |
| 1899 |
/* that can cause heap buffer overflows such as in */ |
| 1900 |
/* http://bugzilla.maptools.org/show_bug.cgi?id=2715 */ |
| 1901 |
if(t2p->pdf_nopassthrough==0 && t2p->tiff_planar!=PLANARCONFIG_SEPARATE){ |
| 1902 |
#ifdef CCITT_SUPPORT |
| 1903 |
if(t2p->tiff_compression==COMPRESSION_CCITTFAX4 |
| 1904 |
){ |
| 1905 |
if(TIFFIsTiled(input) || (TIFFNumberOfStrips(input)==1) ){ |
| 1906 |
t2p->pdf_transcode = T2P_TRANSCODE_RAW; |
| 1907 |
t2p->pdf_compression=T2P_COMPRESS_G4; |
| 1908 |
} |
| 1909 |
} |
| 1910 |
#endif |
| 1911 |
#ifdef ZIP_SUPPORT |
| 1912 |
if(t2p->tiff_compression== COMPRESSION_ADOBE_DEFLATE |
| 1913 |
|| t2p->tiff_compression==COMPRESSION_DEFLATE){ |
| 1914 |
if(TIFFIsTiled(input) || (TIFFNumberOfStrips(input)==1) ){ |
| 1915 |
t2p->pdf_transcode = T2P_TRANSCODE_RAW; |
| 1916 |
t2p->pdf_compression=T2P_COMPRESS_ZIP; |
| 1917 |
} |
| 1918 |
} |
| 1919 |
#endif |
| 1920 |
#ifdef OJPEG_SUPPORT |
| 1921 |
if(t2p->tiff_compression==COMPRESSION_OJPEG){ |
| 1922 |
t2p->pdf_transcode = T2P_TRANSCODE_RAW; |
| 1923 |
t2p->pdf_compression=T2P_COMPRESS_JPEG; |
| 1924 |
t2p_process_ojpeg_tables(t2p, input); |
| 1925 |
} |
| 1926 |
#endif |
| 1927 |
#ifdef JPEG_SUPPORT |
| 1928 |
if(t2p->tiff_compression==COMPRESSION_JPEG){ |
| 1929 |
t2p->pdf_transcode = T2P_TRANSCODE_RAW; |
| 1930 |
t2p->pdf_compression=T2P_COMPRESS_JPEG; |
| 1931 |
} |
| 1932 |
#endif |
| 1933 |
(void)0; |
| 1934 |
} |
| 1935 |
|
| 1936 |
if(t2p->pdf_transcode!=T2P_TRANSCODE_RAW){ |
| 1937 |
t2p->pdf_compression = t2p->pdf_defaultcompression; |
| 1938 |
} |
| 1939 |
|
| 1940 |
#ifdef JPEG_SUPPORT |
| 1941 |
if(t2p->pdf_defaultcompression==T2P_COMPRESS_JPEG){ |
| 1942 |
if(t2p->pdf_colorspace & T2P_CS_PALETTE){ |
| 1943 |
t2p->pdf_sample|=T2P_SAMPLE_REALIZE_PALETTE; |
| 1944 |
t2p->pdf_colorspace ^= T2P_CS_PALETTE; |
| 1945 |
t2p->tiff_pages[t2p->pdf_page].page_extra--; |
| 1946 |
} |
| 1947 |
} |
| 1948 |
if(t2p->tiff_compression==COMPRESSION_JPEG){ |
| 1949 |
if(t2p->tiff_planar==PLANARCONFIG_SEPARATE){ |
| 1950 |
TIFFError( |
| 1951 |
TIFF2PDF_MODULE, |
| 1952 |
"No support for %s with JPEG compression and separated planar configuration", |
| 1953 |
TIFFFileName(input)); |
| 1954 |
t2p->t2p_error=T2P_ERR_ERROR; |
| 1955 |
return; |
| 1956 |
} |
| 1957 |
} |
| 1958 |
#endif |
| 1959 |
#ifdef OJPEG_SUPPORT |
| 1960 |
if(t2p->tiff_compression==COMPRESSION_OJPEG){ |
| 1961 |
if(t2p->tiff_planar==PLANARCONFIG_SEPARATE){ |
| 1962 |
TIFFError( |
| 1963 |
TIFF2PDF_MODULE, |
| 1964 |
"No support for %s with OJPEG compression and separated planar configuration", |
| 1965 |
TIFFFileName(input)); |
| 1966 |
t2p->t2p_error=T2P_ERR_ERROR; |
| 1967 |
return; |
| 1968 |
} |
| 1969 |
} |
| 1970 |
#endif |
| 1971 |
|
| 1972 |
if(t2p->pdf_sample & T2P_SAMPLE_REALIZE_PALETTE){ |
| 1973 |
if(t2p->pdf_colorspace & T2P_CS_CMYK){ |
| 1974 |
t2p->tiff_samplesperpixel=4; |
| 1975 |
t2p->tiff_photometric=PHOTOMETRIC_SEPARATED; |
| 1976 |
} else { |
| 1977 |
t2p->tiff_samplesperpixel=3; |
| 1978 |
t2p->tiff_photometric=PHOTOMETRIC_RGB; |
| 1979 |
} |
| 1980 |
} |
| 1981 |
|
| 1982 |
if (TIFFGetField(input, TIFFTAG_TRANSFERFUNCTION, |
| 1983 |
&(t2p->tiff_transferfunction[0]), |
| 1984 |
&(t2p->tiff_transferfunction[1]), |
| 1985 |
&(t2p->tiff_transferfunction[2]))) { |
| 1986 |
if((t2p->tiff_transferfunction[1] != (float*) NULL) && |
| 1987 |
(t2p->tiff_transferfunction[2] != (float*) NULL) && |
| 1988 |
(t2p->tiff_transferfunction[1] != |
| 1989 |
t2p->tiff_transferfunction[0])) { |
| 1990 |
t2p->tiff_transferfunctioncount=3; |
| 1991 |
} else { |
| 1992 |
t2p->tiff_transferfunctioncount=1; |
| 1993 |
} |
| 1994 |
} else { |
| 1995 |
t2p->tiff_transferfunctioncount=0; |
| 1996 |
} |
| 1997 |
if(TIFFGetField(input, TIFFTAG_WHITEPOINT, &xfloatp)!=0){ |
| 1998 |
t2p->tiff_whitechromaticities[0]=xfloatp[0]; |
| 1999 |
t2p->tiff_whitechromaticities[1]=xfloatp[1]; |
| 2000 |
if(t2p->pdf_colorspace & T2P_CS_GRAY){ |
| 2001 |
t2p->pdf_colorspace |= T2P_CS_CALGRAY; |
| 2002 |
} |
| 2003 |
if(t2p->pdf_colorspace & T2P_CS_RGB){ |
| 2004 |
t2p->pdf_colorspace |= T2P_CS_CALRGB; |
| 2005 |
} |
| 2006 |
} |
| 2007 |
if(TIFFGetField(input, TIFFTAG_PRIMARYCHROMATICITIES, &xfloatp)!=0){ |
| 2008 |
t2p->tiff_primarychromaticities[0]=xfloatp[0]; |
| 2009 |
t2p->tiff_primarychromaticities[1]=xfloatp[1]; |
| 2010 |
t2p->tiff_primarychromaticities[2]=xfloatp[2]; |
| 2011 |
t2p->tiff_primarychromaticities[3]=xfloatp[3]; |
| 2012 |
t2p->tiff_primarychromaticities[4]=xfloatp[4]; |
| 2013 |
t2p->tiff_primarychromaticities[5]=xfloatp[5]; |
| 2014 |
if(t2p->pdf_colorspace & T2P_CS_RGB){ |
| 2015 |
t2p->pdf_colorspace |= T2P_CS_CALRGB; |
| 2016 |
} |
| 2017 |
} |
| 2018 |
if(t2p->pdf_colorspace & T2P_CS_LAB){ |
| 2019 |
if(TIFFGetField(input, TIFFTAG_WHITEPOINT, &xfloatp) != 0){ |
| 2020 |
t2p->tiff_whitechromaticities[0]=xfloatp[0]; |
| 2021 |
t2p->tiff_whitechromaticities[1]=xfloatp[1]; |
| 2022 |
} else { |
| 2023 |
t2p->tiff_whitechromaticities[0]=0.3457F; /* 0.3127F; */ |
| 2024 |
t2p->tiff_whitechromaticities[1]=0.3585F; /* 0.3290F; */ |
| 2025 |
} |
| 2026 |
} |
| 2027 |
if(TIFFGetField(input, |
| 2028 |
TIFFTAG_ICCPROFILE, |
| 2029 |
&(t2p->tiff_iccprofilelength), |
| 2030 |
&(t2p->tiff_iccprofile))!=0){ |
| 2031 |
t2p->pdf_colorspace |= T2P_CS_ICCBASED; |
| 2032 |
} else { |
| 2033 |
t2p->tiff_iccprofilelength=0; |
| 2034 |
t2p->tiff_iccprofile=NULL; |
| 2035 |
} |
| 2036 |
|
| 2037 |
#ifdef CCITT_SUPPORT |
| 2038 |
if( t2p->tiff_bitspersample==1 && |
| 2039 |
t2p->tiff_samplesperpixel==1){ |
| 2040 |
t2p->pdf_compression = T2P_COMPRESS_G4; |
| 2041 |
} |
| 2042 |
#endif |
| 2043 |
|
| 2044 |
|
| 2045 |
return; |
| 2046 |
} |
| 2047 |
|
| 2048 |
/* |
| 2049 |
This function returns the necessary size of a data buffer to contain the raw or |
| 2050 |
uncompressed image data from the input TIFF for a page. |
| 2051 |
*/ |
| 2052 |
|
| 2053 |
static |
| 2054 |
void t2p_read_tiff_size(T2P* t2p, TIFF* input){ |
| 2055 |
|
| 2056 |
uint64* sbc=NULL; |
| 2057 |
#if defined(JPEG_SUPPORT) || defined (OJPEG_SUPPORT) |
| 2058 |
unsigned char* jpt=NULL; |
| 2059 |
tstrip_t i=0; |
| 2060 |
tstrip_t stripcount=0; |
| 2061 |
#endif |
| 2062 |
uint64 k = 0; |
| 2063 |
|
| 2064 |
if(t2p->pdf_transcode == T2P_TRANSCODE_RAW){ |
| 2065 |
#ifdef CCITT_SUPPORT |
| 2066 |
if(t2p->pdf_compression == T2P_COMPRESS_G4 ){ |
| 2067 |
TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc); |
| 2068 |
if (sbc[0] != (uint64)(tmsize_t)sbc[0]) { |
| 2069 |
TIFFError(TIFF2PDF_MODULE, "Integer overflow"); |
| 2070 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2071 |
} |
| 2072 |
t2p->tiff_datasize=(tmsize_t)sbc[0]; |
| 2073 |
return; |
| 2074 |
} |
| 2075 |
#endif |
| 2076 |
#ifdef ZIP_SUPPORT |
| 2077 |
if(t2p->pdf_compression == T2P_COMPRESS_ZIP){ |
| 2078 |
TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc); |
| 2079 |
if (sbc[0] != (uint64)(tmsize_t)sbc[0]) { |
| 2080 |
TIFFError(TIFF2PDF_MODULE, "Integer overflow"); |
| 2081 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2082 |
} |
| 2083 |
t2p->tiff_datasize=(tmsize_t)sbc[0]; |
| 2084 |
return; |
| 2085 |
} |
| 2086 |
#endif |
| 2087 |
#ifdef OJPEG_SUPPORT |
| 2088 |
if(t2p->tiff_compression == COMPRESSION_OJPEG){ |
| 2089 |
if(!TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc)){ |
| 2090 |
TIFFError(TIFF2PDF_MODULE, |
| 2091 |
"Input file %s missing field: TIFFTAG_STRIPBYTECOUNTS", |
| 2092 |
TIFFFileName(input)); |
| 2093 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2094 |
return; |
| 2095 |
} |
| 2096 |
stripcount=TIFFNumberOfStrips(input); |
| 2097 |
for(i=0;i<stripcount;i++){ |
| 2098 |
k = checkAdd64(k, sbc[i], t2p); |
| 2099 |
} |
| 2100 |
if(TIFFGetField(input, TIFFTAG_JPEGIFOFFSET, &(t2p->tiff_dataoffset))){ |
| 2101 |
if(t2p->tiff_dataoffset != 0){ |
| 2102 |
if(TIFFGetField(input, TIFFTAG_JPEGIFBYTECOUNT, &(t2p->tiff_datasize))!=0){ |
| 2103 |
if((uint64)t2p->tiff_datasize < k) { |
| 2104 |
TIFFWarning(TIFF2PDF_MODULE, |
| 2105 |
"Input file %s has short JPEG interchange file byte count", |
| 2106 |
TIFFFileName(input)); |
| 2107 |
t2p->pdf_ojpegiflength=t2p->tiff_datasize; |
| 2108 |
k = checkAdd64(k, t2p->tiff_datasize, t2p); |
| 2109 |
k = checkAdd64(k, 6, t2p); |
| 2110 |
k = checkAdd64(k, stripcount, t2p); |
| 2111 |
k = checkAdd64(k, stripcount, t2p); |
| 2112 |
t2p->tiff_datasize = (tsize_t) k; |
| 2113 |
if ((uint64) t2p->tiff_datasize != k) { |
| 2114 |
TIFFError(TIFF2PDF_MODULE, "Integer overflow"); |
| 2115 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2116 |
} |
| 2117 |
return; |
| 2118 |
} |
| 2119 |
return; |
| 2120 |
}else { |
| 2121 |
TIFFError(TIFF2PDF_MODULE, |
| 2122 |
"Input file %s missing field: TIFFTAG_JPEGIFBYTECOUNT", |
| 2123 |
TIFFFileName(input)); |
| 2124 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2125 |
return; |
| 2126 |
} |
| 2127 |
} |
| 2128 |
} |
| 2129 |
k = checkAdd64(k, stripcount, t2p); |
| 2130 |
k = checkAdd64(k, stripcount, t2p); |
| 2131 |
k = checkAdd64(k, 2048, t2p); |
| 2132 |
t2p->tiff_datasize = (tsize_t) k; |
| 2133 |
if ((uint64) t2p->tiff_datasize != k) { |
| 2134 |
TIFFError(TIFF2PDF_MODULE, "Integer overflow"); |
| 2135 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2136 |
} |
| 2137 |
return; |
| 2138 |
} |
| 2139 |
#endif |
| 2140 |
#ifdef JPEG_SUPPORT |
| 2141 |
if(t2p->tiff_compression == COMPRESSION_JPEG) { |
| 2142 |
uint32 count = 0; |
| 2143 |
if(TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt) != 0 ){ |
| 2144 |
if(count > 4){ |
| 2145 |
k += count; |
| 2146 |
k -= 2; /* don't use EOI of header */ |
| 2147 |
} |
| 2148 |
} else { |
| 2149 |
k = 2; /* SOI for first strip */ |
| 2150 |
} |
| 2151 |
stripcount=TIFFNumberOfStrips(input); |
| 2152 |
if(!TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc)){ |
| 2153 |
TIFFError(TIFF2PDF_MODULE, |
| 2154 |
"Input file %s missing field: TIFFTAG_STRIPBYTECOUNTS", |
| 2155 |
TIFFFileName(input)); |
| 2156 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2157 |
return; |
| 2158 |
} |
| 2159 |
for(i=0;i<stripcount;i++){ |
| 2160 |
k = checkAdd64(k, sbc[i], t2p); |
| 2161 |
k -=2; /* don't use EOI of strip */ |
| 2162 |
k +=2; /* add space for restart marker */ |
| 2163 |
} |
| 2164 |
k = checkAdd64(k, 2, t2p); /* use EOI of last strip */ |
| 2165 |
k = checkAdd64(k, 6, t2p); /* for DRI marker of first strip */ |
| 2166 |
t2p->tiff_datasize = (tsize_t) k; |
| 2167 |
if ((uint64) t2p->tiff_datasize != k) { |
| 2168 |
TIFFError(TIFF2PDF_MODULE, "Integer overflow"); |
| 2169 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2170 |
} |
| 2171 |
return; |
| 2172 |
} |
| 2173 |
#endif |
| 2174 |
(void) 0; |
| 2175 |
} |
| 2176 |
k = checkMultiply64(TIFFScanlineSize(input), t2p->tiff_length, t2p); |
| 2177 |
if(t2p->tiff_planar==PLANARCONFIG_SEPARATE){ |
| 2178 |
k = checkMultiply64(k, t2p->tiff_samplesperpixel, t2p); |
| 2179 |
} |
| 2180 |
if (k == 0) { |
| 2181 |
/* Assume we had overflow inside TIFFScanlineSize */ |
| 2182 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2183 |
} |
| 2184 |
|
| 2185 |
t2p->tiff_datasize = (tsize_t) k; |
| 2186 |
if ((uint64) t2p->tiff_datasize != k) { |
| 2187 |
TIFFError(TIFF2PDF_MODULE, "Integer overflow"); |
| 2188 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2189 |
} |
| 2190 |
|
| 2191 |
return; |
| 2192 |
} |
| 2193 |
|
| 2194 |
/* |
| 2195 |
This function returns the necessary size of a data buffer to contain the raw or |
| 2196 |
uncompressed image data from the input TIFF for a tile of a page. |
| 2197 |
*/ |
| 2198 |
|
| 2199 |
static |
| 2200 |
void t2p_read_tiff_size_tile(T2P* t2p, TIFF* input, ttile_t tile){ |
| 2201 |
|
| 2202 |
uint64* tbc = NULL; |
| 2203 |
uint16 edge=0; |
| 2204 |
#ifdef JPEG_SUPPORT |
| 2205 |
unsigned char* jpt; |
| 2206 |
#endif |
| 2207 |
uint64 k; |
| 2208 |
|
| 2209 |
edge |= t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile); |
| 2210 |
edge |= t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile); |
| 2211 |
|
| 2212 |
if(t2p->pdf_transcode==T2P_TRANSCODE_RAW){ |
| 2213 |
if(edge |
| 2214 |
#if defined(JPEG_SUPPORT) || defined(OJPEG_SUPPORT) |
| 2215 |
&& !(t2p->pdf_compression==T2P_COMPRESS_JPEG) |
| 2216 |
#endif |
| 2217 |
){ |
| 2218 |
t2p->tiff_datasize=TIFFTileSize(input); |
| 2219 |
if (t2p->tiff_datasize == 0) { |
| 2220 |
/* Assume we had overflow inside TIFFTileSize */ |
| 2221 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2222 |
} |
| 2223 |
return; |
| 2224 |
} else { |
| 2225 |
TIFFGetField(input, TIFFTAG_TILEBYTECOUNTS, &tbc); |
| 2226 |
k=tbc[tile]; |
| 2227 |
#ifdef OJPEG_SUPPORT |
| 2228 |
if(t2p->tiff_compression==COMPRESSION_OJPEG){ |
| 2229 |
k = checkAdd64(k, 2048, t2p); |
| 2230 |
} |
| 2231 |
#endif |
| 2232 |
#ifdef JPEG_SUPPORT |
| 2233 |
if(t2p->tiff_compression==COMPRESSION_JPEG) { |
| 2234 |
uint32 count = 0; |
| 2235 |
if(TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt)!=0){ |
| 2236 |
if(count > 4){ |
| 2237 |
k = checkAdd64(k, count, t2p); |
| 2238 |
k -= 2; /* don't use EOI of header or SOI of tile */ |
| 2239 |
} |
| 2240 |
} |
| 2241 |
} |
| 2242 |
#endif |
| 2243 |
t2p->tiff_datasize = (tsize_t) k; |
| 2244 |
if ((uint64) t2p->tiff_datasize != k) { |
| 2245 |
TIFFError(TIFF2PDF_MODULE, "Integer overflow"); |
| 2246 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2247 |
} |
| 2248 |
return; |
| 2249 |
} |
| 2250 |
} |
| 2251 |
k = TIFFTileSize(input); |
| 2252 |
if(t2p->tiff_planar==PLANARCONFIG_SEPARATE){ |
| 2253 |
k = checkMultiply64(k, t2p->tiff_samplesperpixel, t2p); |
| 2254 |
} |
| 2255 |
if (k == 0) { |
| 2256 |
/* Assume we had overflow inside TIFFTileSize */ |
| 2257 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2258 |
} |
| 2259 |
|
| 2260 |
t2p->tiff_datasize = (tsize_t) k; |
| 2261 |
if ((uint64) t2p->tiff_datasize != k) { |
| 2262 |
TIFFError(TIFF2PDF_MODULE, "Integer overflow"); |
| 2263 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2264 |
} |
| 2265 |
|
| 2266 |
return; |
| 2267 |
} |
| 2268 |
|
| 2269 |
/* |
| 2270 |
* This functions returns a non-zero value when the tile is on the right edge |
| 2271 |
* and does not have full imaged tile width. |
| 2272 |
*/ |
| 2273 |
|
| 2274 |
static |
| 2275 |
int t2p_tile_is_right_edge(T2P_TILES tiles, ttile_t tile){ |
| 2276 |
|
| 2277 |
if( ((tile+1) % tiles.tiles_tilecountx == 0) |
| 2278 |
&& (tiles.tiles_edgetilewidth != 0) ){ |
| 2279 |
return(1); |
| 2280 |
} else { |
| 2281 |
return(0); |
| 2282 |
} |
| 2283 |
} |
| 2284 |
|
| 2285 |
/* |
| 2286 |
* This functions returns a non-zero value when the tile is on the bottom edge |
| 2287 |
* and does not have full imaged tile length. |
| 2288 |
*/ |
| 2289 |
|
| 2290 |
static |
| 2291 |
int t2p_tile_is_bottom_edge(T2P_TILES tiles, ttile_t tile){ |
| 2292 |
|
| 2293 |
if( ((tile+1) > (tiles.tiles_tilecount-tiles.tiles_tilecountx) ) |
| 2294 |
&& (tiles.tiles_edgetilelength != 0) ){ |
| 2295 |
return(1); |
| 2296 |
} else { |
| 2297 |
return(0); |
| 2298 |
} |
| 2299 |
} |
| 2300 |
|
| 2301 |
/* |
| 2302 |
This function reads the raster image data from the input TIFF for an image and writes |
| 2303 |
the data to the output PDF XObject image dictionary stream. It returns the amount written |
| 2304 |
or zero on error. |
| 2305 |
*/ |
| 2306 |
|
| 2307 |
static |
| 2308 |
tsize_t t2p_readwrite_pdf_image(T2P* t2p, TIFF* input, TIFF* output){ |
| 2309 |
|
| 2310 |
tsize_t written=0; |
| 2311 |
unsigned char* buffer=NULL; |
| 2312 |
unsigned char* samplebuffer=NULL; |
| 2313 |
tsize_t bufferoffset=0; |
| 2314 |
tsize_t samplebufferoffset=0; |
| 2315 |
tsize_t read=0; |
| 2316 |
tstrip_t i=0; |
| 2317 |
tstrip_t j=0; |
| 2318 |
tstrip_t stripcount=0; |
| 2319 |
tsize_t stripsize=0; |
| 2320 |
tsize_t sepstripcount=0; |
| 2321 |
tsize_t sepstripsize=0; |
| 2322 |
#ifdef OJPEG_SUPPORT |
| 2323 |
toff_t inputoffset=0; |
| 2324 |
uint16 h_samp=1; |
| 2325 |
uint16 v_samp=1; |
| 2326 |
uint16 ri=1; |
| 2327 |
uint32 rows=0; |
| 2328 |
#endif /* ifdef OJPEG_SUPPORT */ |
| 2329 |
#ifdef JPEG_SUPPORT |
| 2330 |
unsigned char* jpt; |
| 2331 |
float* xfloatp; |
| 2332 |
uint64* sbc; |
| 2333 |
unsigned char* stripbuffer; |
| 2334 |
tsize_t striplength=0; |
| 2335 |
uint32 max_striplength=0; |
| 2336 |
#endif /* ifdef JPEG_SUPPORT */ |
| 2337 |
|
| 2338 |
/* Fail if prior error (in particular, can't trust tiff_datasize) */ |
| 2339 |
if (t2p->t2p_error != T2P_ERR_OK) |
| 2340 |
return(0); |
| 2341 |
|
| 2342 |
if(t2p->pdf_transcode == T2P_TRANSCODE_RAW){ |
| 2343 |
#ifdef CCITT_SUPPORT |
| 2344 |
if(t2p->pdf_compression == T2P_COMPRESS_G4){ |
| 2345 |
buffer = (unsigned char*) |
| 2346 |
_TIFFmalloc(t2p->tiff_datasize); |
| 2347 |
if (buffer == NULL) { |
| 2348 |
TIFFError(TIFF2PDF_MODULE, |
| 2349 |
"Can't allocate %lu bytes of memory for " |
| 2350 |
"t2p_readwrite_pdf_image, %s", |
| 2351 |
(unsigned long) t2p->tiff_datasize, |
| 2352 |
TIFFFileName(input)); |
| 2353 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2354 |
return(0); |
| 2355 |
} |
| 2356 |
TIFFReadRawStrip(input, 0, (tdata_t) buffer, |
| 2357 |
t2p->tiff_datasize); |
| 2358 |
if (t2p->tiff_fillorder==FILLORDER_LSB2MSB){ |
| 2359 |
/* |
| 2360 |
* make sure is lsb-to-msb |
| 2361 |
* bit-endianness fill order |
| 2362 |
*/ |
| 2363 |
TIFFReverseBits(buffer, |
| 2364 |
t2p->tiff_datasize); |
| 2365 |
} |
| 2366 |
t2pWriteFile(output, (tdata_t) buffer, |
| 2367 |
t2p->tiff_datasize); |
| 2368 |
_TIFFfree(buffer); |
| 2369 |
return(t2p->tiff_datasize); |
| 2370 |
} |
| 2371 |
#endif /* ifdef CCITT_SUPPORT */ |
| 2372 |
#ifdef ZIP_SUPPORT |
| 2373 |
if (t2p->pdf_compression == T2P_COMPRESS_ZIP) { |
| 2374 |
buffer = (unsigned char*) |
| 2375 |
_TIFFmalloc(t2p->tiff_datasize); |
| 2376 |
if(buffer == NULL){ |
| 2377 |
TIFFError(TIFF2PDF_MODULE, |
| 2378 |
"Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", |
| 2379 |
(unsigned long) t2p->tiff_datasize, |
| 2380 |
TIFFFileName(input)); |
| 2381 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2382 |
return(0); |
| 2383 |
} |
| 2384 |
memset(buffer, 0, t2p->tiff_datasize); |
| 2385 |
TIFFReadRawStrip(input, 0, (tdata_t) buffer, |
| 2386 |
t2p->tiff_datasize); |
| 2387 |
if (t2p->tiff_fillorder==FILLORDER_LSB2MSB) { |
| 2388 |
TIFFReverseBits(buffer, |
| 2389 |
t2p->tiff_datasize); |
| 2390 |
} |
| 2391 |
t2pWriteFile(output, (tdata_t) buffer, |
| 2392 |
t2p->tiff_datasize); |
| 2393 |
_TIFFfree(buffer); |
| 2394 |
return(t2p->tiff_datasize); |
| 2395 |
} |
| 2396 |
#endif /* ifdef ZIP_SUPPORT */ |
| 2397 |
#ifdef OJPEG_SUPPORT |
| 2398 |
if(t2p->tiff_compression == COMPRESSION_OJPEG) { |
| 2399 |
|
| 2400 |
if(t2p->tiff_dataoffset != 0) { |
| 2401 |
buffer = (unsigned char*) |
| 2402 |
_TIFFmalloc(t2p->tiff_datasize); |
| 2403 |
if(buffer == NULL) { |
| 2404 |
TIFFError(TIFF2PDF_MODULE, |
| 2405 |
"Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", |
| 2406 |
(unsigned long) t2p->tiff_datasize, |
| 2407 |
TIFFFileName(input)); |
| 2408 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2409 |
return(0); |
| 2410 |
} |
| 2411 |
memset(buffer, 0, t2p->tiff_datasize); |
| 2412 |
if(t2p->pdf_ojpegiflength==0){ |
| 2413 |
inputoffset=t2pSeekFile(input, 0, |
| 2414 |
SEEK_CUR); |
| 2415 |
t2pSeekFile(input, |
| 2416 |
t2p->tiff_dataoffset, |
| 2417 |
SEEK_SET); |
| 2418 |
t2pReadFile(input, (tdata_t) buffer, |
| 2419 |
t2p->tiff_datasize); |
| 2420 |
t2pSeekFile(input, inputoffset, |
| 2421 |
SEEK_SET); |
| 2422 |
t2pWriteFile(output, (tdata_t) buffer, |
| 2423 |
t2p->tiff_datasize); |
| 2424 |
_TIFFfree(buffer); |
| 2425 |
return(t2p->tiff_datasize); |
| 2426 |
} else { |
| 2427 |
inputoffset=t2pSeekFile(input, 0, |
| 2428 |
SEEK_CUR); |
| 2429 |
t2pSeekFile(input, |
| 2430 |
t2p->tiff_dataoffset, |
| 2431 |
SEEK_SET); |
| 2432 |
bufferoffset = t2pReadFile(input, |
| 2433 |
(tdata_t) buffer, |
| 2434 |
t2p->pdf_ojpegiflength); |
| 2435 |
t2p->pdf_ojpegiflength = 0; |
| 2436 |
t2pSeekFile(input, inputoffset, |
| 2437 |
SEEK_SET); |
| 2438 |
TIFFGetField(input, |
| 2439 |
TIFFTAG_YCBCRSUBSAMPLING, |
| 2440 |
&h_samp, &v_samp); |
| 2441 |
buffer[bufferoffset++]= 0xff; |
| 2442 |
buffer[bufferoffset++]= 0xdd; |
| 2443 |
buffer[bufferoffset++]= 0x00; |
| 2444 |
buffer[bufferoffset++]= 0x04; |
| 2445 |
h_samp*=8; |
| 2446 |
v_samp*=8; |
| 2447 |
ri=(t2p->tiff_width+h_samp-1) / h_samp; |
| 2448 |
TIFFGetField(input, |
| 2449 |
TIFFTAG_ROWSPERSTRIP, |
| 2450 |
&rows); |
| 2451 |
ri*=(rows+v_samp-1)/v_samp; |
| 2452 |
buffer[bufferoffset++]= (ri>>8) & 0xff; |
| 2453 |
buffer[bufferoffset++]= ri & 0xff; |
| 2454 |
stripcount=TIFFNumberOfStrips(input); |
| 2455 |
for(i=0;i<stripcount;i++){ |
| 2456 |
if(i != 0 ){ |
| 2457 |
buffer[bufferoffset++]=0xff; |
| 2458 |
buffer[bufferoffset++]=(0xd0 | ((i-1)%8)); |
| 2459 |
} |
| 2460 |
bufferoffset+=TIFFReadRawStrip(input, |
| 2461 |
i, |
| 2462 |
(tdata_t) &(((unsigned char*)buffer)[bufferoffset]), |
| 2463 |
-1); |
| 2464 |
} |
| 2465 |
t2pWriteFile(output, (tdata_t) buffer, bufferoffset); |
| 2466 |
_TIFFfree(buffer); |
| 2467 |
return(bufferoffset); |
| 2468 |
} |
| 2469 |
} else { |
| 2470 |
if(! t2p->pdf_ojpegdata){ |
| 2471 |
TIFFError(TIFF2PDF_MODULE, |
| 2472 |
"No support for OJPEG image %s with bad tables", |
| 2473 |
TIFFFileName(input)); |
| 2474 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2475 |
return(0); |
| 2476 |
} |
| 2477 |
buffer = (unsigned char*) |
| 2478 |
_TIFFmalloc(t2p->tiff_datasize); |
| 2479 |
if(buffer==NULL){ |
| 2480 |
TIFFError(TIFF2PDF_MODULE, |
| 2481 |
"Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", |
| 2482 |
(unsigned long) t2p->tiff_datasize, |
| 2483 |
TIFFFileName(input)); |
| 2484 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2485 |
return(0); |
| 2486 |
} |
| 2487 |
memset(buffer, 0, t2p->tiff_datasize); |
| 2488 |
_TIFFmemcpy(buffer, t2p->pdf_ojpegdata, t2p->pdf_ojpegdatalength); |
| 2489 |
bufferoffset=t2p->pdf_ojpegdatalength; |
| 2490 |
stripcount=TIFFNumberOfStrips(input); |
| 2491 |
for(i=0;i<stripcount;i++){ |
| 2492 |
if(i != 0){ |
| 2493 |
buffer[bufferoffset++]=0xff; |
| 2494 |
buffer[bufferoffset++]=(0xd0 | ((i-1)%8)); |
| 2495 |
} |
| 2496 |
bufferoffset+=TIFFReadRawStrip(input, |
| 2497 |
i, |
| 2498 |
(tdata_t) &(((unsigned char*)buffer)[bufferoffset]), |
| 2499 |
-1); |
| 2500 |
} |
| 2501 |
if( ! ( (buffer[bufferoffset-1]==0xd9) && (buffer[bufferoffset-2]==0xff) ) ){ |
| 2502 |
buffer[bufferoffset++]=0xff; |
| 2503 |
buffer[bufferoffset++]=0xd9; |
| 2504 |
} |
| 2505 |
t2pWriteFile(output, (tdata_t) buffer, bufferoffset); |
| 2506 |
_TIFFfree(buffer); |
| 2507 |
return(bufferoffset); |
| 2508 |
#if 0 |
| 2509 |
/* |
| 2510 |
This hunk of code removed code is clearly |
| 2511 |
mis-placed and we are not sure where it |
| 2512 |
should be (if anywhere) |
| 2513 |
*/ |
| 2514 |
TIFFError(TIFF2PDF_MODULE, |
| 2515 |
"No support for OJPEG image %s with no JPEG File Interchange offset", |
| 2516 |
TIFFFileName(input)); |
| 2517 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2518 |
return(0); |
| 2519 |
#endif |
| 2520 |
} |
| 2521 |
} |
| 2522 |
#endif /* ifdef OJPEG_SUPPORT */ |
| 2523 |
#ifdef JPEG_SUPPORT |
| 2524 |
if(t2p->tiff_compression == COMPRESSION_JPEG) { |
| 2525 |
uint32 count = 0; |
| 2526 |
buffer = (unsigned char*) |
| 2527 |
_TIFFmalloc(t2p->tiff_datasize); |
| 2528 |
if(buffer==NULL){ |
| 2529 |
TIFFError(TIFF2PDF_MODULE, |
| 2530 |
"Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", |
| 2531 |
(unsigned long) t2p->tiff_datasize, |
| 2532 |
TIFFFileName(input)); |
| 2533 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2534 |
return(0); |
| 2535 |
} |
| 2536 |
memset(buffer, 0, t2p->tiff_datasize); |
| 2537 |
if (TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt) != 0) { |
| 2538 |
if(count > 4) { |
| 2539 |
_TIFFmemcpy(buffer, jpt, count); |
| 2540 |
bufferoffset += count - 2; |
| 2541 |
} |
| 2542 |
} |
| 2543 |
stripcount=TIFFNumberOfStrips(input); |
| 2544 |
TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc); |
| 2545 |
for(i=0;i<stripcount;i++){ |
| 2546 |
if(sbc[i]>max_striplength) max_striplength=sbc[i]; |
| 2547 |
} |
| 2548 |
stripbuffer = (unsigned char*) |
| 2549 |
_TIFFmalloc(max_striplength); |
| 2550 |
if(stripbuffer==NULL){ |
| 2551 |
TIFFError(TIFF2PDF_MODULE, |
| 2552 |
"Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", |
| 2553 |
max_striplength, |
| 2554 |
TIFFFileName(input)); |
| 2555 |
_TIFFfree(buffer); |
| 2556 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2557 |
return(0); |
| 2558 |
} |
| 2559 |
for(i=0;i<stripcount;i++){ |
| 2560 |
striplength=TIFFReadRawStrip(input, i, (tdata_t) stripbuffer, -1); |
| 2561 |
if(!t2p_process_jpeg_strip( |
| 2562 |
stripbuffer, |
| 2563 |
&striplength, |
| 2564 |
buffer, |
| 2565 |
t2p->tiff_datasize, |
| 2566 |
&bufferoffset, |
| 2567 |
i, |
| 2568 |
t2p->tiff_length)){ |
| 2569 |
TIFFError(TIFF2PDF_MODULE, |
| 2570 |
"Can't process JPEG data in input file %s", |
| 2571 |
TIFFFileName(input)); |
| 2572 |
_TIFFfree(samplebuffer); |
| 2573 |
_TIFFfree(buffer); |
| 2574 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2575 |
return(0); |
| 2576 |
} |
| 2577 |
} |
| 2578 |
buffer[bufferoffset++]=0xff; |
| 2579 |
buffer[bufferoffset++]=0xd9; |
| 2580 |
t2pWriteFile(output, (tdata_t) buffer, bufferoffset); |
| 2581 |
_TIFFfree(stripbuffer); |
| 2582 |
_TIFFfree(buffer); |
| 2583 |
return(bufferoffset); |
| 2584 |
} |
| 2585 |
#endif /* ifdef JPEG_SUPPORT */ |
| 2586 |
(void)0; |
| 2587 |
} |
| 2588 |
|
| 2589 |
if(t2p->pdf_sample==T2P_SAMPLE_NOTHING){ |
| 2590 |
buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); |
| 2591 |
if(buffer==NULL){ |
| 2592 |
TIFFError(TIFF2PDF_MODULE, |
| 2593 |
"Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", |
| 2594 |
(unsigned long) t2p->tiff_datasize, |
| 2595 |
TIFFFileName(input)); |
| 2596 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2597 |
return(0); |
| 2598 |
} |
| 2599 |
memset(buffer, 0, t2p->tiff_datasize); |
| 2600 |
stripsize=TIFFStripSize(input); |
| 2601 |
stripcount=TIFFNumberOfStrips(input); |
| 2602 |
for(i=0;i<stripcount;i++){ |
| 2603 |
read = |
| 2604 |
TIFFReadEncodedStrip(input, |
| 2605 |
i, |
| 2606 |
(tdata_t) &buffer[bufferoffset], |
| 2607 |
TIFFmin(stripsize, t2p->tiff_datasize - bufferoffset)); |
| 2608 |
if(read==-1){ |
| 2609 |
TIFFError(TIFF2PDF_MODULE, |
| 2610 |
"Error on decoding strip %u of %s", |
| 2611 |
i, |
| 2612 |
TIFFFileName(input)); |
| 2613 |
_TIFFfree(buffer); |
| 2614 |
t2p->t2p_error=T2P_ERR_ERROR; |
| 2615 |
return(0); |
| 2616 |
} |
| 2617 |
bufferoffset+=read; |
| 2618 |
} |
| 2619 |
} else { |
| 2620 |
if(t2p->pdf_sample & T2P_SAMPLE_PLANAR_SEPARATE_TO_CONTIG){ |
| 2621 |
|
| 2622 |
sepstripsize=TIFFStripSize(input); |
| 2623 |
sepstripcount=TIFFNumberOfStrips(input); |
| 2624 |
|
| 2625 |
stripsize=sepstripsize*t2p->tiff_samplesperpixel; |
| 2626 |
stripcount=sepstripcount/t2p->tiff_samplesperpixel; |
| 2627 |
|
| 2628 |
buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); |
| 2629 |
if(buffer==NULL){ |
| 2630 |
TIFFError(TIFF2PDF_MODULE, |
| 2631 |
"Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", |
| 2632 |
(unsigned long) t2p->tiff_datasize, |
| 2633 |
TIFFFileName(input)); |
| 2634 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2635 |
return(0); |
| 2636 |
} |
| 2637 |
memset(buffer, 0, t2p->tiff_datasize); |
| 2638 |
samplebuffer = (unsigned char*) _TIFFmalloc(stripsize); |
| 2639 |
if(samplebuffer==NULL){ |
| 2640 |
TIFFError(TIFF2PDF_MODULE, |
| 2641 |
"Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", |
| 2642 |
(unsigned long) t2p->tiff_datasize, |
| 2643 |
TIFFFileName(input)); |
| 2644 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2645 |
_TIFFfree(buffer); |
| 2646 |
return(0); |
| 2647 |
} |
| 2648 |
for(i=0;i<stripcount;i++){ |
| 2649 |
samplebufferoffset=0; |
| 2650 |
for(j=0;j<t2p->tiff_samplesperpixel;j++){ |
| 2651 |
read = |
| 2652 |
TIFFReadEncodedStrip(input, |
| 2653 |
i + j*stripcount, |
| 2654 |
(tdata_t) &(samplebuffer[samplebufferoffset]), |
| 2655 |
TIFFmin(sepstripsize, stripsize - samplebufferoffset)); |
| 2656 |
if(read==-1){ |
| 2657 |
TIFFError(TIFF2PDF_MODULE, |
| 2658 |
"Error on decoding strip %u of %s", |
| 2659 |
i + j*stripcount, |
| 2660 |
TIFFFileName(input)); |
| 2661 |
_TIFFfree(buffer); |
| 2662 |
t2p->t2p_error=T2P_ERR_ERROR; |
| 2663 |
return(0); |
| 2664 |
} |
| 2665 |
samplebufferoffset+=read; |
| 2666 |
} |
| 2667 |
t2p_sample_planar_separate_to_contig( |
| 2668 |
t2p, |
| 2669 |
&(buffer[bufferoffset]), |
| 2670 |
samplebuffer, |
| 2671 |
samplebufferoffset); |
| 2672 |
bufferoffset+=samplebufferoffset; |
| 2673 |
} |
| 2674 |
_TIFFfree(samplebuffer); |
| 2675 |
goto dataready; |
| 2676 |
} |
| 2677 |
|
| 2678 |
buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); |
| 2679 |
if(buffer==NULL){ |
| 2680 |
TIFFError(TIFF2PDF_MODULE, |
| 2681 |
"Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", |
| 2682 |
(unsigned long) t2p->tiff_datasize, |
| 2683 |
TIFFFileName(input)); |
| 2684 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2685 |
return(0); |
| 2686 |
} |
| 2687 |
memset(buffer, 0, t2p->tiff_datasize); |
| 2688 |
stripsize=TIFFStripSize(input); |
| 2689 |
stripcount=TIFFNumberOfStrips(input); |
| 2690 |
for(i=0;i<stripcount;i++){ |
| 2691 |
read = |
| 2692 |
TIFFReadEncodedStrip(input, |
| 2693 |
i, |
| 2694 |
(tdata_t) &buffer[bufferoffset], |
| 2695 |
TIFFmin(stripsize, t2p->tiff_datasize - bufferoffset)); |
| 2696 |
if(read==-1){ |
| 2697 |
TIFFError(TIFF2PDF_MODULE, |
| 2698 |
"Error on decoding strip %u of %s", |
| 2699 |
i, |
| 2700 |
TIFFFileName(input)); |
| 2701 |
_TIFFfree(samplebuffer); |
| 2702 |
_TIFFfree(buffer); |
| 2703 |
t2p->t2p_error=T2P_ERR_ERROR; |
| 2704 |
return(0); |
| 2705 |
} |
| 2706 |
bufferoffset+=read; |
| 2707 |
} |
| 2708 |
|
| 2709 |
if(t2p->pdf_sample & T2P_SAMPLE_REALIZE_PALETTE){ |
| 2710 |
// FIXME: overflow? |
| 2711 |
samplebuffer=(unsigned char*)_TIFFrealloc( |
| 2712 |
(tdata_t) buffer, |
| 2713 |
t2p->tiff_datasize * t2p->tiff_samplesperpixel); |
| 2714 |
if(samplebuffer==NULL){ |
| 2715 |
TIFFError(TIFF2PDF_MODULE, |
| 2716 |
"Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", |
| 2717 |
(unsigned long) t2p->tiff_datasize, |
| 2718 |
TIFFFileName(input)); |
| 2719 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2720 |
_TIFFfree(buffer); |
| 2721 |
return(0); |
| 2722 |
} else { |
| 2723 |
buffer=samplebuffer; |
| 2724 |
t2p->tiff_datasize *= t2p->tiff_samplesperpixel; |
| 2725 |
} |
| 2726 |
t2p_sample_realize_palette(t2p, buffer); |
| 2727 |
} |
| 2728 |
|
| 2729 |
if(t2p->pdf_sample & T2P_SAMPLE_RGBA_TO_RGB){ |
| 2730 |
t2p->tiff_datasize=t2p_sample_rgba_to_rgb( |
| 2731 |
(tdata_t)buffer, |
| 2732 |
t2p->tiff_width*t2p->tiff_length); |
| 2733 |
} |
| 2734 |
|
| 2735 |
if(t2p->pdf_sample & T2P_SAMPLE_RGBAA_TO_RGB){ |
| 2736 |
t2p->tiff_datasize=t2p_sample_rgbaa_to_rgb( |
| 2737 |
(tdata_t)buffer, |
| 2738 |
t2p->tiff_width*t2p->tiff_length); |
| 2739 |
} |
| 2740 |
|
| 2741 |
if(t2p->pdf_sample & T2P_SAMPLE_YCBCR_TO_RGB){ |
| 2742 |
samplebuffer=(unsigned char*)_TIFFrealloc( |
| 2743 |
(tdata_t)buffer, |
| 2744 |
t2p->tiff_width*t2p->tiff_length*4); |
| 2745 |
if(samplebuffer==NULL){ |
| 2746 |
TIFFError(TIFF2PDF_MODULE, |
| 2747 |
"Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", |
| 2748 |
(unsigned long) t2p->tiff_datasize, |
| 2749 |
TIFFFileName(input)); |
| 2750 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2751 |
_TIFFfree(buffer); |
| 2752 |
return(0); |
| 2753 |
} else { |
| 2754 |
buffer=samplebuffer; |
| 2755 |
} |
| 2756 |
if(!TIFFReadRGBAImageOriented( |
| 2757 |
input, |
| 2758 |
t2p->tiff_width, |
| 2759 |
t2p->tiff_length, |
| 2760 |
(uint32*)buffer, |
| 2761 |
ORIENTATION_TOPLEFT, |
| 2762 |
0)){ |
| 2763 |
TIFFError(TIFF2PDF_MODULE, |
| 2764 |
"Can't use TIFFReadRGBAImageOriented to extract RGB image from %s", |
| 2765 |
TIFFFileName(input)); |
| 2766 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2767 |
return(0); |
| 2768 |
} |
| 2769 |
t2p->tiff_datasize=t2p_sample_abgr_to_rgb( |
| 2770 |
(tdata_t) buffer, |
| 2771 |
t2p->tiff_width*t2p->tiff_length); |
| 2772 |
|
| 2773 |
} |
| 2774 |
|
| 2775 |
if(t2p->pdf_sample & T2P_SAMPLE_LAB_SIGNED_TO_UNSIGNED){ |
| 2776 |
t2p->tiff_datasize=t2p_sample_lab_signed_to_unsigned( |
| 2777 |
(tdata_t)buffer, |
| 2778 |
t2p->tiff_width*t2p->tiff_length); |
| 2779 |
} |
| 2780 |
} |
| 2781 |
|
| 2782 |
dataready: |
| 2783 |
|
| 2784 |
t2p_disable(output); |
| 2785 |
TIFFSetField(output, TIFFTAG_PHOTOMETRIC, t2p->tiff_photometric); |
| 2786 |
TIFFSetField(output, TIFFTAG_BITSPERSAMPLE, t2p->tiff_bitspersample); |
| 2787 |
TIFFSetField(output, TIFFTAG_SAMPLESPERPIXEL, t2p->tiff_samplesperpixel); |
| 2788 |
TIFFSetField(output, TIFFTAG_IMAGEWIDTH, t2p->tiff_width); |
| 2789 |
TIFFSetField(output, TIFFTAG_IMAGELENGTH, t2p->tiff_length); |
| 2790 |
TIFFSetField(output, TIFFTAG_ROWSPERSTRIP, t2p->tiff_length); |
| 2791 |
TIFFSetField(output, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); |
| 2792 |
TIFFSetField(output, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB); |
| 2793 |
|
| 2794 |
switch(t2p->pdf_compression){ |
| 2795 |
case T2P_COMPRESS_NONE: |
| 2796 |
TIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_NONE); |
| 2797 |
break; |
| 2798 |
#ifdef CCITT_SUPPORT |
| 2799 |
case T2P_COMPRESS_G4: |
| 2800 |
TIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4); |
| 2801 |
break; |
| 2802 |
#endif /* ifdef CCITT_SUPPORT */ |
| 2803 |
#ifdef JPEG_SUPPORT |
| 2804 |
case T2P_COMPRESS_JPEG: |
| 2805 |
if(t2p->tiff_photometric==PHOTOMETRIC_YCBCR) { |
| 2806 |
uint16 hor = 0, ver = 0; |
| 2807 |
if (TIFFGetField(input, TIFFTAG_YCBCRSUBSAMPLING, &hor, &ver) !=0 ) { |
| 2808 |
if(hor != 0 && ver != 0){ |
| 2809 |
TIFFSetField(output, TIFFTAG_YCBCRSUBSAMPLING, hor, ver); |
| 2810 |
} |
| 2811 |
} |
| 2812 |
if(TIFFGetField(input, TIFFTAG_REFERENCEBLACKWHITE, &xfloatp)!=0){ |
| 2813 |
TIFFSetField(output, TIFFTAG_REFERENCEBLACKWHITE, xfloatp); |
| 2814 |
} |
| 2815 |
} |
| 2816 |
if(TIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_JPEG)==0){ |
| 2817 |
TIFFError(TIFF2PDF_MODULE, |
| 2818 |
"Unable to use JPEG compression for input %s and output %s", |
| 2819 |
TIFFFileName(input), |
| 2820 |
TIFFFileName(output)); |
| 2821 |
_TIFFfree(buffer); |
| 2822 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2823 |
return(0); |
| 2824 |
} |
| 2825 |
TIFFSetField(output, TIFFTAG_JPEGTABLESMODE, 0); |
| 2826 |
|
| 2827 |
if(t2p->pdf_colorspace & (T2P_CS_RGB | T2P_CS_LAB)){ |
| 2828 |
TIFFSetField(output, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR); |
| 2829 |
if(t2p->tiff_photometric != PHOTOMETRIC_YCBCR){ |
| 2830 |
TIFFSetField(output, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB); |
| 2831 |
} else { |
| 2832 |
TIFFSetField(output, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RAW); |
| 2833 |
} |
| 2834 |
} |
| 2835 |
if(t2p->pdf_colorspace & T2P_CS_GRAY){ |
| 2836 |
(void)0; |
| 2837 |
} |
| 2838 |
if(t2p->pdf_colorspace & T2P_CS_CMYK){ |
| 2839 |
(void)0; |
| 2840 |
} |
| 2841 |
if(t2p->pdf_defaultcompressionquality != 0){ |
| 2842 |
TIFFSetField(output, |
| 2843 |
TIFFTAG_JPEGQUALITY, |
| 2844 |
t2p->pdf_defaultcompressionquality); |
| 2845 |
} |
| 2846 |
|
| 2847 |
break; |
| 2848 |
#endif /* ifdef JPEG_SUPPORT */ |
| 2849 |
#ifdef ZIP_SUPPORT |
| 2850 |
case T2P_COMPRESS_ZIP: |
| 2851 |
TIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE); |
| 2852 |
if(t2p->pdf_defaultcompressionquality%100 != 0){ |
| 2853 |
TIFFSetField(output, |
| 2854 |
TIFFTAG_PREDICTOR, |
| 2855 |
t2p->pdf_defaultcompressionquality % 100); |
| 2856 |
} |
| 2857 |
if(t2p->pdf_defaultcompressionquality/100 != 0){ |
| 2858 |
TIFFSetField(output, |
| 2859 |
TIFFTAG_ZIPQUALITY, |
| 2860 |
(t2p->pdf_defaultcompressionquality / 100)); |
| 2861 |
} |
| 2862 |
break; |
| 2863 |
#endif /* ifdef ZIP_SUPPORT */ |
| 2864 |
default: |
| 2865 |
break; |
| 2866 |
} |
| 2867 |
|
| 2868 |
t2p_enable(output); |
| 2869 |
t2p->outputwritten = 0; |
| 2870 |
#ifdef JPEG_SUPPORT |
| 2871 |
if(t2p->pdf_compression == T2P_COMPRESS_JPEG |
| 2872 |
&& t2p->tiff_photometric == PHOTOMETRIC_YCBCR){ |
| 2873 |
bufferoffset = TIFFWriteEncodedStrip(output, (tstrip_t)0, |
| 2874 |
buffer, |
| 2875 |
stripsize * stripcount); |
| 2876 |
} else |
| 2877 |
#endif /* ifdef JPEG_SUPPORT */ |
| 2878 |
{ |
| 2879 |
bufferoffset = TIFFWriteEncodedStrip(output, (tstrip_t)0, |
| 2880 |
buffer, |
| 2881 |
t2p->tiff_datasize); |
| 2882 |
} |
| 2883 |
if (buffer != NULL) { |
| 2884 |
_TIFFfree(buffer); |
| 2885 |
buffer=NULL; |
| 2886 |
} |
| 2887 |
|
| 2888 |
if (bufferoffset == (tsize_t)-1) { |
| 2889 |
TIFFError(TIFF2PDF_MODULE, |
| 2890 |
"Error writing encoded strip to output PDF %s", |
| 2891 |
TIFFFileName(output)); |
| 2892 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2893 |
return(0); |
| 2894 |
} |
| 2895 |
|
| 2896 |
written = t2p->outputwritten; |
| 2897 |
return(written); |
| 2898 |
} |
| 2899 |
|
| 2900 |
/* |
| 2901 |
* This function reads the raster image data from the input TIFF for an image |
| 2902 |
* tile and writes the data to the output PDF XObject image dictionary stream |
| 2903 |
* for the tile. It returns the amount written or zero on error. |
| 2904 |
*/ |
| 2905 |
|
| 2906 |
static |
| 2907 |
tsize_t t2p_readwrite_pdf_image_tile(T2P* t2p, TIFF* input, TIFF* output, ttile_t tile){ |
| 2908 |
|
| 2909 |
uint16 edge=0; |
| 2910 |
tsize_t written=0; |
| 2911 |
unsigned char* buffer=NULL; |
| 2912 |
tsize_t bufferoffset=0; |
| 2913 |
unsigned char* samplebuffer=NULL; |
| 2914 |
tsize_t samplebufferoffset=0; |
| 2915 |
tsize_t read=0; |
| 2916 |
uint16 i=0; |
| 2917 |
ttile_t tilecount=0; |
| 2918 |
/* tsize_t tilesize=0; */ |
| 2919 |
ttile_t septilecount=0; |
| 2920 |
tsize_t septilesize=0; |
| 2921 |
#ifdef JPEG_SUPPORT |
| 2922 |
unsigned char* jpt; |
| 2923 |
float* xfloatp; |
| 2924 |
uint32 xuint32=0; |
| 2925 |
#endif |
| 2926 |
|
| 2927 |
/* Fail if prior error (in particular, can't trust tiff_datasize) */ |
| 2928 |
if (t2p->t2p_error != T2P_ERR_OK) |
| 2929 |
return(0); |
| 2930 |
|
| 2931 |
edge |= t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile); |
| 2932 |
edge |= t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile); |
| 2933 |
|
| 2934 |
if( (t2p->pdf_transcode == T2P_TRANSCODE_RAW) && ((edge == 0) |
| 2935 |
#if defined(JPEG_SUPPORT) || defined(OJPEG_SUPPORT) |
| 2936 |
|| (t2p->pdf_compression == T2P_COMPRESS_JPEG) |
| 2937 |
#endif |
| 2938 |
) |
| 2939 |
){ |
| 2940 |
#ifdef CCITT_SUPPORT |
| 2941 |
if(t2p->pdf_compression == T2P_COMPRESS_G4){ |
| 2942 |
buffer= (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); |
| 2943 |
if(buffer==NULL){ |
| 2944 |
TIFFError(TIFF2PDF_MODULE, |
| 2945 |
"Can't allocate %lu bytes of memory " |
| 2946 |
"for t2p_readwrite_pdf_image_tile, %s", |
| 2947 |
(unsigned long) t2p->tiff_datasize, |
| 2948 |
TIFFFileName(input)); |
| 2949 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2950 |
return(0); |
| 2951 |
} |
| 2952 |
TIFFReadRawTile(input, tile, (tdata_t) buffer, t2p->tiff_datasize); |
| 2953 |
if (t2p->tiff_fillorder==FILLORDER_LSB2MSB){ |
| 2954 |
TIFFReverseBits(buffer, t2p->tiff_datasize); |
| 2955 |
} |
| 2956 |
t2pWriteFile(output, (tdata_t) buffer, t2p->tiff_datasize); |
| 2957 |
_TIFFfree(buffer); |
| 2958 |
return(t2p->tiff_datasize); |
| 2959 |
} |
| 2960 |
#endif |
| 2961 |
#ifdef ZIP_SUPPORT |
| 2962 |
if(t2p->pdf_compression == T2P_COMPRESS_ZIP){ |
| 2963 |
buffer= (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); |
| 2964 |
if(buffer==NULL){ |
| 2965 |
TIFFError(TIFF2PDF_MODULE, |
| 2966 |
"Can't allocate %lu bytes of memory " |
| 2967 |
"for t2p_readwrite_pdf_image_tile, %s", |
| 2968 |
(unsigned long) t2p->tiff_datasize, |
| 2969 |
TIFFFileName(input)); |
| 2970 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2971 |
return(0); |
| 2972 |
} |
| 2973 |
TIFFReadRawTile(input, tile, (tdata_t) buffer, t2p->tiff_datasize); |
| 2974 |
if (t2p->tiff_fillorder==FILLORDER_LSB2MSB){ |
| 2975 |
TIFFReverseBits(buffer, t2p->tiff_datasize); |
| 2976 |
} |
| 2977 |
t2pWriteFile(output, (tdata_t) buffer, t2p->tiff_datasize); |
| 2978 |
_TIFFfree(buffer); |
| 2979 |
return(t2p->tiff_datasize); |
| 2980 |
} |
| 2981 |
#endif |
| 2982 |
#ifdef OJPEG_SUPPORT |
| 2983 |
if(t2p->tiff_compression == COMPRESSION_OJPEG){ |
| 2984 |
if(! t2p->pdf_ojpegdata){ |
| 2985 |
TIFFError(TIFF2PDF_MODULE, |
| 2986 |
"No support for OJPEG image %s with " |
| 2987 |
"bad tables", |
| 2988 |
TIFFFileName(input)); |
| 2989 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2990 |
return(0); |
| 2991 |
} |
| 2992 |
buffer=(unsigned char*) _TIFFmalloc(t2p->tiff_datasize); |
| 2993 |
if(buffer==NULL){ |
| 2994 |
TIFFError(TIFF2PDF_MODULE, |
| 2995 |
"Can't allocate %lu bytes of memory " |
| 2996 |
"for t2p_readwrite_pdf_image, %s", |
| 2997 |
(unsigned long) t2p->tiff_datasize, |
| 2998 |
TIFFFileName(input)); |
| 2999 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3000 |
return(0); |
| 3001 |
} |
| 3002 |
_TIFFmemcpy(buffer, t2p->pdf_ojpegdata, t2p->pdf_ojpegdatalength); |
| 3003 |
if(edge!=0){ |
| 3004 |
if(t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile)){ |
| 3005 |
buffer[7]= |
| 3006 |
(t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilelength >> 8) & 0xff; |
| 3007 |
buffer[8]= |
| 3008 |
(t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilelength ) & 0xff; |
| 3009 |
} |
| 3010 |
if(t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile)){ |
| 3011 |
buffer[9]= |
| 3012 |
(t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilewidth >> 8) & 0xff; |
| 3013 |
buffer[10]= |
| 3014 |
(t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilewidth ) & 0xff; |
| 3015 |
} |
| 3016 |
} |
| 3017 |
bufferoffset=t2p->pdf_ojpegdatalength; |
| 3018 |
bufferoffset+=TIFFReadRawTile(input, |
| 3019 |
tile, |
| 3020 |
(tdata_t) &(((unsigned char*)buffer)[bufferoffset]), |
| 3021 |
-1); |
| 3022 |
((unsigned char*)buffer)[bufferoffset++]=0xff; |
| 3023 |
((unsigned char*)buffer)[bufferoffset++]=0xd9; |
| 3024 |
t2pWriteFile(output, (tdata_t) buffer, bufferoffset); |
| 3025 |
_TIFFfree(buffer); |
| 3026 |
return(bufferoffset); |
| 3027 |
} |
| 3028 |
#endif |
| 3029 |
#ifdef JPEG_SUPPORT |
| 3030 |
if(t2p->tiff_compression == COMPRESSION_JPEG){ |
| 3031 |
unsigned char table_end[2]; |
| 3032 |
uint32 count = 0; |
| 3033 |
buffer= (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); |
| 3034 |
if(buffer==NULL){ |
| 3035 |
TIFFError(TIFF2PDF_MODULE, |
| 3036 |
"Can't allocate " TIFF_SIZE_FORMAT " bytes of memory " |
| 3037 |
"for t2p_readwrite_pdf_image_tile, %s", |
| 3038 |
(TIFF_SIZE_T) t2p->tiff_datasize, |
| 3039 |
TIFFFileName(input)); |
| 3040 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3041 |
return(0); |
| 3042 |
} |
| 3043 |
if(TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt) != 0) { |
| 3044 |
if (count > 4) { |
| 3045 |
int retTIFFReadRawTile; |
| 3046 |
/* Ignore EOI marker of JpegTables */ |
| 3047 |
_TIFFmemcpy(buffer, jpt, count - 2); |
| 3048 |
bufferoffset += count - 2; |
| 3049 |
/* Store last 2 bytes of the JpegTables */ |
| 3050 |
table_end[0] = buffer[bufferoffset-2]; |
| 3051 |
table_end[1] = buffer[bufferoffset-1]; |
| 3052 |
xuint32 = bufferoffset; |
| 3053 |
bufferoffset -= 2; |
| 3054 |
retTIFFReadRawTile= TIFFReadRawTile( |
| 3055 |
input, |
| 3056 |
tile, |
| 3057 |
(tdata_t) &(((unsigned char*)buffer)[bufferoffset]), |
| 3058 |
-1); |
| 3059 |
if( retTIFFReadRawTile < 0 ) |
| 3060 |
{ |
| 3061 |
_TIFFfree(buffer); |
| 3062 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3063 |
return(0); |
| 3064 |
} |
| 3065 |
bufferoffset += retTIFFReadRawTile; |
| 3066 |
/* Overwrite SOI marker of image scan with previously */ |
| 3067 |
/* saved end of JpegTables */ |
| 3068 |
buffer[xuint32-2]=table_end[0]; |
| 3069 |
buffer[xuint32-1]=table_end[1]; |
| 3070 |
} |
| 3071 |
} |
| 3072 |
t2pWriteFile(output, (tdata_t) buffer, bufferoffset); |
| 3073 |
_TIFFfree(buffer); |
| 3074 |
return(bufferoffset); |
| 3075 |
} |
| 3076 |
#endif |
| 3077 |
(void)0; |
| 3078 |
} |
| 3079 |
|
| 3080 |
if(t2p->pdf_sample==T2P_SAMPLE_NOTHING){ |
| 3081 |
buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); |
| 3082 |
if(buffer==NULL){ |
| 3083 |
TIFFError(TIFF2PDF_MODULE, |
| 3084 |
"Can't allocate %lu bytes of memory for " |
| 3085 |
"t2p_readwrite_pdf_image_tile, %s", |
| 3086 |
(unsigned long) t2p->tiff_datasize, |
| 3087 |
TIFFFileName(input)); |
| 3088 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3089 |
return(0); |
| 3090 |
} |
| 3091 |
|
| 3092 |
read = TIFFReadEncodedTile( |
| 3093 |
input, |
| 3094 |
tile, |
| 3095 |
(tdata_t) &buffer[bufferoffset], |
| 3096 |
t2p->tiff_datasize); |
| 3097 |
if(read==-1){ |
| 3098 |
TIFFError(TIFF2PDF_MODULE, |
| 3099 |
"Error on decoding tile %u of %s", |
| 3100 |
tile, |
| 3101 |
TIFFFileName(input)); |
| 3102 |
_TIFFfree(buffer); |
| 3103 |
t2p->t2p_error=T2P_ERR_ERROR; |
| 3104 |
return(0); |
| 3105 |
} |
| 3106 |
|
| 3107 |
} else { |
| 3108 |
|
| 3109 |
if(t2p->pdf_sample == T2P_SAMPLE_PLANAR_SEPARATE_TO_CONTIG){ |
| 3110 |
septilesize=TIFFTileSize(input); |
| 3111 |
septilecount=TIFFNumberOfTiles(input); |
| 3112 |
/* tilesize=septilesize*t2p->tiff_samplesperpixel; */ |
| 3113 |
tilecount=septilecount/t2p->tiff_samplesperpixel; |
| 3114 |
buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); |
| 3115 |
if(buffer==NULL){ |
| 3116 |
TIFFError(TIFF2PDF_MODULE, |
| 3117 |
"Can't allocate %lu bytes of memory " |
| 3118 |
"for t2p_readwrite_pdf_image_tile, %s", |
| 3119 |
(unsigned long) t2p->tiff_datasize, |
| 3120 |
TIFFFileName(input)); |
| 3121 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3122 |
return(0); |
| 3123 |
} |
| 3124 |
samplebuffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); |
| 3125 |
if(samplebuffer==NULL){ |
| 3126 |
TIFFError(TIFF2PDF_MODULE, |
| 3127 |
"Can't allocate %lu bytes of memory " |
| 3128 |
"for t2p_readwrite_pdf_image_tile, %s", |
| 3129 |
(unsigned long) t2p->tiff_datasize, |
| 3130 |
TIFFFileName(input)); |
| 3131 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3132 |
return(0); |
| 3133 |
} |
| 3134 |
samplebufferoffset=0; |
| 3135 |
for(i=0;i<t2p->tiff_samplesperpixel;i++){ |
| 3136 |
read = |
| 3137 |
TIFFReadEncodedTile(input, |
| 3138 |
tile + i*tilecount, |
| 3139 |
(tdata_t) &(samplebuffer[samplebufferoffset]), |
| 3140 |
septilesize); |
| 3141 |
if(read==-1){ |
| 3142 |
TIFFError(TIFF2PDF_MODULE, |
| 3143 |
"Error on decoding tile %u of %s", |
| 3144 |
tile + i*tilecount, |
| 3145 |
TIFFFileName(input)); |
| 3146 |
_TIFFfree(samplebuffer); |
| 3147 |
_TIFFfree(buffer); |
| 3148 |
t2p->t2p_error=T2P_ERR_ERROR; |
| 3149 |
return(0); |
| 3150 |
} |
| 3151 |
samplebufferoffset+=read; |
| 3152 |
} |
| 3153 |
t2p_sample_planar_separate_to_contig( |
| 3154 |
t2p, |
| 3155 |
&(buffer[bufferoffset]), |
| 3156 |
samplebuffer, |
| 3157 |
samplebufferoffset); |
| 3158 |
bufferoffset+=samplebufferoffset; |
| 3159 |
_TIFFfree(samplebuffer); |
| 3160 |
} |
| 3161 |
|
| 3162 |
if(buffer==NULL){ |
| 3163 |
buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); |
| 3164 |
if(buffer==NULL){ |
| 3165 |
TIFFError(TIFF2PDF_MODULE, |
| 3166 |
"Can't allocate %lu bytes of memory " |
| 3167 |
"for t2p_readwrite_pdf_image_tile, %s", |
| 3168 |
(unsigned long) t2p->tiff_datasize, |
| 3169 |
TIFFFileName(input)); |
| 3170 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3171 |
return(0); |
| 3172 |
} |
| 3173 |
read = TIFFReadEncodedTile( |
| 3174 |
input, |
| 3175 |
tile, |
| 3176 |
(tdata_t) &buffer[bufferoffset], |
| 3177 |
t2p->tiff_datasize); |
| 3178 |
if(read==-1){ |
| 3179 |
TIFFError(TIFF2PDF_MODULE, |
| 3180 |
"Error on decoding tile %u of %s", |
| 3181 |
tile, |
| 3182 |
TIFFFileName(input)); |
| 3183 |
_TIFFfree(buffer); |
| 3184 |
t2p->t2p_error=T2P_ERR_ERROR; |
| 3185 |
return(0); |
| 3186 |
} |
| 3187 |
} |
| 3188 |
|
| 3189 |
if(t2p->pdf_sample & T2P_SAMPLE_RGBA_TO_RGB){ |
| 3190 |
t2p->tiff_datasize=t2p_sample_rgba_to_rgb( |
| 3191 |
(tdata_t)buffer, |
| 3192 |
t2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth |
| 3193 |
*t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength); |
| 3194 |
} |
| 3195 |
|
| 3196 |
if(t2p->pdf_sample & T2P_SAMPLE_RGBAA_TO_RGB){ |
| 3197 |
t2p->tiff_datasize=t2p_sample_rgbaa_to_rgb( |
| 3198 |
(tdata_t)buffer, |
| 3199 |
t2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth |
| 3200 |
*t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength); |
| 3201 |
} |
| 3202 |
|
| 3203 |
if(t2p->pdf_sample & T2P_SAMPLE_YCBCR_TO_RGB){ |
| 3204 |
TIFFError(TIFF2PDF_MODULE, |
| 3205 |
"No support for YCbCr to RGB in tile for %s", |
| 3206 |
TIFFFileName(input)); |
| 3207 |
_TIFFfree(buffer); |
| 3208 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3209 |
return(0); |
| 3210 |
} |
| 3211 |
|
| 3212 |
if(t2p->pdf_sample & T2P_SAMPLE_LAB_SIGNED_TO_UNSIGNED){ |
| 3213 |
t2p->tiff_datasize=t2p_sample_lab_signed_to_unsigned( |
| 3214 |
(tdata_t)buffer, |
| 3215 |
t2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth |
| 3216 |
*t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength); |
| 3217 |
} |
| 3218 |
} |
| 3219 |
|
| 3220 |
if(t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile) != 0){ |
| 3221 |
t2p_tile_collapse_left( |
| 3222 |
buffer, |
| 3223 |
TIFFTileRowSize(input), |
| 3224 |
t2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth, |
| 3225 |
t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilewidth, |
| 3226 |
t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength); |
| 3227 |
} |
| 3228 |
|
| 3229 |
|
| 3230 |
t2p_disable(output); |
| 3231 |
TIFFSetField(output, TIFFTAG_PHOTOMETRIC, t2p->tiff_photometric); |
| 3232 |
TIFFSetField(output, TIFFTAG_BITSPERSAMPLE, t2p->tiff_bitspersample); |
| 3233 |
TIFFSetField(output, TIFFTAG_SAMPLESPERPIXEL, t2p->tiff_samplesperpixel); |
| 3234 |
if(t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile) == 0){ |
| 3235 |
TIFFSetField( |
| 3236 |
output, |
| 3237 |
TIFFTAG_IMAGEWIDTH, |
| 3238 |
t2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth); |
| 3239 |
} else { |
| 3240 |
TIFFSetField( |
| 3241 |
output, |
| 3242 |
TIFFTAG_IMAGEWIDTH, |
| 3243 |
t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilewidth); |
| 3244 |
} |
| 3245 |
if(t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile) == 0){ |
| 3246 |
TIFFSetField( |
| 3247 |
output, |
| 3248 |
TIFFTAG_IMAGELENGTH, |
| 3249 |
t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength); |
| 3250 |
TIFFSetField( |
| 3251 |
output, |
| 3252 |
TIFFTAG_ROWSPERSTRIP, |
| 3253 |
t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength); |
| 3254 |
} else { |
| 3255 |
TIFFSetField( |
| 3256 |
output, |
| 3257 |
TIFFTAG_IMAGELENGTH, |
| 3258 |
t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilelength); |
| 3259 |
TIFFSetField( |
| 3260 |
output, |
| 3261 |
TIFFTAG_ROWSPERSTRIP, |
| 3262 |
t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilelength); |
| 3263 |
} |
| 3264 |
TIFFSetField(output, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); |
| 3265 |
TIFFSetField(output, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB); |
| 3266 |
|
| 3267 |
switch(t2p->pdf_compression){ |
| 3268 |
case T2P_COMPRESS_NONE: |
| 3269 |
TIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_NONE); |
| 3270 |
break; |
| 3271 |
#ifdef CCITT_SUPPORT |
| 3272 |
case T2P_COMPRESS_G4: |
| 3273 |
TIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4); |
| 3274 |
break; |
| 3275 |
#endif |
| 3276 |
#ifdef JPEG_SUPPORT |
| 3277 |
case T2P_COMPRESS_JPEG: |
| 3278 |
if (t2p->tiff_photometric==PHOTOMETRIC_YCBCR) { |
| 3279 |
uint16 hor = 0, ver = 0; |
| 3280 |
if (TIFFGetField(input, TIFFTAG_YCBCRSUBSAMPLING, &hor, &ver)!=0) { |
| 3281 |
if (hor != 0 && ver != 0) { |
| 3282 |
TIFFSetField(output, TIFFTAG_YCBCRSUBSAMPLING, hor, ver); |
| 3283 |
} |
| 3284 |
} |
| 3285 |
if(TIFFGetField(input, TIFFTAG_REFERENCEBLACKWHITE, &xfloatp)!=0){ |
| 3286 |
TIFFSetField(output, TIFFTAG_REFERENCEBLACKWHITE, xfloatp); |
| 3287 |
} |
| 3288 |
} |
| 3289 |
TIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_JPEG); |
| 3290 |
TIFFSetField(output, TIFFTAG_JPEGTABLESMODE, 0); /* JPEGTABLESMODE_NONE */ |
| 3291 |
if(t2p->pdf_colorspace & (T2P_CS_RGB | T2P_CS_LAB)){ |
| 3292 |
TIFFSetField(output, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR); |
| 3293 |
if(t2p->tiff_photometric != PHOTOMETRIC_YCBCR){ |
| 3294 |
TIFFSetField(output, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB); |
| 3295 |
} else { |
| 3296 |
TIFFSetField(output, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RAW); |
| 3297 |
} |
| 3298 |
} |
| 3299 |
if(t2p->pdf_colorspace & T2P_CS_GRAY){ |
| 3300 |
(void)0; |
| 3301 |
} |
| 3302 |
if(t2p->pdf_colorspace & T2P_CS_CMYK){ |
| 3303 |
(void)0; |
| 3304 |
} |
| 3305 |
if(t2p->pdf_defaultcompressionquality != 0){ |
| 3306 |
TIFFSetField(output, |
| 3307 |
TIFFTAG_JPEGQUALITY, |
| 3308 |
t2p->pdf_defaultcompressionquality); |
| 3309 |
} |
| 3310 |
break; |
| 3311 |
#endif |
| 3312 |
#ifdef ZIP_SUPPORT |
| 3313 |
case T2P_COMPRESS_ZIP: |
| 3314 |
TIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE); |
| 3315 |
if(t2p->pdf_defaultcompressionquality%100 != 0){ |
| 3316 |
TIFFSetField(output, |
| 3317 |
TIFFTAG_PREDICTOR, |
| 3318 |
t2p->pdf_defaultcompressionquality % 100); |
| 3319 |
} |
| 3320 |
if(t2p->pdf_defaultcompressionquality/100 != 0){ |
| 3321 |
TIFFSetField(output, |
| 3322 |
TIFFTAG_ZIPQUALITY, |
| 3323 |
(t2p->pdf_defaultcompressionquality / 100)); |
| 3324 |
} |
| 3325 |
break; |
| 3326 |
#endif |
| 3327 |
default: |
| 3328 |
break; |
| 3329 |
} |
| 3330 |
|
| 3331 |
t2p_enable(output); |
| 3332 |
t2p->outputwritten = 0; |
| 3333 |
bufferoffset = TIFFWriteEncodedStrip(output, (tstrip_t) 0, buffer, |
| 3334 |
TIFFStripSize(output)); |
| 3335 |
if (buffer != NULL) { |
| 3336 |
_TIFFfree(buffer); |
| 3337 |
buffer = NULL; |
| 3338 |
} |
| 3339 |
if (bufferoffset == -1) { |
| 3340 |
TIFFError(TIFF2PDF_MODULE, |
| 3341 |
"Error writing encoded tile to output PDF %s", |
| 3342 |
TIFFFileName(output)); |
| 3343 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3344 |
return(0); |
| 3345 |
} |
| 3346 |
|
| 3347 |
written = t2p->outputwritten; |
| 3348 |
|
| 3349 |
return(written); |
| 3350 |
} |
| 3351 |
|
| 3352 |
#ifdef OJPEG_SUPPORT |
| 3353 |
static |
| 3354 |
int t2p_process_ojpeg_tables(T2P* t2p, TIFF* input){ |
| 3355 |
uint16 proc=0; |
| 3356 |
void* q; |
| 3357 |
uint32 q_length=0; |
| 3358 |
void* dc; |
| 3359 |
uint32 dc_length=0; |
| 3360 |
void* ac; |
| 3361 |
uint32 ac_length=0; |
| 3362 |
uint16* lp; |
| 3363 |
uint16* pt; |
| 3364 |
uint16 h_samp=1; |
| 3365 |
uint16 v_samp=1; |
| 3366 |
unsigned char* ojpegdata; |
| 3367 |
uint16 table_count; |
| 3368 |
uint32 offset_table; |
| 3369 |
uint32 offset_ms_l; |
| 3370 |
uint32 code_count; |
| 3371 |
uint32 i=0; |
| 3372 |
uint32 dest=0; |
| 3373 |
uint16 ri=0; |
| 3374 |
uint32 rows=0; |
| 3375 |
|
| 3376 |
if(!TIFFGetField(input, TIFFTAG_JPEGPROC, &proc)){ |
| 3377 |
TIFFError(TIFF2PDF_MODULE, |
| 3378 |
"Missing JPEGProc field in OJPEG image %s", |
| 3379 |
TIFFFileName(input)); |
| 3380 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3381 |
return(0); |
| 3382 |
} |
| 3383 |
if(proc!=JPEGPROC_BASELINE && proc!=JPEGPROC_LOSSLESS){ |
| 3384 |
TIFFError(TIFF2PDF_MODULE, |
| 3385 |
"Bad JPEGProc field in OJPEG image %s", |
| 3386 |
TIFFFileName(input)); |
| 3387 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3388 |
return(0); |
| 3389 |
} |
| 3390 |
if(!TIFFGetField(input, TIFFTAG_JPEGQTABLES, &q_length, &q)){ |
| 3391 |
TIFFError(TIFF2PDF_MODULE, |
| 3392 |
"Missing JPEGQTables field in OJPEG image %s", |
| 3393 |
TIFFFileName(input)); |
| 3394 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3395 |
return(0); |
| 3396 |
} |
| 3397 |
if(q_length < (64U * t2p->tiff_samplesperpixel)){ |
| 3398 |
TIFFError(TIFF2PDF_MODULE, |
| 3399 |
"Bad JPEGQTables field in OJPEG image %s", |
| 3400 |
TIFFFileName(input)); |
| 3401 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3402 |
return(0); |
| 3403 |
} |
| 3404 |
if(!TIFFGetField(input, TIFFTAG_JPEGDCTABLES, &dc_length, &dc)){ |
| 3405 |
TIFFError(TIFF2PDF_MODULE, |
| 3406 |
"Missing JPEGDCTables field in OJPEG image %s", |
| 3407 |
TIFFFileName(input)); |
| 3408 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3409 |
return(0); |
| 3410 |
} |
| 3411 |
if(proc==JPEGPROC_BASELINE){ |
| 3412 |
if(!TIFFGetField(input, TIFFTAG_JPEGACTABLES, &ac_length, &ac)){ |
| 3413 |
TIFFError(TIFF2PDF_MODULE, |
| 3414 |
"Missing JPEGACTables field in OJPEG image %s", |
| 3415 |
TIFFFileName(input)); |
| 3416 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3417 |
return(0); |
| 3418 |
} |
| 3419 |
} else { |
| 3420 |
if(!TIFFGetField(input, TIFFTAG_JPEGLOSSLESSPREDICTORS, &lp)){ |
| 3421 |
TIFFError(TIFF2PDF_MODULE, |
| 3422 |
"Missing JPEGLosslessPredictors field in OJPEG image %s", |
| 3423 |
TIFFFileName(input)); |
| 3424 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3425 |
return(0); |
| 3426 |
} |
| 3427 |
if(!TIFFGetField(input, TIFFTAG_JPEGPOINTTRANSFORM, &pt)){ |
| 3428 |
TIFFError(TIFF2PDF_MODULE, |
| 3429 |
"Missing JPEGPointTransform field in OJPEG image %s", |
| 3430 |
TIFFFileName(input)); |
| 3431 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3432 |
return(0); |
| 3433 |
} |
| 3434 |
} |
| 3435 |
if(!TIFFGetField(input, TIFFTAG_YCBCRSUBSAMPLING, &h_samp, &v_samp)){ |
| 3436 |
h_samp=1; |
| 3437 |
v_samp=1; |
| 3438 |
} |
| 3439 |
if(t2p->pdf_ojpegdata != NULL){ |
| 3440 |
_TIFFfree(t2p->pdf_ojpegdata); |
| 3441 |
t2p->pdf_ojpegdata=NULL; |
| 3442 |
} |
| 3443 |
t2p->pdf_ojpegdata = _TIFFmalloc(2048); |
| 3444 |
if(t2p->pdf_ojpegdata == NULL){ |
| 3445 |
TIFFError(TIFF2PDF_MODULE, |
| 3446 |
"Can't allocate %u bytes of memory for t2p_process_ojpeg_tables, %s", |
| 3447 |
2048, |
| 3448 |
TIFFFileName(input)); |
| 3449 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3450 |
return(0); |
| 3451 |
} |
| 3452 |
_TIFFmemset(t2p->pdf_ojpegdata, 0x00, 2048); |
| 3453 |
t2p->pdf_ojpegdatalength = 0; |
| 3454 |
table_count=t2p->tiff_samplesperpixel; |
| 3455 |
if(proc==JPEGPROC_BASELINE){ |
| 3456 |
if(table_count>2) table_count=2; |
| 3457 |
} |
| 3458 |
ojpegdata=(unsigned char*)t2p->pdf_ojpegdata; |
| 3459 |
ojpegdata[t2p->pdf_ojpegdatalength++]=0xff; |
| 3460 |
ojpegdata[t2p->pdf_ojpegdatalength++]=0xd8; |
| 3461 |
ojpegdata[t2p->pdf_ojpegdatalength++]=0xff; |
| 3462 |
if(proc==JPEGPROC_BASELINE){ |
| 3463 |
ojpegdata[t2p->pdf_ojpegdatalength++]=0xc0; |
| 3464 |
} else { |
| 3465 |
ojpegdata[t2p->pdf_ojpegdatalength++]=0xc3; |
| 3466 |
} |
| 3467 |
ojpegdata[t2p->pdf_ojpegdatalength++]=0x00; |
| 3468 |
ojpegdata[t2p->pdf_ojpegdatalength++]=(8 + 3*t2p->tiff_samplesperpixel); |
| 3469 |
ojpegdata[t2p->pdf_ojpegdatalength++]=(t2p->tiff_bitspersample & 0xff); |
| 3470 |
if(TIFFIsTiled(input)){ |
| 3471 |
ojpegdata[t2p->pdf_ojpegdatalength++]= |
| 3472 |
(t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength >> 8) & 0xff; |
| 3473 |
ojpegdata[t2p->pdf_ojpegdatalength++]= |
| 3474 |
(t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength ) & 0xff; |
| 3475 |
ojpegdata[t2p->pdf_ojpegdatalength++]= |
| 3476 |
(t2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth >> 8) & 0xff; |
| 3477 |
ojpegdata[t2p->pdf_ojpegdatalength++]= |
| 3478 |
(t2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth ) & 0xff; |
| 3479 |
} else { |
| 3480 |
ojpegdata[t2p->pdf_ojpegdatalength++]= |
| 3481 |
(t2p->tiff_length >> 8) & 0xff; |
| 3482 |
ojpegdata[t2p->pdf_ojpegdatalength++]= |
| 3483 |
(t2p->tiff_length ) & 0xff; |
| 3484 |
ojpegdata[t2p->pdf_ojpegdatalength++]= |
| 3485 |
(t2p->tiff_width >> 8) & 0xff; |
| 3486 |
ojpegdata[t2p->pdf_ojpegdatalength++]= |
| 3487 |
(t2p->tiff_width ) & 0xff; |
| 3488 |
} |
| 3489 |
ojpegdata[t2p->pdf_ojpegdatalength++]=(t2p->tiff_samplesperpixel & 0xff); |
| 3490 |
for(i=0;i<t2p->tiff_samplesperpixel;i++){ |
| 3491 |
ojpegdata[t2p->pdf_ojpegdatalength++]=i; |
| 3492 |
if(i==0){ |
| 3493 |
ojpegdata[t2p->pdf_ojpegdatalength] |= h_samp<<4 & 0xf0;; |
| 3494 |
ojpegdata[t2p->pdf_ojpegdatalength++] |= v_samp & 0x0f; |
| 3495 |
} else { |
| 3496 |
ojpegdata[t2p->pdf_ojpegdatalength++]= 0x11; |
| 3497 |
} |
| 3498 |
ojpegdata[t2p->pdf_ojpegdatalength++]=i; |
| 3499 |
} |
| 3500 |
for(dest=0;dest<t2p->tiff_samplesperpixel;dest++){ |
| 3501 |
ojpegdata[t2p->pdf_ojpegdatalength++]=0xff; |
| 3502 |
ojpegdata[t2p->pdf_ojpegdatalength++]=0xdb; |
| 3503 |
ojpegdata[t2p->pdf_ojpegdatalength++]=0x00; |
| 3504 |
ojpegdata[t2p->pdf_ojpegdatalength++]=0x43; |
| 3505 |
ojpegdata[t2p->pdf_ojpegdatalength++]=dest; |
| 3506 |
_TIFFmemcpy( &(ojpegdata[t2p->pdf_ojpegdatalength++]), |
| 3507 |
&(((unsigned char*)q)[64*dest]), 64); |
| 3508 |
t2p->pdf_ojpegdatalength+=64; |
| 3509 |
} |
| 3510 |
offset_table=0; |
| 3511 |
for(dest=0;dest<table_count;dest++){ |
| 3512 |
ojpegdata[t2p->pdf_ojpegdatalength++]=0xff; |
| 3513 |
ojpegdata[t2p->pdf_ojpegdatalength++]=0xc4; |
| 3514 |
offset_ms_l=t2p->pdf_ojpegdatalength; |
| 3515 |
t2p->pdf_ojpegdatalength+=2; |
| 3516 |
ojpegdata[t2p->pdf_ojpegdatalength++]=dest & 0x0f; |
| 3517 |
_TIFFmemcpy( &(ojpegdata[t2p->pdf_ojpegdatalength]), |
| 3518 |
&(((unsigned char*)dc)[offset_table]), 16); |
| 3519 |
code_count=0; |
| 3520 |
offset_table+=16; |
| 3521 |
for(i=0;i<16;i++){ |
| 3522 |
code_count+=ojpegdata[t2p->pdf_ojpegdatalength++]; |
| 3523 |
} |
| 3524 |
ojpegdata[offset_ms_l]=((19+code_count)>>8) & 0xff; |
| 3525 |
ojpegdata[offset_ms_l+1]=(19+code_count) & 0xff; |
| 3526 |
_TIFFmemcpy( &(ojpegdata[t2p->pdf_ojpegdatalength]), |
| 3527 |
&(((unsigned char*)dc)[offset_table]), code_count); |
| 3528 |
offset_table+=code_count; |
| 3529 |
t2p->pdf_ojpegdatalength+=code_count; |
| 3530 |
} |
| 3531 |
if(proc==JPEGPROC_BASELINE){ |
| 3532 |
offset_table=0; |
| 3533 |
for(dest=0;dest<table_count;dest++){ |
| 3534 |
ojpegdata[t2p->pdf_ojpegdatalength++]=0xff; |
| 3535 |
ojpegdata[t2p->pdf_ojpegdatalength++]=0xc4; |
| 3536 |
offset_ms_l=t2p->pdf_ojpegdatalength; |
| 3537 |
t2p->pdf_ojpegdatalength+=2; |
| 3538 |
ojpegdata[t2p->pdf_ojpegdatalength] |= 0x10; |
| 3539 |
ojpegdata[t2p->pdf_ojpegdatalength++] |=dest & 0x0f; |
| 3540 |
_TIFFmemcpy( &(ojpegdata[t2p->pdf_ojpegdatalength]), |
| 3541 |
&(((unsigned char*)ac)[offset_table]), 16); |
| 3542 |
code_count=0; |
| 3543 |
offset_table+=16; |
| 3544 |
for(i=0;i<16;i++){ |
| 3545 |
code_count+=ojpegdata[t2p->pdf_ojpegdatalength++]; |
| 3546 |
} |
| 3547 |
ojpegdata[offset_ms_l]=((19+code_count)>>8) & 0xff; |
| 3548 |
ojpegdata[offset_ms_l+1]=(19+code_count) & 0xff; |
| 3549 |
_TIFFmemcpy( &(ojpegdata[t2p->pdf_ojpegdatalength]), |
| 3550 |
&(((unsigned char*)ac)[offset_table]), code_count); |
| 3551 |
offset_table+=code_count; |
| 3552 |
t2p->pdf_ojpegdatalength+=code_count; |
| 3553 |
} |
| 3554 |
} |
| 3555 |
if(TIFFNumberOfStrips(input)>1){ |
| 3556 |
ojpegdata[t2p->pdf_ojpegdatalength++]=0xff; |
| 3557 |
ojpegdata[t2p->pdf_ojpegdatalength++]=0xdd; |
| 3558 |
ojpegdata[t2p->pdf_ojpegdatalength++]=0x00; |
| 3559 |
ojpegdata[t2p->pdf_ojpegdatalength++]=0x04; |
| 3560 |
h_samp*=8; |
| 3561 |
v_samp*=8; |
| 3562 |
ri=(t2p->tiff_width+h_samp-1) / h_samp; |
| 3563 |
TIFFGetField(input, TIFFTAG_ROWSPERSTRIP, &rows); |
| 3564 |
ri*=(rows+v_samp-1)/v_samp; |
| 3565 |
ojpegdata[t2p->pdf_ojpegdatalength++]= (ri>>8) & 0xff; |
| 3566 |
ojpegdata[t2p->pdf_ojpegdatalength++]= ri & 0xff; |
| 3567 |
} |
| 3568 |
ojpegdata[t2p->pdf_ojpegdatalength++]=0xff; |
| 3569 |
ojpegdata[t2p->pdf_ojpegdatalength++]=0xda; |
| 3570 |
ojpegdata[t2p->pdf_ojpegdatalength++]=0x00; |
| 3571 |
ojpegdata[t2p->pdf_ojpegdatalength++]=(6 + 2*t2p->tiff_samplesperpixel); |
| 3572 |
ojpegdata[t2p->pdf_ojpegdatalength++]=t2p->tiff_samplesperpixel & 0xff; |
| 3573 |
for(i=0;i<t2p->tiff_samplesperpixel;i++){ |
| 3574 |
ojpegdata[t2p->pdf_ojpegdatalength++]= i & 0xff; |
| 3575 |
if(proc==JPEGPROC_BASELINE){ |
| 3576 |
ojpegdata[t2p->pdf_ojpegdatalength] |= |
| 3577 |
( ( (i>(table_count-1U)) ? (table_count-1U) : i) << 4U) & 0xf0; |
| 3578 |
ojpegdata[t2p->pdf_ojpegdatalength++] |= |
| 3579 |
( (i>(table_count-1U)) ? (table_count-1U) : i) & 0x0f; |
| 3580 |
} else { |
| 3581 |
ojpegdata[t2p->pdf_ojpegdatalength++] = (i << 4) & 0xf0; |
| 3582 |
} |
| 3583 |
} |
| 3584 |
if(proc==JPEGPROC_BASELINE){ |
| 3585 |
t2p->pdf_ojpegdatalength++; |
| 3586 |
ojpegdata[t2p->pdf_ojpegdatalength++]=0x3f; |
| 3587 |
t2p->pdf_ojpegdatalength++; |
| 3588 |
} else { |
| 3589 |
ojpegdata[t2p->pdf_ojpegdatalength++]= (lp[0] & 0xff); |
| 3590 |
t2p->pdf_ojpegdatalength++; |
| 3591 |
ojpegdata[t2p->pdf_ojpegdatalength++]= (pt[0] & 0x0f); |
| 3592 |
} |
| 3593 |
|
| 3594 |
return(1); |
| 3595 |
} |
| 3596 |
#endif |
| 3597 |
|
| 3598 |
#ifdef JPEG_SUPPORT |
| 3599 |
static |
| 3600 |
int t2p_process_jpeg_strip( |
| 3601 |
unsigned char* strip, |
| 3602 |
tsize_t* striplength, |
| 3603 |
unsigned char* buffer, |
| 3604 |
tsize_t buffersize, |
| 3605 |
tsize_t* bufferoffset, |
| 3606 |
tstrip_t no, |
| 3607 |
uint32 height){ |
| 3608 |
|
| 3609 |
tsize_t i=0; |
| 3610 |
|
| 3611 |
while (i < *striplength) { |
| 3612 |
tsize_t datalen; |
| 3613 |
uint16 ri; |
| 3614 |
uint16 v_samp; |
| 3615 |
uint16 h_samp; |
| 3616 |
int j; |
| 3617 |
int ncomp; |
| 3618 |
|
| 3619 |
/* marker header: one or more FFs */ |
| 3620 |
if (strip[i] != 0xff) |
| 3621 |
return(0); |
| 3622 |
i++; |
| 3623 |
while (i < *striplength && strip[i] == 0xff) |
| 3624 |
i++; |
| 3625 |
if (i >= *striplength) |
| 3626 |
return(0); |
| 3627 |
/* SOI is the only pre-SOS marker without a length word */ |
| 3628 |
if (strip[i] == 0xd8) |
| 3629 |
datalen = 0; |
| 3630 |
else { |
| 3631 |
if ((*striplength - i) <= 2) |
| 3632 |
return(0); |
| 3633 |
datalen = (strip[i+1] << 8) | strip[i+2]; |
| 3634 |
if (datalen < 2 || datalen >= (*striplength - i)) |
| 3635 |
return(0); |
| 3636 |
} |
| 3637 |
switch( strip[i] ){ |
| 3638 |
case 0xd8: /* SOI - start of image */ |
| 3639 |
if( *bufferoffset + 2 > buffersize ) |
| 3640 |
return(0); |
| 3641 |
if ( *bufferoffset == 0) { /* LB: do not repeat SOI marker */ |
| 3642 |
_TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i-1]), 2); |
| 3643 |
*bufferoffset+=2; |
| 3644 |
} |
| 3645 |
break; |
| 3646 |
case 0xc0: /* SOF0 */ |
| 3647 |
case 0xc1: /* SOF1 */ |
| 3648 |
case 0xc3: /* SOF3 */ |
| 3649 |
case 0xc9: /* SOF9 */ |
| 3650 |
case 0xca: /* SOF10 */ |
| 3651 |
if(no==0){ |
| 3652 |
if( *bufferoffset + datalen + 2 + 6 > buffersize ) |
| 3653 |
return(0); |
| 3654 |
_TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i-1]), datalen+2); |
| 3655 |
if( *bufferoffset + 9 >= buffersize ) |
| 3656 |
return(0); |
| 3657 |
ncomp = buffer[*bufferoffset+9]; |
| 3658 |
if (ncomp < 1 || ncomp > 4) |
| 3659 |
return(0); |
| 3660 |
v_samp=1; |
| 3661 |
h_samp=1; |
| 3662 |
if( *bufferoffset + 11 + 3*(ncomp-1) >= buffersize ) |
| 3663 |
return(0); |
| 3664 |
for(j=0;j<ncomp;j++){ |
| 3665 |
uint16 samp = buffer[*bufferoffset+11+(3*j)]; |
| 3666 |
if( (samp>>4) > h_samp) |
| 3667 |
h_samp = (samp>>4); |
| 3668 |
if( (samp & 0x0f) > v_samp) |
| 3669 |
v_samp = (samp & 0x0f); |
| 3670 |
} |
| 3671 |
v_samp*=8; |
| 3672 |
h_samp*=8; |
| 3673 |
ri=((( ((uint16)(buffer[*bufferoffset+5])<<8) | |
| 3674 |
(uint16)(buffer[*bufferoffset+6]) )+v_samp-1)/ |
| 3675 |
v_samp); |
| 3676 |
ri*=((( ((uint16)(buffer[*bufferoffset+7])<<8) | |
| 3677 |
(uint16)(buffer[*bufferoffset+8]) )+h_samp-1)/ |
| 3678 |
h_samp); |
| 3679 |
buffer[*bufferoffset+5]= |
| 3680 |
(unsigned char) ((height>>8) & 0xff); |
| 3681 |
buffer[*bufferoffset+6]= |
| 3682 |
(unsigned char) (height & 0xff); |
| 3683 |
*bufferoffset+=datalen+2; |
| 3684 |
/* insert a DRI marker */ |
| 3685 |
buffer[(*bufferoffset)++]=0xff; |
| 3686 |
buffer[(*bufferoffset)++]=0xdd; |
| 3687 |
buffer[(*bufferoffset)++]=0x00; |
| 3688 |
buffer[(*bufferoffset)++]=0x04; |
| 3689 |
buffer[(*bufferoffset)++]=(ri >> 8) & 0xff; |
| 3690 |
buffer[(*bufferoffset)++]= ri & 0xff; |
| 3691 |
} |
| 3692 |
break; |
| 3693 |
case 0xc4: /* DHT */ |
| 3694 |
case 0xdb: /* DQT */ |
| 3695 |
if( *bufferoffset + datalen + 2 > buffersize ) |
| 3696 |
return(0); |
| 3697 |
_TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i-1]), datalen+2); |
| 3698 |
*bufferoffset+=datalen+2; |
| 3699 |
break; |
| 3700 |
case 0xda: /* SOS */ |
| 3701 |
if(no==0){ |
| 3702 |
if( *bufferoffset + datalen + 2 > buffersize ) |
| 3703 |
return(0); |
| 3704 |
_TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i-1]), datalen+2); |
| 3705 |
*bufferoffset+=datalen+2; |
| 3706 |
} else { |
| 3707 |
if( *bufferoffset + 2 > buffersize ) |
| 3708 |
return(0); |
| 3709 |
buffer[(*bufferoffset)++]=0xff; |
| 3710 |
buffer[(*bufferoffset)++]= |
| 3711 |
(unsigned char)(0xd0 | ((no-1)%8)); |
| 3712 |
} |
| 3713 |
i += datalen + 1; |
| 3714 |
/* copy remainder of strip */ |
| 3715 |
if( *bufferoffset + *striplength - i > buffersize ) |
| 3716 |
return(0); |
| 3717 |
_TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i]), *striplength - i); |
| 3718 |
*bufferoffset+= *striplength - i; |
| 3719 |
return(1); |
| 3720 |
default: |
| 3721 |
/* ignore any other marker */ |
| 3722 |
break; |
| 3723 |
} |
| 3724 |
i += datalen + 1; |
| 3725 |
} |
| 3726 |
|
| 3727 |
/* failed to find SOS marker */ |
| 3728 |
return(0); |
| 3729 |
} |
| 3730 |
#endif |
| 3731 |
|
| 3732 |
/* |
| 3733 |
This functions converts a tilewidth x tilelength buffer of samples into an edgetilewidth x |
| 3734 |
tilelength buffer of samples. |
| 3735 |
*/ |
| 3736 |
static |
| 3737 |
void t2p_tile_collapse_left( |
| 3738 |
tdata_t buffer, |
| 3739 |
tsize_t scanwidth, |
| 3740 |
uint32 tilewidth, |
| 3741 |
uint32 edgetilewidth, |
| 3742 |
uint32 tilelength){ |
| 3743 |
|
| 3744 |
uint32 i; |
| 3745 |
tsize_t edgescanwidth=0; |
| 3746 |
|
| 3747 |
edgescanwidth = (scanwidth * edgetilewidth + (tilewidth - 1))/ tilewidth; |
| 3748 |
for(i=0;i<tilelength;i++){ |
| 3749 |
/* We use memmove() since there can be overlaps in src and dst buffers for the first items */ |
| 3750 |
memmove( |
| 3751 |
&(((char*)buffer)[edgescanwidth*i]), |
| 3752 |
&(((char*)buffer)[scanwidth*i]), |
| 3753 |
edgescanwidth); |
| 3754 |
} |
| 3755 |
|
| 3756 |
return; |
| 3757 |
} |
| 3758 |
|
| 3759 |
|
| 3760 |
/* |
| 3761 |
* This function calls TIFFWriteDirectory on the output after blanking its |
| 3762 |
* output by replacing the read, write, and seek procedures with empty |
| 3763 |
* implementations, then it replaces the original implementations. |
| 3764 |
*/ |
| 3765 |
|
| 3766 |
static |
| 3767 |
void |
| 3768 |
t2p_write_advance_directory(T2P* t2p, TIFF* output) |
| 3769 |
{ |
| 3770 |
t2p_disable(output); |
| 3771 |
if(!TIFFWriteDirectory(output)){ |
| 3772 |
TIFFError(TIFF2PDF_MODULE, |
| 3773 |
"Error writing virtual directory to output PDF %s", |
| 3774 |
TIFFFileName(output)); |
| 3775 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3776 |
return; |
| 3777 |
} |
| 3778 |
t2p_enable(output); |
| 3779 |
return; |
| 3780 |
} |
| 3781 |
|
| 3782 |
static |
| 3783 |
tsize_t t2p_sample_planar_separate_to_contig( |
| 3784 |
T2P* t2p, |
| 3785 |
unsigned char* buffer, |
| 3786 |
unsigned char* samplebuffer, |
| 3787 |
tsize_t samplebuffersize){ |
| 3788 |
|
| 3789 |
tsize_t stride=0; |
| 3790 |
tsize_t i=0; |
| 3791 |
tsize_t j=0; |
| 3792 |
|
| 3793 |
stride=samplebuffersize/t2p->tiff_samplesperpixel; |
| 3794 |
for(i=0;i<stride;i++){ |
| 3795 |
for(j=0;j<t2p->tiff_samplesperpixel;j++){ |
| 3796 |
buffer[i*t2p->tiff_samplesperpixel + j] = samplebuffer[i + j*stride]; |
| 3797 |
} |
| 3798 |
} |
| 3799 |
|
| 3800 |
return(samplebuffersize); |
| 3801 |
} |
| 3802 |
|
| 3803 |
static |
| 3804 |
tsize_t t2p_sample_realize_palette(T2P* t2p, unsigned char* buffer){ |
| 3805 |
|
| 3806 |
uint32 sample_count=0; |
| 3807 |
uint16 component_count=0; |
| 3808 |
uint32 palette_offset=0; |
| 3809 |
uint32 sample_offset=0; |
| 3810 |
uint32 i=0; |
| 3811 |
uint32 j=0; |
| 3812 |
size_t data_size; |
| 3813 |
sample_count=t2p->tiff_width*t2p->tiff_length; |
| 3814 |
component_count=t2p->tiff_samplesperpixel; |
| 3815 |
data_size=TIFFSafeMultiply(size_t,sample_count,component_count); |
| 3816 |
if( (data_size == 0U) || (t2p->tiff_datasize < 0) || |
| 3817 |
(data_size > (size_t) t2p->tiff_datasize) ) |
| 3818 |
{ |
| 3819 |
TIFFError(TIFF2PDF_MODULE, |
| 3820 |
"Error: sample_count * component_count > t2p->tiff_datasize"); |
| 3821 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3822 |
return 1; |
| 3823 |
} |
| 3824 |
|
| 3825 |
for(i=sample_count;i>0;i--){ |
| 3826 |
palette_offset=buffer[i-1] * component_count; |
| 3827 |
sample_offset= (i-1) * component_count; |
| 3828 |
for(j=0;j<component_count;j++){ |
| 3829 |
buffer[sample_offset+j]=t2p->pdf_palette[palette_offset+j]; |
| 3830 |
} |
| 3831 |
} |
| 3832 |
|
| 3833 |
return(0); |
| 3834 |
} |
| 3835 |
|
| 3836 |
/* |
| 3837 |
This functions converts in place a buffer of ABGR interleaved data |
| 3838 |
into RGB interleaved data, discarding A. |
| 3839 |
*/ |
| 3840 |
|
| 3841 |
static |
| 3842 |
tsize_t t2p_sample_abgr_to_rgb(tdata_t data, uint32 samplecount) |
| 3843 |
{ |
| 3844 |
uint32 i=0; |
| 3845 |
uint32 sample=0; |
| 3846 |
|
| 3847 |
for(i=0;i<samplecount;i++){ |
| 3848 |
sample=((uint32*)data)[i]; |
| 3849 |
((char*)data)[i*3]= (char) (sample & 0xff); |
| 3850 |
((char*)data)[i*3+1]= (char) ((sample>>8) & 0xff); |
| 3851 |
((char*)data)[i*3+2]= (char) ((sample>>16) & 0xff); |
| 3852 |
} |
| 3853 |
|
| 3854 |
return(i*3); |
| 3855 |
} |
| 3856 |
|
| 3857 |
/* |
| 3858 |
* This functions converts in place a buffer of RGBA interleaved data |
| 3859 |
* into RGB interleaved data, discarding A. |
| 3860 |
*/ |
| 3861 |
|
| 3862 |
static |
| 3863 |
tsize_t |
| 3864 |
t2p_sample_rgbaa_to_rgb(tdata_t data, uint32 samplecount) |
| 3865 |
{ |
| 3866 |
uint32 i; |
| 3867 |
|
| 3868 |
/* For the 3 first samples, there is overlapping between souce and |
| 3869 |
destination, so use memmove(). |
| 3870 |
See http://bugzilla.maptools.org/show_bug.cgi?id=2577 */ |
| 3871 |
for(i = 0; i < 3 && i < samplecount; i++) |
| 3872 |
memmove((uint8*)data + i * 3, (uint8*)data + i * 4, 3); |
| 3873 |
for(; i < samplecount; i++) |
| 3874 |
memcpy((uint8*)data + i * 3, (uint8*)data + i * 4, 3); |
| 3875 |
|
| 3876 |
return(i * 3); |
| 3877 |
} |
| 3878 |
|
| 3879 |
/* |
| 3880 |
* This functions converts in place a buffer of RGBA interleaved data |
| 3881 |
* into RGB interleaved data, adding 255-A to each component sample. |
| 3882 |
*/ |
| 3883 |
|
| 3884 |
static |
| 3885 |
tsize_t |
| 3886 |
t2p_sample_rgba_to_rgb(tdata_t data, uint32 samplecount) |
| 3887 |
{ |
| 3888 |
uint32 i = 0; |
| 3889 |
uint32 sample = 0; |
| 3890 |
uint8 alpha = 0; |
| 3891 |
|
| 3892 |
for (i = 0; i < samplecount; i++) { |
| 3893 |
sample=((uint32*)data)[i]; |
| 3894 |
alpha=(uint8)((255 - ((sample >> 24) & 0xff))); |
| 3895 |
((uint8 *)data)[i * 3] = (uint8) ((sample >> 16) & 0xff) + alpha; |
| 3896 |
((uint8 *)data)[i * 3 + 1] = (uint8) ((sample >> 8) & 0xff) + alpha; |
| 3897 |
((uint8 *)data)[i * 3 + 2] = (uint8) (sample & 0xff) + alpha; |
| 3898 |
} |
| 3899 |
|
| 3900 |
return (i * 3); |
| 3901 |
} |
| 3902 |
|
| 3903 |
/* |
| 3904 |
This function converts the a and b samples of Lab data from signed |
| 3905 |
to unsigned. |
| 3906 |
*/ |
| 3907 |
|
| 3908 |
static |
| 3909 |
tsize_t t2p_sample_lab_signed_to_unsigned(tdata_t buffer, uint32 samplecount){ |
| 3910 |
|
| 3911 |
uint32 i=0; |
| 3912 |
|
| 3913 |
for(i=0;i<samplecount;i++){ |
| 3914 |
if( (((unsigned char*)buffer)[(i*3)+1] & 0x80) !=0){ |
| 3915 |
((unsigned char*)buffer)[(i*3)+1] = |
| 3916 |
(unsigned char)(0x80 + ((char*)buffer)[(i*3)+1]); |
| 3917 |
} else { |
| 3918 |
((unsigned char*)buffer)[(i*3)+1] |= 0x80; |
| 3919 |
} |
| 3920 |
if( (((unsigned char*)buffer)[(i*3)+2] & 0x80) !=0){ |
| 3921 |
((unsigned char*)buffer)[(i*3)+2] = |
| 3922 |
(unsigned char)(0x80 + ((char*)buffer)[(i*3)+2]); |
| 3923 |
} else { |
| 3924 |
((unsigned char*)buffer)[(i*3)+2] |= 0x80; |
| 3925 |
} |
| 3926 |
} |
| 3927 |
|
| 3928 |
return(samplecount*3); |
| 3929 |
} |
| 3930 |
|
| 3931 |
/* |
| 3932 |
This function writes the PDF header to output. |
| 3933 |
*/ |
| 3934 |
|
| 3935 |
static |
| 3936 |
tsize_t t2p_write_pdf_header(T2P* t2p, TIFF* output){ |
| 3937 |
|
| 3938 |
tsize_t written=0; |
| 3939 |
char buffer[16]; |
| 3940 |
int buflen=0; |
| 3941 |
|
| 3942 |
buflen = snprintf(buffer, sizeof(buffer), "%%PDF-%u.%u ", |
| 3943 |
t2p->pdf_majorversion&0xff, |
| 3944 |
t2p->pdf_minorversion&0xff); |
| 3945 |
check_snprintf_ret(t2p, buflen, buffer); |
| 3946 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 3947 |
written += t2pWriteFile(output, (tdata_t)"\n%\342\343\317\323\n", 7); |
| 3948 |
|
| 3949 |
return(written); |
| 3950 |
} |
| 3951 |
|
| 3952 |
/* |
| 3953 |
This function writes the beginning of a PDF object to output. |
| 3954 |
*/ |
| 3955 |
|
| 3956 |
static |
| 3957 |
tsize_t t2p_write_pdf_obj_start(uint32 number, TIFF* output){ |
| 3958 |
|
| 3959 |
tsize_t written=0; |
| 3960 |
char buffer[32]; |
| 3961 |
int buflen=0; |
| 3962 |
|
| 3963 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", (unsigned long)number); |
| 3964 |
check_snprintf_ret((T2P*)NULL, buflen, buffer); |
| 3965 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen ); |
| 3966 |
written += t2pWriteFile(output, (tdata_t) " 0 obj\n", 7); |
| 3967 |
|
| 3968 |
return(written); |
| 3969 |
} |
| 3970 |
|
| 3971 |
/* |
| 3972 |
This function writes the end of a PDF object to output. |
| 3973 |
*/ |
| 3974 |
|
| 3975 |
static |
| 3976 |
tsize_t t2p_write_pdf_obj_end(TIFF* output){ |
| 3977 |
|
| 3978 |
tsize_t written=0; |
| 3979 |
|
| 3980 |
written += t2pWriteFile(output, (tdata_t) "endobj\n", 7); |
| 3981 |
|
| 3982 |
return(written); |
| 3983 |
} |
| 3984 |
|
| 3985 |
/* |
| 3986 |
* This function writes a PDF string object to output. |
| 3987 |
*/ |
| 3988 |
|
| 3989 |
static |
| 3990 |
tsize_t t2p_write_pdf_string(char* pdfstr, TIFF* output) |
| 3991 |
{ |
| 3992 |
tsize_t written = 0; |
| 3993 |
uint32 i = 0; |
| 3994 |
char buffer[64]; |
| 3995 |
size_t len = 0; |
| 3996 |
|
| 3997 |
len = strlen(pdfstr); |
| 3998 |
written += t2pWriteFile(output, (tdata_t) "(", 1); |
| 3999 |
for (i=0; i<len; i++) { |
| 4000 |
if((pdfstr[i]&0x80) || (pdfstr[i]==127) || (pdfstr[i]<32)){ |
| 4001 |
snprintf(buffer, sizeof(buffer), "\\%.3o", ((unsigned char)pdfstr[i])); |
| 4002 |
written += t2pWriteFile(output, (tdata_t)buffer, 4); |
| 4003 |
} else { |
| 4004 |
switch (pdfstr[i]){ |
| 4005 |
case 0x08: |
| 4006 |
written += t2pWriteFile(output, (tdata_t) "\\b", 2); |
| 4007 |
break; |
| 4008 |
case 0x09: |
| 4009 |
written += t2pWriteFile(output, (tdata_t) "\\t", 2); |
| 4010 |
break; |
| 4011 |
case 0x0A: |
| 4012 |
written += t2pWriteFile(output, (tdata_t) "\\n", 2); |
| 4013 |
break; |
| 4014 |
case 0x0C: |
| 4015 |
written += t2pWriteFile(output, (tdata_t) "\\f", 2); |
| 4016 |
break; |
| 4017 |
case 0x0D: |
| 4018 |
written += t2pWriteFile(output, (tdata_t) "\\r", 2); |
| 4019 |
break; |
| 4020 |
case 0x28: |
| 4021 |
written += t2pWriteFile(output, (tdata_t) "\\(", 2); |
| 4022 |
break; |
| 4023 |
case 0x29: |
| 4024 |
written += t2pWriteFile(output, (tdata_t) "\\)", 2); |
| 4025 |
break; |
| 4026 |
case 0x5C: |
| 4027 |
written += t2pWriteFile(output, (tdata_t) "\\\\", 2); |
| 4028 |
break; |
| 4029 |
default: |
| 4030 |
written += t2pWriteFile(output, (tdata_t) &pdfstr[i], 1); |
| 4031 |
} |
| 4032 |
} |
| 4033 |
} |
| 4034 |
written += t2pWriteFile(output, (tdata_t) ") ", 1); |
| 4035 |
|
| 4036 |
return(written); |
| 4037 |
} |
| 4038 |
|
| 4039 |
|
| 4040 |
/* |
| 4041 |
This function writes a buffer of data to output. |
| 4042 |
*/ |
| 4043 |
|
| 4044 |
static |
| 4045 |
tsize_t t2p_write_pdf_stream(tdata_t buffer, tsize_t len, TIFF* output){ |
| 4046 |
|
| 4047 |
tsize_t written=0; |
| 4048 |
|
| 4049 |
written += t2pWriteFile(output, (tdata_t) buffer, len); |
| 4050 |
|
| 4051 |
return(written); |
| 4052 |
} |
| 4053 |
|
| 4054 |
/* |
| 4055 |
This functions writes the beginning of a PDF stream to output. |
| 4056 |
*/ |
| 4057 |
|
| 4058 |
static |
| 4059 |
tsize_t t2p_write_pdf_stream_start(TIFF* output){ |
| 4060 |
|
| 4061 |
tsize_t written=0; |
| 4062 |
|
| 4063 |
written += t2pWriteFile(output, (tdata_t) "stream\n", 7); |
| 4064 |
|
| 4065 |
return(written); |
| 4066 |
} |
| 4067 |
|
| 4068 |
/* |
| 4069 |
This function writes the end of a PDF stream to output. |
| 4070 |
*/ |
| 4071 |
|
| 4072 |
static |
| 4073 |
tsize_t t2p_write_pdf_stream_end(TIFF* output){ |
| 4074 |
|
| 4075 |
tsize_t written=0; |
| 4076 |
|
| 4077 |
written += t2pWriteFile(output, (tdata_t) "\nendstream\n", 11); |
| 4078 |
|
| 4079 |
return(written); |
| 4080 |
} |
| 4081 |
|
| 4082 |
/* |
| 4083 |
This function writes a stream dictionary for a PDF stream to output. |
| 4084 |
*/ |
| 4085 |
|
| 4086 |
static |
| 4087 |
tsize_t t2p_write_pdf_stream_dict(tsize_t len, uint32 number, TIFF* output){ |
| 4088 |
|
| 4089 |
tsize_t written=0; |
| 4090 |
char buffer[32]; |
| 4091 |
int buflen=0; |
| 4092 |
|
| 4093 |
written += t2pWriteFile(output, (tdata_t) "/Length ", 8); |
| 4094 |
if(len!=0){ |
| 4095 |
written += t2p_write_pdf_stream_length(len, output); |
| 4096 |
} else { |
| 4097 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", (unsigned long)number); |
| 4098 |
check_snprintf_ret((T2P*)NULL, buflen, buffer); |
| 4099 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 4100 |
written += t2pWriteFile(output, (tdata_t) " 0 R \n", 6); |
| 4101 |
} |
| 4102 |
|
| 4103 |
return(written); |
| 4104 |
} |
| 4105 |
|
| 4106 |
/* |
| 4107 |
This functions writes the beginning of a PDF stream dictionary to output. |
| 4108 |
*/ |
| 4109 |
|
| 4110 |
static |
| 4111 |
tsize_t t2p_write_pdf_stream_dict_start(TIFF* output){ |
| 4112 |
|
| 4113 |
tsize_t written=0; |
| 4114 |
|
| 4115 |
written += t2pWriteFile(output, (tdata_t) "<< \n", 4); |
| 4116 |
|
| 4117 |
return(written); |
| 4118 |
} |
| 4119 |
|
| 4120 |
/* |
| 4121 |
This function writes the end of a PDF stream dictionary to output. |
| 4122 |
*/ |
| 4123 |
|
| 4124 |
static |
| 4125 |
tsize_t t2p_write_pdf_stream_dict_end(TIFF* output){ |
| 4126 |
|
| 4127 |
tsize_t written=0; |
| 4128 |
|
| 4129 |
written += t2pWriteFile(output, (tdata_t) " >>\n", 4); |
| 4130 |
|
| 4131 |
return(written); |
| 4132 |
} |
| 4133 |
|
| 4134 |
/* |
| 4135 |
This function writes a number to output. |
| 4136 |
*/ |
| 4137 |
|
| 4138 |
static |
| 4139 |
tsize_t t2p_write_pdf_stream_length(tsize_t len, TIFF* output){ |
| 4140 |
|
| 4141 |
tsize_t written=0; |
| 4142 |
char buffer[32]; |
| 4143 |
int buflen=0; |
| 4144 |
|
| 4145 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", (unsigned long)len); |
| 4146 |
check_snprintf_ret((T2P*)NULL, buflen, buffer); |
| 4147 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 4148 |
written += t2pWriteFile(output, (tdata_t) "\n", 1); |
| 4149 |
|
| 4150 |
return(written); |
| 4151 |
} |
| 4152 |
|
| 4153 |
/* |
| 4154 |
* This function writes the PDF Catalog structure to output. |
| 4155 |
*/ |
| 4156 |
|
| 4157 |
static |
| 4158 |
tsize_t t2p_write_pdf_catalog(T2P* t2p, TIFF* output) |
| 4159 |
{ |
| 4160 |
tsize_t written = 0; |
| 4161 |
char buffer[32]; |
| 4162 |
int buflen = 0; |
| 4163 |
|
| 4164 |
written += t2pWriteFile(output, |
| 4165 |
(tdata_t)"<< \n/Type /Catalog \n/Pages ", |
| 4166 |
27); |
| 4167 |
buflen = snprintf(buffer, sizeof(buffer), "%lu", (unsigned long)t2p->pdf_pages); |
| 4168 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4169 |
written += t2pWriteFile(output, (tdata_t) buffer, |
| 4170 |
TIFFmin((size_t)buflen, sizeof(buffer) - 1)); |
| 4171 |
written += t2pWriteFile(output, (tdata_t) " 0 R \n", 6); |
| 4172 |
if(t2p->pdf_fitwindow){ |
| 4173 |
written += t2pWriteFile(output, |
| 4174 |
(tdata_t) "/ViewerPreferences <</FitWindow true>>\n", |
| 4175 |
39); |
| 4176 |
} |
| 4177 |
written += t2pWriteFile(output, (tdata_t)">>\n", 3); |
| 4178 |
|
| 4179 |
return(written); |
| 4180 |
} |
| 4181 |
|
| 4182 |
/* |
| 4183 |
This function writes the PDF Info structure to output. |
| 4184 |
*/ |
| 4185 |
|
| 4186 |
static |
| 4187 |
tsize_t t2p_write_pdf_info(T2P* t2p, TIFF* input, TIFF* output) |
| 4188 |
{ |
| 4189 |
tsize_t written = 0; |
| 4190 |
char* info; |
| 4191 |
char buffer[512]; |
| 4192 |
|
| 4193 |
if(t2p->pdf_datetime[0] == '\0') |
| 4194 |
t2p_pdf_tifftime(t2p, input); |
| 4195 |
if (strlen(t2p->pdf_datetime) > 0) { |
| 4196 |
written += t2pWriteFile(output, (tdata_t) "<< \n/CreationDate ", 18); |
| 4197 |
written += t2p_write_pdf_string(t2p->pdf_datetime, output); |
| 4198 |
written += t2pWriteFile(output, (tdata_t) "\n/ModDate ", 10); |
| 4199 |
written += t2p_write_pdf_string(t2p->pdf_datetime, output); |
| 4200 |
} |
| 4201 |
written += t2pWriteFile(output, (tdata_t) "\n/Producer ", 11); |
| 4202 |
snprintf(buffer, sizeof(buffer), "libtiff / tiff2pdf - %d", TIFFLIB_VERSION); |
| 4203 |
written += t2p_write_pdf_string(buffer, output); |
| 4204 |
written += t2pWriteFile(output, (tdata_t) "\n", 1); |
| 4205 |
if (t2p->pdf_creator[0] != '\0') { |
| 4206 |
written += t2pWriteFile(output, (tdata_t) "/Creator ", 9); |
| 4207 |
written += t2p_write_pdf_string(t2p->pdf_creator, output); |
| 4208 |
written += t2pWriteFile(output, (tdata_t) "\n", 1); |
| 4209 |
} else { |
| 4210 |
if (TIFFGetField(input, TIFFTAG_SOFTWARE, &info) != 0 && info) { |
| 4211 |
if(strlen(info) >= sizeof(t2p->pdf_creator)) |
| 4212 |
info[sizeof(t2p->pdf_creator) - 1] = '\0'; |
| 4213 |
written += t2pWriteFile(output, (tdata_t) "/Creator ", 9); |
| 4214 |
written += t2p_write_pdf_string(info, output); |
| 4215 |
written += t2pWriteFile(output, (tdata_t) "\n", 1); |
| 4216 |
} |
| 4217 |
} |
| 4218 |
if (t2p->pdf_author[0] != '\0') { |
| 4219 |
written += t2pWriteFile(output, (tdata_t) "/Author ", 8); |
| 4220 |
written += t2p_write_pdf_string(t2p->pdf_author, output); |
| 4221 |
written += t2pWriteFile(output, (tdata_t) "\n", 1); |
| 4222 |
} else { |
| 4223 |
if ((TIFFGetField(input, TIFFTAG_ARTIST, &info) != 0 |
| 4224 |
|| TIFFGetField(input, TIFFTAG_COPYRIGHT, &info) != 0) |
| 4225 |
&& info) { |
| 4226 |
if (strlen(info) >= sizeof(t2p->pdf_author)) |
| 4227 |
info[sizeof(t2p->pdf_author) - 1] = '\0'; |
| 4228 |
written += t2pWriteFile(output, (tdata_t) "/Author ", 8); |
| 4229 |
written += t2p_write_pdf_string(info, output); |
| 4230 |
written += t2pWriteFile(output, (tdata_t) "\n", 1); |
| 4231 |
} |
| 4232 |
} |
| 4233 |
if (t2p->pdf_title[0] != '\0') { |
| 4234 |
written += t2pWriteFile(output, (tdata_t) "/Title ", 7); |
| 4235 |
written += t2p_write_pdf_string(t2p->pdf_title, output); |
| 4236 |
written += t2pWriteFile(output, (tdata_t) "\n", 1); |
| 4237 |
} else { |
| 4238 |
if (TIFFGetField(input, TIFFTAG_DOCUMENTNAME, &info) != 0){ |
| 4239 |
if(strlen(info) > 511) { |
| 4240 |
info[512] = '\0'; |
| 4241 |
} |
| 4242 |
written += t2pWriteFile(output, (tdata_t) "/Title ", 7); |
| 4243 |
written += t2p_write_pdf_string(info, output); |
| 4244 |
written += t2pWriteFile(output, (tdata_t) "\n", 1); |
| 4245 |
} |
| 4246 |
} |
| 4247 |
if (t2p->pdf_subject[0] != '\0') { |
| 4248 |
written += t2pWriteFile(output, (tdata_t) "/Subject ", 9); |
| 4249 |
written += t2p_write_pdf_string(t2p->pdf_subject, output); |
| 4250 |
written += t2pWriteFile(output, (tdata_t) "\n", 1); |
| 4251 |
} else { |
| 4252 |
if (TIFFGetField(input, TIFFTAG_IMAGEDESCRIPTION, &info) != 0 && info) { |
| 4253 |
if (strlen(info) >= sizeof(t2p->pdf_subject)) |
| 4254 |
info[sizeof(t2p->pdf_subject) - 1] = '\0'; |
| 4255 |
written += t2pWriteFile(output, (tdata_t) "/Subject ", 9); |
| 4256 |
written += t2p_write_pdf_string(info, output); |
| 4257 |
written += t2pWriteFile(output, (tdata_t) "\n", 1); |
| 4258 |
} |
| 4259 |
} |
| 4260 |
if (t2p->pdf_keywords[0] != '\0') { |
| 4261 |
written += t2pWriteFile(output, (tdata_t) "/Keywords ", 10); |
| 4262 |
written += t2p_write_pdf_string(t2p->pdf_keywords, output); |
| 4263 |
written += t2pWriteFile(output, (tdata_t) "\n", 1); |
| 4264 |
} |
| 4265 |
written += t2pWriteFile(output, (tdata_t) ">> \n", 4); |
| 4266 |
|
| 4267 |
return(written); |
| 4268 |
} |
| 4269 |
|
| 4270 |
/* |
| 4271 |
* This function fills a string of a T2P struct with the current time as a PDF |
| 4272 |
* date string, it is called by t2p_pdf_tifftime. |
| 4273 |
*/ |
| 4274 |
|
| 4275 |
static |
| 4276 |
void t2p_pdf_currenttime(T2P* t2p) |
| 4277 |
{ |
| 4278 |
struct tm* currenttime; |
| 4279 |
time_t timenow; |
| 4280 |
|
| 4281 |
if (time(&timenow) == (time_t) -1) { |
| 4282 |
TIFFError(TIFF2PDF_MODULE, |
| 4283 |
"Can't get the current time: %s", strerror(errno)); |
| 4284 |
timenow = (time_t) 0; |
| 4285 |
} |
| 4286 |
|
| 4287 |
currenttime = localtime(&timenow); |
| 4288 |
snprintf(t2p->pdf_datetime, sizeof(t2p->pdf_datetime), |
| 4289 |
"D:%.4d%.2d%.2d%.2d%.2d%.2d", |
| 4290 |
TIFFclip( (currenttime->tm_year + 1900) % 65536 , 9999), |
| 4291 |
TIFFclip( (currenttime->tm_mon + 1) % 256 , 99), |
| 4292 |
TIFFclip( (currenttime->tm_mday) % 256 , 99), |
| 4293 |
TIFFclip( (currenttime->tm_hour) % 256 , 99), |
| 4294 |
TIFFclip( (currenttime->tm_min) % 256 , 99), |
| 4295 |
TIFFclip( (currenttime->tm_sec) % 256 , 99)); |
| 4296 |
|
| 4297 |
return; |
| 4298 |
} |
| 4299 |
|
| 4300 |
/* |
| 4301 |
* This function fills a string of a T2P struct with the date and time of a |
| 4302 |
* TIFF file if it exists or the current time as a PDF date string. |
| 4303 |
*/ |
| 4304 |
|
| 4305 |
void t2p_pdf_tifftime(T2P* t2p, TIFF* input) |
| 4306 |
{ |
| 4307 |
char* datetime; |
| 4308 |
|
| 4309 |
if (TIFFGetField(input, TIFFTAG_DATETIME, &datetime) != 0 |
| 4310 |
&& (strlen(datetime) >= 19) ){ |
| 4311 |
t2p->pdf_datetime[0]='D'; |
| 4312 |
t2p->pdf_datetime[1]=':'; |
| 4313 |
t2p->pdf_datetime[2]=datetime[0]; |
| 4314 |
t2p->pdf_datetime[3]=datetime[1]; |
| 4315 |
t2p->pdf_datetime[4]=datetime[2]; |
| 4316 |
t2p->pdf_datetime[5]=datetime[3]; |
| 4317 |
t2p->pdf_datetime[6]=datetime[5]; |
| 4318 |
t2p->pdf_datetime[7]=datetime[6]; |
| 4319 |
t2p->pdf_datetime[8]=datetime[8]; |
| 4320 |
t2p->pdf_datetime[9]=datetime[9]; |
| 4321 |
t2p->pdf_datetime[10]=datetime[11]; |
| 4322 |
t2p->pdf_datetime[11]=datetime[12]; |
| 4323 |
t2p->pdf_datetime[12]=datetime[14]; |
| 4324 |
t2p->pdf_datetime[13]=datetime[15]; |
| 4325 |
t2p->pdf_datetime[14]=datetime[17]; |
| 4326 |
t2p->pdf_datetime[15]=datetime[18]; |
| 4327 |
t2p->pdf_datetime[16] = '\0'; |
| 4328 |
} else { |
| 4329 |
t2p_pdf_currenttime(t2p); |
| 4330 |
} |
| 4331 |
|
| 4332 |
return; |
| 4333 |
} |
| 4334 |
|
| 4335 |
/* |
| 4336 |
* This function writes a PDF Pages Tree structure to output. |
| 4337 |
*/ |
| 4338 |
|
| 4339 |
static |
| 4340 |
tsize_t t2p_write_pdf_pages(T2P* t2p, TIFF* output) |
| 4341 |
{ |
| 4342 |
tsize_t written=0; |
| 4343 |
tdir_t i=0; |
| 4344 |
char buffer[32]; |
| 4345 |
int buflen=0; |
| 4346 |
|
| 4347 |
int page=0; |
| 4348 |
written += t2pWriteFile(output, |
| 4349 |
(tdata_t) "<< \n/Type /Pages \n/Kids [ ", 26); |
| 4350 |
page = t2p->pdf_pages+1; |
| 4351 |
for (i=0;i<t2p->tiff_pagecount;i++){ |
| 4352 |
buflen=snprintf(buffer, sizeof(buffer), "%d", page); |
| 4353 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4354 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 4355 |
written += t2pWriteFile(output, (tdata_t) " 0 R ", 5); |
| 4356 |
if ( ((i+1)%8)==0 ) { |
| 4357 |
written += t2pWriteFile(output, (tdata_t) "\n", 1); |
| 4358 |
} |
| 4359 |
page +=3; |
| 4360 |
page += t2p->tiff_pages[i].page_extra; |
| 4361 |
if(t2p->tiff_pages[i].page_tilecount>0){ |
| 4362 |
page += (2 * t2p->tiff_pages[i].page_tilecount); |
| 4363 |
} else { |
| 4364 |
page +=2; |
| 4365 |
} |
| 4366 |
} |
| 4367 |
written += t2pWriteFile(output, (tdata_t) "] \n/Count ", 10); |
| 4368 |
buflen=snprintf(buffer, sizeof(buffer), "%d", t2p->tiff_pagecount); |
| 4369 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4370 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 4371 |
written += t2pWriteFile(output, (tdata_t) " \n>> \n", 6); |
| 4372 |
|
| 4373 |
return(written); |
| 4374 |
} |
| 4375 |
|
| 4376 |
/* |
| 4377 |
This function writes a PDF Page structure to output. |
| 4378 |
*/ |
| 4379 |
|
| 4380 |
static |
| 4381 |
tsize_t t2p_write_pdf_page(uint32 object, T2P* t2p, TIFF* output){ |
| 4382 |
|
| 4383 |
unsigned int i=0; |
| 4384 |
tsize_t written=0; |
| 4385 |
char buffer[256]; |
| 4386 |
int buflen=0; |
| 4387 |
|
| 4388 |
written += t2pWriteFile(output, (tdata_t) "<<\n/Type /Page \n/Parent ", 24); |
| 4389 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", (unsigned long)t2p->pdf_pages); |
| 4390 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4391 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 4392 |
written += t2pWriteFile(output, (tdata_t) " 0 R \n", 6); |
| 4393 |
written += t2pWriteFile(output, (tdata_t) "/MediaBox [", 11); |
| 4394 |
buflen=snprintf(buffer, sizeof(buffer), "%.4f",t2p->pdf_mediabox.x1); |
| 4395 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4396 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 4397 |
written += t2pWriteFile(output, (tdata_t) " ", 1); |
| 4398 |
buflen=snprintf(buffer, sizeof(buffer), "%.4f",t2p->pdf_mediabox.y1); |
| 4399 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4400 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 4401 |
written += t2pWriteFile(output, (tdata_t) " ", 1); |
| 4402 |
buflen=snprintf(buffer, sizeof(buffer), "%.4f",t2p->pdf_mediabox.x2); |
| 4403 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4404 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 4405 |
written += t2pWriteFile(output, (tdata_t) " ", 1); |
| 4406 |
buflen=snprintf(buffer, sizeof(buffer), "%.4f",t2p->pdf_mediabox.y2); |
| 4407 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4408 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 4409 |
written += t2pWriteFile(output, (tdata_t) "] \n", 3); |
| 4410 |
written += t2pWriteFile(output, (tdata_t) "/Contents ", 10); |
| 4411 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", (unsigned long)(object + 1)); |
| 4412 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4413 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 4414 |
written += t2pWriteFile(output, (tdata_t) " 0 R \n", 6); |
| 4415 |
written += t2pWriteFile(output, (tdata_t) "/Resources << \n", 15); |
| 4416 |
if( t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount != 0 ){ |
| 4417 |
written += t2pWriteFile(output, (tdata_t) "/XObject <<\n", 12); |
| 4418 |
for(i=0;i<t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount;i++){ |
| 4419 |
written += t2pWriteFile(output, (tdata_t) "/Im", 3); |
| 4420 |
buflen = snprintf(buffer, sizeof(buffer), "%u", t2p->pdf_page+1); |
| 4421 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4422 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 4423 |
written += t2pWriteFile(output, (tdata_t) "_", 1); |
| 4424 |
buflen = snprintf(buffer, sizeof(buffer), "%u", i+1); |
| 4425 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4426 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 4427 |
written += t2pWriteFile(output, (tdata_t) " ", 1); |
| 4428 |
buflen = snprintf(buffer, sizeof(buffer), "%lu", |
| 4429 |
(unsigned long)(object+3+(2*i)+t2p->tiff_pages[t2p->pdf_page].page_extra)); |
| 4430 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4431 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 4432 |
written += t2pWriteFile(output, (tdata_t) " 0 R ", 5); |
| 4433 |
if(i%4==3){ |
| 4434 |
written += t2pWriteFile(output, (tdata_t) "\n", 1); |
| 4435 |
} |
| 4436 |
} |
| 4437 |
written += t2pWriteFile(output, (tdata_t) ">>\n", 3); |
| 4438 |
} else { |
| 4439 |
written += t2pWriteFile(output, (tdata_t) "/XObject <<\n", 12); |
| 4440 |
written += t2pWriteFile(output, (tdata_t) "/Im", 3); |
| 4441 |
buflen = snprintf(buffer, sizeof(buffer), "%u", t2p->pdf_page+1); |
| 4442 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4443 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 4444 |
written += t2pWriteFile(output, (tdata_t) " ", 1); |
| 4445 |
buflen = snprintf(buffer, sizeof(buffer), "%lu", |
| 4446 |
(unsigned long)(object+3+(2*i)+t2p->tiff_pages[t2p->pdf_page].page_extra)); |
| 4447 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4448 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 4449 |
written += t2pWriteFile(output, (tdata_t) " 0 R ", 5); |
| 4450 |
written += t2pWriteFile(output, (tdata_t) ">>\n", 3); |
| 4451 |
} |
| 4452 |
if(t2p->tiff_transferfunctioncount != 0) { |
| 4453 |
written += t2pWriteFile(output, (tdata_t) "/ExtGState <<", 13); |
| 4454 |
t2pWriteFile(output, (tdata_t) "/GS1 ", 5); |
| 4455 |
buflen = snprintf(buffer, sizeof(buffer), "%lu", |
| 4456 |
(unsigned long)(object + 3)); |
| 4457 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4458 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 4459 |
written += t2pWriteFile(output, (tdata_t) " 0 R ", 5); |
| 4460 |
written += t2pWriteFile(output, (tdata_t) ">> \n", 4); |
| 4461 |
} |
| 4462 |
written += t2pWriteFile(output, (tdata_t) "/ProcSet [ ", 11); |
| 4463 |
if(t2p->pdf_colorspace & T2P_CS_BILEVEL |
| 4464 |
|| t2p->pdf_colorspace & T2P_CS_GRAY |
| 4465 |
){ |
| 4466 |
written += t2pWriteFile(output, (tdata_t) "/ImageB ", 8); |
| 4467 |
} else { |
| 4468 |
written += t2pWriteFile(output, (tdata_t) "/ImageC ", 8); |
| 4469 |
if(t2p->pdf_colorspace & T2P_CS_PALETTE){ |
| 4470 |
written += t2pWriteFile(output, (tdata_t) "/ImageI ", 8); |
| 4471 |
} |
| 4472 |
} |
| 4473 |
written += t2pWriteFile(output, (tdata_t) "]\n>>\n>>\n", 8); |
| 4474 |
|
| 4475 |
return(written); |
| 4476 |
} |
| 4477 |
|
| 4478 |
/* |
| 4479 |
This function composes the page size and image and tile locations on a page. |
| 4480 |
*/ |
| 4481 |
|
| 4482 |
static |
| 4483 |
void t2p_compose_pdf_page(T2P* t2p){ |
| 4484 |
|
| 4485 |
uint32 i=0; |
| 4486 |
uint32 i2=0; |
| 4487 |
T2P_TILE* tiles=NULL; |
| 4488 |
T2P_BOX* boxp=NULL; |
| 4489 |
uint32 tilecountx=0; |
| 4490 |
uint32 tilecounty=0; |
| 4491 |
uint32 tilewidth=0; |
| 4492 |
uint32 tilelength=0; |
| 4493 |
int istiled=0; |
| 4494 |
float f=0; |
| 4495 |
float width_ratio=0; |
| 4496 |
float length_ratio=0; |
| 4497 |
|
| 4498 |
t2p->pdf_xres = t2p->tiff_xres; |
| 4499 |
t2p->pdf_yres = t2p->tiff_yres; |
| 4500 |
if(t2p->pdf_overrideres) { |
| 4501 |
t2p->pdf_xres = t2p->pdf_defaultxres; |
| 4502 |
t2p->pdf_yres = t2p->pdf_defaultyres; |
| 4503 |
} |
| 4504 |
if(t2p->pdf_xres == 0.0) |
| 4505 |
t2p->pdf_xres = t2p->pdf_defaultxres; |
| 4506 |
if(t2p->pdf_yres == 0.0) |
| 4507 |
t2p->pdf_yres = t2p->pdf_defaultyres; |
| 4508 |
if (t2p->pdf_image_fillpage) { |
| 4509 |
width_ratio = t2p->pdf_defaultpagewidth/t2p->tiff_width; |
| 4510 |
length_ratio = t2p->pdf_defaultpagelength/t2p->tiff_length; |
| 4511 |
if (width_ratio < length_ratio ) { |
| 4512 |
t2p->pdf_imagewidth = t2p->pdf_defaultpagewidth; |
| 4513 |
t2p->pdf_imagelength = t2p->tiff_length * width_ratio; |
| 4514 |
} else { |
| 4515 |
t2p->pdf_imagewidth = t2p->tiff_width * length_ratio; |
| 4516 |
t2p->pdf_imagelength = t2p->pdf_defaultpagelength; |
| 4517 |
} |
| 4518 |
} else if (t2p->tiff_resunit != RESUNIT_CENTIMETER /* RESUNIT_NONE and */ |
| 4519 |
&& t2p->tiff_resunit != RESUNIT_INCH) { /* other cases */ |
| 4520 |
t2p->pdf_imagewidth = ((float)(t2p->tiff_width))/t2p->pdf_xres; |
| 4521 |
t2p->pdf_imagelength = ((float)(t2p->tiff_length))/t2p->pdf_yres; |
| 4522 |
} else { |
| 4523 |
t2p->pdf_imagewidth = |
| 4524 |
((float)(t2p->tiff_width))*PS_UNIT_SIZE/t2p->pdf_xres; |
| 4525 |
t2p->pdf_imagelength = |
| 4526 |
((float)(t2p->tiff_length))*PS_UNIT_SIZE/t2p->pdf_yres; |
| 4527 |
} |
| 4528 |
if(t2p->pdf_overridepagesize != 0) { |
| 4529 |
t2p->pdf_pagewidth = t2p->pdf_defaultpagewidth; |
| 4530 |
t2p->pdf_pagelength = t2p->pdf_defaultpagelength; |
| 4531 |
} else { |
| 4532 |
t2p->pdf_pagewidth = t2p->pdf_imagewidth; |
| 4533 |
t2p->pdf_pagelength = t2p->pdf_imagelength; |
| 4534 |
} |
| 4535 |
t2p->pdf_mediabox.x1=0.0; |
| 4536 |
t2p->pdf_mediabox.y1=0.0; |
| 4537 |
t2p->pdf_mediabox.x2=t2p->pdf_pagewidth; |
| 4538 |
t2p->pdf_mediabox.y2=t2p->pdf_pagelength; |
| 4539 |
t2p->pdf_imagebox.x1=0.0; |
| 4540 |
t2p->pdf_imagebox.y1=0.0; |
| 4541 |
t2p->pdf_imagebox.x2=t2p->pdf_imagewidth; |
| 4542 |
t2p->pdf_imagebox.y2=t2p->pdf_imagelength; |
| 4543 |
if(t2p->pdf_overridepagesize!=0){ |
| 4544 |
t2p->pdf_imagebox.x1+=((t2p->pdf_pagewidth-t2p->pdf_imagewidth)/2.0F); |
| 4545 |
t2p->pdf_imagebox.y1+=((t2p->pdf_pagelength-t2p->pdf_imagelength)/2.0F); |
| 4546 |
t2p->pdf_imagebox.x2+=((t2p->pdf_pagewidth-t2p->pdf_imagewidth)/2.0F); |
| 4547 |
t2p->pdf_imagebox.y2+=((t2p->pdf_pagelength-t2p->pdf_imagelength)/2.0F); |
| 4548 |
} |
| 4549 |
if(t2p->tiff_orientation > 4){ |
| 4550 |
f=t2p->pdf_mediabox.x2; |
| 4551 |
t2p->pdf_mediabox.x2=t2p->pdf_mediabox.y2; |
| 4552 |
t2p->pdf_mediabox.y2=f; |
| 4553 |
} |
| 4554 |
istiled=((t2p->tiff_tiles[t2p->pdf_page]).tiles_tilecount==0) ? 0 : 1; |
| 4555 |
if(istiled==0){ |
| 4556 |
t2p_compose_pdf_page_orient(&(t2p->pdf_imagebox), t2p->tiff_orientation); |
| 4557 |
return; |
| 4558 |
} else { |
| 4559 |
tilewidth=(t2p->tiff_tiles[t2p->pdf_page]).tiles_tilewidth; |
| 4560 |
tilelength=(t2p->tiff_tiles[t2p->pdf_page]).tiles_tilelength; |
| 4561 |
if( tilewidth > INT_MAX || |
| 4562 |
tilelength > INT_MAX || |
| 4563 |
t2p->tiff_width > INT_MAX - tilewidth || |
| 4564 |
t2p->tiff_length > INT_MAX - tilelength ) |
| 4565 |
{ |
| 4566 |
TIFFError(TIFF2PDF_MODULE, "Integer overflow"); |
| 4567 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 4568 |
return; |
| 4569 |
} |
| 4570 |
tilecountx=(t2p->tiff_width + |
| 4571 |
tilewidth -1)/ |
| 4572 |
tilewidth; |
| 4573 |
(t2p->tiff_tiles[t2p->pdf_page]).tiles_tilecountx=tilecountx; |
| 4574 |
tilecounty=(t2p->tiff_length + |
| 4575 |
tilelength -1)/ |
| 4576 |
tilelength; |
| 4577 |
(t2p->tiff_tiles[t2p->pdf_page]).tiles_tilecounty=tilecounty; |
| 4578 |
(t2p->tiff_tiles[t2p->pdf_page]).tiles_edgetilewidth= |
| 4579 |
t2p->tiff_width % tilewidth; |
| 4580 |
(t2p->tiff_tiles[t2p->pdf_page]).tiles_edgetilelength= |
| 4581 |
t2p->tiff_length % tilelength; |
| 4582 |
tiles=(t2p->tiff_tiles[t2p->pdf_page]).tiles_tiles; |
| 4583 |
for(i2=0;i2<tilecounty-1;i2++){ |
| 4584 |
for(i=0;i<tilecountx-1;i++){ |
| 4585 |
boxp=&(tiles[i2*tilecountx+i].tile_box); |
| 4586 |
boxp->x1 = |
| 4587 |
t2p->pdf_imagebox.x1 |
| 4588 |
+ ((float)(t2p->pdf_imagewidth * i * tilewidth) |
| 4589 |
/ (float)t2p->tiff_width); |
| 4590 |
boxp->x2 = |
| 4591 |
t2p->pdf_imagebox.x1 |
| 4592 |
+ ((float)(t2p->pdf_imagewidth * (i+1) * tilewidth) |
| 4593 |
/ (float)t2p->tiff_width); |
| 4594 |
boxp->y1 = |
| 4595 |
t2p->pdf_imagebox.y2 |
| 4596 |
- ((float)(t2p->pdf_imagelength * (i2+1) * tilelength) |
| 4597 |
/ (float)t2p->tiff_length); |
| 4598 |
boxp->y2 = |
| 4599 |
t2p->pdf_imagebox.y2 |
| 4600 |
- ((float)(t2p->pdf_imagelength * i2 * tilelength) |
| 4601 |
/ (float)t2p->tiff_length); |
| 4602 |
} |
| 4603 |
boxp=&(tiles[i2*tilecountx+i].tile_box); |
| 4604 |
boxp->x1 = |
| 4605 |
t2p->pdf_imagebox.x1 |
| 4606 |
+ ((float)(t2p->pdf_imagewidth * i * tilewidth) |
| 4607 |
/ (float)t2p->tiff_width); |
| 4608 |
boxp->x2 = t2p->pdf_imagebox.x2; |
| 4609 |
boxp->y1 = |
| 4610 |
t2p->pdf_imagebox.y2 |
| 4611 |
- ((float)(t2p->pdf_imagelength * (i2+1) * tilelength) |
| 4612 |
/ (float)t2p->tiff_length); |
| 4613 |
boxp->y2 = |
| 4614 |
t2p->pdf_imagebox.y2 |
| 4615 |
- ((float)(t2p->pdf_imagelength * i2 * tilelength) |
| 4616 |
/ (float)t2p->tiff_length); |
| 4617 |
} |
| 4618 |
for(i=0;i<tilecountx-1;i++){ |
| 4619 |
boxp=&(tiles[i2*tilecountx+i].tile_box); |
| 4620 |
boxp->x1 = |
| 4621 |
t2p->pdf_imagebox.x1 |
| 4622 |
+ ((float)(t2p->pdf_imagewidth * i * tilewidth) |
| 4623 |
/ (float)t2p->tiff_width); |
| 4624 |
boxp->x2 = |
| 4625 |
t2p->pdf_imagebox.x1 |
| 4626 |
+ ((float)(t2p->pdf_imagewidth * (i+1) * tilewidth) |
| 4627 |
/ (float)t2p->tiff_width); |
| 4628 |
boxp->y1 = t2p->pdf_imagebox.y1; |
| 4629 |
boxp->y2 = |
| 4630 |
t2p->pdf_imagebox.y2 |
| 4631 |
- ((float)(t2p->pdf_imagelength * i2 * tilelength) |
| 4632 |
/ (float)t2p->tiff_length); |
| 4633 |
} |
| 4634 |
boxp=&(tiles[i2*tilecountx+i].tile_box); |
| 4635 |
boxp->x1 = |
| 4636 |
t2p->pdf_imagebox.x1 |
| 4637 |
+ ((float)(t2p->pdf_imagewidth * i * tilewidth) |
| 4638 |
/ (float)t2p->tiff_width); |
| 4639 |
boxp->x2 = t2p->pdf_imagebox.x2; |
| 4640 |
boxp->y1 = t2p->pdf_imagebox.y1; |
| 4641 |
boxp->y2 = |
| 4642 |
t2p->pdf_imagebox.y2 |
| 4643 |
- ((float)(t2p->pdf_imagelength * i2 * tilelength) |
| 4644 |
/ (float)t2p->tiff_length); |
| 4645 |
} |
| 4646 |
if(t2p->tiff_orientation==0 || t2p->tiff_orientation==1){ |
| 4647 |
for(i=0;i<(t2p->tiff_tiles[t2p->pdf_page]).tiles_tilecount;i++){ |
| 4648 |
t2p_compose_pdf_page_orient( &(tiles[i].tile_box) , 0); |
| 4649 |
} |
| 4650 |
return; |
| 4651 |
} |
| 4652 |
for(i=0;i<(t2p->tiff_tiles[t2p->pdf_page]).tiles_tilecount;i++){ |
| 4653 |
boxp=&(tiles[i].tile_box); |
| 4654 |
boxp->x1 -= t2p->pdf_imagebox.x1; |
| 4655 |
boxp->x2 -= t2p->pdf_imagebox.x1; |
| 4656 |
boxp->y1 -= t2p->pdf_imagebox.y1; |
| 4657 |
boxp->y2 -= t2p->pdf_imagebox.y1; |
| 4658 |
if(t2p->tiff_orientation==2 || t2p->tiff_orientation==3){ |
| 4659 |
boxp->x1 = t2p->pdf_imagebox.x2 - t2p->pdf_imagebox.x1 - boxp->x1; |
| 4660 |
boxp->x2 = t2p->pdf_imagebox.x2 - t2p->pdf_imagebox.x1 - boxp->x2; |
| 4661 |
} |
| 4662 |
if(t2p->tiff_orientation==3 || t2p->tiff_orientation==4){ |
| 4663 |
boxp->y1 = t2p->pdf_imagebox.y2 - t2p->pdf_imagebox.y1 - boxp->y1; |
| 4664 |
boxp->y2 = t2p->pdf_imagebox.y2 - t2p->pdf_imagebox.y1 - boxp->y2; |
| 4665 |
} |
| 4666 |
if(t2p->tiff_orientation==8 || t2p->tiff_orientation==5){ |
| 4667 |
boxp->y1 = t2p->pdf_imagebox.y2 - t2p->pdf_imagebox.y1 - boxp->y1; |
| 4668 |
boxp->y2 = t2p->pdf_imagebox.y2 - t2p->pdf_imagebox.y1 - boxp->y2; |
| 4669 |
} |
| 4670 |
if(t2p->tiff_orientation==5 || t2p->tiff_orientation==6){ |
| 4671 |
boxp->x1 = t2p->pdf_imagebox.x2 - t2p->pdf_imagebox.x1 - boxp->x1; |
| 4672 |
boxp->x2 = t2p->pdf_imagebox.x2 - t2p->pdf_imagebox.x1 - boxp->x2; |
| 4673 |
} |
| 4674 |
if(t2p->tiff_orientation > 4){ |
| 4675 |
f=boxp->x1; |
| 4676 |
boxp->x1 = boxp->y1; |
| 4677 |
boxp->y1 = f; |
| 4678 |
f=boxp->x2; |
| 4679 |
boxp->x2 = boxp->y2; |
| 4680 |
boxp->y2 = f; |
| 4681 |
t2p_compose_pdf_page_orient_flip(boxp, t2p->tiff_orientation); |
| 4682 |
} else { |
| 4683 |
t2p_compose_pdf_page_orient(boxp, t2p->tiff_orientation); |
| 4684 |
} |
| 4685 |
|
| 4686 |
} |
| 4687 |
|
| 4688 |
return; |
| 4689 |
} |
| 4690 |
|
| 4691 |
static |
| 4692 |
void t2p_compose_pdf_page_orient(T2P_BOX* boxp, uint16 orientation){ |
| 4693 |
|
| 4694 |
float m1[9]; |
| 4695 |
float f=0.0; |
| 4696 |
|
| 4697 |
if( boxp->x1 > boxp->x2){ |
| 4698 |
f=boxp->x1; |
| 4699 |
boxp->x1=boxp->x2; |
| 4700 |
boxp->x2 = f; |
| 4701 |
} |
| 4702 |
if( boxp->y1 > boxp->y2){ |
| 4703 |
f=boxp->y1; |
| 4704 |
boxp->y1=boxp->y2; |
| 4705 |
boxp->y2 = f; |
| 4706 |
} |
| 4707 |
boxp->mat[0]=m1[0]=boxp->x2-boxp->x1; |
| 4708 |
boxp->mat[1]=m1[1]=0.0; |
| 4709 |
boxp->mat[2]=m1[2]=0.0; |
| 4710 |
boxp->mat[3]=m1[3]=0.0; |
| 4711 |
boxp->mat[4]=m1[4]=boxp->y2-boxp->y1; |
| 4712 |
boxp->mat[5]=m1[5]=0.0; |
| 4713 |
boxp->mat[6]=m1[6]=boxp->x1; |
| 4714 |
boxp->mat[7]=m1[7]=boxp->y1; |
| 4715 |
boxp->mat[8]=m1[8]=1.0; |
| 4716 |
switch(orientation){ |
| 4717 |
case 0: |
| 4718 |
case 1: |
| 4719 |
break; |
| 4720 |
case 2: |
| 4721 |
boxp->mat[0]=0.0F-m1[0]; |
| 4722 |
boxp->mat[6]+=m1[0]; |
| 4723 |
break; |
| 4724 |
case 3: |
| 4725 |
boxp->mat[0]=0.0F-m1[0]; |
| 4726 |
boxp->mat[4]=0.0F-m1[4]; |
| 4727 |
boxp->mat[6]+=m1[0]; |
| 4728 |
boxp->mat[7]+=m1[4]; |
| 4729 |
break; |
| 4730 |
case 4: |
| 4731 |
boxp->mat[4]=0.0F-m1[4]; |
| 4732 |
boxp->mat[7]+=m1[4]; |
| 4733 |
break; |
| 4734 |
case 5: |
| 4735 |
boxp->mat[0]=0.0F; |
| 4736 |
boxp->mat[1]=0.0F-m1[0]; |
| 4737 |
boxp->mat[3]=0.0F-m1[4]; |
| 4738 |
boxp->mat[4]=0.0F; |
| 4739 |
boxp->mat[6]+=m1[4]; |
| 4740 |
boxp->mat[7]+=m1[0]; |
| 4741 |
break; |
| 4742 |
case 6: |
| 4743 |
boxp->mat[0]=0.0F; |
| 4744 |
boxp->mat[1]=0.0F-m1[0]; |
| 4745 |
boxp->mat[3]=m1[4]; |
| 4746 |
boxp->mat[4]=0.0F; |
| 4747 |
boxp->mat[7]+=m1[0]; |
| 4748 |
break; |
| 4749 |
case 7: |
| 4750 |
boxp->mat[0]=0.0F; |
| 4751 |
boxp->mat[1]=m1[0]; |
| 4752 |
boxp->mat[3]=m1[4]; |
| 4753 |
boxp->mat[4]=0.0F; |
| 4754 |
break; |
| 4755 |
case 8: |
| 4756 |
boxp->mat[0]=0.0F; |
| 4757 |
boxp->mat[1]=m1[0]; |
| 4758 |
boxp->mat[3]=0.0F-m1[4]; |
| 4759 |
boxp->mat[4]=0.0F; |
| 4760 |
boxp->mat[6]+=m1[4]; |
| 4761 |
break; |
| 4762 |
} |
| 4763 |
|
| 4764 |
return; |
| 4765 |
} |
| 4766 |
|
| 4767 |
static |
| 4768 |
void t2p_compose_pdf_page_orient_flip(T2P_BOX* boxp, uint16 orientation){ |
| 4769 |
|
| 4770 |
float m1[9]; |
| 4771 |
float f=0.0; |
| 4772 |
|
| 4773 |
if( boxp->x1 > boxp->x2){ |
| 4774 |
f=boxp->x1; |
| 4775 |
boxp->x1=boxp->x2; |
| 4776 |
boxp->x2 = f; |
| 4777 |
} |
| 4778 |
if( boxp->y1 > boxp->y2){ |
| 4779 |
f=boxp->y1; |
| 4780 |
boxp->y1=boxp->y2; |
| 4781 |
boxp->y2 = f; |
| 4782 |
} |
| 4783 |
boxp->mat[0]=m1[0]=boxp->x2-boxp->x1; |
| 4784 |
boxp->mat[1]=m1[1]=0.0F; |
| 4785 |
boxp->mat[2]=m1[2]=0.0F; |
| 4786 |
boxp->mat[3]=m1[3]=0.0F; |
| 4787 |
boxp->mat[4]=m1[4]=boxp->y2-boxp->y1; |
| 4788 |
boxp->mat[5]=m1[5]=0.0F; |
| 4789 |
boxp->mat[6]=m1[6]=boxp->x1; |
| 4790 |
boxp->mat[7]=m1[7]=boxp->y1; |
| 4791 |
boxp->mat[8]=m1[8]=1.0F; |
| 4792 |
switch(orientation){ |
| 4793 |
case 5: |
| 4794 |
boxp->mat[0]=0.0F; |
| 4795 |
boxp->mat[1]=0.0F-m1[4]; |
| 4796 |
boxp->mat[3]=0.0F-m1[0]; |
| 4797 |
boxp->mat[4]=0.0F; |
| 4798 |
boxp->mat[6]+=m1[0]; |
| 4799 |
boxp->mat[7]+=m1[4]; |
| 4800 |
break; |
| 4801 |
case 6: |
| 4802 |
boxp->mat[0]=0.0F; |
| 4803 |
boxp->mat[1]=0.0F-m1[4]; |
| 4804 |
boxp->mat[3]=m1[0]; |
| 4805 |
boxp->mat[4]=0.0F; |
| 4806 |
boxp->mat[7]+=m1[4]; |
| 4807 |
break; |
| 4808 |
case 7: |
| 4809 |
boxp->mat[0]=0.0F; |
| 4810 |
boxp->mat[1]=m1[4]; |
| 4811 |
boxp->mat[3]=m1[0]; |
| 4812 |
boxp->mat[4]=0.0F; |
| 4813 |
break; |
| 4814 |
case 8: |
| 4815 |
boxp->mat[0]=0.0F; |
| 4816 |
boxp->mat[1]=m1[4]; |
| 4817 |
boxp->mat[3]=0.0F-m1[0]; |
| 4818 |
boxp->mat[4]=0.0F; |
| 4819 |
boxp->mat[6]+=m1[0]; |
| 4820 |
break; |
| 4821 |
} |
| 4822 |
|
| 4823 |
return; |
| 4824 |
} |
| 4825 |
|
| 4826 |
/* |
| 4827 |
This function writes a PDF Contents stream to output. |
| 4828 |
*/ |
| 4829 |
|
| 4830 |
static |
| 4831 |
tsize_t t2p_write_pdf_page_content_stream(T2P* t2p, TIFF* output){ |
| 4832 |
|
| 4833 |
tsize_t written=0; |
| 4834 |
ttile_t i=0; |
| 4835 |
char buffer[512]; |
| 4836 |
int buflen=0; |
| 4837 |
T2P_BOX box; |
| 4838 |
|
| 4839 |
if(t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount>0){ |
| 4840 |
for(i=0;i<t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount; i++){ |
| 4841 |
box=t2p->tiff_tiles[t2p->pdf_page].tiles_tiles[i].tile_box; |
| 4842 |
buflen=snprintf(buffer, sizeof(buffer), |
| 4843 |
"q %s %.4f %.4f %.4f %.4f %.4f %.4f cm /Im%d_%ld Do Q\n", |
| 4844 |
t2p->tiff_transferfunctioncount?"/GS1 gs ":"", |
| 4845 |
box.mat[0], |
| 4846 |
box.mat[1], |
| 4847 |
box.mat[3], |
| 4848 |
box.mat[4], |
| 4849 |
box.mat[6], |
| 4850 |
box.mat[7], |
| 4851 |
t2p->pdf_page + 1, |
| 4852 |
(long)(i + 1)); |
| 4853 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4854 |
written += t2p_write_pdf_stream(buffer, buflen, output); |
| 4855 |
} |
| 4856 |
} else { |
| 4857 |
box=t2p->pdf_imagebox; |
| 4858 |
buflen=snprintf(buffer, sizeof(buffer), |
| 4859 |
"q %s %.4f %.4f %.4f %.4f %.4f %.4f cm /Im%d Do Q\n", |
| 4860 |
t2p->tiff_transferfunctioncount?"/GS1 gs ":"", |
| 4861 |
box.mat[0], |
| 4862 |
box.mat[1], |
| 4863 |
box.mat[3], |
| 4864 |
box.mat[4], |
| 4865 |
box.mat[6], |
| 4866 |
box.mat[7], |
| 4867 |
t2p->pdf_page+1); |
| 4868 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4869 |
written += t2p_write_pdf_stream(buffer, buflen, output); |
| 4870 |
} |
| 4871 |
|
| 4872 |
return(written); |
| 4873 |
} |
| 4874 |
|
| 4875 |
/* |
| 4876 |
This function writes a PDF Image XObject stream dictionary to output. |
| 4877 |
*/ |
| 4878 |
|
| 4879 |
static |
| 4880 |
tsize_t t2p_write_pdf_xobject_stream_dict(ttile_t tile, |
| 4881 |
T2P* t2p, |
| 4882 |
TIFF* output){ |
| 4883 |
|
| 4884 |
tsize_t written=0; |
| 4885 |
char buffer[32]; |
| 4886 |
int buflen=0; |
| 4887 |
|
| 4888 |
written += t2p_write_pdf_stream_dict(0, t2p->pdf_xrefcount+1, output); |
| 4889 |
written += t2pWriteFile(output, |
| 4890 |
(tdata_t) "/Type /XObject \n/Subtype /Image \n/Name /Im", |
| 4891 |
42); |
| 4892 |
buflen=snprintf(buffer, sizeof(buffer), "%u", t2p->pdf_page+1); |
| 4893 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4894 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 4895 |
if(tile != 0){ |
| 4896 |
written += t2pWriteFile(output, (tdata_t) "_", 1); |
| 4897 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", (unsigned long)tile); |
| 4898 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4899 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 4900 |
} |
| 4901 |
written += t2pWriteFile(output, (tdata_t) "\n/Width ", 8); |
| 4902 |
if(tile==0){ |
| 4903 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", (unsigned long)t2p->tiff_width); |
| 4904 |
} else { |
| 4905 |
if(t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile-1)!=0){ |
| 4906 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", |
| 4907 |
(unsigned long)t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilewidth); |
| 4908 |
} else { |
| 4909 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", |
| 4910 |
(unsigned long)t2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth); |
| 4911 |
} |
| 4912 |
} |
| 4913 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4914 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 4915 |
written += t2pWriteFile(output, (tdata_t) "\n/Height ", 9); |
| 4916 |
if(tile==0){ |
| 4917 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", (unsigned long)t2p->tiff_length); |
| 4918 |
} else { |
| 4919 |
if(t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile-1)!=0){ |
| 4920 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", |
| 4921 |
(unsigned long)t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilelength); |
| 4922 |
} else { |
| 4923 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", |
| 4924 |
(unsigned long)t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength); |
| 4925 |
} |
| 4926 |
} |
| 4927 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4928 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 4929 |
written += t2pWriteFile(output, (tdata_t) "\n/BitsPerComponent ", 19); |
| 4930 |
buflen=snprintf(buffer, sizeof(buffer), "%u", t2p->tiff_bitspersample); |
| 4931 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4932 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 4933 |
written += t2pWriteFile(output, (tdata_t) "\n/ColorSpace ", 13); |
| 4934 |
written += t2p_write_pdf_xobject_cs(t2p, output); |
| 4935 |
if (t2p->pdf_image_interpolate) |
| 4936 |
written += t2pWriteFile(output, |
| 4937 |
(tdata_t) "\n/Interpolate true", 18); |
| 4938 |
if( (t2p->pdf_switchdecode != 0) |
| 4939 |
#ifdef CCITT_SUPPORT |
| 4940 |
&& ! (t2p->pdf_colorspace & T2P_CS_BILEVEL |
| 4941 |
&& t2p->pdf_compression == T2P_COMPRESS_G4) |
| 4942 |
#endif |
| 4943 |
){ |
| 4944 |
written += t2p_write_pdf_xobject_decode(t2p, output); |
| 4945 |
} |
| 4946 |
written += t2p_write_pdf_xobject_stream_filter(tile, t2p, output); |
| 4947 |
|
| 4948 |
return(written); |
| 4949 |
} |
| 4950 |
|
| 4951 |
/* |
| 4952 |
* This function writes a PDF Image XObject Colorspace name to output. |
| 4953 |
*/ |
| 4954 |
|
| 4955 |
|
| 4956 |
static |
| 4957 |
tsize_t t2p_write_pdf_xobject_cs(T2P* t2p, TIFF* output){ |
| 4958 |
|
| 4959 |
tsize_t written=0; |
| 4960 |
char buffer[128]; |
| 4961 |
int buflen=0; |
| 4962 |
|
| 4963 |
float X_W=1.0; |
| 4964 |
float Y_W=1.0; |
| 4965 |
float Z_W=1.0; |
| 4966 |
|
| 4967 |
if( (t2p->pdf_colorspace & T2P_CS_ICCBASED) != 0){ |
| 4968 |
written += t2p_write_pdf_xobject_icccs(t2p, output); |
| 4969 |
return(written); |
| 4970 |
} |
| 4971 |
if( (t2p->pdf_colorspace & T2P_CS_PALETTE) != 0){ |
| 4972 |
written += t2pWriteFile(output, (tdata_t) "[ /Indexed ", 11); |
| 4973 |
t2p->pdf_colorspace ^= T2P_CS_PALETTE; |
| 4974 |
written += t2p_write_pdf_xobject_cs(t2p, output); |
| 4975 |
t2p->pdf_colorspace |= T2P_CS_PALETTE; |
| 4976 |
buflen=snprintf(buffer, sizeof(buffer), "%u", (0x0001 << t2p->tiff_bitspersample)-1 ); |
| 4977 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4978 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 4979 |
written += t2pWriteFile(output, (tdata_t) " ", 1); |
| 4980 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", (unsigned long)t2p->pdf_palettecs ); |
| 4981 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4982 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 4983 |
written += t2pWriteFile(output, (tdata_t) " 0 R ]\n", 7); |
| 4984 |
return(written); |
| 4985 |
} |
| 4986 |
if(t2p->pdf_colorspace & T2P_CS_BILEVEL){ |
| 4987 |
written += t2pWriteFile(output, (tdata_t) "/DeviceGray \n", 13); |
| 4988 |
} |
| 4989 |
if(t2p->pdf_colorspace & T2P_CS_GRAY){ |
| 4990 |
if(t2p->pdf_colorspace & T2P_CS_CALGRAY){ |
| 4991 |
written += t2p_write_pdf_xobject_calcs(t2p, output); |
| 4992 |
} else { |
| 4993 |
written += t2pWriteFile(output, (tdata_t) "/DeviceGray \n", 13); |
| 4994 |
} |
| 4995 |
} |
| 4996 |
if(t2p->pdf_colorspace & T2P_CS_RGB){ |
| 4997 |
if(t2p->pdf_colorspace & T2P_CS_CALRGB){ |
| 4998 |
written += t2p_write_pdf_xobject_calcs(t2p, output); |
| 4999 |
} else { |
| 5000 |
written += t2pWriteFile(output, (tdata_t) "/DeviceRGB \n", 12); |
| 5001 |
} |
| 5002 |
} |
| 5003 |
if(t2p->pdf_colorspace & T2P_CS_CMYK){ |
| 5004 |
written += t2pWriteFile(output, (tdata_t) "/DeviceCMYK \n", 13); |
| 5005 |
} |
| 5006 |
if(t2p->pdf_colorspace & T2P_CS_LAB){ |
| 5007 |
written += t2pWriteFile(output, (tdata_t) "[/Lab << \n", 10); |
| 5008 |
written += t2pWriteFile(output, (tdata_t) "/WhitePoint ", 12); |
| 5009 |
X_W = t2p->tiff_whitechromaticities[0]; |
| 5010 |
Y_W = t2p->tiff_whitechromaticities[1]; |
| 5011 |
Z_W = 1.0F - (X_W + Y_W); |
| 5012 |
X_W /= Y_W; |
| 5013 |
Z_W /= Y_W; |
| 5014 |
Y_W = 1.0F; |
| 5015 |
buflen=snprintf(buffer, sizeof(buffer), "[%.4f %.4f %.4f] \n", X_W, Y_W, Z_W); |
| 5016 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5017 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5018 |
written += t2pWriteFile(output, (tdata_t) "/Range ", 7); |
| 5019 |
buflen=snprintf(buffer, sizeof(buffer), "[%d %d %d %d] \n", |
| 5020 |
t2p->pdf_labrange[0], |
| 5021 |
t2p->pdf_labrange[1], |
| 5022 |
t2p->pdf_labrange[2], |
| 5023 |
t2p->pdf_labrange[3]); |
| 5024 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5025 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5026 |
written += t2pWriteFile(output, (tdata_t) ">>] \n", 5); |
| 5027 |
|
| 5028 |
} |
| 5029 |
|
| 5030 |
return(written); |
| 5031 |
} |
| 5032 |
|
| 5033 |
static |
| 5034 |
tsize_t t2p_write_pdf_transfer(T2P* t2p, TIFF* output){ |
| 5035 |
|
| 5036 |
tsize_t written=0; |
| 5037 |
char buffer[32]; |
| 5038 |
int buflen=0; |
| 5039 |
|
| 5040 |
written += t2pWriteFile(output, (tdata_t) "<< /Type /ExtGState \n/TR ", 25); |
| 5041 |
if(t2p->tiff_transferfunctioncount == 1){ |
| 5042 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", |
| 5043 |
(unsigned long)(t2p->pdf_xrefcount + 1)); |
| 5044 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5045 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5046 |
written += t2pWriteFile(output, (tdata_t) " 0 R ", 5); |
| 5047 |
} else { |
| 5048 |
written += t2pWriteFile(output, (tdata_t) "[ ", 2); |
| 5049 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", |
| 5050 |
(unsigned long)(t2p->pdf_xrefcount + 1)); |
| 5051 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5052 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5053 |
written += t2pWriteFile(output, (tdata_t) " 0 R ", 5); |
| 5054 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", |
| 5055 |
(unsigned long)(t2p->pdf_xrefcount + 2)); |
| 5056 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5057 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5058 |
written += t2pWriteFile(output, (tdata_t) " 0 R ", 5); |
| 5059 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", |
| 5060 |
(unsigned long)(t2p->pdf_xrefcount + 3)); |
| 5061 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5062 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5063 |
written += t2pWriteFile(output, (tdata_t) " 0 R ", 5); |
| 5064 |
written += t2pWriteFile(output, (tdata_t) "/Identity ] ", 12); |
| 5065 |
} |
| 5066 |
|
| 5067 |
written += t2pWriteFile(output, (tdata_t) " >> \n", 5); |
| 5068 |
|
| 5069 |
return(written); |
| 5070 |
} |
| 5071 |
|
| 5072 |
static |
| 5073 |
tsize_t t2p_write_pdf_transfer_dict(T2P* t2p, TIFF* output, uint16 i){ |
| 5074 |
|
| 5075 |
tsize_t written=0; |
| 5076 |
char buffer[32]; |
| 5077 |
int buflen=0; |
| 5078 |
(void)i; /* XXX */ |
| 5079 |
|
| 5080 |
written += t2pWriteFile(output, (tdata_t) "/FunctionType 0 \n", 17); |
| 5081 |
written += t2pWriteFile(output, (tdata_t) "/Domain [0.0 1.0] \n", 19); |
| 5082 |
written += t2pWriteFile(output, (tdata_t) "/Range [0.0 1.0] \n", 18); |
| 5083 |
buflen=snprintf(buffer, sizeof(buffer), "/Size [%u] \n", (1<<t2p->tiff_bitspersample)); |
| 5084 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5085 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5086 |
written += t2pWriteFile(output, (tdata_t) "/BitsPerSample 16 \n", 19); |
| 5087 |
written += t2p_write_pdf_stream_dict(((tsize_t)1)<<(t2p->tiff_bitspersample+1), 0, output); |
| 5088 |
|
| 5089 |
return(written); |
| 5090 |
} |
| 5091 |
|
| 5092 |
static |
| 5093 |
tsize_t t2p_write_pdf_transfer_stream(T2P* t2p, TIFF* output, uint16 i){ |
| 5094 |
|
| 5095 |
tsize_t written=0; |
| 5096 |
|
| 5097 |
written += t2p_write_pdf_stream( |
| 5098 |
t2p->tiff_transferfunction[i], |
| 5099 |
(((tsize_t)1)<<(t2p->tiff_bitspersample+1)), |
| 5100 |
output); |
| 5101 |
|
| 5102 |
return(written); |
| 5103 |
} |
| 5104 |
|
| 5105 |
/* |
| 5106 |
This function writes a PDF Image XObject Colorspace array to output. |
| 5107 |
*/ |
| 5108 |
|
| 5109 |
static |
| 5110 |
tsize_t t2p_write_pdf_xobject_calcs(T2P* t2p, TIFF* output){ |
| 5111 |
|
| 5112 |
tsize_t written=0; |
| 5113 |
char buffer[256]; |
| 5114 |
int buflen=0; |
| 5115 |
|
| 5116 |
float X_W=0.0; |
| 5117 |
float Y_W=0.0; |
| 5118 |
float Z_W=0.0; |
| 5119 |
float X_R=0.0; |
| 5120 |
float Y_R=0.0; |
| 5121 |
float Z_R=0.0; |
| 5122 |
float X_G=0.0; |
| 5123 |
float Y_G=0.0; |
| 5124 |
float Z_G=0.0; |
| 5125 |
float X_B=0.0; |
| 5126 |
float Y_B=0.0; |
| 5127 |
float Z_B=0.0; |
| 5128 |
float x_w=0.0; |
| 5129 |
float y_w=0.0; |
| 5130 |
float z_w=0.0; |
| 5131 |
float x_r=0.0; |
| 5132 |
float y_r=0.0; |
| 5133 |
float x_g=0.0; |
| 5134 |
float y_g=0.0; |
| 5135 |
float x_b=0.0; |
| 5136 |
float y_b=0.0; |
| 5137 |
float R=1.0; |
| 5138 |
float G=1.0; |
| 5139 |
float B=1.0; |
| 5140 |
|
| 5141 |
written += t2pWriteFile(output, (tdata_t) "[", 1); |
| 5142 |
if(t2p->pdf_colorspace & T2P_CS_CALGRAY){ |
| 5143 |
written += t2pWriteFile(output, (tdata_t) "/CalGray ", 9); |
| 5144 |
X_W = t2p->tiff_whitechromaticities[0]; |
| 5145 |
Y_W = t2p->tiff_whitechromaticities[1]; |
| 5146 |
Z_W = 1.0F - (X_W + Y_W); |
| 5147 |
X_W /= Y_W; |
| 5148 |
Z_W /= Y_W; |
| 5149 |
Y_W = 1.0F; |
| 5150 |
} |
| 5151 |
if(t2p->pdf_colorspace & T2P_CS_CALRGB){ |
| 5152 |
written += t2pWriteFile(output, (tdata_t) "/CalRGB ", 8); |
| 5153 |
x_w = t2p->tiff_whitechromaticities[0]; |
| 5154 |
y_w = t2p->tiff_whitechromaticities[1]; |
| 5155 |
x_r = t2p->tiff_primarychromaticities[0]; |
| 5156 |
y_r = t2p->tiff_primarychromaticities[1]; |
| 5157 |
x_g = t2p->tiff_primarychromaticities[2]; |
| 5158 |
y_g = t2p->tiff_primarychromaticities[3]; |
| 5159 |
x_b = t2p->tiff_primarychromaticities[4]; |
| 5160 |
y_b = t2p->tiff_primarychromaticities[5]; |
| 5161 |
z_w = y_w * ((x_g - x_b)*y_r - (x_r-x_b)*y_g + (x_r-x_g)*y_b); |
| 5162 |
Y_R = (y_r/R) * ((x_g-x_b)*y_w - (x_w-x_b)*y_g + (x_w-x_g)*y_b) / z_w; |
| 5163 |
X_R = Y_R * x_r / y_r; |
| 5164 |
Z_R = Y_R * (((1-x_r)/y_r)-1); |
| 5165 |
Y_G = ((0.0F-(y_g))/G) * ((x_r-x_b)*y_w - (x_w-x_b)*y_r + (x_w-x_r)*y_b) / z_w; |
| 5166 |
X_G = Y_G * x_g / y_g; |
| 5167 |
Z_G = Y_G * (((1-x_g)/y_g)-1); |
| 5168 |
Y_B = (y_b/B) * ((x_r-x_g)*y_w - (x_w-x_g)*y_r + (x_w-x_r)*y_g) / z_w; |
| 5169 |
X_B = Y_B * x_b / y_b; |
| 5170 |
Z_B = Y_B * (((1-x_b)/y_b)-1); |
| 5171 |
X_W = (X_R * R) + (X_G * G) + (X_B * B); |
| 5172 |
Y_W = (Y_R * R) + (Y_G * G) + (Y_B * B); |
| 5173 |
Z_W = (Z_R * R) + (Z_G * G) + (Z_B * B); |
| 5174 |
X_W /= Y_W; |
| 5175 |
Z_W /= Y_W; |
| 5176 |
Y_W = 1.0; |
| 5177 |
} |
| 5178 |
written += t2pWriteFile(output, (tdata_t) "<< \n", 4); |
| 5179 |
if(t2p->pdf_colorspace & T2P_CS_CALGRAY){ |
| 5180 |
written += t2pWriteFile(output, (tdata_t) "/WhitePoint ", 12); |
| 5181 |
buflen=snprintf(buffer, sizeof(buffer), "[%.4f %.4f %.4f] \n", X_W, Y_W, Z_W); |
| 5182 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5183 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5184 |
written += t2pWriteFile(output, (tdata_t) "/Gamma 2.2 \n", 12); |
| 5185 |
} |
| 5186 |
if(t2p->pdf_colorspace & T2P_CS_CALRGB){ |
| 5187 |
written += t2pWriteFile(output, (tdata_t) "/WhitePoint ", 12); |
| 5188 |
buflen=snprintf(buffer, sizeof(buffer), "[%.4f %.4f %.4f] \n", X_W, Y_W, Z_W); |
| 5189 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5190 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5191 |
written += t2pWriteFile(output, (tdata_t) "/Matrix ", 8); |
| 5192 |
buflen=snprintf(buffer, sizeof(buffer), "[%.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f] \n", |
| 5193 |
X_R, Y_R, Z_R, |
| 5194 |
X_G, Y_G, Z_G, |
| 5195 |
X_B, Y_B, Z_B); |
| 5196 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5197 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5198 |
written += t2pWriteFile(output, (tdata_t) "/Gamma [2.2 2.2 2.2] \n", 22); |
| 5199 |
} |
| 5200 |
written += t2pWriteFile(output, (tdata_t) ">>] \n", 5); |
| 5201 |
|
| 5202 |
return(written); |
| 5203 |
} |
| 5204 |
|
| 5205 |
/* |
| 5206 |
This function writes a PDF Image XObject Colorspace array to output. |
| 5207 |
*/ |
| 5208 |
|
| 5209 |
static |
| 5210 |
tsize_t t2p_write_pdf_xobject_icccs(T2P* t2p, TIFF* output){ |
| 5211 |
|
| 5212 |
tsize_t written=0; |
| 5213 |
char buffer[32]; |
| 5214 |
int buflen=0; |
| 5215 |
|
| 5216 |
written += t2pWriteFile(output, (tdata_t) "[/ICCBased ", 11); |
| 5217 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", (unsigned long)t2p->pdf_icccs); |
| 5218 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5219 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5220 |
written += t2pWriteFile(output, (tdata_t) " 0 R] \n", 7); |
| 5221 |
|
| 5222 |
return(written); |
| 5223 |
} |
| 5224 |
|
| 5225 |
static |
| 5226 |
tsize_t t2p_write_pdf_xobject_icccs_dict(T2P* t2p, TIFF* output){ |
| 5227 |
|
| 5228 |
tsize_t written=0; |
| 5229 |
char buffer[32]; |
| 5230 |
int buflen=0; |
| 5231 |
|
| 5232 |
written += t2pWriteFile(output, (tdata_t) "/N ", 3); |
| 5233 |
buflen=snprintf(buffer, sizeof(buffer), "%u \n", t2p->tiff_samplesperpixel); |
| 5234 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5235 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5236 |
written += t2pWriteFile(output, (tdata_t) "/Alternate ", 11); |
| 5237 |
t2p->pdf_colorspace ^= T2P_CS_ICCBASED; |
| 5238 |
written += t2p_write_pdf_xobject_cs(t2p, output); |
| 5239 |
t2p->pdf_colorspace |= T2P_CS_ICCBASED; |
| 5240 |
written += t2p_write_pdf_stream_dict(t2p->tiff_iccprofilelength, 0, output); |
| 5241 |
|
| 5242 |
return(written); |
| 5243 |
} |
| 5244 |
|
| 5245 |
static |
| 5246 |
tsize_t t2p_write_pdf_xobject_icccs_stream(T2P* t2p, TIFF* output){ |
| 5247 |
|
| 5248 |
tsize_t written=0; |
| 5249 |
|
| 5250 |
written += t2p_write_pdf_stream( |
| 5251 |
(tdata_t) t2p->tiff_iccprofile, |
| 5252 |
(tsize_t) t2p->tiff_iccprofilelength, |
| 5253 |
output); |
| 5254 |
|
| 5255 |
return(written); |
| 5256 |
} |
| 5257 |
|
| 5258 |
/* |
| 5259 |
This function writes a palette stream for an indexed color space to output. |
| 5260 |
*/ |
| 5261 |
|
| 5262 |
static |
| 5263 |
tsize_t t2p_write_pdf_xobject_palettecs_stream(T2P* t2p, TIFF* output){ |
| 5264 |
|
| 5265 |
tsize_t written=0; |
| 5266 |
|
| 5267 |
written += t2p_write_pdf_stream( |
| 5268 |
(tdata_t) t2p->pdf_palette, |
| 5269 |
(tsize_t) t2p->pdf_palettesize, |
| 5270 |
output); |
| 5271 |
|
| 5272 |
return(written); |
| 5273 |
} |
| 5274 |
|
| 5275 |
/* |
| 5276 |
This function writes a PDF Image XObject Decode array to output. |
| 5277 |
*/ |
| 5278 |
|
| 5279 |
static |
| 5280 |
tsize_t t2p_write_pdf_xobject_decode(T2P* t2p, TIFF* output){ |
| 5281 |
|
| 5282 |
tsize_t written=0; |
| 5283 |
int i=0; |
| 5284 |
|
| 5285 |
written += t2pWriteFile(output, (tdata_t) "/Decode [ ", 10); |
| 5286 |
for (i=0;i<t2p->tiff_samplesperpixel;i++){ |
| 5287 |
written += t2pWriteFile(output, (tdata_t) "1 0 ", 4); |
| 5288 |
} |
| 5289 |
written += t2pWriteFile(output, (tdata_t) "]\n", 2); |
| 5290 |
|
| 5291 |
return(written); |
| 5292 |
} |
| 5293 |
|
| 5294 |
/* |
| 5295 |
This function writes a PDF Image XObject stream filter name and parameters to |
| 5296 |
output. |
| 5297 |
*/ |
| 5298 |
|
| 5299 |
static |
| 5300 |
tsize_t t2p_write_pdf_xobject_stream_filter(ttile_t tile, T2P* t2p, TIFF* output){ |
| 5301 |
|
| 5302 |
tsize_t written=0; |
| 5303 |
char buffer[32]; |
| 5304 |
int buflen=0; |
| 5305 |
|
| 5306 |
if(t2p->pdf_compression==T2P_COMPRESS_NONE){ |
| 5307 |
return(written); |
| 5308 |
} |
| 5309 |
written += t2pWriteFile(output, (tdata_t) "/Filter ", 8); |
| 5310 |
switch(t2p->pdf_compression){ |
| 5311 |
#ifdef CCITT_SUPPORT |
| 5312 |
case T2P_COMPRESS_G4: |
| 5313 |
written += t2pWriteFile(output, (tdata_t) "/CCITTFaxDecode ", 16); |
| 5314 |
written += t2pWriteFile(output, (tdata_t) "/DecodeParms ", 13); |
| 5315 |
written += t2pWriteFile(output, (tdata_t) "<< /K -1 ", 9); |
| 5316 |
if(tile==0){ |
| 5317 |
written += t2pWriteFile(output, (tdata_t) "/Columns ", 9); |
| 5318 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", |
| 5319 |
(unsigned long)t2p->tiff_width); |
| 5320 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5321 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5322 |
written += t2pWriteFile(output, (tdata_t) " /Rows ", 7); |
| 5323 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", |
| 5324 |
(unsigned long)t2p->tiff_length); |
| 5325 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5326 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5327 |
} else { |
| 5328 |
if(t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile-1)==0){ |
| 5329 |
written += t2pWriteFile(output, (tdata_t) "/Columns ", 9); |
| 5330 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", |
| 5331 |
(unsigned long)t2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth); |
| 5332 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5333 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5334 |
} else { |
| 5335 |
written += t2pWriteFile(output, (tdata_t) "/Columns ", 9); |
| 5336 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", |
| 5337 |
(unsigned long)t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilewidth); |
| 5338 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5339 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5340 |
} |
| 5341 |
if(t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile-1)==0){ |
| 5342 |
written += t2pWriteFile(output, (tdata_t) " /Rows ", 7); |
| 5343 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", |
| 5344 |
(unsigned long)t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength); |
| 5345 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5346 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5347 |
} else { |
| 5348 |
written += t2pWriteFile(output, (tdata_t) " /Rows ", 7); |
| 5349 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", |
| 5350 |
(unsigned long)t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilelength); |
| 5351 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5352 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5353 |
} |
| 5354 |
} |
| 5355 |
if(t2p->pdf_switchdecode == 0){ |
| 5356 |
written += t2pWriteFile(output, (tdata_t) " /BlackIs1 true ", 16); |
| 5357 |
} |
| 5358 |
written += t2pWriteFile(output, (tdata_t) ">>\n", 3); |
| 5359 |
break; |
| 5360 |
#endif |
| 5361 |
#ifdef JPEG_SUPPORT |
| 5362 |
case T2P_COMPRESS_JPEG: |
| 5363 |
written += t2pWriteFile(output, (tdata_t) "/DCTDecode ", 11); |
| 5364 |
|
| 5365 |
if(t2p->tiff_photometric != PHOTOMETRIC_YCBCR) { |
| 5366 |
written += t2pWriteFile(output, (tdata_t) "/DecodeParms ", 13); |
| 5367 |
written += t2pWriteFile(output, (tdata_t) "<< /ColorTransform 1 >>\n", 24); |
| 5368 |
} |
| 5369 |
break; |
| 5370 |
#endif |
| 5371 |
#ifdef ZIP_SUPPORT |
| 5372 |
case T2P_COMPRESS_ZIP: |
| 5373 |
written += t2pWriteFile(output, (tdata_t) "/FlateDecode ", 13); |
| 5374 |
if(t2p->pdf_compressionquality%100){ |
| 5375 |
written += t2pWriteFile(output, (tdata_t) "/DecodeParms ", 13); |
| 5376 |
written += t2pWriteFile(output, (tdata_t) "<< /Predictor ", 14); |
| 5377 |
buflen=snprintf(buffer, sizeof(buffer), "%u", t2p->pdf_compressionquality%100); |
| 5378 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5379 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5380 |
written += t2pWriteFile(output, (tdata_t) " /Columns ", 10); |
| 5381 |
buflen = snprintf(buffer, sizeof(buffer), "%lu", |
| 5382 |
(unsigned long)t2p->tiff_width); |
| 5383 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5384 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5385 |
written += t2pWriteFile(output, (tdata_t) " /Colors ", 9); |
| 5386 |
buflen=snprintf(buffer, sizeof(buffer), "%u", t2p->tiff_samplesperpixel); |
| 5387 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5388 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5389 |
written += t2pWriteFile(output, (tdata_t) " /BitsPerComponent ", 19); |
| 5390 |
buflen=snprintf(buffer, sizeof(buffer), "%u", t2p->tiff_bitspersample); |
| 5391 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5392 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5393 |
written += t2pWriteFile(output, (tdata_t) ">>\n", 3); |
| 5394 |
} |
| 5395 |
break; |
| 5396 |
#endif |
| 5397 |
default: |
| 5398 |
break; |
| 5399 |
} |
| 5400 |
|
| 5401 |
return(written); |
| 5402 |
} |
| 5403 |
|
| 5404 |
/* |
| 5405 |
This function writes a PDF xref table to output. |
| 5406 |
*/ |
| 5407 |
|
| 5408 |
static |
| 5409 |
tsize_t t2p_write_pdf_xreftable(T2P* t2p, TIFF* output){ |
| 5410 |
|
| 5411 |
tsize_t written=0; |
| 5412 |
char buffer[64]; |
| 5413 |
int buflen=0; |
| 5414 |
uint32 i=0; |
| 5415 |
|
| 5416 |
written += t2pWriteFile(output, (tdata_t) "xref\n0 ", 7); |
| 5417 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", (unsigned long)(t2p->pdf_xrefcount + 1)); |
| 5418 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5419 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5420 |
written += t2pWriteFile(output, (tdata_t) " \n0000000000 65535 f \n", 22); |
| 5421 |
for (i=0;i<t2p->pdf_xrefcount;i++){ |
| 5422 |
snprintf(buffer, sizeof(buffer), "%.10lu 00000 n \n", |
| 5423 |
(unsigned long)t2p->pdf_xrefoffsets[i]); |
| 5424 |
written += t2pWriteFile(output, (tdata_t) buffer, 20); |
| 5425 |
} |
| 5426 |
|
| 5427 |
return(written); |
| 5428 |
} |
| 5429 |
|
| 5430 |
/* |
| 5431 |
* This function writes a PDF trailer to output. |
| 5432 |
*/ |
| 5433 |
|
| 5434 |
static |
| 5435 |
tsize_t t2p_write_pdf_trailer(T2P* t2p, TIFF* output) |
| 5436 |
{ |
| 5437 |
|
| 5438 |
tsize_t written = 0; |
| 5439 |
char buffer[32]; |
| 5440 |
int buflen = 0; |
| 5441 |
size_t i = 0; |
| 5442 |
|
| 5443 |
for (i = 0; i < sizeof(t2p->pdf_fileid) - 8; i += 8) |
| 5444 |
snprintf(t2p->pdf_fileid + i, 9, "%.8X", rand()); |
| 5445 |
|
| 5446 |
written += t2pWriteFile(output, (tdata_t) "trailer\n<<\n/Size ", 17); |
| 5447 |
buflen = snprintf(buffer, sizeof(buffer), "%lu", (unsigned long)(t2p->pdf_xrefcount+1)); |
| 5448 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5449 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5450 |
written += t2pWriteFile(output, (tdata_t) "\n/Root ", 7); |
| 5451 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", (unsigned long)t2p->pdf_catalog); |
| 5452 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5453 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5454 |
written += t2pWriteFile(output, (tdata_t) " 0 R \n/Info ", 12); |
| 5455 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", (unsigned long)t2p->pdf_info); |
| 5456 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5457 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5458 |
written += t2pWriteFile(output, (tdata_t) " 0 R \n/ID[<", 11); |
| 5459 |
written += t2pWriteFile(output, (tdata_t) t2p->pdf_fileid, |
| 5460 |
sizeof(t2p->pdf_fileid) - 1); |
| 5461 |
written += t2pWriteFile(output, (tdata_t) "><", 2); |
| 5462 |
written += t2pWriteFile(output, (tdata_t) t2p->pdf_fileid, |
| 5463 |
sizeof(t2p->pdf_fileid) - 1); |
| 5464 |
written += t2pWriteFile(output, (tdata_t) ">]\n>>\nstartxref\n", 16); |
| 5465 |
buflen=snprintf(buffer, sizeof(buffer), "%lu", (unsigned long)t2p->pdf_startxref); |
| 5466 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5467 |
written += t2pWriteFile(output, (tdata_t) buffer, buflen); |
| 5468 |
written += t2pWriteFile(output, (tdata_t) "\n%%EOF\n", 7); |
| 5469 |
|
| 5470 |
return(written); |
| 5471 |
} |
| 5472 |
|
| 5473 |
/* |
| 5474 |
|
| 5475 |
This function writes a PDF to a file given a pointer to a TIFF. |
| 5476 |
|
| 5477 |
The idea with using a TIFF* as output for a PDF file is that the file |
| 5478 |
can be created with TIFFClientOpen for memory-mapped use within the TIFF |
| 5479 |
library, and TIFFWriteEncodedStrip can be used to write compressed data to |
| 5480 |
the output. The output is not actually a TIFF file, it is a PDF file. |
| 5481 |
|
| 5482 |
This function uses only t2pWriteFile and TIFFWriteEncodedStrip to write to |
| 5483 |
the output TIFF file. When libtiff would otherwise be writing data to the |
| 5484 |
output file, the write procedure of the TIFF structure is replaced with an |
| 5485 |
empty implementation. |
| 5486 |
|
| 5487 |
The first argument to the function is an initialized and validated T2P |
| 5488 |
context struct pointer. |
| 5489 |
|
| 5490 |
The second argument to the function is the TIFF* that is the input that has |
| 5491 |
been opened for reading and no other functions have been called upon it. |
| 5492 |
|
| 5493 |
The third argument to the function is the TIFF* that is the output that has |
| 5494 |
been opened for writing. It has to be opened so that it hasn't written any |
| 5495 |
data to the output. If the output is seekable then it's OK to seek to the |
| 5496 |
beginning of the file. The function only writes to the output PDF and does |
| 5497 |
not seek. See the example usage in the main() function. |
| 5498 |
|
| 5499 |
TIFF* output = TIFFOpen("output.pdf", "w"); |
| 5500 |
assert(output != NULL); |
| 5501 |
|
| 5502 |
if(output->tif_seekproc != NULL){ |
| 5503 |
t2pSeekFile(output, (toff_t) 0, SEEK_SET); |
| 5504 |
} |
| 5505 |
|
| 5506 |
This function returns the file size of the output PDF file. On error it |
| 5507 |
returns zero and the t2p->t2p_error variable is set to T2P_ERR_ERROR. |
| 5508 |
|
| 5509 |
After this function completes, call t2p_free on t2p, TIFFClose on input, |
| 5510 |
and TIFFClose on output. |
| 5511 |
*/ |
| 5512 |
|
| 5513 |
static |
| 5514 |
tsize_t t2p_write_pdf(T2P* t2p, TIFF* input, TIFF* output){ |
| 5515 |
|
| 5516 |
tsize_t written=0; |
| 5517 |
ttile_t i2=0; |
| 5518 |
tsize_t streamlen=0; |
| 5519 |
uint16 i=0; |
| 5520 |
|
| 5521 |
t2p_read_tiff_init(t2p, input); |
| 5522 |
if(t2p->t2p_error!=T2P_ERR_OK){return(0);} |
| 5523 |
t2p->pdf_xrefoffsets= (uint32*) _TIFFmalloc(TIFFSafeMultiply(tmsize_t,t2p->pdf_xrefcount,sizeof(uint32)) ); |
| 5524 |
if(t2p->pdf_xrefoffsets==NULL){ |
| 5525 |
TIFFError( |
| 5526 |
TIFF2PDF_MODULE, |
| 5527 |
"Can't allocate %u bytes of memory for t2p_write_pdf", |
| 5528 |
(unsigned int) (t2p->pdf_xrefcount * sizeof(uint32)) ); |
| 5529 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 5530 |
return(written); |
| 5531 |
} |
| 5532 |
t2p->pdf_xrefcount=0; |
| 5533 |
t2p->pdf_catalog=1; |
| 5534 |
t2p->pdf_info=2; |
| 5535 |
t2p->pdf_pages=3; |
| 5536 |
written += t2p_write_pdf_header(t2p, output); |
| 5537 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; |
| 5538 |
t2p->pdf_catalog=t2p->pdf_xrefcount; |
| 5539 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 5540 |
written += t2p_write_pdf_catalog(t2p, output); |
| 5541 |
written += t2p_write_pdf_obj_end(output); |
| 5542 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; |
| 5543 |
t2p->pdf_info=t2p->pdf_xrefcount; |
| 5544 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 5545 |
written += t2p_write_pdf_info(t2p, input, output); |
| 5546 |
written += t2p_write_pdf_obj_end(output); |
| 5547 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; |
| 5548 |
t2p->pdf_pages=t2p->pdf_xrefcount; |
| 5549 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 5550 |
written += t2p_write_pdf_pages(t2p, output); |
| 5551 |
written += t2p_write_pdf_obj_end(output); |
| 5552 |
for(t2p->pdf_page=0;t2p->pdf_page<t2p->tiff_pagecount;t2p->pdf_page++){ |
| 5553 |
t2p_read_tiff_data(t2p, input); |
| 5554 |
if(t2p->t2p_error!=T2P_ERR_OK){return(0);} |
| 5555 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; |
| 5556 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 5557 |
written += t2p_write_pdf_page(t2p->pdf_xrefcount, t2p, output); |
| 5558 |
written += t2p_write_pdf_obj_end(output); |
| 5559 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; |
| 5560 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 5561 |
written += t2p_write_pdf_stream_dict_start(output); |
| 5562 |
written += t2p_write_pdf_stream_dict(0, t2p->pdf_xrefcount+1, output); |
| 5563 |
written += t2p_write_pdf_stream_dict_end(output); |
| 5564 |
written += t2p_write_pdf_stream_start(output); |
| 5565 |
streamlen=written; |
| 5566 |
written += t2p_write_pdf_page_content_stream(t2p, output); |
| 5567 |
streamlen=written-streamlen; |
| 5568 |
written += t2p_write_pdf_stream_end(output); |
| 5569 |
written += t2p_write_pdf_obj_end(output); |
| 5570 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; |
| 5571 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 5572 |
written += t2p_write_pdf_stream_length(streamlen, output); |
| 5573 |
written += t2p_write_pdf_obj_end(output); |
| 5574 |
if(t2p->tiff_transferfunctioncount != 0){ |
| 5575 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; |
| 5576 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 5577 |
written += t2p_write_pdf_transfer(t2p, output); |
| 5578 |
written += t2p_write_pdf_obj_end(output); |
| 5579 |
for(i=0; i < t2p->tiff_transferfunctioncount; i++){ |
| 5580 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; |
| 5581 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 5582 |
written += t2p_write_pdf_stream_dict_start(output); |
| 5583 |
written += t2p_write_pdf_transfer_dict(t2p, output, i); |
| 5584 |
written += t2p_write_pdf_stream_dict_end(output); |
| 5585 |
written += t2p_write_pdf_stream_start(output); |
| 5586 |
/* streamlen=written; */ /* value not used */ |
| 5587 |
written += t2p_write_pdf_transfer_stream(t2p, output, i); |
| 5588 |
/* streamlen=written-streamlen; */ /* value not used */ |
| 5589 |
written += t2p_write_pdf_stream_end(output); |
| 5590 |
written += t2p_write_pdf_obj_end(output); |
| 5591 |
} |
| 5592 |
} |
| 5593 |
if( (t2p->pdf_colorspace & T2P_CS_PALETTE) != 0){ |
| 5594 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; |
| 5595 |
t2p->pdf_palettecs=t2p->pdf_xrefcount; |
| 5596 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 5597 |
written += t2p_write_pdf_stream_dict_start(output); |
| 5598 |
written += t2p_write_pdf_stream_dict(t2p->pdf_palettesize, 0, output); |
| 5599 |
written += t2p_write_pdf_stream_dict_end(output); |
| 5600 |
written += t2p_write_pdf_stream_start(output); |
| 5601 |
/* streamlen=written; */ /* value not used */ |
| 5602 |
written += t2p_write_pdf_xobject_palettecs_stream(t2p, output); |
| 5603 |
/* streamlen=written-streamlen; */ /* value not used */ |
| 5604 |
written += t2p_write_pdf_stream_end(output); |
| 5605 |
written += t2p_write_pdf_obj_end(output); |
| 5606 |
} |
| 5607 |
if( (t2p->pdf_colorspace & T2P_CS_ICCBASED) != 0){ |
| 5608 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; |
| 5609 |
t2p->pdf_icccs=t2p->pdf_xrefcount; |
| 5610 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 5611 |
written += t2p_write_pdf_stream_dict_start(output); |
| 5612 |
written += t2p_write_pdf_xobject_icccs_dict(t2p, output); |
| 5613 |
written += t2p_write_pdf_stream_dict_end(output); |
| 5614 |
written += t2p_write_pdf_stream_start(output); |
| 5615 |
/* streamlen=written; */ /* value not used */ |
| 5616 |
written += t2p_write_pdf_xobject_icccs_stream(t2p, output); |
| 5617 |
/* streamlen=written-streamlen; */ /* value not used */ |
| 5618 |
written += t2p_write_pdf_stream_end(output); |
| 5619 |
written += t2p_write_pdf_obj_end(output); |
| 5620 |
} |
| 5621 |
if(t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount !=0){ |
| 5622 |
for(i2=0;i2<t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount;i2++){ |
| 5623 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; |
| 5624 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 5625 |
written += t2p_write_pdf_stream_dict_start(output); |
| 5626 |
written += t2p_write_pdf_xobject_stream_dict( |
| 5627 |
i2+1, |
| 5628 |
t2p, |
| 5629 |
output); |
| 5630 |
written += t2p_write_pdf_stream_dict_end(output); |
| 5631 |
written += t2p_write_pdf_stream_start(output); |
| 5632 |
streamlen=written; |
| 5633 |
t2p_read_tiff_size_tile(t2p, input, i2); |
| 5634 |
written += t2p_readwrite_pdf_image_tile(t2p, input, output, i2); |
| 5635 |
t2p_write_advance_directory(t2p, output); |
| 5636 |
if(t2p->t2p_error!=T2P_ERR_OK){return(0);} |
| 5637 |
streamlen=written-streamlen; |
| 5638 |
written += t2p_write_pdf_stream_end(output); |
| 5639 |
written += t2p_write_pdf_obj_end(output); |
| 5640 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; |
| 5641 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 5642 |
written += t2p_write_pdf_stream_length(streamlen, output); |
| 5643 |
written += t2p_write_pdf_obj_end(output); |
| 5644 |
} |
| 5645 |
} else { |
| 5646 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; |
| 5647 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 5648 |
written += t2p_write_pdf_stream_dict_start(output); |
| 5649 |
written += t2p_write_pdf_xobject_stream_dict( |
| 5650 |
0, |
| 5651 |
t2p, |
| 5652 |
output); |
| 5653 |
written += t2p_write_pdf_stream_dict_end(output); |
| 5654 |
written += t2p_write_pdf_stream_start(output); |
| 5655 |
streamlen=written; |
| 5656 |
t2p_read_tiff_size(t2p, input); |
| 5657 |
written += t2p_readwrite_pdf_image(t2p, input, output); |
| 5658 |
t2p_write_advance_directory(t2p, output); |
| 5659 |
if(t2p->t2p_error!=T2P_ERR_OK){return(0);} |
| 5660 |
streamlen=written-streamlen; |
| 5661 |
written += t2p_write_pdf_stream_end(output); |
| 5662 |
written += t2p_write_pdf_obj_end(output); |
| 5663 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written; |
| 5664 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 5665 |
written += t2p_write_pdf_stream_length(streamlen, output); |
| 5666 |
written += t2p_write_pdf_obj_end(output); |
| 5667 |
} |
| 5668 |
} |
| 5669 |
t2p->pdf_startxref = written; |
| 5670 |
written += t2p_write_pdf_xreftable(t2p, output); |
| 5671 |
written += t2p_write_pdf_trailer(t2p, output); |
| 5672 |
t2p_disable(output); |
| 5673 |
|
| 5674 |
return(written); |
| 5675 |
} |
| 5676 |
|
| 5677 |
/* vim: set ts=8 sts=8 sw=8 noet: */ |
| 5678 |
/* |
| 5679 |
* Local Variables: |
| 5680 |
* mode: c |
| 5681 |
* c-basic-offset: 8 |
| 5682 |
* fill-column: 78 |
| 5683 |
* End: |
| 5684 |
*/ |
| 5685 |
|
| 5686 |
#endif |
| 5687 |
|