001: /*
002: * $RCSfile: MlibMeanOpImage.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:59 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.mlib;
013:
014: import java.awt.Rectangle;
015: import java.awt.image.Raster;
016: import java.awt.image.WritableRaster;
017: import java.awt.image.RenderedImage;
018: import java.awt.image.SampleModel;
019: import java.awt.image.DataBuffer;
020: import java.awt.image.PixelInterleavedSampleModel;
021: import javax.media.jai.ImageLayout;
022: import javax.media.jai.OpImage;
023: import javax.media.jai.PlanarImage;
024: import javax.media.jai.ROI;
025: import javax.media.jai.StatisticsOpImage;
026: import com.sun.media.jai.opimage.MeanOpImage;
027: import com.sun.medialib.mlib.*;
028:
029: // import com.sun.media.jai.test.OpImageTester;
030:
031: /**
032: * An OpImage that performs the Mean operation on an image through mediaLib.
033: *
034: */
035: final class MlibMeanOpImage extends MeanOpImage {
036:
037: /**
038: * Constructs an MlibMeanOpImage. 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 The source image.
043: */
044: public MlibMeanOpImage(RenderedImage source, ROI roi, int xStart,
045: int yStart, int xPeriod, int yPeriod) {
046:
047: super (source, roi, xStart, yStart, xPeriod, yPeriod);
048: }
049:
050: protected void accumulateStatistics(String name, Raster source,
051: Object stats) {
052: // Get image and band count.
053: PlanarImage sourceImage = getSourceImage(0);
054: int numBands = sourceImage.getSampleModel().getNumBands();
055:
056: // Determine the format tag and create an accessor.
057: int formatTag = MediaLibAccessor
058: .findCompatibleTag(null, source);
059: MediaLibAccessor srcAccessor = new MediaLibAccessor(source,
060: source.getBounds(), formatTag);
061:
062: // Get the mediaLib image.
063: mediaLibImage[] srcML = srcAccessor.getMediaLibImages();
064:
065: // NOTE: currently srcML.length always equals 1
066:
067: double[] dmean = new double[numBands];
068:
069: switch (srcAccessor.getDataType()) {
070: case DataBuffer.TYPE_BYTE:
071: case DataBuffer.TYPE_USHORT:
072: case DataBuffer.TYPE_SHORT:
073: case DataBuffer.TYPE_INT:
074: for (int i = 0; i < srcML.length; i++) {
075: Image.Mean(dmean, srcML[i]);
076: }
077: break;
078:
079: case DataBuffer.TYPE_FLOAT:
080: case DataBuffer.TYPE_DOUBLE:
081: for (int i = 0; i < srcML.length; i++) {
082: Image.Mean_Fp(dmean, srcML[i]);
083: }
084: break;
085:
086: default:
087: throw new RuntimeException(JaiI18N.getString("Generic2"));
088: }
089:
090: dmean = srcAccessor.getDoubleParameters(0, dmean);
091:
092: // Update the mean.
093: double[] mean = (double[]) stats;
094: double weight = (double) (source.getWidth() * source
095: .getHeight())
096: / (double) (width * height);
097: for (int i = 0; i < numBands; i++) {
098: mean[i] += dmean[i] * weight;
099: }
100: }
101: }
|