int bytesWritten = 0; int size = imageWidth*imageHeight*3; int count = imageWidth*24; byte[] buffer = new byte[count]; int i, j; DataBufferByte bufferByte = (DataBufferByte) image.getRaster().getDataBuffer();  byte[] red = bufferByte.getData(Encoder.RED_BAND); byte[] green = bufferByte.getData(Encoder.GREEN_BAND); byte[] blue = bufferByte.getData(Encoder.BLUE_BAND); while (bytesWritten < size) { if ((bytesWritten+count) > size) count = size - bytesWritten; j = bytesWritten/3;  for (i = 0; i < count; i += 3) { buffer[i]   = red[j]; buffer[i+1] = green[j]; buffer[i+2] = blue[j]; j++; } output.write(buffer, 0, count); bytesWritten += count; } writeColorMap(red, green, blue);
DataBuffer db = image.getRaster().getDataBuffer(); if (db instanceof DataBufferByte) writeRGBDataBufferByte((DataBufferByte) db); else if (db instanceof DataBufferInt) writeRGBDataBufferInt((DataBufferInt) db);
    private void writeRGBPixels()        throws IOException    {        int bytesWritten = 0;        int size = imageWidth*imageHeight*3;        int count = imageWidth*24;      //3*8        byte[] buffer = new byte[count];        int i, j;        DataBufferByte         bufferByte = (DataBufferByte) image.getRaster().getDataBuffer();        //model chosen                  byte[] red = bufferByte.getData(Encoder.RED_BAND);        byte[] green = bufferByte.getData(Encoder.GREEN_BAND);        byte[] blue = bufferByte.getData(Encoder.BLUE_BAND);        while (bytesWritten < size) {            if ((bytesWritten+count) > size)                count = size - bytesWritten;            j = bytesWritten/3;            //TIFF save as BRG and not RGB.            for (i = 0; i < count; i += 3) {                buffer[i]   = red[j];                buffer[i+1] = green[j];                buffer[i+2] = blue[j];                j++;            }            output.write(buffer, 0, count);            bytesWritten += count;        }        writeColorMap(red, green, blue);    }    