/Users/lyon/j4p/src/ip/vs/ColorUtils.java
|
1 /**
2 * Victor Silva - University of Bridgeport 20 Feb. 1997
3 *
4 * Various utilities for Victor Silva's image
5 * processing project.
6 *
7 */
8 package ip.vs;
9
10
11 public class ColorUtils {
12 static public int[] imagetoInt(short[] imageR, short[] imageG,
13 short[] imageB, int numPixels) {
14 int[] imageData = new int[numPixels];
15
16 for (int i = 0; i < numPixels; i++) {
17 imageData[i] = ((0xFF) << 24) |
18 ((imageR[i] & 0xFF) << 16) |
19 ((imageG[i] & 0xFF) << 8) |
20 ((imageB[i] & 0xFF));
21 }
22 return imageData;
23 }
24
25 static public int[] imagetoInt(float[] imageR, float[] imageG,
26 float[] imageB) {
27 // Assume the Green and Blue arrays are the same size as Red.
28 int numPixels = imageR.length;
29 int[] imageData = new int[numPixels];
30
31 for (int i = 0; i < numPixels; i++) {
32 imageData[i] = ((0xFF) << 24) |
33 ((((int) imageR[i]) & 0xFF) << 16) |
34 ((((int) imageG[i]) & 0xFF) << 8) |
35 ((((int) imageB[i]) & 0xFF));
36 }
37 return imageData;
38 }
39
40 static public int[] imagetoInt(double[] imageR, double[] imageG,
41 double[] imageB, int numPixels) {
42 int[] imageData = new int[numPixels];
43
44 for (int i = 0; i < numPixels; i++) {
45 imageData[i] = ((0xFF) << 24) |
46 ((((int) imageR[i]) & 0xFF) << 16) |
47 ((((int) imageG[i]) & 0xFF) << 8) |
48 ((((int) imageB[i]) & 0xFF));
49 }
50 return imageData;
51 }
52
53 // RGB isolation utils
54 public short getRed(int valARGB) {
55 return ((short) ((valARGB >> 16) & 0xFF));
56 }
57
58 public short getGreen(int valARGB) {
59 return ((short) ((valARGB >> 8) & 0xFF));
60 }
61
62 public short getBlue(int valARGB) {
63 return ((short) (valARGB & 0xFF));
64 }
65
66 public short getA(int valARGB) {
67 return ((short) ((valARGB >> 24) & 0xFF));
68 }
69
70 public int putRGB(short r, short g, short b) {
71 int pixRGB;
72
73 pixRGB = (((int) 0xFF) << 24) | (((int) r & 0xFF) << 16) | (((int) g & 0xFF) << 8) |
74 ((int) b & 0xFF);
75
76 return (pixRGB);
77 }
78
79
80 // Maximum of R,G and B
81 public short max(short r, short g, short b) {
82 if (r > g) {
83 if (r > b) {
84 return (r);
85 } else {
86 return (b);
87 }
88 } else {
89 if (g > b) {
90 return (g);
91 } else {
92 return (b);
93 }
94 }
95 }
96
97 // Minimum of R,G and B
98 public short min(short r, short g, short b) {
99 if (r < g) {
100 if (r < b) {
101 return (r);
102 } else {
103 return (b);
104 }
105 } else {
106 if (g < b) {
107 return (g);
108 } else {
109 return (b);
110 }
111 }
112 }
113
114
115 public short[] getRedArray(int[] inputArray) {
116
117 int totalPix = inputArray.length;
118 short r[] = new short[totalPix];
119
120 for (int i = 0; i < totalPix; i++) {
121 r[i] = (short) ((inputArray[i] & 0x00FF0000) >> 16);
122 }
123 return r;
124 }
125
126 public short[] getGreenArray(int[] inputArray) {
127
128 int totalPix = inputArray.length;
129 short g[] = new short[totalPix];
130
131 for (int i = 0; i < totalPix; i++) {
132 g[i] = (short) ((inputArray[i] & 0x0000FF00) >> 8);
133 }
134 return g;
135 }
136
137 public short[] getBlueArray(int[] inputArray) {
138
139 int totalPix = inputArray.length;
140 short b[] = new short[totalPix];
141
142 for (int i = 0; i < totalPix; i++) {
143 b[i] = (short) (inputArray[i] & 0x000000FF);
144 }
145 return b;
146 }
147 }