57 import javax.imageio.spi.ImageReaderSpi;
58 import javax.imageio.stream.ImageInputStream;
59 import javax.imageio.event.IIOReadProgressListener;
60 import javax.imageio.event.IIOReadUpdateListener;
61 import javax.imageio.event.IIOReadWarningListener;
62
63 import java.io.*;
64 import java.nio.*;
65 import java.security.AccessController;
66 import java.security.PrivilegedAction;
67 import java.util.ArrayList;
68 import java.util.Iterator;
69 import java.util.StringTokenizer;
70
71 import com.sun.imageio.plugins.common.ImageUtil;
72 import com.sun.imageio.plugins.common.I18N;
73
74 /** This class is the Java Image IO plugin reader for BMP images.
75 * It may subsample the image, clip the image, select sub-bands,
76 * and shift the decoded image origin if the proper decoding parameter
77 * are set in the provided <code>ImageReadParam</code>.
78 *
79 * This class supports Microsoft Windows Bitmap Version 3-5,
80 * as well as OS/2 Bitmap Version 2.x (for single-image BMP file).
81 */
82 public class BMPImageReader extends ImageReader implements BMPConstants {
83 // BMP Image types
84 private static final int VERSION_2_1_BIT = 0;
85 private static final int VERSION_2_4_BIT = 1;
86 private static final int VERSION_2_8_BIT = 2;
87 private static final int VERSION_2_24_BIT = 3;
88
89 private static final int VERSION_3_1_BIT = 4;
90 private static final int VERSION_3_4_BIT = 5;
91 private static final int VERSION_3_8_BIT = 6;
92 private static final int VERSION_3_24_BIT = 7;
93
94 private static final int VERSION_3_NT_16_BIT = 8;
95 private static final int VERSION_3_NT_32_BIT = 9;
96
97 private static final int VERSION_4_1_BIT = 10;
142 /** The metadata from the stream. */
143 private BMPMetadata metadata;
144
145 /** The destination image. */
146 private BufferedImage bi;
147
148 /** Indicates whether subsampled, subregion is required, and offset is
149 * defined
150 */
151 private boolean noTransform = true;
152
153 /** Indicates whether subband is selected. */
154 private boolean seleBand = false;
155
156 /** The scaling factors. */
157 private int scaleX, scaleY;
158
159 /** source and destination bands. */
160 private int[] sourceBands, destBands;
161
162 /** Constructs <code>BMPImageReader</code> from the provided
163 * <code>ImageReaderSpi</code>.
164 */
165 public BMPImageReader(ImageReaderSpi originator) {
166 super(originator);
167 }
168
169 /** Overrides the method defined in the superclass. */
170 public void setInput(Object input,
171 boolean seekForwardOnly,
172 boolean ignoreMetadata) {
173 super.setInput(input, seekForwardOnly, ignoreMetadata);
174 iis = (ImageInputStream) input; // Always works
175 if(iis != null)
176 iis.setByteOrder(ByteOrder.LITTLE_ENDIAN);
177 resetHeaderInfo();
178 }
179
180 /** Overrides the method defined in the superclass. */
181 public int getNumImages(boolean allowSearch) throws IOException {
182 if (iis == null) {
183 throw new IllegalStateException(I18N.getString("GetNumImages0"));
1653 // Encoded mode
1654 int alternate[] = { (values[count] & 0xf0) >> 4,
1655 values[count] & 0x0f };
1656 for (int i=0; (i < value) && (l < width); i++) {
1657 val[l++] = (byte)alternate[i & 1];
1658 }
1659
1660 count++;
1661 }
1662
1663 // If End-of-RLE data, then exit the while loop
1664 if (flag) {
1665 break;
1666 }
1667 }
1668 }
1669
1670 /** Decodes the jpeg/png image embedded in the bitmap using any jpeg
1671 * ImageIO-style plugin.
1672 *
1673 * @param bi The destination <code>BufferedImage</code>.
1674 * @param bmpParam The <code>ImageReadParam</code> for decoding this
1675 * BMP image. The parameters for subregion, band selection and
1676 * subsampling are used in decoding the jpeg image.
1677 */
1678
1679 private BufferedImage readEmbedded(int type,
1680 BufferedImage bi, ImageReadParam bmpParam)
1681 throws IOException {
1682 String format;
1683 switch(type) {
1684 case BI_JPEG:
1685 format = "JPEG";
1686 break;
1687 case BI_PNG:
1688 format = "PNG";
1689 break;
1690 default:
1691 throw new
1692 IOException("Unexpected compression type: " + type);
1693 }
1694 ImageReader reader =
|
57 import javax.imageio.spi.ImageReaderSpi;
58 import javax.imageio.stream.ImageInputStream;
59 import javax.imageio.event.IIOReadProgressListener;
60 import javax.imageio.event.IIOReadUpdateListener;
61 import javax.imageio.event.IIOReadWarningListener;
62
63 import java.io.*;
64 import java.nio.*;
65 import java.security.AccessController;
66 import java.security.PrivilegedAction;
67 import java.util.ArrayList;
68 import java.util.Iterator;
69 import java.util.StringTokenizer;
70
71 import com.sun.imageio.plugins.common.ImageUtil;
72 import com.sun.imageio.plugins.common.I18N;
73
74 /** This class is the Java Image IO plugin reader for BMP images.
75 * It may subsample the image, clip the image, select sub-bands,
76 * and shift the decoded image origin if the proper decoding parameter
77 * are set in the provided {@code ImageReadParam}.
78 *
79 * This class supports Microsoft Windows Bitmap Version 3-5,
80 * as well as OS/2 Bitmap Version 2.x (for single-image BMP file).
81 */
82 public class BMPImageReader extends ImageReader implements BMPConstants {
83 // BMP Image types
84 private static final int VERSION_2_1_BIT = 0;
85 private static final int VERSION_2_4_BIT = 1;
86 private static final int VERSION_2_8_BIT = 2;
87 private static final int VERSION_2_24_BIT = 3;
88
89 private static final int VERSION_3_1_BIT = 4;
90 private static final int VERSION_3_4_BIT = 5;
91 private static final int VERSION_3_8_BIT = 6;
92 private static final int VERSION_3_24_BIT = 7;
93
94 private static final int VERSION_3_NT_16_BIT = 8;
95 private static final int VERSION_3_NT_32_BIT = 9;
96
97 private static final int VERSION_4_1_BIT = 10;
142 /** The metadata from the stream. */
143 private BMPMetadata metadata;
144
145 /** The destination image. */
146 private BufferedImage bi;
147
148 /** Indicates whether subsampled, subregion is required, and offset is
149 * defined
150 */
151 private boolean noTransform = true;
152
153 /** Indicates whether subband is selected. */
154 private boolean seleBand = false;
155
156 /** The scaling factors. */
157 private int scaleX, scaleY;
158
159 /** source and destination bands. */
160 private int[] sourceBands, destBands;
161
162 /** Constructs {@code BMPImageReader} from the provided
163 * {@code ImageReaderSpi}.
164 */
165 public BMPImageReader(ImageReaderSpi originator) {
166 super(originator);
167 }
168
169 /** Overrides the method defined in the superclass. */
170 public void setInput(Object input,
171 boolean seekForwardOnly,
172 boolean ignoreMetadata) {
173 super.setInput(input, seekForwardOnly, ignoreMetadata);
174 iis = (ImageInputStream) input; // Always works
175 if(iis != null)
176 iis.setByteOrder(ByteOrder.LITTLE_ENDIAN);
177 resetHeaderInfo();
178 }
179
180 /** Overrides the method defined in the superclass. */
181 public int getNumImages(boolean allowSearch) throws IOException {
182 if (iis == null) {
183 throw new IllegalStateException(I18N.getString("GetNumImages0"));
1653 // Encoded mode
1654 int alternate[] = { (values[count] & 0xf0) >> 4,
1655 values[count] & 0x0f };
1656 for (int i=0; (i < value) && (l < width); i++) {
1657 val[l++] = (byte)alternate[i & 1];
1658 }
1659
1660 count++;
1661 }
1662
1663 // If End-of-RLE data, then exit the while loop
1664 if (flag) {
1665 break;
1666 }
1667 }
1668 }
1669
1670 /** Decodes the jpeg/png image embedded in the bitmap using any jpeg
1671 * ImageIO-style plugin.
1672 *
1673 * @param bi The destination {@code BufferedImage}.
1674 * @param bmpParam The {@code ImageReadParam} for decoding this
1675 * BMP image. The parameters for subregion, band selection and
1676 * subsampling are used in decoding the jpeg image.
1677 */
1678
1679 private BufferedImage readEmbedded(int type,
1680 BufferedImage bi, ImageReadParam bmpParam)
1681 throws IOException {
1682 String format;
1683 switch(type) {
1684 case BI_JPEG:
1685 format = "JPEG";
1686 break;
1687 case BI_PNG:
1688 format = "PNG";
1689 break;
1690 default:
1691 throw new
1692 IOException("Unexpected compression type: " + type);
1693 }
1694 ImageReader reader =
|