I use the following code for resizing images:
AreaAveragingScaleFilter theFilter =
new AreaAveragingScaleFilter(pNewWidth, pNewHeight);
FilteredImageSource theProducer =
new FilteredImageSource(theOrigImage.getSource(), theFilter);
Image image2 = Toolkit.getDefaultToolkit().createImage(theProducer);
BufferedImage theNewImage2 =
new BufferedImage( pNewWidth, pNewHeight, theOrigImage.getType() );
Graphics2D g2 = theNewImage2.createGraphics();
g2.drawImage(image2, 0, 0, null);
g2.dispose();
The problem with it is that it occassionaly takes a very long time to
resize an image. The odd thing is that if I take an image right out of
my camera (a 3MB image) it resizes it in 5 seconds. If I edit that same
image in Photoshop with various settings and save it (resulting in a
1.5MB image), it takes 3 minutes to resize it. Also, this does not
happen consistently. A lot of the time, the edited images resize very
quickly, but a few of them are taking 3 minutes.
Any ideas why an edited image would take so much longer to resize even
though it's half the size of the original?
BTW, the reason I use the above code is because it results in the
highest quality resize. I've tried a number of other ways and none is
as good as the above code.
Andrey Kuznetsov - 05 Feb 2005 04:58 GMT
> AreaAveragingScaleFilter theFilter =
> new AreaAveragingScaleFilter(pNewWidth, pNewHeight);
[quoted text clipped - 11 lines]
> g2.drawImage(image2, 0, 0, null);
> g2.dispose();
If you speak about downscaling then try Imagero Reader.
I don't know which file types you want to read.
For jpeg files Imagero Reader is a bit slower as standard java,
because sun uses native libraries (libjpeg).
However, you should know, that if you resize image with java it is possible
that image will be reread from disc.
With Imagero Reader you can create multiple scalings very quikly.
And quality is also very good - Photoshop is slightly better, but to see
that
you should look long time at 200% zoom.
If scale factor is less then 1/8, then you can also use subsampling (JPEG
only),
this speed up read process because only DC-coeffs are decoded.

Signature
Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
Chris McMahon - 05 Feb 2005 19:11 GMT
>>AreaAveragingScaleFilter theFilter =
>> new AreaAveragingScaleFilter(pNewWidth, pNewHeight);
[quoted text clipped - 25 lines]
> only),
> this speed up read process because only DC-coeffs are decoded.
Any idea what could make the resizing take so long on certain edited
pictures using the code above?
Andrey Kuznetsov - 05 Feb 2005 20:16 GMT
> Any idea what could make the resizing take so long on certain edited
> pictures using the code above?
I need some more information:
Is it always so bad with edited pictures or sometimes?
With all edited pictures or with some of them?
What shows task manager while that long processing?

Signature
Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
Chris McMahon - 05 Feb 2005 20:54 GMT
>>Any idea what could make the resizing take so long on certain edited
>>pictures using the code above?
[quoted text clipped - 4 lines]
> With all edited pictures or with some of them?
> What shows task manager while that long processing?
Only sometimes.
Only some edited pictures.
CPU is maxed out while resizing.
It only happens with the AreaAveragingScaleFilter or using
getScaledInstance().
Andrey Kuznetsov - 05 Feb 2005 22:08 GMT
> Only sometimes.
> Only some edited pictures.
> CPU is maxed out while resizing.
>
> It only happens with the AreaAveragingScaleFilter or using
> getScaledInstance().
getScaledInstance can reload image from disc.
It looks like low memory problem.
Try to profile your memory footprint.
(YourKit profiler is pretty easy to use).
Try to assign some more memory to your program.
Also important thing to watch in task manager is page errors
If it is too big (i.e. half million an more) than you have a big problem
Buy more RAM... or use Imagero Reader - if you need just one
scale of your image then only needed row amount are held in memory
(for example if you scale 10:1 then in memory are always only 10 rows).

Signature
Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
Chris McMahon - 07 Feb 2005 02:29 GMT
I'm using the following code with Imagero:
JpegReader decoder = new JpegReader( pSrcFile, 1 );
final ImageProducerAdapter producer = (ImageProducerAdapter)
decoder.getProducer(0);
producer.setScaleX(
(float)pNewWidth / (float)theOrigImage.getWidth() ) ;
producer.setScaleY(
(float)pNewHeight / (float)theOrigImage.getHeight() ) ;
Image image = Toolkit.getDefaultToolkit().createImage(producer);
ImageIcon icon = new ImageIcon(image);
BufferedImage bi =
new BufferedImage(
icon.getIconWidth(), icon.getIconHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics g1 = bi.getGraphics();
g1.drawImage(image, 0, 0, null);
g1.dispose();
However it doesn't do a great job with anti-aliasing (not as good as the
original code I posted in this thread). Is the above code the best code
to use? Is there a parameter I can use to increase it's anti-aliasing
filtering?
Andrey Kuznetsov - 07 Feb 2005 09:49 GMT
> However it doesn't do a great job with anti-aliasing (not as good as the
> original code I posted in this thread). Is the above code the best code
> to use?
this is really strange...
I tested it many times, and resizing quality was very good.
How do you display it? After saving to jpeg?
> Is there a parameter I can use to increase it's anti-aliasing filtering?
no

Signature
Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
Chris McMahon - 08 Feb 2005 01:53 GMT
>>However it doesn't do a great job with anti-aliasing (not as good as the
>>original code I posted in this thread). Is the above code the best code
[quoted text clipped - 3 lines]
> I tested it many times, and resizing quality was very good.
> How do you display it? After saving to jpeg?
I can send you the images for comparison if you'd like. The imagero is
a little sharper than my original code, but it has slightly more
artifacts and jaggies. I guess it's a matter of opinion which is
better. You have to show the image at at least 100% to see it. I've
viewed the images in Photoshop Elements 3.0.
BTW, do you know if there is a place to get the SunGraphics2D.class
source code? It's the drawImage() that's taking all the time?
Andrey Kuznetsov - 08 Feb 2005 07:56 GMT
> I can send you the images for comparison if you'd like. The imagero is a
> little sharper than my original code, but it has slightly more artifacts
> and jaggies. I guess it's a matter of opinion which is better. You have
> to show the image at at least 100% to see it. I've viewed the images in
> Photoshop Elements 3.0.
ok do i
info at imagero dot com
or (without typing) just go here
http://reader.imagero.com/mailto.php
> BTW, do you know if there is a place to get the SunGraphics2D.class source
> code? It's the drawImage() that's taking all the time?
this is good idea! In which color space you saving your images?
If it is not sRGB then java can be slooooow.
The worst case I think is CMYK - every paint image is converted to sRGB ;-(

Signature
Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities