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
- Get the CCITT parameters
- Create a metadata header
- Append the raw CCITT data
and the Java code to write TIFF…
/*
* 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;
int w = -1;
int h = -1;
/*
* 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 writeTag are convenience methods
// add the values as bytes to the stream
/* IFD – Image File Directory */
writeWord(String.valueOf(tagCount), bos); // num of entries
writeTag("256", "04", "01", String.valueOf(w), bos); // width
writeTag("257", "04", "01", String.valueOf(h), bos); // length
// BitsPerSample 258 – B&W 1 bit image
writeTag("258", "03", "01", "00010000h", bos);
if (k == 0)
writeTag("259", "03", "01", "00030000h", bos); // compression
else if (k > 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);
/*
* 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);
}
}
In this tutorial you learned how to change CCITT data to a TIFF image, we have many more blog posts for Java developers working with image technology. Please feel free to check them out.
As experienced Java developers, we help you work with images in Java and bring over a decade of hands-on experience with many image file formats.
Top comments (0)