001: /*
002: * $RCSfile: MlibErodeRIF.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.1 $
009: * $Date: 2005/02/11 04:55:55 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.mlib;
013:
014: import java.awt.RenderingHints;
015: import java.awt.image.RenderedImage;
016: import java.awt.image.renderable.ParameterBlock;
017: import java.awt.image.renderable.RenderedImageFactory;
018: import javax.media.jai.BorderExtender;
019: import javax.media.jai.ImageLayout;
020: import javax.media.jai.KernelJAI;
021: import java.util.Map;
022: import com.sun.media.jai.opimage.RIFUtil;
023:
024: /**
025: * A <code>RIF</code> supporting the "Erode" operation in the
026: * rendered image mode using MediaLib.
027: *
028: * @see javax.media.jai.operator.ErodeDescriptor
029: * @see MlibErodeOpImage
030: */
031: public class MlibErodeRIF implements RenderedImageFactory {
032:
033: /** Constructor. */
034: public MlibErodeRIF() {
035: }
036:
037: /**
038: * Creates a new instance of <code>MlibErodeOpImage</code> in
039: * the rendered image mode.
040: *
041: * @param args The source image and erosion kernel.
042: * @param hints May contain rendering hints and destination image layout.
043: */
044: public RenderedImage create(ParameterBlock args,
045: RenderingHints hints) {
046: /* Get ImageLayout and TileCache from RenderingHints. */
047: ImageLayout layout = RIFUtil.getImageLayoutHint(hints);
048:
049: boolean isBinary = false;
050: if (!MediaLibAccessor.isMediaLibCompatible(args, layout)
051: || !MediaLibAccessor.hasSameNumBands(args, layout)) {
052: if (!MediaLibAccessor.isMediaLibBinaryCompatible(args,
053: layout)) {
054: return null;
055: }
056: isBinary = true;
057: }
058:
059: /* Get BorderExtender from hints if any. */
060: BorderExtender extender = RIFUtil.getBorderExtenderHint(hints);
061:
062: RenderedImage source = args.getRenderedSource(0);
063:
064: KernelJAI unRotatedKernel = (KernelJAI) args
065: .getObjectParameter(0);
066: KernelJAI kJAI = unRotatedKernel.getRotatedKernel();
067:
068: int kWidth = kJAI.getWidth();
069: int kHeight = kJAI.getHeight();
070: int xOri = kJAI.getXOrigin();
071: int yOri = kJAI.getYOrigin();
072: int numB = source.getSampleModel().getNumBands();
073:
074: /* mediaLib does not handle kernels with either dimension < 2. */
075:
076: if (xOri != 1 || yOri != 1 || kWidth != 3 || kHeight != 3
077: || numB != 1) {
078: return null;
079: }
080:
081: // check for plus and square type of kernel
082:
083: float[] kdata = kJAI.getKernelData();
084:
085: if (isBinary && isKernel3Square1(kdata) || !isBinary
086: && isKernel3Square0(kdata)) {
087:
088: return new MlibErode3SquareOpImage(source, extender, hints,
089: layout);
090:
091: }
092:
093: if (isBinary && isKernel3Plus1(kdata)) {
094: // plus shape
095:
096: return new MlibErode3PlusOpImage(source, extender, hints,
097: layout);
098: }
099:
100: return null;
101:
102: }
103:
104: // check to see if a 3x3 kernel has 1s at the plus positions and 0s elsewhere
105: private boolean isKernel3Plus1(float[] kdata) {
106:
107: return (kdata[0] == 0.0F && kdata[1] == 1.0F
108: && kdata[2] == 0.0F && kdata[3] == 1.0F
109: && kdata[4] == 1.0F && kdata[5] == 1.0F
110: && kdata[6] == 0.0F && kdata[7] == 1.0F && kdata[8] == 0.0F);
111: }
112:
113: // check to see if a 3x3 kernel has 1s at the plus positions
114: private boolean isKernel3Square0(float[] kdata) {
115:
116: return (kdata[0] == 0.0F && kdata[1] == 0.0F
117: && kdata[2] == 0.0F && kdata[3] == 0.0F
118: && kdata[4] == 0.0F && kdata[5] == 0.0F
119: && kdata[6] == 0.0F && kdata[7] == 0.0F && kdata[8] == 0.0F);
120: }
121:
122: // check to see if a 3x3 kernel has 1s at the plus positions
123: private boolean isKernel3Square1(float[] kdata) {
124:
125: return (kdata[0] == 1.0F && kdata[1] == 1.0F
126: && kdata[2] == 1.0F && kdata[3] == 1.0F
127: && kdata[4] == 1.0F && kdata[5] == 1.0F
128: && kdata[6] == 1.0F && kdata[7] == 1.0F && kdata[8] == 1.0F);
129: }
130: }
|