Skip to main content

Image resizing using SubsampleAverage

9 replies [Last post]
pingw
Offline
Joined: 2010-02-02

Image resizing using JAI

This resizing algorithm works perfect, BUT he cuts 1-2 pixels from bottom edge and 1-2 pixels from right edge AND adds artifacts to the left and top edges. What I can do with artefacts and cutted pixels, how I can take them back???

We have next algorithm of resizing:

<br />
        RenderedOp image;<br />
        try {<br />
            SeekableStream s = SeekableStream.wrapInputStream(is, true);<br />
            image = JAI.create("stream", s);<br />
            ((OpImage) image.getRendering()).setTileCache(null);</p>
<p>            double resizeFactor = getResizeFactor(width, height, image<br />
                    .getWidth(), image.getHeight());<br />
            if (resizeFactor < 1) {<br />
                image = JAI.create("SubsampleAverage", image, resizeFactor,<br />
                        resizeFactor, QUALITY_HINTS);<br />
            } else if (resizeFactor > 1) {<br />
                image = ScaleDescriptor.create(image, (float) resizeFactor,<br />
                        (float) resizeFactor, 0.0f, 0.0f, Interpolation<br />
                                .getInstance(Interpolation.INTERP_BICUBIC),<br />
                        null);<br />
            }</p>
<p>            try {<br />
                ByteArrayOutputStream os = new ByteArrayOutputStream();<br />
                try {<br />
                    ImageIO.write(image, "png", os);<br />
                    return os.toByteArray();<br />
                } finally {<br />
                    os.close();<br />
                }<br />
            } catch (IOException e) {<br />
                throw new RuntimeException(e);<br />
            }<br />
        } finally {<br />
            is.close();<br />
        }<br />

Source without resizing: http://www.servit.ch/sites/all/themes/servit_oss/logo.png
Resize example #1: http://208.109.126.120:7080/gdesk-web/thumbnail/1123_148x148.png (you may see cutted bottom edge)
Resize example #2: http://208.109.126.120:7080/gdesk-web/thumbnail/1123_30x30.png (you may see cutted bottom and right edges and artifacts at left and top)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
imagero
Offline
Joined: 2003-11-18

You could just cut bottom and right lines...

pingw
Offline
Joined: 2010-02-02

I couldn't because:
1) user can upload image at any size
2) black lines not always apear, I mean that if we resize 300x250 to 148x148 black lines will apear and if 431x200 to 148x148 black line will not apear.
And how I should to know when I should cut image?

imagero
Offline
Joined: 2003-11-18

What is about simple pixel values checking?

Just go through bottom/right line pixels if they all are black (and bottom/right line of original image is not black), you crop image.

Or you may use Image Scaling Framework, which is free (LGPL). You can choose there between 9 scaling algorithms. ISF is working however only with BufferedImage objects. Using JAI for small images is overkill.

Message was edited by: imagero

pingw
Offline
Joined: 2010-02-02

I have changed my code which makes image convertation and now I have other problem.
I have black line at bottom of the image after resizing. Did anyone knows how to fix it.
You find image with black edge here http://208.109.126.120:7080/gdesk-web/thumbnail/1126_148x148.png

I need any help.

Converting to PNG:
[code]
PNGEncodeParam.RGB pngEncodeParam = new PNGEncodeParam.RGB();
pngEncodeParam.setBackgroundRGB(new int[] { 255, 255, 255 });
pngEncodeParam.setTransparentRGB(new int[] { 255, 255, 255 });

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageEncoder encoder = ImageCodec.createImageEncoder("PNG",
outputStream, pngEncodeParam);
PlanarImage planarImage = scaledImage.getNewRendering();
encoder.encode(planarImage);

[/code]

Anonymous

There is a known problem with SubSampleAverage and black lines when using
MediaLib acceleration.

Try
System.setProperty("com.sun.media.jai.disableMediaLib", "true");
to disable MediaLib acceleration.

Regards,
Helmut

pingw
Offline
Joined: 2010-02-02

Thank you for your answer and Yes you were right disableMediaLib=true helps.
It removes black lines BUT it kills quality. I created small table to show. In which you can find big difference between accelerated resize and not accelerated, especially at small images. I want quality without black lines. http://208.109.126.120:7080/1.html

Rafael Santos

Just a suggestion, since it appears you need several different sizes of
the image: why not store the image in SVG and render it as required
and/or pre-render it in the number of sizes you need and clean up
(manually or programatically!) the ones with the line? Or why not do it
without JAI?

Rafael

> Thank you for your answer and Yes you were right disableMediaLib=true helps.
> It removes black lines BUT it kills quality. I created small table to show. In which you can find big difference between accelerated resize and not accelerated, especially at small images. I want quality without black lines. http://208.109.126.120:7080/1.html
> [Message sent by forum member 'pingw' (krivochurov@enterra-inc.com)]
>
> http://forums.java.net/jive/thread.jspa?messageID=386939
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> For additional commands, e-mail: interest-help@jai.dev.java.net
>
>
>

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

pingw
Offline
Joined: 2010-02-02

Image resizing mechanism is a part of system. I want to say that it's a production system and before make fundamentally changes I want to be sure that it's can be implemented using existing algorithm. And if I will sure in it I will try to change algorithm.

We talking about company logo, which uploads by the user, and I don't know good algorithms which convert image from every format to svg.

pingw
Offline
Joined: 2010-02-02