in src/main/java/org/apache/xmlgraphics/image/codec/png/PNGRed.java [1745:1829]
private void decodePass(WritableRaster imRas,
int xOffset, int yOffset,
int xStep, int yStep,
int passWidth, int passHeight) {
if ((passWidth == 0) || (passHeight == 0)) {
return;
}
int bytesPerRow = (inputBands * passWidth * bitDepth + 7) / 8;
int eltsPerRow = (bitDepth == 16) ? bytesPerRow / 2 : bytesPerRow;
byte[] curr = new byte[bytesPerRow];
byte[] prior = new byte[bytesPerRow];
// Create a 1-row tall Raster to hold the data
WritableRaster passRow =
createRaster(passWidth, 1, inputBands,
eltsPerRow,
bitDepth);
DataBuffer dataBuffer = passRow.getDataBuffer();
int type = dataBuffer.getDataType();
byte[] byteData = null;
short[] shortData = null;
if (type == DataBuffer.TYPE_BYTE) {
byteData = ((DataBufferByte)dataBuffer).getData();
} else {
shortData = ((DataBufferUShort)dataBuffer).getData();
}
// Decode the (sub)image row-by-row
int srcY;
int dstY;
for (srcY = 0, dstY = yOffset;
srcY < passHeight;
srcY++, dstY += yStep) {
// Read the filter type byte and a row of data
int filter = 0;
try {
filter = dataStream.read();
dataStream.readFully(curr, 0, bytesPerRow);
} catch (Exception e) {
e.printStackTrace();
}
switch (filter) {
case PNG_FILTER_NONE:
break;
case PNG_FILTER_SUB:
decodeSubFilter(curr, bytesPerRow, bytesPerPixel);
break;
case PNG_FILTER_UP:
decodeUpFilter(curr, prior, bytesPerRow);
break;
case PNG_FILTER_AVERAGE:
decodeAverageFilter(curr, prior, bytesPerRow, bytesPerPixel);
break;
case PNG_FILTER_PAETH:
decodePaethFilter(curr, prior, bytesPerRow, bytesPerPixel);
break;
default:
// Error -- unknown filter type
String msg = PropertyUtil.getString("PNGImageDecoder16");
throw new RuntimeException(msg);
}
// Copy data into passRow byte by byte
if (bitDepth < 16) {
System.arraycopy(curr, 0, byteData, 0, bytesPerRow);
} else {
int idx = 0;
for (int j = 0; j < eltsPerRow; j++) {
shortData[j] =
(short)((curr[idx] << 8) | (curr[idx + 1] & 0xff));
idx += 2;
}
}
processPixels(postProcess,
passRow, imRas, xOffset, xStep, dstY, passWidth);
// Swap curr and prior
byte[] tmp = prior;
prior = curr;
curr = tmp;
}
}