Reputation: 723
I have a TIF with multiple pages inside (all are old style JPEG encoded). What I want to do is to remove some of the pages from the TIF.
I tried the FreeDirectory method but it seems no effect at all.
Upvotes: 2
Views: 759
Reputation: 14246
There is no easy way to do this using the library.
You can try and use UnlinkDirectory but this won't reduce the file size. And the data will still reside in the file. The frame just won't be accessible for a well-behaved viewer.
An other way is to copy relevant frames to a new image. You can start by playing with TiffCP utility for this. Then you can take a look at the source code of the utility.
Please note that OJPEG compression is supported in read-only mode only. You won't be able to copy the image data without decompressing and recompressing it again. This process can be lossy or lossless. A lossless process will probably yield bigger files.
Upvotes: 0
Reputation: 49219
Since libTiff.Net is based off of libTiff, clearly it is going to be possible to do this task, but the APIs for doing that just aren't there in a reasonable format. For the most part, I believe that libTiff.Net's role in the universe is to provide BitMiracle (hi!) with a means to convert TIFFs to PDF, which is a very common task.
The problem is that you need to find the appropriate IFD for the page in the document and remove it. That's straight forward, but that leaves all the image data behind. If you coalesce the data past it into the hole from the image data, then you've got to find everything that refers to moved data and adjust it, which means that ultimately you need to understand the TIFF spec fully to do it. Welcome to hell, here's you're accordion.
If you're looking for an alternative, I wrote code to wrap up all the details of TIFF manipulation into a nice little API at my last company, Atalasoft for the dotImage product. It's not free and when I last checked, it looked like they had jacked up the price. Still, you can still get a 30 day free trial if this is a one-off, otherwise, get out the checkbook.
Here's how you'd do it in dotImage:
TiffDocument doc = new TiffDocument(sourceStream);
// obviously, in a loop you should remove the pages in descending order
doc.Pages.RemoveAt(zeroBasedPageIndex);
doc.Save(destinationStream);
FYI, all the heavy lifting happens in doc.Save(). The rest is fairly light-weight.
Upvotes: 1