java - GrayScale Images rendering to dark area when written using JAI / ImageIO -



java - GrayScale Images rendering to dark area when written using JAI / ImageIO -

can explain why happens. read image , render output writer. if color file (or black , white), renders fine. however, if source image grayscale, black box.

sample files available @ https://www.dropbox.com/sh/kyfsh5curobwxrw/aacfwr1nhx8lpuzpzvgwipqia?dl=0

my pom plugin dependancy snippets follow.

<dependency> <groupid>javax.media</groupid> <artifactid>jai_core</artifactid> <version>1.1.3</version> </dependency> <dependency> <groupid>com.sun.media</groupid> <artifactid>jai_imageio</artifactid> <version>1.1</version> </dependency>

a test program. understand bit of code in of no value, in reality part of larger suite of operations. code represents efforts narrow downwards issue little piece of code.

import javax.imageio.iioimage; import javax.imageio.imageio; import javax.imageio.imagereader; import javax.imageio.imagewriter; import javax.imageio.stream.imageinputstream; import javax.imageio.stream.imageoutputstream; import java.awt.*; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; public class grayscaleimaging { public static void main(string[] args) throws ioexception { //works // final file inputfile = new file("/home/vinayb/downloads/page1_color.tif"); // final file outputfile = new file("/home/vinayb/downloads/page1_color_mod.tif"); //doesn't work final file inputfile = new file("/home/vinayb/downloads/page1_grayscale.tif"); final file outputfile = new file("/home/vinayb/downloads/page1_grayscale_mod.tif"); if (outputfile.exists()) { outputfile.delete(); } imagereader imagereader = null; imagewriter imagewriter = null; graphics2d g = null; seek (final imageinputstream imageinputstream = imageio.createimageinputstream(inputfile); final imageoutputstream imageoutputstream = imageio.createimageoutputstream(outputfile);) { //setup reader imagereader = imageio.getimagereaders(imageinputstream).next(); imagereader.setinput(imageinputstream); //read image final bufferedimage initialimage = imagereader.read(0); //prepare graphics output final bufferedimage finalimage = new bufferedimage(initialimage.getwidth(), initialimage.getheight(), imagetype(initialimage)); g = finalimage.creategraphics(); //do image //dosomething(g) //draw image g.drawimage(initialimage, 0, 0, initialimage.getwidth(), initialimage.getheight(), null); //setup author based on reader imagewriter = imageio.getimagewriter(imagereader); imagewriter.setoutput(imageoutputstream); //write imagewriter.write(null, new iioimage(initialimage, null, imagereader.getimagemetadata(0)), imagewriter.getdefaultwriteparam()); } { //cleanup if (imagewriter != null) { imagewriter.dispose(); } if (imagereader != null) { imagereader.dispose(); } if (g != null) { g.dispose(); } } } private static int imagetype(bufferedimage bufferedimage) { homecoming bufferedimage.gettype() == 0 ? bufferedimage.type_int_argb : bufferedimage.gettype(); } }

okay, have fixed decoder, sample file! :-)

anyway, after research, have come conclusion problem definitively sample file, not code nor library using.

the issue file tiff metadata contains photometricinterpretation == 3/palette , colormap tags. ie. image uses indexed color model/palette. if image read should according (my understanding of) spec, using supplied color map, image comes out black. if instead ignore this, , rather read grayness scale (assuming photometricinterpretation == 1/blackiszero), comes out black text on white (light gray) background.

edit:

a improve explanation, values in color map 8 bit quantities (using low 8 bits of each color entry) instead of using total 16 bit should... if observe while reading , creating palette using low 8 bits, image comes out intended (as in dropbox). still bad image according spec, detectable.

java graphics rendering javax.imageio

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -