Site iconJava PDF Blog

How to Convert CCITT data to TIFF image

TIFF icon

What is CCITT data?

CCITT is used to compress black and white image data. Using Huffman encoding, the data is squeezed into a much smaller compressed stream.

CCITT is also a compression format used in the TIFF file format. By adding some additional bytes to your raw CCITT data, and saving it in a file ending .tif, you can create a TIFF Image from raw CCITT data. My example is written in Java (but it should be easy to recode in any language). It  will take the raw data and add the required bytes.

CCITT data in PDF files


CCITT is used as a compression format in PDF files for images in XObjects. You can manually extract the CCITT data and the Dictionary values (K, isBlack, etc) from PDF files if you want to reuse the images.  If you have extracted the CCITT data from a PDF, there may be some differences between the raw image and the image in the PDF – remember this is the raw image which may be inverted, coloured, clipped, etc.

How to convert CCITT to a Tiff

public static void saveAsTIFF(int w, int h,PdfObject DecodeParms,
byte[] data, String fileName){
  1. Get the CCITT parameters
    /**
     * default values (these may be set in a PDF DecodeParms dictionary)
    **/boolean isBlack = false;  //flag to show if default is black/white
    int k = 0;
    w = -1;
    
  2. Create a metadata header
    /**
     * build the image
    **/ByteArrayOutputStream bos=new ByteArrayOutputStream();
    
    /**
     * tiff header (id, version, offset)
    **/String[] headerValues={"4d","4d","00","2a","00","00","00","08"};
    for(int i=0;i<headervalues.length;i++) bos.write(integer.parseint(headervalues[i],16));="" int="" tagcount="9;" appears="" to="" be="" minimum="" needed="" writeword="" and="" write="" tag="" are="" convenience="" methods="" add="" the="" values="" as="" bytes="" stream="" **="" *="" ifd="" image="" file="" directory="" writeword(string.valueof(tagcount),bos);="" num="" of="" entries="" writetag("256",="" "04",="" "01",="" string.valueof(w),="" bos);="" **width*="" writetag("257",="" string.valueof(h),="" **length*="" **bitspersample="" 258="" -="" b&w="" 1="" bit="" image*="" writetag("258",="" "03",="" "00010000h",="" if="" (k="=" 0){="" writetag("259",="" "00030000h",="" compression="" }else=""> 0)
       writeTag("259", "03", "01", "00020000h", bos); //compression
    else if (k < 0)
       writeTag("259", "03", "01", "00040000h", bos); //compression
    
    //photometricInterpretation
    if(!isBlack)
       writeTag("262", "03", "01", "00000000h", bos);
    else
       writeTag("262", "03", "01", "00010000h", bos);
    
    //stripOffsets -start of data after tables
    writeTag("273", "04", "1","122", bos); 
    
    //samplesPerPixel
    writeTag("277", "03", "01", "00010000h", bos);
    //rowsPerStrip - uses height
    writeTag("278", "04", "01", String.valueOf(h), bos);
    //stripByteCount - 1 strip so all data
    writeTag("279", "04", "1", String.valueOf(data.length),bos);
    // write next IOD offset  zero as no other table
    writeDWord("0",bos); 
    
    
  3. Append the raw CCITT data
    /**
     * write the CCITT image data at the end
    **/try{
    
       bos.write(data);
       bos.close();
    
    } catch (IOException e) {
       LogWriter.writeLog("[PDF] Tiff exception  "+e);
    }
    
    /**save data as image */try {
    
       FileOutputStream fos=new FileOutputStream(fileName);
       fos.write(bos.toByteArray());
       fos.close();
    
       } catch (Error err) {
          LogWriter.writeLog("[PDF] Tiff error "+err);
       } catch (Exception e1) {
          LogWriter.writeLog("[PDF] Tiff exception  "+e1);
       }
    }

Do you need to read or write Tiff files in Java?

Our JDeli image library offers a range of advantages over ImageIO and alternatives for Tiff files, including: