JAL-1807 test
[jalviewjs.git] / bin / javajs / img / PngEncoder.js
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;
5 this.filter = 0;
6 this.bytesPerPixel = 0;
7 this.compressionLevel = 0;
8 this.type = null;
9 this.transparentColor = null;
10 this.appData = null;
11 this.appPrefix = null;
12 this.comment = null;
13 this.bytes = null;
14 this.scanLines = null;
15 this.byteWidth = 0;
16 Clazz.instantialize (this, arguments);
17 }, javajs.img, "PngEncoder", javajs.img.CRCEncoder);
18 Clazz.overrideMethod (c$, "setParams", 
19 function (params) {
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;
23 this.filter = 0;
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");
31 }, "java.util.Map");
32 Clazz.overrideMethod (c$, "generate", 
33 function () {
34 if (this.bytes == null) {
35 if (!this.pngEncode ()) {
36 this.out.cancel ();
37 return;
38 }this.bytes = this.getBytes ();
39 } else {
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);
47 });
48 Clazz.defineMethod (c$, "pngEncode", 
49 ($fz = function () {
50 var pngIdBytes =  Clazz.newByteArray (-1, [-119, 80, 78, 71, 13, 10, 26, 10]);
51 this.writeBytes (pngIdBytes);
52 this.writeHeader ();
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.");
66 return;
67 }
68 encoder.setData (b, 37);
69 encoder.writeString (s);
70 encoder.writeCRC ();
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", 
81 ($fz = function () {
82 this.writeInt4 (13);
83 this.startPos = this.bytePos;
84 this.writeString ("IHDR");
85 this.writeInt4 (this.width);
86 this.writeInt4 (this.height);
87 this.writeByte (8);
88 this.writeByte (this.encodeAlpha ? 6 : 2);
89 this.writeByte (0);
90 this.writeByte (0);
91 this.writeByte (0);
92 this.writeCRC ();
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);
99 this.writeCRC ();
100 }, $fz.isPrivate = true, $fz), "~S");
101 Clazz.defineMethod (c$, "writeTransparentColor", 
102 ($fz = function (icolor) {
103 this.writeInt4 (6);
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);
109 this.writeCRC ();
110 }, $fz.isPrivate = true, $fz), "~N");
111 Clazz.defineMethod (c$, "writeImageData", 
112 ($fz = function () {
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;
117 var nRows;
118 var scanPos;
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);
122 var pt = 0;
123 try {
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;
128 scanPos = 0;
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);
137 }}
138 compBytes.write (this.scanLines, 0, scanPos);
139 rowsLeft -= nRows;
140 }
141 compBytes.close ();
142 var compressedLines = outBytes.toByteArray ();
143 this.writeInt4 (compressedLines.length);
144 this.startPos = this.bytePos;
145 this.writeString ("IDAT");
146 this.writeBytes (compressedLines);
147 this.writeCRC ();
148 this.writeEnd ();
149 deflater.finish ();
150 return true;
151 } catch (e) {
152 if (Clazz.exceptionOf (e, java.io.IOException)) {
153 System.err.println (e.toString ());
154 return false;
155 } else {
156 throw e;
157 }
158 }
159 }, $fz.isPrivate = true, $fz));
160 Clazz.defineMethod (c$, "writeEnd", 
161 ($fz = function () {
162 this.writeInt4 (0);
163 this.startPos = this.bytePos;
164 this.writeString ("IEND");
165 this.writeCRC ();
166 }, $fz.isPrivate = true, $fz));
167 Clazz.defineStatics (c$,
168 "FILTER_NONE", 0,
169 "FILTER_SUB", 1,
170 "FILTER_UP", 2,
171 "FILTER_LAST", 2,
172 "PT_FIRST_TAG", 37);
173 });