/** * Constructs a new TIFF saver from the given filename. * @param filename Filename of the output stream that we may use to create * extra input or output streams as required. */ public TiffSaver(String filename) throws IOException { this(new RandomAccessOutputStream(filename), filename); }
@Override public void saveStop() throws IOException { if (saveStream != null) { saveStream.close(); saveStream = null; } }
private void writeFooter() throws IOException { footerPointer = out.getFilePointer(); // write IEND chunk out.writeInt(0); out.writeBytes("IEND"); out.writeInt(crc("IEND".getBytes(Constants.ENCODING))); // update frame count out.seek(numFramesPointer); out.writeInt(numFrames); out.skipBytes(4); byte[] b = new byte[12]; b[0] = 'a'; b[1] = 'c'; b[2] = 'T'; b[3] = 'L'; DataTools.unpackBytes(numFrames, b, 4, 4, false); DataTools.unpackBytes(0, b, 8, 4, false); out.writeInt(crc(b)); }
/** Write the atom length and type. */ private void writeAtom(int length, String type) throws IOException { out.writeInt(length); out.writeBytes(type); }
protected void writePlane(String varName, byte[] array, int w, int h) throws IOException { int i = 0; out.writeLine(" public byte[][] " + varName + " = {"); for (int y=0; y<h; y++) { out.writeBytes(" {"); for (int x=0; x<w; x++) { out.writeBytes(String.valueOf(array[i++])); if (x < w - 1) out.writeBytes(", "); else out.writeBytes("}"); } if (y < h - 1) out.writeLine(","); else out.writeLine(""); } out.writeLine(" };"); out.writeLine(""); }
/** Writes the TIFF file header. */ public void writeHeader() throws IOException { // write endianness indicator out.seek(0); if (isLittleEndian()) { out.writeByte(TiffConstants.LITTLE); out.writeByte(TiffConstants.LITTLE); } else { out.writeByte(TiffConstants.BIG); out.writeByte(TiffConstants.BIG); } // write magic number if (bigTiff) { out.writeShort(TiffConstants.BIG_TIFF_MAGIC_NUMBER); } else out.writeShort(TiffConstants.MAGIC_NUMBER); // write the offset to the first IFD // for vanilla TIFFs, 8 is the offset to the first IFD // for BigTIFFs, 8 is the number of bytes in an offset if (bigTiff) { out.writeShort(8); out.writeShort(0); // write the offset to the first IFD for BigTIFF files out.writeLong(16); } else { out.writeInt(8); } }
public void writeIFD(IFD ifd, long nextOffset) throws FormatException, IOException { TreeSet<Integer> keys = new TreeSet<Integer>(ifd.keySet()); int keyCount = keys.size(); if (ifd.containsKey(new Integer(IFD.LITTLE_ENDIAN))) keyCount--; if (ifd.containsKey(new Integer(IFD.BIG_TIFF))) keyCount--; if (ifd.containsKey(new Integer(IFD.REUSE))) keyCount--; long fp = out.getFilePointer(); int bytesPerEntry = bigTiff ? TiffConstants.BIG_TIFF_BYTES_PER_ENTRY : TiffConstants.BYTES_PER_ENTRY; int ifdBytes = (bigTiff ? 16 : 6) + bytesPerEntry * keyCount; if (bigTiff) out.writeLong(keyCount); else out.writeShort(keyCount); ByteArrayHandle extra = new ByteArrayHandle(); RandomAccessOutputStream extraStream = new RandomAccessOutputStream(extra); for (Integer key : keys) { if (key.equals(IFD.LITTLE_ENDIAN) || key.equals(IFD.BIG_TIFF) || key.equals(IFD.REUSE)) continue; Object value = ifd.get(key); writeIFDValue(extraStream, ifdBytes + fp, key.intValue(), value); } if (bigTiff) out.seek(out.getFilePointer()); writeIntValue(out, nextOffset); out.write(extra.getBytes(), 0, (int) extra.length()); extraStream.close(); }
pixels = new RandomAccessOutputStream(currentId); pixels.seek(pixelOffset + (realIndex + 1) * planeSize); pixels.seek(pixelOffset + realIndex * planeSize); if (isFullPlane(x, y, w, h) && (interleaved || rgbChannels == 1)) { pixels.write(buf); pixels.skipBytes(bytesPerPixel * rgbChannels * sizeX * y); for (int row=0; row<h; row++) { ByteArrayOutputStream strip = new ByteArrayOutputStream(); pixels.skipBytes(bytesPerPixel * rgbChannels * x); pixels.write(strip.toByteArray()); pixels.skipBytes(bytesPerPixel * rgbChannels * (sizeX - w - x)); overwriteDimensions(getMetadataRetrieve()); pixels.close(); pixels = null;
RandomAccessOutputStream out = new RandomAccessOutputStream(handle); out.order(isLittleEndian()); int q = Integer.parseInt(t.nextToken().trim()); if (getPixelType() == FormatTools.UINT16) { out.writeShort(q); else out.writeByte(q); out.close(); try (RandomAccessInputStream s = new RandomAccessInputStream(handle)) { s.seek(0);
private void saveComment(String file, String xml) throws IOException { if (out != null) out.close(); out = new RandomAccessOutputStream(file); RandomAccessInputStream in = null; try { TiffSaver saver = new TiffSaver(out, file); saver.setBigTiff(isBigTiff); in = new RandomAccessInputStream(file); saver.overwriteLastIFDOffset(in); saver.overwriteComment(in, xml); } catch (FormatException exc) { IOException io = new IOException("Unable to append OME-XML comment"); io.initCause(exc); throw io; } finally { if (out != null) out.close(); if (in != null) in.close(); } }
out.writeBytes(DUMMY_PIXEL); out.seek(planeOffset + offset); for (int row=0; row<h; row++) { out.skipBytes(nChannels * x * 2); for (int col=0; col<w*nChannels; col++) { int i = row * w * nChannels + col; out.writeBytes(buffer.toString()); buffer.delete(0, buffer.length()); out.skipBytes(nChannels * (sizeX - w - x) * 2); out.seek(out.length()); out.writeBytes("\nshowpage\n");
out.seek(BYTE_COUNT_OFFSET); out.writeInt(numBytes + 8); out.seek(offsets.get(no)); out.skipBytes(planeSize + pad * height); out.seek(offsets.get(no) + y * (nChannels * width + pad)); out.skipBytes(nChannels * x); out.write(tmp, row * rowLen, rowLen); for (int i=0; i<pad; i++) { out.writeByte(0); out.skipBytes(nChannels * (width - w - x));
private int[] overwriteDimensions(MetadataRetrieve meta) throws IOException { out.seek(dimensionOffset); int sizeX = meta.getPixelsSizeX(series).getValue().intValue(); int sizeY = meta.getPixelsSizeY(series).getValue().intValue(); out.writeBytes("layout\torder\tbits\t" + dimOrder.toString() + "\n"); out.writeBytes("layout\tsizes\t"); for (int i=0; i<sizes.length; i++) { out.writeBytes(sizes[i] + "\t"); while ((out.getFilePointer() - dimensionOffset) < dimensionLength - 1) { out.writeBytes(" "); out.writeBytes("\n");
/** * @param b Source buffer to read data from. * @param off Offset within the buffer to start reading from. * @param len Number of bytes to read. * @throws IOException If there is an error writing to the stream. */ public void write(ByteBuffer b, int off, int len) throws IOException { flush(); outputFile.write(b, off, len); }
public void overwriteLastIFDOffset(RandomAccessInputStream raf) throws FormatException, IOException { if (raf == null) throw new FormatException("Output cannot be null"); TiffParser parser = new TiffParser(raf); long[] offsets = parser.getIFDOffsets(); out.seek(raf.getFilePointer() - (bigTiff ? 8 : 4)); writeIntValue(out, 0); }
/** * Seek to the given (x, y) coordinate of the image that starts at * the given offset. */ protected void seekToPlaneOffset(long baseOffset, int x, int y) throws IOException { out.seek(baseOffset); MetadataRetrieve r = getMetadataRetrieve(); int samples = getSamplesPerPixel(); int pixelType = FormatTools.pixelTypeFromString(r.getPixelsType(series).toString()); int bpp = FormatTools.getBytesPerPixel(pixelType); if (interleaved) bpp *= samples; try { int sizeX = getSizeX(); out.skipBytes(bpp * (y * sizeX + x)); } catch (FormatException e) { throw new IOException(e); } }
/** * Writes the given string followed by a newline character. * * @param s the line of text to be written. A newline does not * need to be appended, as this method automatically writes * a newline character. * @throws IOException if writing is not possible */ public void writeLine(String s) throws IOException { writeBytes(s); writeBytes("\n"); }
/** * Writes the bits represented by a bit string to the buffer. * * @throws IllegalArgumentException If any characters other than * '0' and '1' appear in the string. */ public void writeBits(String bitString) throws IOException { if (bitString == null) { throw new IllegalArgumentException("Bit string cannot be null"); } for (char c : bitString.toCharArray()) { if (c == '1') { writeBits(1, 1); } else if (c == '0') { writeBits(0, 1); } else { throw new IllegalArgumentException( "Found illegal character '" + c + "'; write terminated"); } } }
public void writeIFD(IFD ifd, long nextOffset) throws FormatException, IOException { TreeSet<Integer> keys = new TreeSet<Integer>(ifd.keySet()); int keyCount = keys.size(); if (ifd.containsKey(new Integer(IFD.LITTLE_ENDIAN))) keyCount--; if (ifd.containsKey(new Integer(IFD.BIG_TIFF))) keyCount--; if (ifd.containsKey(new Integer(IFD.REUSE))) keyCount--; long fp = out.getFilePointer(); int bytesPerEntry = bigTiff ? TiffConstants.BIG_TIFF_BYTES_PER_ENTRY : TiffConstants.BYTES_PER_ENTRY; int ifdBytes = (bigTiff ? 16 : 6) + bytesPerEntry * keyCount; if (bigTiff) out.writeLong(keyCount); else out.writeShort(keyCount); ByteArrayHandle extra = new ByteArrayHandle(); RandomAccessOutputStream extraStream = new RandomAccessOutputStream(extra); for (Integer key : keys) { if (key.equals(IFD.LITTLE_ENDIAN) || key.equals(IFD.BIG_TIFF) || key.equals(IFD.REUSE)) continue; Object value = ifd.get(key); writeIFDValue(extraStream, ifdBytes + fp, key.intValue(), value); } if (bigTiff) out.seek(out.getFilePointer()); writeIntValue(out, nextOffset); out.write(extra.getBytes(), 0, (int) extra.length()); extraStream.close(); }