01: /*
02: * $RCSfile: MlibErrorDiffusionRIF.java,v $
03: *
04: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
05: *
06: * Use is subject to license terms.
07: *
08: * $Revision: 1.1 $
09: * $Date: 2005/02/11 04:55:56 $
10: * $State: Exp $
11: */
12: package com.sun.media.jai.mlib;
13:
14: import java.awt.RenderingHints;
15: import java.awt.image.DataBuffer;
16: import java.awt.image.RenderedImage;
17: import java.awt.image.SampleModel;
18: import java.awt.image.renderable.ParameterBlock;
19: import java.awt.image.renderable.RenderedImageFactory;
20: import javax.media.jai.ImageLayout;
21: import javax.media.jai.KernelJAI;
22: import javax.media.jai.LookupTableJAI;
23: import java.util.Map;
24: import com.sun.media.jai.opimage.RIFUtil;
25: import com.sun.media.jai.util.ImageUtil;
26:
27: /**
28: * A <code>RIF</code> supporting the "ErrorDiffusion" operation in the
29: * rendered image mode using mediaLib.
30: *
31: * @see javax.media.jai.operator.ErrorDiffusionDescriptor
32: * @see MlibErrorDiffusionOpImage
33: */
34: public class MlibErrorDiffusionRIF implements RenderedImageFactory {
35:
36: /** Constructor. */
37: public MlibErrorDiffusionRIF() {
38: }
39:
40: /**
41: * Creates a new instance of <code>MlibErrorDiffusionOpImage</code> in
42: * the rendered image mode.
43: *
44: * @param args The source image and lookup table.
45: * @param hints May contain rendering hints and destination image layout.
46: */
47: public RenderedImage create(ParameterBlock args,
48: RenderingHints hints) {
49: // Get source and parameters.
50: RenderedImage source = args.getRenderedSource(0);
51: LookupTableJAI colorMap = (LookupTableJAI) args
52: .getObjectParameter(0);
53: KernelJAI errorKernel = (KernelJAI) args.getObjectParameter(1);
54:
55: // Check colorMap compatibility.
56: if (colorMap.getNumBands() != 1 && colorMap.getNumBands() != 3) {
57: // 1 or 3 band colorMaps only.
58: return null;
59: } else if (colorMap.getDataType() != DataBuffer.TYPE_BYTE) {
60: // byte colorMaps only
61: return null;
62: }
63:
64: // Check source compatibility.
65: SampleModel sourceSM = source.getSampleModel();
66: if (sourceSM.getDataType() != DataBuffer.TYPE_BYTE) {
67: // byte source images only
68: return null;
69: } else if (sourceSM.getNumBands() != colorMap.getNumBands()) {
70: // band counts must match
71: return null;
72: }
73:
74: // Get ImageLayout from RenderingHints if any.
75: ImageLayout layoutHint = RIFUtil.getImageLayoutHint(hints);
76:
77: // Calculate the final ImageLayout.
78: ImageLayout layout = MlibErrorDiffusionOpImage.layoutHelper(
79: layoutHint, source, colorMap);
80:
81: // Check for source and destination compatibility. The ColorModel
82: // is suppressed in the second test because it will be an
83: // IndexColorModel which would cause the test to fail.
84: SampleModel destSM = layout.getSampleModel(null);
85: if (!MediaLibAccessor.isMediaLibCompatible(args)
86: || (!MediaLibAccessor
87: .isMediaLibCompatible(destSM, null) && !ImageUtil
88: .isBinary(destSM))) {
89: return null;
90: }
91:
92: return new MlibErrorDiffusionOpImage(source, hints, layout,
93: colorMap, errorKernel);
94: }
95: }
|