[Hippo-cms7-user] Improving image resizing quality (CMS image gallery)

Jannis Bloemendal j.bloemendal at onehippo.com
Tue Jun 15 11:27:54 CEST 2010


Nice :),

I have some more ideas for improvements... but you could also make
some tests with a gaussian kernal, instead of just averaging:

    /**
     * Make a Gaussian kernel.
     * @param sigma the inflection point of the gauss function
     * @return the matrix of factors for the convolution
     */
    public static Kernel makeGaussKernel(float sigma) {
        // there is only a need to calc 6 * sigma x 6 * sigma matrix
values, after the values are almost 0
        int radius = (int) Math.ceil(sigma*3);
        int size = radius * 2 + 1;
        float[] matrix = new float[size*size];
        float sigma22 = 2 * sigma * sigma;
        float sigmaPi2 = 2 * (float) Math.PI * sigma;
        float total = 0;
        int index = 0;
        for (int row = -radius; row <= radius; row++) {
            for (int col = -radius; col <= radius; col++) {
                float xDistance = row * row;
                float yDistance = col * col;
                matrix[index] = (float) Math.exp(-(xDistance +
yDistance) / sigma22) / sigmaPi2;
                total += matrix[index];
                index++;
            }
        }

        // fix gaps in the normal distribution matrix, so the sum is ~ 1
        for (int i = 0; i < matrix.length; i++) {
            matrix[i] /= total;
        }

        return new Kernel(size, size, matrix);
    }

On Mon, Jun 14, 2010 at 9:53 AM, Jasha Joachimsthal
<j.joachimsthal at onehippo.com> wrote:
> Cool :-)
> Another thing for the committers: most of the methods in the ImageUtils are
> private which makes it nearly impossible to extend the class without
> copy-pasting a lot of them. Can they change from private to protected?
> Jasha Joachimsthal
>
> j.joachimsthal at onehippo.com - jasha at apache.org
>
> www.onehippo.com
> Amsterdam - Hippo B.V. Oosteinde 11 1017 WT Amsterdam +31(0)20-5224466
> San Francisco - Hippo USA Inc. 185 H Street, suite B, Petaluma CA 94952 +1
> (707) 7734646
>
>
>
>
>
>
> On 11 June 2010 16:18, Arje Cahn <a.cahn at onehippo.com> wrote:
>>
>> Nice improvement, Kenan :)
>>
>> Could you try to turn that into a patch and send that to the
>> mailinglist? Then one of our committers can apply your patch to the
>> core codebase.
>>
>> Tx,
>>
>> Arjé Cahn
>>
>>
>>
>> On Fri, Jun 11, 2010 at 4:05 PM, Kenan Salic <k.salic at onehippo.com> wrote:
>> > Hi,
>> >
>> > Mostly pictures that are uploaded and are re-sized to a smaller
>> > thumbnail
>> > don't have nice quality. This offers a possible solution to improve the
>> > quality a bit.
>> >
>> > ------------------
>> > EXAMPLES:
>> >
>> > THUMBNAIL BEFORE
>> >
>> > THUMBNAIL AFTER
>> >
>> > CLICK HERE TO SEE MORE EXAMPLES (You have to click on some images to see
>> > the
>> > actual picture)
>> > -------------------
>> >
>> > The org.hippoecm.frontend.plugins.gallery.ImageUtils class resizes the
>> > image
>> > with method createThumbnail()
>> >
>> > What I have done, is made an extension of the ImageUtils and overriden
>> > the
>> > createThumbnail method by adding:
>> >
>> > if(resizeRatio < 0.6d){
>> >       originalImage = resizeTrick(originalImage);
>> >  }
>> >
>> > Before the scaling of the picture. This method is called upon if the
>> > resize
>> > ratio is smaller than 0.6 (works the best with smaller pictures).
>> >
>> > the resizeTrick method:
>> >
>> > private BufferedImage resizeTrick(BufferedImage image){
>> >        image = createCompatibleImage(image);
>> >        image = blurImage(image);
>> >        return image;
>> >    }
>> >
>> >    private BufferedImage createCompatibleImage(BufferedImage image){
>> >        GraphicsConfiguration gc =
>> > BufferedImageGraphicsConfig.getConfig(image);
>> >         int w = image.getWidth();
>> >         int h = image.getHeight();
>> >         BufferedImage result = gc.createCompatibleImage(w, h,
>> > Transparency.TRANSLUCENT);
>> >         Graphics2D g2 = result.createGraphics();
>> >         g2.drawRenderedImage(image, null);
>> >         g2.dispose();
>> >         return result;
>> >    }
>> >
>> >    private BufferedImage blurImage(BufferedImage image){
>> >        float ninth = 1.0f/9.0f;
>> >         float[] blurKernel = {
>> >                 ninth, ninth, ninth,
>> >                 ninth, ninth, ninth,
>> >                 ninth, ninth, ninth
>> >         };
>> >
>> >         Map<RenderingHints.Key, Object> map = new
>> > HashMap<RenderingHints.Key, Object>();
>> >
>> >
>> > map.put(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BICUBIC);
>> >
>> >
>> > map.put(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
>> >
>> >
>> > map.put(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
>> >         RenderingHints hints = new RenderingHints(map);
>> >         BufferedImageOp op = new ConvolveOp(new Kernel(3, 3,
>> > blurKernel),
>> > ConvolveOp.EDGE_NO_OP, hints);
>> >         return op.filter(image, null);
>> >    }
>> >
>> >
>> > What the snippet does, is blur the image by a ninth before scaling. This
>> > will make the re-sized image look a bit better.
>> >
>> > Also what I have done in the createScaledImage method, added some
>> > RenderHints before drawing the image:
>> >
>> >  Map<RenderingHints.Key, Object> map = new HashMap<RenderingHints.Key,
>> > Object>();
>> >
>> >  map.put(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BICUBIC);
>> >
>> >  map.put(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
>> >
>> >  map.put(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
>> >
>> > g2d.setRenderingHints(map);
>> >
>> > -------------------
>> > EXAMPLES:
>> >
>> > THUMBNAIL BEFORE
>> >
>> > THUMBNAIL IMPROVED
>> >
>> > CLICK HERE TO SEE MORE EXAMPLES (You have to click on some images to see
>> > the
>> > actual picture)
>> >
>> > -----------------
>> > Based upon this: blog
>> >
>> > Regards
>> >
>> > _______________________________________________
>> > Hippo-cms7-user mailing list and forums
>> > http://www.onehippo.org/cms7/support/forums.html
>> >
>> _______________________________________________
>> Hippo-cms7-user mailing list and forums
>> http://www.onehippo.org/cms7/support/forums.html
>
>
> _______________________________________________
> Hippo-cms7-user mailing list and forums
> http://www.onehippo.org/cms7/support/forums.html
>



More information about the Hippo-cms7-user mailing list