2014.10.15 02:56 "[Tiff] Tiff2pdf page ordering", by Richard Nolde

2014.10.15 02:56 "[Tiff] Tiff2pdf page ordering", by Richard Nolde

Regarding the strange page order:

tiff2pdf built is generating pages out of order for one of our test

files 123.tiff (attached). That tiff file is viewable in Windows Photo

Viewer and the pages have numbers 1, 2 and 3. The generated pdf is

231.pdf (attached). As you can see the first page is generated at the
end instead.

I have no access to a Winders build environment, but I remember looking at Ross' code when I thought I could add PDF output to tiffcrop some years back. I believe the key factor here is that he parses the TIFF directories with a routine that includes the code below. He isn't just reading the directories one after the other and writing them out as he finds them. I never figured out why this was necessary or what it accomplished unless it was to deal with some combinations of subfile types and out of order presentation. Is it possible that the qsort call works differently in that environment?

I wasn't able to download the sample TIFFs so I haven't seem them. Since the PDF output is limited to version 1.1 or 1.2, it might be worth seeing if changing the version makes a difference. Also, the early types aren't hard to decipher with any file viewer that handles binary files. If someone want send me the Windows source file and the PDF produced from it, I'll take a peak inside.

Richard Nolde

...
        for(i=0;i<directorycount;i++){

uint32 subfiletype = 0;

if(!TIFFSetDirectory(input, i)){
        TIFFError(
                TIFF2PDF_MODULE,
                "Can't set directory %u of input file %s",
                i,
                TIFFFileName(input));
        return;
}
if(TIFFGetField(input, TIFFTAG_PAGENUMBER, &pagen, &paged)){
        if((pagen>paged) && (paged != 0)){

t2p->tiff_pages[t2p->tiff_pagecount].page_number =
                                        paged;
                        } else {

t2p->tiff_pages[t2p->tiff_pagecount].page_number =
                                        pagen;
                        }
                        goto ispage2;
                }
                if(TIFFGetField(input, TIFFTAG_SUBFILETYPE, &subfiletype)){
                        if ( ((subfiletype & FILETYPE_PAGE) != 0)
                             || (subfiletype == 0)){
                                goto ispage;
                        } else {
                                goto isnotpage;
                        }
                }
                if(TIFFGetField(input, TIFFTAG_OSUBFILETYPE, &subfiletype)){
                        if ((subfiletype == OFILETYPE_IMAGE)

                                || (subfiletype == OFILETYPE_PAGE)
                                || (subfiletype == 0) ){

                goto ispage;
        } else {
                goto isnotpage;
        }
}
ispage:

t2p->tiff_pages[t2p->tiff_pagecount].page_number=t2p->tiff_pagecount;
                ispage2:
                t2p->tiff_pages[t2p->tiff_pagecount].page_directory=i;
                if(TIFFIsTiled(input)){

t2p->tiff_pages[t2p->tiff_pagecount].page_tilecount =
                                TIFFNumberOfTiles(input);
                }
                t2p->tiff_pagecount++;
                isnotpage:
                (void)0;
       }

qsort((void*) t2p->tiff_pages, t2p->tiff_pagecount,
      sizeof(T2P_PAGE), t2p_cmp_t2p_page);

for(i=0;i<t2p->tiff_pagecount;i++){
        t2p->pdf_xrefcount += 5;
        TIFFSetDirectory(input, t2p->tiff_pages[i].page_directory );
        if((TIFFGetField(input, TIFFTAG_PHOTOMETRIC, &xuint16)
            && (xuint16==PHOTOMETRIC_PALETTE))
           || TIFFGetField(input, TIFFTAG_INDEXED, &xuint16)) {
                t2p->tiff_pages[i].page_extra++;
                t2p->pdf_xrefcount++;
        }