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