📄 bmpimagereader.java
字号:
for (int i = destinationRegion.x, x = sourceRegion.x, j = 0; i < destinationRegion.x + destinationRegion.width; i++, j++, x += scaleX) { srcPos[j] = x >> 1; srcOff[j] = (1 - (x & 1)) << 2; destPos[j] = i >> 1; destOff[j] = (1 - (i & 1)) << 2; } 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]) & 0x0F; 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 8 bit BMP image data private void read8Bit(byte[] bdata) throws IOException { // Padding bytes at the end of each scanline int padding = width % 4; if (padding != 0) { padding = 4 - padding; } int lineLength = width + padding; if (noTransform) { int j = isBottomUp ? (height -1) * width : 0; for (int i=0; i<height; i++) { if (abortRequested()) { break; } iis.readFully(bdata, j, width); iis.skipBytes(padding); j += isBottomUp ? -width : width; 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 = ((ComponentSampleModel)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); int k = destinationRegion.y * lineStride; if (isBottomUp) k += (destinationRegion.height - 1) * lineStride; k += destinationRegion.x; 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, m = sourceRegion.x; i < destinationRegion.width; i++, m += scaleX) { //get the bit and assign to the data buffer of the raster bdata[k + i] = buf[m]; } 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 24 bit BMP image data private void read24Bit(byte[] bdata) throws IOException { // Padding bytes at the end of each scanline // width * bitsPerPixel should be divisible by 32 int padding = width * 3 % 4; if ( padding != 0) padding = 4 - padding; int lineStride = width * 3; int lineLength = lineStride + padding; if (noTransform) { int j = isBottomUp ? (height -1) * width * 3 : 0; for (int i=0; i<height; i++) { if (abortRequested()) { break; } iis.readFully(bdata, j, lineStride); iis.skipBytes(padding); j += isBottomUp ? -lineStride : lineStride; processImageUpdate(bi, 0, i, destinationRegion.width, 1, 1, 1, new int[]{0}); processImageProgress(100.0F * i/destinationRegion.height); } } else { byte[] buf = new byte[lineLength]; lineStride = ((ComponentSampleModel)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); int k = destinationRegion.y * lineStride; if (isBottomUp) k += (destinationRegion.height - 1) * lineStride; k += destinationRegion.x * 3; 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, m = 3 * sourceRegion.x; i < destinationRegion.width; i++, m += 3 * scaleX) { //get the bit and assign to the data buffer of the raster int n = 3 * i + k; for (int b = 0; b < destBands.length; b++) bdata[n + destBands[b]] = buf[m + sourceBands[b]]; } 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); } } } private void read16Bit(short sdata[]) throws IOException { // Padding bytes at the end of each scanline // width * bitsPerPixel should be divisible by 32 int padding = width * 2 % 4; if ( padding != 0) padding = 4 - padding; int lineLength = width + padding / 2; if (noTransform) { int j = isBottomUp ? (height -1) * width : 0; for (int i=0; i<height; i++) { if (abortRequested()) { break; } iis.readFully(sdata, j, width); iis.skipBytes(padding); j += isBottomUp ? -width : width; processImageUpdate(bi, 0, i, destinationRegion.width, 1, 1, 1, new int[]{0}); processImageProgress(100.0F * i/destinationRegion.height); } } else { short[] buf = new short[lineLength]; int lineStride = ((SinglePixelPackedSampleModel)sampleModel).getScanlineStride(); if (isBottomUp) { int lastLine = sourceRegion.y + (destinationRegion.height - 1) * scaleY; iis.skipBytes(lineLength * (height - 1 - lastLine) << 1); } else iis.skipBytes(lineLength * sourceRegion.y << 1); int skipLength = lineLength * (scaleY - 1) << 1; int k = destinationRegion.y * lineStride; if (isBottomUp) k += (destinationRegion.height - 1) * lineStride; k += destinationRegion.x; for (int j = 0, y = sourceRegion.y; j < destinationRegion.height; j++, y+=scaleY) { if (abortRequested()) break; iis.readFully(buf, 0, lineLength); for (int i = 0, m = sourceRegion.x; i < destinationRegion.width; i++, m += scaleX) { //get the bit and assign to the data buffer of the raster sdata[k + i] = buf[m]; } 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); } } } private void read32Bit(int idata[]) throws IOException { if (noTransform) { int j = isBottomUp ? (height -1) * width : 0; for (int i=0; i<height; i++) { if (abortRequested()) { break; } iis.readFully(idata, j, width); j += isBottomUp ? -width : width; processImageUpdate(bi, 0, i, destinationRegion.width, 1, 1, 1, new int[]{0}); processImageProgress(100.0F * i/destinationRegion.height); } } else { int[] buf = new int[width]; int lineStride = ((SinglePixelPackedSampleModel)sampleModel).getScanlineStride(); if (isBottomUp) { int lastLine = sourceRegion.y + (destinationRegion.height - 1) * scaleY; iis.skipBytes(width * (height - 1 - lastLine) << 2); } else iis.skipBytes(width * sourceRegion.y << 2); int skipLength = width * (scaleY - 1) << 2; int k = destinationRegion.y * lineStride; if (isBottomUp) k += (destinationRegion.height - 1) * lineStride; k += destinationRegion.x; for (int j = 0, y = sourceRegion.y; j < destinationRegion.height; j++, y+=scaleY) { if (abortRequested()) break; iis.readFully(buf, 0, width); for (int i = 0, m = sourceRegion.x; i < destinationRegion.width; i++, m += scaleX) { //get the bit and assign to the data buffer of the raster idata[k + i] = buf[m]; } 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); } } } private void readRLE8(byte bdata[]) throws IOException { // If imageSize field is not provided, calculate it. int imSize = (int)imageSize; if (imSize == 0) { imSize = (int)(bitmapFileSize - bitmapOffset); } int padding = 0; // If width is not 32 bit aligned, then while uncompressing each // scanline will have padding bytes, calculate the amount of padding int remainder = width % 4; if (remainder != 0) { padding = 4 - remainder; } // Read till we have the whole image byte values[] = new byte[imSize]; int bytesRead = 0; iis.readFully(values, 0, imSize); // Since data is compressed, decompress it decodeRLE8(imSize, padding, values, bdata); } private void decodeRLE8(int imSize, int padding, byte[] values, byte[] bdata) throws IOException { byte val[] = new byte[width * height]; int count = 0, l = 0; int value; boolean flag = false; int lineNo = isBottomUp ? height - 1 : 0; int lineStride = ((ComponentSampleModel)sampleModel).getScanlineStride(); int finished = 0; while (count != imSize) { value = values[count++] & 0xff; if (value == 0) { switch(values[count++] & 0xff) { case 0: // End-of-scanline marker if (lineNo >= sourceRegion.y && lineNo < sourceRegion.y + sourceRegion.height) { if (noTransform) { int pos = lineNo * width; for(int i = 0; i < width; i++) bdata[pos++] = val[i]; processImageUpdate(bi, 0, lineNo, destinationRegion.width, 1, 1, 1,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -