JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / bin / javajs / img / BMPDecoder.js
1 Clazz.declarePackage ("javajs.img");\r
2 Clazz.load (null, "javajs.img.BMPDecoder", ["javajs.util.Rdr"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.bis = null;\r
5 this.temp = null;\r
6 Clazz.instantialize (this, arguments);\r
7 }, javajs.img, "BMPDecoder");\r
8 Clazz.makeConstructor (c$, \r
9 function () {\r
10 });\r
11 Clazz.defineMethod (c$, "decodeWindowsBMP", \r
12 function (bytes) {\r
13 try {\r
14 this.bis = javajs.util.Rdr.getBIS (bytes);\r
15 this.temp =  Clazz.newByteArray (4, 0);\r
16 if (this.readByte () != 66 || this.readByte () != 77) return null;\r
17 this.readInt ();\r
18 this.readShort ();\r
19 this.readShort ();\r
20 this.readInt ();\r
21 var imageWidth;\r
22 var imageHeight;\r
23 var bitsPerPixel;\r
24 var nColors = 0;\r
25 var imageSize = 0;\r
26 var headerSize = this.readInt ();\r
27 switch (headerSize) {\r
28 case 12:\r
29 imageWidth = this.readShort ();\r
30 imageHeight = this.readShort ();\r
31 this.readShort ();\r
32 bitsPerPixel = this.readShort ();\r
33 break;\r
34 case 40:\r
35 imageWidth = this.readInt ();\r
36 imageHeight = this.readInt ();\r
37 this.readShort ();\r
38 bitsPerPixel = this.readShort ();\r
39 var ncompression = this.readInt ();\r
40 if (ncompression != 0) {\r
41 System.out.println ("BMP Compression is :" + ncompression + " -- aborting");\r
42 return null;\r
43 }imageSize = this.readInt ();\r
44 this.readInt ();\r
45 this.readInt ();\r
46 nColors = this.readInt ();\r
47 this.readInt ();\r
48 break;\r
49 default:\r
50 System.out.println ("BMP Header unrecognized, length=" + headerSize + " -- aborting");\r
51 return null;\r
52 }\r
53 var isYReversed = (imageHeight < 0);\r
54 if (isYReversed) imageHeight = -imageHeight;\r
55 var nPixels = imageHeight * imageWidth;\r
56 var bytesPerPixel = Clazz.doubleToInt (bitsPerPixel / 8);\r
57 nColors = (nColors > 0 ? nColors : 1 << bitsPerPixel);\r
58 var npad = (bytesPerPixel == 4 ? 0 : imageSize == 0 ? 4 - (imageWidth % 4) : (Clazz.doubleToInt (imageSize / imageHeight)) - imageWidth * bytesPerPixel) % 4;\r
59 var palette;\r
60 var buf =  Clazz.newIntArray (nPixels, 0);\r
61 var dpt = (isYReversed ? imageWidth : -imageWidth);\r
62 var pt0 = (isYReversed ? 0 : nPixels + dpt);\r
63 var pt1 = (isYReversed ? nPixels : dpt);\r
64 switch (bitsPerPixel) {\r
65 case 32:\r
66 case 24:\r
67 for (var pt = pt0; pt != pt1; pt += dpt, this.pad (npad)) for (var i = 0; i < imageWidth; i++) buf[pt + i] = this.readColor (bytesPerPixel);\r
68 \r
69 \r
70 break;\r
71 case 8:\r
72 palette =  Clazz.newIntArray (nColors, 0);\r
73 for (var i = 0; i < nColors; i++) palette[i] = this.readColor (4);\r
74 \r
75 for (var pt = pt0; pt != pt1; pt += dpt, this.pad (npad)) for (var i = 0; i < imageWidth; i++) buf[pt + i] = palette[this.readByte ()];\r
76 \r
77 \r
78 break;\r
79 case 4:\r
80 npad = (4 - ((Clazz.doubleToInt ((imageWidth + 1) / 2)) % 4)) % 4;\r
81 palette =  Clazz.newIntArray (nColors, 0);\r
82 for (var i = 0; i < nColors; i++) palette[i] = this.readColor (4);\r
83 \r
84 var b4 = 0;\r
85 for (var pt = pt0; pt != pt1; pt += dpt, this.pad (npad)) for (var i = 0, shift = 4; i < imageWidth; i++, shift = 4 - shift) buf[pt + i] = palette[((shift == 4 ? (b4 = this.readByte ()) : b4) >> shift) & 0xF];\r
86 \r
87 \r
88 break;\r
89 case 1:\r
90 var color1 = this.readColor (3);\r
91 var color2 = this.readColor (3);\r
92 npad = (4 - ((Clazz.doubleToInt ((imageWidth + 7) / 8)) % 4)) % 4;\r
93 var b = 0;\r
94 for (var pt = pt0; pt != pt1; pt += dpt, this.pad (npad)) for (var i = 0, bpt = -1; i < imageWidth; i++, bpt--) {\r
95 if (bpt < 0) {\r
96 b = this.readByte ();\r
97 bpt = 7;\r
98 }buf[pt + i] = ((b & (1 << bpt)) == 0 ? color1 : color2);\r
99 }\r
100 \r
101 break;\r
102 case 64:\r
103 case 2:\r
104 default:\r
105 System.out.println ("Not a 32-, 24-, 8-, 4-, or 1-bit Windows Bitmap, aborting...");\r
106 return null;\r
107 }\r
108 return  Clazz.newArray (-1, [buf, Integer.$valueOf (imageWidth), Integer.$valueOf (imageHeight)]);\r
109 } catch (e) {\r
110 if (Clazz.exceptionOf (e, Exception)) {\r
111 System.out.println ("Caught exception in loadbitmap!");\r
112 } else {\r
113 throw e;\r
114 }\r
115 }\r
116 return null;\r
117 }, "~A");\r
118 Clazz.defineMethod (c$, "pad", \r
119 ($fz = function (npad) {\r
120 for (var i = 0; i < npad; i++) this.readByte ();\r
121 \r
122 return true;\r
123 }, $fz.isPrivate = true, $fz), "~N");\r
124 Clazz.defineMethod (c$, "readColor", \r
125 ($fz = function (n) {\r
126 this.bis.read (this.temp, 0, n);\r
127 return -16777216 | ((this.temp[2] & 0xff) << 16) | ((this.temp[1] & 0xff) << 8) | this.temp[0] & 0xff;\r
128 }, $fz.isPrivate = true, $fz), "~N");\r
129 Clazz.defineMethod (c$, "readInt", \r
130 ($fz = function () {\r
131 this.bis.read (this.temp, 0, 4);\r
132 return ((this.temp[3] & 0xff) << 24) | ((this.temp[2] & 0xff) << 16) | ((this.temp[1] & 0xff) << 8) | this.temp[0] & 0xff;\r
133 }, $fz.isPrivate = true, $fz));\r
134 Clazz.defineMethod (c$, "readShort", \r
135 ($fz = function () {\r
136 this.bis.read (this.temp, 0, 2);\r
137 return ((this.temp[1] & 0xff) << 8) | this.temp[0] & 0xff;\r
138 }, $fz.isPrivate = true, $fz));\r
139 Clazz.defineMethod (c$, "readByte", \r
140 ($fz = function () {\r
141 this.bis.read (this.temp, 0, 1);\r
142 return this.temp[0] & 0xff;\r
143 }, $fz.isPrivate = true, $fz));\r
144 });\r