T6 Compressed Tiff image - java

I am trying to read an image file in JPG format, write a text into the image,
and save it into a file in a single strip, compressed TIFF image format.
I used Apache Commons library to do the compression and the writing of the
output image file. I have no idea why the output image is drawn like it was split in two parts and the second part drawn first. If anyone can help me resolve this or point out what am I doing wrong here. Thanks..
This is the sample input and output image from the code below...
sample image
FileOutputStream fos = null;
BufferedOutputStream os = null;
ByteArrayOutputStream baos = null;
try {
String inputPath = "D:\\Test\\input.jpg";
File inputFile = new File(inputPath);
BufferedImage inputImage = ImageIO.read(inputFile);
int width = inputImage.getWidth();
int height = inputImage.getHeight();
BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
Font font = new Font("Arial", Font.BOLD, 40 );
java.awt.Graphics2D g2 = outputImage.createGraphics();
g2.setFont( font );
g2.setColor ( Color.BLACK );
g2.drawImage ( inputImage, 0, 0, width, height, Color.WHITE, null);
g2.drawString("TEST TEXT", 20, 40);
g2.dispose();
PixelDensity resolution = PixelDensity.createFromPixelsPerInch ( 200, 200);
double resolutionX = resolution.horizontalDensityInches();
double resolutionY = resolution.verticalDensityInches();
RationalNumber rationalNumX = RationalNumber.valueOf ( resolutionX );
RationalNumber rationalNumY = RationalNumber.valueOf ( resolutionY );
TiffOutputSet outputSet = new TiffOutputSet(ByteOrder.LITTLE_ENDIAN);
TiffOutputDirectory directory = outputSet.addRootDirectory();
baos = new ByteArrayOutputStream();
ImageIO.write(outputImage, "tif", baos);
byte[] bytes = baos.toByteArray();
byte[] compressedBytes = T4AndT6Compression.compressT6(bytes, width, height);
TiffImageData.Data data = new TiffImageData.Data(0, compressedBytes.length, compressedBytes);
TiffElement.DataElement[] dataElements = new TiffElement.DataElement[]{ data };
TiffImageData tiffImageData = new TiffImageData.Strips(dataElements, height);
directory.add ( TiffTagConstants.TIFF_TAG_NEW_SUBFILE_TYPE, 1 );
directory.add ( TiffTagConstants.TIFF_TAG_PHOTOMETRIC_INTERPRETATION, (short) 1 );
directory.add ( TiffTagConstants.TIFF_TAG_COMPRESSION, (short) 4 );
directory.add ( TiffTagConstants.TIFF_TAG_BITS_PER_SAMPLE, (short) 1 );
directory.add ( TiffTagConstants.TIFF_TAG_RESOLUTION_UNIT, (short) 2 );
directory.add ( TiffTagConstants.TIFF_TAG_ROWS_PER_STRIP, height );
directory.add ( TiffTagConstants.TIFF_TAG_IMAGE_WIDTH, width );
directory.add ( TiffTagConstants.TIFF_TAG_IMAGE_LENGTH, height );
directory.add ( TiffTagConstants.TIFF_TAG_XRESOLUTION, rationalNumX );
directory.add ( TiffTagConstants.TIFF_TAG_YRESOLUTION, rationalNumY );
directory.setTiffImageData( tiffImageData );
String outputPath = "D:\\Test\\output.tif";
File outputFile = new File(outputPath);
fos = new FileOutputStream(outputFile);
os = new BufferedOutputStream(fos);
new TiffImageWriterLossy().write( os, outputSet );
} catch (IOException ex) {
Logger.getLogger(JavaApplication7.class.getName()).log(Level.SEVERE, null, ex);
} catch (ImageWriteException ex) {
Logger.getLogger(JavaApplication7.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if ( baos != null ) {
try {
baos.flush();
baos.close();
} catch ( IOException ex ) { }
}
if ( os != null ) {
try {
os.flush();
os.close();
} catch ( IOException ex ) { }
}
if ( fos != null ) {
try {
fos.flush();
fos.close();
} catch ( IOException ex ) { }
}
}

The problem with your code is that you first store the BufferedImage as a TIFF file using ImageIO, then compress the entire file along with headers as the pixel data of the image you pass to commons imaging:
baos = new ByteArrayOutputStream();
ImageIO.write(outputImage, "tif", baos);
byte[] bytes = baos.toByteArray(); // <-- This is not pixel data, but a complete TIFF file
byte[] compressedBytes = T4AndT6Compression.compressT6(bytes, width, height);
The reason this actually resembles the image you expect, is that ImageIO writes the image data uncompressed. The garbage lines and offset before your image, is the TIFF header and tags displayed as pixels...
Instead, you probably meant to do something like:
// Cast is safe here, as you know outputImage is TYPE_BYTE_BINARY
byte[] bytes = ((DataBufferByte) outputImage.getRaster().getDataBuffer()).getData();
byte[] compressedBytes = T4AndT6Compression.compressT6(bytes, width, height);
This will compress just the pixels, and your image should be fine.
You could also do everything using just ImageIO, and avoid the extra dependency on commons imaging, by doing something like this:
try (ImageOutputStream stream = ImageIO.createImageOutputStream(outputFile)) {
ImageTypeSpecifier imageTypeSpecifier = ImageTypeSpecifier.createFromRenderedImage(outputImage);
ImageWriter writer = ImageIO.getImageWriters(imageTypeSpecifier, "TIFF").next();
writer.setOutput(stream);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionType("CCITT T.6");
IIOMetadata metadata = writer.getDefaultImageMetadata(imageTypeSpecifier, param);
// TODO: Set 200 DPI, default is likely 72, and perhaps subfile type if needed,
// other tags will be set correctly for you
writer.write(null, new IIOImage(outputImage, null, metadata), param);
}

Related

javax.imageio.IIOException: Missing Huffman code table entry while Adding text to an jpg image

I am adding text to an image using this code in Android :
public Bitmap drawTextToBitmap(Context gContext, Bitmap image, String gText) {
Resources resources = gContext.getResources();
float scale = resources.getDisplayMetrics().density;
android.graphics.Bitmap.Config bitmapConfig =
image.getConfig();
// set default bitmap config if none
if(bitmapConfig == null) {
bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
}
// resource bitmaps are imutable,
// so we need to convert it to mutable one
Bitmap bitmap = null;
try{
bitmap = image.copy(bitmapConfig, true);
image.recycle();
Canvas canvas = new Canvas(bitmap);
// new antialised Paint
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// text color - #3D3D3D
paint.setColor(Color.WHITE);
// text size in pixels
paint.setTextSize((int) (50 * scale));
// text shadow
paint.setShadowLayer(1f, 0f, 1f, Color.BLACK);
// draw text to the Canvas center
Rect bounds = new Rect();
paint.getTextBounds(gText, 0, gText.length(), bounds);
int padding = bounds.height()/2;
int x = bitmap.getWidth() - (bounds.width()+padding);
int y = (bitmap.getHeight() - (bounds.height()+padding));
canvas.drawText(gText, x, y, paint);
}catch (Throwable e){
AppLog.e("DrawingBitmap","error while adding timestamp",e);
}
return bitmap;
}
Then I create a new File with the transformed bitmap
storeImage(newBitmap, newFileName);
private File storeImage(Bitmap image, String nameFile) {
File pictureFile = new File(getExternalCacheDir(), nameFile);
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
image.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
} catch (FileNotFoundException e) {
AppLog.e("error creating bitmap", "File not found: " + e.getMessage());
} catch (IOException e) {
AppLog.e("error creating bitmap", "Error accessing file: " + e.getMessage());
}
return pictureFile;
}
I send the file to my server, I receive an input stream, I create a File, I scale it and I create a new File with the scaled image :
ImageWriter.write(metadata, new IIOImage(image, null, metadata), param);
I get an IIOException:
javax.imageio.IIOException: Missing Huffman code table entry
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeImage(Native Method)
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeOnThread(JPEGImageWriter.java:1067)
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:363)
at com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:162)
if I don't call drawTextToBitmap() from android I don't get that error.
If someone can help me ... thx
EDIT : here is the way I use to get metadata from my file
private static IIOMetadata readMetaData(File source) {
try {
ImageInputStream stream = ImageIO.createImageInputStream(source);
Iterator<ImageReader> readers = ImageIO.getImageReaders(stream);
IIOMetadata metadata = null;
if (readers.hasNext()) {
ImageReader reader = readers.next();
reader.setInput(stream);
metadata = reader.getImageMetadata(0);
}
return metadata;
}catch (Exception e){
_logger.error(e);
e.printStackTrace();
}
return null;
}
Edit 2 :
Using jpegParams.setOptimizeHuffmanTables(true); works but it resets all metadata and I want to keep them like gps location ...
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(quality);
if (param instanceof JPEGImageWriteParam) {
((JPEGImageWriteParam) param).setOptimizeHuffmanTables(true);
}
IIOImage image = new IIOImage(reader.read(0), null, reader.getImageMetadata(0));
writer.write(null, image, param);
here's my code, which keeps the metadata of image, and get rid of "missing huffman code table" stuff.

Java : PDF page preview error, expected xref

I am trying to create a preview for PDF files which are created by Ballasamic Mockups. Around 50% of the time, I am not getting a preview and getting xref missing error. What am I doing wrong?
Error log :
com.sun.pdfview.PDFParseException: Expected 'xref' at start of table
at com.sun.pdfview.PDFFile.readTrailer(PDFFile.java:974)
at com.sun.pdfview.PDFFile.parseFile(PDFFile.java:1175)
at com.sun.pdfview.PDFFile.<init>(PDFFile.java:126)
at com.sun.pdfview.PDFFile.<init>(PDFFile.java:102)
Code :
private byte[] onlyCreatePdfPreview(String path, int attachId) {
try {
File file = new File(path);
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdffile = new com.sun.pdfview.PDFFile(buf);
PDFPage page = pdffile.getPage(0);
Rectangle rect = new Rectangle(0, 0,
(int) page.getBBox().getWidth(),
(int) page.getBBox().getHeight());
java.awt.Image img = page.getImage(
rect.width, rect.height, //width & height
rect, // clip rect
null, // null for the ImageObserver
true, // fill background with white
true // block until drawing is done
);
BufferedImage buffered = toBufferedImage(img);
buffered = Scalr.resize(buffered, Scalr.Method.ULTRA_QUALITY, 400, 250);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(buffered, "png", baos);
baos.flush();
return baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
}
What am I doing wrong? Thank you.
Final Working code
try {
String pdfPath = zipLocation + String.valueOf(new BigInteger(130, random).toString(32));
PdfReader reader = new PdfReader(path);
PdfStamper pdfStamper = new PdfStamper(reader,new FileOutputStream(pdfPath));
pdfStamper.getWriter().setPdfVersion(PdfWriter.PDF_VERSION_1_4);
pdfStamper.close();
reader.close();
RandomAccessFile raf = new RandomAccessFile(pdfPath, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdffile = new com.sun.pdfview.PDFFile(buf);
PDFPage page = pdffile.getPage(0);
Rectangle rect = new Rectangle(0, 0,
(int) page.getBBox().getWidth(),
(int) page.getBBox().getHeight());
java.awt.Image img = page.getImage(
rect.width, rect.height, //width & height
rect, // clip rect
null, // null for the ImageObserver
true, // fill background with white
true // block until drawing is done
);
BufferedImage buffered = toBufferedImage(img);
buffered = Scalr.resize(buffered, Scalr.Method.ULTRA_QUALITY, 400, 250);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(buffered, "png", baos);
baos.flush();
return baos.toByteArray();
}//catch block
(This answer collects information from comments to the question which eventually led to a solution.)
Apparently com.sun.pdfview.PDFFile expects the cross references to start with xref. But this expectation only makes sense for PDFs following a PDF Reference up to revision 3 (version 1.4) published November 2001; PDFs following a later Reference or even the ISO 32000 standard (part 1 or 2) have the choice of using a cross reference stream (starting with an object number) instead of a cross reference table (starting with xref).
Thus, one should switch to using software following newer specifications than some more than 15 years old version or one has to convert one's PDFs to follow the old specifications, at least on the surface.
One can convert manually (e.g. using Adobe Acrobat) or automatized (e.g. using iText). (These examples software products really are only examples, other products can also be used for this task.)
If using a current iText 5 version, the conversion looks like this:
PdfReader reader = new PdfReader(SOURCE);
PdfStamper stamper = new PdfStamper(reader, DEST);
stamper.getWriter().setPdfVersion(PdfWriter.PDF_VERSION_1_4);
stamper.close();
reader.close();
And one has to take care that if SOURCE is a file name or a random access file, DEST must not be a file output stream to the same file. Otherwise the original file is truncated before the PdfStamper had a chance to copy it all to its output.

Resize a byte array image in pixels

I have an image stored as a byte array. Regardless how large the size, I want to always resize it down to 100x100 pixels.
I am trying to convert the byte array into a bufferedimage, resize and save it back as a bytearray. But with the following code, the images no longer appear on my page.
byte[] pict; // this array contains the image
BufferedImage bufferedImage;
ByteArrayInputStream bais = new ByteArrayInputStream(pict);
try {
bufferedImage = ImageIO.read(bais);
bufferedImage = Scalr.resize(bufferedImage, 100);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( bufferedImage, "jpg", baos );
baos.flush();
pict = baos.toByteArray();
baos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
OutputStream o = response.getOutputStream();
o.write(pict);
Do it this way instead:
Image tmpImage = ImageIO.read(bais);
Image scaled = tmpImage.getScaledInstance(100, 100, Image.SCALE_SMOOTH);
Then, to convert to a byte array, convert the image into a bufferedimage and then that into the byte array:
BufferedImage buffered = ((ToolkitImage) scaled).getBufferedImage();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(buffered, "jpg", baos);
baos.flush();
pict = baos.toByteArray();
baos.close();

ImageIO faster than GZIPStream at max speed

I am looking for the fastest way to download an image from the GPU to a file, for later loading in the same application (so not necessarily PNG e.g.).
I noticed however that when I use a DeflaterStream directly, it is considerably slower than ImageIO with DeflaterStream ( so using ImageIO.write(deflaterstream) ).
Am I doing something wrong? Or is ImageIO just heavily optimized/better than fastest GZIP compression?
glBindTexture(GL_TEXTURE_2D, textureId);
int bpp = 4; // Assuming a 32-bit display with a byte each for red, green, blue, and alpha.
ByteBuffer buffer = BufferUtils.createByteBuffer(SAVE_WIDTH * SAVE_HEIGHT * bpp);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA8, GL_UNSIGNED_BYTE, buffer );
// File file = new File("saves/s-" + layer + ".png"); // The file to save to.
String format = "png"; // Example: "PNG" or "JPG"
// BufferedImage image = new BufferedImage(SAVE_WIDTH, SAVE_HEIGHT, BufferedImage.TYPE_INT_ARGB);
try {
FileOutputStream bOut = new FileOutputStream("saves/p-"+ layer + ".gz");
DeflaterOutputStream gzipOut = new DeflaterOutputStream (bOut, new Deflater(Deflater.BEST_SPEED));
//buffer.flip();
System.out.println("Bytes remaining " + buffer.remaining());
while (buffer.hasRemaining()) gzipOut.write(buffer.get());
gzipOut.close();
bOut.close();
} catch (IOException ex) {
Logger.getLogger(SaveStateManager.class.getName()).log(Level.SEVERE, null, ex);
}
Conmpression is always expensive but you might be able to improve with
OutputStream bOut = new BufferedOutputStream(new FileOutputStream("saves/p-" + layer + ".gz"));
DeflaterOutputStream defOut = new DeflaterOutputStream(bOut, new Deflater(Deflater.BEST_SPEED));
//buffer.flip();
byte[] bytes = new byte[1024];
while (buffer.hasRemaining()) {
int len = Math.min(buffer.remaining(), bytes.length);
buffer.get(bytes, 0, len);
defOut.write(bytes, 0, len);
}
defOut.close();

Convert java.awt.Image to TIFF byte array with compression

I have a java.awt.Image that I need to add CCITT T.6 compression and convert to a TIFF byte array. I have seen some examples of using TIFFImageWriteParam and other classes from the javax.imageio package but I can’t find a complete example going all the way from Image to byte array.
Here is what I have so far beginning with a java.awt.Image obtained from scanning. This works just fine to generate a byte array of a TIFF, but I need to find a way, using TIFFImageWriteParam or some other means, to compress the TIFF prior to processing it as a byte array:
thisImage = ... a java.awt.Image from a scanner
if(thisImage!=null){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedImage bimg = new BufferedImage(thisImage.getWidth(null),thisImage.getHeight(null), BufferedImage.TYPE_BYTE_BINARY);
bimg.createGraphics().drawImage(thisImage, 0, 0, null);
try {
ImageIO.write(bimg, "tiff", baos);
} catch (Exception e) {
e.printStackTrace();
}
thisByteArray = baos.toByteArray();
...
Any help would be appreciated.
I found a solution thanks to: this thread.
Here is what I ended up doing that solved my issue:
thisImage = thisImage = ... a java.awt.Image from a scanner
if(thisImage!=null){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
boolean foundWriter = false;
BufferedImage bimg = new BufferedImage(thisImage.getWidth(null),thisImage.getHeight(null), BufferedImage.TYPE_BYTE_BINARY);
bimg.createGraphics().drawImage(thisImage, 0, 0, null);
for(Iterator<ImageWriter> writerIter = ImageIO.getImageWritersByFormatName("tif"); writerIter.hasNext() && !foundWriter;) {
foundWriter = true;
ImageWriter writer = (ImageWriter)writerIter.next();
writer.setOutput(ios);
TIFFImageWriteParam writeParam = (TIFFImageWriteParam)writer.getDefaultWriteParam();
writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
writeParam.setCompressionType("CCITT T.6");
writer.prepareWriteSequence(null);
ImageTypeSpecifier spec = ImageTypeSpecifier.createFromRenderedImage(bimg);
javax.imageio.metadata.IIOMetadata metadata = writer.getDefaultImageMetadata(spec, writeParam);
IIOImage iioImage = new IIOImage(bimg, null, metadata);
writer.writeToSequence(iioImage, writeParam);
bimg.flush();
writer.endWriteSequence();
ios.flush();
writer.dispose();
ios.close();
thisByteArray = baos.toByteArray();
baos.close();
}
}

Categories