Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / java / awt / image / DirectColorModel.js
1 Clazz.declarePackage ("java.awt.image");
2 Clazz.load (["java.awt.image.PackedColorModel"], "java.awt.image.DirectColorModel", ["java.lang.ClassCastException", "$.IllegalArgumentException", "$.UnsupportedOperationException", "java.awt.color.ColorSpace", "java.awt.image.ColorModel", "$.Raster"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.red_mask = 0;
5 this.green_mask = 0;
6 this.blue_mask = 0;
7 this.alpha_mask = 0;
8 this.is_LinearRGB = false;
9 this.lRGBprecision = 0;
10 this.tosRGB8LUT = null;
11 this.fromsRGB8LUT8 = null;
12 this.fromsRGB8LUT16 = null;
13 Clazz.instantialize (this, arguments);
14 }, java.awt.image, "DirectColorModel", java.awt.image.PackedColorModel);
15 Clazz.makeConstructor (c$, 
16 function (bits, rmask, gmask, bmask) {
17 this.construct (bits, rmask, gmask, bmask, 0);
18 }, "~N,~N,~N,~N");
19 Clazz.makeConstructor (c$, 
20 function (bits, rmask, gmask, bmask, amask) {
21 Clazz.superConstructor (this, java.awt.image.DirectColorModel, [java.awt.color.ColorSpace.getInstance (1000), bits, rmask, gmask, bmask, amask, false, amask == 0 ? 1 : 3, java.awt.image.ColorModel.getDefaultTransferType (bits)]);
22 }, "~N,~N,~N,~N,~N");
23 Clazz.makeConstructor (c$, 
24 function (space, bits, rmask, gmask, bmask, amask, isAlphaPremultiplied, transferType) {
25 Clazz.superConstructor (this, java.awt.image.DirectColorModel, [space, bits, rmask, gmask, bmask, amask, isAlphaPremultiplied, amask == 0 ? 1 : 3, transferType]);
26 }, "java.awt.color.ColorSpace,~N,~N,~N,~N,~N,~B,~N");
27 Clazz.defineMethod (c$, "getRedMask", 
28 function () {
29 return this.maskArray[0];
30 });
31 Clazz.defineMethod (c$, "getGreenMask", 
32 function () {
33 return this.maskArray[1];
34 });
35 Clazz.defineMethod (c$, "getBlueMask", 
36 function () {
37 return this.maskArray[2];
38 });
39 Clazz.defineMethod (c$, "getAlphaMask", 
40 function () {
41 if (this.supportsAlpha) {
42 return this.maskArray[3];
43 } else {
44 return 0;
45 }});
46 Clazz.defineMethod (c$, "getDefaultRGBComponents", 
47  function (pixel) {
48 var components = this.getComponents (pixel, null, 0);
49 var norm = this.getNormalizedComponents (components, 0, null, 0);
50 return this.colorSpace.toRGB (norm);
51 }, "~N");
52 Clazz.defineMethod (c$, "getsRGBComponentFromsRGB", 
53  function (pixel, idx) {
54 var c = ((pixel & this.maskArray[idx]) >>> this.maskOffsets[idx]);
55 if (this.$isAlphaPremultiplied) {
56 var a = ((pixel & this.maskArray[3]) >>> this.maskOffsets[3]);
57 c = (a == 0) ? 0 : Clazz.floatToInt (((c * this.scaleFactors[idx]) * 255.0 / (a * this.scaleFactors[3])) + 0.5);
58 } else if (this.scaleFactors[idx] != 1.0) {
59 c = Clazz.floatToInt ((c * this.scaleFactors[idx]) + 0.5);
60 }return c;
61 }, "~N,~N");
62 Clazz.defineMethod (c$, "getsRGBComponentFromLinearRGB", 
63  function (pixel, idx) {
64 var c = ((pixel & this.maskArray[idx]) >>> this.maskOffsets[idx]);
65 if (this.$isAlphaPremultiplied) {
66 var factor = ((1 << this.lRGBprecision) - 1);
67 var a = ((pixel & this.maskArray[3]) >>> this.maskOffsets[3]);
68 c = (a == 0) ? 0 : Clazz.floatToInt (((c * this.scaleFactors[idx]) * factor / (a * this.scaleFactors[3])) + 0.5);
69 } else if (this.nBits[idx] != this.lRGBprecision) {
70 if (this.lRGBprecision == 16) {
71 c = Clazz.floatToInt ((c * this.scaleFactors[idx] * 257.0) + 0.5);
72 } else {
73 c = Clazz.floatToInt ((c * this.scaleFactors[idx]) + 0.5);
74 }}return this.tosRGB8LUT[c] & 0xff;
75 }, "~N,~N");
76 Clazz.defineMethod (c$, "getRed", 
77 function (pixel) {
78 if (this.is_sRGB) {
79 return this.getsRGBComponentFromsRGB (pixel, 0);
80 } else if (this.is_LinearRGB) {
81 return this.getsRGBComponentFromLinearRGB (pixel, 0);
82 }var rgb = this.getDefaultRGBComponents (pixel);
83 return Clazz.floatToInt (rgb[0] * 255.0 + 0.5);
84 }, "~N");
85 Clazz.defineMethod (c$, "getGreen", 
86 function (pixel) {
87 if (this.is_sRGB) {
88 return this.getsRGBComponentFromsRGB (pixel, 1);
89 } else if (this.is_LinearRGB) {
90 return this.getsRGBComponentFromLinearRGB (pixel, 1);
91 }var rgb = this.getDefaultRGBComponents (pixel);
92 return Clazz.floatToInt (rgb[1] * 255.0 + 0.5);
93 }, "~N");
94 Clazz.defineMethod (c$, "getBlue", 
95 function (pixel) {
96 if (this.is_sRGB) {
97 return this.getsRGBComponentFromsRGB (pixel, 2);
98 } else if (this.is_LinearRGB) {
99 return this.getsRGBComponentFromLinearRGB (pixel, 2);
100 }var rgb = this.getDefaultRGBComponents (pixel);
101 return Clazz.floatToInt (rgb[2] * 255.0 + 0.5);
102 }, "~N");
103 Clazz.defineMethod (c$, "getAlpha", 
104 function (pixel) {
105 if (!this.supportsAlpha) return 255;
106 var a = ((pixel & this.maskArray[3]) >>> this.maskOffsets[3]);
107 if (this.scaleFactors[3] != 1.0) {
108 a = Clazz.floatToInt (a * this.scaleFactors[3] + 0.5);
109 }return a;
110 }, "~N");
111 Clazz.defineMethod (c$, "getRGB", 
112 function (pixel) {
113 if (this.is_sRGB || this.is_LinearRGB) {
114 return (this.getAlpha (pixel) << 24) | (this.getRed (pixel) << 16) | (this.getGreen (pixel) << 8) | (this.getBlue (pixel) << 0);
115 }var rgb = this.getDefaultRGBComponents (pixel);
116 return (this.getAlpha (pixel) << 24) | ((Clazz.floatToInt (rgb[0] * 255.0 + 0.5)) << 16) | ((Clazz.floatToInt (rgb[1] * 255.0 + 0.5)) << 8) | ((Clazz.floatToInt (rgb[2] * 255.0 + 0.5)) << 0);
117 }, "~N");
118 Clazz.defineMethod (c$, "getRed", 
119 function (inData) {
120 var pixel = 0;
121 switch (this.transferType) {
122 case 0:
123 var bdata = inData;
124 pixel = bdata[0] & 0xff;
125 break;
126 case 3:
127 var idata = inData;
128 pixel = idata[0];
129 break;
130 default:
131 throw  new UnsupportedOperationException ("This method has not been " + "implemented for transferType " + this.transferType);
132 }
133 return this.getRed (pixel);
134 }, "~O");
135 Clazz.defineMethod (c$, "getGreen", 
136 function (inData) {
137 var pixel = 0;
138 switch (this.transferType) {
139 case 0:
140 var bdata = inData;
141 pixel = bdata[0] & 0xff;
142 break;
143 case 3:
144 var idata = inData;
145 pixel = idata[0];
146 break;
147 default:
148 throw  new UnsupportedOperationException ("This method has not been " + "implemented for transferType " + this.transferType);
149 }
150 return this.getGreen (pixel);
151 }, "~O");
152 Clazz.defineMethod (c$, "getBlue", 
153 function (inData) {
154 var pixel = 0;
155 switch (this.transferType) {
156 case 0:
157 var bdata = inData;
158 pixel = bdata[0] & 0xff;
159 break;
160 case 3:
161 var idata = inData;
162 pixel = idata[0];
163 break;
164 default:
165 throw  new UnsupportedOperationException ("This method has not been " + "implemented for transferType " + this.transferType);
166 }
167 return this.getBlue (pixel);
168 }, "~O");
169 Clazz.defineMethod (c$, "getAlpha", 
170 function (inData) {
171 var pixel = 0;
172 switch (this.transferType) {
173 case 0:
174 var bdata = inData;
175 pixel = bdata[0] & 0xff;
176 break;
177 case 3:
178 var idata = inData;
179 pixel = idata[0];
180 break;
181 default:
182 throw  new UnsupportedOperationException ("This method has not been " + "implemented for transferType " + this.transferType);
183 }
184 return this.getAlpha (pixel);
185 }, "~O");
186 Clazz.defineMethod (c$, "getRGB", 
187 function (inData) {
188 var pixel = 0;
189 switch (this.transferType) {
190 case 0:
191 var bdata = inData;
192 pixel = bdata[0] & 0xff;
193 break;
194 case 3:
195 var idata = inData;
196 pixel = idata[0];
197 break;
198 default:
199 throw  new UnsupportedOperationException ("This method has not been " + "implemented for transferType " + this.transferType);
200 }
201 return this.getRGB (pixel);
202 }, "~O");
203 Clazz.defineMethod (c$, "getDataElements", 
204 function (rgb, pixel) {
205 var intpixel = null;
206 if (this.transferType == 3 && pixel != null) {
207 intpixel = pixel;
208 intpixel[0] = 0;
209 } else {
210 intpixel =  Clazz.newIntArray (1, 0);
211 }var defaultCM = java.awt.image.ColorModel.getRGBdefault ();
212 if (this === defaultCM || this.equals (defaultCM)) {
213 intpixel[0] = rgb;
214 return intpixel;
215 }var red;
216 var grn;
217 var blu;
218 var alp;
219 red = (rgb >> 16) & 0xff;
220 grn = (rgb >> 8) & 0xff;
221 blu = rgb & 0xff;
222 if (this.is_sRGB || this.is_LinearRGB) {
223 var precision;
224 var factor;
225 if (this.is_LinearRGB) {
226 if (this.lRGBprecision == 8) {
227 red = this.fromsRGB8LUT8[red] & 0xff;
228 grn = this.fromsRGB8LUT8[grn] & 0xff;
229 blu = this.fromsRGB8LUT8[blu] & 0xff;
230 precision = 8;
231 factor = 0.003921569;
232 } else {
233 red = this.fromsRGB8LUT16[red] & 0xffff;
234 grn = this.fromsRGB8LUT16[grn] & 0xffff;
235 blu = this.fromsRGB8LUT16[blu] & 0xffff;
236 precision = 16;
237 factor = 1.5259022E-5;
238 }} else {
239 precision = 8;
240 factor = 0.003921569;
241 }if (this.supportsAlpha) {
242 alp = (rgb >> 24) & 0xff;
243 if (this.$isAlphaPremultiplied) {
244 factor *= (alp * (0.003921569));
245 precision = -1;
246 }if (this.nBits[3] != 8) {
247 alp = Clazz.floatToInt ((alp * (0.003921569) * ((1 << this.nBits[3]) - 1)) + 0.5);
248 if (alp > ((1 << this.nBits[3]) - 1)) {
249 alp = (1 << this.nBits[3]) - 1;
250 }}intpixel[0] = alp << this.maskOffsets[3];
251 }if (this.nBits[0] != precision) {
252 red = Clazz.floatToInt ((red * factor * ((1 << this.nBits[0]) - 1)) + 0.5);
253 }if (this.nBits[1] != precision) {
254 grn = Clazz.floatToInt ((grn * factor * ((1 << this.nBits[1]) - 1)) + 0.5);
255 }if (this.nBits[2] != precision) {
256 blu = Clazz.floatToInt ((blu * factor * ((1 << this.nBits[2]) - 1)) + 0.5);
257 }} else {
258 var norm =  Clazz.newFloatArray (3, 0);
259 var factor = 0.003921569;
260 norm[0] = red * factor;
261 norm[1] = grn * factor;
262 norm[2] = blu * factor;
263 norm = this.colorSpace.fromRGB (norm);
264 if (this.supportsAlpha) {
265 alp = (rgb >> 24) & 0xff;
266 if (this.$isAlphaPremultiplied) {
267 factor *= alp;
268 for (var i = 0; i < 3; i++) {
269 norm[i] *= factor;
270 }
271 }if (this.nBits[3] != 8) {
272 alp = Clazz.floatToInt ((alp * (0.003921569) * ((1 << this.nBits[3]) - 1)) + 0.5);
273 if (alp > ((1 << this.nBits[3]) - 1)) {
274 alp = (1 << this.nBits[3]) - 1;
275 }}intpixel[0] = alp << this.maskOffsets[3];
276 }red = Clazz.floatToInt ((norm[0] * ((1 << this.nBits[0]) - 1)) + 0.5);
277 grn = Clazz.floatToInt ((norm[1] * ((1 << this.nBits[1]) - 1)) + 0.5);
278 blu = Clazz.floatToInt ((norm[2] * ((1 << this.nBits[2]) - 1)) + 0.5);
279 }if (this.maxBits > 23) {
280 if (red > ((1 << this.nBits[0]) - 1)) {
281 red = (1 << this.nBits[0]) - 1;
282 }if (grn > ((1 << this.nBits[1]) - 1)) {
283 grn = (1 << this.nBits[1]) - 1;
284 }if (blu > ((1 << this.nBits[2]) - 1)) {
285 blu = (1 << this.nBits[2]) - 1;
286 }}intpixel[0] |= (red << this.maskOffsets[0]) | (grn << this.maskOffsets[1]) | (blu << this.maskOffsets[2]);
287 switch (this.transferType) {
288 case 0:
289 {
290 var bdata;
291 if (pixel == null) {
292 bdata =  Clazz.newByteArray (1, 0);
293 } else {
294 bdata = pixel;
295 }bdata[0] = (0xff & intpixel[0]);
296 return bdata;
297 }case 3:
298 return intpixel;
299 }
300 throw  new UnsupportedOperationException ("This method has not been " + "implemented for transferType " + this.transferType);
301 }, "~N,~O");
302 Clazz.defineMethod (c$, "getComponents", 
303 function (pixel, components, offset) {
304 if (components == null) {
305 components =  Clazz.newIntArray (offset + this.numComponents, 0);
306 }for (var i = 0; i < this.numComponents; i++) {
307 components[offset + i] = (pixel & this.maskArray[i]) >>> this.maskOffsets[i];
308 }
309 return components;
310 }, "~N,~A,~N");
311 Clazz.defineMethod (c$, "getComponents", 
312 function (pixel, components, offset) {
313 var intpixel = 0;
314 switch (this.transferType) {
315 case 0:
316 var bdata = pixel;
317 intpixel = bdata[0] & 0xff;
318 break;
319 case 3:
320 var idata = pixel;
321 intpixel = idata[0];
322 break;
323 default:
324 throw  new UnsupportedOperationException ("This method has not been " + "implemented for transferType " + this.transferType);
325 }
326 return this.getComponents (intpixel, components, offset);
327 }, "~O,~A,~N");
328 Clazz.overrideMethod (c$, "createCompatibleWritableRaster", 
329 function (w, h) {
330 if ((w <= 0) || (h <= 0)) {
331 throw  new IllegalArgumentException ("Width (" + w + ") and height (" + h + ") cannot be <= 0");
332 }var bandmasks;
333 if (this.supportsAlpha) {
334 bandmasks =  Clazz.newIntArray (4, 0);
335 bandmasks[3] = this.alpha_mask;
336 } else {
337 bandmasks =  Clazz.newIntArray (3, 0);
338 }bandmasks[0] = this.red_mask;
339 bandmasks[1] = this.green_mask;
340 bandmasks[2] = this.blue_mask;
341 if (this.pixel_bits > 8) {
342 return java.awt.image.Raster.createPackedRaster (3, w, h, bandmasks, null);
343 } else {
344 return java.awt.image.Raster.createPackedRaster (0, w, h, bandmasks, null);
345 }}, "~N,~N");
346 Clazz.defineMethod (c$, "getDataElement", 
347 function (components, offset) {
348 var pixel = 0;
349 for (var i = 0; i < this.numComponents; i++) {
350 pixel |= ((components[offset + i] << this.maskOffsets[i]) & this.maskArray[i]);
351 }
352 return pixel;
353 }, "~A,~N");
354 Clazz.defineMethod (c$, "getDataElements", 
355 function (components, offset, obj) {
356 var pixel = 0;
357 for (var i = 0; i < this.numComponents; i++) {
358 pixel |= ((components[offset + i] << this.maskOffsets[i]) & this.maskArray[i]);
359 }
360 switch (this.transferType) {
361 case 0:
362 if (Clazz.instanceOf (obj, Array)) {
363 var bdata = obj;
364 bdata[0] = (pixel & 0xff);
365 return bdata;
366 } else {
367 var bdata =  Clazz.newByteArray (-1, [(pixel & 0xff)]);
368 return bdata;
369 }case 3:
370 if (Clazz.instanceOf (obj, Array)) {
371 var idata = obj;
372 idata[0] = pixel;
373 return idata;
374 } else {
375 var idata =  Clazz.newIntArray (-1, [pixel]);
376 return idata;
377 }default:
378 throw  new ClassCastException ("This method has not been " + "implemented for transferType " + this.transferType);
379 }
380 }, "~A,~N,~O");
381 Clazz.overrideMethod (c$, "toString", 
382 function () {
383 return  String.instantialize ("DirectColorModel: rmask=" + Integer.toHexString (this.red_mask) + " gmask=" + Integer.toHexString (this.green_mask) + " bmask=" + Integer.toHexString (this.blue_mask) + " amask=" + Integer.toHexString (this.alpha_mask));
384 });
385 });