X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/8414a40c52191d4c7cfeea74df22d9d64cbec415..cd0f218cdd3114dc6770bc9f6703225aabb5a2de:/src/tiff/tools/tiff2pdf.c diff --git a/src/tiff/tools/tiff2pdf.c b/src/tiff/tools/tiff2pdf.c index 0b1795b5da..b9a6075afe 100644 --- a/src/tiff/tools/tiff2pdf.c +++ b/src/tiff/tools/tiff2pdf.c @@ -31,40 +31,53 @@ #include #include #include - -#include "tiffiop.h" +#include #if HAVE_UNISTD_H # include #endif -#ifndef NULL -#define NULL ((void*)0) +#ifdef HAVE_FCNTL_H +# include +#endif + +#ifdef HAVE_IO_H +# include +#endif + +#ifdef NEED_LIBPORT +# include "libport.h" #endif -#if defined(VMS) -#define unlink remove +#include "tiffiop.h" +#include "tiffio.h" + +#ifndef HAVE_GETOPT +extern int getopt(int, char**, char*); +#endif + +#ifndef EXIT_SUCCESS +# define EXIT_SUCCESS 0 #endif -#if defined(_WIN32) && defined(USE_WIN32_FILEIO) -#include -#include -#define unlink DeleteFileA +#ifndef EXIT_FAILURE +# define EXIT_FAILURE 1 #endif #define TIFF2PDF_MODULE "tiff2pdf" -#define T2P_VERSION "d" + +#define PS_UNIT_SIZE 72.0F /* This type is of PDF color spaces. */ -typedef enum{ - T2P_CS_BILEVEL=0x01, /* Bilevel, black and white */ - T2P_CS_GRAY=0x02, /* Single channel */ - T2P_CS_RGB=0x04, /* Three channel tristimulus RGB */ - T2P_CS_CMYK=0x08, /* Four channel CMYK print inkset */ - T2P_CS_LAB=0x10, /* Three channel L*a*b* color space */ - T2P_CS_PALETTE=0x1000 /* One of the above with a color map */ - , T2P_CS_CALGRAY=0x20 /* Calibrated single channel */ - , T2P_CS_CALRGB=0x40 /* Calibrated three channel tristimulus RGB */ - , T2P_CS_ICCBASED=0x80 /* ICC profile color specification */ +typedef enum { + T2P_CS_BILEVEL = 0x01, /* Bilevel, black and white */ + T2P_CS_GRAY = 0x02, /* Single channel */ + T2P_CS_RGB = 0x04, /* Three channel tristimulus RGB */ + T2P_CS_CMYK = 0x08, /* Four channel CMYK print inkset */ + T2P_CS_LAB = 0x10, /* Three channel L*a*b* color space */ + T2P_CS_PALETTE = 0x1000,/* One of the above with a color map */ + T2P_CS_CALGRAY = 0x20, /* Calibrated single channel */ + T2P_CS_CALRGB = 0x40, /* Calibrated three channel tristimulus RGB */ + T2P_CS_ICCBASED = 0x80 /* ICC profile color specification */ } t2p_cs_t; /* This type is of PDF compression types. */ @@ -160,9 +173,6 @@ typedef struct { uint16 tiff_orientation; toff_t tiff_dataoffset; tsize_t tiff_datasize; - TIFFReadWriteProc tiff_readproc; - TIFFReadWriteProc tiff_writeproc; - TIFFSeekProc tiff_seekproc; uint16 tiff_resunit; uint16 pdf_centimeters; uint16 pdf_overrideres; @@ -177,6 +187,7 @@ typedef struct { float pdf_pagelength; float pdf_imagewidth; float pdf_imagelength; + int pdf_image_fillpage; /* 0 (default: no scaling, 1:scale imagesize to pagesize */ T2P_BOX pdf_mediabox; T2P_BOX pdf_imagebox; uint16 pdf_majorversion; @@ -187,13 +198,20 @@ typedef struct { uint32 pdf_palettecs; uint16 pdf_fitwindow; uint32 pdf_startxref; - char* pdf_fileid; - char* pdf_datetime; - char* pdf_creator; - char* pdf_author; - char* pdf_title; - char* pdf_subject; - char* pdf_keywords; +#define TIFF2PDF_FILEID_SIZE 33 + char pdf_fileid[TIFF2PDF_FILEID_SIZE]; +#define TIFF2PDF_DATETIME_SIZE 17 + char pdf_datetime[TIFF2PDF_DATETIME_SIZE]; +#define TIFF2PDF_CREATOR_SIZE 512 + char pdf_creator[TIFF2PDF_CREATOR_SIZE]; +#define TIFF2PDF_AUTHOR_SIZE 512 + char pdf_author[TIFF2PDF_AUTHOR_SIZE]; +#define TIFF2PDF_TITLE_SIZE 512 + char pdf_title[TIFF2PDF_TITLE_SIZE]; +#define TIFF2PDF_SUBJECT_SIZE 512 + char pdf_subject[TIFF2PDF_SUBJECT_SIZE]; +#define TIFF2PDF_KEYWORDS_SIZE 512 + char pdf_keywords[TIFF2PDF_KEYWORDS_SIZE]; t2p_cs_t pdf_colorspace; uint16 pdf_colorspace_invert; uint16 pdf_switchdecode; @@ -225,6 +243,11 @@ typedef struct { uint32 pdf_icccs; uint32 tiff_iccprofilelength; tdata_t tiff_iccprofile; + + /* fields for custom read/write procedures */ + FILE *outputfile; + int outputdisable; + tsize_t outputwritten; } T2P; /* These functions are called by main. */ @@ -247,10 +270,6 @@ void t2p_free(T2P*); } #endif -tsize_t t2p_empty_readproc(thandle_t, tdata_t, tsize_t); -tsize_t t2p_empty_writeproc(thandle_t, tdata_t, tsize_t); -toff_t t2p_empty_seekproc(thandle_t, toff_t, int); -int t2p_empty_closeproc(thandle_t); void t2p_read_tiff_init(T2P*, TIFF*); int t2p_cmp_t2p_page(const void*, const void*); void t2p_read_tiff_data(T2P*, TIFF*); @@ -279,7 +298,7 @@ tsize_t t2p_sample_lab_signed_to_unsigned(tdata_t, uint32); tsize_t t2p_write_pdf_header(T2P*, TIFF*); tsize_t t2p_write_pdf_obj_start(uint32, TIFF*); tsize_t t2p_write_pdf_obj_end(TIFF*); -tsize_t t2p_write_pdf_name(char*, TIFF*); +tsize_t t2p_write_pdf_name(unsigned char*, TIFF*); tsize_t t2p_write_pdf_string(char*, TIFF*); tsize_t t2p_write_pdf_stream(tdata_t, tsize_t, TIFF*); tsize_t t2p_write_pdf_stream_start(TIFF*); @@ -313,6 +332,137 @@ tsize_t t2p_write_pdf_xobject_stream_filter(ttile_t, T2P*, TIFF*); tsize_t t2p_write_pdf_xreftable(T2P*, TIFF*); tsize_t t2p_write_pdf_trailer(T2P*, TIFF*); +static void +t2p_disable(TIFF *tif) +{ + T2P *t2p = (T2P*) TIFFClientdata(tif); + t2p->outputdisable = 1; +} + +static void +t2p_enable(TIFF *tif) +{ + T2P *t2p = (T2P*) TIFFClientdata(tif); + t2p->outputdisable = 0; +} + +/* + * Procs for TIFFClientOpen + */ + +static tmsize_t +t2pReadFile(TIFF *tif, tdata_t data, tmsize_t size) +{ + thandle_t client = TIFFClientdata(tif); + TIFFReadWriteProc proc = TIFFGetReadProc(tif); + if (proc) + return proc(client, data, size); + return -1; +} + +static tmsize_t +t2pWriteFile(TIFF *tif, tdata_t data, tmsize_t size) +{ + thandle_t client = TIFFClientdata(tif); + TIFFReadWriteProc proc = TIFFGetWriteProc(tif); + if (proc) + return proc(client, data, size); + return -1; +} + +static uint64 +t2pSeekFile(TIFF *tif, toff_t offset, int whence) +{ + thandle_t client = TIFFClientdata(tif); + TIFFSeekProc proc = TIFFGetSeekProc(tif); + if (proc) + return proc(client, offset, whence); + return -1; +} + +static tmsize_t +t2p_readproc(thandle_t handle, tdata_t data, tmsize_t size) +{ + (void) handle, (void) data, (void) size; + return -1; +} + +static tmsize_t +t2p_writeproc(thandle_t handle, tdata_t data, tmsize_t size) +{ + T2P *t2p = (T2P*) handle; + if (t2p->outputdisable <= 0 && t2p->outputfile) { + tsize_t written = fwrite(data, 1, size, t2p->outputfile); + t2p->outputwritten += written; + return written; + } + return size; +} + +static uint64 +t2p_seekproc(thandle_t handle, uint64 offset, int whence) +{ + T2P *t2p = (T2P*) handle; + if (t2p->outputdisable <= 0 && t2p->outputfile) + return fseek(t2p->outputfile, (long) offset, whence); + return offset; +} + +static int +t2p_closeproc(thandle_t handle) +{ + (void) handle; + return 0; +} + +static uint64 +t2p_sizeproc(thandle_t handle) +{ + (void) handle; + return -1; +} + +static int +t2p_mapproc(thandle_t handle, void **data, toff_t *offset) +{ + (void) handle, (void) data, (void) offset; + return -1; +} + +static void +t2p_unmapproc(thandle_t handle, void *data, toff_t offset) +{ + (void) handle, (void) data, (void) offset; +} + +static uint64 +checkAdd64(uint64 summand1, uint64 summand2, T2P* t2p) +{ + uint64 bytes = summand1 + summand2; + + if (bytes - summand1 != summand2) { + TIFFError(TIFF2PDF_MODULE, "Integer overflow"); + t2p->t2p_error = T2P_ERR_ERROR; + bytes = 0; + } + + return bytes; +} + +static uint64 +checkMultiply64(uint64 first, uint64 second, T2P* t2p) +{ + uint64 bytes = first * second; + + if (second && bytes / second != first) { + TIFFError(TIFF2PDF_MODULE, "Integer overflow"); + t2p->t2p_error = T2P_ERR_ERROR; + bytes = 0; + } + + return bytes; +} + /* This is the main function. @@ -385,23 +535,22 @@ tsize_t t2p_write_pdf_trailer(T2P*, TIFF*); options: -o: output to file name - -j compress with JPEG (requires libjpeg configured with libtiff) - -z compress with Zip/Deflate (requires zlib configured with libtiff) + -j: compress with JPEG (requires libjpeg configured with libtiff) + -z: compress with Zip/Deflate (requires zlib configured with libtiff) -q: compression quality - -n no compressed data passthrough - -d do not compress (decompress) - - -i invert colors - + -n: no compressed data passthrough + -d: do not compress (decompress) + -i: invert colors -u: set distance unit, 'i' for inch, 'm' for centimeter -x: set x resolution default -y: set y resolution default -w: width in units -l: length in units -r: 'd' for resolution default, 'o' for resolution override - -p: paper size, eg "letter", "legal", "A4" - -f set PDF "Fit Window" user preference - -b set PDF "Interpolate" user preference + -p: paper size, eg "letter", "legal", "a4" + -F: make the tiff fill the PDF page + -f: set pdf "fit window" user preference + -b: set PDF "Interpolate" user preference -e: date, overrides image or current date/time default, YYYYMMDDHHMMSS -c: creator, overrides image software default -a: author, overrides image artist default @@ -409,7 +558,7 @@ tsize_t t2p_write_pdf_trailer(T2P*, TIFF*); -s: subject, overrides image image description default -k: keywords - -h usage + -h: usage examples: @@ -419,17 +568,17 @@ tsize_t t2p_write_pdf_trailer(T2P*, TIFF*); tiff2pdf input.tiff - The above example would generate PDF output from input.tiff and write it + The above example would generate PDF output from input.tiff and write it to standard output. tiff2pdf -j -p letter -o output.pdf input.tiff - The above example would generate the file output.pdf from input.tiff, - putting the image pages on a letter sized page, compressing the output + The above example would generate the file output.pdf from input.tiff, + putting the image pages on a letter sized page, compressing the output with JPEG. Please report bugs through: - + http://bugzilla.remotesensing.org/buglist.cgi?product=libtiff See also libtiff.3t, tiffcp. @@ -437,24 +586,23 @@ tsize_t t2p_write_pdf_trailer(T2P*, TIFF*); int main(int argc, char** argv){ - extern int optind; extern char *optarg; + extern int optind; + const char *outfilename = NULL; T2P *t2p = NULL; TIFF *input = NULL, *output = NULL; - const char *outfilename = NULL; - tsize_t written=0; - int c=0; - + int c, ret = EXIT_SUCCESS; + t2p = t2p_init(); if (t2p == NULL){ - TIFFError( - TIFF2PDF_MODULE, - "Can't initialize context"); - goto failexit; + TIFFError(TIFF2PDF_MODULE, "Can't initialize context"); + goto fail; } - while ((c = getopt(argc, argv, "o:q:u:x:y:w:l:r:p:e:c:a:t:s:k:jzndifbh")) != -1){ + while (argv && + (c = getopt(argc, argv, + "o:q:u:x:y:w:l:r:p:e:c:a:t:s:k:jzndifbhF")) != -1){ switch (c) { case 'o': outfilename = optarg; @@ -508,12 +656,12 @@ int main(int argc, char** argv){ case 'w': t2p->pdf_overridepagesize=1; t2p->pdf_defaultpagewidth = - ((float)atof(optarg) * 72.0F) / (t2p->pdf_centimeters?2.54F:1.0F); + ((float)atof(optarg) * PS_UNIT_SIZE) / (t2p->pdf_centimeters?2.54F:1.0F); break; case 'l': t2p->pdf_overridepagesize=1; t2p->pdf_defaultpagelength = - ((float)atof(optarg) * 72.0F) / (t2p->pdf_centimeters?2.54F:1.0F); + ((float)atof(optarg) * PS_UNIT_SIZE) / (t2p->pdf_centimeters?2.54F:1.0F); break; case 'r': if(optarg[0]=='o'){ @@ -535,210 +683,133 @@ int main(int argc, char** argv){ case 'i': t2p->pdf_colorspace_invert=1; break; + case 'F': + t2p->pdf_image_fillpage = 1; + break; case 'f': t2p->pdf_fitwindow=1; break; case 'e': - t2p->pdf_datetime = (char*)_TIFFmalloc(17); - if(t2p->pdf_datetime==NULL){ - TIFFError(TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory for main", - 17); - goto failfreet2p; - } - if(strlen(optarg)==0){ - t2p->pdf_datetime[0]=0; + if (strlen(optarg) == 0) { + t2p->pdf_datetime[0] = '\0'; } else { - if(strlen(optarg)>14){optarg[14]=0;} - t2p->pdf_datetime[0]='D'; - t2p->pdf_datetime[1]=':'; - strcpy(&(t2p->pdf_datetime[2]), optarg); + t2p->pdf_datetime[0] = 'D'; + t2p->pdf_datetime[1] = ':'; + strncpy(t2p->pdf_datetime + 2, optarg, + sizeof(t2p->pdf_datetime) - 3); + t2p->pdf_datetime[sizeof(t2p->pdf_datetime) - 1] = '\0'; } break; case 'c': - t2p->pdf_creator = - (char *)_TIFFmalloc(strlen(optarg) + 1); - if(t2p->pdf_creator==NULL){ - TIFFError(TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory for main", - strlen(optarg)+1); - goto failfreet2p; - } - strcpy(t2p->pdf_creator, optarg); - t2p->pdf_creator[strlen(optarg)]=0; + strncpy(t2p->pdf_creator, optarg, sizeof(t2p->pdf_creator) - 1); + t2p->pdf_creator[sizeof(t2p->pdf_creator) - 1] = '\0'; break; case 'a': - t2p->pdf_author = - (char *)_TIFFmalloc(strlen(optarg) + 1); - if(t2p->pdf_author==NULL){ - TIFFError( - TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory for main", - strlen(optarg)+1); - goto failfreet2p; - } - strcpy(t2p->pdf_author, optarg); - t2p->pdf_author[strlen(optarg)]=0; + strncpy(t2p->pdf_author, optarg, sizeof(t2p->pdf_author) - 1); + t2p->pdf_author[sizeof(t2p->pdf_author) - 1] = '\0'; break; case 't': - t2p->pdf_title= (char*)_TIFFmalloc(strlen(optarg)+1); - if(t2p->pdf_title==NULL){ - TIFFError( - TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory for main", - strlen(optarg)+1); - goto failfreet2p; - } - strcpy(t2p->pdf_title, optarg); - t2p->pdf_title[strlen(optarg)]=0; + strncpy(t2p->pdf_title, optarg, sizeof(t2p->pdf_title) - 1); + t2p->pdf_title[sizeof(t2p->pdf_title) - 1] = '\0'; break; case 's': - t2p->pdf_subject= (char*)_TIFFmalloc(strlen(optarg)+1); - if(t2p->pdf_subject==NULL){ - TIFFError( - TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory for main", - strlen(optarg)+1); - goto failfreet2p; - } - strcpy(t2p->pdf_subject, optarg); - t2p->pdf_subject[strlen(optarg)]=0; + strncpy(t2p->pdf_subject, optarg, sizeof(t2p->pdf_subject) - 1); + t2p->pdf_subject[sizeof(t2p->pdf_subject) - 1] = '\0'; break; case 'k': - t2p->pdf_keywords= (char*)_TIFFmalloc(strlen(optarg)+1); - if(t2p->pdf_keywords==NULL){ - TIFFError( - TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory for main", - strlen(optarg)+1); - goto failfreet2p; - } - strcpy(t2p->pdf_keywords, optarg); - t2p->pdf_keywords[strlen(optarg)]=0; - break; + strncpy(t2p->pdf_keywords, optarg, sizeof(t2p->pdf_keywords) - 1); + t2p->pdf_keywords[sizeof(t2p->pdf_keywords) - 1] = '\0'; + break; case 'b': t2p->pdf_image_interpolate = 1; break; case 'h': case '?': tiff2pdf_usage(); - goto failfreet2p; + goto success; break; } } - t2p_validate(t2p); - - if(argc>optind){ + /* + * Input + */ + if(argc > optind) { input = TIFFOpen(argv[optind++], "r"); - if(input==NULL){ - TIFFError( - TIFF2PDF_MODULE, - "Can't open input file %s for reading", - argv[optind-1]); - goto failfreet2p; + if (input==NULL) { + TIFFError(TIFF2PDF_MODULE, + "Can't open input file %s for reading", + argv[optind-1]); + goto fail; } } else { - TIFFError( - TIFF2PDF_MODULE, - "No input file specified"); + TIFFError(TIFF2PDF_MODULE, "No input file specified"); tiff2pdf_usage(); - goto failfreet2p; + goto fail; } - if(argc>optind){ - TIFFError( - TIFF2PDF_MODULE, - "No support for multiple input files"); + if(argc > optind) { + TIFFError(TIFF2PDF_MODULE, + "No support for multiple input files"); tiff2pdf_usage(); - goto failcloseinput; + goto fail; } + /* + * Output + */ + t2p->outputdisable = 0; if (outfilename) { - output = TIFFOpen(outfilename, "w"); - if(output == NULL) { - TIFFError(TIFF2PDF_MODULE, - "Can't open output file %s for writing", - optarg); - goto failfreet2p; - } - if(output->tif_seekproc != NULL) { - TIFFSeekFile(output, (toff_t) 0, SEEK_SET); + t2p->outputfile = fopen(outfilename, "wb"); + if (t2p->outputfile == NULL) { + TIFFError(TIFF2PDF_MODULE, + "Can't open output file %s for writing", + outfilename); + goto fail; } } else { -#if !defined(_WIN32) || defined(AVOID_WIN32_FILEIO) - output = TIFFFdOpen((int)fileno(tmpfile()), "-", "w"); -#else - { - TCHAR temppath[MAX_PATH]; - TCHAR tempfile[MAX_PATH]; - GetTempPath((DWORD)MAX_PATH, (LPTSTR)temppath); - GetTempFileName((LPCTSTR)temppath, (LPTSTR) "t2p", 0, (LPTSTR)tempfile); - output = TIFFFdOpen( (int)CreateFile( - (LPCTSTR)tempfile, - GENERIC_WRITE, - 0, - NULL, - CREATE_ALWAYS, - FILE_FLAG_DELETE_ON_CLOSE, - NULL), - "-", "w"); - } -#endif - if(output==NULL){ - TIFFError(TIFF2PDF_MODULE, - "Can't open temporary output file for writing to stdout", - argv[optind-1]); - goto failcloseinput; - } - TIFFFlush(output); - output->tif_readproc=t2p_empty_readproc; - output->tif_seekproc=t2p_empty_seekproc; - output->tif_closeproc=t2p_empty_closeproc; -#if !defined(_WIN32) || defined(AVOID_WIN32_FILEIO) - close(output->tif_fd); - output->tif_fd=(int)fileno(stdout); -#else - CloseHandle((HANDLE) output->tif_fd); - output->tif_fd=(int)GetStdHandle(STD_OUTPUT_HANDLE); -#endif - output->tif_clientdata=(thandle_t)output->tif_fd; + outfilename = "-"; + t2p->outputfile = stdout; } - written = t2p_write_pdf(t2p, input, output); - - if(t2p->t2p_error != 0){ - TIFFError( - TIFF2PDF_MODULE, - "An error occurred in converting TIFF %s to PDF %s", - TIFFFileName(input), - TIFFFileName(output) - ); - goto failcloseinput; + output = TIFFClientOpen(outfilename, "w", (thandle_t) t2p, + t2p_readproc, t2p_writeproc, t2p_seekproc, + t2p_closeproc, t2p_sizeproc, + t2p_mapproc, t2p_unmapproc); + if (output == NULL) { + TIFFError(TIFF2PDF_MODULE, + "Can't initialize output descriptor"); + goto fail; } - - if(input != NULL){ + + /* + * Validate + */ + t2p_validate(t2p); + t2pSeekFile(output, (toff_t) 0, SEEK_SET); + + /* + * Write + */ + t2p_write_pdf(t2p, input, output); + if (t2p->t2p_error != 0) { + TIFFError(TIFF2PDF_MODULE, + "An error occurred creating output PDF file"); + goto fail; + } + + goto success; +fail: + ret = EXIT_FAILURE; +success: + if(input != NULL) TIFFClose(input); - } - if(output != NULL){ + if (output != NULL) TIFFClose(output); - } - if(t2p != NULL){ - t2p_free(t2p); - } - - return(EXIT_SUCCESS); - -failcloseinput: - if(input != NULL){ - TIFFClose(input); - } -failfreet2p: - if(t2p != NULL){ + if (t2p != NULL) t2p_free(t2p); - } -failexit: - return(EXIT_FAILURE); + return ret; + } void tiff2pdf_usage(){ @@ -747,14 +818,15 @@ void tiff2pdf_usage(){ "options:", " -o: output to file name", #ifdef JPEG_SUPPORT - " -j compress with JPEG", + " -j: compress with JPEG", #endif #ifdef ZIP_SUPPORT - " -z compress with Zip/Deflate", + " -z: compress with Zip/Deflate", #endif " -q: compression quality", - " -n no compressed data passthrough", - " -d do not compress (decompress)", + " -n: no compressed data passthrough", + " -d: do not compress (decompress)", + " -i: invert colors", " -u: set distance unit, 'i' for inch, 'm' for centimeter", " -x: set x resolution default in dots per unit", " -y: set y resolution default in dots per unit", @@ -762,15 +834,16 @@ void tiff2pdf_usage(){ " -l: length in units", " -r: 'd' for resolution default, 'o' for resolution override", " -p: paper size, eg \"letter\", \"legal\", \"A4\"", - " -f set PDF \"Fit Window\" user preference", + " -F: make the tiff fill the PDF page", + " -f: set PDF \"Fit Window\" user preference", " -e: date, overrides image or current date/time default, YYYYMMDDHHMMSS", " -c: sets document creator, overrides image software default", " -a: sets document author, overrides image artist default", " -t: sets document title, overrides image document name default", " -s: sets document subject, overrides image image description default", " -k: sets document keywords", - " -b set PDF \"Interpolate\" user preference", - " -h usage", + " -b: set PDF \"Interpolate\" user preference", + " -h: usage", NULL }; int i=0; @@ -785,8 +858,7 @@ void tiff2pdf_usage(){ int tiff2pdf_match_paper_size(float* width, float* length, char* papersize){ - int i=0; - int len=0; + size_t i, len; const char* sizes[]={ "LETTER", "A4", "LEGAL", "EXECUTIVE", "LETTER", "LEGAL", "LEDGER", "TABLOID", @@ -845,17 +917,17 @@ int tiff2pdf_match_paper_size(float* width, float* length, char* papersize){ } /* - This function allocates and initializes a T2P context struct pointer. -*/ - -T2P* t2p_init(){ + * This function allocates and initializes a T2P context struct pointer. + */ +T2P* t2p_init() +{ T2P* t2p = (T2P*) _TIFFmalloc(sizeof(T2P)); if(t2p==NULL){ TIFFError( TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory for t2p_init", - sizeof(T2P)); + "Can't allocate %lu bytes of memory for t2p_init", + (unsigned long) sizeof(T2P)); return( (T2P*) NULL ); } _TIFFmemset(t2p, 0x00, sizeof(T2P)); @@ -871,14 +943,14 @@ T2P* t2p_init(){ } /* - This function frees a T2P context struct pointer and any allocated data fields of it. -*/ - -void t2p_free(T2P* t2p){ + * This function frees a T2P context struct pointer and any allocated data fields of it. + */ - int i=0; +void t2p_free(T2P* t2p) +{ + int i = 0; - if(t2p != NULL){ + if (t2p != NULL) { if(t2p->pdf_xrefoffsets != NULL){ _TIFFfree( (tdata_t) t2p->pdf_xrefoffsets); } @@ -896,27 +968,6 @@ void t2p_free(T2P* t2p){ if(t2p->pdf_palette != NULL){ _TIFFfree( (tdata_t) t2p->pdf_palette); } - if(t2p->pdf_fileid != NULL){ - _TIFFfree( (tdata_t) t2p->pdf_fileid); - } - if(t2p->pdf_datetime != NULL){ - _TIFFfree( (tdata_t) t2p->pdf_datetime); - } - if(t2p->pdf_creator != NULL){ - _TIFFfree( (tdata_t) t2p->pdf_creator); - } - if(t2p->pdf_author != NULL){ - _TIFFfree( (tdata_t) t2p->pdf_author); - } - if(t2p->pdf_title != NULL){ - _TIFFfree( (tdata_t) t2p->pdf_title); - } - if(t2p->pdf_subject != NULL){ - _TIFFfree( (tdata_t) t2p->pdf_subject); - } - if(t2p->pdf_keywords != NULL){ - _TIFFfree( (tdata_t) t2p->pdf_keywords); - } #ifdef OJPEG_SUPPORT if(t2p->pdf_ojpegdata != NULL){ _TIFFfree( (tdata_t) t2p->pdf_ojpegdata); @@ -937,7 +988,7 @@ void t2p_validate(T2P* t2p){ #ifdef JPEG_SUPPORT if(t2p->pdf_defaultcompression==T2P_COMPRESS_JPEG){ - if(t2p->pdf_defaultcompressionquality<100 || + if(t2p->pdf_defaultcompressionquality>100 || t2p->pdf_defaultcompressionquality<1){ t2p->pdf_defaultcompressionquality=0; } @@ -945,25 +996,17 @@ void t2p_validate(T2P* t2p){ #endif #ifdef ZIP_SUPPORT if(t2p->pdf_defaultcompression==T2P_COMPRESS_ZIP){ - switch (t2p->pdf_defaultcompressionquality){ - case 1: case 10: case 11: case 12: case 13: case 14: case 15: - case 101: case 110: case 111: case 112: case 113: case 114: case 115: - case 201: case 210: case 211: case 212: case 213: case 214: case 215: - case 301: case 310: case 311: case 312: case 313: case 314: case 315: - case 401: case 410: case 411: case 412: case 413: case 414: case 415: - case 501: case 510: case 511: case 512: case 513: case 514: case 515: - case 601: case 610: case 611: case 612: case 613: case 614: case 615: - case 701: case 710: case 711: case 712: case 713: case 714: case 715: - case 801: case 810: case 811: case 812: case 813: case 814: case 815: - case 901: case 910: case 911: case 912: case 913: case 914: case 915: - break; - default: - t2p->pdf_defaultcompressionquality=0; + uint16 m=t2p->pdf_defaultcompressionquality%100; + if(t2p->pdf_defaultcompressionquality/100 > 9 || + (m>1 && m<10) || m>15){ + t2p->pdf_defaultcompressionquality=0; } if(t2p->pdf_defaultcompressionquality%100 !=0){ + t2p->pdf_defaultcompressionquality/=100; + t2p->pdf_defaultcompressionquality*=100; TIFFError( TIFF2PDF_MODULE, - "PNG Group predictor differencing not implemented, assuming compresion quality %u", + "PNG Group predictor differencing not implemented, assuming compression quality %u", t2p->pdf_defaultcompressionquality); } t2p->pdf_defaultcompressionquality%=100; @@ -996,8 +1039,8 @@ void t2p_read_tiff_init(T2P* t2p, TIFF* input){ if(t2p->tiff_pages==NULL){ TIFFError( TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory for tiff_pages array, %s", - directorycount * sizeof(T2P_PAGE), + "Can't allocate %lu bytes of memory for tiff_pages array, %s", + (unsigned long) directorycount * sizeof(T2P_PAGE), TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return; @@ -1007,8 +1050,8 @@ void t2p_read_tiff_init(T2P* t2p, TIFF* input){ if(t2p->tiff_tiles==NULL){ TIFFError( TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory for tiff_tiles array, %s", - directorycount * sizeof(T2P_TILES), + "Can't allocate %lu bytes of memory for tiff_tiles array, %s", + (unsigned long) directorycount * sizeof(T2P_TILES), TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return; @@ -1023,6 +1066,7 @@ void t2p_read_tiff_init(T2P* t2p, TIFF* input){ "Can't set directory %u of input file %s", i, TIFFFileName(input)); + t2p->t2p_error = T2P_ERR_ERROR; return; } if(TIFFGetField(input, TIFFTAG_PAGENUMBER, &pagen, &paged)){ @@ -1139,8 +1183,8 @@ void t2p_read_tiff_init(T2P* t2p, TIFF* input){ if( t2p->tiff_tiles[i].tiles_tiles == NULL){ TIFFError( TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory for t2p_read_tiff_init, %s", - t2p->tiff_tiles[i].tiles_tilecount * sizeof(T2P_TILE), + "Can't allocate %lu bytes of memory for t2p_read_tiff_init, %s", + (unsigned long) t2p->tiff_tiles[i].tiles_tilecount * sizeof(T2P_TILE), TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return; @@ -1152,8 +1196,9 @@ void t2p_read_tiff_init(T2P* t2p, TIFF* input){ } /* - This function is used by qsort to sort a T2P_PAGE* array of page structures by page number. -*/ + * This function is used by qsort to sort a T2P_PAGE* array of page structures + * by page number. + */ int t2p_cmp_t2p_page(const void* e1, const void* e2){ @@ -1379,8 +1424,7 @@ void t2p_read_tiff_data(T2P* t2p, TIFF* input){ TIFFError( TIFF2PDF_MODULE, "No support for palettized image %s with not one sample per pixel", - TIFFFileName(input), - t2p->tiff_samplesperpixel); + TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return; } @@ -1449,8 +1493,7 @@ void t2p_read_tiff_data(T2P* t2p, TIFF* input){ TIFFError( TIFF2PDF_MODULE, "No support for palettized CMYK image %s with not one sample per pixel", - TIFFFileName(input), - t2p->tiff_samplesperpixel); + TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return; } @@ -1483,7 +1526,7 @@ void t2p_read_tiff_data(T2P* t2p, TIFF* input){ t2p->pdf_palette[(i*4)] = (unsigned char) (r[i]>>8); t2p->pdf_palette[(i*4)+1]= (unsigned char) (g[i]>>8); t2p->pdf_palette[(i*4)+2]= (unsigned char) (b[i]>>8); - t2p->pdf_palette[(i*4)+2]= (unsigned char) (a[i]>>8); + t2p->pdf_palette[(i*4)+3]= (unsigned char) (a[i]>>8); } t2p->pdf_palettesize *= 4; break; @@ -1529,8 +1572,7 @@ void t2p_read_tiff_data(T2P* t2p, TIFF* input){ TIFFError( TIFF2PDF_MODULE, "No support for %s with photometric interpretation LogL/LogLuv", - TIFFFileName(input), - t2p->tiff_photometric); + TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return; default: @@ -1591,13 +1633,15 @@ void t2p_read_tiff_data(T2P* t2p, TIFF* input){ if(TIFFGetField(input, TIFFTAG_YRESOLUTION, &(t2p->tiff_yres) ) == 0){ t2p->tiff_yres=0.0; } - TIFFGetFieldDefaulted(input, TIFFTAG_RESOLUTIONUNIT, &(t2p->tiff_resunit) ); - if(t2p->tiff_resunit==RESUNIT_CENTIMETER){ - t2p->tiff_xres*=2.54F; - t2p->tiff_yres*=2.54F; - } else if (t2p->tiff_resunit!=RESUNIT_INCH && t2p->pdf_centimeters!=0){ - t2p->tiff_xres*=2.54F; - t2p->tiff_yres*=2.54F; + TIFFGetFieldDefaulted(input, TIFFTAG_RESOLUTIONUNIT, + &(t2p->tiff_resunit)); + if(t2p->tiff_resunit == RESUNIT_CENTIMETER) { + t2p->tiff_xres *= 2.54F; + t2p->tiff_yres *= 2.54F; + } else if (t2p->tiff_resunit != RESUNIT_INCH + && t2p->pdf_centimeters != 0) { + t2p->tiff_xres *= 2.54F; + t2p->tiff_yres *= 2.54F; } t2p_compose_pdf_page(t2p); @@ -1755,29 +1799,26 @@ void t2p_read_tiff_data(T2P* t2p, TIFF* input){ void t2p_read_tiff_size(T2P* t2p, TIFF* input){ - uint32* sbc=NULL; + uint64* sbc=NULL; #if defined(JPEG_SUPPORT) || defined (OJPEG_SUPPORT) unsigned char* jpt=NULL; - uint16 xuint16=0; tstrip_t i=0; tstrip_t stripcount=0; #endif -#ifdef OJPEG_SUPPORT - tsize_t k = 0; -#endif + uint64 k = 0; if(t2p->pdf_transcode == T2P_TRANSCODE_RAW){ #ifdef CCITT_SUPPORT if(t2p->pdf_compression == T2P_COMPRESS_G4 ){ TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc); - t2p->tiff_datasize=sbc[0]; + t2p->tiff_datasize=(tmsize_t)sbc[0]; return; } #endif #ifdef ZIP_SUPPORT if(t2p->pdf_compression == T2P_COMPRESS_ZIP){ TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc); - t2p->tiff_datasize=sbc[0]; + t2p->tiff_datasize=(tmsize_t)sbc[0]; return; } #endif @@ -1792,19 +1833,25 @@ void t2p_read_tiff_size(T2P* t2p, TIFF* input){ } stripcount=TIFFNumberOfStrips(input); for(i=0;itiff_dataoffset))){ if(t2p->tiff_dataoffset != 0){ if(TIFFGetField(input, TIFFTAG_JPEGIFBYTECOUNT, &(t2p->tiff_datasize))!=0){ - if(t2p->tiff_datasize < k) { - t2p->pdf_ojpegiflength=t2p->tiff_datasize; - t2p->tiff_datasize+=k; - t2p->tiff_datasize+=6; - t2p->tiff_datasize+=2*stripcount; + if((uint64)t2p->tiff_datasize < k) { TIFFWarning(TIFF2PDF_MODULE, "Input file %s has short JPEG interchange file byte count", TIFFFileName(input)); + t2p->pdf_ojpegiflength=t2p->tiff_datasize; + k = checkAdd64(k, t2p->tiff_datasize, t2p); + k = checkAdd64(k, 6, t2p); + k = checkAdd64(k, stripcount, t2p); + k = checkAdd64(k, stripcount, t2p); + t2p->tiff_datasize = (tsize_t) k; + if ((uint64) t2p->tiff_datasize != k) { + TIFFError(TIFF2PDF_MODULE, "Integer overflow"); + t2p->t2p_error = T2P_ERR_ERROR; + } return; } return; @@ -1817,21 +1864,27 @@ void t2p_read_tiff_size(T2P* t2p, TIFF* input){ } } } - t2p->tiff_datasize+=k; - t2p->tiff_datasize+=2*stripcount; - t2p->tiff_datasize+=2048; + k = checkAdd64(k, stripcount, t2p); + k = checkAdd64(k, stripcount, t2p); + k = checkAdd64(k, 2048, t2p); + t2p->tiff_datasize = (tsize_t) k; + if ((uint64) t2p->tiff_datasize != k) { + TIFFError(TIFF2PDF_MODULE, "Integer overflow"); + t2p->t2p_error = T2P_ERR_ERROR; + } return; } #endif #ifdef JPEG_SUPPORT - if(t2p->tiff_compression == COMPRESSION_JPEG){ - if(TIFFGetField(input, TIFFTAG_JPEGTABLES, &xuint16, &jpt) != 0 ){ - if(xuint16>4){ - t2p->tiff_datasize+= xuint16; - t2p->tiff_datasize -=2; /* don't use EOI of header */ + if(t2p->tiff_compression == COMPRESSION_JPEG) { + uint32 count = 0; + if(TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt) != 0 ){ + if(count > 4){ + k += count; + k -= 2; /* don't use EOI of header */ } } else { - t2p->tiff_datasize=2; /* SOI for first strip */ + k = 2; /* SOI for first strip */ } stripcount=TIFFNumberOfStrips(input); if(!TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc)){ @@ -1842,17 +1895,33 @@ void t2p_read_tiff_size(T2P* t2p, TIFF* input){ return; } for(i=0;itiff_datasize += sbc[i]; - t2p->tiff_datasize -=4; /* don't use SOI or EOI of strip */ + k = checkAdd64(k, sbc[i], t2p); + k -=4; /* don't use SOI or EOI of strip */ } - t2p->tiff_datasize +=2; /* use EOI of last strip */ + k = checkAdd64(k, 2, t2p); /* use EOI of last strip */ + t2p->tiff_datasize = (tsize_t) k; + if ((uint64) t2p->tiff_datasize != k) { + TIFFError(TIFF2PDF_MODULE, "Integer overflow"); + t2p->t2p_error = T2P_ERR_ERROR; + } + return; } #endif (void) 0; } - t2p->tiff_datasize=TIFFScanlineSize(input) * t2p->tiff_length; + k = checkMultiply64(TIFFScanlineSize(input), t2p->tiff_length, t2p); if(t2p->tiff_planar==PLANARCONFIG_SEPARATE){ - t2p->tiff_datasize*= t2p->tiff_samplesperpixel; + k = checkMultiply64(k, t2p->tiff_samplesperpixel, t2p); + } + if (k == 0) { + /* Assume we had overflow inside TIFFScanlineSize */ + t2p->t2p_error = T2P_ERR_ERROR; + } + + t2p->tiff_datasize = (tsize_t) k; + if ((uint64) t2p->tiff_datasize != k) { + TIFFError(TIFF2PDF_MODULE, "Integer overflow"); + t2p->t2p_error = T2P_ERR_ERROR; } return; @@ -1865,12 +1934,12 @@ void t2p_read_tiff_size(T2P* t2p, TIFF* input){ void t2p_read_tiff_size_tile(T2P* t2p, TIFF* input, ttile_t tile){ - uint32* tbc = NULL; + uint64* tbc = NULL; uint16 edge=0; #ifdef JPEG_SUPPORT - uint16 xuint16=0; unsigned char* jpt; #endif + uint64 k; edge |= t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile); edge |= t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile); @@ -1882,41 +1951,60 @@ void t2p_read_tiff_size_tile(T2P* t2p, TIFF* input, ttile_t tile){ #endif ){ t2p->tiff_datasize=TIFFTileSize(input); + if (t2p->tiff_datasize == 0) { + /* Assume we had overflow inside TIFFTileSize */ + t2p->t2p_error = T2P_ERR_ERROR; + } return; } else { TIFFGetField(input, TIFFTAG_TILEBYTECOUNTS, &tbc); - t2p->tiff_datasize=tbc[tile]; + k=tbc[tile]; #ifdef OJPEG_SUPPORT if(t2p->tiff_compression==COMPRESSION_OJPEG){ - t2p->tiff_datasize+=2048; - return; + k = checkAdd64(k, 2048, t2p); } #endif #ifdef JPEG_SUPPORT - if(t2p->tiff_compression==COMPRESSION_JPEG){ - if(TIFFGetField(input, TIFFTAG_JPEGTABLES, &xuint16, &jpt)!=0){ - if(xuint16>4){ - t2p->tiff_datasize+=xuint16; - t2p->tiff_datasize-=4; /* don't use EOI of header or SOI of tile */ + if(t2p->tiff_compression==COMPRESSION_JPEG) { + uint32 count = 0; + if(TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt)!=0){ + if(count > 4){ + k = checkAdd64(k, count, t2p); + k -= 2; /* don't use EOI of header or SOI of tile */ } } } #endif + t2p->tiff_datasize = (tsize_t) k; + if ((uint64) t2p->tiff_datasize != k) { + TIFFError(TIFF2PDF_MODULE, "Integer overflow"); + t2p->t2p_error = T2P_ERR_ERROR; + } return; } } - t2p->tiff_datasize=TIFFTileSize(input); + k = TIFFTileSize(input); if(t2p->tiff_planar==PLANARCONFIG_SEPARATE){ - t2p->tiff_datasize*= t2p->tiff_samplesperpixel; + k = checkMultiply64(k, t2p->tiff_samplesperpixel, t2p); + } + if (k == 0) { + /* Assume we had overflow inside TIFFTileSize */ + t2p->t2p_error = T2P_ERR_ERROR; + } + + t2p->tiff_datasize = (tsize_t) k; + if ((uint64) t2p->tiff_datasize != k) { + TIFFError(TIFF2PDF_MODULE, "Integer overflow"); + t2p->t2p_error = T2P_ERR_ERROR; } return; } /* - This functions returns a non-zero value when the tile is on the right edge - and does not have full imaged tile width. -*/ + * This functions returns a non-zero value when the tile is on the right edge + * and does not have full imaged tile width. + */ int t2p_tile_is_right_edge(T2P_TILES tiles, ttile_t tile){ @@ -1926,15 +2014,12 @@ int t2p_tile_is_right_edge(T2P_TILES tiles, ttile_t tile){ } else { return(0); } - - return(0); - } /* - This functions returns a non-zero value when the tile is on the bottom edge - and does not have full imaged tile length. -*/ + * This functions returns a non-zero value when the tile is on the bottom edge + * and does not have full imaged tile length. + */ int t2p_tile_is_bottom_edge(T2P_TILES tiles, ttile_t tile){ @@ -1944,14 +2029,12 @@ int t2p_tile_is_bottom_edge(T2P_TILES tiles, ttile_t tile){ } else { return(0); } - - return(0); } /* - This function returns a non-zero value when the tile is a right edge tile or a bottom - edge tile. -*/ + * This function returns a non-zero value when the tile is a right edge tile + * or a bottom edge tile. + */ int t2p_tile_is_edge(T2P_TILES tiles, ttile_t tile){ @@ -1968,52 +2051,6 @@ int t2p_tile_is_corner_edge(T2P_TILES tiles, ttile_t tile){ return(t2p_tile_is_right_edge(tiles, tile) & t2p_tile_is_bottom_edge(tiles, tile) ); } -/* - This function is an empty (dummy) TIFFReadWriteProc that returns the amount - requested to be read without reading anything. -*/ - -tsize_t t2p_empty_readproc(thandle_t fd, tdata_t buf, tsize_t size){ - - (void) fd; (void) buf; (void) size; - - return (size); -} - -/* - This function is an empty (dummy) TIFFReadWriteProc that returns the amount - requested to be written without writing anything. -*/ - -tsize_t t2p_empty_writeproc(thandle_t fd, tdata_t buf, tsize_t size){ - - (void) fd; (void) buf; (void) size; - - return (size); -} - -/* - This function is an empty (dummy) TIFFSeekProc that returns off. -*/ - -toff_t t2p_empty_seekproc(thandle_t fd, toff_t off, int whence){ - - (void) fd; (void) off; (void) whence; - - return( off ); -} - -/* - This function is an empty (dummy) TIFFCloseProc that returns 0. -*/ - -int t2p_empty_closeproc(thandle_t fd){ - - (void) fd; - - return(0); -} - /* This function reads the raster image data from the input TIFF for an image and writes @@ -2044,87 +2081,115 @@ tsize_t t2p_readwrite_pdf_image(T2P* t2p, TIFF* input, TIFF* output){ #endif #ifdef JPEG_SUPPORT unsigned char* jpt; - uint16 xuint16_1=0; - uint16 xuint16_2=0; float* xfloatp; - uint32* sbc; + uint64* sbc; unsigned char* stripbuffer; tsize_t striplength=0; uint32 max_striplength=0; #endif + /* Fail if prior error (in particular, can't trust tiff_datasize) */ + if (t2p->t2p_error != T2P_ERR_OK) + return(0); + if(t2p->pdf_transcode == T2P_TRANSCODE_RAW){ #ifdef CCITT_SUPPORT if(t2p->pdf_compression == T2P_COMPRESS_G4){ - buffer= (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); - if(buffer==NULL){ + buffer = (unsigned char*) + _TIFFmalloc(t2p->tiff_datasize); + if (buffer == NULL) { TIFFError(TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", - t2p->tiff_datasize, + "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", + (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } - TIFFReadRawStrip(input, 0, (tdata_t) buffer, t2p->tiff_datasize); + TIFFReadRawStrip(input, 0, (tdata_t) buffer, + t2p->tiff_datasize); if (t2p->tiff_fillorder==FILLORDER_LSB2MSB){ - /* make sure is lsb-to-msb bit-endianness fill order */ - TIFFReverseBits(buffer, t2p->tiff_datasize); + /* + * make sure is lsb-to-msb + * bit-endianness fill order + */ + TIFFReverseBits(buffer, + t2p->tiff_datasize); } - TIFFWriteFile(output, (tdata_t) buffer, t2p->tiff_datasize); + t2pWriteFile(output, (tdata_t) buffer, + t2p->tiff_datasize); _TIFFfree(buffer); return(t2p->tiff_datasize); } #endif #ifdef ZIP_SUPPORT - if(t2p->pdf_compression == T2P_COMPRESS_ZIP){ - buffer= (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); - memset(buffer, 0, t2p->tiff_datasize); - if(buffer==NULL){ + if (t2p->pdf_compression == T2P_COMPRESS_ZIP) { + buffer = (unsigned char*) + _TIFFmalloc(t2p->tiff_datasize); + if(buffer == NULL){ TIFFError(TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", - t2p->tiff_datasize, + "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", + (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } - TIFFReadRawStrip(input, 0, (tdata_t) buffer, t2p->tiff_datasize); - if (t2p->tiff_fillorder==FILLORDER_LSB2MSB){ - TIFFReverseBits(buffer, t2p->tiff_datasize); + memset(buffer, 0, t2p->tiff_datasize); + TIFFReadRawStrip(input, 0, (tdata_t) buffer, + t2p->tiff_datasize); + if (t2p->tiff_fillorder==FILLORDER_LSB2MSB) { + TIFFReverseBits(buffer, + t2p->tiff_datasize); } - TIFFWriteFile(output, (tdata_t) buffer, t2p->tiff_datasize); + t2pWriteFile(output, (tdata_t) buffer, + t2p->tiff_datasize); _TIFFfree(buffer); return(t2p->tiff_datasize); } #endif #ifdef OJPEG_SUPPORT - if(t2p->tiff_compression == COMPRESSION_OJPEG){ + if(t2p->tiff_compression == COMPRESSION_OJPEG) { - if(t2p->tiff_dataoffset != 0){ - buffer= (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); - memset(buffer, 0, t2p->tiff_datasize); - if(buffer==NULL){ + if(t2p->tiff_dataoffset != 0) { + buffer = (unsigned char*) + _TIFFmalloc(t2p->tiff_datasize); + if(buffer == NULL) { TIFFError(TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", - t2p->tiff_datasize, + "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", + (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } + memset(buffer, 0, t2p->tiff_datasize); if(t2p->pdf_ojpegiflength==0){ - inputoffset=TIFFSeekFile(input, 0, SEEK_CUR); - TIFFSeekFile(input, t2p->tiff_dataoffset, SEEK_SET); - TIFFReadFile(input, (tdata_t) buffer, t2p->tiff_datasize); - TIFFSeekFile(input, inputoffset, SEEK_SET); - TIFFWriteFile(output, (tdata_t) buffer, t2p->tiff_datasize); + inputoffset=t2pSeekFile(input, 0, + SEEK_CUR); + t2pSeekFile(input, + t2p->tiff_dataoffset, + SEEK_SET); + t2pReadFile(input, (tdata_t) buffer, + t2p->tiff_datasize); + t2pSeekFile(input, inputoffset, + SEEK_SET); + t2pWriteFile(output, (tdata_t) buffer, + t2p->tiff_datasize); _TIFFfree(buffer); return(t2p->tiff_datasize); } else { - inputoffset=TIFFSeekFile(input, 0, SEEK_CUR); - TIFFSeekFile(input, t2p->tiff_dataoffset, SEEK_SET); - bufferoffset=TIFFReadFile(input, (tdata_t) buffer, t2p->pdf_ojpegiflength); - t2p->pdf_ojpegiflength=0; - TIFFSeekFile(input, inputoffset, SEEK_SET); - TIFFGetField(input, TIFFTAG_YCBCRSUBSAMPLING, &h_samp, &v_samp); + inputoffset=t2pSeekFile(input, 0, + SEEK_CUR); + t2pSeekFile(input, + t2p->tiff_dataoffset, + SEEK_SET); + bufferoffset = t2pReadFile(input, + (tdata_t) buffer, + t2p->pdf_ojpegiflength); + t2p->pdf_ojpegiflength = 0; + t2pSeekFile(input, inputoffset, + SEEK_SET); + TIFFGetField(input, + TIFFTAG_YCBCRSUBSAMPLING, + &h_samp, &v_samp); buffer[bufferoffset++]= 0xff; buffer[bufferoffset++]= 0xdd; buffer[bufferoffset++]= 0x00; @@ -2132,7 +2197,9 @@ tsize_t t2p_readwrite_pdf_image(T2P* t2p, TIFF* input, TIFF* output){ h_samp*=8; v_samp*=8; ri=(t2p->tiff_width+h_samp-1) / h_samp; - TIFFGetField(input, TIFFTAG_ROWSPERSTRIP, &rows); + TIFFGetField(input, + TIFFTAG_ROWSPERSTRIP, + &rows); ri*=(rows+v_samp-1)/v_samp; buffer[bufferoffset++]= (ri>>8) & 0xff; buffer[bufferoffset++]= ri & 0xff; @@ -2147,28 +2214,29 @@ tsize_t t2p_readwrite_pdf_image(T2P* t2p, TIFF* input, TIFF* output){ (tdata_t) &(((unsigned char*)buffer)[bufferoffset]), -1); } - TIFFWriteFile(output, (tdata_t) buffer, bufferoffset); + t2pWriteFile(output, (tdata_t) buffer, bufferoffset); _TIFFfree(buffer); return(bufferoffset); } } else { if(! t2p->pdf_ojpegdata){ TIFFError(TIFF2PDF_MODULE, - "No support for OJPEG image %s with bad tables", + "No support for OJPEG image %s with bad tables", TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } - buffer=(unsigned char*) _TIFFmalloc(t2p->tiff_datasize); - memset(buffer, 0, t2p->tiff_datasize); + buffer = (unsigned char*) + _TIFFmalloc(t2p->tiff_datasize); if(buffer==NULL){ TIFFError(TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", - t2p->tiff_datasize, + "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", + (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } + memset(buffer, 0, t2p->tiff_datasize); _TIFFmemcpy(buffer, t2p->pdf_ojpegdata, t2p->pdf_ojpegdatalength); bufferoffset=t2p->pdf_ojpegdatalength; stripcount=TIFFNumberOfStrips(input); @@ -2186,11 +2254,11 @@ tsize_t t2p_readwrite_pdf_image(T2P* t2p, TIFF* input, TIFF* output){ buffer[bufferoffset++]=0xff; buffer[bufferoffset++]=0xd9; } - TIFFWriteFile(output, (tdata_t) buffer, bufferoffset); + t2pWriteFile(output, (tdata_t) buffer, bufferoffset); _TIFFfree(buffer); return(bufferoffset); TIFFError(TIFF2PDF_MODULE, - "No support for OJPEG image %s with no JPEG File Interchange offset", + "No support for OJPEG image %s with no JPEG File Interchange offset", TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); @@ -2199,21 +2267,23 @@ tsize_t t2p_readwrite_pdf_image(T2P* t2p, TIFF* input, TIFF* output){ } #endif #ifdef JPEG_SUPPORT - if(t2p->tiff_compression == COMPRESSION_JPEG){ - buffer= (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); - memset(buffer, 0, t2p->tiff_datasize); + if(t2p->tiff_compression == COMPRESSION_JPEG) { + uint32 count = 0; + buffer = (unsigned char*) + _TIFFmalloc(t2p->tiff_datasize); if(buffer==NULL){ TIFFError(TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", - t2p->tiff_datasize, + "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", + (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } - if(TIFFGetField(input, TIFFTAG_JPEGTABLES, &xuint16_1, &jpt) != 0){ - if(xuint16_1>4){ - _TIFFmemcpy(buffer, jpt, xuint16_1); - bufferoffset+=xuint16_1-2; + memset(buffer, 0, t2p->tiff_datasize); + if (TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt) != 0) { + if(count > 4) { + _TIFFmemcpy(buffer, jpt, count); + bufferoffset += count - 2; } } stripcount=TIFFNumberOfStrips(input); @@ -2221,10 +2291,11 @@ tsize_t t2p_readwrite_pdf_image(T2P* t2p, TIFF* input, TIFF* output){ for(i=0;imax_striplength) max_striplength=sbc[i]; } - stripbuffer=(unsigned char*) _TIFFmalloc(max_striplength); + stripbuffer = (unsigned char*) + _TIFFmalloc(max_striplength); if(stripbuffer==NULL){ TIFFError(TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", + "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", max_striplength, TIFFFileName(input)); _TIFFfree(buffer); @@ -2241,7 +2312,7 @@ tsize_t t2p_readwrite_pdf_image(T2P* t2p, TIFF* input, TIFF* output){ i, t2p->tiff_length)){ TIFFError(TIFF2PDF_MODULE, - "Can't process JPEG data in input file %s", + "Can't process JPEG data in input file %s", TIFFFileName(input)); _TIFFfree(samplebuffer); _TIFFfree(buffer); @@ -2251,7 +2322,7 @@ tsize_t t2p_readwrite_pdf_image(T2P* t2p, TIFF* input, TIFF* output){ } buffer[bufferoffset++]=0xff; buffer[bufferoffset++]=0xd9; - TIFFWriteFile(output, (tdata_t) buffer, bufferoffset); + t2pWriteFile(output, (tdata_t) buffer, bufferoffset); _TIFFfree(stripbuffer); _TIFFfree(buffer); return(bufferoffset); @@ -2262,15 +2333,15 @@ tsize_t t2p_readwrite_pdf_image(T2P* t2p, TIFF* input, TIFF* output){ if(t2p->pdf_sample==T2P_SAMPLE_NOTHING){ buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); - memset(buffer, 0, t2p->tiff_datasize); if(buffer==NULL){ TIFFError(TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", - t2p->tiff_datasize, + "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", + (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } + memset(buffer, 0, t2p->tiff_datasize); stripsize=TIFFStripSize(input); stripcount=TIFFNumberOfStrips(input); for(i=0;itiff_samplesperpixel; buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); - memset(buffer, 0, t2p->tiff_datasize); if(buffer==NULL){ TIFFError(TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", - t2p->tiff_datasize, + "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", + (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } + memset(buffer, 0, t2p->tiff_datasize); samplebuffer = (unsigned char*) _TIFFmalloc(stripsize); if(samplebuffer==NULL){ TIFFError(TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", - t2p->tiff_datasize, + "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", + (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); @@ -2328,7 +2399,7 @@ tsize_t t2p_readwrite_pdf_image(T2P* t2p, TIFF* input, TIFF* output){ sepstripsize); if(read==-1){ TIFFError(TIFF2PDF_MODULE, - "Error on decoding strip %u of %s", + "Error on decoding strip %u of %s", i + j*stripcount, TIFFFileName(input)); _TIFFfree(buffer); @@ -2349,15 +2420,15 @@ tsize_t t2p_readwrite_pdf_image(T2P* t2p, TIFF* input, TIFF* output){ } buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); - memset(buffer, 0, t2p->tiff_datasize); if(buffer==NULL){ TIFFError(TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", - t2p->tiff_datasize, + "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", + (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } + memset(buffer, 0, t2p->tiff_datasize); stripsize=TIFFStripSize(input); stripcount=TIFFNumberOfStrips(input); for(i=0;ipdf_sample & T2P_SAMPLE_REALIZE_PALETTE){ + // FIXME: overflow? samplebuffer=(unsigned char*)_TIFFrealloc( (tdata_t) buffer, t2p->tiff_datasize * t2p->tiff_samplesperpixel); if(samplebuffer==NULL){ TIFFError(TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", - t2p->tiff_datasize, + "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", + (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; _TIFFfree(buffer); @@ -2415,8 +2487,8 @@ tsize_t t2p_readwrite_pdf_image(T2P* t2p, TIFF* input, TIFF* output){ t2p->tiff_width*t2p->tiff_length*4); if(samplebuffer==NULL){ TIFFError(TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", - t2p->tiff_datasize, + "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", + (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; _TIFFfree(buffer); @@ -2432,7 +2504,7 @@ tsize_t t2p_readwrite_pdf_image(T2P* t2p, TIFF* input, TIFF* output){ ORIENTATION_TOPLEFT, 0)){ TIFFError(TIFF2PDF_MODULE, - "Can't use TIFFReadRGBAImageOriented to extract RGB image from %s", + "Can't use TIFFReadRGBAImageOriented to extract RGB image from %s", TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); @@ -2450,11 +2522,9 @@ tsize_t t2p_readwrite_pdf_image(T2P* t2p, TIFF* input, TIFF* output){ } } - dataready: - - t2p->tiff_writeproc=output->tif_writeproc; - output->tif_writeproc=t2p_empty_writeproc; +dataready: + t2p_disable(output); TIFFSetField(output, TIFFTAG_PHOTOMETRIC, t2p->tiff_photometric); TIFFSetField(output, TIFFTAG_BITSPERSAMPLE, t2p->tiff_bitspersample); TIFFSetField(output, TIFFTAG_SAMPLESPERPIXEL, t2p->tiff_samplesperpixel); @@ -2475,10 +2545,11 @@ tsize_t t2p_readwrite_pdf_image(T2P* t2p, TIFF* input, TIFF* output){ #endif #ifdef JPEG_SUPPORT case T2P_COMPRESS_JPEG: - if(t2p->tiff_photometric==PHOTOMETRIC_YCBCR){ - if(TIFFGetField(input, TIFFTAG_YCBCRSUBSAMPLING, &xuint16_1, &xuint16_2)!=0){ - if(xuint16_1 != 0 && xuint16_2 != 0){ - TIFFSetField(output, TIFFTAG_YCBCRSUBSAMPLING, xuint16_1, xuint16_2); + if(t2p->tiff_photometric==PHOTOMETRIC_YCBCR) { + uint16 hor = 0, ver = 0; + if (TIFFGetField(input, TIFFTAG_YCBCRSUBSAMPLING, &hor, &ver) !=0 ) { + if(hor != 0 && ver != 0){ + TIFFSetField(output, TIFFTAG_YCBCRSUBSAMPLING, hor, ver); } } if(TIFFGetField(input, TIFFTAG_REFERENCEBLACKWHITE, &xfloatp)!=0){ @@ -2487,7 +2558,7 @@ tsize_t t2p_readwrite_pdf_image(T2P* t2p, TIFF* input, TIFF* output){ } if(TIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_JPEG)==0){ TIFFError(TIFF2PDF_MODULE, - "Unable to use JPEG compression for input %s and output %s", + "Unable to use JPEG compression for input %s and output %s", TIFFFileName(input), TIFFFileName(output)); _TIFFfree(buffer); @@ -2536,37 +2607,44 @@ tsize_t t2p_readwrite_pdf_image(T2P* t2p, TIFF* input, TIFF* output){ default: break; } - - output->tif_writeproc=t2p->tiff_writeproc; + + t2p_enable(output); + t2p->outputwritten = 0; #ifdef JPEG_SUPPORT - if(t2p->pdf_compression==T2P_COMPRESS_JPEG && t2p->tiff_photometric==PHOTOMETRIC_YCBCR){ - bufferoffset=TIFFWriteEncodedStrip(output, (tstrip_t)0, buffer,stripsize*stripcount); + if(t2p->pdf_compression == T2P_COMPRESS_JPEG + && t2p->tiff_photometric == PHOTOMETRIC_YCBCR){ + bufferoffset = TIFFWriteEncodedStrip(output, (tstrip_t)0, + buffer, + stripsize * stripcount); } else #endif - bufferoffset=TIFFWriteEncodedStrip(output, (tstrip_t)0, buffer, t2p->tiff_datasize); - if(buffer != NULL){ + { + bufferoffset = TIFFWriteEncodedStrip(output, (tstrip_t)0, + buffer, + t2p->tiff_datasize); + } + if (buffer != NULL) { _TIFFfree(buffer); buffer=NULL; } - if(bufferoffset==(tsize_t)-1){ + if (bufferoffset == (tsize_t)-1) { TIFFError(TIFF2PDF_MODULE, - "Error writing encoded strip to output PDF %s", - TIFFFileName(output)); + "Error writing encoded strip to output PDF %s", + TIFFFileName(output)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } - written= output->tif_dir.td_stripbytecount[0]; - + written = t2p->outputwritten; return(written); } /* - This function reads the raster image data from the input TIFF for an image tile and writes - the data to the output PDF XObject image dictionary stream for the tile. It returns the - amount written or zero on error. -*/ + * This function reads the raster image data from the input TIFF for an image + * tile and writes the data to the output PDF XObject image dictionary stream + * for the tile. It returns the amount written or zero on error. + */ tsize_t t2p_readwrite_pdf_image_tile(T2P* t2p, TIFF* input, TIFF* output, ttile_t tile){ @@ -2584,12 +2662,14 @@ tsize_t t2p_readwrite_pdf_image_tile(T2P* t2p, TIFF* input, TIFF* output, ttile_ tsize_t septilesize=0; #ifdef JPEG_SUPPORT unsigned char* jpt; - uint16 xuint16_1=0; - uint16 xuint16_2=0; float* xfloatp; uint32 xuint32=0; #endif + /* Fail if prior error (in particular, can't trust tiff_datasize) */ + if (t2p->t2p_error != T2P_ERR_OK) + return(0); + edge |= t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile); edge |= t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile); @@ -2604,9 +2684,9 @@ tsize_t t2p_readwrite_pdf_image_tile(T2P* t2p, TIFF* input, TIFF* output, ttile_ buffer= (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); if(buffer==NULL){ TIFFError(TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory " + "Can't allocate %lu bytes of memory " "for t2p_readwrite_pdf_image_tile, %s", - t2p->tiff_datasize, + (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); @@ -2615,7 +2695,7 @@ tsize_t t2p_readwrite_pdf_image_tile(T2P* t2p, TIFF* input, TIFF* output, ttile_ if (t2p->tiff_fillorder==FILLORDER_LSB2MSB){ TIFFReverseBits(buffer, t2p->tiff_datasize); } - TIFFWriteFile(output, (tdata_t) buffer, t2p->tiff_datasize); + t2pWriteFile(output, (tdata_t) buffer, t2p->tiff_datasize); _TIFFfree(buffer); return(t2p->tiff_datasize); } @@ -2625,9 +2705,9 @@ tsize_t t2p_readwrite_pdf_image_tile(T2P* t2p, TIFF* input, TIFF* output, ttile_ buffer= (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); if(buffer==NULL){ TIFFError(TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory " + "Can't allocate %lu bytes of memory " "for t2p_readwrite_pdf_image_tile, %s", - t2p->tiff_datasize, + (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); @@ -2636,7 +2716,7 @@ tsize_t t2p_readwrite_pdf_image_tile(T2P* t2p, TIFF* input, TIFF* output, ttile_ if (t2p->tiff_fillorder==FILLORDER_LSB2MSB){ TIFFReverseBits(buffer, t2p->tiff_datasize); } - TIFFWriteFile(output, (tdata_t) buffer, t2p->tiff_datasize); + t2pWriteFile(output, (tdata_t) buffer, t2p->tiff_datasize); _TIFFfree(buffer); return(t2p->tiff_datasize); } @@ -2654,9 +2734,9 @@ tsize_t t2p_readwrite_pdf_image_tile(T2P* t2p, TIFF* input, TIFF* output, ttile_ buffer=(unsigned char*) _TIFFmalloc(t2p->tiff_datasize); if(buffer==NULL){ TIFFError(TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory " + "Can't allocate %lu bytes of memory " "for t2p_readwrite_pdf_image, %s", - t2p->tiff_datasize, + (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); @@ -2683,7 +2763,7 @@ tsize_t t2p_readwrite_pdf_image_tile(T2P* t2p, TIFF* input, TIFF* output, ttile_ -1); ((unsigned char*)buffer)[bufferoffset++]=0xff; ((unsigned char*)buffer)[bufferoffset++]=0xd9; - TIFFWriteFile(output, (tdata_t) buffer, bufferoffset); + t2pWriteFile(output, (tdata_t) buffer, bufferoffset); _TIFFfree(buffer); return(bufferoffset); } @@ -2691,24 +2771,25 @@ tsize_t t2p_readwrite_pdf_image_tile(T2P* t2p, TIFF* input, TIFF* output, ttile_ #ifdef JPEG_SUPPORT if(t2p->tiff_compression == COMPRESSION_JPEG){ unsigned char table_end[2]; + uint32 count = 0; buffer= (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); if(buffer==NULL){ TIFFError(TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory " + "Can't allocate %lu bytes of memory " "for t2p_readwrite_pdf_image_tile, %s", t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } - if(TIFFGetField(input, TIFFTAG_JPEGTABLES, &xuint16_1, &jpt) != 0) { - if(xuint16_1 > 0){ - _TIFFmemcpy(buffer, jpt, xuint16_1); - bufferoffset += xuint16_1 - 2; + if(TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt) != 0) { + if (count > 0) { + _TIFFmemcpy(buffer, jpt, count); + bufferoffset += count - 2; table_end[0] = buffer[bufferoffset-2]; table_end[1] = buffer[bufferoffset-1]; } - if(xuint16_1 > 0) { + if (count > 0) { xuint32 = bufferoffset; bufferoffset += TIFFReadRawTile( input, @@ -2725,7 +2806,7 @@ tsize_t t2p_readwrite_pdf_image_tile(T2P* t2p, TIFF* input, TIFF* output, ttile_ -1); } } - TIFFWriteFile(output, (tdata_t) buffer, bufferoffset); + t2pWriteFile(output, (tdata_t) buffer, bufferoffset); _TIFFfree(buffer); return(bufferoffset); } @@ -2737,9 +2818,9 @@ tsize_t t2p_readwrite_pdf_image_tile(T2P* t2p, TIFF* input, TIFF* output, ttile_ buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); if(buffer==NULL){ TIFFError(TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory for " + "Can't allocate %lu bytes of memory for " "t2p_readwrite_pdf_image_tile, %s", - t2p->tiff_datasize, + (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); @@ -2770,9 +2851,9 @@ tsize_t t2p_readwrite_pdf_image_tile(T2P* t2p, TIFF* input, TIFF* output, ttile_ buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); if(buffer==NULL){ TIFFError(TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory " + "Can't allocate %lu bytes of memory " "for t2p_readwrite_pdf_image_tile, %s", - t2p->tiff_datasize, + (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); @@ -2780,9 +2861,9 @@ tsize_t t2p_readwrite_pdf_image_tile(T2P* t2p, TIFF* input, TIFF* output, ttile_ samplebuffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); if(samplebuffer==NULL){ TIFFError(TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory " + "Can't allocate %lu bytes of memory " "for t2p_readwrite_pdf_image_tile, %s", - t2p->tiff_datasize, + (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); @@ -2819,9 +2900,9 @@ tsize_t t2p_readwrite_pdf_image_tile(T2P* t2p, TIFF* input, TIFF* output, ttile_ buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize); if(buffer==NULL){ TIFFError(TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory " + "Can't allocate %lu bytes of memory " "for t2p_readwrite_pdf_image_tile, %s", - t2p->tiff_datasize, + (unsigned long) t2p->tiff_datasize, TIFFFileName(input)); t2p->t2p_error = T2P_ERR_ERROR; return(0); @@ -2882,9 +2963,8 @@ tsize_t t2p_readwrite_pdf_image_tile(T2P* t2p, TIFF* input, TIFF* output, ttile_ t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength); } - t2p->tiff_writeproc=output->tif_writeproc; - output->tif_writeproc=t2p_empty_writeproc; + t2p_disable(output); TIFFSetField(output, TIFFTAG_PHOTOMETRIC, t2p->tiff_photometric); TIFFSetField(output, TIFFTAG_BITSPERSAMPLE, t2p->tiff_bitspersample); TIFFSetField(output, TIFFTAG_SAMPLESPERPIXEL, t2p->tiff_samplesperpixel); @@ -2932,10 +3012,11 @@ tsize_t t2p_readwrite_pdf_image_tile(T2P* t2p, TIFF* input, TIFF* output, ttile_ #endif #ifdef JPEG_SUPPORT case T2P_COMPRESS_JPEG: - if(t2p->tiff_photometric==PHOTOMETRIC_YCBCR){ - if(TIFFGetField(input, TIFFTAG_YCBCRSUBSAMPLING, &xuint16_1, &xuint16_2)!=0){ - if(xuint16_1 != 0 && xuint16_2 != 0){ - TIFFSetField(output, TIFFTAG_YCBCRSUBSAMPLING, xuint16_1, xuint16_2); + if (t2p->tiff_photometric==PHOTOMETRIC_YCBCR) { + uint16 hor = 0, ver = 0; + if (TIFFGetField(input, TIFFTAG_YCBCRSUBSAMPLING, &hor, &ver)!=0) { + if (hor != 0 && ver != 0) { + TIFFSetField(output, TIFFTAG_YCBCRSUBSAMPLING, hor, ver); } } if(TIFFGetField(input, TIFFTAG_REFERENCEBLACKWHITE, &xfloatp)!=0){ @@ -2983,22 +3064,24 @@ tsize_t t2p_readwrite_pdf_image_tile(T2P* t2p, TIFF* input, TIFF* output, ttile_ default: break; } - - output->tif_writeproc=t2p->tiff_writeproc; - bufferoffset=TIFFWriteEncodedStrip(output, (tstrip_t) 0, buffer, TIFFStripSize(output)); - if(buffer != NULL){ + + t2p_enable(output); + t2p->outputwritten = 0; + bufferoffset = TIFFWriteEncodedStrip(output, (tstrip_t) 0, buffer, + TIFFStripSize(output)); + if (buffer != NULL) { _TIFFfree(buffer); - buffer=NULL; + buffer = NULL; } - if(bufferoffset==-1){ + if (bufferoffset == -1) { TIFFError(TIFF2PDF_MODULE, - "Error writing encoded tile to output PDF %s", - TIFFFileName(output)); + "Error writing encoded tile to output PDF %s", + TIFFFileName(output)); t2p->t2p_error = T2P_ERR_ERROR; return(0); } - written= output->tif_dir.td_stripbytecount[0]; + written = t2p->outputwritten; return(written); } @@ -3099,6 +3182,7 @@ int t2p_process_ojpeg_tables(T2P* t2p, TIFF* input){ "Can't allocate %u bytes of memory for t2p_process_ojpeg_tables, %s", 2048, TIFFFileName(input)); + t2p->t2p_error = T2P_ERR_ERROR; return(0); } _TIFFmemset(t2p->pdf_ojpegdata, 0x00, 2048); @@ -3267,6 +3351,9 @@ int t2p_process_jpeg_strip( while(i<(*striplength)){ switch( strip[i] ){ case 0xd8: + /* SOI - start of image */ + _TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i-1]), 2); + *bufferoffset+=2; i+=2; break; case 0xc0: @@ -3348,11 +3435,11 @@ void t2p_tile_collapse_left( uint32 edgetilewidth, uint32 tilelength){ - uint32 i=0; + uint32 i; tsize_t edgescanwidth=0; edgescanwidth = (scanwidth * edgetilewidth + (tilewidth - 1))/ tilewidth; - for(i=i;itiff_writeproc=output->tif_writeproc; - output->tif_writeproc=t2p_empty_writeproc; - t2p->tiff_readproc=output->tif_readproc; - output->tif_readproc=t2p_empty_readproc; - t2p->tiff_seekproc=output->tif_seekproc; - output->tif_seekproc=t2p_empty_seekproc; - output->tif_header.tiff_diroff=0; +void +t2p_write_advance_directory(T2P* t2p, TIFF* output) +{ + t2p_disable(output); if(!TIFFWriteDirectory(output)){ TIFFError(TIFF2PDF_MODULE, "Error writing virtual directory to output PDF %s", @@ -3385,10 +3467,7 @@ void t2p_write_advance_directory(T2P* t2p, TIFF* output){ t2p->t2p_error = T2P_ERR_ERROR; return; } - output->tif_writeproc=t2p->tiff_writeproc; - output->tif_readproc=t2p->tiff_readproc; - output->tif_seekproc=t2p->tiff_seekproc; - + t2p_enable(output); return; } @@ -3484,11 +3563,10 @@ t2p_sample_rgba_to_rgb(tdata_t data, uint32 samplecount) for (i = 0; i < samplecount; i++) { sample=((uint32*)data)[i]; - alpha=(uint8)((255 - (sample & 0xff))); - ((uint8 *)data)[i * 3] = (uint8) ((sample >> 24) & 0xff) + alpha; - ((uint8 *)data)[i * 3 + 1] = (uint8) ((sample >> 16) & 0xff) + alpha; - ((uint8 *)data)[i * 3 + 2] = (uint8) ((sample >> 8) & 0xff) + alpha; - + alpha=(uint8)((255 - ((sample >> 24) & 0xff))); + ((uint8 *)data)[i * 3] = (uint8) ((sample >> 16) & 0xff) + alpha; + ((uint8 *)data)[i * 3 + 1] = (uint8) ((sample >> 8) & 0xff) + alpha; + ((uint8 *)data)[i * 3 + 2] = (uint8) (sample & 0xff) + alpha; } return (i * 3); @@ -3532,8 +3610,8 @@ tsize_t t2p_write_pdf_header(T2P* t2p, TIFF* output){ int buflen=0; buflen=sprintf(buffer, "%%PDF-%u.%u ", t2p->pdf_majorversion&0xff, t2p->pdf_minorversion&0xff); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t)"\r%\342\343\317\323\r\n", 8); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t)"\n%\342\343\317\323\n", 7); return(written); } @@ -3549,8 +3627,8 @@ tsize_t t2p_write_pdf_obj_start(uint32 number, TIFF* output){ int buflen=0; buflen=sprintf(buffer, "%lu", (unsigned long)number); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen ); - written += TIFFWriteFile(output, (tdata_t) " 0 obj\r", 7); + written += t2pWriteFile(output, (tdata_t) buffer, buflen ); + written += t2pWriteFile(output, (tdata_t) " 0 obj\n", 7); return(written); } @@ -3563,7 +3641,7 @@ tsize_t t2p_write_pdf_obj_end(TIFF* output){ tsize_t written=0; - written += TIFFWriteFile(output, (tdata_t) "endobj\r", 7); + written += t2pWriteFile(output, (tdata_t) "endobj\n", 7); return(written); } @@ -3572,136 +3650,149 @@ tsize_t t2p_write_pdf_obj_end(TIFF* output){ This function writes a PDF name object to output. */ -tsize_t t2p_write_pdf_name(char* name, TIFF* output){ +tsize_t t2p_write_pdf_name(unsigned char* name, TIFF* output){ tsize_t written=0; uint32 i=0; - char buffer[4]; + char buffer[64]; uint16 nextchar=0; - uint32 namelen=0; + size_t namelen=0; - namelen=strlen(name); + namelen = strlen((char *)name); if (namelen>126) { namelen=126; } - written += TIFFWriteFile(output, (tdata_t) "/", 1); + written += t2pWriteFile(output, (tdata_t) "/", 1); for (i=0;i 0x7E){ - sprintf(buffer, "#%.2X", name[i]); - written += TIFFWriteFile(output, (tdata_t) buffer, 3); + sprintf(buffer, "#%.2X", name[i]); + buffer[sizeof(buffer) - 1] = '\0'; + written += t2pWriteFile(output, (tdata_t) buffer, 3); nextchar=1; } if (nextchar==0){ switch (name[i]){ case 0x23: - sprintf(buffer, "#%.2X", name[i]); - written += TIFFWriteFile(output, (tdata_t) buffer, 3); + sprintf(buffer, "#%.2X", name[i]); + buffer[sizeof(buffer) - 1] = '\0'; + written += t2pWriteFile(output, (tdata_t) buffer, 3); break; case 0x25: - sprintf(buffer, "#%.2X", name[i]); - written += TIFFWriteFile(output, (tdata_t) buffer, 3); + sprintf(buffer, "#%.2X", name[i]); + buffer[sizeof(buffer) - 1] = '\0'; + written += t2pWriteFile(output, (tdata_t) buffer, 3); break; case 0x28: - sprintf(buffer, "#%.2X", name[i]); - written += TIFFWriteFile(output, (tdata_t) buffer, 3); + sprintf(buffer, "#%.2X", name[i]); + buffer[sizeof(buffer) - 1] = '\0'; + written += t2pWriteFile(output, (tdata_t) buffer, 3); break; case 0x29: sprintf(buffer, "#%.2X", name[i]); - written += TIFFWriteFile(output, (tdata_t) buffer, 3); + buffer[sizeof(buffer) - 1] = '\0'; + written += t2pWriteFile(output, (tdata_t) buffer, 3); break; case 0x2F: sprintf(buffer, "#%.2X", name[i]); - written += TIFFWriteFile(output, (tdata_t) buffer, 3); + buffer[sizeof(buffer) - 1] = '\0'; + written += t2pWriteFile(output, (tdata_t) buffer, 3); break; case 0x3C: sprintf(buffer, "#%.2X", name[i]); - written += TIFFWriteFile(output, (tdata_t) buffer, 3); + buffer[sizeof(buffer) - 1] = '\0'; + written += t2pWriteFile(output, (tdata_t) buffer, 3); break; case 0x3E: - sprintf(buffer, "#%.2X", name[i]); - written += TIFFWriteFile(output, (tdata_t) buffer, 3); + sprintf(buffer, "#%.2X", name[i]); + buffer[sizeof(buffer) - 1] = '\0'; + written += t2pWriteFile(output, (tdata_t) buffer, 3); break; case 0x5B: sprintf(buffer, "#%.2X", name[i]); - written += TIFFWriteFile(output, (tdata_t) buffer, 3); + buffer[sizeof(buffer) - 1] = '\0'; + written += t2pWriteFile(output, (tdata_t) buffer, 3); break; case 0x5D: - sprintf(buffer, "#%.2X", name[i]); - written += TIFFWriteFile(output, (tdata_t) buffer, 3); + sprintf(buffer, "#%.2X", name[i]); + buffer[sizeof(buffer) - 1] = '\0'; + written += t2pWriteFile(output, (tdata_t) buffer, 3); break; case 0x7B: sprintf(buffer, "#%.2X", name[i]); - written += TIFFWriteFile(output, (tdata_t) buffer, 3); + buffer[sizeof(buffer) - 1] = '\0'; + written += t2pWriteFile(output, (tdata_t) buffer, 3); break; case 0x7D: sprintf(buffer, "#%.2X", name[i]); - written += TIFFWriteFile(output, (tdata_t) buffer, 3); + buffer[sizeof(buffer) - 1] = '\0'; + written += t2pWriteFile(output, (tdata_t) buffer, 3); break; default: - written += TIFFWriteFile(output, (tdata_t) &name[i], 1); + written += t2pWriteFile(output, (tdata_t) &name[i], 1); } } nextchar=0; } - written += TIFFWriteFile(output, (tdata_t) " ", 1); + written += t2pWriteFile(output, (tdata_t) " ", 1); return(written); } /* - This function writes a PDF string object to output. -*/ + * This function writes a PDF string object to output. + */ -tsize_t t2p_write_pdf_string(char* pdfstr, TIFF* output){ - - tsize_t written=0; - uint32 i=0; - char buffer[5]; - uint32 len=0; +tsize_t t2p_write_pdf_string(char* pdfstr, TIFF* output) +{ + tsize_t written = 0; + uint32 i = 0; + char buffer[64]; + size_t len = 0; - len=strlen(pdfstr); - written += TIFFWriteFile(output, (tdata_t) "(", 1); - for (i=0;i>\r", 4); + written += t2pWriteFile(output, (tdata_t) " >>\n", 4); return(written); } @@ -3805,34 +3896,35 @@ tsize_t t2p_write_pdf_stream_length(tsize_t len, TIFF* output){ int buflen=0; buflen=sprintf(buffer, "%lu", (unsigned long)len); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) "\r", 1); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) "\n", 1); return(written); } /* - This function writes the PDF Catalog structure to output. -*/ - -tsize_t t2p_write_pdf_catalog(T2P* t2p, TIFF* output){ + * This function writes the PDF Catalog structure to output. + */ - tsize_t written=0; +tsize_t t2p_write_pdf_catalog(T2P* t2p, TIFF* output) +{ + tsize_t written = 0; char buffer[16]; - int buflen=0; + int buflen = 0; - written += TIFFWriteFile(output, - (tdata_t)"<< \r/Type /Catalog \r/Pages ", + written += t2pWriteFile(output, + (tdata_t)"<< \n/Type /Catalog \n/Pages ", 27); - buflen=sprintf(buffer, "%lu", (unsigned long)t2p->pdf_pages); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen ); - written += TIFFWriteFile(output, (tdata_t) " 0 R \r", 6); + buflen = snprintf(buffer, sizeof(buffer), "%lu", (unsigned long)t2p->pdf_pages); + written += t2pWriteFile(output, (tdata_t) buffer, + TIFFmin((size_t)buflen, sizeof(buffer) - 1)); + written += t2pWriteFile(output, (tdata_t) " 0 R \n", 6); if(t2p->pdf_fitwindow){ - written += TIFFWriteFile(output, - (tdata_t) "/ViewerPreferences <>\r", + written += t2pWriteFile(output, + (tdata_t) "/ViewerPreferences <>\n", 39); } - written += TIFFWriteFile(output, (tdata_t)">>\r", 3); + written += t2pWriteFile(output, (tdata_t)">>\n", 3); return(written); } @@ -3841,148 +3933,130 @@ tsize_t t2p_write_pdf_catalog(T2P* t2p, TIFF* output){ This function writes the PDF Info structure to output. */ -tsize_t t2p_write_pdf_info(T2P* t2p, TIFF* input, TIFF* output){ - - tsize_t written=0; +tsize_t t2p_write_pdf_info(T2P* t2p, TIFF* input, TIFF* output) +{ + tsize_t written = 0; char* info; char buffer[512]; - int buflen=0; - - if(t2p->pdf_datetime==NULL){ + + if(t2p->pdf_datetime[0] == '\0') t2p_pdf_tifftime(t2p, input); - } - if(strlen(t2p->pdf_datetime) > 0){ - written += TIFFWriteFile(output, (tdata_t) "<< \r/CreationDate ", 18); + if (strlen(t2p->pdf_datetime) > 0) { + written += t2pWriteFile(output, (tdata_t) "<< \n/CreationDate ", 18); written += t2p_write_pdf_string(t2p->pdf_datetime, output); - written += TIFFWriteFile(output, (tdata_t) "\r/ModDate ", 10); + written += t2pWriteFile(output, (tdata_t) "\n/ModDate ", 10); written += t2p_write_pdf_string(t2p->pdf_datetime, output); } - written += TIFFWriteFile(output, (tdata_t) "\r/Producer ", 11); - _TIFFmemset((tdata_t)buffer, 0x00, 512); - buflen=sprintf(buffer, "libtiff / tiff2pdf - %d / %s", TIFFLIB_VERSION, T2P_VERSION); + written += t2pWriteFile(output, (tdata_t) "\n/Producer ", 11); + _TIFFmemset((tdata_t)buffer, 0x00, sizeof(buffer)); + snprintf(buffer, sizeof(buffer), "libtiff / tiff2pdf - %d", TIFFLIB_VERSION); written += t2p_write_pdf_string(buffer, output); - written += TIFFWriteFile(output, (tdata_t) "\r", 1); - if(t2p->pdf_creator != NULL){ - if(strlen(t2p->pdf_creator)>0){ - if(strlen(t2p->pdf_creator)>511){t2p->pdf_creator[512]=(char)0;} - written += TIFFWriteFile(output, (tdata_t) "/Creator ", 9); - written += t2p_write_pdf_string(t2p->pdf_creator, output); - written += TIFFWriteFile(output, (tdata_t) "\r", 1); - } - } else{ - if( TIFFGetField(input, TIFFTAG_SOFTWARE, &info) != 0){ - if(strlen(info)>511){info[512]=(char)0;} - written += TIFFWriteFile(output, (tdata_t) "/Creator ", 9); + written += t2pWriteFile(output, (tdata_t) "\n", 1); + if (t2p->pdf_creator[0] != '\0') { + written += t2pWriteFile(output, (tdata_t) "/Creator ", 9); + written += t2p_write_pdf_string(t2p->pdf_creator, output); + written += t2pWriteFile(output, (tdata_t) "\n", 1); + } else { + if (TIFFGetField(input, TIFFTAG_SOFTWARE, &info) != 0 && info) { + if(strlen(info) >= sizeof(t2p->pdf_creator)) + info[sizeof(t2p->pdf_creator) - 1] = '\0'; + written += t2pWriteFile(output, (tdata_t) "/Creator ", 9); written += t2p_write_pdf_string(info, output); - written += TIFFWriteFile(output, (tdata_t) "\r", 1); + written += t2pWriteFile(output, (tdata_t) "\n", 1); } } - if(t2p->pdf_author != NULL){ - if(strlen(t2p->pdf_author)>0){ - if(strlen(t2p->pdf_author)>511){t2p->pdf_author[512]=(char)0;} - written += TIFFWriteFile(output, (tdata_t) "/Author ", 8); - written += t2p_write_pdf_string(t2p->pdf_author, output); - written += TIFFWriteFile(output, (tdata_t) "\r", 1); - } - } else{ - if( TIFFGetField(input, TIFFTAG_ARTIST, &info) != 0){ - if(strlen(info)>511){info[512]=(char)0;} - written += TIFFWriteFile(output, (tdata_t) "/Author ", 8); - written += t2p_write_pdf_string(info, output); - written += TIFFWriteFile(output, (tdata_t) "\r", 1); - } else if ( TIFFGetField(input, TIFFTAG_COPYRIGHT, &info) != 0){ - if(strlen(info)>511){info[512]=(char)0;} - written += TIFFWriteFile(output, (tdata_t) "/Author ", 8); + if (t2p->pdf_author[0] != '\0') { + written += t2pWriteFile(output, (tdata_t) "/Author ", 8); + written += t2p_write_pdf_string(t2p->pdf_author, output); + written += t2pWriteFile(output, (tdata_t) "\n", 1); + } else { + if ((TIFFGetField(input, TIFFTAG_ARTIST, &info) != 0 + || TIFFGetField(input, TIFFTAG_COPYRIGHT, &info) != 0) + && info) { + if (strlen(info) >= sizeof(t2p->pdf_author)) + info[sizeof(t2p->pdf_author) - 1] = '\0'; + written += t2pWriteFile(output, (tdata_t) "/Author ", 8); written += t2p_write_pdf_string(info, output); - written += TIFFWriteFile(output, (tdata_t) "\r", 1); - } - } - if(t2p->pdf_title != NULL){ - if(strlen(t2p->pdf_title)>0){ - if(strlen(t2p->pdf_title)>511){t2p->pdf_title[512]=(char)0;} - written += TIFFWriteFile(output, (tdata_t) "/Title ", 7); - written += t2p_write_pdf_string(t2p->pdf_title, output); - written += TIFFWriteFile(output, (tdata_t) "\r", 1); + written += t2pWriteFile(output, (tdata_t) "\n", 1); } - } else{ - if( TIFFGetField(input, TIFFTAG_DOCUMENTNAME, &info) != 0){ - if(strlen(info)>511){info[512]=(char)0;} - written += TIFFWriteFile(output, (tdata_t) "/Title ", 7); + } + if (t2p->pdf_title[0] != '\0') { + written += t2pWriteFile(output, (tdata_t) "/Title ", 7); + written += t2p_write_pdf_string(t2p->pdf_title, output); + written += t2pWriteFile(output, (tdata_t) "\n", 1); + } else { + if (TIFFGetField(input, TIFFTAG_DOCUMENTNAME, &info) != 0){ + if(strlen(info) > 511) { + info[512] = '\0'; + } + written += t2pWriteFile(output, (tdata_t) "/Title ", 7); written += t2p_write_pdf_string(info, output); - written += TIFFWriteFile(output, (tdata_t) "\r", 1); + written += t2pWriteFile(output, (tdata_t) "\n", 1); } } - if(t2p->pdf_subject != NULL){ - if(strlen(t2p->pdf_subject)>0){ - if(strlen(t2p->pdf_subject)>511){t2p->pdf_subject[512]=(char)0;} - written += TIFFWriteFile(output, (tdata_t) "/Subject ", 9); - written += t2p_write_pdf_string(t2p->pdf_subject, output); - written += TIFFWriteFile(output, (tdata_t) "\r", 1); - } - } else{ - if( TIFFGetField(input, TIFFTAG_IMAGEDESCRIPTION, &info) != 0){ - if(strlen(info)>511){info[512]=(char)0;} - written += TIFFWriteFile(output, (tdata_t) "/Subject ", 9); + if (t2p->pdf_subject[0] != '\0') { + written += t2pWriteFile(output, (tdata_t) "/Subject ", 9); + written += t2p_write_pdf_string(t2p->pdf_subject, output); + written += t2pWriteFile(output, (tdata_t) "\n", 1); + } else { + if (TIFFGetField(input, TIFFTAG_IMAGEDESCRIPTION, &info) != 0 && info) { + if (strlen(info) >= sizeof(t2p->pdf_subject)) + info[sizeof(t2p->pdf_subject) - 1] = '\0'; + written += t2pWriteFile(output, (tdata_t) "/Subject ", 9); written += t2p_write_pdf_string(info, output); - written += TIFFWriteFile(output, (tdata_t) "\r", 1); + written += t2pWriteFile(output, (tdata_t) "\n", 1); } } - if(t2p->pdf_keywords != NULL){ - if(strlen(t2p->pdf_keywords)>0){ - if(strlen(t2p->pdf_keywords)>511){t2p->pdf_keywords[512]=(char)0;} - written += TIFFWriteFile(output, (tdata_t) "/Keywords ", 10); - written += t2p_write_pdf_string(t2p->pdf_keywords, output); - written += TIFFWriteFile(output, (tdata_t) "\r", 1); - } + if (t2p->pdf_keywords[0] != '\0') { + written += t2pWriteFile(output, (tdata_t) "/Keywords ", 10); + written += t2p_write_pdf_string(t2p->pdf_keywords, output); + written += t2pWriteFile(output, (tdata_t) "\n", 1); } - written += TIFFWriteFile(output, (tdata_t) ">> \r", 4); + written += t2pWriteFile(output, (tdata_t) ">> \n", 4); return(written); } /* - This function fills a string of a T2P struct with the current time as a PDF date string, - it is called by t2p_pdf_tifftime. -*/ - -void t2p_pdf_currenttime(T2P* t2p){ + * This function fills a string of a T2P struct with the current time as a PDF + * date string, it is called by t2p_pdf_tifftime. + */ +void t2p_pdf_currenttime(T2P* t2p) +{ struct tm* currenttime; time_t timenow; - timenow=time(0); - currenttime=localtime(&timenow); - sprintf(t2p->pdf_datetime, "D:%.4d%.2d%.2d%.2d%.2d%.2d", - (currenttime->tm_year+1900) % 65536, - (currenttime->tm_mon+1) % 256, - (currenttime->tm_mday) % 256, - (currenttime->tm_hour) % 256, - (currenttime->tm_min) % 256, - (currenttime->tm_sec) % 256); + if (time(&timenow) == (time_t) -1) { + TIFFError(TIFF2PDF_MODULE, + "Can't get the current time: %s", strerror(errno)); + timenow = (time_t) 0; + } + + currenttime = localtime(&timenow); + snprintf(t2p->pdf_datetime, sizeof(t2p->pdf_datetime), + "D:%.4d%.2d%.2d%.2d%.2d%.2d", + (currenttime->tm_year + 1900) % 65536, + (currenttime->tm_mon + 1) % 256, + (currenttime->tm_mday) % 256, + (currenttime->tm_hour) % 256, + (currenttime->tm_min) % 256, + (currenttime->tm_sec) % 256); return; } /* - This function fills a string of a T2P struct with the date and time of a TIFF file if it - exists or the current time as a PDF date string. -*/ - -void t2p_pdf_tifftime(T2P* t2p, TIFF* input){ + * This function fills a string of a T2P struct with the date and time of a + * TIFF file if it exists or the current time as a PDF date string. + */ +void t2p_pdf_tifftime(T2P* t2p, TIFF* input) +{ char* datetime; - t2p->pdf_datetime= (char*) _TIFFmalloc(19); - if(t2p->pdf_datetime==NULL){ - TIFFError(TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory for t2p_pdf_tiff_time", - 17); - t2p->t2p_error = T2P_ERR_ERROR; - return; - } - t2p->pdf_datetime[16]=0; - if( TIFFGetField(input, TIFFTAG_DATETIME, &datetime) != 0 - && (strlen(datetime) >= 19) ){ + if (TIFFGetField(input, TIFFTAG_DATETIME, &datetime) != 0 + && (strlen(datetime) >= 19) ){ t2p->pdf_datetime[0]='D'; t2p->pdf_datetime[1]=':'; t2p->pdf_datetime[2]=datetime[0]; @@ -3999,6 +4073,7 @@ void t2p_pdf_tifftime(T2P* t2p, TIFF* input){ t2p->pdf_datetime[13]=datetime[15]; t2p->pdf_datetime[14]=datetime[17]; t2p->pdf_datetime[15]=datetime[18]; + t2p->pdf_datetime[16] = '\0'; } else { t2p_pdf_currenttime(t2p); } @@ -4007,28 +4082,26 @@ void t2p_pdf_tifftime(T2P* t2p, TIFF* input){ } /* - This function writes a PDF Pages Tree structure to output. -*/ - -tsize_t t2p_write_pdf_pages(T2P* t2p, - TIFF* output){ + * This function writes a PDF Pages Tree structure to output. + */ +tsize_t t2p_write_pdf_pages(T2P* t2p, TIFF* output) +{ tsize_t written=0; tdir_t i=0; char buffer[16]; int buflen=0; int page=0; - written += TIFFWriteFile(output, - (tdata_t) "<< \r/Type /Pages \r/Kids [ ", - 26); + written += t2pWriteFile(output, + (tdata_t) "<< \n/Type /Pages \n/Kids [ ", 26); page = t2p->pdf_pages+1; for (i=0;itiff_pagecount;i++){ buflen=sprintf(buffer, "%d", page); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) " 0 R ", 5); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) " 0 R ", 5); if ( ((i+1)%8)==0 ) { - written += TIFFWriteFile(output, (tdata_t) "\r", 1); + written += t2pWriteFile(output, (tdata_t) "\n", 1); } page +=3; page += t2p->tiff_pages[i].page_extra; @@ -4038,11 +4111,11 @@ tsize_t t2p_write_pdf_pages(T2P* t2p, page +=2; } } - written += TIFFWriteFile(output, (tdata_t) "] \r/Count ", 10); + written += t2pWriteFile(output, (tdata_t) "] \n/Count ", 10); _TIFFmemset(buffer, 0x00, 16); buflen=sprintf(buffer, "%d", t2p->tiff_pagecount); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) " \r>> \r", 6); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) " \n>> \n", 6); return(written); } @@ -4057,87 +4130,87 @@ tsize_t t2p_write_pdf_page(uint32 object, T2P* t2p, TIFF* output){ tsize_t written=0; char buffer[16]; int buflen=0; - - written += TIFFWriteFile(output, (tdata_t) "<<\r/Type /Page \r/Parent ", 24); + + written += t2pWriteFile(output, (tdata_t) "<<\n/Type /Page \n/Parent ", 24); buflen=sprintf(buffer, "%lu", (unsigned long)t2p->pdf_pages); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) " 0 R \r", 6); - written += TIFFWriteFile(output, (tdata_t) "/MediaBox [", 11); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) " 0 R \n", 6); + written += t2pWriteFile(output, (tdata_t) "/MediaBox [", 11); buflen=sprintf(buffer, "%.4f",t2p->pdf_mediabox.x1); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) " ", 1); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) " ", 1); buflen=sprintf(buffer, "%.4f",t2p->pdf_mediabox.y1); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) " ", 1); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) " ", 1); buflen=sprintf(buffer, "%.4f",t2p->pdf_mediabox.x2); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) " ", 1); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) " ", 1); buflen=sprintf(buffer, "%.4f",t2p->pdf_mediabox.y2); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) "] \r", 3); - written += TIFFWriteFile(output, (tdata_t) "/Contents ", 10); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) "] \n", 3); + written += t2pWriteFile(output, (tdata_t) "/Contents ", 10); buflen=sprintf(buffer, "%lu", (unsigned long)(object + 1)); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) " 0 R \r", 6); - written += TIFFWriteFile(output, (tdata_t) "/Resources << \r", 15); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) " 0 R \n", 6); + written += t2pWriteFile(output, (tdata_t) "/Resources << \n", 15); if( t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount != 0 ){ - written += TIFFWriteFile(output, (tdata_t) "/XObject <<\r", 12); + written += t2pWriteFile(output, (tdata_t) "/XObject <<\n", 12); for(i=0;itiff_tiles[t2p->pdf_page].tiles_tilecount;i++){ - written += TIFFWriteFile(output, (tdata_t) "/Im", 3); + written += t2pWriteFile(output, (tdata_t) "/Im", 3); buflen = sprintf(buffer, "%u", t2p->pdf_page+1); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) "_", 1); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) "_", 1); buflen = sprintf(buffer, "%u", i+1); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) " ", 1); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) " ", 1); buflen = sprintf( buffer, "%lu", (unsigned long)(object+3+(2*i)+t2p->tiff_pages[t2p->pdf_page].page_extra)); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) " 0 R ", 5); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) " 0 R ", 5); if(i%4==3){ - written += TIFFWriteFile(output, (tdata_t) "\r", 1); + written += t2pWriteFile(output, (tdata_t) "\n", 1); } } - written += TIFFWriteFile(output, (tdata_t) ">>\r", 3); + written += t2pWriteFile(output, (tdata_t) ">>\n", 3); } else { - written += TIFFWriteFile(output, (tdata_t) "/XObject <<\r", 12); - written += TIFFWriteFile(output, (tdata_t) "/Im", 3); + written += t2pWriteFile(output, (tdata_t) "/XObject <<\n", 12); + written += t2pWriteFile(output, (tdata_t) "/Im", 3); buflen = sprintf(buffer, "%u", t2p->pdf_page+1); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) " ", 1); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) " ", 1); buflen = sprintf( buffer, "%lu", (unsigned long)(object+3+(2*i)+t2p->tiff_pages[t2p->pdf_page].page_extra)); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) " 0 R ", 5); - written += TIFFWriteFile(output, (tdata_t) ">>\r", 3); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) " 0 R ", 5); + written += t2pWriteFile(output, (tdata_t) ">>\n", 3); } if(t2p->tiff_transferfunctioncount != 0) { - written += TIFFWriteFile(output, (tdata_t) "/ExtGState <<", 13); - TIFFWriteFile(output, (tdata_t) "/GS1 ", 5); + written += t2pWriteFile(output, (tdata_t) "/ExtGState <<", 13); + t2pWriteFile(output, (tdata_t) "/GS1 ", 5); buflen = sprintf( buffer, "%lu", (unsigned long)(object + 3)); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) " 0 R ", 5); - written += TIFFWriteFile(output, (tdata_t) ">> \r", 4); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) " 0 R ", 5); + written += t2pWriteFile(output, (tdata_t) ">> \n", 4); } - written += TIFFWriteFile(output, (tdata_t) "/ProcSet [ ", 11); + written += t2pWriteFile(output, (tdata_t) "/ProcSet [ ", 11); if(t2p->pdf_colorspace == T2P_CS_BILEVEL || t2p->pdf_colorspace == T2P_CS_GRAY ){ - written += TIFFWriteFile(output, (tdata_t) "/ImageB ", 8); + written += t2pWriteFile(output, (tdata_t) "/ImageB ", 8); } else { - written += TIFFWriteFile(output, (tdata_t) "/ImageC ", 8); + written += t2pWriteFile(output, (tdata_t) "/ImageC ", 8); if(t2p->pdf_colorspace & T2P_CS_PALETTE){ - written += TIFFWriteFile(output, (tdata_t) "/ImageI ", 8); + written += t2pWriteFile(output, (tdata_t) "/ImageI ", 8); } } - written += TIFFWriteFile(output, (tdata_t) "]\r>>\r>>\r", 8); + written += t2pWriteFile(output, (tdata_t) "]\n>>\n>>\n", 8); return(written); } @@ -4158,22 +4231,40 @@ void t2p_compose_pdf_page(T2P* t2p){ uint32 tilelength=0; int istiled=0; float f=0; + float width_ratio=0; + float length_ratio=0; t2p->pdf_xres = t2p->tiff_xres; t2p->pdf_yres = t2p->tiff_yres; - if(t2p->pdf_overrideres){ + if(t2p->pdf_overrideres) { t2p->pdf_xres = t2p->pdf_defaultxres; t2p->pdf_yres = t2p->pdf_defaultyres; } - if(t2p->pdf_xres==0.0){ + if(t2p->pdf_xres == 0.0) t2p->pdf_xres = t2p->pdf_defaultxres; - } - if(t2p->pdf_yres==0.0){ + if(t2p->pdf_yres == 0.0) t2p->pdf_yres = t2p->pdf_defaultyres; + if (t2p->pdf_image_fillpage) { + width_ratio = t2p->pdf_defaultpagewidth/t2p->tiff_width; + length_ratio = t2p->pdf_defaultpagelength/t2p->tiff_length; + if (width_ratio < length_ratio ) { + t2p->pdf_imagewidth = t2p->pdf_defaultpagewidth; + t2p->pdf_imagelength = t2p->tiff_length * width_ratio; + } else { + t2p->pdf_imagewidth = t2p->tiff_width * length_ratio; + t2p->pdf_imagelength = t2p->pdf_defaultpagelength; + } + } else if (t2p->tiff_resunit != RESUNIT_CENTIMETER /* RESUNIT_NONE and */ + && t2p->tiff_resunit != RESUNIT_INCH) { /* other cases */ + t2p->pdf_imagewidth = ((float)(t2p->tiff_width))/t2p->pdf_xres; + t2p->pdf_imagelength = ((float)(t2p->tiff_length))/t2p->pdf_yres; + } else { + t2p->pdf_imagewidth = + ((float)(t2p->tiff_width))*PS_UNIT_SIZE/t2p->pdf_xres; + t2p->pdf_imagelength = + ((float)(t2p->tiff_length))*PS_UNIT_SIZE/t2p->pdf_yres; } - t2p->pdf_imagewidth=((float)(t2p->tiff_width)) *72.0F / t2p->pdf_xres; - t2p->pdf_imagelength=((float)(t2p->tiff_length)) *72.0F / t2p->pdf_yres; - if(t2p->pdf_overridepagesize != 0){ + if(t2p->pdf_overridepagesize != 0) { t2p->pdf_pagewidth = t2p->pdf_defaultpagewidth; t2p->pdf_pagelength = t2p->pdf_defaultpagelength; } else { @@ -4476,7 +4567,7 @@ tsize_t t2p_write_pdf_page_content_stream(T2P* t2p, TIFF* output){ for(i=0;itiff_tiles[t2p->pdf_page].tiles_tilecount; i++){ box=t2p->tiff_tiles[t2p->pdf_page].tiles_tiles[i].tile_box; buflen=sprintf(buffer, - "q %s %.4f %.4f %.4f %.4f %.4f %.4f cm /Im%d_%ld Do Q\r", + "q %s %.4f %.4f %.4f %.4f %.4f %.4f cm /Im%d_%ld Do Q\n", t2p->tiff_transferfunctioncount?"/GS1 gs ":"", box.mat[0], box.mat[1], @@ -4491,7 +4582,7 @@ tsize_t t2p_write_pdf_page_content_stream(T2P* t2p, TIFF* output){ } else { box=t2p->pdf_imagebox; buflen=sprintf(buffer, - "q %s %.4f %.4f %.4f %.4f %.4f %.4f cm /Im%d Do Q\r", + "q %s %.4f %.4f %.4f %.4f %.4f %.4f cm /Im%d Do Q\n", t2p->tiff_transferfunctioncount?"/GS1 gs ":"", box.mat[0], box.mat[1], @@ -4519,17 +4610,17 @@ tsize_t t2p_write_pdf_xobject_stream_dict(ttile_t tile, int buflen=0; written += t2p_write_pdf_stream_dict(0, t2p->pdf_xrefcount+1, output); - written += TIFFWriteFile(output, - (tdata_t) "/Type /XObject \r/Subtype /Image \r/Name /Im", + written += t2pWriteFile(output, + (tdata_t) "/Type /XObject \n/Subtype /Image \n/Name /Im", 42); buflen=sprintf(buffer, "%u", t2p->pdf_page+1); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); if(tile != 0){ - written += TIFFWriteFile(output, (tdata_t) "_", 1); + written += t2pWriteFile(output, (tdata_t) "_", 1); buflen=sprintf(buffer, "%lu", (unsigned long)tile); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); } - written += TIFFWriteFile(output, (tdata_t) "\r/Width ", 8); + written += t2pWriteFile(output, (tdata_t) "\n/Width ", 8); _TIFFmemset((tdata_t)buffer, 0x00, 16); if(tile==0){ buflen=sprintf(buffer, "%lu", (unsigned long)t2p->tiff_width); @@ -4546,8 +4637,8 @@ tsize_t t2p_write_pdf_xobject_stream_dict(ttile_t tile, (unsigned long)t2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth); } } - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) "\r/Height ", 9); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) "\n/Height ", 9); _TIFFmemset((tdata_t)buffer, 0x00, 16); if(tile==0){ buflen=sprintf(buffer, "%lu", (unsigned long)t2p->tiff_length); @@ -4564,16 +4655,16 @@ tsize_t t2p_write_pdf_xobject_stream_dict(ttile_t tile, (unsigned long)t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength); } } - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) "\r/BitsPerComponent ", 19); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) "\n/BitsPerComponent ", 19); _TIFFmemset((tdata_t)buffer, 0x00, 16); buflen=sprintf(buffer, "%u", t2p->tiff_bitspersample); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) "\r/ColorSpace ", 13); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) "\n/ColorSpace ", 13); written += t2p_write_pdf_xobject_cs(t2p, output); if (t2p->pdf_image_interpolate) - written += TIFFWriteFile(output, - (tdata_t) "\r/Interpolate true", 18); + written += t2pWriteFile(output, + (tdata_t) "\n/Interpolate true", 18); if( (t2p->pdf_switchdecode != 0) #ifdef CCITT_SUPPORT && ! (t2p->pdf_colorspace == T2P_CS_BILEVEL @@ -4607,66 +4698,58 @@ tsize_t t2p_write_pdf_xobject_cs(T2P* t2p, TIFF* output){ return(written); } if( (t2p->pdf_colorspace & T2P_CS_PALETTE) != 0){ - written += TIFFWriteFile(output, (tdata_t) "[ /Indexed ", 11); + written += t2pWriteFile(output, (tdata_t) "[ /Indexed ", 11); t2p->pdf_colorspace ^= T2P_CS_PALETTE; written += t2p_write_pdf_xobject_cs(t2p, output); t2p->pdf_colorspace |= T2P_CS_PALETTE; buflen=sprintf(buffer, "%u", (0x0001 << t2p->tiff_bitspersample)-1 ); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) " ", 1); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) " ", 1); _TIFFmemset(buffer, 0x00, 16); buflen=sprintf(buffer, "%lu", (unsigned long)t2p->pdf_palettecs ); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) " 0 R ]\r", 7); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) " 0 R ]\n", 7); return(written); } if(t2p->pdf_colorspace & T2P_CS_BILEVEL){ - written += TIFFWriteFile(output, (tdata_t) "/DeviceGray \r", 13); + written += t2pWriteFile(output, (tdata_t) "/DeviceGray \n", 13); } if(t2p->pdf_colorspace & T2P_CS_GRAY){ if(t2p->pdf_colorspace & T2P_CS_CALGRAY){ written += t2p_write_pdf_xobject_calcs(t2p, output); } else { - written += TIFFWriteFile(output, (tdata_t) "/DeviceGray \r", 13); + written += t2pWriteFile(output, (tdata_t) "/DeviceGray \n", 13); } } if(t2p->pdf_colorspace & T2P_CS_RGB){ if(t2p->pdf_colorspace & T2P_CS_CALRGB){ written += t2p_write_pdf_xobject_calcs(t2p, output); } else { - written += TIFFWriteFile(output, (tdata_t) "/DeviceRGB \r", 12); + written += t2pWriteFile(output, (tdata_t) "/DeviceRGB \n", 12); } } if(t2p->pdf_colorspace & T2P_CS_CMYK){ - written += TIFFWriteFile(output, (tdata_t) "/DeviceCMYK \r", 13); + written += t2pWriteFile(output, (tdata_t) "/DeviceCMYK \n", 13); } if(t2p->pdf_colorspace & T2P_CS_LAB){ - written += TIFFWriteFile(output, (tdata_t) "[/Lab << \r", 10); - written += TIFFWriteFile(output, (tdata_t) "/WhitePoint ", 12); + written += t2pWriteFile(output, (tdata_t) "[/Lab << \n", 10); + written += t2pWriteFile(output, (tdata_t) "/WhitePoint ", 12); X_W = t2p->tiff_whitechromaticities[0]; Y_W = t2p->tiff_whitechromaticities[1]; Z_W = 1.0F - (X_W + Y_W); X_W /= Y_W; Z_W /= Y_W; Y_W = 1.0F; - buflen=sprintf(buffer, "[%.4f %.4f %.4f] \r", X_W, Y_W, Z_W); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - X_W = 0.3457F; /* 0.3127F; */ /* D50, commented D65 */ - Y_W = 0.3585F; /* 0.3290F; */ - Z_W = 1.0F - (X_W + Y_W); - X_W /= Y_W; - Z_W /= Y_W; - Y_W = 1.0F; - buflen=sprintf(buffer, "[%.4f %.4f %.4f] \r", X_W, Y_W, Z_W); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) "/Range ", 7); - buflen=sprintf(buffer, "[%d %d %d %d] \r", + buflen=sprintf(buffer, "[%.4f %.4f %.4f] \n", X_W, Y_W, Z_W); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) "/Range ", 7); + buflen=sprintf(buffer, "[%d %d %d %d] \n", t2p->pdf_labrange[0], t2p->pdf_labrange[1], t2p->pdf_labrange[2], t2p->pdf_labrange[3]); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) ">>] \r", 5); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) ">>] \n", 5); } @@ -4679,30 +4762,30 @@ tsize_t t2p_write_pdf_transfer(T2P* t2p, TIFF* output){ char buffer[16]; int buflen=0; - written += TIFFWriteFile(output, (tdata_t) "<< /Type /ExtGState \r/TR ", 25); + written += t2pWriteFile(output, (tdata_t) "<< /Type /ExtGState \n/TR ", 25); if(t2p->tiff_transferfunctioncount == 1){ buflen=sprintf(buffer, "%lu", (unsigned long)(t2p->pdf_xrefcount + 1)); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) " 0 R ", 5); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) " 0 R ", 5); } else { - written += TIFFWriteFile(output, (tdata_t) "[ ", 2); + written += t2pWriteFile(output, (tdata_t) "[ ", 2); buflen=sprintf(buffer, "%lu", (unsigned long)(t2p->pdf_xrefcount + 1)); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) " 0 R ", 5); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) " 0 R ", 5); buflen=sprintf(buffer, "%lu", (unsigned long)(t2p->pdf_xrefcount + 2)); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) " 0 R ", 5); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) " 0 R ", 5); buflen=sprintf(buffer, "%lu", (unsigned long)(t2p->pdf_xrefcount + 3)); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) " 0 R ", 5); - written += TIFFWriteFile(output, (tdata_t) "/Identity ] ", 12); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) " 0 R ", 5); + written += t2pWriteFile(output, (tdata_t) "/Identity ] ", 12); } - written += TIFFWriteFile(output, (tdata_t) " >> \r", 5); + written += t2pWriteFile(output, (tdata_t) " >> \n", 5); return(written); } @@ -4712,15 +4795,15 @@ tsize_t t2p_write_pdf_transfer_dict(T2P* t2p, TIFF* output, uint16 i){ tsize_t written=0; char buffer[32]; int buflen=0; - (void)i; // XXX + (void)i; /* XXX */ - written += TIFFWriteFile(output, (tdata_t) "/FunctionType 0 \r", 17); - written += TIFFWriteFile(output, (tdata_t) "/Domain [0.0 1.0] \r", 19); - written += TIFFWriteFile(output, (tdata_t) "/Range [0.0 1.0] \r", 18); - buflen=sprintf(buffer, "/Size [%u] \r", (1<tiff_bitspersample)); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) "/BitsPerSample 16 \r", 19); - written += t2p_write_pdf_stream_dict(1<<(t2p->tiff_bitspersample+1), 0, output); + written += t2pWriteFile(output, (tdata_t) "/FunctionType 0 \n", 17); + written += t2pWriteFile(output, (tdata_t) "/Domain [0.0 1.0] \n", 19); + written += t2pWriteFile(output, (tdata_t) "/Range [0.0 1.0] \n", 18); + buflen=sprintf(buffer, "/Size [%u] \n", (1<tiff_bitspersample)); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) "/BitsPerSample 16 \n", 19); + written += t2p_write_pdf_stream_dict(((tsize_t)1)<<(t2p->tiff_bitspersample+1), 0, output); return(written); } @@ -4731,7 +4814,7 @@ tsize_t t2p_write_pdf_transfer_stream(T2P* t2p, TIFF* output, uint16 i){ written += t2p_write_pdf_stream( t2p->tiff_transferfunction[i], - (1<<(t2p->tiff_bitspersample+1)), + (((tsize_t)1)<<(t2p->tiff_bitspersample+1)), output); return(written); @@ -4772,9 +4855,9 @@ tsize_t t2p_write_pdf_xobject_calcs(T2P* t2p, TIFF* output){ float G=1.0; float B=1.0; - written += TIFFWriteFile(output, (tdata_t) "[", 1); + written += t2pWriteFile(output, (tdata_t) "[", 1); if(t2p->pdf_colorspace & T2P_CS_CALGRAY){ - written += TIFFWriteFile(output, (tdata_t) "/CalGray ", 9); + written += t2pWriteFile(output, (tdata_t) "/CalGray ", 9); X_W = t2p->tiff_whitechromaticities[0]; Y_W = t2p->tiff_whitechromaticities[1]; Z_W = 1.0F - (X_W + Y_W); @@ -4783,7 +4866,7 @@ tsize_t t2p_write_pdf_xobject_calcs(T2P* t2p, TIFF* output){ Y_W = 1.0F; } if(t2p->pdf_colorspace & T2P_CS_CALRGB){ - written += TIFFWriteFile(output, (tdata_t) "/CalRGB ", 8); + written += t2pWriteFile(output, (tdata_t) "/CalRGB ", 8); x_w = t2p->tiff_whitechromaticities[0]; y_w = t2p->tiff_whitechromaticities[1]; x_r = t2p->tiff_primarychromaticities[0]; @@ -4809,26 +4892,26 @@ tsize_t t2p_write_pdf_xobject_calcs(T2P* t2p, TIFF* output){ Z_W /= Y_W; Y_W = 1.0; } - written += TIFFWriteFile(output, (tdata_t) "<< \r", 4); + written += t2pWriteFile(output, (tdata_t) "<< \n", 4); if(t2p->pdf_colorspace & T2P_CS_CALGRAY){ - written += TIFFWriteFile(output, (tdata_t) "/WhitePoint ", 12); - buflen=sprintf(buffer, "[%.4f %.4f %.4f] \r", X_W, Y_W, Z_W); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) "/Gamma 2.2 \r", 12); + written += t2pWriteFile(output, (tdata_t) "/WhitePoint ", 12); + buflen=sprintf(buffer, "[%.4f %.4f %.4f] \n", X_W, Y_W, Z_W); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) "/Gamma 2.2 \n", 12); } if(t2p->pdf_colorspace & T2P_CS_CALRGB){ - written += TIFFWriteFile(output, (tdata_t) "/WhitePoint ", 12); - buflen=sprintf(buffer, "[%.4f %.4f %.4f] \r", X_W, Y_W, Z_W); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) "/Matrix ", 8); - buflen=sprintf(buffer, "[%.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f] \r", + written += t2pWriteFile(output, (tdata_t) "/WhitePoint ", 12); + buflen=sprintf(buffer, "[%.4f %.4f %.4f] \n", X_W, Y_W, Z_W); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) "/Matrix ", 8); + buflen=sprintf(buffer, "[%.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f] \n", X_R, Y_R, Z_R, X_G, Y_G, Z_G, X_B, Y_B, Z_B); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) "/Gamma [2.2 2.2 2.2] \r", 22); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) "/Gamma [2.2 2.2 2.2] \n", 22); } - written += TIFFWriteFile(output, (tdata_t) ">>] \r", 5); + written += t2pWriteFile(output, (tdata_t) ">>] \n", 5); return(written); } @@ -4843,10 +4926,10 @@ tsize_t t2p_write_pdf_xobject_icccs(T2P* t2p, TIFF* output){ char buffer[16]; int buflen=0; - written += TIFFWriteFile(output, (tdata_t) "[/ICCBased ", 11); + written += t2pWriteFile(output, (tdata_t) "[/ICCBased ", 11); buflen=sprintf(buffer, "%lu", (unsigned long)t2p->pdf_icccs); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) " 0 R] \r", 7); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) " 0 R] \n", 7); return(written); } @@ -4857,10 +4940,10 @@ tsize_t t2p_write_pdf_xobject_icccs_dict(T2P* t2p, TIFF* output){ char buffer[16]; int buflen=0; - written += TIFFWriteFile(output, (tdata_t) "/N ", 3); - buflen=sprintf(buffer, "%u \r", t2p->tiff_samplesperpixel); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) "/Alternate ", 11); + written += t2pWriteFile(output, (tdata_t) "/N ", 3); + buflen=sprintf(buffer, "%u \n", t2p->tiff_samplesperpixel); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) "/Alternate ", 11); t2p->pdf_colorspace ^= T2P_CS_ICCBASED; written += t2p_write_pdf_xobject_cs(t2p, output); t2p->pdf_colorspace |= T2P_CS_ICCBASED; @@ -4906,11 +4989,11 @@ tsize_t t2p_write_pdf_xobject_decode(T2P* t2p, TIFF* output){ tsize_t written=0; int i=0; - written += TIFFWriteFile(output, (tdata_t) "/Decode [ ", 10); + written += t2pWriteFile(output, (tdata_t) "/Decode [ ", 10); for (i=0;itiff_samplesperpixel;i++){ - written += TIFFWriteFile(output, (tdata_t) "1 0 ", 4); + written += t2pWriteFile(output, (tdata_t) "1 0 ", 4); } - written += TIFFWriteFile(output, (tdata_t) "]\r", 2); + written += t2pWriteFile(output, (tdata_t) "]\n", 2); return(written); } @@ -4929,93 +5012,93 @@ tsize_t t2p_write_pdf_xobject_stream_filter(ttile_t tile, T2P* t2p, TIFF* output if(t2p->pdf_compression==T2P_COMPRESS_NONE){ return(written); } - written += TIFFWriteFile(output, (tdata_t) "/Filter ", 8); + written += t2pWriteFile(output, (tdata_t) "/Filter ", 8); switch(t2p->pdf_compression){ #ifdef CCITT_SUPPORT case T2P_COMPRESS_G4: - written += TIFFWriteFile(output, (tdata_t) "/CCITTFaxDecode ", 16); - written += TIFFWriteFile(output, (tdata_t) "/DecodeParms ", 13); - written += TIFFWriteFile(output, (tdata_t) "<< /K -1 ", 9); + written += t2pWriteFile(output, (tdata_t) "/CCITTFaxDecode ", 16); + written += t2pWriteFile(output, (tdata_t) "/DecodeParms ", 13); + written += t2pWriteFile(output, (tdata_t) "<< /K -1 ", 9); if(tile==0){ - written += TIFFWriteFile(output, (tdata_t) "/Columns ", 9); + written += t2pWriteFile(output, (tdata_t) "/Columns ", 9); buflen=sprintf(buffer, "%lu", (unsigned long)t2p->tiff_width); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) " /Rows ", 7); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) " /Rows ", 7); buflen=sprintf(buffer, "%lu", (unsigned long)t2p->tiff_length); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); } else { if(t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile-1)==0){ - written += TIFFWriteFile(output, (tdata_t) "/Columns ", 9); + written += t2pWriteFile(output, (tdata_t) "/Columns ", 9); buflen=sprintf( buffer, "%lu", (unsigned long)t2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); } else { - written += TIFFWriteFile(output, (tdata_t) "/Columns ", 9); + written += t2pWriteFile(output, (tdata_t) "/Columns ", 9); buflen=sprintf( buffer, "%lu", (unsigned long)t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilewidth); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); } if(t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile-1)==0){ - written += TIFFWriteFile(output, (tdata_t) " /Rows ", 7); + written += t2pWriteFile(output, (tdata_t) " /Rows ", 7); buflen=sprintf( buffer, "%lu", (unsigned long)t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); } else { - written += TIFFWriteFile(output, (tdata_t) " /Rows ", 7); + written += t2pWriteFile(output, (tdata_t) " /Rows ", 7); buflen=sprintf( buffer, "%lu", (unsigned long)t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilelength); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); } } if(t2p->pdf_switchdecode == 0){ - written += TIFFWriteFile(output, (tdata_t) " /BlackIs1 true ", 16); + written += t2pWriteFile(output, (tdata_t) " /BlackIs1 true ", 16); } - written += TIFFWriteFile(output, (tdata_t) ">>\r", 3); + written += t2pWriteFile(output, (tdata_t) ">>\n", 3); break; #endif #ifdef JPEG_SUPPORT case T2P_COMPRESS_JPEG: - written += TIFFWriteFile(output, (tdata_t) "/DCTDecode ", 11); + written += t2pWriteFile(output, (tdata_t) "/DCTDecode ", 11); if(t2p->tiff_photometric != PHOTOMETRIC_YCBCR) { - written += TIFFWriteFile(output, (tdata_t) "/DecodeParms ", 13); - written += TIFFWriteFile(output, (tdata_t) "<< /ColorTransform 0 >>\r", 24); + written += t2pWriteFile(output, (tdata_t) "/DecodeParms ", 13); + written += t2pWriteFile(output, (tdata_t) "<< /ColorTransform 0 >>\n", 24); } break; #endif #ifdef ZIP_SUPPORT case T2P_COMPRESS_ZIP: - written += TIFFWriteFile(output, (tdata_t) "/FlateDecode ", 13); + written += t2pWriteFile(output, (tdata_t) "/FlateDecode ", 13); if(t2p->pdf_compressionquality%100){ - written += TIFFWriteFile(output, (tdata_t) "/DecodeParms ", 13); - written += TIFFWriteFile(output, (tdata_t) "<< /Predictor ", 14); + written += t2pWriteFile(output, (tdata_t) "/DecodeParms ", 13); + written += t2pWriteFile(output, (tdata_t) "<< /Predictor ", 14); _TIFFmemset(buffer, 0x00, 16); buflen=sprintf(buffer, "%u", t2p->pdf_compressionquality%100); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) " /Columns ", 10); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) " /Columns ", 10); _TIFFmemset(buffer, 0x00, 16); buflen = sprintf(buffer, "%lu", (unsigned long)t2p->tiff_width); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) " /Colors ", 9); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) " /Colors ", 9); _TIFFmemset(buffer, 0x00, 16); buflen=sprintf(buffer, "%u", t2p->tiff_samplesperpixel); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) " /BitsPerComponent ", 19); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) " /BitsPerComponent ", 19); _TIFFmemset(buffer, 0x00, 16); buflen=sprintf(buffer, "%u", t2p->tiff_bitspersample); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) ">>\r", 3); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) ">>\n", 3); } break; #endif @@ -5037,21 +5120,21 @@ tsize_t t2p_write_pdf_xreftable(T2P* t2p, TIFF* output){ int buflen=0; uint32 i=0; - written += TIFFWriteFile(output, (tdata_t) "xref\r0 ", 7); + written += t2pWriteFile(output, (tdata_t) "xref\n0 ", 7); buflen=sprintf(buffer, "%lu", (unsigned long)(t2p->pdf_xrefcount + 1)); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); - written += TIFFWriteFile(output, (tdata_t) " \r0000000000 65535 f\r\n", 22); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) " \n0000000000 65535 f \n", 22); for (i=0;ipdf_xrefcount;i++){ - sprintf(buffer, "%.10lu 00000 n\r\n", + sprintf(buffer, "%.10lu 00000 n \n", (unsigned long)t2p->pdf_xrefoffsets[i]); - written += TIFFWriteFile(output, (tdata_t) buffer, 20); + written += t2pWriteFile(output, (tdata_t) buffer, 20); } return(written); } /* - * This function writes a PDF trailer to output. + * This function writes a PDF trailer to output. */ tsize_t t2p_write_pdf_trailer(T2P* t2p, TIFF* output) @@ -5060,46 +5143,34 @@ tsize_t t2p_write_pdf_trailer(T2P* t2p, TIFF* output) tsize_t written = 0; char buffer[32]; int buflen = 0; - char fileidbuf[16]; - int i = 0; + size_t i = 0; - ((int*)fileidbuf)[0] = rand(); - ((int*)fileidbuf)[1] = rand(); - ((int*)fileidbuf)[2] = rand(); - ((int*)fileidbuf)[3] = rand(); - t2p->pdf_fileid = (char*)_TIFFmalloc(33); - if(t2p->pdf_fileid == NULL) { - TIFFError( - TIFF2PDF_MODULE, - "Can't allocate %u bytes of memory for t2p_write_pdf_trailer", - 33 ); - t2p->t2p_error = T2P_ERR_ERROR; - return(0); - } - _TIFFmemset(t2p->pdf_fileid, 0x00, 33); - for (i=0; i<16; i++) - sprintf(&(t2p->pdf_fileid[2*i]), "%.2hhX", fileidbuf[i]); - written += TIFFWriteFile(output, (tdata_t) "trailer\r<<\r/Size ", 17); + for (i = 0; i < sizeof(t2p->pdf_fileid) - 8; i += 8) + snprintf(t2p->pdf_fileid + i, 9, "%.8X", rand()); + + written += t2pWriteFile(output, (tdata_t) "trailer\n<<\n/Size ", 17); buflen = sprintf(buffer, "%lu", (unsigned long)(t2p->pdf_xrefcount+1)); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); _TIFFmemset(buffer, 0x00, 32); - written += TIFFWriteFile(output, (tdata_t) "\r/Root ", 7); + written += t2pWriteFile(output, (tdata_t) "\n/Root ", 7); buflen=sprintf(buffer, "%lu", (unsigned long)t2p->pdf_catalog); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); _TIFFmemset(buffer, 0x00, 32); - written += TIFFWriteFile(output, (tdata_t) " 0 R \r/Info ", 12); + written += t2pWriteFile(output, (tdata_t) " 0 R \n/Info ", 12); buflen=sprintf(buffer, "%lu", (unsigned long)t2p->pdf_info); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); _TIFFmemset(buffer, 0x00, 32); - written += TIFFWriteFile(output, (tdata_t) " 0 R \r/ID[<", 11); - written += TIFFWriteFile(output, (tdata_t) t2p->pdf_fileid, 32); - written += TIFFWriteFile(output, (tdata_t) "><", 2); - written += TIFFWriteFile(output, (tdata_t) t2p->pdf_fileid, 32); - written += TIFFWriteFile(output, (tdata_t) ">]\r>>\rstartxref\r", 16); + written += t2pWriteFile(output, (tdata_t) " 0 R \n/ID[<", 11); + written += t2pWriteFile(output, (tdata_t) t2p->pdf_fileid, + sizeof(t2p->pdf_fileid) - 1); + written += t2pWriteFile(output, (tdata_t) "><", 2); + written += t2pWriteFile(output, (tdata_t) t2p->pdf_fileid, + sizeof(t2p->pdf_fileid) - 1); + written += t2pWriteFile(output, (tdata_t) ">]\n>>\nstartxref\n", 16); buflen=sprintf(buffer, "%lu", (unsigned long)t2p->pdf_startxref); - written += TIFFWriteFile(output, (tdata_t) buffer, buflen); + written += t2pWriteFile(output, (tdata_t) buffer, buflen); _TIFFmemset(buffer, 0x00, 32); - written += TIFFWriteFile(output, (tdata_t) "\r%%EOF\r", 7); + written += t2pWriteFile(output, (tdata_t) "\n%%EOF\n", 7); return(written); } @@ -5113,7 +5184,7 @@ tsize_t t2p_write_pdf_trailer(T2P* t2p, TIFF* output) library, and TIFFWriteEncodedStrip can be used to write compressed data to the output. The output is not actually a TIFF file, it is a PDF file. - This function uses only TIFFWriteFile and TIFFWriteEncodedStrip to write to + This function uses only t2pWriteFile and TIFFWriteEncodedStrip to write to the output TIFF file. When libtiff would otherwise be writing data to the output file, the write procedure of the TIFF structure is replaced with an empty implementation. @@ -5134,7 +5205,7 @@ tsize_t t2p_write_pdf_trailer(T2P* t2p, TIFF* output) assert(output != NULL); if(output->tif_seekproc != NULL){ - TIFFSeekFile(output, (toff_t) 0, SEEK_SET); + t2pSeekFile(output, (toff_t) 0, SEEK_SET); } This function returns the file size of the output PDF file. On error it @@ -5157,8 +5228,9 @@ tsize_t t2p_write_pdf(T2P* t2p, TIFF* input, TIFF* output){ if(t2p->pdf_xrefoffsets==NULL){ TIFFError( TIFF2PDF_MODULE, - "Can't allocate %lu bytes of memory for t2p_write_pdf", - t2p->pdf_xrefcount * sizeof(uint32) ); + "Can't allocate %u bytes of memory for t2p_write_pdf", + (unsigned int) (t2p->pdf_xrefcount * sizeof(uint32)) ); + t2p->t2p_error = T2P_ERR_ERROR; return(written); } t2p->pdf_xrefcount=0; @@ -5298,13 +5370,19 @@ tsize_t t2p_write_pdf(T2P* t2p, TIFF* input, TIFF* output){ written += t2p_write_pdf_obj_end(output); } } - t2p->pdf_startxref=written; + t2p->pdf_startxref = written; written += t2p_write_pdf_xreftable(t2p, output); written += t2p_write_pdf_trailer(t2p, output); - t2p->tiff_writeproc=output->tif_writeproc; - output->tif_writeproc=t2p_empty_writeproc; + t2p_disable(output); return(written); } /* vim: set ts=8 sts=8 sw=8 noet: */ +/* + * Local Variables: + * mode: c + * c-basic-offset: 8 + * fill-column: 78 + * End: + */