Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / java / awt / image / ColorModel.js
1 Clazz.declarePackage ("java.awt.image");
2 Clazz.load (["java.awt.Transparency", "java.awt.color.ColorSpace"], "java.awt.image.ColorModel", ["java.lang.IllegalArgumentException", "$.NullPointerException", "$.UnsupportedOperationException", "JU.AU"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.pixel_bits = 0;
5 this.nBits = null;
6 this.transparency = 3;
7 this.supportsAlpha = true;
8 this.$isAlphaPremultiplied = false;
9 this.numComponents = -1;
10 this.numColorComponents = -1;
11 this.colorSpace = null;
12 this.colorSpaceType = 5;
13 this.maxBits = 0;
14 this.is_sRGB = true;
15 this.transferType = 0;
16 Clazz.instantialize (this, arguments);
17 }, java.awt.image, "ColorModel", null, java.awt.Transparency);
18 Clazz.prepareFields (c$, function () {
19 this.colorSpace = java.awt.color.ColorSpace.getInstance (1000);
20 });
21 c$.getRGBdefault = Clazz.defineMethod (c$, "getRGBdefault", 
22 function () {
23 if (java.awt.image.ColorModel.RGBdefault == null) {
24 java.awt.image.ColorModel.RGBdefault =  new java.awt.image.DirectColorModel (32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
25 }return java.awt.image.ColorModel.RGBdefault;
26 });
27 Clazz.makeConstructor (c$, 
28 function (bits) {
29 this.pixel_bits = bits;
30 if (bits < 1) {
31 throw  new IllegalArgumentException ("Number of bits must be > 0");
32 }this.numComponents = 4;
33 this.numColorComponents = 3;
34 this.maxBits = bits;
35 this.transferType = java.awt.image.ColorModel.getDefaultTransferType (bits);
36 }, "~N");
37 Clazz.makeConstructor (c$, 
38 function (pixel_bits, bits, cspace, hasAlpha, isAlphaPremultiplied, transparency, transferType) {
39 this.colorSpace = cspace;
40 this.colorSpaceType = cspace.getType ();
41 this.numColorComponents = cspace.getNumComponents ();
42 this.numComponents = this.numColorComponents + (hasAlpha ? 1 : 0);
43 this.supportsAlpha = hasAlpha;
44 if (bits.length < this.numComponents) {
45 throw  new IllegalArgumentException ("Number of color/alpha components should be " + this.numComponents + " but length of bits array is " + bits.length);
46 }if (transparency < 1 || transparency > 3) {
47 throw  new IllegalArgumentException ("Unknown transparency: " + transparency);
48 }if (this.supportsAlpha == false) {
49 this.$isAlphaPremultiplied = false;
50 this.transparency = 1;
51 } else {
52 this.$isAlphaPremultiplied = isAlphaPremultiplied;
53 this.transparency = transparency;
54 }this.nBits = bits.clone ();
55 this.pixel_bits = pixel_bits;
56 if (pixel_bits <= 0) {
57 throw  new IllegalArgumentException ("Number of pixel bits must be > 0");
58 }this.maxBits = 0;
59 for (var i = 0; i < bits.length; i++) {
60 if (bits[i] < 0) {
61 throw  new IllegalArgumentException ("Number of bits must be >= 0");
62 }if (this.maxBits < bits[i]) {
63 this.maxBits = bits[i];
64 }}
65 if (this.maxBits == 0) {
66 throw  new IllegalArgumentException ("There must be at least one component with > 0 pixel bits.");
67 }if (cspace !== java.awt.color.ColorSpace.getInstance (1000)) {
68 this.is_sRGB = false;
69 }this.transferType = transferType;
70 }, "~N,~A,java.awt.color.ColorSpace,~B,~B,~N,~N");
71 Clazz.defineMethod (c$, "hasAlpha", 
72 function () {
73 return this.supportsAlpha;
74 });
75 Clazz.defineMethod (c$, "isAlphaPremultiplied", 
76 function () {
77 return this.$isAlphaPremultiplied;
78 });
79 Clazz.defineMethod (c$, "getTransferType", 
80 function () {
81 return this.transferType;
82 });
83 Clazz.defineMethod (c$, "getPixelSize", 
84 function () {
85 return this.pixel_bits;
86 });
87 Clazz.defineMethod (c$, "getComponentSize", 
88 function (componentIdx) {
89 if (this.nBits == null) {
90 throw  new NullPointerException ("Number of bits array is null.");
91 }return this.nBits[componentIdx];
92 }, "~N");
93 Clazz.defineMethod (c$, "getComponentSize", 
94 function () {
95 if (this.nBits != null) {
96 return this.nBits.clone ();
97 }return null;
98 });
99 Clazz.overrideMethod (c$, "getTransparency", 
100 function () {
101 return this.transparency;
102 });
103 Clazz.defineMethod (c$, "getNumComponents", 
104 function () {
105 return this.numComponents;
106 });
107 Clazz.defineMethod (c$, "getNumColorComponents", 
108 function () {
109 return this.numColorComponents;
110 });
111 Clazz.defineMethod (c$, "getRGB", 
112 function (pixel) {
113 return (this.getAlpha (pixel) << 24) | (this.getRed (pixel) << 16) | (this.getGreen (pixel) << 8) | (this.getBlue (pixel) << 0);
114 }, "~N");
115 Clazz.defineMethod (c$, "getRed", 
116 function (inData) {
117 var pixel = 0;
118 var length = 0;
119 var idata = inData;
120 pixel = idata[0];
121 length = idata.length;
122 if (length == 1) {
123 return this.getRed (pixel);
124 } else {
125 throw  new UnsupportedOperationException ("This method is not supported by this color model");
126 }}, "~O");
127 Clazz.defineMethod (c$, "getGreen", 
128 function (inData) {
129 var pixel = 0;
130 var length = 0;
131 var idata = inData;
132 pixel = idata[0];
133 length = idata.length;
134 if (length == 1) {
135 return this.getGreen (pixel);
136 } else {
137 throw  new UnsupportedOperationException ("This method is not supported by this color model");
138 }}, "~O");
139 Clazz.defineMethod (c$, "getBlue", 
140 function (inData) {
141 var pixel = 0;
142 var length = 0;
143 var idata = inData;
144 pixel = idata[0];
145 length = idata.length;
146 if (length == 1) {
147 return this.getBlue (pixel);
148 } else {
149 throw  new UnsupportedOperationException ("This method is not supported by this color model");
150 }}, "~O");
151 Clazz.defineMethod (c$, "getAlpha", 
152 function (inData) {
153 var pixel = 0;
154 var length = 0;
155 var idata = inData;
156 pixel = idata[0];
157 length = idata.length;
158 if (length == 1) {
159 return this.getAlpha (pixel);
160 } else {
161 throw  new UnsupportedOperationException ("This method is not supported by this color model");
162 }}, "~O");
163 Clazz.defineMethod (c$, "getRGB", 
164 function (inData) {
165 return (this.getAlpha (inData) << 24) | (this.getRed (inData) << 16) | (this.getGreen (inData) << 8) | (this.getBlue (inData) << 0);
166 }, "~O");
167 Clazz.defineMethod (c$, "getDataElements", 
168 function (rgb, pixel) {
169 throw  new UnsupportedOperationException ("This method is not supported by this color model.");
170 }, "~N,~O");
171 Clazz.defineMethod (c$, "getComponents", 
172 function (pixel, components, offset) {
173 throw  new UnsupportedOperationException ("This method is not supported by this color model.");
174 }, "~N,~A,~N");
175 Clazz.defineMethod (c$, "getComponents", 
176 function (pixel, components, offset) {
177 throw  new UnsupportedOperationException ("This method is not supported by this color model.");
178 }, "~O,~A,~N");
179 Clazz.defineMethod (c$, "getUnnormalizedComponents", 
180 function (normComponents, normOffset, components, offset) {
181 if (this.colorSpace == null) {
182 throw  new UnsupportedOperationException ("This method is not supported by this color model.");
183 }if (this.nBits == null) {
184 throw  new UnsupportedOperationException ("This method is not supported.  Unable to determine #bits per component.");
185 }if ((normComponents.length - normOffset) < this.numComponents) {
186 throw  new IllegalArgumentException ("Incorrect number of components.  Expecting " + this.numComponents);
187 }if (components == null) {
188 components =  Clazz.newIntArray (offset + this.numComponents, 0);
189 }if (this.supportsAlpha && this.$isAlphaPremultiplied) {
190 var normAlpha = normComponents[normOffset + this.numColorComponents];
191 for (var i = 0; i < this.numColorComponents; i++) {
192 components[offset + i] = Clazz.floatToInt (normComponents[normOffset + i] * ((1 << this.nBits[i]) - 1) * normAlpha + 0.5);
193 }
194 components[offset + this.numColorComponents] = Clazz.floatToInt (normAlpha * ((1 << this.nBits[this.numColorComponents]) - 1) + 0.5);
195 } else {
196 for (var i = 0; i < this.numComponents; i++) {
197 components[offset + i] = Clazz.floatToInt (normComponents[normOffset + i] * ((1 << this.nBits[i]) - 1) + 0.5);
198 }
199 }return components;
200 }, "~A,~N,~A,~N");
201 Clazz.defineMethod (c$, "getNormalizedComponents", 
202 function (components, offset, normComponents, normOffset) {
203 if (this.colorSpace == null) {
204 throw  new UnsupportedOperationException ("This method is not supported by this color model.");
205 }if (this.nBits == null) {
206 throw  new UnsupportedOperationException ("This method is not supported.  Unable to determine #bits per component.");
207 }if ((components.length - offset) < this.numComponents) {
208 throw  new IllegalArgumentException ("Incorrect number of components.  Expecting " + this.numComponents);
209 }if (normComponents == null) {
210 normComponents =  Clazz.newFloatArray (this.numComponents + normOffset, 0);
211 }if (this.supportsAlpha && this.$isAlphaPremultiplied) {
212 var normAlpha = components[offset + this.numColorComponents];
213 normAlpha /= ((1 << this.nBits[this.numColorComponents]) - 1);
214 if (normAlpha != 0.0) {
215 for (var i = 0; i < this.numColorComponents; i++) {
216 normComponents[normOffset + i] = (components[offset + i]) / (normAlpha * (((1 << this.nBits[i]) - 1)));
217 }
218 } else {
219 for (var i = 0; i < this.numColorComponents; i++) {
220 normComponents[normOffset + i] = 0.0;
221 }
222 }normComponents[normOffset + this.numColorComponents] = normAlpha;
223 } else {
224 for (var i = 0; i < this.numComponents; i++) {
225 normComponents[normOffset + i] = (components[offset + i]) / (((1 << this.nBits[i]) - 1));
226 }
227 }return normComponents;
228 }, "~A,~N,~A,~N");
229 Clazz.defineMethod (c$, "getDataElement", 
230 function (components, offset) {
231 return this.getDataElementInt (components, offset);
232 }, "~A,~N");
233 Clazz.defineMethod (c$, "getDataElementInt", 
234 function (components, offset) {
235 throw  new UnsupportedOperationException ("This method is not supported by this color model.");
236 }, "~A,~N");
237 Clazz.defineMethod (c$, "getDataElement", 
238 function (normComponents, normOffset) {
239 if (JU.AU.isAI (normComponents)) {
240 var ints = normComponents;
241 return this.getDataElementInt (ints, normOffset);
242 }var components = this.getUnnormalizedComponents (normComponents, normOffset, null, 0);
243 return this.getDataElement (components, 0);
244 }, "~A,~N");
245 Clazz.defineMethod (c$, "getDataElements", 
246 function (normComponents, normOffset, obj) {
247 var components = this.getUnnormalizedComponents (normComponents, normOffset, null, 0);
248 return this.getDataElements (components, 0, obj);
249 }, "~A,~N,~O");
250 Clazz.defineMethod (c$, "getNormalizedComponents", 
251 function (pixel, normComponents, normOffset) {
252 var components = this.getComponents (pixel, null, 0);
253 return this.getNormalizedComponents (components, 0, normComponents, normOffset);
254 }, "~O,~A,~N");
255 Clazz.overrideMethod (c$, "equals", 
256 function (obj) {
257 if (!(Clazz.instanceOf (obj, java.awt.image.ColorModel))) {
258 return false;
259 }var cm = obj;
260 if (this === cm) {
261 return true;
262 }if (this.supportsAlpha != cm.hasAlpha () || this.$isAlphaPremultiplied != cm.isAlphaPremultiplied () || this.pixel_bits != cm.getPixelSize () || this.transparency != cm.getTransparency () || this.numComponents != cm.getNumComponents ()) {
263 return false;
264 }var nb = cm.getComponentSize ();
265 if ((this.nBits != null) && (nb != null)) {
266 for (var i = 0; i < this.numComponents; i++) {
267 if (this.nBits[i] != nb[i]) {
268 return false;
269 }}
270 } else {
271 return ((this.nBits == null) && (nb == null));
272 }return true;
273 }, "~O");
274 Clazz.overrideMethod (c$, "hashCode", 
275 function () {
276 var result = 0;
277 result = (this.supportsAlpha ? 2 : 3) + (this.$isAlphaPremultiplied ? 4 : 5) + this.pixel_bits * 6 + this.transparency * 7 + this.numComponents * 8;
278 if (this.nBits != null) {
279 for (var i = 0; i < this.numComponents; i++) {
280 result = result + this.nBits[i] * (i + 9);
281 }
282 }return result;
283 });
284 Clazz.defineMethod (c$, "getColorSpace", 
285 function () {
286 return this.colorSpace;
287 });
288 Clazz.defineMethod (c$, "isCompatibleRaster", 
289 function (raster) {
290 throw  new UnsupportedOperationException ("This method has not been implemented for this ColorModel.");
291 }, "java.awt.image.Raster");
292 Clazz.defineMethod (c$, "createCompatibleWritableRaster", 
293 function (w, h) {
294 throw  new UnsupportedOperationException ("This method is not supported by this color model");
295 }, "~N,~N");
296 Clazz.defineMethod (c$, "createCompatibleSampleModel", 
297 function (w, h) {
298 throw  new UnsupportedOperationException ("This method is not supported by this color model");
299 }, "~N,~N");
300 Clazz.defineMethod (c$, "isCompatibleSampleModel", 
301 function (sm) {
302 throw  new UnsupportedOperationException ("This method is not supported by this color model");
303 }, "java.awt.image.SampleModel");
304 Clazz.overrideMethod (c$, "finalize", 
305 function () {
306 });
307 Clazz.defineMethod (c$, "getAlphaRaster", 
308 function (raster) {
309 return null;
310 }, "java.awt.image.WritableRaster");
311 Clazz.overrideMethod (c$, "toString", 
312 function () {
313 return  String.instantialize ("ColorModel: #pixelBits = " + this.pixel_bits + " numComponents = " + this.numComponents + " color space = " + this.colorSpace + " transparency = " + this.transparency + " has alpha = " + this.supportsAlpha + " isAlphaPre = " + this.$isAlphaPremultiplied);
314 });
315 c$.getDefaultTransferType = Clazz.defineMethod (c$, "getDefaultTransferType", 
316 function (pixel_bits) {
317 if (pixel_bits <= 8) {
318 return 0;
319 }return 3;
320 }, "~N");
321 c$.isLinearRGBspace = Clazz.defineMethod (c$, "isLinearRGBspace", 
322 function (cs) {
323 return false;
324 }, "java.awt.color.ColorSpace");
325 c$.isLinearGRAYspace = Clazz.defineMethod (c$, "isLinearGRAYspace", 
326 function (cs) {
327 return false;
328 }, "java.awt.color.ColorSpace");
329 Clazz.defineStatics (c$,
330 "RGBdefault", null,
331 "l8Tos8", null,
332 "s8Tol8", null,
333 "l16Tos8", null,
334 "s8Tol16", null,
335 "g8Tos8Map", null,
336 "lg16Toog8Map", null,
337 "g16Tos8Map", null,
338 "lg16Toog16Map", null);
339 });