Java Code Examples for net.imglib2.Interval#min()

The following examples show how to use net.imglib2.Interval#min() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RenderBoxHelperFX.java    From paintera with GNU General Public License v2.0 6 votes vote down vote up
public void renderCanvas(final Interval targetInterval, final Path canvas)
{
	final double tX0 = targetInterval.min(0);
	final double tX1 = targetInterval.max(0);
	final double tY0 = targetInterval.min(1);
	final double tY1 = targetInterval.max(1);

	final double[] c000 = new double[] {tX0, tY0, 0};
	final double[] c100 = new double[] {tX1, tY0, 0};
	final double[] c010 = new double[] {tX0, tY1, 0};
	final double[] c110 = new double[] {tX1, tY1, 0};

	canvas.getElements().add(new MoveTo(perspectiveX(c000), perspectiveY(c000)));
	canvas.getElements().add(new LineTo(perspectiveX(c100), perspectiveY(c100)));
	canvas.getElements().add(new LineTo(perspectiveX(c110), perspectiveY(c110)));
	canvas.getElements().add(new LineTo(perspectiveX(c010), perspectiveY(c010)));
	canvas.getElements().add(new ClosePath());
}
 
Example 2
Source File: TIFFFormat.java    From scifio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Saves the given image to the specified series in the current file. The
 * IFD hashtable allows specification of TIFF parameters such as bit depth,
 * compression and units.
 */
public void savePlane(final int imageIndex, final long planeIndex,
	final Plane plane, IFD ifd, final Interval bounds) throws IOException,
	FormatException
{
	final byte[] buf = plane.getBytes();
	if (checkParams) checkParams(imageIndex, planeIndex, buf, bounds);
	final int xAxis = getMetadata().get(imageIndex).getAxisIndex(Axes.X);
	final int yAxis = getMetadata().get(imageIndex).getAxisIndex(Axes.Y);
	final int x = (int) bounds.min(xAxis), y = (int) bounds.min(yAxis), //
			w = (int) bounds.dimension(xAxis), h = (int) bounds.dimension(yAxis);
	if (ifd == null) ifd = new IFD(log());
	final int type = getMetadata().get(imageIndex).getPixelType();
	final long index = planeIndex;
	// This operation is synchronized
	synchronized (this) {
		// This operation is synchronized against the TIFF saver.
		synchronized (tiffSaver) {
			prepareToWritePlane(imageIndex, planeIndex, plane, ifd, x, y, w, h);
		}
	}

	tiffSaver.writeImage(buf, ifd, index, type, x, y, w, h,
		planeIndex == getMetadata().get(imageIndex).getPlaneCount() - 1 &&
			imageIndex == getMetadata().getImageCount() - 1);
}
 
Example 3
Source File: PlaneSeparator.java    From scifio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Returns true if we have a cached plane matching the given image and plane
 * indices, with extents to cover the desired offsets and lengths
 */
private boolean haveCached(final long source, final int imageIndex,
	final Interval bounds)
{
	if (source != lastPlaneIndex || imageIndex != lastImageIndex ||
		lastPlane == null || lastPlaneMin == null || lastPlaneMax == null)
	{
		return false;
	}
	// TODO It would be nice to fix up this logic so that we can use
	// cached planes when requesting a sub-region of the cached plane.
	// See https://github.com/scifio/scifio/issues/155
	for (int d = 0; d < bounds.numDimensions(); d++) {
		if (bounds.min(d) != lastPlaneMin[d]) return false;
		if (bounds.max(d) != lastPlaneMax[d]) return false;
	}
	return true;
}
 
Example 4
Source File: FormatTools.java    From scifio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Checks that the given tile size is valid for the given reader. */
public static void checkTileSize(final Metadata m, final Interval bounds,
	final int imageIndex) throws FormatException
{
	final List<CalibratedAxis> axes = m.get(imageIndex).getAxesPlanar();

	for (int i = 0; i < axes.size(); i++) {
		final long start = bounds.min(i);
		final long end = bounds.max(i);
		final long length = m.get(imageIndex).getAxisLength(axes.get(i));

		if (start < 0 || end < 0 || end >= length) {
			throw new FormatException("Invalid planar size: start=" + start +
				", end=" + end + ", length in metadata=" + length);
		}
	}
}
 
Example 5
Source File: Morphologies.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Computes the min coordinate and the size of an {@link Interval} after
 * padding with a list of {@link Shape}s in a series morphology operations.
 * 
 * @param source the interval to be applied with some morphology operation
 * @param shapes the list of Shapes for padding
 * @return a size-2 array storing the min coordinate and the size of the
 *         padded interval
 */
public static final long[][] computeMinSize(final Interval source,
	final List<Shape> shapes)
{

	final int numDims = source.numDimensions();
	final long[] min = new long[numDims];
	final long[] size = new long[numDims];

	for (int i = 0; i < numDims; i++) {
		min[i] = source.min(i);
		size[i] = source.dimension(i);
	}

	for (final Shape shape : shapes) {
		final Neighborhood<BitType> nh = MorphologyUtils.getNeighborhood(shape,
			source);
		for (int i = 0; i < numDims; i++) {
			min[i] += nh.min(i);
			size[i] += nh.dimension(i) - 1;
		}
	}

	return new long[][] { min, size };
}
 
Example 6
Source File: GIFFormat.java    From scifio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public ByteArrayPlane openPlane(final int imageIndex, final long planeIndex,
	final ByteArrayPlane plane, final Interval bounds,
	final SCIFIOConfig config) throws FormatException, IOException
{
	final byte[] buf = plane.getData();
	final Metadata meta = getMetadata();
	final int xIndex = meta.get(imageIndex).getAxisIndex(Axes.X);
	final int yIndex = meta.get(imageIndex).getAxisIndex(Axes.Y);
	plane.setColorTable(meta.getColorTable(0, 0));
	FormatTools.checkPlaneForReading(meta, imageIndex, planeIndex, buf.length,
		bounds);
	final int x = (int) bounds.min(xIndex);
	final int y = (int) bounds.min(yIndex);
	final int w = (int) bounds.dimension(xIndex);
	final int h = (int) bounds.dimension(yIndex);
	final int[] act = meta.getColorTables().get((int) planeIndex);

	final byte[] b = meta.getImages().get((int) planeIndex);
	if (planeIndex > 0 && meta.isTransparency()) {
		final byte[] prev = meta.getImages().get((int) planeIndex - 1);
		int idx = meta.getTransIndex();
		if (idx >= 127) idx = 0;
		for (int i = 0; i < b.length; i++) {
			if ((act[b[i] & 0xff] & 0xffffff) == idx) {
				b[i] = prev[i];
			}
		}
		meta.getImages().setElementAt(b, (int) planeIndex);
	}

	for (int row = 0; row < h; row++) {
		System.arraycopy(b, (row + y) * (int) meta.get(imageIndex)
			.getAxisLength(Axes.X) + x, buf, row * w, w);
	}

	return plane;
}
 
Example 7
Source File: PlaneSeparator.java    From scifio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Converts the given plane information using the current metadata to a format
 * usable by the wrapped reader, stored in the "lastPlane"... variables.
 */
private void updateLastPlaneInfo(final long source, final int imageIndex,
	final int splitOffset, final Interval bounds)
{
	final Metadata meta = getMetadata();
	final Metadata parentMeta = getParentMeta();
	lastPlaneIndex = source;
	lastImageIndex = imageIndex;
	// create the plane offsets and lengths to match the underlying image
	lastPlaneMin = new long[bounds.numDimensions() + splitOffset];
	lastPlaneMax = new long[bounds.numDimensions() + splitOffset];

	// Create the offset and length arrays to match the underlying,
	// unsplit dimensions. This is required to pass to the wrapped reader.
	// The unsplit plane will then have the appropriate region extracted.
	for (final CalibratedAxis axis : parentMeta.get(imageIndex)
		.getAxesPlanar())
	{
		final int parentIndex = parentMeta.get(imageIndex).getAxisIndex(axis
			.type());
		final int currentIndex = meta.get(imageIndex).getAxisIndex(axis.type());
		// This axis is still a planar axis, so we can read it from the
		// current plane offsets/lengths
		if (currentIndex >= 0 && currentIndex < meta.get(imageIndex)
			.getPlanarAxisCount())
		{
			lastPlaneMin[parentIndex] = bounds.min(currentIndex);
			lastPlaneMax[parentIndex] = bounds.max(currentIndex);
		}
		// This axis is a planar axis in the underlying metadata that was
		// split out, so we will insert a [0,length] range
		else if (parentMeta.get(imageIndex).getAxisIndex(axis.type()) < parentMeta
			.get(imageIndex).getPlanarAxisCount())
		{
			lastPlaneMin[parentIndex] = 0;
			lastPlaneMax[parentIndex] = parentMeta.get(imageIndex).getAxisLength(
				axis.type()) - 1;
		}
	}
}
 
Example 8
Source File: JPEGTileFormat.java    From scifio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public ByteArrayPlane openPlane(final int imageIndex, final long planeIndex,
	final ByteArrayPlane plane, final Interval bounds,
	final SCIFIOConfig config) throws FormatException, IOException
{
	final Metadata meta = getMetadata();
	final byte[] buf = plane.getBytes();
	final int xIndex = meta.get(imageIndex).getAxisIndex(Axes.X);
	final int yIndex = meta.get(imageIndex).getAxisIndex(Axes.Y);
	final int x = (int) bounds.min(xIndex);
	final int y = (int) bounds.min(yIndex);
	final int w = (int) bounds.dimension(xIndex);
	final int h = (int) bounds.dimension(yIndex);
	FormatTools.checkPlaneForReading(meta, imageIndex, planeIndex, buf.length,
		bounds);

	final int c = (int) meta.get(imageIndex).getAxisLength(Axes.CHANNEL);

	for (int ty = y; ty < y + h; ty++) {
		byte[] scanline = meta.getDecoder().getScanline(ty);
		if (scanline == null) {
			meta.getDecoder().initialize(getHandle(), 0);
			scanline = meta.getDecoder().getScanline(ty);
		}
		System.arraycopy(scanline, c * x, buf, (ty - y) * c * w, c * w);
	}

	return plane;
}
 
Example 9
Source File: SCIFIOMetadataTools.java    From scifio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns true if the provided axes correspond to a complete image row
 */
public static boolean wholeRow(final int imageIndex, final Metadata meta,
	final Interval bounds)
{
	final int yIndex = meta.get(imageIndex).getAxisIndex(Axes.Y);

	for (int d = 0; d < bounds.numDimensions(); d++) {
		if (d == yIndex) continue;
		final long length = meta.get(imageIndex).getAxisLength(d);
		if (bounds.min(d) != 0 || bounds.dimension(d) != length) return false;
	}

	return true;
}
 
Example 10
Source File: FourNeighborhoodExtrema.java    From BigStitcher with GNU General Public License v2.0 5 votes vote down vote up
/**
 * split the given Interval into nSplits intervals along the largest dimension
 * @param interval input interval
 * @param nSplits how may splits
 * @return list of intervals input was split into
 */
public static List<Interval> splitAlongLargestDimension(Interval interval, long nSplits){
	
	List<Interval> res = new ArrayList<Interval>();
	
	long[] min = new long[interval.numDimensions()];
	long[] max = new long[interval.numDimensions()];
	interval.min(min);
	interval.max(max);
	
	int splitDim = 0;
	for (int i = 0; i< interval.numDimensions(); i++){
		if (interval.dimension(i) > interval.dimension(splitDim)) splitDim = i;
	}

	// there could be more splits than actual dimension entries
	nSplits = Math.min( nSplits, interval.dimension(splitDim) );

	long chunkSize = interval.dimension(splitDim) / nSplits;
	long maxSplitDim = max[splitDim];
	
	for (int i = 0; i<nSplits; i++){
		if (i != 0){
			min[splitDim] += chunkSize;	
		}
		max[splitDim] = min[splitDim] + chunkSize - 1;
		if (i == nSplits -1){
			max[splitDim] = maxSplitDim;
		}
		res.add(new FinalInterval(min, max));
	}
		
	return res;
}
 
Example 11
Source File: FractalSpimDataGenerator.java    From BigStitcher with GNU General Public License v2.0 5 votes vote down vote up
/**
 * create SpimData containing Views at each Interval
 * @param intervals list of intervals
 * @return generated SpimData
 */
public SpimData generateSpimData(final List<Interval> intervals)
{
	final List<RealLocalizable> mins = new ArrayList<>();
	for(Interval iv : intervals)
	{
		RealPoint min = new RealPoint( iv.numDimensions() );
		iv.min( min );
		mins.add( min );
	}
	return generateSpimData( intervals, mins );
}
 
Example 12
Source File: FractalSpimDataGenerator.java    From BigStitcher with GNU General Public License v2.0 5 votes vote down vote up
public static List< RealLocalizable > getTileMins(List<Interval> intervals)
{
	final List<RealLocalizable> mins = new ArrayList<>();
	for(Interval iv : intervals)
	{
		RealPoint min = new RealPoint( iv.numDimensions() );
		iv.min( min );
		mins.add( min );
	}
	return mins;
}
 
Example 13
Source File: ICSFormat.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void writePlane(final int imageIndex, final long planeIndex,
	final Plane plane, final Interval bounds) throws FormatException,
	IOException
{
	checkParams(imageIndex, planeIndex, plane.getBytes(), bounds);
	final Metadata meta = getMetadata();
	final boolean interleaved = plane.getImageMetadata()
		.getInterleavedAxisCount() > 0;

	final int xAxis = meta.get(imageIndex).getAxisIndex(Axes.X);
	final int yAxis = meta.get(imageIndex).getAxisIndex(Axes.Y);
	final int x = (int) bounds.min(xAxis), y = (int) bounds.min(yAxis), //
			w = (int) bounds.dimension(xAxis), h = (int) bounds.dimension(yAxis);

	int rgbChannels = 1;

	if (meta.get(imageIndex).isMultichannel()) {
		final int cIndex = meta.get(imageIndex).getAxisIndex(Axes.CHANNEL);
		rgbChannels = (int) (bounds.dimension(cIndex));
	}

	final int sizeX = (int) meta.get(imageIndex).getAxisLength(Axes.X);
	final int pixelType = getMetadata().get(imageIndex).getPixelType();
	final int bytesPerPixel = FormatTools.getBytesPerPixel(pixelType);
	final int planeSize = (int) (meta.get(0).getSize() / meta.get(0)
		.getPlaneCount());

	pixels.seek(pixelOffset + planeIndex * planeSize);
	if (SCIFIOMetadataTools.wholePlane(imageIndex, meta, bounds) &&
		(interleaved || rgbChannels == 1))
	{
		pixels.write(plane.getBytes());
	}
	else {
		pixels.skipBytes(bytesPerPixel * rgbChannels * sizeX * y);
		for (int row = 0; row < h; row++) {
			final ByteArrayOutputStream strip = new ByteArrayOutputStream();
			for (int col = 0; col < w; col++) {
				for (int c = 0; c < rgbChannels; c++) {
					final int index = interleaved ? rgbChannels * (row * w + col) + c
						: w * (c * h + row) + col;
					strip.write(plane.getBytes(), index * bytesPerPixel,
						bytesPerPixel);
				}
			}
			pixels.skipBytes(bytesPerPixel * rgbChannels * x);
			pixels.write(strip.toByteArray());
			pixels.skipBytes(bytesPerPixel * rgbChannels * (sizeX - w - x));
		}
	}
	lastPlane = planeIndex;
}
 
Example 14
Source File: BMPFormat.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public ByteArrayPlane openPlane(final int imageIndex, final long planeIndex,
	final ByteArrayPlane plane, final Interval bounds,
	final SCIFIOConfig config) throws FormatException, IOException
{
	final Metadata meta = getMetadata();
	final ImageMetadata imageMeta = meta.get(imageIndex);
	final int xIndex = imageMeta.getAxisIndex(Axes.X);
	final int yIndex = imageMeta.getAxisIndex(Axes.Y);
	final int x = (int) bounds.min(xIndex);
	final int y = (int) bounds.min(yIndex);
	final int w = (int) bounds.dimension(xIndex);
	final int h = (int) bounds.dimension(yIndex);

	final byte[] buf = plane.getData();
	final int compression = meta.getCompression();
	final int bpp = imageMeta.getBitsPerPixel();
	final int sizeX = (int) imageMeta.getAxisLength(Axes.X);
	final int sizeY = (int) imageMeta.getAxisLength(Axes.Y);
	final int sizeC = (int) imageMeta.getAxisLength(Axes.CHANNEL);

	FormatTools.checkPlaneForReading(meta, imageIndex, planeIndex, buf.length,
		bounds);

	if (compression != RAW && getHandle().length() < FormatTools.getPlaneSize(
		this, imageIndex))
	{
		throw new UnsupportedCompressionException(compression +
			" not supported");
	}

	final int rowsToSkip = meta.isInvertY() ? y : sizeY - (h + y);
	final int rowLength = sizeX * (imageMeta.isIndexed() ? 1 : sizeC);
	getHandle().seek(meta.getGlobal() + rowsToSkip * rowLength);

	int pad = ((rowLength * bpp) / 8) % 2;
	if (pad == 0) {
		pad = ((rowLength * bpp) / 8) % 4;
	}
	else {
		pad *= sizeC;
	}
	int planeSize = sizeX * sizeC * h;
	if (bpp >= 8) planeSize *= (bpp / 8);
	else planeSize /= (8 / bpp);
	planeSize += pad * h;
	if (planeSize + getHandle().offset() > getHandle().length()) {
		planeSize -= (pad * h);

		// sometimes we have RGB images with a single padding byte
		if (planeSize + sizeY + getHandle().offset() <= getHandle().length()) {
			pad = 1;
			planeSize += h;
		}
		else {
			pad = 0;
		}
	}

	getHandle().skipBytes(rowsToSkip * pad);

	final byte[] rawPlane = new byte[planeSize];
	getHandle().read(rawPlane);

	final BitBuffer bb = new BitBuffer(rawPlane);

	final ColorTable palette = meta.getColorTable(0, 0);
	plane.setColorTable(palette);

	final int effectiveC = palette != null && palette.getLength() > 0 ? 1
		: sizeC;
	for (int row = h - 1; row >= 0; row--) {
		final int rowIndex = meta.isInvertY() ? h - 1 - row : row;
		bb.skipBits(x * bpp * effectiveC);
		for (int i = 0; i < w * effectiveC; i++) {
			if (bpp <= 8) {
				buf[rowIndex * w * effectiveC + i] = (byte) (bb.getBits(bpp) &
					0xff);
			}
			else {
				for (int b = 0; b < bpp / 8; b++) {
					buf[(bpp / 8) * (rowIndex * w * effectiveC + i) + b] = (byte) (bb
						.getBits(8) & 0xff);
				}
			}
		}
		if (row > 0) {
			bb.skipBits((sizeX - w - x) * bpp * effectiveC + pad * 8);
		}
	}

	if (imageMeta.getAxisLength(Axes.CHANNEL) > 1) {
		ImageTools.bgrToRgb(buf, imageMeta.getInterleavedAxisCount() > 0, 1,
			(int) imageMeta.getAxisLength(Axes.CHANNEL));
	}
	return plane;
}
 
Example 15
Source File: PCXFormat.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public ByteArrayPlane openPlane(final int imageIndex, final long planeIndex,
	final ByteArrayPlane plane, final Interval bounds,
	final SCIFIOConfig config) throws FormatException, IOException
{
	final Metadata meta = getMetadata();
	plane.setColorTable(meta.getColorTable(imageIndex, planeIndex));
	final byte[] buf = plane.getData();

	FormatTools.checkPlaneForReading(meta, imageIndex, planeIndex, buf.length,
		bounds);

	getHandle().seek(meta.getOffset());

	// PCX uses a simple RLE compression algorithm

	final byte[] b = new byte[meta.getBytesPerLine() * (int) meta.get(
		imageIndex).getAxisLength(Axes.Y) * meta.getnColorPlanes()];
	int pt = 0;
	while (pt < b.length) {
		int val = getHandle().read() & 0xff;
		if (((val & 0xc0) >> 6) == 3) {
			final int len = val & 0x3f;
			val = getHandle().read() & 0xff;
			for (int q = 0; q < len; q++) {
				b[pt++] = (byte) val;
				if ((pt % meta.getBytesPerLine()) == 0) {
					break;
				}
			}
		}
		else b[pt++] = (byte) (val & 0xff);
	}

	final int xIndex = meta.get(imageIndex).getAxisIndex(Axes.X);
	final int yIndex = meta.get(imageIndex).getAxisIndex(Axes.Y);
	final int x = (int) bounds.min(xIndex);
	final int y = (int) bounds.min(yIndex);
	final int w = (int) bounds.dimension(xIndex);
	final int h = (int) bounds.dimension(yIndex);
	final int src = y * meta.getnColorPlanes() * meta.getBytesPerLine();
	for (int row = 0; row < h; row++) {
		int rowOffset = row * meta.getnColorPlanes() * meta.getBytesPerLine();
		for (int c = 0; c < meta.getnColorPlanes(); c++) {
			System.arraycopy(b, src + rowOffset + x, buf, c * w * h + row * w, w);
			rowOffset += meta.getBytesPerLine();
		}
	}

	return plane;
}
 
Example 16
Source File: NativeQTFormat.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void writePlane(final int imageIndex, final long planeIndex,
	final Plane plane, final Interval bounds) throws FormatException,
	IOException
{
	final byte[] buf = plane.getBytes();
	checkParams(imageIndex, planeIndex, buf, bounds);
	if (needLegacy) {
		legacy.savePlane(imageIndex, planeIndex, plane, bounds);
		return;
	}

	final Metadata meta = getMetadata();
	final boolean interleaved = plane.getImageMetadata()
		.getInterleavedAxisCount() > 0;
	// get the width and height of the image
	final int width = (int) meta.get(imageIndex).getAxisLength(Axes.X);
	// 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

	final int nChannels = (int) meta.get(imageIndex).getAxisLength(
		Axes.CHANNEL);

	final int xIndex = meta.get(imageIndex).getAxisIndex(Axes.X);
	final int yIndex = meta.get(imageIndex).getAxisIndex(Axes.Y);
	final int x = (int) bounds.min(xIndex), y = (int) bounds.min(yIndex), //
			w = (int) bounds.dimension(xIndex), h = (int) bounds.dimension(
				yIndex);

	getHandle().seek(offsets.get((int) planeIndex) + 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

	final 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
		final byte[] tmp2 = new byte[tmp.length];
		System.arraycopy(tmp, 0, tmp2, 0, tmp.length);
		for (int i = 0; i < tmp.length; i++) {
			final int c = i / (w * h);
			final int index = i % (w * h);
			tmp[index * nChannels + c] = tmp2[i];
		}
	}

	final int rowLen = tmp.length / h;
	for (int row = 0; row < h; row++) {
		getHandle().skipBytes(nChannels * x);
		getHandle().write(tmp, row * rowLen, rowLen);
		for (int i = 0; i < pad; i++) {
			getHandle().writeByte(0);
		}
		if (row < h - 1) {
			getHandle().skipBytes(nChannels * (width - w - x));
		}
	}
	numWritten++;
}
 
Example 17
Source File: EPSFormat.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void writePlane(final int imageIndex, final long planeIndex,
	final Plane plane, final Interval bounds) throws FormatException,
	IOException
{

	final byte[] buf = plane.getBytes();
	final boolean interleaved = plane.getImageMetadata()
		.getInterleavedAxisCount() > 0;
	checkParams(imageIndex, planeIndex, buf, bounds);
	final ImageMetadata imageMetadata = getMetadata().get(imageIndex);
	final int xAxis = imageMetadata.getAxisIndex(Axes.X);
	final int yAxis = imageMetadata.getAxisIndex(Axes.Y);
	final int x = (int) bounds.min(xAxis);
	final int y = (int) bounds.min(yAxis);
	final int w = (int) bounds.dimension(xAxis);
	final int h = (int) bounds.dimension(yAxis);
	final int sizeX = (int) imageMetadata.getAxisLength(Axes.X);
	final int nChannels = (int) imageMetadata.getAxisLength(Axes.CHANNEL);

	// write pixel data
	// for simplicity, write 80 char lines

	final int planeSize = (int) (bounds.max(xAxis) * bounds.max(yAxis));

	final StringBuilder buffer = new StringBuilder();

	final int offset = y * sizeX * nChannels * 2;
	final DataHandle<Location> handle = getHandle();
	handle.seek(planeOffset + offset);
	for (int row = 0; row < h; row++) {
		handle.skipBytes(nChannels * x * 2);
		for (int col = 0; col < w * nChannels; col++) {
			final int i = row * w * nChannels + col;
			final int index = interleaved || nChannels == 1 ? i : (i %
				nChannels) * planeSize + (i / nChannels);
			final String s = Integer.toHexString(buf[index]);
			// only want last 2 characters of s
			if (s.length() > 1) buffer.append(s.substring(s.length() - 2));
			else {
				buffer.append("0");
				buffer.append(s);
			}
		}
		buffer.append('\n');
		handle.writeBytes(buffer.toString());
		buffer.delete(0, buffer.length());
		handle.skipBytes(nChannels * (sizeX - w - x) * 2);
	}

	// write footer

	handle.seek(handle.length());
	handle.writeBytes("\nshowpage\n");
}
 
Example 18
Source File: BlockSpec.java    From paintera with GNU General Public License v2.0 4 votes vote down vote up
public void fromInterval(final Interval interval)
{
	interval.min(min);
	interval.max(max);
	grid.getCellPosition(min, pos);
}
 
Example 19
Source File: MinimalTIFFFormat.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
		public ByteArrayPlane openPlane(final int imageIndex, final long planeIndex,
			final ByteArrayPlane plane, final Interval bounds,
			final SCIFIOConfig config) throws FormatException, IOException
		{
			final Metadata meta = getMetadata();
			plane.setColorTable(meta.getColorTable(imageIndex, planeIndex));
			final byte[] buf = plane.getBytes();
			final IFDList ifds = meta.getIfds();
			final TiffParser tiffParser = meta.getTiffParser();
			final int xIndex = meta.get(imageIndex).getAxisIndex(Axes.X);
			final int yIndex = meta.get(imageIndex).getAxisIndex(Axes.Y);
			final int x = (int) bounds.min(xIndex);
			final int y = (int) bounds.min(yIndex);
			final int w = (int) bounds.dimension(xIndex);
			final int h = (int) bounds.dimension(yIndex);
			FormatTools.checkPlaneForReading(meta, imageIndex, planeIndex, buf.length,
				bounds);

			final IFD firstIFD = ifds.get(0);
			meta.setLastPlane(planeIndex);
			final IFD ifd = ifds.get((int) planeIndex);
			if ((firstIFD.getCompression() == TiffCompression.JPEG_2000 || firstIFD
				.getCompression() == TiffCompression.JPEG_2000_LOSSY) && meta
					.getResolutionLevels() != null)
			{
				// FIXME: resolution levels
//        if (getCoreIndex() > 0) {
//          ifd = meta.getSubResolutionIFDs().get(planeIndex).get(getCoreIndex() - 1);
//        }
				setResolutionLevel(ifd);
			}

			tiffParser.getSamples(ifd, buf, x, y, w, h);

			final boolean float16 = meta.get(imageIndex)
				.getPixelType() == FormatTools.FLOAT && firstIFD
					.getBitsPerSample()[0] == 16;
			final boolean float24 = meta.get(imageIndex)
				.getPixelType() == FormatTools.FLOAT && firstIFD
					.getBitsPerSample()[0] == 24;

			if (float16 || float24) {
				final int nPixels = w * h * (int) meta.get(imageIndex).getAxisLength(
					Axes.CHANNEL);
				final int nBytes = float16 ? 2 : 3;
				final int mantissaBits = float16 ? 10 : 16;
				final int exponentBits = float16 ? 5 : 7;
				final int maxExponent = (int) Math.pow(2, exponentBits) - 1;
				final int bits = (nBytes * 8) - 1;

				final byte[] newBuf = new byte[buf.length];
				for (int i = 0; i < nPixels; i++) {
					final int v = Bytes.toInt(buf, i * nBytes, nBytes, meta.get(
						imageIndex).isLittleEndian());
					final int sign = v >> bits;
					int exponent = (v >> mantissaBits) & (int) (Math.pow(2,
						exponentBits) - 1);
					int mantissa = v & (int) (Math.pow(2, mantissaBits) - 1);

					if (exponent == 0) {
						if (mantissa != 0) {
							while ((mantissa & (int) Math.pow(2, mantissaBits)) == 0) {
								mantissa <<= 1;
								exponent--;
							}
							exponent++;
							mantissa &= (int) (Math.pow(2, mantissaBits) - 1);
							exponent += 127 - (Math.pow(2, exponentBits - 1) - 1);
						}
					}
					else if (exponent == maxExponent) {
						exponent = 255;
					}
					else {
						exponent += 127 - (Math.pow(2, exponentBits - 1) - 1);
					}

					mantissa <<= (23 - mantissaBits);

					final int value = (sign << 31) | (exponent << 23) | mantissa;
					Bytes.unpack(value, newBuf, i * 4, 4, meta.get(imageIndex)
						.isLittleEndian());
				}
				System.arraycopy(newBuf, 0, buf, 0, newBuf.length);
			}

			return plane;
		}
 
Example 20
Source File: RenderBoxHelperFX.java    From paintera with GNU General Public License v2.0 4 votes vote down vote up
public void renderBox(final Interval sourceInterval, final AffineTransform3D transform, final Path front, final
Path back)
{
	final double sX0 = sourceInterval.min(0);
	final double sX1 = sourceInterval.max(0);
	final double sY0 = sourceInterval.min(1);
	final double sY1 = sourceInterval.max(1);
	final double sZ0 = sourceInterval.min(2);
	final double sZ1 = sourceInterval.max(2);

	final double[] p000 = new double[] {sX0, sY0, sZ0};
	final double[] p100 = new double[] {sX1, sY0, sZ0};
	final double[] p010 = new double[] {sX0, sY1, sZ0};
	final double[] p110 = new double[] {sX1, sY1, sZ0};
	final double[] p001 = new double[] {sX0, sY0, sZ1};
	final double[] p101 = new double[] {sX1, sY0, sZ1};
	final double[] p011 = new double[] {sX0, sY1, sZ1};
	final double[] p111 = new double[] {sX1, sY1, sZ1};

	final double[] q000 = new double[3];
	final double[] q100 = new double[3];
	final double[] q010 = new double[3];
	final double[] q110 = new double[3];
	final double[] q001 = new double[3];
	final double[] q101 = new double[3];
	final double[] q011 = new double[3];
	final double[] q111 = new double[3];

	transform.apply(p000, q000);
	transform.apply(p100, q100);
	transform.apply(p010, q010);
	transform.apply(p110, q110);
	transform.apply(p001, q001);
	transform.apply(p101, q101);
	transform.apply(p011, q011);
	transform.apply(p111, q111);

	splitEdge(q000, q100, front, back);
	splitEdge(q100, q110, front, back);
	splitEdge(q110, q010, front, back);
	splitEdge(q010, q000, front, back);

	splitEdge(q001, q101, front, back);
	splitEdge(q101, q111, front, back);
	splitEdge(q111, q011, front, back);
	splitEdge(q011, q001, front, back);

	splitEdge(q000, q001, front, back);
	splitEdge(q100, q101, front, back);
	splitEdge(q110, q111, front, back);
	splitEdge(q010, q011, front, back);
}