title: JAI: How do I extract a single page input stream from a multipaged TIFF image container?
body: <p>I have a component that converts PDF documents to images, <em>one image per page</em>. Since the component uses converters producing in-memory images, it hits the JVM heap heavily and takes some time to finish conversions.</p>
  
  <p>I'm trying to improve the overall performance of the conversion process, and found a native library with a JNI binding to convert PDFs to TIFFs. That library can convert PDFs to single TIFF files only (requires intermediate file system storage; does not even consume conversion streams), therefore result TIFF files have converted pages embedded, and not per-page images on the file system. Having a native library improves the overall conversion drastically and the performance gets really faster, but there is a real bottleneck: since I have to make a source-page to destination-page conversion, now I must extract every page from the result file and write all of them elsewhere. A simple and naive approach with <code>RenderedImage</code>s:</p>
  
  <pre class="lang-java prettyprint-override"><code>final SeekableStream seekableStream = new FileSeekableStream(tempFile);
  final ImageDecoder imageDecoder = createImageDecoder("tiff", seekableStream, null);
  ...
  //                                               V--- heap is wasted here
  final RenderedImage renderedImage = imageDecoder.decodeAsRenderedImage(pageNumber);
  // ... do the rest stuff ...
  </code></pre>
  
  <p>Actually speaking, I would really like just to extract a concrete page input stream from the TIFF container file (<code>tempFile</code>) and just redirect it to elsewhere without having it to be stored as an in-memory image. I would imagine an approach similar to containers processing where I need to seek for a specific entry to extract data from it (say, something like ZIP files processing, etc). But I couldn't find anything like that in <code>ImageDecoder</code>, or I'm probably wrong with my expectations and just missing something important here...</p>
  
  <p>Is it possible to extract TIFF container page input streams using JAI API or probably third-party alternatives? Thanks in advance.</p>