001: /*
002: * $RCSfile: MlibLookupOpImage.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:58 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.mlib;
013:
014: import java.awt.Rectangle;
015: import java.awt.image.ComponentSampleModel;
016: import java.awt.image.DataBuffer;
017: import java.awt.image.Raster;
018: import java.awt.image.RenderedImage;
019: import java.awt.image.SampleModel;
020: import java.awt.image.WritableRaster;
021: import java.util.Map;
022: import javax.media.jai.ImageLayout;
023: import javax.media.jai.LookupTableJAI;
024: import javax.media.jai.PlanarImage;
025: import javax.media.jai.PointOpImage;
026: import com.sun.media.jai.util.ImageUtil;
027: import com.sun.media.jai.util.JDKWorkarounds;
028: import com.sun.medialib.mlib.*;
029:
030: final class MlibLookupOpImage extends PointOpImage {
031: private LookupTableJAI table;
032:
033: public MlibLookupOpImage(RenderedImage source, Map config,
034: ImageLayout layout, LookupTableJAI table) {
035: super (source, layout, config, true);
036:
037: this .table = table;
038:
039: SampleModel sm = source.getSampleModel(); // source sample model
040:
041: if (sampleModel.getTransferType() != table.getDataType()
042: || sampleModel.getNumBands() != table
043: .getDestNumBands(sm.getNumBands())) {
044: /*
045: * The current SampleModel is not suitable for the supplied
046: * source and lookup table. Create a suitable SampleModel
047: * and ColorModel for the destination image.
048: */
049: sampleModel = table.getDestSampleModel(sm, tileWidth,
050: tileHeight);
051: if (colorModel != null
052: && !JDKWorkarounds.areCompatibleDataModels(
053: sampleModel, colorModel)) {
054: colorModel = ImageUtil.getCompatibleColorModel(
055: sampleModel, config);
056: }
057: }
058: // Set flag to permit in-place operation.
059: permitInPlaceOperation();
060: }
061:
062: protected void computeRect(Raster[] sources, WritableRaster dest,
063: Rectangle destRect) {
064: Raster source = sources[0];
065: Rectangle srcRect = mapDestRect(destRect, 0);
066:
067: int srcTag = MediaLibAccessor.findCompatibleTag(null, source);
068: int dstTag = MediaLibAccessor.findCompatibleTag(null, dest);
069:
070: SampleModel sm = source.getSampleModel();
071: if (sm.getNumBands() > 1) {
072: int srcCopy = srcTag & MediaLibAccessor.COPY_MASK;
073: int dstCopy = dstTag & MediaLibAccessor.COPY_MASK;
074:
075: int srcDtype = srcTag & MediaLibAccessor.DATATYPE_MASK;
076: int dstDtype = dstTag & MediaLibAccessor.DATATYPE_MASK;
077:
078: if (srcCopy == MediaLibAccessor.UNCOPIED
079: && dstCopy == MediaLibAccessor.UNCOPIED
080: && MediaLibAccessor.isPixelSequential(sm)
081: && MediaLibAccessor.isPixelSequential(sampleModel)
082: && MediaLibAccessor.hasMatchingBandOffsets(
083: (ComponentSampleModel) sm,
084: (ComponentSampleModel) sampleModel)) {
085: } else {
086: srcTag = srcDtype | MediaLibAccessor.COPIED;
087: dstTag = dstDtype | MediaLibAccessor.COPIED;
088: }
089: }
090:
091: MediaLibAccessor src = new MediaLibAccessor(source, srcRect,
092: srcTag);
093: MediaLibAccessor dst = new MediaLibAccessor(dest, destRect,
094: dstTag);
095:
096: mediaLibImage[] srcMLI = src.getMediaLibImages();
097: mediaLibImage[] dstMLI = dst.getMediaLibImages();
098:
099: if (srcMLI.length < dstMLI.length) {
100: mediaLibImage srcMLI0 = srcMLI[0];
101: srcMLI = new mediaLibImage[dstMLI.length];
102:
103: for (int i = 0; i < dstMLI.length; i++) {
104: srcMLI[i] = srcMLI0;
105: }
106: }
107:
108: int[] bandOffsets = dst.getBandOffsets();
109: Object table = getTableData(bandOffsets);
110: int[] offsets = getTableOffsets(bandOffsets);
111:
112: for (int i = 0; i < dstMLI.length; i++) {
113: Image.LookUp2(dstMLI[i], srcMLI[i], table, offsets);
114: }
115:
116: if (dst.isDataCopy()) {
117: dst.copyDataToRaster();
118: }
119: }
120:
121: private Object getTableData(int[] bandOffsets) {
122: int tbands = table.getNumBands();
123: int dbands = sampleModel.getNumBands();
124: Object data = null;
125:
126: switch (table.getDataType()) {
127: case DataBuffer.TYPE_BYTE:
128: byte[][] bdata = new byte[dbands][];
129: if (tbands < dbands) {
130: for (int i = 0; i < dbands; i++) {
131: bdata[i] = table.getByteData(0);
132: }
133: } else {
134: for (int i = 0; i < dbands; i++) {
135: bdata[i] = table.getByteData(bandOffsets[i]);
136: }
137: }
138: data = bdata;
139: break;
140: case DataBuffer.TYPE_USHORT:
141: case DataBuffer.TYPE_SHORT:
142: short[][] sdata = new short[dbands][];
143: if (tbands < dbands) {
144: for (int i = 0; i < dbands; i++) {
145: sdata[i] = table.getShortData(0);
146: }
147: } else {
148: for (int i = 0; i < dbands; i++) {
149: sdata[i] = table.getShortData(bandOffsets[i]);
150: }
151: }
152: data = sdata;
153: break;
154: case DataBuffer.TYPE_INT:
155: int[][] idata = new int[dbands][];
156: if (tbands < dbands) {
157: for (int i = 0; i < dbands; i++) {
158: idata[i] = table.getIntData(0);
159: }
160: } else {
161: for (int i = 0; i < dbands; i++) {
162: idata[i] = table.getIntData(bandOffsets[i]);
163: }
164: }
165: data = idata;
166: break;
167: case DataBuffer.TYPE_FLOAT:
168: float[][] fdata = new float[dbands][];
169: if (tbands < dbands) {
170: for (int i = 0; i < dbands; i++) {
171: fdata[i] = table.getFloatData(0);
172: }
173: } else {
174: for (int i = 0; i < dbands; i++) {
175: fdata[i] = table.getFloatData(bandOffsets[i]);
176: }
177: }
178: data = fdata;
179: break;
180: case DataBuffer.TYPE_DOUBLE:
181: double[][] ddata = new double[dbands][];
182: if (tbands < dbands) {
183: for (int i = 0; i < dbands; i++) {
184: ddata[i] = table.getDoubleData(0);
185: }
186: } else {
187: for (int i = 0; i < dbands; i++) {
188: ddata[i] = table.getDoubleData(bandOffsets[i]);
189: }
190: }
191: data = ddata;
192: break;
193: }
194:
195: return data;
196: }
197:
198: private int[] getTableOffsets(int[] bandOffsets) {
199: int tbands = table.getNumBands();
200: int dbands = sampleModel.getNumBands();
201: int[] offsets = new int[dbands];
202:
203: if (tbands < dbands) {
204: for (int i = 0; i < dbands; i++) {
205: offsets[i] = table.getOffset(0);
206: }
207: } else {
208: for (int i = 0; i < dbands; i++) {
209: offsets[i] = table.getOffset(bandOffsets[i]);
210: }
211: }
212:
213: return offsets;
214: }
215: }
|