The Source for Java Technology Collaboration
User: Password:



Start New Message Delete Post a Reply

Article: 
 The Perils of Image.getScaledInstance()
Subject:  Infinite loop
Date:  2007-06-18 07:56:35
From:  prenon


Very good and helpful article.
I found a minor bug in the getInstanceMethod() when it is called with higherQuality set to true with an image smaller or equal in size to the target size. When this happens, the method goes into a never ending loop.
There is a simple fix: force w and/or h to their respective target value when the situation is detected.


...
if (higherQuality) {
// Use multi-step technique: start with original size, then
// scale down in multiple passes with drawImage()
// until the target size is reached
w = img.getWidth();
if (w < targetWidth) {
w = targetWidth;
}
if (h < targetHeight) {
h = targetHeight;
}
h = img.getHeight();
} else {
....

 Feed java.net RSS Feeds