dragon66 / icafe

Java library for reading, writing, converting and manipulating images and metadata
Eclipse Public License 1.0
204 stars 58 forks source link

Error generating images color tif #27

Closed jhidalgo93 closed 8 years ago

jhidalgo93 commented 8 years ago

At the time to get a page of a PDF and apply convertToImage ( BufferedImage.TYPE_INT_RGB , 300) did not save the color image in grayscale only

dragon66 commented 8 years ago

Hi @jhidalgo93,

I don't have enough information to figure out exactly how you used icafe to generate the TIFF. Basically if you set image parameter and let icafe to generate grayscale, it will generate all the pages in grayscale unless you change the parameter to let it generate color in the middle of the process. I guess you set it to generate grayscale in the beginning. If you set it to color, all the pages whether or not it is actually looks like a grayscale image will generate TIFF in color format which will be larger than grayscale output.

jhidalgo93 commented 8 years ago

Could you give me an example of that parameter should add to the generated tiff out color.

The example I'm following is

import java.awt.image.BufferedImage; import java.io.FileOutputStream; import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage;

import com.icafe4j.image.ImageColorType; import com.icafe4j.image.ImageParam; import com.icafe4j.image.options.TIFFOptions; import com.icafe4j.image.quant.DitherMethod; import com.icafe4j.image.quant.DitherMatrix; import com.icafe4j.image.tiff.TIFFTweaker; import com.icafe4j.image.tiff.TiffFieldEnum.Compression; import com.icafe4j.io.FileCacheRandomAccessOutputStream; import com.icafe4j.io.RandomAccessOutputStream;

public class Pdf2TiffConverter { public static void main(String[] args) { String pdf = "princecatalogue.pdf"; PDDocument pddoc = null; try { pddoc = PDDocument.load(pdf); } catch (IOException e) { }

    try {
        savePdfAsTiff(pddoc);
    } catch (IOException e) {
    }
}

private static void savePdfAsTiff(PDDocument pdf) throws IOException {
    BufferedImage[] images = new BufferedImage[pdf.getNumberOfPages()];
    for (int i = 0; i < images.length; i++) {
        PDPage page = (PDPage) pdf.getDocumentCatalog().getAllPages()
                .get(i);
        BufferedImage image;
        try {

// image = page.convertToImage(BufferedImage.TYPE_INT_RGB, 288); //works image = page.convertToImage(BufferedImage.TYPE_INT_RGB, 300); // does not work images[i] = image; } catch (IOException e) { e.printStackTrace(); } }

    FileOutputStream fos = new FileOutputStream("a.tiff");
    RandomAccessOutputStream rout = new FileCacheRandomAccessOutputStream(
            fos);
    ImageParam.ImageParamBuilder builder = ImageParam.getBuilder();
    ImageParam[] param = new ImageParam[1];
    TIFFOptions tiffOptions = new TIFFOptions();
    tiffOptions.setTiffCompression(Compression.CCITTFAX4);
    builder.imageOptions(tiffOptions);
    builder.colorType(ImageColorType.BILEVEL).ditherMatrix(DitherMatrix.getBayer8x8Diag()).applyDither(true).ditherMethod(DitherMethod.BAYER);
    param[0] = builder.build();
    TIFFTweaker.writeMultipageTIFF(rout, param, images);
    rout.close();
    fos.close();
}

}

the version icafe is 1.8

dragon66 commented 8 years ago

If you use Compression.CCITTFAX4, the only choice is black and white since that is what CCITTFAX4 is for.

What you can do are:

1) Use a different compression like Compression.LZW or Compression.DEFLATE. 2) Set ImageParamBuilder colorType like this builder.colorType(ImageColorType.INDEXED) or builder.colorType(ImageColorType.FULL_COLOR), the later one will generate larger image but may or may not improve the quality of the output depending on the original PDF. 3) Set builder.applyDither(true) if you use builder.colorType(ImageColorType.INDEXED) which will use default dither method which in most cases generate better quality color image.

Let me know if that works for you.

Wen

jhidalgo93 commented 8 years ago

Thank you very much for your answer tomorrow when I get to the office as aplicare

Greetings from Colombia

dragon66 commented 8 years ago

You are welcome. Also take a look at the wiki page for icafe and there are a bunch of test cases inside the test folder of the project too which show how icafe work. I haven't find time to write detailed document though.

jhidalgo93 commented 8 years ago

Thank you very much your answer worked for me