1 Clazz.declarePackage ("javajs.img");
2 Clazz.load (["javajs.img.CRCEncoder"], "javajs.img.PngEncoder", ["java.io.ByteArrayOutputStream", "java.util.zip.Deflater", "$.DeflaterOutputStream"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.encodeAlpha = false;
6 this.bytesPerPixel = 0;
7 this.compressionLevel = 0;
9 this.transparentColor = null;
11 this.appPrefix = null;
14 this.scanLines = null;
16 Clazz.instantialize (this, arguments);
17 }, javajs.img, "PngEncoder", javajs.img.CRCEncoder);
18 Clazz.overrideMethod (c$, "setParams",
20 if (this.quality < 0) this.quality = (params.containsKey ("qualityPNG") ? (params.get ("qualityPNG")).intValue () : 2);
21 if (this.quality > 9) this.quality = 9;
22 this.encodeAlpha = false;
24 this.compressionLevel = this.quality;
25 this.transparentColor = params.get ("transparentColor");
26 this.comment = params.get ("comment");
27 this.type = (params.get ("type") + "0000").substring (0, 4);
28 this.bytes = params.get ("pngImgData");
29 this.appData = params.get ("pngAppData");
30 this.appPrefix = params.get ("pngAppPrefix");
32 Clazz.overrideMethod (c$, "generate",
34 if (this.bytes == null) {
35 if (!this.pngEncode ()) {
38 }this.bytes = this.getBytes ();
40 this.dataLen = this.bytes.length;
41 }var len = this.dataLen;
42 if (this.appData != null) {
43 javajs.img.PngEncoder.setJmolTypeText (this.appPrefix, this.bytes, len, this.appData.length, this.type);
44 this.out.write (this.bytes, 0, len);
45 len = (this.bytes = this.appData).length;
46 }this.out.write (this.bytes, 0, len);
48 Clazz.defineMethod (c$, "pngEncode",
50 var pngIdBytes = Clazz.newByteArray (-1, [-119, 80, 78, 71, 13, 10, 26, 10]);
51 this.writeBytes (pngIdBytes);
53 this.writeText (javajs.img.PngEncoder.getApplicationText (this.appPrefix, this.type, 0, 0));
54 this.writeText ("Software\0Jmol " + this.comment);
55 this.writeText ("Creation Time\0" + this.date);
56 if (!this.encodeAlpha && this.transparentColor != null) this.writeTransparentColor (this.transparentColor.intValue ());
57 return this.writeImageData ();
58 }, $fz.isPrivate = true, $fz));
59 c$.setJmolTypeText = Clazz.defineMethod (c$, "setJmolTypeText",
60 ($fz = function (prefix, b, nPNG, nState, type) {
61 var s = "tEXt" + javajs.img.PngEncoder.getApplicationText (prefix, type, nPNG, nState);
62 var encoder = new javajs.img.PngEncoder ();
63 var test = s.substring (0, 4 + prefix.length).getBytes ();
64 for (var i = test.length; --i >= 0; ) if (b[i + 37] != test[i]) {
65 System.out.println ("image is not of the right form; appending data, but not adding tEXt tag.");
68 encoder.setData (b, 37);
69 encoder.writeString (s);
71 }, $fz.isPrivate = true, $fz), "~S,~A,~N,~N,~S");
72 c$.getApplicationText = Clazz.defineMethod (c$, "getApplicationText",
73 ($fz = function (prefix, type, nPNG, nState) {
74 var sPNG = "000000000" + nPNG;
75 sPNG = sPNG.substring (sPNG.length - 9);
76 var sState = "000000000" + nState;
77 sState = sState.substring (sState.length - 9);
78 return prefix + "\0" + type + (type.equals ("PNG") ? "0" : "") + sPNG + "+" + sState;
79 }, $fz.isPrivate = true, $fz), "~S,~S,~N,~N");
80 Clazz.defineMethod (c$, "writeHeader",
83 this.startPos = this.bytePos;
84 this.writeString ("IHDR");
85 this.writeInt4 (this.width);
86 this.writeInt4 (this.height);
88 this.writeByte (this.encodeAlpha ? 6 : 2);
93 }, $fz.isPrivate = true, $fz));
94 Clazz.defineMethod (c$, "writeText",
95 ($fz = function (msg) {
96 this.writeInt4 (msg.length);
97 this.startPos = this.bytePos;
98 this.writeString ("tEXt" + msg);
100 }, $fz.isPrivate = true, $fz), "~S");
101 Clazz.defineMethod (c$, "writeTransparentColor",
102 ($fz = function (icolor) {
104 this.startPos = this.bytePos;
105 this.writeString ("tRNS");
106 this.writeInt2 ((icolor >> 16) & 0xFF);
107 this.writeInt2 ((icolor >> 8) & 0xFF);
108 this.writeInt2 (icolor & 0xFF);
110 }, $fz.isPrivate = true, $fz), "~N");
111 Clazz.defineMethod (c$, "writeImageData",
113 this.bytesPerPixel = (this.encodeAlpha ? 4 : 3);
114 this.byteWidth = this.width * this.bytesPerPixel;
115 var scanWidth = this.byteWidth + 1;
116 var rowsLeft = this.height;
119 var deflater = new java.util.zip.Deflater (this.compressionLevel);
120 var outBytes = new java.io.ByteArrayOutputStream (1024);
121 var compBytes = new java.util.zip.DeflaterOutputStream (outBytes, deflater);
124 while (rowsLeft > 0) {
125 nRows = Math.max (1, Math.min (Clazz.doubleToInt (32767 / scanWidth), rowsLeft));
126 this.scanLines = Clazz.newByteArray (scanWidth * nRows, 0);
127 var nPixels = this.width * nRows;
129 for (var i = 0; i < nPixels; i++, pt++) {
130 if (i % this.width == 0) {
131 this.scanLines[scanPos++] = this.filter;
132 }this.scanLines[scanPos++] = ((this.pixels[pt] >> 16) & 0xff);
133 this.scanLines[scanPos++] = ((this.pixels[pt] >> 8) & 0xff);
134 this.scanLines[scanPos++] = ((this.pixels[pt]) & 0xff);
135 if (this.encodeAlpha) {
136 this.scanLines[scanPos++] = ((this.pixels[pt] >> 24) & 0xff);
138 compBytes.write (this.scanLines, 0, scanPos);
142 var compressedLines = outBytes.toByteArray ();
143 this.writeInt4 (compressedLines.length);
144 this.startPos = this.bytePos;
145 this.writeString ("IDAT");
146 this.writeBytes (compressedLines);
152 if (Clazz.exceptionOf (e, java.io.IOException)) {
153 System.err.println (e.toString ());
159 }, $fz.isPrivate = true, $fz));
160 Clazz.defineMethod (c$, "writeEnd",
163 this.startPos = this.bytePos;
164 this.writeString ("IEND");
166 }, $fz.isPrivate = true, $fz));
167 Clazz.defineStatics (c$,