in src/main/java/org/apache/commons/imaging/formats/pcx/PcxWriter.java [176:256]
private void writePixels(final BufferedImage src, final int bitDepth, final int planes, final int bytesPerLine, final SimplePalette palette,
final AbstractBinaryOutputStream bos) throws IOException {
final byte[] plane0 = Allocator.byteArray(bytesPerLine);
final byte[] plane1 = Allocator.byteArray(bytesPerLine);
final byte[] plane2 = Allocator.byteArray(bytesPerLine);
final byte[] plane3 = Allocator.byteArray(bytesPerLine);
final byte[][] allPlanes = { plane0, plane1, plane2, plane3 };
for (int y = 0; y < src.getHeight(); y++) {
for (int i = 0; i < planes; i++) {
Arrays.fill(allPlanes[i], (byte) 0);
}
if (bitDepth == 1 && planes == 1) {
for (int x = 0; x < src.getWidth(); x++) {
final int rgb = 0xffffff & src.getRGB(x, y);
final int bit;
if (rgb == 0x000000) {
bit = 0;
} else {
bit = 1;
}
plane0[x >>> 3] |= bit << 7 - (x & 7);
}
} else if (bitDepth == 1 && planes == 2) {
for (int x = 0; x < src.getWidth(); x++) {
final int argb = src.getRGB(x, y);
final int index = palette.getPaletteIndex(0xffffff & argb);
plane0[x >>> 3] |= (index & 1) << 7 - (x & 7);
plane1[x >>> 3] |= (index & 2) >> 1 << 7 - (x & 7);
}
} else if (bitDepth == 1 && planes == 3) {
for (int x = 0; x < src.getWidth(); x++) {
final int argb = src.getRGB(x, y);
final int index = palette.getPaletteIndex(0xffffff & argb);
plane0[x >>> 3] |= (index & 1) << 7 - (x & 7);
plane1[x >>> 3] |= (index & 2) >> 1 << 7 - (x & 7);
plane2[x >>> 3] |= (index & 4) >> 2 << 7 - (x & 7);
}
} else if (bitDepth == 1 && planes == 4) {
for (int x = 0; x < src.getWidth(); x++) {
final int argb = src.getRGB(x, y);
final int index = palette.getPaletteIndex(0xffffff & argb);
plane0[x >>> 3] |= (index & 1) << 7 - (x & 7);
plane1[x >>> 3] |= (index & 2) >> 1 << 7 - (x & 7);
plane2[x >>> 3] |= (index & 4) >> 2 << 7 - (x & 7);
plane3[x >>> 3] |= (index & 8) >> 3 << 7 - (x & 7);
}
} else if (bitDepth == 2 && planes == 1) {
for (int x = 0; x < src.getWidth(); x++) {
final int argb = src.getRGB(x, y);
final int index = palette.getPaletteIndex(0xffffff & argb);
plane0[x >>> 2] |= index << 2 * (3 - (x & 3));
}
} else if (bitDepth == 4 && planes == 1) {
for (int x = 0; x < src.getWidth(); x++) {
final int argb = src.getRGB(x, y);
final int index = palette.getPaletteIndex(0xffffff & argb);
plane0[x >>> 1] |= index << 4 * (1 - (x & 1));
}
} else if (bitDepth == 8 && planes == 1) {
for (int x = 0; x < src.getWidth(); x++) {
final int argb = src.getRGB(x, y);
final int index = palette.getPaletteIndex(0xffffff & argb);
plane0[x] = (byte) index;
}
} else if (bitDepth == 8 && planes == 3) {
for (int x = 0; x < src.getWidth(); x++) {
final int argb = src.getRGB(x, y);
plane0[x] = (byte) (argb >>> 16);
plane1[x] = (byte) (argb >>> 8);
plane2[x] = (byte) argb;
}
}
for (int i = 0; i < planes; i++) {
rleWriter.write(bos, allPlanes[i]);
}
}
rleWriter.flush(bos);
}