JDK-6191064 : REGRESSION:Graphics.drawImage based on a subimage draws at the wrong location
  • Type: Bug
  • Component: client-libs
  • Sub-Component: 2d
  • Affected Version: 5.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-11-04
  • Updated: 2014-02-27
  • Resolved: 2004-12-21
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
5.0u2 b04Fixed
Description
FULL PRODUCT VERSION :
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Windows XP

EXTRA RELEVANT SYSTEM CONFIGURATION :
1.5.0 shows problem, 1.4.2_03 is ok on same machine

A DESCRIPTION OF THE PROBLEM :
Using drawImage to show a subimage extracted from the bottom right of a larger image causes the image to appear in the wrong location (appears to be offset as if the origin of the larger image were based at the destination x,y). My sample is scaled for twips, which seems to contribute to the problem. Without the scaling the result seems ok.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Create a sample image 100x100 pixels.Load and view it using the attached sample code.  You should see the bottom right of your image.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
On JDK 1.4.2 the subimage is within the rectangle,  this is the desired behaviour.
ACTUAL -
On JDK 1.5.0 the subimage is completely outside the rectangle.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
// Test_SubImage.java

import java.awt.*;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;

public class Test_SubImage extends JComponent
{
	public static void main(String[] args) throws Exception
	{
		System.out.println(System.getProperty("java.version"));
		JFrame f = new JFrame("Test SubImage");
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.getContentPane().add(new Test_SubImage());
		f.setBounds(50,50,400,400);
		f.setVisible(true);
	}
	
	public Test_SubImage() throws IOException
	{
		// test with a 100x100 pixel image
		 m_Image = ImageIO.read(new File("C:\\Temp\\100x100.png"));
	}
	public void paint(Graphics g)
	{
		super.paint(g);
		Graphics2D g2 = (Graphics2D)g;
		g2.scale(0.05,0.05); // twips = 1/20 of a point
		
		// note dst is in twips, src is in pixels
		int dstX = 1640; // these values extracted from a debugging session
		int dstY = 1140;
		int srcX = 45;
		int srcY = 53;
		int srcW = 55;
		int srcH = 47;
		g2.drawImage(m_Image,
                          dstX,dstY,dstX+srcW*20,dstY+srcH*20,
                          srcX,srcY,srcX+srcW,srcY+srcH,null);
		g2.drawRect(dstX,dstY,srcW*20,srcH*20); // should coincide with drawn image
	}
	
	private Image m_Image;
}

// end of Test_SubImage.java
---------- END SOURCE ----------
###@###.### 11/4/04 23:22 GMT

Comments
EVALUATION This bug was introduced at some point in 1.5, most likely when the non-integer translation fixes were applied to the image pipeline. It is not present in the current 1.6 sources which have the new image transformation pipelines added. There were a number of places where some coordinate transformations were adjusted while the image transformation pipelines were being developed and it looks like one of these fixes this bug. Since those coordinate transform fixes are fairly isolated they should be easy to extract and apply to an update release of 1.5. ###@###.### 2004-11-06 04:08:43 GMT
06-11-2004