< prev index next >

src/java.desktop/share/classes/com/sun/imageio/plugins/bmp/BMPImageReader.java

Print this page




 161      */
 162     private boolean noTransform = true;
 163 
 164     /** Indicates whether subband is selected. */
 165     private boolean seleBand = false;
 166 
 167     /** The scaling factors. */
 168     private int scaleX, scaleY;
 169 
 170     /** source and destination bands. */
 171     private int[] sourceBands, destBands;
 172 
 173     /** Constructs {@code BMPImageReader} from the provided
 174      *  {@code ImageReaderSpi}.
 175      */
 176     public BMPImageReader(ImageReaderSpi originator) {
 177         super(originator);
 178     }
 179 
 180     /** Overrides the method defined in the superclass. */

 181     public void setInput(Object input,
 182                          boolean seekForwardOnly,
 183                          boolean ignoreMetadata) {
 184         super.setInput(input, seekForwardOnly, ignoreMetadata);
 185         iis = (ImageInputStream) input; // Always works
 186         if(iis != null)
 187             iis.setByteOrder(ByteOrder.LITTLE_ENDIAN);
 188         resetHeaderInfo();
 189     }
 190 
 191     /** Overrides the method defined in the superclass. */

 192     public int getNumImages(boolean allowSearch) throws IOException {
 193         if (iis == null) {
 194             throw new IllegalStateException(I18N.getString("GetNumImages0"));
 195         }
 196         if (seekForwardOnly && allowSearch) {
 197             throw new IllegalStateException(I18N.getString("GetNumImages1"));
 198         }
 199         return 1;
 200     }
 201 
 202     @Override
 203     public int getWidth(int imageIndex) throws IOException {
 204         checkIndex(imageIndex);
 205         try {
 206             readHeader();
 207         } catch (IllegalArgumentException e) {
 208             throw new IIOException(I18N.getString("BMPImageReader6"), e);
 209         }
 210         return width;
 211     }
 212 

 213     public int getHeight(int imageIndex) throws IOException {
 214         checkIndex(imageIndex);
 215         try {
 216             readHeader();
 217         } catch (IllegalArgumentException e) {
 218             throw new IIOException(I18N.getString("BMPImageReader6"), e);
 219         }
 220         return height;
 221     }
 222 
 223     private void checkIndex(int imageIndex) {
 224         if (imageIndex != 0) {
 225             throw new IndexOutOfBoundsException(I18N.getString("BMPImageReader0"));
 226         }
 227     }
 228 
 229     /**
 230      * Process the image header.
 231      *
 232      * @exception IllegalStateException if source stream is not set.


 745                                                 numBands,
 746                                                 numBands * width,
 747                                                 bandOffsets);
 748 
 749             colorModel =
 750                 ImageUtil.createColorModel(colorSpace, sampleModel);
 751         }
 752 
 753         originalSampleModel = sampleModel;
 754         originalColorModel = colorModel;
 755 
 756         // Reset to the start of bitmap; then jump to the
 757         //start of image data
 758         iis.reset();
 759         iis.skipBytes(bitmapOffset);
 760         bitmapStart = iis.getStreamPosition();
 761 
 762         gotHeader = true;
 763     }
 764 

 765     public Iterator<ImageTypeSpecifier> getImageTypes(int imageIndex)
 766       throws IOException {
 767         checkIndex(imageIndex);
 768         try {
 769             readHeader();
 770         } catch (IllegalArgumentException e) {
 771             throw new IIOException(I18N.getString("BMPImageReader6"), e);
 772         }
 773         ArrayList<ImageTypeSpecifier> list = new ArrayList<>(1);
 774         list.add(new ImageTypeSpecifier(originalColorModel,
 775                                         originalSampleModel));
 776         return list.iterator();
 777     }
 778 

 779     public ImageReadParam getDefaultReadParam() {
 780         return new ImageReadParam();
 781     }
 782 

 783     public IIOMetadata getImageMetadata(int imageIndex)
 784       throws IOException {
 785         checkIndex(imageIndex);
 786         if (metadata == null) {
 787             try {
 788                 readHeader();
 789             } catch (IllegalArgumentException e) {
 790                 throw new IIOException(I18N.getString("BMPImageReader6"), e);
 791             }
 792         }
 793         return metadata;
 794     }
 795 

 796     public IIOMetadata getStreamMetadata() throws IOException {
 797         return null;
 798     }
 799 

 800     public boolean isRandomAccessEasy(int imageIndex) throws IOException {
 801         checkIndex(imageIndex);
 802         try {
 803             readHeader();
 804         } catch (IllegalArgumentException e) {
 805             throw new IIOException(I18N.getString("BMPImageReader6"), e);
 806         }
 807         return metadata.compression == BI_RGB;
 808     }
 809 

 810     public BufferedImage read(int imageIndex, ImageReadParam param)
 811         throws IOException {
 812 
 813         if (iis == null) {
 814             throw new IllegalStateException(I18N.getString("BMPImageReader5"));
 815         }
 816 
 817         checkIndex(imageIndex);
 818         clearAbortRequest();
 819         processImageStarted(imageIndex);
 820         if (abortRequested()) {
 821             processReadAborted();
 822             return bi;
 823         }
 824 
 825         if (param == null)
 826             param = getDefaultReadParam();
 827 
 828         //read header
 829         try {


1030 
1031         case VERSION_3_EXT_24_BIT:
1032         case VERSION_4_24_BIT:
1033             read24Bit(bdata);
1034             break;
1035 
1036         case VERSION_3_EXT_32_BIT:
1037         case VERSION_4_32_BIT:
1038             read32Bit(idata);
1039             break;
1040         }
1041 
1042         if (abortRequested())
1043             processReadAborted();
1044         else
1045             processImageComplete();
1046 
1047         return bi;
1048     }
1049 

1050     public boolean canReadRaster() {
1051         return true;
1052     }
1053 

1054     public Raster readRaster(int imageIndex,
1055                              ImageReadParam param) throws IOException {
1056         BufferedImage bi = read(imageIndex, param);
1057         return bi.getData();
1058     }
1059 
1060     private void resetHeaderInfo() {
1061         gotHeader = false;
1062         bi = null;
1063         sampleModel = originalSampleModel = null;
1064         colorModel = originalColorModel = null;
1065     }
1066 

1067     public void reset() {
1068         super.reset();
1069         iis = null;
1070         resetHeaderInfo();
1071     }
1072 
1073     // Deal with 1 Bit images using IndexColorModels
1074     private void read1Bit(byte[] bdata) throws IOException {
1075         int bytesPerScanline = (width + 7) / 8;
1076         int padding = bytesPerScanline % 4;
1077         if (padding != 0) {
1078             padding = 4 - padding;
1079         }
1080 
1081         int lineLength = bytesPerScanline + padding;
1082 
1083         if (noTransform) {
1084             int j = isBottomUp ? (height -1)*bytesPerScanline : 0;
1085 
1086             for (int i=0; i<height; i++) {




 161      */
 162     private boolean noTransform = true;
 163 
 164     /** Indicates whether subband is selected. */
 165     private boolean seleBand = false;
 166 
 167     /** The scaling factors. */
 168     private int scaleX, scaleY;
 169 
 170     /** source and destination bands. */
 171     private int[] sourceBands, destBands;
 172 
 173     /** Constructs {@code BMPImageReader} from the provided
 174      *  {@code ImageReaderSpi}.
 175      */
 176     public BMPImageReader(ImageReaderSpi originator) {
 177         super(originator);
 178     }
 179 
 180     /** Overrides the method defined in the superclass. */
 181     @Override
 182     public void setInput(Object input,
 183                          boolean seekForwardOnly,
 184                          boolean ignoreMetadata) {
 185         super.setInput(input, seekForwardOnly, ignoreMetadata);
 186         iis = (ImageInputStream) input; // Always works
 187         if(iis != null)
 188             iis.setByteOrder(ByteOrder.LITTLE_ENDIAN);
 189         resetHeaderInfo();
 190     }
 191 
 192     /** Overrides the method defined in the superclass. */
 193     @Override
 194     public int getNumImages(boolean allowSearch) throws IOException {
 195         if (iis == null) {
 196             throw new IllegalStateException(I18N.getString("GetNumImages0"));
 197         }
 198         if (seekForwardOnly && allowSearch) {
 199             throw new IllegalStateException(I18N.getString("GetNumImages1"));
 200         }
 201         return 1;
 202     }
 203 
 204     @Override
 205     public int getWidth(int imageIndex) throws IOException {
 206         checkIndex(imageIndex);
 207         try {
 208             readHeader();
 209         } catch (IllegalArgumentException e) {
 210             throw new IIOException(I18N.getString("BMPImageReader6"), e);
 211         }
 212         return width;
 213     }
 214 
 215     @Override
 216     public int getHeight(int imageIndex) throws IOException {
 217         checkIndex(imageIndex);
 218         try {
 219             readHeader();
 220         } catch (IllegalArgumentException e) {
 221             throw new IIOException(I18N.getString("BMPImageReader6"), e);
 222         }
 223         return height;
 224     }
 225 
 226     private void checkIndex(int imageIndex) {
 227         if (imageIndex != 0) {
 228             throw new IndexOutOfBoundsException(I18N.getString("BMPImageReader0"));
 229         }
 230     }
 231 
 232     /**
 233      * Process the image header.
 234      *
 235      * @exception IllegalStateException if source stream is not set.


 748                                                 numBands,
 749                                                 numBands * width,
 750                                                 bandOffsets);
 751 
 752             colorModel =
 753                 ImageUtil.createColorModel(colorSpace, sampleModel);
 754         }
 755 
 756         originalSampleModel = sampleModel;
 757         originalColorModel = colorModel;
 758 
 759         // Reset to the start of bitmap; then jump to the
 760         //start of image data
 761         iis.reset();
 762         iis.skipBytes(bitmapOffset);
 763         bitmapStart = iis.getStreamPosition();
 764 
 765         gotHeader = true;
 766     }
 767 
 768     @Override
 769     public Iterator<ImageTypeSpecifier> getImageTypes(int imageIndex)
 770       throws IOException {
 771         checkIndex(imageIndex);
 772         try {
 773             readHeader();
 774         } catch (IllegalArgumentException e) {
 775             throw new IIOException(I18N.getString("BMPImageReader6"), e);
 776         }
 777         ArrayList<ImageTypeSpecifier> list = new ArrayList<>(1);
 778         list.add(new ImageTypeSpecifier(originalColorModel,
 779                                         originalSampleModel));
 780         return list.iterator();
 781     }
 782 
 783     @Override
 784     public ImageReadParam getDefaultReadParam() {
 785         return new ImageReadParam();
 786     }
 787 
 788     @Override
 789     public IIOMetadata getImageMetadata(int imageIndex)
 790       throws IOException {
 791         checkIndex(imageIndex);
 792         if (metadata == null) {
 793             try {
 794                 readHeader();
 795             } catch (IllegalArgumentException e) {
 796                 throw new IIOException(I18N.getString("BMPImageReader6"), e);
 797             }
 798         }
 799         return metadata;
 800     }
 801 
 802     @Override
 803     public IIOMetadata getStreamMetadata() throws IOException {
 804         return null;
 805     }
 806 
 807     @Override
 808     public boolean isRandomAccessEasy(int imageIndex) throws IOException {
 809         checkIndex(imageIndex);
 810         try {
 811             readHeader();
 812         } catch (IllegalArgumentException e) {
 813             throw new IIOException(I18N.getString("BMPImageReader6"), e);
 814         }
 815         return metadata.compression == BI_RGB;
 816     }
 817 
 818     @Override
 819     public BufferedImage read(int imageIndex, ImageReadParam param)
 820         throws IOException {
 821 
 822         if (iis == null) {
 823             throw new IllegalStateException(I18N.getString("BMPImageReader5"));
 824         }
 825 
 826         checkIndex(imageIndex);
 827         clearAbortRequest();
 828         processImageStarted(imageIndex);
 829         if (abortRequested()) {
 830             processReadAborted();
 831             return bi;
 832         }
 833 
 834         if (param == null)
 835             param = getDefaultReadParam();
 836 
 837         //read header
 838         try {


1039 
1040         case VERSION_3_EXT_24_BIT:
1041         case VERSION_4_24_BIT:
1042             read24Bit(bdata);
1043             break;
1044 
1045         case VERSION_3_EXT_32_BIT:
1046         case VERSION_4_32_BIT:
1047             read32Bit(idata);
1048             break;
1049         }
1050 
1051         if (abortRequested())
1052             processReadAborted();
1053         else
1054             processImageComplete();
1055 
1056         return bi;
1057     }
1058 
1059     @Override
1060     public boolean canReadRaster() {
1061         return true;
1062     }
1063 
1064     @Override
1065     public Raster readRaster(int imageIndex,
1066                              ImageReadParam param) throws IOException {
1067         BufferedImage bi = read(imageIndex, param);
1068         return bi.getData();
1069     }
1070 
1071     private void resetHeaderInfo() {
1072         gotHeader = false;
1073         bi = null;
1074         sampleModel = originalSampleModel = null;
1075         colorModel = originalColorModel = null;
1076     }
1077 
1078     @Override
1079     public void reset() {
1080         super.reset();
1081         iis = null;
1082         resetHeaderInfo();
1083     }
1084 
1085     // Deal with 1 Bit images using IndexColorModels
1086     private void read1Bit(byte[] bdata) throws IOException {
1087         int bytesPerScanline = (width + 7) / 8;
1088         int padding = bytesPerScanline % 4;
1089         if (padding != 0) {
1090             padding = 4 - padding;
1091         }
1092 
1093         int lineLength = bytesPerScanline + padding;
1094 
1095         if (noTransform) {
1096             int j = isBottomUp ? (height -1)*bytesPerScanline : 0;
1097 
1098             for (int i=0; i<height; i++) {


< prev index next >