JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / bin / javajs / img / CRCEncoder.js
1 Clazz.declarePackage ("javajs.img");\r
2 Clazz.load (["javajs.img.ImageEncoder"], "javajs.img.CRCEncoder", ["java.util.zip.CRC32", "javajs.util.AU"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.startPos = 0;\r
5 this.bytePos = 0;\r
6 this.crc = null;\r
7 this.pngBytes = null;\r
8 this.dataLen = 0;\r
9 this.int2 = null;\r
10 this.int4 = null;\r
11 Clazz.instantialize (this, arguments);\r
12 }, javajs.img, "CRCEncoder", javajs.img.ImageEncoder);\r
13 Clazz.prepareFields (c$, function () {\r
14 this.int2 =  Clazz.newByteArray (2, 0);\r
15 this.int4 =  Clazz.newByteArray (4, 0);\r
16 });\r
17 Clazz.makeConstructor (c$, \r
18 function () {\r
19 Clazz.superConstructor (this, javajs.img.CRCEncoder, []);\r
20 this.pngBytes =  Clazz.newByteArray (250, 0);\r
21 this.crc =  new java.util.zip.CRC32 ();\r
22 });\r
23 Clazz.defineMethod (c$, "setData", \r
24 function (b, pt) {\r
25 this.pngBytes = b;\r
26 this.dataLen = b.length;\r
27 this.startPos = this.bytePos = pt;\r
28 }, "~A,~N");\r
29 Clazz.defineMethod (c$, "getBytes", \r
30 function () {\r
31 return (this.dataLen == this.pngBytes.length ? this.pngBytes : javajs.util.AU.arrayCopyByte (this.pngBytes, this.dataLen));\r
32 });\r
33 Clazz.defineMethod (c$, "writeCRC", \r
34 function () {\r
35 this.crc.reset ();\r
36 this.crc.update (this.pngBytes, this.startPos, this.bytePos - this.startPos);\r
37 this.writeInt4 (this.crc.getValue ());\r
38 });\r
39 Clazz.defineMethod (c$, "writeInt2", \r
40 function (n) {\r
41 this.int2[0] = ((n >> 8) & 0xff);\r
42 this.int2[1] = (n & 0xff);\r
43 this.writeBytes (this.int2);\r
44 }, "~N");\r
45 Clazz.defineMethod (c$, "writeInt4", \r
46 function (n) {\r
47 javajs.img.CRCEncoder.getInt4 (n, this.int4);\r
48 this.writeBytes (this.int4);\r
49 }, "~N");\r
50 c$.getInt4 = Clazz.defineMethod (c$, "getInt4", \r
51 function (n, int4) {\r
52 int4[0] = ((n >> 24) & 0xff);\r
53 int4[1] = ((n >> 16) & 0xff);\r
54 int4[2] = ((n >> 8) & 0xff);\r
55 int4[3] = (n & 0xff);\r
56 }, "~N,~A");\r
57 Clazz.defineMethod (c$, "writeByte", \r
58 function (b) {\r
59 var temp =  Clazz.newByteArray (-1, [b]);\r
60 this.writeBytes (temp);\r
61 }, "~N");\r
62 Clazz.defineMethod (c$, "writeString", \r
63 function (s) {\r
64 this.writeBytes (s.getBytes ());\r
65 }, "~S");\r
66 Clazz.defineMethod (c$, "writeBytes", \r
67 function (data) {\r
68 var newPos = this.bytePos + data.length;\r
69 this.dataLen = Math.max (this.dataLen, newPos);\r
70 if (newPos > this.pngBytes.length) this.pngBytes = javajs.util.AU.arrayCopyByte (this.pngBytes, newPos + 16);\r
71 System.arraycopy (data, 0, this.pngBytes, this.bytePos, data.length);\r
72 this.bytePos = newPos;\r
73 }, "~A");\r
74 });\r