JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / bin / javajs / img / PngEncoder.js
1 Clazz.declarePackage ("javajs.img");\r
2 Clazz.load (["javajs.img.CRCEncoder"], "javajs.img.PngEncoder", ["java.io.ByteArrayOutputStream", "java.util.zip.Deflater", "$.DeflaterOutputStream"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.encodeAlpha = false;\r
5 this.filter = 0;\r
6 this.bytesPerPixel = 0;\r
7 this.compressionLevel = 0;\r
8 this.type = null;\r
9 this.transparentColor = null;\r
10 this.appData = null;\r
11 this.appPrefix = null;\r
12 this.comment = null;\r
13 this.bytes = null;\r
14 this.scanLines = null;\r
15 this.byteWidth = 0;\r
16 Clazz.instantialize (this, arguments);\r
17 }, javajs.img, "PngEncoder", javajs.img.CRCEncoder);\r
18 Clazz.overrideMethod (c$, "setParams", \r
19 function (params) {\r
20 if (this.quality < 0) this.quality = (params.containsKey ("qualityPNG") ? (params.get ("qualityPNG")).intValue () : 2);\r
21 if (this.quality > 9) this.quality = 9;\r
22 this.encodeAlpha = false;\r
23 this.filter = 0;\r
24 this.compressionLevel = this.quality;\r
25 this.transparentColor = params.get ("transparentColor");\r
26 this.comment = params.get ("comment");\r
27 this.type = (params.get ("type") + "0000").substring (0, 4);\r
28 this.bytes = params.get ("pngImgData");\r
29 this.appData = params.get ("pngAppData");\r
30 this.appPrefix = params.get ("pngAppPrefix");\r
31 }, "java.util.Map");\r
32 Clazz.overrideMethod (c$, "generate", \r
33 function () {\r
34 if (this.bytes == null) {\r
35 if (!this.pngEncode ()) {\r
36 this.out.cancel ();\r
37 return;\r
38 }this.bytes = this.getBytes ();\r
39 } else {\r
40 this.dataLen = this.bytes.length;\r
41 }var len = this.dataLen;\r
42 if (this.appData != null) {\r
43 javajs.img.PngEncoder.setJmolTypeText (this.appPrefix, this.bytes, len, this.appData.length, this.type);\r
44 this.out.write (this.bytes, 0, len);\r
45 len = (this.bytes = this.appData).length;\r
46 }this.out.write (this.bytes, 0, len);\r
47 });\r
48 Clazz.defineMethod (c$, "pngEncode", \r
49 ($fz = function () {\r
50 var pngIdBytes =  Clazz.newByteArray (-1, [-119, 80, 78, 71, 13, 10, 26, 10]);\r
51 this.writeBytes (pngIdBytes);\r
52 this.writeHeader ();\r
53 this.writeText (javajs.img.PngEncoder.getApplicationText (this.appPrefix, this.type, 0, 0));\r
54 this.writeText ("Software\0Jmol " + this.comment);\r
55 this.writeText ("Creation Time\0" + this.date);\r
56 if (!this.encodeAlpha && this.transparentColor != null) this.writeTransparentColor (this.transparentColor.intValue ());\r
57 return this.writeImageData ();\r
58 }, $fz.isPrivate = true, $fz));\r
59 c$.setJmolTypeText = Clazz.defineMethod (c$, "setJmolTypeText", \r
60 ($fz = function (prefix, b, nPNG, nState, type) {\r
61 var s = "tEXt" + javajs.img.PngEncoder.getApplicationText (prefix, type, nPNG, nState);\r
62 var encoder =  new javajs.img.PngEncoder ();\r
63 var test = s.substring (0, 4 + prefix.length).getBytes ();\r
64 for (var i = test.length; --i >= 0; ) if (b[i + 37] != test[i]) {\r
65 System.out.println ("image is not of the right form; appending data, but not adding tEXt tag.");\r
66 return;\r
67 }\r
68 encoder.setData (b, 37);\r
69 encoder.writeString (s);\r
70 encoder.writeCRC ();\r
71 }, $fz.isPrivate = true, $fz), "~S,~A,~N,~N,~S");\r
72 c$.getApplicationText = Clazz.defineMethod (c$, "getApplicationText", \r
73 ($fz = function (prefix, type, nPNG, nState) {\r
74 var sPNG = "000000000" + nPNG;\r
75 sPNG = sPNG.substring (sPNG.length - 9);\r
76 var sState = "000000000" + nState;\r
77 sState = sState.substring (sState.length - 9);\r
78 return prefix + "\0" + type + (type.equals ("PNG") ? "0" : "") + sPNG + "+" + sState;\r
79 }, $fz.isPrivate = true, $fz), "~S,~S,~N,~N");\r
80 Clazz.defineMethod (c$, "writeHeader", \r
81 ($fz = function () {\r
82 this.writeInt4 (13);\r
83 this.startPos = this.bytePos;\r
84 this.writeString ("IHDR");\r
85 this.writeInt4 (this.width);\r
86 this.writeInt4 (this.height);\r
87 this.writeByte (8);\r
88 this.writeByte (this.encodeAlpha ? 6 : 2);\r
89 this.writeByte (0);\r
90 this.writeByte (0);\r
91 this.writeByte (0);\r
92 this.writeCRC ();\r
93 }, $fz.isPrivate = true, $fz));\r
94 Clazz.defineMethod (c$, "writeText", \r
95 ($fz = function (msg) {\r
96 this.writeInt4 (msg.length);\r
97 this.startPos = this.bytePos;\r
98 this.writeString ("tEXt" + msg);\r
99 this.writeCRC ();\r
100 }, $fz.isPrivate = true, $fz), "~S");\r
101 Clazz.defineMethod (c$, "writeTransparentColor", \r
102 ($fz = function (icolor) {\r
103 this.writeInt4 (6);\r
104 this.startPos = this.bytePos;\r
105 this.writeString ("tRNS");\r
106 this.writeInt2 ((icolor >> 16) & 0xFF);\r
107 this.writeInt2 ((icolor >> 8) & 0xFF);\r
108 this.writeInt2 (icolor & 0xFF);\r
109 this.writeCRC ();\r
110 }, $fz.isPrivate = true, $fz), "~N");\r
111 Clazz.defineMethod (c$, "writeImageData", \r
112 ($fz = function () {\r
113 this.bytesPerPixel = (this.encodeAlpha ? 4 : 3);\r
114 this.byteWidth = this.width * this.bytesPerPixel;\r
115 var scanWidth = this.byteWidth + 1;\r
116 var rowsLeft = this.height;\r
117 var nRows;\r
118 var scanPos;\r
119 var deflater =  new java.util.zip.Deflater (this.compressionLevel);\r
120 var outBytes =  new java.io.ByteArrayOutputStream (1024);\r
121 var compBytes =  new java.util.zip.DeflaterOutputStream (outBytes, deflater);\r
122 var pt = 0;\r
123 try {\r
124 while (rowsLeft > 0) {\r
125 nRows = Math.max (1, Math.min (Clazz.doubleToInt (32767 / scanWidth), rowsLeft));\r
126 this.scanLines =  Clazz.newByteArray (scanWidth * nRows, 0);\r
127 var nPixels = this.width * nRows;\r
128 scanPos = 0;\r
129 for (var i = 0; i < nPixels; i++, pt++) {\r
130 if (i % this.width == 0) {\r
131 this.scanLines[scanPos++] = this.filter;\r
132 }this.scanLines[scanPos++] = ((this.pixels[pt] >> 16) & 0xff);\r
133 this.scanLines[scanPos++] = ((this.pixels[pt] >> 8) & 0xff);\r
134 this.scanLines[scanPos++] = ((this.pixels[pt]) & 0xff);\r
135 if (this.encodeAlpha) {\r
136 this.scanLines[scanPos++] = ((this.pixels[pt] >> 24) & 0xff);\r
137 }}\r
138 compBytes.write (this.scanLines, 0, scanPos);\r
139 rowsLeft -= nRows;\r
140 }\r
141 compBytes.close ();\r
142 var compressedLines = outBytes.toByteArray ();\r
143 this.writeInt4 (compressedLines.length);\r
144 this.startPos = this.bytePos;\r
145 this.writeString ("IDAT");\r
146 this.writeBytes (compressedLines);\r
147 this.writeCRC ();\r
148 this.writeEnd ();\r
149 deflater.finish ();\r
150 return true;\r
151 } catch (e) {\r
152 if (Clazz.exceptionOf (e, java.io.IOException)) {\r
153 System.err.println (e.toString ());\r
154 return false;\r
155 } else {\r
156 throw e;\r
157 }\r
158 }\r
159 }, $fz.isPrivate = true, $fz));\r
160 Clazz.defineMethod (c$, "writeEnd", \r
161 ($fz = function () {\r
162 this.writeInt4 (0);\r
163 this.startPos = this.bytePos;\r
164 this.writeString ("IEND");\r
165 this.writeCRC ();\r
166 }, $fz.isPrivate = true, $fz));\r
167 Clazz.defineStatics (c$,\r
168 "FILTER_NONE", 0,\r
169 "FILTER_SUB", 1,\r
170 "FILTER_UP", 2,\r
171 "FILTER_LAST", 2,\r
172 "PT_FIRST_TAG", 37);\r
173 });\r