2009/04/10

How to avoid borders when using UnsharpMask operation with JAI

Summary

When using unsharp mask with JAI (Java Advanced Imaging), a resulting image may have borders. This article explains how to avoid it.

How to use unsharp mask with JAI

The following is the sample code. The key is to use RenderingHints to specify a BorderExtender. Without this, borders may appear.
UnsharpTest.java
public class UnsharpTest {
    public static void main(String[] args) throws IOException {
        
        // load image
        
        RenderedOp image = JAI.create("stream", new FileSeekableStream(new File("test.jpg")));

        // unsharp image
        
        ParameterBlock unsharp_params = new ParameterBlock();
        unsharp_params.addSource(image);
        unsharp_params.add(null); // convolution kernel
        unsharp_params.add(1.0F); // gain factor

        RenderingHints unsharp_hints = new RenderingHints(JAI.KEY_BORDER_EXTENDER,
            BorderExtender.createInstance(BorderExtender.BORDER_COPY));
        
        image = JAI.create("UnsharpMask", unsharp_params, unsharp_hints);
        
        // save image
        
        JPEGEncodeParam encParam = new JPEGEncodeParam();
        encParam.setQuality(0.80F);
        
        ParameterBlock filestore_params = new ParameterBlock();
        filestore_params.addSource(image);
        filestore_params.add("image_unsharpened.jpg"); // path of the file to write to
        filestore_params.add("JPEG"); // format of the file
        filestore_params.add(encParam); // encoding parameters
        
        JAI.create("filestore", filestore_params);
    }
posted by apptaro at 21:07 | Comment(0) | TrackBack(0) | Java/Servlet
Comments
Write Comments
Name: [Required]

Email:

HP:

Comments: [Required]

Code: [Required]


*Input characters in the image
Trackback URL
http://blog.seesaa.jp/tb/117238110
Trackback without referring link will not be accepted.

Trackbacks
×

この広告は1年以上新しい記事の投稿がないブログに表示されております。