1 /*
2  * $RCSfile: ColorQuantizerRIF.java,v $
3  *
4  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
5  *
6  * Use is subject to license terms.
7  *
8  * $Revision: 1.1 $
9  * $Date: 2005/02/11 04:56:17 $
10  * $State: Exp $
11  */package com.lightcrafts.media.jai.opimage;
12 import java.awt.RenderingHints;
13 import java.awt.image.RenderedImage;
14 import java.awt.image.SampleModel;
15 import java.awt.image.DataBuffer;
16 import java.awt.image.renderable.RenderedImageFactory;
17 import java.awt.image.renderable.ParameterBlock;
18 import java.util.Map;
19 import com.lightcrafts.mediax.jai.ImageLayout;
20 import com.lightcrafts.mediax.jai.JAI;
21 import com.lightcrafts.mediax.jai.ROI;
22 import com.lightcrafts.mediax.jai.operator.ColorQuantizerDescriptor;
23 import com.lightcrafts.mediax.jai.operator.ColorQuantizerType;
24 
25 /**
26  * <p> Class implementing the RIF interface for the ColorQuantizer
27  * operator.  An instance of this class should be registered with the
28  * OperationRegistry with operation name "ColorQuantizer."
29  */
30 public class ColorQuantizerRIF implements RenderedImageFactory {
31 
32     /** <p> Default constructor (there is no input). */
ColorQuantizerRIF()33     public ColorQuantizerRIF() {}
34 
35     /**
36      * <p> Creates a new instance of ColorQuantizerOpImage in the
37      * rendered layer.  This method satisfies the implementation of RIF.
38      *
39      * @param paramBlock  The source image, the color quantization algorithm
40      *                    name, the maximum number of colors, the
41      *                    parameter for training (the histogram size for
42      *                    median-cut, the cycle for neuquant, and maximum tree
43      *                    size for oct-tree), and the ROI.
44      * @param renderHints RenderingHints.
45      */
create(ParameterBlock paramBlock, RenderingHints renderHints)46     public RenderedImage create(ParameterBlock paramBlock,
47                                 RenderingHints renderHints) {
48         RenderedImage source = paramBlock.getRenderedSource(0);
49 
50         ImageLayout layout = renderHints == null ? null :
51             (ImageLayout)renderHints.get(JAI.KEY_IMAGE_LAYOUT);
52 
53         ColorQuantizerType algorithm =
54             (ColorQuantizerType)paramBlock.getObjectParameter(0);
55         int maxColorNum = paramBlock.getIntParameter(1);
56         int upperBound = paramBlock.getIntParameter(2);
57         ROI roi= (ROI)paramBlock.getObjectParameter(3);
58         int xPeriod = paramBlock.getIntParameter(4);
59         int yPeriod = paramBlock.getIntParameter(5);
60 
61         // check if 3-band byte-type image
62 	SampleModel sm = source.getSampleModel();
63         if (sm.getNumBands() != 3  && sm.getDataType() == DataBuffer.TYPE_BYTE)
64             throw new IllegalArgumentException("ColorQuantizerRIF0");
65 
66         if (algorithm.equals(ColorQuantizerDescriptor.NEUQUANT))
67             return new NeuQuantOpImage(source, (Map)renderHints, layout,
68                                         maxColorNum, upperBound, roi,
69                                         xPeriod, yPeriod);
70 
71         if (algorithm.equals(ColorQuantizerDescriptor.OCTTREE))
72             return new OctTreeOpImage(source, (Map)renderHints, layout,
73                                         maxColorNum, upperBound, roi,
74                                         xPeriod, yPeriod);
75         else
76             return new MedianCutOpImage(source, (Map)renderHints, layout,
77                                         maxColorNum, upperBound, roi,
78                                         xPeriod, yPeriod);
79 
80     } // create
81 
82 } // ColorQuantizerRIF
83