private int writeImageData(RenderedImage im, OutputStream out)
throws IOException {
int total = 0;
// Get the encoder
XTIFFTileCodec codec = directory.createTileCodec(tparam);
// Create a buffer to hold the data
// to be written to the file, so we can use array writes.
int tsize = codec.getCompressedTileSize(im);
bpixels = new byte[tsize];
// Encode one tile at a time
Rectangle rect = new Rectangle();
float minX = (float) im.getMinX();
float minY = (float) im.getMinY();
float rows = (float) tileLength;
float cols = (float) tileWidth;
int i = 0;
for (int row = 0; row < length; row += tileLength) {
for (int col = 0; col < width; col += tileWidth) {
if (!isTiled)
rows = Math.min(tileLength, length - row);
rect.setRect(minX + col, minY + row, cols, rows);
int tileSize = codec.encode(im, rect, bpixels);
out.write(bpixels, 0, tileSize);
stripTileOffsets[i] = currentOffset;
stripTileByteCounts[i++] = tileSize;
currentOffset += tileSize;
total += tileSize;