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