Skip to main content

[jai imageio] thumbnail generation and quality with different jdk & library

4 replies [Last post]
xarnorx
Offline
Joined: 2006-11-10
Points: 0

HI to all,

I done a test with different rendering method (jai inclusive) and different jdk (1.5,1.6 on win,osX and linux)
Only jiu create a good Thumbnail in all situation.
Graphics2D works fine in osX but in windows and linux I get poor result.
With my snippets jai produce always a poor thumbnail in all jdk.

Here my Test class. You need in your classpath jiu.jar (http://schmidt.devlib.org/jiu/download.html) jai_codec.jar, jai_core.jar (http://download.java.net/media/jai/builds/release/1_1_3/jai-1_1_3-lib.zip) and EditableImage.jar (https://mistral.dev.java.net/files/documents/5620/49995/mistral-0.9.4.44...)

import it.tidalwave.image.EditableImage;
import it.tidalwave.image.Quality;
import it.tidalwave.image.op.ReadOp;
import it.tidalwave.image.op.ScaleOp;
import it.tidalwave.image.op.WriteOp;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.Deflater;

import javax.imageio.ImageIO;
import javax.media.jai.Interpolation;
import javax.media.jai.InterpolationBicubic2;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.RenderedOp;

import net.sourceforge.jiu.codecs.CodecMode;
import net.sourceforge.jiu.codecs.ImageLoader;
import net.sourceforge.jiu.codecs.InvalidFileStructureException;
import net.sourceforge.jiu.codecs.InvalidImageIndexException;
import net.sourceforge.jiu.codecs.PNGCodec;
import net.sourceforge.jiu.codecs.PNMCodec;
import net.sourceforge.jiu.codecs.UnsupportedCodecModeException;
import net.sourceforge.jiu.codecs.UnsupportedTypeException;
import net.sourceforge.jiu.data.PixelImage;
import net.sourceforge.jiu.geometry.Resample;
import net.sourceforge.jiu.geometry.ScaleReplication;
import net.sourceforge.jiu.ops.ImageToImageOperation;
import net.sourceforge.jiu.ops.MissingParameterException;
import net.sourceforge.jiu.ops.Operation;
import net.sourceforge.jiu.ops.OperationFailedException;
import net.sourceforge.jiu.ops.WrongParameterException;

public class TestThumb {

/**
* @param args
* @throws IOException
* @throws OperationFailedException
* @throws MissingParameterException
*/
public static void main(String[] args) throws IOException {
double scale = 0.045;
String source = "source2.png";
if (args.length > 0){
source = args[0];
scale = Double.parseDouble(args[1]);
}
String thumbName = "thumb";

// Mistral version
File file = new File(source);
EditableImage image = EditableImage.create(new ReadOp(file));
image.execute(new ScaleOp(scale, Quality.BEST));
image.execute(new WriteOp("png",new File(thumbName +"_ms.png")));

//Graphics2D version
boolean quality = true;
Object interpolating = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
Object rendering = RenderingHints.VALUE_RENDER_SPEED;
Object colorRendering = RenderingHints.VALUE_COLOR_RENDER_SPEED;
Object antialiasing = RenderingHints.VALUE_ANTIALIAS_OFF;
Object dithering = RenderingHints.VALUE_DITHER_DISABLE;

if (quality){
interpolating = RenderingHints.VALUE_INTERPOLATION_BICUBIC;
rendering = RenderingHints.VALUE_RENDER_QUALITY;
colorRendering = RenderingHints.VALUE_COLOR_RENDER_QUALITY;
antialiasing = RenderingHints.VALUE_ANTIALIAS_ON;
dithering = RenderingHints.VALUE_DITHER_ENABLE;
}

BufferedImage image2D = ImageIO.read(new File(source));
int width = (int) (scale * image2D.getWidth());
int height = (int) (scale * image2D.getHeight());
BufferedImage thumbnail = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);

Graphics2D bg = thumbnail.createGraphics();
// set all thumbnail property
System.out.println("thumbnail interpolation: " + interpolating);
System.out.println("thumbnail rendering mode: " + rendering);
System.out.println("thumbnail color rendering mode: " + colorRendering);
System.out.println("thumbnail antialising mode: " + antialiasing);
System.out.println("thumbnail dithering mode: " + dithering);
bg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, interpolating);
bg.setRenderingHint(RenderingHints.KEY_RENDERING, rendering);
bg.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, colorRendering);
bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialiasing);
bg.setRenderingHint(RenderingHints.KEY_DITHERING, dithering);

// scale image
AffineTransform xform = AffineTransform.getScaleInstance(scale,scale);
bg.drawRenderedImage(image2D, xform);
bg.dispose();

try {
ImageIO.write(thumbnail, "png", new File( thumbName + "_2d.png"));
} catch (IOException e) {
System.out.println(e);
}

// JIU version
PixelImage jiuImage = null;
try {
System.out.println("jiu load image...");
jiuImage = ImageLoader.load(source);
} catch (InvalidFileStructureException e) {
e.printStackTrace();
} catch (InvalidImageIndexException e) {
e.printStackTrace();
} catch (UnsupportedTypeException e) {
e.printStackTrace();
}

PixelImage scaledImage = null;
try {
Resample resample = new Resample();
resample.setInputImage(jiuImage);
resample.setSize((int)(jiuImage.getWidth() * scale), (int)(jiuImage.getHeight() * scale));
System.out.println("jiu scale image..."+ scale);
resample.setFilter(Resample.FILTER_TYPE_LANCZOS3);
resample.process();
scaledImage = resample.getOutputImage();
} catch (MissingParameterException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (WrongParameterException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

try {
String filename = thumbName + "_jui.png";
System.out.println("jiu write image..."+ filename);
PNGCodec codec = new PNGCodec();
codec.setFile(filename, CodecMode.SAVE);
codec.setImage(scaledImage);
codec.setCompressionLevel(Deflater.BEST_COMPRESSION);
codec.process();
codec.close();
} catch (MissingParameterException e) {
e.printStackTrace();
} catch (OperationFailedException e) {
e.printStackTrace();
}

//JAI version
RenderedOp im = JAI.create("fileload", source);
// try different setting but nothing happen
RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
qualityHints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
qualityHints.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
qualityHints.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
qualityHints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

ParameterBlock pb = new ParameterBlock();
pb.addSource(im); // The source image
pb.add((float)scale); // The xScale
pb.add((float)scale); // The yScale
pb.add(0.0F); // The x translation
pb.add(0.0F); // The y translation
pb.add(Interpolation.getInstance(Interpolation.INTERP_BICUBIC_2)); // The interpolation
// Create the scale operation
System.out.println("jai scale image..." + scale);
im = JAI.create("scale", pb, qualityHints);
String filename = thumbName + "_jai.png";
String format = "png";
System.out.println("jai write image..."+ filename);
RenderedOp op = JAI.create("filestore", im, filename, format);

}

}

I tested my class with this image -> http://www.kemen.it/demo/thumb/source.png
I would like use jai java edition in two my project but I can't with this result. Jiu burn a lot of memory and cpu.

Any help or Idea will be appreciate.

regards Lorenzo Sicilia

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
aaitkhelifa
Offline
Joined: 2008-12-22
Points: 0

It seem, with Mistral framework, there is no resizing ...

fabriziogiudici
Offline
Joined: 2006-01-04
Points: 0

There is, but it only works with JAI at the moment, not Java2D.

Brian Burkhalter

Please read this posting

http://archives.java.sun.com/cgi-bin/wa?A2=ind0311&L=jai-interest&F=&S=&...

and review your work afterwards.

Brian

On Thu, 15 Mar 2007, jai-imageio@javadesktop.org wrote:

> HI to all,
>
> I done a test with different rendering method (jai inclusive) and different jdk (1.5,1.6 on win,osX and linux)
> Only jiu create a good Thumbnail in all situation.
> Graphics2D works fine in osX but in windows and linux I get poor result.
> With my snippets jai produce always a poor thumbnail in all jdk.
>
> Here my Test class. You need in your classpath jiu.jar (http://schmidt.devlib.org/jiu/download.html) jai_codec.jar, jai_core.jar (http://download.java.net/media/jai/builds/release/1_1_3/jai-1_1_3-lib.zip) and EditableImage.jar (https://mistral.dev.java.net/files/documents/5620/49995/mistral-0.9.4.44...)
>
>
>
> import it.tidalwave.image.EditableImage;
> import it.tidalwave.image.Quality;
> import it.tidalwave.image.op.ReadOp;
> import it.tidalwave.image.op.ScaleOp;
> import it.tidalwave.image.op.WriteOp;
>
> import java.awt.Graphics2D;
> import java.awt.RenderingHints;
> import java.awt.geom.AffineTransform;
> import java.awt.image.BufferedImage;
> import java.awt.image.renderable.ParameterBlock;
> import java.io.File;
> import java.io.FileOutputStream;
> import java.io.IOException;
> import java.util.zip.Deflater;
>
> import javax.imageio.ImageIO;
> import javax.media.jai.Interpolation;
> import javax.media.jai.InterpolationBicubic2;
> import javax.media.jai.JAI;
> import javax.media.jai.PlanarImage;
> import javax.media.jai.RenderedOp;
>
> import net.sourceforge.jiu.codecs.CodecMode;
> import net.sourceforge.jiu.codecs.ImageLoader;
> import net.sourceforge.jiu.codecs.InvalidFileStructureException;
> import net.sourceforge.jiu.codecs.InvalidImageIndexException;
> import net.sourceforge.jiu.codecs.PNGCodec;
> import net.sourceforge.jiu.codecs.PNMCodec;
> import net.sourceforge.jiu.codecs.UnsupportedCodecModeException;
> import net.sourceforge.jiu.codecs.UnsupportedTypeException;
> import net.sourceforge.jiu.data.PixelImage;
> import net.sourceforge.jiu.geometry.Resample;
> import net.sourceforge.jiu.geometry.ScaleReplication;
> import net.sourceforge.jiu.ops.ImageToImageOperation;
> import net.sourceforge.jiu.ops.MissingParameterException;
> import net.sourceforge.jiu.ops.Operation;
> import net.sourceforge.jiu.ops.OperationFailedException;
> import net.sourceforge.jiu.ops.WrongParameterException;
>
> public class TestThumb {
>
> /**
> * @param args
> * @throws IOException
> * @throws OperationFailedException
> * @throws MissingParameterException
> */
> public static void main(String[] args) throws IOException {
> double scale = 0.045;
> String source = "source2.png";
> if (args.length > 0){
> source = args[0];
> scale = Double.parseDouble(args[1]);
> }
> String thumbName = "thumb";
>
> // Mistral version
> File file = new File(source);
> EditableImage image = EditableImage.create(new ReadOp(file));
> image.execute(new ScaleOp(scale, Quality.BEST));
> image.execute(new WriteOp("png",new File(thumbName +"_ms.png")));
>
> //Graphics2D version
> boolean quality = true;
> Object interpolating = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
> Object rendering = RenderingHints.VALUE_RENDER_SPEED;
> Object colorRendering = RenderingHints.VALUE_COLOR_RENDER_SPEED;
> Object antialiasing = RenderingHints.VALUE_ANTIALIAS_OFF;
> Object dithering = RenderingHints.VALUE_DITHER_DISABLE;
>
> if (quality){
> interpolating = RenderingHints.VALUE_INTERPOLATION_BICUBIC;
> rendering = RenderingHints.VALUE_RENDER_QUALITY;
> colorRendering = RenderingHints.VALUE_COLOR_RENDER_QUALITY;
> antialiasing = RenderingHints.VALUE_ANTIALIAS_ON;
> dithering = RenderingHints.VALUE_DITHER_ENABLE;
> }
>
> BufferedImage image2D = ImageIO.read(new File(source));
> int width = (int) (scale * image2D.getWidth());
> int height = (int) (scale * image2D.getHeight());
> BufferedImage thumbnail = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
>
>
> Graphics2D bg = thumbnail.createGraphics();
> // set all thumbnail property
> System.out.println("thumbnail interpolation: " + interpolating);
> System.out.println("thumbnail rendering mode: " + rendering);
> System.out.println("thumbnail color rendering mode: " + colorRendering);
> System.out.println("thumbnail antialising mode: " + antialiasing);
> System.out.println("thumbnail dithering mode: " + dithering);
> bg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, interpolating);
> bg.setRenderingHint(RenderingHints.KEY_RENDERING, rendering);
> bg.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, colorRendering);
> bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialiasing);
> bg.setRenderingHint(RenderingHints.KEY_DITHERING, dithering);
>
> // scale image
> AffineTransform xform = AffineTransform.getScaleInstance(scale,scale);
> bg.drawRenderedImage(image2D, xform);
> bg.dispose();
>
> try {
> ImageIO.write(thumbnail, "png", new File( thumbName + "_2d.png"));
> } catch (IOException e) {
> System.out.println(e);
> }
>
> // JIU version
> PixelImage jiuImage = null;
> try {
> System.out.println("jiu load image...");
> jiuImage = ImageLoader.load(source);
> } catch (InvalidFileStructureException e) {
> e.printStackTrace();
> } catch (InvalidImageIndexException e) {
> e.printStackTrace();
> } catch (UnsupportedTypeException e) {
> e.printStackTrace();
> }
>
> PixelImage scaledImage = null;
> try {
> Resample resample = new Resample();
> resample.setInputImage(jiuImage);
> resample.setSize((int)(jiuImage.getWidth() * scale), (int)(jiuImage.getHeight() * scale));
> System.out.println("jiu scale image..."+ scale);
> resample.setFilter(Resample.FILTER_TYPE_LANCZOS3);
> resample.process();
> scaledImage = resample.getOutputImage();
> } catch (MissingParameterException e1) {
> // TODO Auto-generated catch block
> e1.printStackTrace();
> } catch (WrongParameterException e1) {
> // TODO Auto-generated catch block
> e1.printStackTrace();
> }
>
> try {
> String filename = thumbName + "_jui.png";
> System.out.println("jiu write image..."+ filename);
> PNGCodec codec = new PNGCodec();
> codec.setFile(filename, CodecMode.SAVE);
> codec.setImage(scaledImage);
> codec.setCompressionLevel(Deflater.BEST_COMPRESSION);
> codec.process();
> codec.close();
> } catch (MissingParameterException e) {
> e.printStackTrace();
> } catch (OperationFailedException e) {
> e.printStackTrace();
> }
>
> //JAI version
> RenderedOp im = JAI.create("fileload", source);
> // try different setting but nothing happen
> RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
> qualityHints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
> // qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
> qualityHints.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
> qualityHints.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
> qualityHints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
>
> ParameterBlock pb = new ParameterBlock();
> pb.addSource(im); // The source image
> pb.add((float)scale); // The xScale
> pb.add((float)scale); // The yScale
> pb.add(0.0F); // The x translation
> pb.add(0.0F); // The y translation
> pb.add(Interpolation.getInstance(Interpolation.INTERP_BICUBIC_2)); // The interpolation
> // Create the scale operation
> System.out.println("jai scale image..." + scale);
> im = JAI.create("scale", pb, qualityHints);
> String filename = thumbName + "_jai.png";
> String format = "png";
> System.out.println("jai write image..."+ filename);
> RenderedOp op = JAI.create("filestore", im, filename, format);
>
> }
>
>
> }
>
> I tested my class with this image -> http://www.kemen.it/demo/thumb/source.png
> I would like use jai java edition in two my project but I can't with this result. Jiu burn a lot of memory and cpu.
>
> Any help or Idea will be appreciate.
>
> regards Lorenzo Sicilia
> [Message sent by forum member 'xarnorx' (xarnorx)]
>
> http://forums.java.net/jive/thread.jspa?messageID=208111
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>
>

----------------
Brian Burkhalter
Java Media, Imaging, and Graphics
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

Fabrizio Giudici

On Mar 15, 2007, at 16:28 , Brian Burkhalter wrote:

> Please read this posting
>
> http://archives.java.sun.com/cgi-bin/wa?A2=ind0311&L=jai-
> interest&F=&S=&P=15036
>
> and review your work afterwards.

Lorenzo,

I've sent you a private email with a pointer to the latest build of
Mistral - I've (partially, at the moment) implemented the algorithm
pointed by Brian (which is the only viable way to go) with the JAI
adapter.

PS As I don't think I've ever talked about Mistral in this mailing
list, among other things it's basically a wrapper against JAI, that
can be used to simplify the programming model.

--
Fabrizio Giudici, Ph.D. - Java Architect, Project Manager
Tidalwave s.a.s. - "We make Java work. Everywhere."
weblogs.java.net/blog/fabriziogiudici - www.tidalwave.it/blog
Fabrizio.Giudici@tidalwave.it - mobile: +39 348.150.6941

[smime.p7s]