001: /*
002: * $RCSfile: MlibBandSelectOpImage.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:50 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.mlib;
013:
014: import java.awt.Rectangle;
015: import java.awt.image.DataBuffer;
016: import java.awt.image.Raster;
017: import java.awt.image.RenderedImage;
018: import java.awt.image.WritableRaster;
019: import java.util.Map;
020: import javax.media.jai.ImageLayout;
021: import javax.media.jai.OpImage;
022: import javax.media.jai.PlanarImage;
023: import javax.media.jai.PointOpImage;
024: import javax.media.jai.RasterFactory;
025: import com.sun.media.jai.util.ImageUtil;
026: import com.sun.media.jai.util.JDKWorkarounds;
027: import com.sun.medialib.mlib.*;
028:
029: /**
030: * An OpImage class that extracts (a) selected band(s) from an image.
031: *
032: */
033: final class MlibBandSelectOpImage extends PointOpImage {
034: /* Bitmask for the bands to be extracted. */
035: private int cmask = 0x00000000;
036:
037: /**
038: * Constructs an MlibBandSelectOpImage. The image dimensions are copied
039: * from the source image. The tile grid layout, SampleModel, and
040: * ColorModel may optionally be specified by an ImageLayout object.
041: *
042: * @param source a RenderedImage.
043: * @param layout an ImageLayout optionally containing the tile
044: * grid layout, SampleModel, and ColorModel, or null.
045: */
046: public MlibBandSelectOpImage(RenderedImage source, Map config,
047: ImageLayout layout, int[] bandIndices) {
048: super (source, layout, config, true);
049:
050: int numBands = bandIndices.length;
051: if (getSampleModel().getNumBands() != numBands) {
052: // Create a new SampleModel and ColorModel.
053: sampleModel = RasterFactory.createComponentSampleModel(
054: sampleModel, sampleModel.getDataType(), tileWidth,
055: tileHeight, numBands);
056:
057: if (colorModel != null
058: && !JDKWorkarounds.areCompatibleDataModels(
059: sampleModel, colorModel)) {
060: colorModel = ImageUtil.getCompatibleColorModel(
061: sampleModel, config);
062: }
063: }
064:
065: // Initialize the band selection bitmask.
066: int maxShift = source.getSampleModel().getNumBands() - 1;
067: for (int i = 0; i < bandIndices.length; i++) {
068: cmask |= 0x00000001 << (maxShift - bandIndices[i]);
069: }
070: }
071:
072: /**
073: * Extract the selected bands.
074: * The sources are cobbled.
075: *
076: * @param sources an array of sources, guarantee to provide all
077: * necessary source data for computing the rectangle.
078: * @param dest a tile that contains the rectangle to be computed.
079: * @param destRect the rectangle within this OpImage to be processed.
080: */
081: protected void computeRect(Raster[] sources, WritableRaster dest,
082: Rectangle destRect) {
083: Raster source = sources[0];
084: Rectangle srcRect = mapDestRect(destRect, 0);
085:
086: int formatTag = MediaLibAccessor.findCompatibleTag(sources,
087: dest);
088:
089: MediaLibAccessor srcAccessor = new MediaLibAccessor(source,
090: srcRect, formatTag);
091: MediaLibAccessor dstAccessor = new MediaLibAccessor(dest,
092: destRect, formatTag);
093:
094: mediaLibImage[] srcML = srcAccessor.getMediaLibImages();
095: mediaLibImage[] dstML = dstAccessor.getMediaLibImages();
096:
097: for (int i = 0; i < dstML.length; i++) {
098: Image.ChannelExtract(dstML[i], srcML[i], cmask);
099: }
100:
101: if (dstAccessor.isDataCopy()) {
102: dstAccessor.clampDataArrays();
103: dstAccessor.copyDataToRaster();
104: }
105: }
106: }
|