photopea / UTIF.js

Fast and advanced TIFF decoder
MIT License
424 stars 87 forks source link

Splitting multi-page tiffs into multiple single page tiffs #102

Closed JPorter44 closed 2 years ago

JPorter44 commented 3 years ago

When attempting to split a multi-page tiff I'm running into an error.

unknown type of tag: 254 at Object.push../node_modules/utif/UTIF.js.UTIF._writeIFD (UTIF.js:961) at Object.push../node_modules/utif/UTIF.js.UTIF.encode (UTIF.js:74) at Object.push../node_modules/utif/UTIF.js.UTIF.encodeImage (UTIF.js:57) image

I'm successfully converting any tiffs that aren't Group4 compression to PNGs with UPNG. I would like to be able to split multi-page Group4 compressed tiffs into multiple single page group4 compressed tiffs. Is this possible or am I doing something wrong? Here is an example of my code where I am attempting to do this. The file variable is a javascript File object.

 let buffer = await file.arrayBuffer();
 let ifds = UTIF.decode(buffer);

for (let i = 0; i < ifds.length; i++) {
    const ifd = ifds[i];
    UTIF.decodeImage(buffer, ifd);
    let compression = ifd["t259"] ? ifd["t259"][0] : 1;

    if (compression == 4) {
        let rgba = UTIF.toRGBA8(ifd);
        let arrayBuffer = UTIF.encodeImage(rgba, ifd.width, ifd.height, ifd);
        files.push(new File([arrayBuffer], file.name, {
            type: file.type,
            lastModified: Date.now()
        }));
    }
}
johnthad commented 3 years ago

I've not tried encoding with UTIF.js, so I'm making a guess here: Tag 254 is valid per the TIFF spec (NewSubfileType), but if UTIF.js is complaining about it you might try removing it from idf before calling UTIF.encodeImage(...). I'd try calling delete idf.t254 before UTIF.encodeImage(...). Does that work?

JPorter44 commented 3 years ago

@johnthad I tried removing that which then caused me to have to delete like 10 more tags. In the end it didn't work because I was ending up with files that were 1KB large and blank/corrupted. I believe my issue is assuming UTIF.encodeImage(...) keeps the existing compression. I would still like to know if it's possible to keep compression while splitting a multi-page file to single page files.

johnthad commented 3 years ago

Sorry, I use UTIF.js only for decompressing and displaying in a browser. All my work writing TIFF Group IV has been with Java, most recently with Java Advanced Imaging Image I/O Tools API Core (standalone).

jardicc commented 2 years ago

UTIF does not use compression during encode. Anyway adding it is not that hard. In nodeJS you can simply use this: import { deflateSync } from "zlib"; instead of pako and have much faster performance.

photopea commented 2 years ago

The TIFF encoder in Photopea supports only basic features. You should use PNG instead of TIFF when you store images.

jardicc commented 2 years ago

@photopea Problem with PNG is that it can't store layers, additional channels, or it does not support other advanced features. But I do agree that in many cases PNG can replace TIFF.

photopea commented 2 years ago

Put it into multiple PNG files, and put them into a ZIP if you need just one file.

jardicc commented 2 years ago

@photopea Maybe that could work for JPorter44 or johnthad but in my case I wish I could ¯_(ツ)_/¯ ...because then you have problem that you need to stack and merge those files in correct order and offset.. because you want to edit file later. So then you have to add info into zip what to do with those files and write plugin that will compose it together. So you are inventing custom propriatary file format. Which might not be necessary since that already exists and has some support. ...so I made advanced Tiff encoder myself. And I can also say that adding compression into UTIF is not that much work and will make it like 4 times smaller files... if anyone wants to implement.