Hello,
I am facing a problem of scaling an image which is loaded from a file (gif
or jpeg) using
the Image Decoders of JAI (Java Advanced Imaging). The first part of the
problem is, if a
scaling factor is an integral number , it is throwing an exception and the
following is the
stack trace:
java.awt.image.ImagingOpException: Unable to transform src image
at
java.awt.image.AffineTransformOp.filter(AffineTransformOp.java:236)
at sun.java2d.SunGraphics2D.renderingPipeImage(Compiled Code)
at sun.java2d.SunGraphics2D.drawImage(Compiled Code)
at
sun.awt.image.BufferedImageGraphics2D.drawImage(BufferedImageGraphics
2D.java:496)
But if the scaling factor is float or double, it is not throwing an
exception.
Is this a known bug and if it is when will it be fixed.
The second part of the problem is:
I am displaying the loaded image in a scrollpane (JScrollpane of Swing).
Assuming that the
scaling factor used is a float or double and no exception is thrown, I use a
scale factor of
15.99999 (actually I want the factor to be 16) and try to scroll the image.
The scrolling is
very very slow. The scrolling speed is much slower as compared to the
scrolling of a scaled
image where the original image is loaded from a file(gif or jpeg) using the
Toolkit class.
Here is the code to show both the parts of the problem.
In class ScaleTest1, the image is loaded from a file (gif or jpeg) using the
Toolkit class
and the scrolling is quite fast.
In class ScaleTest2, the image is loaded from a file (gif or jpeg) using the
JAI class and
the scrolling is tremendously slow. Also try to enter an integral scaling
factor which
results in an exception.
You will have to change the filepaths to load a file in the source code. The
zoom factor can
be entered by selecting a menuitem.
Please let me know if you have any solutions to the problem.
I am using JDK "1.2" version for Java Runtime Environment and JAI beta for
Java Advanced
Imaging classes.
--------------------------------------------------------------------------------------
ScaleTest1:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.print.*;
import java.awt.geom.*;
public class ScaleTest1 extends JFrame
{
ScrollablePicture sp = null;
public ScaleTest1()
{
try
{
jbInit();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
ScaleTest1 scaleTest = new ScaleTest1();
scaleTest.pack();
scaleTest.setVisible(true);
}
private JMenuBar getFrameMenuBar()
{
JMenuBar mb = new JMenuBar();
JMenu menu = new JMenu("File");
JMenuItem menuItem = new JMenuItem("Zoom");
//Add listener
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String value =
JOptionPane.showInputDialog(ScaleTest1.this,"Enter zoom
factor");
sp.zoom(Float.parseFloat(value));
}
});
menu.add(menuItem);
mb.add(menu);
return mb;
}
private void jbInit() throws Exception
{
Image image =
Toolkit.getDefaultToolkit().getImage("f:\\ajay\\images\\AboutSwing.gif");
MediaTracker md = new MediaTracker(this);
md.addImage(image, 0);
md.waitForID(0);
sp = new ScrollablePicture(image);
sp.setPreferredSize(new
Dimension(image.getWidth(this),image.getHeight(this)));
sp.setAutoscrolls(true);
JScrollPane scrollPane = new JScrollPane(sp,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
this.getContentPane().add(scrollPane);
this.setJMenuBar(getFrameMenuBar());
}
private class ScrollablePicture extends JComponent implements Scrollable
{
int maxUnitIncrement = 1;
Image image = null;
int originalWidth = 0;
int originalHeight = 0;
private float zoomFactor = 1.0f;
public ScrollablePicture(Image image)
{
this.image = image;
originalWidth = image.getWidth(null);
originalHeight = image.getHeight(null);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2D = (Graphics2D)g;
AffineTransform af =
AffineTransform.getScaleInstance(zoomFactor,zoomFactor);
g2D.drawImage(image,af,this);
}
private void zoom(float zoomFactor)
{
this.zoomFactor = zoomFactor;
int destWidth = (int)Math.ceil(originalWidth*zoomFactor);
int destHeight = (int)Math.ceil(originalHeight*zoomFactor);
setPreferredSize(new Dimension(destWidth, destHeight));
setSize(new Dimension(destWidth, destHeight));
revalidate();
repaint();
}
public Dimension getPreferredScrollableViewportSize()
{
Dimension d = getPreferredSize();
int width = d.width -30;
int height = d.height-30;
System.out.println("Preferred Size "+d);
return (new Dimension(width,height));
}
public int getScrollableUnitIncrement(Rectangle visibleRect, int
orientation, int
direction)
{
return maxUnitIncrement;
}
public int getScrollableBlockIncrement(Rectangle visibleRect, int
orientation, int
direction)
{
if (orientation == SwingConstants.HORIZONTAL)
return visibleRect.width - maxUnitIncrement;
else
return visibleRect.height - maxUnitIncrement;
}
public boolean getScrollableTracksViewportWidth() {
return false;
}
public boolean getScrollableTracksViewportHeight() {
return false;
}
}
}
---------------------------------------------------------------------------------
ScaleTest2:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.print.*;
import java.awt.geom.*;
import javax.media.jai.*;
import javax.media.jai.codec.*;
public class ScaleTest2 extends JFrame
{
ScrollablePicture sp = null;
public ScaleTest2()
{
try
{
jbInit();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
ScaleTest2 scaleTest = new ScaleTest2();
// scrollPaneTest.setSize(300,250);
scaleTest.pack();
scaleTest.setVisible(true);
}
private JMenuBar getFrameMenuBar()
{
JMenuBar mb = new JMenuBar();
JMenu menu = new JMenu("File");
JMenuItem menuItem = new JMenuItem("Zoom");
//Add listener
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String value =
JOptionPane.showInputDialog(ScaleTest2.this,"Enter zoom
factor");
sp.zoom(Float.parseFloat(value));
}
});
menu.add(menuItem);
mb.add(menu);
return mb;
}
private void jbInit() throws Exception
{
RenderedImage im =
(RenderedImage)JAI.create("fileload","f:\\ajay\\images\\Cougar.jpg");
BufferedImage image =
(PlanarImage.wrapRenderedImage(im)).getAsBufferedImage();
sp = new ScrollablePicture(image);
sp.setPreferredSize(new
Dimension(image.getWidth(),image.getHeight()));
sp.setAutoscrolls(true);
JScrollPane scrollPane = new JScrollPane(sp,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
this.getContentPane().add(scrollPane);
this.setJMenuBar(getFrameMenuBar());
}
private class ScrollablePicture extends JComponent implements Scrollable
{
int maxUnitIncrement = 1;
BufferedImage image = null;
int originalWidth = 0;
int originalHeight = 0;
private float zoomFactor = 1.0f;
public ScrollablePicture(BufferedImage image)
{
this.image = image;
originalWidth = image.getWidth(null);
originalHeight = image.getHeight(null);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2D = (Graphics2D)g;
AffineTransform af =
AffineTransform.getScaleInstance(zoomFactor,zoomFactor);
g2D.drawImage(image,af,this);
}
private void zoom(float zoomFactor)
{
this.zoomFactor = zoomFactor;
int destWidth = (int)Math.ceil(originalWidth*zoomFactor);
int destHeight = (int)Math.ceil(originalHeight*zoomFactor);
setPreferredSize(new Dimension(destWidth, destHeight));
setSize(new Dimension(destWidth, destHeight));
revalidate();
repaint();
}
public Dimension getPreferredScrollableViewportSize()
{
Dimension d = getPreferredSize();
int width = d.width -30;
int height = d.height-30;
System.out.println("Preferred Size "+d);
return (new Dimension(width,height));
}
public int getScrollableUnitIncrement(Rectangle visibleRect, int
orientation, int
direction)
{
return maxUnitIncrement;
}
public int getScrollableBlockIncrement(Rectangle visibleRect, int
orientation, int
direction)
{
if (orientation == SwingConstants.HORIZONTAL)
return visibleRect.width - maxUnitIncrement;
else
return visibleRect.height - maxUnitIncrement;
}
public boolean getScrollableTracksViewportWidth() {
return false;
}
public boolean getScrollableTracksViewportHeight() {
return false;
}
}
}
______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com
=====================================================================
To subscribe/unsubscribe, send mail to [EMAIL PROTECTED]
Java 2D Home Page: http://java.sun.com/products/java-media/2D/