* ensure thread safety.
*/
private int prepareToWriteImage(
int no, byte[] buf, IFD ifd, int x, int y, int w, int h)
throws IOException, FormatException {
MetadataRetrieve retrieve = getMetadataRetrieve();
Boolean bigEndian = retrieve.getPixelsBinDataBigEndian(series, 0);
boolean littleEndian = bigEndian == null ?
false : !bigEndian.booleanValue();
// Ensure that no more than one thread manipulated the initialized array
// at one time.
synchronized (this) {
if (no < initialized[series].length && !initialized[series][no]) {
initialized[series][no] = true;
RandomAccessInputStream tmp = new RandomAccessInputStream(currentId);
if (tmp.length() == 0) {
synchronized (this) {
// write TIFF header
tiffSaver.writeHeader();
}
}
tmp.close();
}
}
int c = getSamplesPerPixel();
int type = FormatTools.pixelTypeFromString(
retrieve.getPixelsType(series).toString());
int bytesPerPixel = FormatTools.getBytesPerPixel(type);
int blockSize = w * h * c * bytesPerPixel;
if (blockSize > buf.length) {
c = buf.length / (w * h * bytesPerPixel);
}
if (bytesPerPixel > 1 && c != 1 && c != 3) {
// split channels
checkParams = false;
if (no == 0) {
initialized[series] = new boolean[initialized[series].length * c];
}
for (int i=0; i<c; i++) {
byte[] b = ImageTools.splitChannels(buf, i, c, bytesPerPixel,
false, interleaved);
saveBytes(no * c + i, b, (IFD) ifd.clone(), x, y, w, h);
}
checkParams = true;
return -1;
}
formatCompression(ifd);
byte[][] lut = (cm == null)?null:AWTImageTools.get8BitLookupTable(cm);
if (lut != null) {
int[] colorMap = new int[lut.length * lut[0].length];
for (int i=0; i<lut.length; i++) {
for (int j=0; j<lut[0].length; j++) {
colorMap[i * lut[0].length + j] = (int) ((lut[i][j] & 0xff) << 8);
}
}
ifd.putIFDValue(IFD.COLOR_MAP, colorMap);
}
int width = retrieve.getPixelsSizeX(series).getValue().intValue();
int height = retrieve.getPixelsSizeY(series).getValue().intValue();
ifd.put(new Integer(IFD.IMAGE_WIDTH), new Long(width));
ifd.put(new Integer(IFD.IMAGE_LENGTH), new Long(height));
PositiveFloat px = retrieve.getPixelsPhysicalSizeX(series);
Double physicalSizeX = px == null ? null : px.getValue();
if (physicalSizeX == null || physicalSizeX.doubleValue() == 0) {
physicalSizeX = 0d;
}
else physicalSizeX = 1d / physicalSizeX;
PositiveFloat py = retrieve.getPixelsPhysicalSizeY(series);
Double physicalSizeY = py == null ? null : py.getValue();
if (physicalSizeY == null || physicalSizeY.doubleValue() == 0) {
physicalSizeY = 0d;
}
else physicalSizeY = 1d / physicalSizeY;