1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.javafx.iio.bmp;
  27 
  28 import com.sun.javafx.iio.*;
  29 import com.sun.javafx.iio.common.*;
  30 import java.io.*;
  31 import java.nio.ByteBuffer;
  32 
  33 final class BMPDescriptor extends ImageDescriptor {
  34 
  35     static final String formatName = "BMP";
  36     static final String extensions[] = { "bmp" };
  37     static final Signature signatures[] = {new Signature((byte)0x42, (byte)0x4D)};
  38     static final ImageDescriptor theInstance = new BMPDescriptor();
  39 
  40     private BMPDescriptor() {
  41         super(formatName, extensions, signatures);
  42     }
  43 }
  44 
  45 // the difference of LEInputStream from DataInputStream is Endianness
  46 final class LEInputStream {
  47 
  48     final public InputStream in;
  49 
  50     LEInputStream(InputStream is) {
  51         in = is;
  52     }
  53 
  54     public final short readShort() throws IOException {
  55         int ch1 = in.read();
  56         int ch2 = in.read();
  57         if ((ch1 | ch2) < 0) {
  58             throw new EOFException();
  59         }
  60         return (short)((ch2 << 8) + ch1);
  61     }
  62 
  63     public final int readInt() throws IOException {
  64         int ch1 = in.read();
  65         int ch2 = in.read();
  66         int ch3 = in.read();
  67         int ch4 = in.read();
  68         if ((ch1 | ch2 | ch3 | ch4) < 0) {
  69             throw new EOFException();
  70         }
  71         return ((ch4 << 24) + (ch3 << 16) + (ch2 << 8) + ch1);
  72     }
  73 
  74     public final void skipBytes(int n) throws IOException {
  75         int m = (int)in.skip(n);
  76         if (m < n) {
  77             throw new EOFException();
  78         }
  79     }
  80 }
  81 
  82 final class BitmapInfoHeader {
  83 
  84     static final int BIH_SIZE = 40;
  85     static final int BIH4_SIZE = 108;
  86     static final int BIH5_SIZE = 124;
  87 
  88     final int    biSize;
  89     final int    biWidth;
  90     final int    biHeight;
  91     final short  biPlanes;
  92     final short  biBitCount;
  93     final int    biCompression;
  94     final int    biSizeImage;
  95     final int    biXPelsPerMeter;
  96     final int    biYPelsPerMeter;
  97     final int    biClrUsed;
  98     final int    biClrImportant;
  99 
 100     BitmapInfoHeader(LEInputStream data) throws IOException {
 101         biSize = data.readInt();
 102         biWidth = data.readInt();
 103         biHeight = data.readInt();
 104         biPlanes = data.readShort();
 105         biBitCount = data.readShort();
 106         biCompression = data.readInt();
 107         biSizeImage = data.readInt();
 108         biXPelsPerMeter = data.readInt();
 109         biYPelsPerMeter = data.readInt();
 110         biClrUsed = data.readInt();
 111         biClrImportant = data.readInt();
 112 
 113         if (biSize > BIH_SIZE) {
 114             if (biSize == BIH4_SIZE || biSize == BIH5_SIZE) {
 115                 data.skipBytes(biSize - BIH_SIZE);
 116             } else {
 117                 throw new IOException("BitmapInfoHeader is corrupt");
 118             }
 119         }
 120         validate();
 121     }
 122 
 123     void validate() {
 124         if (biCompression != 0 || biPlanes != 1 || biBitCount != 24) {
 125             throw new RuntimeException(
 126                     "Unsupported BMP image: " +
 127                     "only 24 bit uncompressed BMP`s is supported");
 128         }
 129     }
 130 }
 131 
 132 final class BMPImageLoader extends ImageLoaderImpl {
 133 
 134     static final short BM = 0x4D42;
 135     static final int BFH_SIZE = 14;
 136 
 137     final LEInputStream data;
 138 
 139     short bfType; // must be equal to BM
 140     int   bfSize;
 141     int   bfOffBits;
 142     int   bgra_palette[];
 143     BitmapInfoHeader bih;
 144 
 145     BMPImageLoader(InputStream input) throws IOException {
 146         super(BMPDescriptor.theInstance);
 147         data = new LEInputStream(input);
 148         bfType = data.readShort();
 149         if (isValid()) {
 150             readHeader();
 151         }
 152     }
 153 
 154     private void readHeader() throws IOException {
 155         bfSize = data.readInt();
 156         data.skipBytes(4); // 32  bits reserved
 157         bfOffBits = data.readInt();
 158         bih = new BitmapInfoHeader(data);
 159         if (bih.biSize + BFH_SIZE != bfOffBits) {
 160             data.skipBytes(bfOffBits - bih.biSize - BFH_SIZE);
 161         }
 162     }
 163 
 164     private boolean isValid() {
 165         return bfType == BM;
 166     }
 167 
 168     public void dispose() { }
 169 
 170     static void GBRtoRGB(byte data[], int pos, int size) {
 171         for (int sz = size / 3; sz != 0; --sz) {
 172             byte x = data[pos], y = data[pos + 2];
 173             data[pos + 2] = x; data[pos] = y;
 174             pos += 3;
 175         }
 176     }
 177 
 178     public ImageFrame load(int imageIndex, int width, int height,
 179             boolean preserveAspectRatio, boolean smooth) throws IOException
 180 {
 181         if (0 != imageIndex) {
 182             return null;
 183         }
 184 
 185         if ((width > 0 && width != bih.biWidth) ||
 186             (height > 0 && height != bih.biHeight))
 187         {
 188             throw new RuntimeException("scaling for BMP is not supported");
 189         }
 190 
 191         // Pass image metadata to any listeners.
 192         ImageMetadata imageMetadata = new ImageMetadata(null, Boolean.TRUE,
 193             null, null, null, null, bih.biWidth, bih.biHeight,
 194             null, null, null);
 195         updateImageMetadata(imageMetadata);
 196 
 197         int bmpStride = (bih.biBitCount*bih.biWidth/8 + 3) & ~3;
 198         int rowLength = (bih.biBitCount/8)*bih.biWidth;
 199 
 200         int hght = Math.abs(bih.biHeight);
 201 
 202         byte image[] = new byte[rowLength * hght];
 203 
 204         for (int i = 0; i != hght; ++i) {
 205             int line = bih.biHeight < 0 ? i : hght-i-1;
 206             int nRead = data.in.read(image, line * rowLength, rowLength);
 207             GBRtoRGB(image, line * rowLength, nRead);
 208 
 209             if (nRead != rowLength) {
 210                 break;
 211             }
 212 
 213             if (nRead < bmpStride) {
 214                 data.skipBytes(bmpStride-nRead);
 215             }
 216         }
 217 
 218         return new ImageFrame(ImageStorage.ImageType.RGB, ByteBuffer.wrap(image),
 219                 bih.biWidth, hght, rowLength, null, null);
 220     }
 221 }
 222 
 223 public final class BMPImageLoaderFactory implements ImageLoaderFactory {
 224 
 225     private static final BMPImageLoaderFactory theInstance =
 226             new BMPImageLoaderFactory();
 227 
 228     public static ImageLoaderFactory getInstance() {
 229         return theInstance;
 230     }
 231 
 232     public ImageFormatDescription getFormatDescription() {
 233         return BMPDescriptor.theInstance;
 234     }
 235 
 236     public ImageLoader createImageLoader(InputStream input) throws IOException {
 237         return new BMPImageLoader(input);
 238     }
 239 }
 240