Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / java / awt / image / SinglePixelPackedSampleModel.js
1 Clazz.declarePackage ("java.awt.image");
2 Clazz.load (["java.awt.image.SampleModel"], "java.awt.image.SinglePixelPackedSampleModel", ["java.lang.ArrayIndexOutOfBoundsException", "$.IllegalArgumentException", "java.util.Arrays", "java.awt.image.DataBufferByte", "$.DataBufferInt", "$.RasterFormatException"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.bitMasks = null;
5 this.bitOffsets = null;
6 this.bitSizes = null;
7 this.maxBitSize = 0;
8 this.scanlineStride = 0;
9 Clazz.instantialize (this, arguments);
10 }, java.awt.image, "SinglePixelPackedSampleModel", java.awt.image.SampleModel);
11 Clazz.makeConstructor (c$, 
12 function (dataType, w, h, bitMasks) {
13 this.construct (dataType, w, h, w, bitMasks);
14 if (dataType != 0 && dataType != 3) {
15 throw  new IllegalArgumentException ("Unsupported data type " + dataType);
16 }}, "~N,~N,~N,~A");
17 Clazz.makeConstructor (c$, 
18 function (dataType, w, h, scanlineStride, bitMasks) {
19 Clazz.superConstructor (this, java.awt.image.SinglePixelPackedSampleModel, [dataType, w, h, bitMasks.length]);
20 if (dataType != 0 && dataType != 3) {
21 throw  new IllegalArgumentException ("Unsupported data type " + dataType);
22 }this.dataType = dataType;
23 this.bitMasks = bitMasks.clone ();
24 this.scanlineStride = scanlineStride;
25 this.bitOffsets =  Clazz.newIntArray (this.numBands, 0);
26 this.bitSizes =  Clazz.newIntArray (this.numBands, 0);
27 this.maxBitSize = 0;
28 for (var i = 0; i < this.numBands; i++) {
29 var bitOffset = 0;
30 var bitSize = 0;
31 var mask;
32 mask = bitMasks[i];
33 if (mask != 0) {
34 while ((mask & 1) == 0) {
35 mask = mask >>> 1;
36 bitOffset++;
37 }
38 while ((mask & 1) == 1) {
39 mask = mask >>> 1;
40 bitSize++;
41 }
42 if (mask != 0) {
43 throw  new IllegalArgumentException ("Mask " + bitMasks[i] + " must be contiguous");
44 }}this.bitOffsets[i] = bitOffset;
45 this.bitSizes[i] = bitSize;
46 if (bitSize > this.maxBitSize) {
47 this.maxBitSize = bitSize;
48 }}
49 }, "~N,~N,~N,~N,~A");
50 Clazz.overrideMethod (c$, "getNumDataElements", 
51 function () {
52 return 1;
53 });
54 Clazz.defineMethod (c$, "getBufferSize", 
55  function () {
56 var size = this.scanlineStride * (this.height - 1) + this.width;
57 return size;
58 });
59 Clazz.overrideMethod (c$, "createCompatibleSampleModel", 
60 function (w, h) {
61 var sampleModel =  new java.awt.image.SinglePixelPackedSampleModel (this.dataType, w, h, this.bitMasks);
62 return sampleModel;
63 }, "~N,~N");
64 Clazz.overrideMethod (c$, "createDataBuffer", 
65 function () {
66 var dataBuffer = null;
67 var size = this.getBufferSize ();
68 switch (this.dataType) {
69 case 0:
70 dataBuffer =  new java.awt.image.DataBufferByte (size);
71 break;
72 case 3:
73 dataBuffer =  new java.awt.image.DataBufferInt (size);
74 break;
75 }
76 return dataBuffer;
77 });
78 Clazz.defineMethod (c$, "getSampleSize", 
79 function () {
80 var mask;
81 var sampleSize =  Clazz.newIntArray (this.numBands, 0);
82 for (var i = 0; i < this.numBands; i++) {
83 sampleSize[i] = 0;
84 mask = this.bitMasks[i] >>> this.bitOffsets[i];
85 while ((mask & 1) != 0) {
86 sampleSize[i]++;
87 mask = mask >>> 1;
88 }
89 }
90 return sampleSize;
91 });
92 Clazz.defineMethod (c$, "getSampleSize", 
93 function (band) {
94 var sampleSize = 0;
95 var mask = this.bitMasks[band] >>> this.bitOffsets[band];
96 while ((mask & 1) != 0) {
97 sampleSize++;
98 mask = mask >>> 1;
99 }
100 return sampleSize;
101 }, "~N");
102 Clazz.defineMethod (c$, "getOffset", 
103 function (x, y) {
104 var offset = y * this.scanlineStride + x;
105 return offset;
106 }, "~N,~N");
107 Clazz.defineMethod (c$, "getBitOffsets", 
108 function () {
109 return this.bitOffsets.clone ();
110 });
111 Clazz.defineMethod (c$, "getBitMasks", 
112 function () {
113 return this.bitMasks.clone ();
114 });
115 Clazz.defineMethod (c$, "getScanlineStride", 
116 function () {
117 return this.scanlineStride;
118 });
119 Clazz.overrideMethod (c$, "createSubsetSampleModel", 
120 function (bands) {
121 if (bands.length > this.numBands) throw  new java.awt.image.RasterFormatException ("There are only " + this.numBands + " bands");
122 var newBitMasks =  Clazz.newIntArray (bands.length, 0);
123 for (var i = 0; i < bands.length; i++) newBitMasks[i] = this.bitMasks[bands[i]];
124
125 return  new java.awt.image.SinglePixelPackedSampleModel (this.dataType, this.width, this.height, this.scanlineStride, newBitMasks);
126 }, "~A");
127 Clazz.defineMethod (c$, "getDataElements", 
128 function (x, y, obj, data) {
129 if ((x < 0) || (y < 0) || (x >= this.width) || (y >= this.height)) {
130 throw  new ArrayIndexOutOfBoundsException ("Coordinate out of bounds!");
131 }var type = this.getTransferType ();
132 switch (type) {
133 case 0:
134 var bdata;
135 if (obj == null) bdata =  Clazz.newByteArray (1, 0);
136  else bdata = obj;
137 bdata[0] = data.getElem (y * this.scanlineStride + x);
138 obj = bdata;
139 break;
140 case 3:
141 var idata;
142 if (obj == null) idata =  Clazz.newIntArray (1, 0);
143  else idata = obj;
144 idata[0] = data.getElem (y * this.scanlineStride + x);
145 obj = idata;
146 break;
147 }
148 return obj;
149 }, "~N,~N,~O,java.awt.image.DataBuffer");
150 Clazz.defineMethod (c$, "getPixel", 
151 function (x, y, iArray, data) {
152 if ((x < 0) || (y < 0) || (x >= this.width) || (y >= this.height)) {
153 throw  new ArrayIndexOutOfBoundsException ("Coordinate out of bounds!");
154 }var pixels;
155 if (iArray == null) {
156 pixels =  Clazz.newIntArray (this.numBands, 0);
157 } else {
158 pixels = iArray;
159 }var value = data.getElem (y * this.scanlineStride + x);
160 for (var i = 0; i < this.numBands; i++) {
161 pixels[i] = (value & this.bitMasks[i]) >>> this.bitOffsets[i];
162 }
163 return pixels;
164 }, "~N,~N,~A,java.awt.image.DataBuffer");
165 Clazz.defineMethod (c$, "getPixels", 
166 function (x, y, w, h, iArray, data) {
167 if ((x < 0) || (y < 0) || (x + w > this.width) || (y + h > this.height)) {
168 throw  new ArrayIndexOutOfBoundsException ("Coordinate out of bounds!");
169 }var pixels;
170 if (iArray != null) {
171 pixels = iArray;
172 } else {
173 pixels =  Clazz.newIntArray (w * h * this.numBands, 0);
174 }var lineOffset = y * this.scanlineStride + x;
175 var dstOffset = 0;
176 for (var i = 0; i < h; i++) {
177 for (var j = 0; j < w; j++) {
178 var value = data.getElem (lineOffset + j);
179 for (var k = 0; k < this.numBands; k++) {
180 pixels[dstOffset++] = ((value & this.bitMasks[k]) >>> this.bitOffsets[k]);
181 }
182 }
183 lineOffset += this.scanlineStride;
184 }
185 return pixels;
186 }, "~N,~N,~N,~N,~A,java.awt.image.DataBuffer");
187 Clazz.overrideMethod (c$, "getSample", 
188 function (x, y, b, data) {
189 if ((x < 0) || (y < 0) || (x >= this.width) || (y >= this.height)) {
190 throw  new ArrayIndexOutOfBoundsException ("Coordinate out of bounds!");
191 }var sample = data.getElem (y * this.scanlineStride + x);
192 return ((sample & this.bitMasks[b]) >>> this.bitOffsets[b]);
193 }, "~N,~N,~N,java.awt.image.DataBuffer");
194 Clazz.defineMethod (c$, "getSamples", 
195 function (x, y, w, h, b, iArray, data) {
196 if ((x < 0) || (y < 0) || (x + w > this.width) || (y + h > this.height)) {
197 throw  new ArrayIndexOutOfBoundsException ("Coordinate out of bounds!");
198 }var samples;
199 if (iArray != null) {
200 samples = iArray;
201 } else {
202 samples =  Clazz.newIntArray (w * h, 0);
203 }var lineOffset = y * this.scanlineStride + x;
204 var dstOffset = 0;
205 for (var i = 0; i < h; i++) {
206 for (var j = 0; j < w; j++) {
207 var value = data.getElem (lineOffset + j);
208 samples[dstOffset++] = ((value & this.bitMasks[b]) >>> this.bitOffsets[b]);
209 }
210 lineOffset += this.scanlineStride;
211 }
212 return samples;
213 }, "~N,~N,~N,~N,~N,~A,java.awt.image.DataBuffer");
214 Clazz.defineMethod (c$, "setDataElements", 
215 function (x, y, obj, data) {
216 if ((x < 0) || (y < 0) || (x >= this.width) || (y >= this.height)) {
217 throw  new ArrayIndexOutOfBoundsException ("Coordinate out of bounds!");
218 }var type = this.getTransferType ();
219 switch (type) {
220 case 0:
221 var barray = obj;
222 data.setElem (y * this.scanlineStride + x, (barray[0]) & 0xff);
223 break;
224 case 3:
225 var iarray = obj;
226 data.setElem (y * this.scanlineStride + x, iarray[0]);
227 break;
228 }
229 }, "~N,~N,~O,java.awt.image.DataBuffer");
230 Clazz.defineMethod (c$, "setPixel", 
231 function (x, y, iArray, data) {
232 if ((x < 0) || (y < 0) || (x >= this.width) || (y >= this.height)) {
233 throw  new ArrayIndexOutOfBoundsException ("Coordinate out of bounds!");
234 }var lineOffset = y * this.scanlineStride + x;
235 var value = data.getElem (lineOffset);
236 for (var i = 0; i < this.numBands; i++) {
237 value &= ~this.bitMasks[i];
238 value |= ((iArray[i] << this.bitOffsets[i]) & this.bitMasks[i]);
239 }
240 data.setElem (lineOffset, value);
241 }, "~N,~N,~A,java.awt.image.DataBuffer");
242 Clazz.defineMethod (c$, "setPixels", 
243 function (x, y, w, h, iArray, data) {
244 if ((x < 0) || (y < 0) || (x + w > this.width) || (y + h > this.height)) {
245 throw  new ArrayIndexOutOfBoundsException ("Coordinate out of bounds!");
246 }var lineOffset = y * this.scanlineStride + x;
247 var srcOffset = 0;
248 for (var i = 0; i < h; i++) {
249 for (var j = 0; j < w; j++) {
250 var value = data.getElem (lineOffset + j);
251 for (var k = 0; k < this.numBands; k++) {
252 value &= ~this.bitMasks[k];
253 var srcValue = iArray[srcOffset++];
254 value |= ((srcValue << this.bitOffsets[k]) & this.bitMasks[k]);
255 }
256 data.setElem (lineOffset + j, value);
257 }
258 lineOffset += this.scanlineStride;
259 }
260 }, "~N,~N,~N,~N,~A,java.awt.image.DataBuffer");
261 Clazz.defineMethod (c$, "setSample", 
262 function (x, y, b, s, data) {
263 if ((x < 0) || (y < 0) || (x >= this.width) || (y >= this.height)) {
264 throw  new ArrayIndexOutOfBoundsException ("Coordinate out of bounds!");
265 }var value = data.getElem (y * this.scanlineStride + x);
266 value &= ~this.bitMasks[b];
267 value |= (s << this.bitOffsets[b]) & this.bitMasks[b];
268 data.setElem (y * this.scanlineStride + x, value);
269 }, "~N,~N,~N,~N,java.awt.image.DataBuffer");
270 Clazz.defineMethod (c$, "setSamples", 
271 function (x, y, w, h, b, iArray, data) {
272 if ((x < 0) || (y < 0) || (x + w > this.width) || (y + h > this.height)) {
273 throw  new ArrayIndexOutOfBoundsException ("Coordinate out of bounds!");
274 }var lineOffset = y * this.scanlineStride + x;
275 var srcOffset = 0;
276 for (var i = 0; i < h; i++) {
277 for (var j = 0; j < w; j++) {
278 var value = data.getElem (lineOffset + j);
279 value &= ~this.bitMasks[b];
280 var sample = iArray[srcOffset++];
281 value |= (sample << this.bitOffsets[b]) & this.bitMasks[b];
282 data.setElem (lineOffset + j, value);
283 }
284 lineOffset += this.scanlineStride;
285 }
286 }, "~N,~N,~N,~N,~N,~A,java.awt.image.DataBuffer");
287 Clazz.overrideMethod (c$, "equals", 
288 function (o) {
289 if ((o == null) || !(Clazz.instanceOf (o, java.awt.image.SinglePixelPackedSampleModel))) {
290 return false;
291 }var that = o;
292 return this.width == that.width && this.height == that.height && this.numBands == that.numBands && this.dataType == that.dataType && java.util.Arrays.equals (this.bitMasks, that.bitMasks) && java.util.Arrays.equals (this.bitOffsets, that.bitOffsets) && java.util.Arrays.equals (this.bitSizes, that.bitSizes) && this.maxBitSize == that.maxBitSize && this.scanlineStride == that.scanlineStride;
293 }, "~O");
294 Clazz.overrideMethod (c$, "hashCode", 
295 function () {
296 var hash = 0;
297 hash = this.width;
298 hash <<= 8;
299 hash ^= this.height;
300 hash <<= 8;
301 hash ^= this.numBands;
302 hash <<= 8;
303 hash ^= this.dataType;
304 hash <<= 8;
305 for (var i = 0; i < this.bitMasks.length; i++) {
306 hash ^= this.bitMasks[i];
307 hash <<= 8;
308 }
309 for (var i = 0; i < this.bitOffsets.length; i++) {
310 hash ^= this.bitOffsets[i];
311 hash <<= 8;
312 }
313 for (var i = 0; i < this.bitSizes.length; i++) {
314 hash ^= this.bitSizes[i];
315 hash <<= 8;
316 }
317 hash ^= this.maxBitSize;
318 hash <<= 8;
319 hash ^= this.scanlineStride;
320 return hash;
321 });
322 });