1 51 package org.apache.fop.image; 52 53 import java.net.URL ; 55 import java.io.InputStream ; 56 import java.io.IOException ; 57 58 import org.apache.fop.datatypes.ColorSpace; 60 import org.apache.fop.image.analyser.ImageReader; 61 62 68 public class BmpImage extends AbstractFopImage { 69 public BmpImage(URL href) throws FopImageException { 70 super(href); 71 } 72 73 public BmpImage(URL href, 74 ImageReader imgReader) throws FopImageException { 75 super(href, imgReader); 76 } 77 78 protected void loadImage() throws FopImageException { 79 int wpos = 18; 80 int hpos = 22; int[] headermap = new int[54]; 82 int filepos = 0; 83 InputStream file = null; 84 byte palette[] = null; 85 try { 86 file = this.m_href.openStream(); 87 boolean eof = false; 88 while ((!eof) && (filepos < 54)) { 89 int input = file.read(); 90 if (input == -1) 91 eof = true; 92 else 93 headermap[filepos++] = input; 94 } 95 96 if (headermap[28] == 4 || headermap[28] == 8) { 97 int palettesize = 1 << headermap[28]; 98 palette = new byte[palettesize * 3]; 99 int countr = 0; 100 while (!eof && countr < palettesize) { 101 int count2 = 2; 102 while (!eof && count2 >= -1) { 103 int input = file.read(); 104 if (input == -1) 105 eof = true; 106 else if (count2 >= 0) { 107 palette[countr * 3 + count2] = (byte)(input 108 & 0xFF); 109 } 110 count2--; 111 filepos++; 112 } 113 countr++; 114 } 115 } 116 } catch (IOException e) { 117 throw new FopImageException("Error while loading image " 118 + this.m_href.toString() + " : " 119 + e.getClass() + " - " 120 + e.getMessage()); 121 } 122 this.m_width = headermap[wpos] + headermap[wpos + 1] * 256 124 + headermap[wpos + 2] * 256 * 256 125 + headermap[wpos + 3] * 256 * 256 * 256; 126 this.m_height = headermap[hpos] + headermap[hpos + 1] * 256 127 + headermap[hpos + 2] * 256 * 256 128 + headermap[hpos + 3] * 256 * 256 * 256; 129 130 int imagestart = headermap[10] + headermap[11] * 256 131 + headermap[12] * 256 * 256 132 + headermap[13] * 256 * 256 * 256; 133 this.m_bitsPerPixel = headermap[28]; 134 this.m_colorSpace = new ColorSpace(ColorSpace.DEVICE_RGB); 135 int bytes; 136 if (this.m_bitsPerPixel == 1) 137 bytes = (this.m_width + 7) / 8; 138 else if (this.m_bitsPerPixel == 24) 139 bytes = this.m_width * 3; 140 else if (this.m_bitsPerPixel == 4 || this.m_bitsPerPixel == 8) 141 bytes = this.m_width / (8 / this.m_bitsPerPixel); 142 else 143 throw new FopImageException("Image (" + this.m_href.toString() 144 + ") has " + this.m_bitsPerPixel 145 + " which is not a supported BMP format."); 146 if ((bytes & 0x03) != 0) { 147 bytes |= 0x03; 148 bytes++; 149 } 150 151 this.m_bitmapsSize = this.m_width * this.m_height * 3; 153 this.m_bitmaps = new byte[this.m_bitmapsSize]; 154 155 int[] temp = new int[bytes * this.m_height]; 156 try { 157 int input; 158 int count = 0; 159 file.skip((long)(imagestart - filepos)); 160 while ((input = file.read()) != -1) 161 temp[count++] = input; 162 file.close(); 163 } catch (IOException e) { 164 throw new FopImageException("Error while loading image " 165 + this.m_href.toString() + " : " 166 + e.getClass() + " - " 167 + e.getMessage()); 168 } 169 170 for (int i = 0; i < this.m_height; i++) { 171 int x = 0; 172 int j = 0; 173 while (j < bytes) { 174 int p = temp[(this.m_height - i - 1) * bytes + j]; 175 176 if (this.m_bitsPerPixel == 24 && x < this.m_width) { 177 int countr = 2; 178 do { 179 this.m_bitmaps[3 * (i * this.m_width + x) + countr] = 180 (byte)(temp[(this.m_height - i - 1) * bytes + j] 181 & 0xFF); 182 j++; 183 } while (--countr >= 0); 184 x++; 185 } else if (this.m_bitsPerPixel == 1) { 186 for (int countr = 0; countr < 8 && x < this.m_width; 187 countr++) { 188 if ((p & 0x80) != 0) { 189 this.m_bitmaps[3 * (i * this.m_width + x)] = 190 (byte)0xFF; 191 this.m_bitmaps[3 * (i * this.m_width + x) + 1] = 192 (byte)0xFF; 193 this.m_bitmaps[3 * (i * this.m_width + x) + 2] = 194 (byte)0xFF; 195 } else { 196 this.m_bitmaps[3 * (i * this.m_width + x)] = 197 (byte)0; 198 this.m_bitmaps[3 * (i * this.m_width + x) + 1] = 199 (byte)0; 200 this.m_bitmaps[3 * (i * this.m_width + x) + 2] = 201 (byte)0; 202 } 203 p <<= 1; 204 x++; 205 } 206 j++; 207 } else if (this.m_bitsPerPixel == 4) { 208 for (int countr = 0; countr < 2 && x < this.m_width; 209 countr++) { 210 int pal = ((p & 0xF0) >> 4) * 3; 211 this.m_bitmaps[3 * (i * this.m_width + x)] = 212 palette[pal]; 213 this.m_bitmaps[3 * (i * this.m_width + x) + 1] = 214 palette[pal + 1]; 215 this.m_bitmaps[3 * (i * this.m_width + x) + 2] = 216 palette[pal + 2]; 217 p <<= 4; 218 x++; 219 } 220 j++; 221 } else if (this.m_bitsPerPixel == 8) { 222 if (x < this.m_width) { 223 p *= 3; 224 this.m_bitmaps[3 * (i * this.m_width + x)] = 225 palette[p]; 226 this.m_bitmaps[3 * (i * this.m_width + x) + 1] = 227 palette[p + 1]; 228 this.m_bitmaps[3 * (i * this.m_width + x) + 2] = 229 palette[p + 2]; 230 j++; 231 x++; 232 } else 233 j = bytes; 234 } else 235 j++; 236 } 237 } 238 239 this.m_bitsPerPixel = 8; 243 } 244 245 } 246 | Popular Tags |