📄 bmpimagereader.java
字号:
// If the destination band is set used it sourceBands = param.getSourceBands(); destBands = param.getDestinationBands(); seleBand = (sourceBands != null) && (destBands != null); noTransform = destinationRegion.equals(new Rectangle(0, 0, width, height)) || seleBand; if (!seleBand) { sourceBands = new int[numBands]; destBands = new int[numBands]; for (int i = 0; i < numBands; i++) destBands[i] = sourceBands[i] = i; } // If the destination is provided, then use it. Otherwise, create new one bi = param.getDestination(); // Get the image data. WritableRaster raster = null; if (bi == null) { if (sampleModel != null && colorModel != null) { sampleModel = sampleModel.createCompatibleSampleModel(destinationRegion.x + destinationRegion.width, destinationRegion.y + destinationRegion.height); if (seleBand) sampleModel = sampleModel.createSubsetSampleModel(sourceBands); raster = Raster.createWritableRaster(sampleModel, new Point()); bi = new BufferedImage(colorModel, raster, false, null); } } else { raster = bi.getWritableTile(0, 0); sampleModel = bi.getSampleModel(); colorModel = bi.getColorModel(); noTransform &= destinationRegion.equals(raster.getBounds()); } byte bdata[] = null; // buffer for byte data short sdata[] = null; // buffer for short data int idata[] = null; // buffer for int data // the sampleModel can be null in case of embedded image if (sampleModel != null) { if (sampleModel.getDataType() == DataBuffer.TYPE_BYTE) bdata = (byte[]) ((DataBufferByte)raster.getDataBuffer()).getData(); else if (sampleModel.getDataType() == DataBuffer.TYPE_USHORT) sdata = (short[]) ((DataBufferUShort)raster.getDataBuffer()).getData(); else if (sampleModel.getDataType() == DataBuffer.TYPE_INT) idata = (int[]) ((DataBufferInt)raster.getDataBuffer()).getData(); } // There should only be one tile. switch(imageType) { case VERSION_2_1_BIT: // no compression read1Bit(bdata); break; case VERSION_2_4_BIT: // no compression read4Bit(bdata); break; case VERSION_2_8_BIT: // no compression read8Bit(bdata); break; case VERSION_2_24_BIT: // no compression read24Bit(bdata); break; case VERSION_3_1_BIT: // 1-bit images cannot be compressed. read1Bit(bdata); break; case VERSION_3_4_BIT: switch((int)compression) { case BI_RGB: read4Bit(bdata); break; case BI_RLE4: readRLE4(bdata); break; default: throw new RuntimeException(I18N.getString("BMPImageReader1")); } break; case VERSION_3_8_BIT: switch((int)compression) { case BI_RGB: read8Bit(bdata); break; case BI_RLE8: readRLE8(bdata); break; default: throw new RuntimeException(I18N.getString("BMPImageReader1")); } break; case VERSION_3_24_BIT: // 24-bit images are not compressed read24Bit(bdata); break; case VERSION_3_NT_16_BIT: read16Bit(sdata); break; case VERSION_3_NT_32_BIT: read32Bit(idata); break; case VERSION_3_XP_EMBEDDED: case VERSION_4_XP_EMBEDDED: case VERSION_5_XP_EMBEDDED: bi = readEmbedded((int)compression, bi, param); break; case VERSION_4_1_BIT: read1Bit(bdata); break; case VERSION_4_4_BIT: switch((int)compression) { case BI_RGB: read4Bit(bdata); break; case BI_RLE4: readRLE4(bdata); break; default: throw new RuntimeException(I18N.getString("BMPImageReader1")); } case VERSION_4_8_BIT: switch((int)compression) { case BI_RGB: read8Bit(bdata); break; case BI_RLE8: readRLE8(bdata); break; default: throw new RuntimeException(I18N.getString("BMPImageReader1")); } break; case VERSION_4_16_BIT: read16Bit(sdata); break; case VERSION_4_24_BIT: read24Bit(bdata); break; case VERSION_4_32_BIT: read32Bit(idata); break; } if (abortRequested()) processReadAborted(); else processImageComplete(); return bi; } public boolean canReadRaster() { return true; } public Raster readRaster(int imageIndex, ImageReadParam param) throws IOException { BufferedImage bi = read(imageIndex, param); return bi.getData(); } private void resetHeaderInfo() { gotHeader = false; bi = null; sampleModel = originalSampleModel = null; colorModel = originalColorModel = null; } public void reset() { super.reset(); iis = null; resetHeaderInfo(); } // Deal with 1 Bit images using IndexColorModels private void read1Bit(byte[] bdata) throws IOException { int bytesPerScanline = (width + 7) / 8; int padding = bytesPerScanline % 4; if (padding != 0) { padding = 4 - padding; } int lineLength = bytesPerScanline + padding; if (noTransform) { int j = isBottomUp ? (height -1)*bytesPerScanline : 0; for (int i=0; i<height; i++) { if (abortRequested()) { break; } iis.readFully(bdata, j, bytesPerScanline); iis.skipBytes(padding); j += isBottomUp ? -bytesPerScanline : bytesPerScanline; processImageUpdate(bi, 0, i, destinationRegion.width, 1, 1, 1, new int[]{0}); processImageProgress(100.0F * i/destinationRegion.height); } } else { byte[] buf = new byte[lineLength]; int lineStride = ((MultiPixelPackedSampleModel)sampleModel).getScanlineStride(); if (isBottomUp) { int lastLine = sourceRegion.y + (destinationRegion.height - 1) * scaleY; iis.skipBytes(lineLength * (height - 1 - lastLine)); } else iis.skipBytes(lineLength * sourceRegion.y); int skipLength = lineLength * (scaleY - 1); // cache the values to avoid duplicated computation int[] srcOff = new int[destinationRegion.width]; int[] destOff = new int[destinationRegion.width]; int[] srcPos = new int[destinationRegion.width]; int[] destPos = new int[destinationRegion.width]; for (int i = destinationRegion.x, x = sourceRegion.x, j = 0; i < destinationRegion.x + destinationRegion.width; i++, j++, x += scaleX) { srcPos[j] = x >> 3; srcOff[j] = 7 - (x & 7); destPos[j] = i >> 3; destOff[j] = 7 - (i & 7); } int k = destinationRegion.y * lineStride; if (isBottomUp) k += (destinationRegion.height - 1) * lineStride; for (int j = 0, y = sourceRegion.y; j < destinationRegion.height; j++, y+=scaleY) { if (abortRequested()) break; iis.read(buf, 0, lineLength); for (int i = 0; i < destinationRegion.width; i++) { //get the bit and assign to the data buffer of the raster int v = (buf[srcPos[i]] >> srcOff[i]) & 1; bdata[k + destPos[i]] |= v << destOff[i]; } k += isBottomUp ? -lineStride : lineStride; iis.skipBytes(skipLength); processImageUpdate(bi, 0, j, destinationRegion.width, 1, 1, 1, new int[]{0}); processImageProgress(100.0F*j/destinationRegion.height); } } } // Method to read a 4 bit BMP image data private void read4Bit(byte[] bdata) throws IOException { int bytesPerScanline = (width + 1) / 2; // Padding bytes at the end of each scanline int padding = bytesPerScanline % 4; if (padding != 0) padding = 4 - padding; int lineLength = bytesPerScanline + padding; if (noTransform) { int j = isBottomUp ? (height -1) * bytesPerScanline : 0; for (int i=0; i<height; i++) { if (abortRequested()) { break; } iis.readFully(bdata, j, bytesPerScanline); iis.skipBytes(padding); j += isBottomUp ? -bytesPerScanline : bytesPerScanline; processImageUpdate(bi, 0, i, destinationRegion.width, 1, 1, 1, new int[]{0}); processImageProgress(100.0F * i/destinationRegion.height); } } else { byte[] buf = new byte[lineLength]; int lineStride = ((MultiPixelPackedSampleModel)sampleModel).getScanlineStride(); if (isBottomUp) { int lastLine = sourceRegion.y + (destinationRegion.height - 1) * scaleY; iis.skipBytes(lineLength * (height - 1 - lastLine)); } else iis.skipBytes(lineLength * sourceRegion.y); int skipLength = lineLength * (scaleY - 1); // cache the values to avoid duplicated computation int[] srcOff = new int[destinationRegion.width]; int[] destOff = new int[destinationRegion.width]; int[] srcPos = new int[destinationRegion.width]; int[] destPos = new int[destinationRegion.width];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -