use of loci.formats.meta.MetadataRetrieve in project bioformats by openmicroscopy.
the class QTWriter method saveBytes.
// -- IFormatWriter API methods --
/**
* @see loci.formats.IFormatWriter#saveBytes(int, byte[], int, int, int, int)
*/
@Override
public void saveBytes(int no, byte[] buf, int x, int y, int w, int h) throws FormatException, IOException {
checkParams(no, buf, x, y, w, h);
if (needLegacy) {
legacy.saveBytes(no, buf, x, y, w, h);
return;
}
MetadataRetrieve r = getMetadataRetrieve();
// get the width and height of the image
int width = r.getPixelsSizeX(series).getValue().intValue();
int height = r.getPixelsSizeY(series).getValue().intValue();
// need to check if the width is a multiple of 8
// if it is, great; if not, we need to pad each scanline with enough
// bytes to make the width a multiple of 8
int nChannels = getSamplesPerPixel();
int planeSize = width * height * nChannels;
if (!initialized[series][no]) {
initialized[series][no] = true;
setCodec();
if (codec != CODEC_RAW) {
needLegacy = true;
legacy.setId(currentId);
legacy.saveBytes(no, buf, x, y, w, h);
return;
}
// update the number of pixel bytes written
numBytes += (planeSize + pad * height);
out.seek(BYTE_COUNT_OFFSET);
out.writeInt(numBytes + 8);
out.seek(offsets.get(no));
if (!isFullPlane(x, y, w, h)) {
out.skipBytes(planeSize + pad * height);
}
}
out.seek(offsets.get(no) + y * (nChannels * width + pad));
// invert each pixel
// this will makes the colors look right in other readers (e.g. xine),
// but needs to be reversed in QTReader
byte[] tmp = new byte[buf.length];
if (nChannels == 1 && !needLegacy) {
for (int i = 0; i < buf.length; i++) {
tmp[i] = (byte) (255 - buf[i]);
}
} else
System.arraycopy(buf, 0, tmp, 0, buf.length);
if (!interleaved) {
// need to write interleaved data
byte[] tmp2 = new byte[tmp.length];
System.arraycopy(tmp, 0, tmp2, 0, tmp.length);
for (int i = 0; i < tmp.length; i++) {
int c = i / (w * h);
int index = i % (w * h);
tmp[index * nChannels + c] = tmp2[i];
}
}
int rowLen = tmp.length / h;
for (int row = 0; row < h; row++) {
out.skipBytes(nChannels * x);
out.write(tmp, row * rowLen, rowLen);
for (int i = 0; i < pad; i++) {
out.writeByte(0);
}
if (row < h - 1) {
out.skipBytes(nChannels * (width - w - x));
}
}
numWritten++;
writeFooter();
}
use of loci.formats.meta.MetadataRetrieve in project bioformats by openmicroscopy.
the class TiffWriter method prepareToWriteImage.
/**
* Performs the preparation for work prior to the usage of the TIFF saver.
* This method is factored out from <code>saveBytes()</code> in an attempt to
* ensure thread safety.
*/
protected int prepareToWriteImage(int no, byte[] buf, IFD ifd, int x, int y, int w, int h) throws IOException, FormatException {
MetadataRetrieve retrieve = getMetadataRetrieve();
boolean littleEndian = false;
if (retrieve.getPixelsBigEndian(series) != null) {
littleEndian = !retrieve.getPixelsBigEndian(series).booleanValue();
} else if (retrieve.getPixelsBinDataCount(series) == 0) {
littleEndian = !retrieve.getPixelsBinDataBigEndian(series, 0).booleanValue();
}
// at one time.
synchronized (this) {
if (!initialized[series][no]) {
initialized[series][no] = true;
RandomAccessInputStream tmp = createInputStream();
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);
}
formatCompression(ifd);
byte[][] lut = 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);
} else {
short[][] lut16 = AWTImageTools.getLookupTable(cm);
if (lut16 != null) {
int[] colorMap = new int[lut16.length * lut16[0].length];
for (int i = 0; i < lut16.length; i++) {
for (int j = 0; j < lut16[0].length; j++) {
colorMap[i * lut16[0].length + j] = (int) (lut16[i][j] & 0xffff);
}
}
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));
Length px = retrieve.getPixelsPhysicalSizeX(series);
Double physicalSizeX = px == null || px.value(UNITS.MICROMETER) == null ? null : px.value(UNITS.MICROMETER).doubleValue();
if (physicalSizeX == null || physicalSizeX.doubleValue() == 0) {
physicalSizeX = 0d;
} else
physicalSizeX = 1d / physicalSizeX;
Length py = retrieve.getPixelsPhysicalSizeY(series);
Double physicalSizeY = py == null || py.value(UNITS.MICROMETER) == null ? null : py.value(UNITS.MICROMETER).doubleValue();
if (physicalSizeY == null || physicalSizeY.doubleValue() == 0) {
physicalSizeY = 0d;
} else
physicalSizeY = 1d / physicalSizeY;
ifd.put(IFD.RESOLUTION_UNIT, 3);
ifd.put(IFD.X_RESOLUTION, new TiffRational((long) (physicalSizeX * 1000 * 10000), 1000));
ifd.put(IFD.Y_RESOLUTION, new TiffRational((long) (physicalSizeY * 1000 * 10000), 1000));
if (!isBigTiff) {
isBigTiff = (out.length() + 2 * (width * height * c * bytesPerPixel)) >= 4294967296L;
if (isBigTiff) {
throw new FormatException("File is too large; call setBigTiff(true)");
}
}
// write the image
ifd.put(new Integer(IFD.LITTLE_ENDIAN), new Boolean(littleEndian));
if (!ifd.containsKey(IFD.REUSE)) {
ifd.put(IFD.REUSE, out.length());
out.seek(out.length());
} else {
out.seek((Long) ifd.get(IFD.REUSE));
}
ifd.putIFDValue(IFD.PLANAR_CONFIGURATION, interleaved || getSamplesPerPixel() == 1 ? 1 : 2);
int sampleFormat = 1;
if (FormatTools.isSigned(type))
sampleFormat = 2;
if (FormatTools.isFloatingPoint(type))
sampleFormat = 3;
ifd.putIFDValue(IFD.SAMPLE_FORMAT, sampleFormat);
int channels = retrieve.getPixelsSizeC(series).getValue().intValue();
int z = retrieve.getPixelsSizeZ(series).getValue().intValue();
int t = retrieve.getPixelsSizeT(series).getValue().intValue();
ifd.putIFDValue(IFD.IMAGE_DESCRIPTION, "ImageJ=\nhyperstack=true\nimages=" + (channels * z * t) + "\nchannels=" + channels + "\nslices=" + z + "\nframes=" + t);
int index = no;
for (int i = 0; i < getSeries(); i++) {
index += getPlaneCount(i);
}
return index;
}
use of loci.formats.meta.MetadataRetrieve in project bioformats by openmicroscopy.
the class TiffWriter method setupTiffSaver.
// -- Helper methods --
protected void setupTiffSaver() throws IOException {
out.close();
out = createOutputStream();
tiffSaver = createTiffSaver();
MetadataRetrieve retrieve = getMetadataRetrieve();
boolean littleEndian = false;
if (retrieve.getPixelsBigEndian(series) != null) {
littleEndian = !retrieve.getPixelsBigEndian(series).booleanValue();
} else if (retrieve.getPixelsBinDataCount(series) == 0) {
littleEndian = !retrieve.getPixelsBinDataBigEndian(series, 0).booleanValue();
}
tiffSaver.setWritingSequentially(sequential);
tiffSaver.setLittleEndian(littleEndian);
tiffSaver.setBigTiff(isBigTiff);
tiffSaver.setCodecOptions(options);
}
use of loci.formats.meta.MetadataRetrieve in project bioformats by openmicroscopy.
the class TiffWriter method getTile.
private byte[] getTile(byte[] buf, Region tileParams, Region srcParams) {
MetadataRetrieve retrieve = getMetadataRetrieve();
int type = FormatTools.pixelTypeFromString(retrieve.getPixelsType(series).toString());
int channel_count = getSamplesPerPixel();
int bytesPerPixel = FormatTools.getBytesPerPixel(type);
int tileSize = tileParams.width * tileParams.height * bytesPerPixel * channel_count;
byte[] returnBuf = new byte[tileSize];
for (int row = tileParams.y; row != tileParams.y + tileParams.height; row++) {
for (int sampleoffset = 0; sampleoffset < (tileParams.width * channel_count); sampleoffset++) {
int channel_index = sampleoffset / tileParams.width;
int channel_offset = (sampleoffset - (tileParams.width * channel_index)) * bytesPerPixel;
int full_row_width = srcParams.width * bytesPerPixel;
int full_plane_size = full_row_width * srcParams.height;
int xoffset = (tileParams.x - srcParams.x) * bytesPerPixel;
int yoffset = (row - srcParams.y) * full_row_width;
int row_offset = (row - tileParams.y) * tileParams.width * bytesPerPixel;
int src_index = yoffset + xoffset + channel_offset + (channel_index * full_plane_size);
int dest_index = (tileParams.height * tileParams.width * channel_index * bytesPerPixel) + row_offset;
for (int pixelByte = 0; pixelByte < bytesPerPixel; pixelByte++) {
returnBuf[dest_index + channel_offset + pixelByte] = buf[src_index + pixelByte];
}
}
}
return returnBuf;
}
use of loci.formats.meta.MetadataRetrieve in project bioformats by openmicroscopy.
the class V3DrawWriter method saveBytes.
// -- IFormatWriter API methods --
/**
* @see loci.formats.IFormatWriter#saveBytes(int, byte[], int, int, int,
* int)
*/
@Override
public void saveBytes(int no, byte[] buf, int x, int y, int w, int h) throws FormatException, IOException {
if (!isFullPlane(x, y, w, h)) {
throw new FormatException("V3DRawWriter does not support writing tiles");
}
// for header
final String formatkey = "raw_image_stack_by_hpeng";
byte[] v2 = new byte[2];
byte[] v4 = new byte[4];
// new variable for clarity: vaa3d is in xyzct format
int[] sz = new int[4];
checkParams(no, buf, x, y, w, h);
if (pixels == null) {
pixels = new RandomAccessOutputStream(currentId);
}
String endianString = "L";
MetadataRetrieve meta = getMetadataRetrieve();
boolean bigendian = false;
if (meta.getPixelsBigEndian(series) != null) {
bigendian = meta.getPixelsBigEndian(series).booleanValue();
} else if (meta.getPixelsBinDataCount(series) == 0) {
bigendian = meta.getPixelsBinDataBigEndian(series, 0).booleanValue();
}
if (!bigendian) {
endianString = "L";
} else {
endianString = "B";
}
int rgbChannels = getSamplesPerPixel();
String order = meta.getPixelsDimensionOrder(series).getValue();
int sizeZ = meta.getPixelsSizeZ(series).getValue().intValue();
int sizeC = meta.getChannelCount(series);
if (rgbChannels <= sizeC) {
sizeC /= rgbChannels;
}
int sizeT = meta.getPixelsSizeT(series).getValue().intValue();
int planes = sizeZ * sizeC * sizeT;
int[] coords = FormatTools.getZCTCoords(order, sizeZ, sizeC, sizeT, planes, no);
int realIndex = FormatTools.getIndex(outputOrder, sizeZ, sizeC, sizeT, planes, coords[0], coords[1], coords[2]);
int sizeX = meta.getPixelsSizeX(series).getValue().intValue();
int sizeY = meta.getPixelsSizeY(series).getValue().intValue();
int pixelType = FormatTools.pixelTypeFromString(meta.getPixelsType(series).toString());
int bytesPerPixel = FormatTools.getBytesPerPixel(pixelType);
long planeSize = sizeX * sizeY * bytesPerPixel * rgbChannels;
// FormatTools.get
sz[0] = sizeX;
sz[1] = sizeY;
// temporary aggregate for layer
sz[2] = sizeZ * sizeT;
// temp aggregate for color
sz[3] = sizeC * rgbChannels;
if (!initialized[series][realIndex]) {
initialized[series][realIndex] = true;
}
try {
// write the header if it's the first time through
if (lastPlane == -1) {
// write format key
pixels.write(formatkey.getBytes(Constants.ENCODING));
// endianness.
pixels.write(endianString.getBytes(Constants.ENCODING));
unpackBytes(bytesPerPixel, v2, 0, 2, !bigendian);
// unitSize
pixels.write(v2);
for (int d : sz) {
unpackBytes(d, v4, 0, 4, !bigendian);
pixels.write(v4);
}
// and image dimensions into header
pixels.write(buf);
LOGGER.info("********* V3DrawWriter.java internal variables *********");
LOGGER.info("bytesPerPixel = " + bytesPerPixel);
LOGGER.info("pixelType = " + pixelType);
LOGGER.info("rgbChannels =" + rgbChannels);
LOGGER.info("sizeC = " + sizeC);
LOGGER.info("sizeZ = " + sizeZ);
LOGGER.info("sizeT = " + sizeT);
LOGGER.info("endian= " + endianString);
} else {
pixels.seek(planeSize * realIndex + pixelOffset);
// write the rest of the plane
pixels.write(buf);
}
lastPlane = realIndex;
} finally {
pixels.close();
pixels = null;
}
}
Aggregations