1 /*
2  * $RCSfile: MlibUtils.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.2 $
9  * $Date: 2005/12/15 18:35:47 $
10  * $State: Exp $
11  */
12 package com.lightcrafts.media.jai.mlib;
13 import java.awt.image.ColorModel;
14 import com.sun.medialib.mlib.Image;
15 import com.sun.medialib.mlib.mediaLibImage;
16 
17 final class MlibUtils {
18     /**
19      * If constants array is less than numBands it is replaced
20      * by an array of length numBands filled with constants[0].
21      * Otherwise the input array is cloned.
22      */
initConstants(int[] constants, int numBands)23     static final int[] initConstants(int[] constants, int numBands) {
24         int[] c = null;
25         if (constants.length < numBands) {
26             c = new int[numBands];
27             for (int i = 0; i < numBands; i++) {
28                 c[i] = constants[0];
29             }
30         } else {
31             c = (int[])constants.clone();
32         }
33 
34         return c;
35     }
36 
37     /**
38      * If constants array is less than numBands it is replaced
39      * by an array of length numBands filled with constants[0].
40      * Otherwise the input array is cloned.
41      */
initConstants(double[] constants, int numBands)42     static final double[] initConstants(double[] constants, int numBands) {
43         double[] c = null;
44         if (constants.length < numBands) {
45             c = new double[numBands];
46             for (int i = 0; i < numBands; i++) {
47                 c[i] = constants[0];
48             }
49         } else {
50             c = (double[])constants.clone();
51         }
52 
53         return c;
54     }
55 
56     /**
57      * If the color depth in bits of any band of the image does not
58      * match the full bit depth as determined from the image type then
59      * clamp the image to the unnomarlized range represented by the
60      * ColorModel.
61      */
clampImage(mediaLibImage image, ColorModel colorModel)62     static void clampImage(mediaLibImage image, ColorModel colorModel) {
63         if(image == null) {
64             throw new IllegalArgumentException("image == null!");
65         }
66 
67         if(colorModel != null) {
68             // Set the full bit depth as a function of image type.
69             int fullDepth = 0;
70             switch(image.getType()) {
71             case mediaLibImage.MLIB_BYTE:
72                 fullDepth = 8;
73                 break;
74             case mediaLibImage.MLIB_INT:
75                 fullDepth = 32;
76                 break;
77             default: // USHORT and SHORT
78                 fullDepth = 16;
79             }
80 
81             // Set the low and high thresholds and the thresholding flag.
82             int[] numBits = colorModel.getComponentSize();
83             int[] high = new int[numBits.length];
84             int[] low = new int[numBits.length]; // zero by default
85             boolean applyThreshold = false;
86             for(int j = 0; j < numBits.length; j++) {
87                 high[j] = (1 << numBits[j]) - 1;
88                 if(numBits[j] != fullDepth) {
89                     applyThreshold = true;
90                 }
91             }
92 
93             // Apply threshold if color depth of any band is not full depth.
94             if(applyThreshold) {
95                 Image.Thresh4(image, high, low, high, low);
96             }
97         }
98     }
99 }
100