001: /*
002: * $RCSfile: MlibUtils.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.2 $
009: * $Date: 2005/12/15 18:35:47 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.mlib;
013:
014: import java.awt.image.ColorModel;
015: import com.sun.medialib.mlib.Image;
016: import com.sun.medialib.mlib.mediaLibImage;
017:
018: final class MlibUtils {
019: /**
020: * If constants array is less than numBands it is replaced
021: * by an array of length numBands filled with constants[0].
022: * Otherwise the input array is cloned.
023: */
024: static final int[] initConstants(int[] constants, int numBands) {
025: int[] c = null;
026: if (constants.length < numBands) {
027: c = new int[numBands];
028: for (int i = 0; i < numBands; i++) {
029: c[i] = constants[0];
030: }
031: } else {
032: c = (int[]) constants.clone();
033: }
034:
035: return c;
036: }
037:
038: /**
039: * If constants array is less than numBands it is replaced
040: * by an array of length numBands filled with constants[0].
041: * Otherwise the input array is cloned.
042: */
043: static final double[] initConstants(double[] constants, int numBands) {
044: double[] c = null;
045: if (constants.length < numBands) {
046: c = new double[numBands];
047: for (int i = 0; i < numBands; i++) {
048: c[i] = constants[0];
049: }
050: } else {
051: c = (double[]) constants.clone();
052: }
053:
054: return c;
055: }
056:
057: /**
058: * If the color depth in bits of any band of the image does not
059: * match the full bit depth as determined from the image type then
060: * clamp the image to the unnomarlized range represented by the
061: * ColorModel.
062: */
063: static void clampImage(mediaLibImage image, ColorModel colorModel) {
064: if (image == null) {
065: throw new IllegalArgumentException("image == null!");
066: }
067:
068: if (colorModel != null) {
069: // Set the full bit depth as a function of image type.
070: int fullDepth = 0;
071: switch (image.getType()) {
072: case mediaLibImage.MLIB_BYTE:
073: fullDepth = 8;
074: break;
075: case mediaLibImage.MLIB_INT:
076: fullDepth = 32;
077: break;
078: default: // USHORT and SHORT
079: fullDepth = 16;
080: }
081:
082: // Set the low and high thresholds and the thresholding flag.
083: int[] numBits = colorModel.getComponentSize();
084: int[] high = new int[numBits.length];
085: int[] low = new int[numBits.length]; // zero by default
086: boolean applyThreshold = false;
087: for (int j = 0; j < numBits.length; j++) {
088: high[j] = (1 << numBits[j]) - 1;
089: if (numBits[j] != fullDepth) {
090: applyThreshold = true;
091: }
092: }
093:
094: // Apply threshold if color depth of any band is not full depth.
095: if (applyThreshold) {
096: Image.Thresh4(image, high, low, high, low);
097: }
098: }
099: }
100: }
|