This discussion is archived
9 Replies Latest reply: Dec 15, 2010 9:36 PM by DarrylBurke RSS

Generating TIFF using COMPRESSION_GROUP3_2D

843802 Newbie
Currently Being Moderated
Please, somebody can help me?

This TIFF generated by Black Ice Fax. This TIFF functions perfectly in Dialogic URA. I made a component (J2EE) that makes the TIFF generation but the Dialogic URA does not make the sending of the FAX.

Original TIFF:

Magic: 0x4949 Version: 0x2a
Directory 0: offset 52034 (0xcb42) next 84918 (0x14bb6)
ImageWidth (256) LONG (4) 1<1728>
ImageLength (257) LONG (4) 1<2200>
BitsPerSample (258) SHORT (3) 1<1>
Compression (259) SHORT (3) 1<3>
Photometric (262) SHORT (3) 1<0>
FillOrder (266) SHORT (3) 1<2>
StripOffsets (273) LONG (4) 1<8>
SamplesPerPixel (277) SHORT (3) 1<1>
RowsPerStrip (278) SHORT (3) 1<2200>
StripByteCounts (279) LONG (4) 1<52009>
XResolution (282) RATIONAL (5) 1<204>
YResolution (283) RATIONAL (5) 1<196>
Group3Options (292) LONG (4) 1<4>
ResolutionUnit (296) SHORT (3) 1<2>
PageNumber (297) SHORT (3) 2<2 0>

Directory 1: offset 84918 (0x14bb6) next 0 (0)
ImageWidth (256) LONG (4) 1<1728>
ImageLength (257) LONG (4) 1<2200>
BitsPerSample (258) SHORT (3) 1<1>
Compression (259) SHORT (3) 1<3>
Photometric (262) SHORT (3) 1<0>
FillOrder (266) SHORT (3) 1<2>
StripOffsets (273) LONG (4) 1<52220>
SamplesPerPixel (277) SHORT (3) 1<1>
RowsPerStrip (278) SHORT (3) 1<2200>
StripByteCounts (279) LONG (4) 1<32682>
XResolution (282) RATIONAL (5) 1<204>
YResolution (283) RATIONAL (5) 1<196>
Group3Options (292) LONG (4) 1<4>
ResolutionUnit (296) SHORT (3) 1<2>
PageNumber (297) SHORT (3) 2<2 1>


New TIFF:

Magic: 0x4949 Version: 0x2a
Directory 0: offset 8 (0x8) next 66330 (0x1031a)
SubFileType (254) LONG (4) 1<2>
ImageWidth (256) LONG (4) 1<1728>
ImageLength (257) LONG (4) 1<2200>
BitsPerSample (258) SHORT (3) 1<1>
Compression (259) SHORT (3) 1<3>
Photometric (262) SHORT (3) 1<1>
FillOrder (266) SHORT (3) 1<2>
StripOffsets (273) LONG (4) 1<292>
Orientation (274) SHORT (3) 1<1>
SamplesPerPixel (277) SHORT (3) 1<1>
RowsPerStrip (278) LONG (4) 1<2200>
StripByteCounts (279) LONG (4) 1<66038>
XResolution (282) RATIONAL (5) 1<204>
YResolution (283) RATIONAL (5) 1<196>
Group3Options (292) LONG (4) 1<4>
ResolutionUnit (296) SHORT (3) 1<2>
PageNumber (297) SHORT (3) 2<2 0>
Software (305) ASCII (2) 22
BadFaxLines (326) LONG (4) 1<0>
CleanFaxData (327) SHORT (3) 1<0>

Directory 1: offset 66330 (0x1031a) next 0 (0)
SubFileType (254) LONG (4) 1<2>
ImageWidth (256) LONG (4) 1<1728>
ImageLength (257) LONG (4) 1<2200>
BitsPerSample (258) SHORT (3) 1<1>
Compression (259) SHORT (3) 1<3>
Photometric (262) SHORT (3) 1<1>
FillOrder (266) SHORT (3) 1<2>
StripOffsets (273) LONG (4) 1<66614>
Orientation (274) SHORT (3) 1<1>
SamplesPerPixel (277) SHORT (3) 1<1>
RowsPerStrip (278) LONG (4) 1<2200>
StripByteCounts (279) LONG (4) 1<39878>
XResolution (282) RATIONAL (5) 1<204>
YResolution (283) RATIONAL (5) 1<196>
Group3Options (292) LONG (4) 1<4>
ResolutionUnit (296) SHORT (3) 1<2>
PageNumber (297) SHORT (3) 2<2 1>
Software (305) ASCII (2) 22
BadFaxLines (326) LONG (4) 1<0>
CleanFaxData (327) SHORT (3) 1<0>

Thanks

Luiz Carlos Fonseca
http://www.fonsecanet.com.br
  • 1. Re: Generating TIFF using COMPRESSION_GROUP3_2D
    843802 Newbie
    Currently Being Moderated
    I've been doing quite a bit of work with Java and TIFF images recently, so I may be able to help you.

    What exactly are you trying to do and how are you currently doing it? I don't know what your problem is. Are you losing metadata? I did have that problem, and may be able to help you fix your issue. It would be helpful if you posted your code for reading and writing metadata.
  • 2. Re: Generating TIFF using COMPRESSION_GROUP3_2D
    843802 Newbie
    Currently Being Moderated
    I'm using JPedal (version 3.10b10STD) to read the PDF file:

    PdfDecoder pdfFile = new PdfDecoder();
    try {

    pdfFile.useHiResScreenDisplay(true);
    pdfFile.setLocale(new Locale("br"));
    pdfFile.setExtractionMode(PDF_MODE, PDF_DPI, PDF_SCALE);
    pdfFile.openPdfArray(bai);
                   
    if (pdfFile.isFileViewable()) {

    File[] fileNames = new File[1];
    fileNames[0] = new File("D:/TIF/ARQUIVO.TIF");
    File resultFile = new File("D:/TIF/testeV3.TIF");               
    PrinterDialogicHelper.covertTiff(pdfFile, resultFile);

    }
    } catch (Exception e) {
         LOGGER.error(e);
    } finally {
         pdfFile.closePdfFile();
    }

    Class PrinterDialogicHelper:

    package com.porto.infra.printer.util;

    import java.awt.image.BufferedImage;
    import java.awt.image.ImageObserver;
    import java.awt.image.RenderedImage;
    import java.awt.image.renderable.ParameterBlock;
    import java.io.File;
    import java.util.Vector;

    import javax.media.jai.JAI;

    import org.jpedal.PdfDecoder;

    import com.porto.infra.util.LogManager;
    import com.sun.media.jai.codec.TIFFEncodeParam;
    import com.sun.media.jai.codec.TIFFField;

    public class PrinterDialogicHelper {     
         private static LogManager LOGGER = LogManager.getLog(PrinterDialogicHelper.class);     
         private static final int DEFAULT_HEIGHT = 2200;     //altura
         private static final int DEFAULT_WIDTH = 1728;     //largura
         private static int totalPageCount = 0;

         public PrinterDialogicHelper() {
         }
         
         public static boolean covertTiff(PdfDecoder pdfFile, File resultFile) {          
              boolean result = true;
              int pages = pdfFile.getPageCount();
              totalPageCount = pages;
              Object[][] objPagesTags = new Object[totalPageCount + 1][2];
              Vector images = new Vector();          
              RenderedImage first = null;
              int pageCount = 0;
              try {
                   for (int j = 0; j < pages; j++) {
                        BufferedImage pdfPageImage = pdfFile.getPageAsImage(pageCount + 1);
                        ImageObserver imageObserver = null;
                        BufferedImage faxImage = new BufferedImage(pdfPageImage.getWidth(imageObserver), pdfPageImage.getHeight(imageObserver), BufferedImage.TYPE_BYTE_BINARY);
                        if(LOGGER.isDebugEnabled()){
                             LOGGER.debug("Getting pdf page: " + (pageCount + 1));
                             LOGGER.debug("Create image with dimensions: '" + pdfPageImage.getWidth() + "' x '" + pdfPageImage.getHeight() + "' pixels");
                        }
                        faxImage.getGraphics().drawImage(pdfPageImage, 0, 0, imageObserver);
                        if (j == 0) {
                             //adiciona a primeira imagem
                             first = scaleToPix(faxImage, DEFAULT_WIDTH, DEFAULT_HEIGHT);
                        } else {
                             //adiciona as demais imagens
                             TIFFEncodeParam paramPagesTags = new TIFFEncodeParam();
                             setParam(paramPagesTags);
                             setExtraParam(paramPagesTags, pageCount, totalPageCount);
                             RenderedImage renderedImage = scaleToPix(faxImage, DEFAULT_WIDTH, DEFAULT_HEIGHT);                         
                             objPagesTags[pageCount][0] = renderedImage;
                             objPagesTags[pageCount][1] = paramPagesTags;
                             images.add(objPagesTags[pageCount]);
                        }
                        pageCount++;
                   }
                   TIFFEncodeParam param = new TIFFEncodeParam();
                   setParam(param);
                   setExtraParam(param, 0, totalPageCount);
                   param.setExtraImages(images.iterator());
                   resultFile.getParentFile().mkdirs();
                   ParameterBlock pb = new ParameterBlock();
                   pb.addSource(first);
                   pb.add(resultFile.toString());
                   pb.add("TIFF");
                   pb.add(param);
                   JAI.create("filestore", pb);
              } catch (Exception e) {
                   LOGGER.error(e);
              }
              return result;
         }          

         private static void setParam(TIFFEncodeParam param) {          
              param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP3_2D);          
              param.setWriteTiled(false);
              param.setTileSize(-1, DEFAULT_HEIGHT);
              param.setLittleEndian(true);
              param.setReverseFillOrder(true);
              param.setT4Encode2D(false);
              param.setT4PadEOLs(true);          
         }

         private static void setExtraParam(TIFFEncodeParam param, int PageNb, int TotalPageNb) {
              String SoftwareVersion[] = new String[1];
              SoftwareVersion[0] = "TIFF For Dialogic 1.0";
              TIFFField[] dialogicTags;
              dialogicTags = new TIFFField[11];
              dialogicTags[0] = new TIFFField(254, TIFFField.TIFF_LONG, 1, (Object) new long[] { (long) 2 });
              dialogicTags[1] = new TIFFField(258, TIFFField.TIFF_SHORT, 1, (Object) new char[] { 1 });
              dialogicTags[2] = new TIFFField(262, TIFFField.TIFF_SHORT, 1, (Object) new char[] { 0 });
              dialogicTags[3] = new TIFFField(274, TIFFField.TIFF_SHORT, 1, (Object) new char[] { 1 });
              dialogicTags[4] = new TIFFField(282, TIFFField.TIFF_RATIONAL, 1, (Object) new long[][] { { (long) 204, (long) 1 }, { (long) 0, (long) 0 } });
              dialogicTags[5] = new TIFFField(283, TIFFField.TIFF_RATIONAL, 1, (Object) new long[][] { { (long) 196, (long) 1 }, { (long) 0, (long) 0 } });
              dialogicTags[6] = new TIFFField(296, TIFFField.TIFF_SHORT, 1, (Object) new char[] { 2 });
              dialogicTags[7] = new TIFFField(297, TIFFField.TIFF_SHORT, 2, (Object) new char[] { (char) PageNb, (char) TotalPageNb });
              dialogicTags[8] = new TIFFField(305, TIFFField.TIFF_ASCII, SoftwareVersion.length, (Object) SoftwareVersion);
              dialogicTags[9] = new TIFFField(326, TIFFField.TIFF_LONG, 1, (Object) new long[] { (long) 0 });
              dialogicTags[10] = new TIFFField(327, TIFFField.TIFF_SHORT, 1, (Object) new char[] { 0 });
              param.setExtraFields(dialogicTags);
         }

         public static RenderedImage scaleToPix(RenderedImage source, double pixX, double pixY) {
              
              if (LOGGER.isDebugEnabled()) {
                   LOGGER.debug("Redimensionando a imagem ...");
              }
              
              if (source == null) {
                   return null;
              }
              int ww = source.getWidth();
              int hh = source.getHeight();
              float fattX = (float) (pixX / ww);
              float fattY = (float) (pixY / hh);
                   
    //          //para preservar o aspecto da imagem
    //          float scale = Math.max(fattX, fattY);
    //          //n�o permite deixar a imagem maior do que a original
    //          if (scale > 1.0) {
    //               scale = 1.0F;
    //          }
    //          fattX = scale;
    //          fattY = scale;
              
              if (LOGGER.isDebugEnabled()) {
                   LOGGER.debug("Source Image: '" + ww + "' x '" + hh + "' pixels");
                   LOGGER.debug("Destin Image: '" + pixX + "' x '" + pixY + "' pixels");
                   LOGGER.debug("Scale.......: '" + fattX + "' x '" + fattY + "'");
              }          

              ParameterBlock pb = new ParameterBlock();
              pb.addSource(source);     // The source image
              pb.add(fattX);               // The xScale
              pb.add(fattY);               // The yScale
              pb.add(0.0f);               // The x translation
              pb.add(0.0f);               // The y translation
    //          pb.add(Interpolation.getInstance(Interpolation.INTERP_BILINEAR)); // The interpolation                    
              
              try {
                   source = JAI.create("scale", pb, null);
                   return source;
              } catch (Exception e) {
                   LOGGER.error(e);
                   return null;
              }
         }     
    }

    DUMP TIFF OK:

    Magic: 0x4949 <little-endian> Version: 0x2a
    Directory 0: offset 52034 (0xcb42) next 84918 (0x14bb6)
    ImageWidth (256) LONG (4) 1<1728>
    ImageLength (257) LONG (4) 1<2200>
    BitsPerSample (258) SHORT (3) 1<1>
    Compression (259) SHORT (3) 1<3>
    Photometric (262) SHORT (3) 1<0>
    FillOrder (266) SHORT (3) 1<2>
    StripOffsets (273) LONG (4) 1<8>
    SamplesPerPixel (277) SHORT (3) 1<1>
    RowsPerStrip (278) SHORT (3) 1<2200>
    StripByteCounts (279) LONG (4) 1<52009>
    XResolution (282) RATIONAL (5) 1<204>
    YResolution (283) RATIONAL (5) 1<196>
    Group3Options (292) LONG (4) 1<4>
    ResolutionUnit (296) SHORT (3) 1<2>
    PageNumber (297) SHORT (3) 2<2 0>

    Directory 1: offset 84918 (0x14bb6) next 0 (0)
    ImageWidth (256) LONG (4) 1<1728>
    ImageLength (257) LONG (4) 1<2200>
    BitsPerSample (258) SHORT (3) 1<1>
    Compression (259) SHORT (3) 1<3>
    Photometric (262) SHORT (3) 1<0>
    FillOrder (266) SHORT (3) 1<2>
    StripOffsets (273) LONG (4) 1<52220>
    SamplesPerPixel (277) SHORT (3) 1<1>
    RowsPerStrip (278) SHORT (3) 1<2200>
    StripByteCounts (279) LONG (4) 1<32682>
    XResolution (282) RATIONAL (5) 1<204>
    YResolution (283) RATIONAL (5) 1<196>
    Group3Options (292) LONG (4) 1<4>
    ResolutionUnit (296) SHORT (3) 1<2>
    PageNumber (297) SHORT (3) 2<2 1>

    DUMP new TIFF:
    Magic: 0x4949 <little-endian> Version: 0x2a
    Directory 0: offset 8 (0x8) next 67808 (0x108e0)
    SubFileType (254) LONG (4) 1<2>
    ImageWidth (256) LONG (4) 1<1728>
    ImageLength (257) LONG (4) 1<2200>
    BitsPerSample (258) SHORT (3) 1<1>
    Compression (259) SHORT (3) 1<3>
    Photometric (262) SHORT (3) 1<1>
    FillOrder (266) SHORT (3) 1<2>
    StripOffsets (273) LONG (4) 1<292>
    Orientation (274) SHORT (3) 1<1>
    SamplesPerPixel (277) SHORT (3) 1<1>
    RowsPerStrip (278) LONG (4) 1<2200>
    StripByteCounts (279) LONG (4) 1<67516>
    XResolution (282) RATIONAL (5) 1<204>
    YResolution (283) RATIONAL (5) 1<196>
    Group3Options (292) LONG (4) 1<4>
    ResolutionUnit (296) SHORT (3) 1<2>
    PageNumber (297) SHORT (3) 2<2 0>
    Software (305) ASCII (2) 22<TIFF For Dialogic 1.0\0>
    BadFaxLines (326) LONG (4) 1<0>
    CleanFaxData (327) SHORT (3) 1<0>

    Directory 1: offset 67808 (0x108e0) next 0 (0)
    SubFileType (254) LONG (4) 1<2>
    ImageWidth (256) LONG (4) 1<1728>
    ImageLength (257) LONG (4) 1<2200>
    BitsPerSample (258) SHORT (3) 1<1>
    Compression (259) SHORT (3) 1<3>
    Photometric (262) SHORT (3) 1<1>
    FillOrder (266) SHORT (3) 1<2>
    StripOffsets (273) LONG (4) 1<68092>
    Orientation (274) SHORT (3) 1<1>
    SamplesPerPixel (277) SHORT (3) 1<1>
    RowsPerStrip (278) LONG (4) 1<2200>
    StripByteCounts (279) LONG (4) 1<40170>
    XResolution (282) RATIONAL (5) 1<204>
    YResolution (283) RATIONAL (5) 1<196>
    Group3Options (292) LONG (4) 1<4>
    ResolutionUnit (296) SHORT (3) 1<2>
    PageNumber (297) SHORT (3) 2<2 1>
    Software (305) ASCII (2) 22<TIFF For Dialogic 1.0\0>
    BadFaxLines (326) LONG (4) 1<0>
    CleanFaxData (327) SHORT (3) 1<0>


    Thanks for your help,

    Att.
    Luiz Carlos Fonseca
    http://www.fonsecanet.com.br
  • 3. Re: Generating TIFF using COMPRESSION_GROUP3_2D
    843802 Newbie
    Currently Being Moderated
    I'm sorry but I still don't know what your problem acutally is!

    It looks like your compression settings are working correctly, if that is what you were having issues with, but I'm really not sure.

    Also, it looks like your
    private static void setParam(TIFFEncodeParam param)
    method is working okay.

    What do you need help with?
  • 4. Re: Generating TIFF using COMPRESSION_GROUP3_2D
    843802 Newbie
    Currently Being Moderated
    Before moving in the scale of the image, I used the method below. He finished functioning correctly. Thanks for your help !


         public static BufferedImage invert(BufferedImage image) {
              if(LOGGER.isDebugEnabled()){
                   LOGGER.debug("Invertendo as cores da imagem ...");
              }
              BufferedImage dst = JAI.create("invert", image).getAsBufferedImage();
              return dst;
         }
  • 5. Re: Generating TIFF using COMPRESSION_GROUP3_2D
    843802 Newbie
    Currently Being Moderated
    Your photometric interpolation changed between the old and new image. Which would explain why you'd need to "invert" the image bytes.

    Set the photometric interpolation TIFF tag to the same thing as the original image... And then you shouldn't have to invert.


    CowKing
  • 6. Re: Generating TIFF using COMPRESSION_GROUP3_2D
    843802 Newbie
    Currently Being Moderated
    Hi!!..

    This function works fine in general, but in some case, this not work very well.
    This is because exist a bug in JAI (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4667086)

    The solution is to reduce the size of the image before call this function. Then, I restored the original size of that and I don't have any problems.

    Tanks for your help !.
  • 7. Re: Generating TIFF using COMPRESSION_GROUP3_2D
    843802 Newbie
    Currently Being Moderated
    Hi All,

    I am getting the below error while trying to process particular type of TIFF images. However most of the tiff images are getting displayed without any issues.
    Here is the error that i am getting.

    java.lang.Error: Invalid code encountered while decoding 2D group 4 compressed data.
    at com.sun.media.jai.codecimpl.TIFFFaxDecoder.decodeT6(TIFFFaxDecoder.java:1093)
    at com.sun.media.jai.codecimpl.TIFFImage.getTile(TIFFImage.java:1067)
    at javax.media.jai.RenderedImageAdapter.getTile(RenderedImageAdapter.java:156)
    at javax.media.jai.NullOpImage.computeTile(NullOpImage.java:170)
    at com.sun.media.jai.util.SunTileScheduler.scheduleTile(SunTileScheduler.java:912)
    at javax.media.jai.OpImage.getTile(OpImage.java:1139)
    at javax.media.jai.PlanarImage.getData(PlanarImage.java:2098)
    at javax.media.jai.ScaleOpImage.computeTile(ScaleOpImage.java:1109)
    at com.sun.media.jai.util.SunTileScheduler.scheduleTile(SunTileScheduler.java:912)
    at javax.media.jai.OpImage.getTile(OpImage.java:1139)
    at javax.media.jai.PlanarImage.copyData(PlanarImage.java:2356)
    at javax.media.jai.RenderedOp.copyData(RenderedOp.java:2310)
    at javax.media.jai.PlanarImage.getAsBufferedImage(PlanarImage.java:2538)

    After looking solution i found out that this error occurs for Class F type of TIFF images. Can some one throw some light on this issue?
  • 8. Re: Generating TIFF using COMPRESSION_GROUP3_2D
    824356 Newbie
    Currently Being Moderated
    I am also getting the same error. Please let me know if you solution for the same. Thanks.
  • 9. Re: Generating TIFF using COMPRESSION_GROUP3_2D
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Moderator advice: Please don't post to threads which are long dead, and don't hijack another poster's thread. When you have a question, start your own thread. Feel free to post a link to a related thread.

    Moderator action: Locking this thread.

    db