~ubuntu-branches/ubuntu/trusty/tiff/trusty

« back to all changes in this revision

Viewing changes to tools/tiff2pdf.c

  • Committer: Package Import Robot
  • Author(s): Jay Berkenbilt
  • Date: 2012-06-24 13:45:42 UTC
  • mfrom: (15.1.14 sid)
  • Revision ID: package-import@ubuntu.com-20120624134542-u7dltcqwnb6orprf
Tags: 4.0.2-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: tiff2pdf.c,v 1.37.2.19 2010-12-13 05:41:11 faxguy Exp $
 
1
/* $Id: tiff2pdf.c,v 1.67 2012-06-15 21:51:54 fwarmerdam Exp $
2
2
 *
3
3
 * tiff2pdf - converts a TIFF image to a PDF document
4
4
 *
45
45
# include <io.h>
46
46
#endif
47
47
 
 
48
#ifdef NEED_LIBPORT
 
49
# include "libport.h"
 
50
#endif
 
51
 
48
52
#include "tiffiop.h"
49
53
#include "tiffio.h"
50
54
 
346
350
 * Procs for TIFFClientOpen
347
351
 */
348
352
 
349
 
static tsize_t 
350
 
t2pReadFile(TIFF *tif, tdata_t data, tsize_t size)
351
 
{
352
 
        thandle_t client = TIFFClientdata(tif);
353
 
        TIFFReadWriteProc proc =  TIFFGetReadProc(tif);
354
 
        if (proc)
355
 
                return proc(client, data, size);
356
 
        return -1;
357
 
}
358
 
 
359
 
static tsize_t 
360
 
t2pWriteFile(TIFF *tif, tdata_t data, tsize_t size)
361
 
{
362
 
        thandle_t client = TIFFClientdata(tif);
363
 
        TIFFReadWriteProc proc =  TIFFGetWriteProc(tif);
364
 
        if (proc)
365
 
                return proc(client, data, size);
366
 
        return -1;
367
 
}
368
 
 
369
 
static toff_t
 
353
static tmsize_t 
 
354
t2pReadFile(TIFF *tif, tdata_t data, tmsize_t size)
 
355
{
 
356
        thandle_t client = TIFFClientdata(tif);
 
357
        TIFFReadWriteProc proc = TIFFGetReadProc(tif);
 
358
        if (proc)
 
359
                return proc(client, data, size);
 
360
        return -1;
 
361
}
 
362
 
 
363
static tmsize_t 
 
364
t2pWriteFile(TIFF *tif, tdata_t data, tmsize_t size)
 
365
{
 
366
        thandle_t client = TIFFClientdata(tif);
 
367
        TIFFReadWriteProc proc = TIFFGetWriteProc(tif);
 
368
        if (proc)
 
369
                return proc(client, data, size);
 
370
        return -1;
 
371
}
 
372
 
 
373
static uint64
370
374
t2pSeekFile(TIFF *tif, toff_t offset, int whence)
371
375
{
372
376
        thandle_t client = TIFFClientdata(tif);
373
 
        TIFFSeekProc proc =  TIFFGetSeekProc(tif);
 
377
        TIFFSeekProc proc = TIFFGetSeekProc(tif);
374
378
        if (proc)
375
379
                return proc(client, offset, whence);
376
380
        return -1;
377
381
}
378
382
 
379
 
static tsize_t 
380
 
t2p_readproc(thandle_t handle, tdata_t data, tsize_t size) 
 
383
static tmsize_t 
 
384
t2p_readproc(thandle_t handle, tdata_t data, tmsize_t size) 
381
385
{
382
386
        (void) handle, (void) data, (void) size;
383
387
        return -1;
384
388
}
385
389
 
386
 
static tsize_t 
387
 
t2p_writeproc(thandle_t handle, tdata_t data, tsize_t size) 
 
390
static tmsize_t 
 
391
t2p_writeproc(thandle_t handle, tdata_t data, tmsize_t size) 
388
392
{
389
393
        T2P *t2p = (T2P*) handle;
390
394
        if (t2p->outputdisable <= 0 && t2p->outputfile) {
395
399
        return size; 
396
400
}
397
401
 
398
 
static toff_t 
399
 
t2p_seekproc(thandle_t handle, toff_t offset, int whence) 
 
402
static uint64 
 
403
t2p_seekproc(thandle_t handle, uint64 offset, int whence) 
400
404
401
405
        T2P *t2p = (T2P*) handle;
402
406
        if (t2p->outputdisable <= 0 && t2p->outputfile)
403
 
                return fseek(t2p->outputfile, offset, whence);
 
407
                return fseek(t2p->outputfile, (long) offset, whence);
404
408
        return offset;
405
409
}
406
410
 
411
415
        return 0; 
412
416
}
413
417
 
414
 
static toff_t 
 
418
static uint64 
415
419
t2p_sizeproc(thandle_t handle) 
416
420
{
417
421
        (void) handle;
419
423
}
420
424
 
421
425
static int 
422
 
t2p_mapproc(thandle_t handle, tdata_t *data, toff_t *offset) 
 
426
t2p_mapproc(thandle_t handle, void **data, toff_t *offset) 
423
427
424
428
        (void) handle, (void) data, (void) offset;
425
429
        return -1; 
426
430
}
427
431
 
428
432
static void 
429
 
t2p_unmapproc(thandle_t handle, tdata_t data, toff_t offset)
 
433
t2p_unmapproc(thandle_t handle, void *data, toff_t offset)
430
434
431
435
        (void) handle, (void) data, (void) offset;
432
436
}
564
568
 
565
569
        tiff2pdf input.tiff
566
570
 
567
 
    The above example would generate PDF output from input.tiff and write it 
 
571
    The above example would generate PDF output from input.tiff and write it
568
572
    to standard output.
569
573
 
570
574
        tiff2pdf -j -p letter -o output.pdf input.tiff
571
575
 
572
 
    The above example would generate the file output.pdf from input.tiff, 
573
 
    putting the image pages on a letter sized page, compressing the output 
 
576
    The above example would generate the file output.pdf from input.tiff,
 
577
    putting the image pages on a letter sized page, compressing the output
574
578
    with JPEG.
575
579
 
576
580
        Please report bugs through:
577
 
         
 
581
 
578
582
        http://bugzilla.remotesensing.org/buglist.cgi?product=libtiff
579
583
 
580
584
    See also libtiff.3t, tiffcp.
587
591
        const char *outfilename = NULL;
588
592
        T2P *t2p = NULL;
589
593
        TIFF *input = NULL, *output = NULL;
590
 
        tsize_t written = 0;
591
594
        int c, ret = EXIT_SUCCESS;
592
 
        
 
595
 
593
596
        t2p = t2p_init();
594
597
 
595
598
        if (t2p == NULL){
772
775
        output = TIFFClientOpen(outfilename, "w", (thandle_t) t2p,
773
776
                                t2p_readproc, t2p_writeproc, t2p_seekproc, 
774
777
                                t2p_closeproc, t2p_sizeproc, 
775
 
                                t2p_mapproc, t2p_unmapproc );
 
778
                                t2p_mapproc, t2p_unmapproc);
776
779
        if (output == NULL) {
777
780
                TIFFError(TIFF2PDF_MODULE,
778
781
                          "Can't initialize output descriptor");
788
791
        /*
789
792
         * Write
790
793
         */
791
 
        written = t2p_write_pdf(t2p, input, output);
 
794
        t2p_write_pdf(t2p, input, output);
792
795
        if (t2p->t2p_error != 0) {
793
796
                TIFFError(TIFF2PDF_MODULE,
794
797
                          "An error occurred creating output PDF file");
923
926
        if(t2p==NULL){
924
927
                TIFFError(
925
928
                        TIFF2PDF_MODULE, 
926
 
                        "Can't allocate %u bytes of memory for t2p_init", 
927
 
                        sizeof(T2P));
 
929
                        "Can't allocate %lu bytes of memory for t2p_init", 
 
930
                        (unsigned long) sizeof(T2P));
928
931
                return( (T2P*) NULL );
929
932
        }
930
933
        _TIFFmemset(t2p, 0x00, sizeof(T2P));
1036
1039
        if(t2p->tiff_pages==NULL){
1037
1040
                TIFFError(
1038
1041
                        TIFF2PDF_MODULE, 
1039
 
                        "Can't allocate %u bytes of memory for tiff_pages array, %s", 
1040
 
                        directorycount * sizeof(T2P_PAGE), 
 
1042
                        "Can't allocate %lu bytes of memory for tiff_pages array, %s", 
 
1043
                        (unsigned long) directorycount * sizeof(T2P_PAGE), 
1041
1044
                        TIFFFileName(input));
1042
1045
                t2p->t2p_error = T2P_ERR_ERROR;
1043
1046
                return;
1047
1050
        if(t2p->tiff_tiles==NULL){
1048
1051
                TIFFError(
1049
1052
                        TIFF2PDF_MODULE, 
1050
 
                        "Can't allocate %u bytes of memory for tiff_tiles array, %s", 
1051
 
                        directorycount * sizeof(T2P_TILES), 
 
1053
                        "Can't allocate %lu bytes of memory for tiff_tiles array, %s", 
 
1054
                        (unsigned long) directorycount * sizeof(T2P_TILES), 
1052
1055
                        TIFFFileName(input));
1053
1056
                t2p->t2p_error = T2P_ERR_ERROR;
1054
1057
                return;
1179
1182
                        if( t2p->tiff_tiles[i].tiles_tiles == NULL){
1180
1183
                                TIFFError(
1181
1184
                                        TIFF2PDF_MODULE, 
1182
 
                                        "Can't allocate %u bytes of memory for t2p_read_tiff_init, %s", 
1183
 
                                        t2p->tiff_tiles[i].tiles_tilecount * sizeof(T2P_TILE), 
 
1185
                                        "Can't allocate %lu bytes of memory for t2p_read_tiff_init, %s", 
 
1186
                                        (unsigned long) t2p->tiff_tiles[i].tiles_tilecount * sizeof(T2P_TILE), 
1184
1187
                                        TIFFFileName(input));
1185
1188
                                t2p->t2p_error = T2P_ERR_ERROR;
1186
1189
                                return;
1795
1798
 
1796
1799
void t2p_read_tiff_size(T2P* t2p, TIFF* input){
1797
1800
 
1798
 
        uint32* sbc=NULL;
 
1801
        uint64* sbc=NULL;
1799
1802
#if defined(JPEG_SUPPORT) || defined (OJPEG_SUPPORT)
1800
1803
        unsigned char* jpt=NULL;
1801
1804
        tstrip_t i=0;
1807
1810
#ifdef CCITT_SUPPORT
1808
1811
                if(t2p->pdf_compression == T2P_COMPRESS_G4 ){
1809
1812
                        TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc);
1810
 
                        t2p->tiff_datasize=sbc[0];
 
1813
                        t2p->tiff_datasize=(tmsize_t)sbc[0];
1811
1814
                        return;
1812
1815
                }
1813
1816
#endif
1814
1817
#ifdef ZIP_SUPPORT
1815
1818
                if(t2p->pdf_compression == T2P_COMPRESS_ZIP){
1816
1819
                        TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc);
1817
 
                        t2p->tiff_datasize=sbc[0];
 
1820
                        t2p->tiff_datasize=(tmsize_t)sbc[0];
1818
1821
                        return;
1819
1822
                }
1820
1823
#endif
1834
1837
                        if(TIFFGetField(input, TIFFTAG_JPEGIFOFFSET, &(t2p->tiff_dataoffset))){
1835
1838
                                if(t2p->tiff_dataoffset != 0){
1836
1839
                                        if(TIFFGetField(input, TIFFTAG_JPEGIFBYTECOUNT, &(t2p->tiff_datasize))!=0){
1837
 
                                                if(t2p->tiff_datasize < k) {
 
1840
                                                if((uint64)t2p->tiff_datasize < k) {
1838
1841
                                                        TIFFWarning(TIFF2PDF_MODULE, 
1839
1842
                                                                "Input file %s has short JPEG interchange file byte count", 
1840
1843
                                                                TIFFFileName(input));
1930
1933
 
1931
1934
void t2p_read_tiff_size_tile(T2P* t2p, TIFF* input, ttile_t tile){
1932
1935
 
1933
 
        uint32* tbc = NULL;
 
1936
        uint64* tbc = NULL;
1934
1937
        uint16 edge=0;
1935
1938
#ifdef JPEG_SUPPORT
1936
1939
        unsigned char* jpt;
1967
1970
                                        if(count > 4){
1968
1971
                                                k = checkAdd64(k, count, t2p);
1969
1972
                                                k -= 2; /* don't use EOI of header or SOI of tile */
1970
 
 
1971
1973
                                        }
1972
1974
                                }
1973
1975
                        }
2079
2081
#ifdef JPEG_SUPPORT
2080
2082
        unsigned char* jpt;
2081
2083
        float* xfloatp;
2082
 
        uint32* sbc;
 
2084
        uint64* sbc;
2083
2085
        unsigned char* stripbuffer;
2084
2086
        tsize_t striplength=0;
2085
2087
        uint32 max_striplength=0;
2096
2098
                                _TIFFmalloc(t2p->tiff_datasize);
2097
2099
                        if (buffer == NULL) {
2098
2100
                                TIFFError(TIFF2PDF_MODULE, 
2099
 
        "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", 
2100
 
                                        t2p->tiff_datasize, 
 
2101
        "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", 
 
2102
                                        (unsigned long) t2p->tiff_datasize, 
2101
2103
                                        TIFFFileName(input));
2102
2104
                                t2p->t2p_error = T2P_ERR_ERROR;
2103
2105
                                return(0);
2124
2126
                                _TIFFmalloc(t2p->tiff_datasize);
2125
2127
                        if(buffer == NULL){
2126
2128
                                TIFFError(TIFF2PDF_MODULE, 
2127
 
        "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", 
2128
 
                                        t2p->tiff_datasize, 
 
2129
        "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", 
 
2130
                                        (unsigned long) t2p->tiff_datasize, 
2129
2131
                                        TIFFFileName(input));
2130
2132
                                t2p->t2p_error = T2P_ERR_ERROR;
2131
2133
                                return(0);
2151
2153
                                        _TIFFmalloc(t2p->tiff_datasize);
2152
2154
                                if(buffer == NULL) {
2153
2155
                                        TIFFError(TIFF2PDF_MODULE, 
2154
 
        "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", 
2155
 
                                                t2p->tiff_datasize, 
 
2156
        "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", 
 
2157
                                                (unsigned long) t2p->tiff_datasize, 
2156
2158
                                                TIFFFileName(input));
2157
2159
                                        t2p->t2p_error = T2P_ERR_ERROR;
2158
2160
                                        return(0);
2227
2229
                                        _TIFFmalloc(t2p->tiff_datasize);
2228
2230
                                if(buffer==NULL){
2229
2231
                                        TIFFError(TIFF2PDF_MODULE, 
2230
 
        "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", 
2231
 
                                                t2p->tiff_datasize, 
 
2232
        "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", 
 
2233
                                                (unsigned long) t2p->tiff_datasize, 
2232
2234
                                                TIFFFileName(input));
2233
2235
                                        t2p->t2p_error = T2P_ERR_ERROR;
2234
2236
                                        return(0);
2270
2272
                                _TIFFmalloc(t2p->tiff_datasize);
2271
2273
                        if(buffer==NULL){
2272
2274
                                TIFFError(TIFF2PDF_MODULE, 
2273
 
        "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", 
2274
 
                                        t2p->tiff_datasize, 
 
2275
        "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", 
 
2276
                                        (unsigned long) t2p->tiff_datasize, 
2275
2277
                                        TIFFFileName(input));
2276
2278
                                t2p->t2p_error = T2P_ERR_ERROR;
2277
2279
                                return(0);
2332
2334
                buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);
2333
2335
                if(buffer==NULL){
2334
2336
                        TIFFError(TIFF2PDF_MODULE, 
2335
 
        "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", 
2336
 
                                t2p->tiff_datasize, 
 
2337
        "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", 
 
2338
                                (unsigned long) t2p->tiff_datasize, 
2337
2339
                                TIFFFileName(input));
2338
2340
                        t2p->t2p_error = T2P_ERR_ERROR;
2339
2341
                        return(0);
2370
2372
                        buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);
2371
2373
                        if(buffer==NULL){
2372
2374
                                TIFFError(TIFF2PDF_MODULE, 
2373
 
        "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", 
2374
 
                                        t2p->tiff_datasize, 
 
2375
        "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", 
 
2376
                                        (unsigned long) t2p->tiff_datasize, 
2375
2377
                                        TIFFFileName(input));
2376
2378
                                t2p->t2p_error = T2P_ERR_ERROR;
2377
2379
                                return(0);
2380
2382
                        samplebuffer = (unsigned char*) _TIFFmalloc(stripsize);
2381
2383
                        if(samplebuffer==NULL){
2382
2384
                                TIFFError(TIFF2PDF_MODULE, 
2383
 
        "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", 
2384
 
                                        t2p->tiff_datasize, 
 
2385
        "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", 
 
2386
                                        (unsigned long) t2p->tiff_datasize, 
2385
2387
                                        TIFFFileName(input));
2386
2388
                                t2p->t2p_error = T2P_ERR_ERROR;
2387
2389
                                return(0);
2419
2421
                buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);
2420
2422
                if(buffer==NULL){
2421
2423
                        TIFFError(TIFF2PDF_MODULE, 
2422
 
        "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", 
2423
 
                                t2p->tiff_datasize, 
 
2424
        "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", 
 
2425
                                (unsigned long) t2p->tiff_datasize, 
2424
2426
                                TIFFFileName(input));
2425
2427
                        t2p->t2p_error = T2P_ERR_ERROR;
2426
2428
                        return(0);
2448
2450
                }
2449
2451
 
2450
2452
                if(t2p->pdf_sample & T2P_SAMPLE_REALIZE_PALETTE){
 
2453
                        // FIXME: overflow?
2451
2454
                        samplebuffer=(unsigned char*)_TIFFrealloc( 
2452
2455
                                (tdata_t) buffer, 
2453
2456
                                t2p->tiff_datasize * t2p->tiff_samplesperpixel);
2454
2457
                        if(samplebuffer==NULL){
2455
2458
                                TIFFError(TIFF2PDF_MODULE, 
2456
 
        "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", 
2457
 
                                        t2p->tiff_datasize, 
 
2459
        "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", 
 
2460
                                        (unsigned long) t2p->tiff_datasize, 
2458
2461
                                        TIFFFileName(input));
2459
2462
                                t2p->t2p_error = T2P_ERR_ERROR;
2460
2463
                          _TIFFfree(buffer);
2483
2486
                                t2p->tiff_width*t2p->tiff_length*4);
2484
2487
                        if(samplebuffer==NULL){
2485
2488
                                TIFFError(TIFF2PDF_MODULE, 
2486
 
        "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s", 
2487
 
                                        t2p->tiff_datasize, 
 
2489
        "Can't allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s", 
 
2490
                                        (unsigned long) t2p->tiff_datasize, 
2488
2491
                                        TIFFFileName(input));
2489
2492
                                t2p->t2p_error = T2P_ERR_ERROR;
2490
2493
                                _TIFFfree(buffer);
2614
2617
                                                     stripsize * stripcount); 
2615
2618
        } else
2616
2619
#endif
2617
 
        {
 
2620
        {
2618
2621
                bufferoffset = TIFFWriteEncodedStrip(output, (tstrip_t)0,
2619
2622
                                                     buffer,
2620
2623
                                                     t2p->tiff_datasize); 
2680
2683
                        buffer= (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);
2681
2684
                        if(buffer==NULL){
2682
2685
                                TIFFError(TIFF2PDF_MODULE, 
2683
 
                                        "Can't allocate %u bytes of memory "
 
2686
                                        "Can't allocate %lu bytes of memory "
2684
2687
                                        "for t2p_readwrite_pdf_image_tile, %s", 
2685
 
                                        t2p->tiff_datasize, 
 
2688
                                        (unsigned long) t2p->tiff_datasize, 
2686
2689
                                        TIFFFileName(input));
2687
2690
                                t2p->t2p_error = T2P_ERR_ERROR;
2688
2691
                                return(0);
2701
2704
                        buffer= (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);
2702
2705
                        if(buffer==NULL){
2703
2706
                                TIFFError(TIFF2PDF_MODULE, 
2704
 
                                        "Can't allocate %u bytes of memory "
 
2707
                                        "Can't allocate %lu bytes of memory "
2705
2708
                                        "for t2p_readwrite_pdf_image_tile, %s", 
2706
 
                                        t2p->tiff_datasize, 
 
2709
                                        (unsigned long) t2p->tiff_datasize, 
2707
2710
                                        TIFFFileName(input));
2708
2711
                                t2p->t2p_error = T2P_ERR_ERROR;
2709
2712
                                return(0);
2730
2733
                        buffer=(unsigned char*) _TIFFmalloc(t2p->tiff_datasize);
2731
2734
                        if(buffer==NULL){
2732
2735
                                TIFFError(TIFF2PDF_MODULE, 
2733
 
                                        "Can't allocate %u bytes of memory "
 
2736
                                        "Can't allocate %lu bytes of memory "
2734
2737
                                        "for t2p_readwrite_pdf_image, %s", 
2735
 
                                        t2p->tiff_datasize, 
 
2738
                                        (unsigned long) t2p->tiff_datasize, 
2736
2739
                                        TIFFFileName(input));
2737
2740
                                t2p->t2p_error = T2P_ERR_ERROR;
2738
2741
                                return(0);
2771
2774
                        buffer= (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);
2772
2775
                        if(buffer==NULL){
2773
2776
                                TIFFError(TIFF2PDF_MODULE, 
2774
 
                                        "Can't allocate %u bytes of memory "
 
2777
                                        "Can't allocate %lu bytes of memory "
2775
2778
                                        "for t2p_readwrite_pdf_image_tile, %s", 
2776
2779
                                        t2p->tiff_datasize, 
2777
2780
                                        TIFFFileName(input));
2814
2817
                buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);
2815
2818
                if(buffer==NULL){
2816
2819
                        TIFFError(TIFF2PDF_MODULE, 
2817
 
                                "Can't allocate %u bytes of memory for "
 
2820
                                "Can't allocate %lu bytes of memory for "
2818
2821
                                "t2p_readwrite_pdf_image_tile, %s", 
2819
 
                                t2p->tiff_datasize, 
 
2822
                                (unsigned long) t2p->tiff_datasize, 
2820
2823
                                TIFFFileName(input));
2821
2824
                        t2p->t2p_error = T2P_ERR_ERROR;
2822
2825
                        return(0);
2847
2850
                        buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);
2848
2851
                        if(buffer==NULL){
2849
2852
                                TIFFError(TIFF2PDF_MODULE, 
2850
 
                                        "Can't allocate %u bytes of memory "
 
2853
                                        "Can't allocate %lu bytes of memory "
2851
2854
                                        "for t2p_readwrite_pdf_image_tile, %s", 
2852
 
                                        t2p->tiff_datasize, 
 
2855
                                        (unsigned long) t2p->tiff_datasize, 
2853
2856
                                        TIFFFileName(input));
2854
2857
                                t2p->t2p_error = T2P_ERR_ERROR;
2855
2858
                                return(0);
2857
2860
                        samplebuffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);
2858
2861
                        if(samplebuffer==NULL){
2859
2862
                                TIFFError(TIFF2PDF_MODULE, 
2860
 
                                        "Can't allocate %u bytes of memory "
 
2863
                                        "Can't allocate %lu bytes of memory "
2861
2864
                                        "for t2p_readwrite_pdf_image_tile, %s", 
2862
 
                                        t2p->tiff_datasize, 
 
2865
                                        (unsigned long) t2p->tiff_datasize, 
2863
2866
                                        TIFFFileName(input));
2864
2867
                                t2p->t2p_error = T2P_ERR_ERROR;
2865
2868
                                return(0);
2896
2899
                        buffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);
2897
2900
                        if(buffer==NULL){
2898
2901
                                TIFFError(TIFF2PDF_MODULE, 
2899
 
                                        "Can't allocate %u bytes of memory "
 
2902
                                        "Can't allocate %lu bytes of memory "
2900
2903
                                        "for t2p_readwrite_pdf_image_tile, %s", 
2901
 
                                        t2p->tiff_datasize, 
 
2904
                                        (unsigned long) t2p->tiff_datasize, 
2902
2905
                                        TIFFFileName(input));
2903
2906
                                t2p->t2p_error = T2P_ERR_ERROR;
2904
2907
                                return(0);
3430
3433
        uint32 edgetilewidth, 
3431
3434
        uint32 tilelength){
3432
3435
        
3433
 
        uint32 i=0;
 
3436
        uint32 i;
3434
3437
        tsize_t edgescanwidth=0;
3435
3438
        
3436
3439
        edgescanwidth = (scanwidth * edgetilewidth + (tilewidth - 1))/ tilewidth;
3437
 
        for(i=i;i<tilelength;i++){
 
3440
        for(i=0;i<tilelength;i++){
3438
3441
                _TIFFmemcpy( 
3439
3442
                        &(((char*)buffer)[edgescanwidth*i]), 
3440
3443
                        &(((char*)buffer)[scanwidth*i]), 
3651
3654
        uint32 i=0;
3652
3655
        char buffer[64];
3653
3656
        uint16 nextchar=0;
3654
 
        uint32 namelen=0;
 
3657
        size_t namelen=0;
3655
3658
        
3656
3659
        namelen = strlen((char *)name);
3657
3660
        if (namelen>126) {
3742
3745
/*
3743
3746
 * This function writes a PDF string object to output.
3744
3747
 */
3745
 
 
 
3748
        
3746
3749
tsize_t t2p_write_pdf_string(char* pdfstr, TIFF* output)
3747
3750
{
3748
3751
        tsize_t written = 0;
3749
3752
        uint32 i = 0;
3750
3753
        char buffer[64];
3751
 
        uint32 len = 0;
 
3754
        size_t len = 0;
3752
3755
        
3753
3756
        len = strlen(pdfstr);
3754
3757
        written += t2pWriteFile(output, (tdata_t) "(", 1);
3755
3758
        for (i=0; i<len; i++) {
3756
3759
                if((pdfstr[i]&0x80) || (pdfstr[i]==127) || (pdfstr[i]<32)){
3757
 
                        snprintf(buffer, sizeof(buffer), "\\%.3hho", pdfstr[i]);
 
3760
                        snprintf(buffer, sizeof(buffer), "\\%.3o", ((unsigned char)pdfstr[i]));
3758
3761
                        written += t2pWriteFile(output, (tdata_t)buffer, 4);
3759
3762
                } else {
3760
3763
                        switch (pdfstr[i]){
3933
3936
        tsize_t written = 0;
3934
3937
        char* info;
3935
3938
        char buffer[512];
3936
 
        
 
3939
 
3937
3940
        if(t2p->pdf_datetime[0] == '\0')
3938
3941
                t2p_pdf_tifftime(t2p, input);
3939
3942
        if (strlen(t2p->pdf_datetime) > 0) {
3948
3951
        written += t2p_write_pdf_string(buffer, output);
3949
3952
        written += t2pWriteFile(output, (tdata_t) "\n", 1);
3950
3953
        if (t2p->pdf_creator[0] != '\0') {
3951
 
                        written += t2pWriteFile(output, (tdata_t) "/Creator ", 9);
3952
 
                        written += t2p_write_pdf_string(t2p->pdf_creator, output);
3953
 
                        written += t2pWriteFile(output, (tdata_t) "\n", 1);
 
3954
                written += t2pWriteFile(output, (tdata_t) "/Creator ", 9);
 
3955
                written += t2p_write_pdf_string(t2p->pdf_creator, output);
 
3956
                written += t2pWriteFile(output, (tdata_t) "\n", 1);
3954
3957
        } else {
3955
3958
                if (TIFFGetField(input, TIFFTAG_SOFTWARE, &info) != 0 && info) {
3956
3959
                        if(strlen(info) >= sizeof(t2p->pdf_creator))
3961
3964
                }
3962
3965
        }
3963
3966
        if (t2p->pdf_author[0] != '\0') {
3964
 
                        written += t2pWriteFile(output, (tdata_t) "/Author ", 8);
3965
 
                        written += t2p_write_pdf_string(t2p->pdf_author, output);
3966
 
                        written += t2pWriteFile(output, (tdata_t) "\n", 1);
 
3967
                written += t2pWriteFile(output, (tdata_t) "/Author ", 8);
 
3968
                written += t2p_write_pdf_string(t2p->pdf_author, output);
 
3969
                written += t2pWriteFile(output, (tdata_t) "\n", 1);
3967
3970
        } else {
3968
3971
                if ((TIFFGetField(input, TIFFTAG_ARTIST, &info) != 0
3969
3972
                     || TIFFGetField(input, TIFFTAG_COPYRIGHT, &info) != 0)
3973
3976
                        written += t2pWriteFile(output, (tdata_t) "/Author ", 8);
3974
3977
                        written += t2p_write_pdf_string(info, output);
3975
3978
                        written += t2pWriteFile(output, (tdata_t) "\n", 1);
3976
 
                } 
 
3979
                }
3977
3980
        }
3978
3981
        if (t2p->pdf_title[0] != '\0') {
3979
 
                        written += t2pWriteFile(output, (tdata_t) "/Title ", 7);
3980
 
                        written += t2p_write_pdf_string(t2p->pdf_title, output);
3981
 
                        written += t2pWriteFile(output, (tdata_t) "\n", 1);
 
3982
                written += t2pWriteFile(output, (tdata_t) "/Title ", 7);
 
3983
                written += t2p_write_pdf_string(t2p->pdf_title, output);
 
3984
                written += t2pWriteFile(output, (tdata_t) "\n", 1);
3982
3985
        } else {
3983
3986
                if (TIFFGetField(input, TIFFTAG_DOCUMENTNAME, &info) != 0){
3984
3987
                        if(strlen(info) > 511) {
3990
3993
                }
3991
3994
        }
3992
3995
        if (t2p->pdf_subject[0] != '\0') {
3993
 
                        written += t2pWriteFile(output, (tdata_t) "/Subject ", 9);
3994
 
                        written += t2p_write_pdf_string(t2p->pdf_subject, output);
3995
 
                        written += t2pWriteFile(output, (tdata_t) "\n", 1);
 
3996
                written += t2pWriteFile(output, (tdata_t) "/Subject ", 9);
 
3997
                written += t2p_write_pdf_string(t2p->pdf_subject, output);
 
3998
                written += t2pWriteFile(output, (tdata_t) "\n", 1);
3996
3999
        } else {
3997
4000
                if (TIFFGetField(input, TIFFTAG_IMAGEDESCRIPTION, &info) != 0 && info) {
3998
4001
                        if (strlen(info) >= sizeof(t2p->pdf_subject))
4003
4006
                }
4004
4007
        }
4005
4008
        if (t2p->pdf_keywords[0] != '\0') {
4006
 
                        written += t2pWriteFile(output, (tdata_t) "/Keywords ", 10);
4007
 
                        written += t2p_write_pdf_string(t2p->pdf_keywords, output);
4008
 
                        written += t2pWriteFile(output, (tdata_t) "\n", 1);
4009
 
                }
4010
 
        written += t2pWriteFile(output, (tdata_t) ">> \n", 4);  
 
4009
                written += t2pWriteFile(output, (tdata_t) "/Keywords ", 10);
 
4010
                written += t2p_write_pdf_string(t2p->pdf_keywords, output);
 
4011
                written += t2pWriteFile(output, (tdata_t) "\n", 1);
 
4012
        }
 
4013
        written += t2pWriteFile(output, (tdata_t) ">> \n", 4);
4011
4014
 
4012
4015
        return(written);
4013
4016
}
4033
4036
                 "D:%.4d%.2d%.2d%.2d%.2d%.2d",
4034
4037
                 (currenttime->tm_year + 1900) % 65536,
4035
4038
                 (currenttime->tm_mon + 1) % 256,
4036
 
                (currenttime->tm_mday) % 256, 
4037
 
                (currenttime->tm_hour) % 256, 
4038
 
                (currenttime->tm_min) % 256, 
4039
 
                (currenttime->tm_sec) % 256);
 
4039
                 (currenttime->tm_mday) % 256,
 
4040
                 (currenttime->tm_hour) % 256,
 
4041
                 (currenttime->tm_min) % 256,
 
4042
                 (currenttime->tm_sec) % 256);
4040
4043
 
4041
4044
        return;
4042
4045
}
4088
4091
        int buflen=0;
4089
4092
 
4090
4093
        int page=0;
4091
 
        written += t2pWriteFile(output, 
 
4094
        written += t2pWriteFile(output,
4092
4095
                (tdata_t) "<< \n/Type /Pages \n/Kids [ ", 26);
4093
4096
        page = t2p->pdf_pages+1;
4094
4097
        for (i=0;i<t2p->tiff_pagecount;i++){
4125
4128
        tsize_t written=0;
4126
4129
        char buffer[16];
4127
4130
        int buflen=0;
4128
 
        
 
4131
 
4129
4132
        written += t2pWriteFile(output, (tdata_t) "<<\n/Type /Page \n/Parent ", 24);
4130
4133
        buflen=sprintf(buffer, "%lu", (unsigned long)t2p->pdf_pages);
4131
4134
        written += t2pWriteFile(output, (tdata_t) buffer, buflen);
4798
4801
        buflen=sprintf(buffer, "/Size [%u] \n", (1<<t2p->tiff_bitspersample));
4799
4802
        written += t2pWriteFile(output, (tdata_t) buffer, buflen);
4800
4803
        written += t2pWriteFile(output, (tdata_t) "/BitsPerSample 16 \n", 19);
4801
 
        written += t2p_write_pdf_stream_dict(1<<(t2p->tiff_bitspersample+1), 0, output);
 
4804
        written += t2p_write_pdf_stream_dict(((tsize_t)1)<<(t2p->tiff_bitspersample+1), 0, output);
4802
4805
 
4803
4806
        return(written);
4804
4807
}
4809
4812
 
4810
4813
        written += t2p_write_pdf_stream(
4811
4814
                t2p->tiff_transferfunction[i], 
4812
 
                (1<<(t2p->tiff_bitspersample+1)), 
 
4815
                (((tsize_t)1)<<(t2p->tiff_bitspersample+1)), 
4813
4816
                output);
4814
4817
 
4815
4818
        return(written);
5224
5227
                TIFFError(
5225
5228
                        TIFF2PDF_MODULE, 
5226
5229
                        "Can't allocate %u bytes of memory for t2p_write_pdf", 
5227
 
                        t2p->pdf_xrefcount * sizeof(uint32) );
 
5230
                        (unsigned int) (t2p->pdf_xrefcount * sizeof(uint32)) );
5228
5231
                return(written);
5229
5232
        }
5230
5233
        t2p->pdf_xrefcount=0;