int[] data = new int[width * height];
bi.getRGB(0, 0, width, height, data, 0, width);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
int pixel, count, i, lastCount;
if (!rawbits) {
dos.writeBytes(regularMagicNumber);
dos.writeBytes(" " + width);
dos.writeBytes(" " + height);
dos.writeBytes(" " + MAX_COLOR_VALUE + "\n");
count = 0; // Keep lines less that 70 characters long
// Keep track of the number of characters added per
// pass
lastCount = 0;
Debug.output("PPMFormatter: Header is " + dos.size() + " bytes");
Debug.output("PPMFormatter: Height = " + height);
Debug.output("PPMFormatter: Width = " + width);
Debug.output("PPMFormatter: data length = " + data.length);
for (i = 0; i < data.length; i++) {
pixel = data[i];
int r = (pixel >>> 16) & 0x000000FF;
int g = (pixel >>> 8) & 0x000000FF;
int b = (pixel) & 0x000000FF;
dos.writeBytes(" " + r);
dos.writeBytes(" " + g);
dos.writeBytes(" " + b);
if (count > 57) {
dos.writeBytes("\n");
count = 0;
} else {
count += dos.size() - lastCount;
}
lastCount = dos.size();
}
Debug.output("PPMFormatter: after data, size is " + dos.size());
} else {
dos.writeBytes(rawbitsMagicNumber);
dos.writeBytes(" " + width);
dos.writeBytes(" " + height);
dos.writeBytes(" " + MAX_COLOR_VALUE + "\n");
for (i = 0; i < data.length; i++) {
pixel = data[i];
dos.writeByte(pixel >>> 16);
dos.writeByte(pixel >>> 8);
dos.writeByte(pixel);
}
}
return baos.toByteArray();