/** Opens a stack of images. */ ImagePlus openStack(ColorModel cm, boolean show) { ImageStack stack = new ImageStack(fi.width, fi.height, cm); long skip = fi.getOffset(); Object pixels; try { ImageReader reader = new ImageReader(fi); InputStream is = createInputStream(fi); if (is == null) return null; IJ.resetEscape(); for (int i = 1; i <= fi.nImages; i++) { if (!silentMode) IJ.showStatus("Reading: " + i + "/" + fi.nImages); if (IJ.escapePressed()) { IJ.beep(); IJ.showProgress(1.0); silentMode = false; return null; } pixels = reader.readPixels(is, skip); if (pixels == null) break; stack.addSlice(null, pixels); skip = fi.gapBetweenImages; if (!silentMode) IJ.showProgress(i, fi.nImages); } is.close(); } catch (Exception e) { IJ.log("" + e); } catch (OutOfMemoryError e) { IJ.outOfMemory(fi.fileName); stack.trim(); } if (!silentMode) IJ.showProgress(1.0); if (stack.getSize() == 0) return null; if (fi.sliceLabels != null && fi.sliceLabels.length <= stack.getSize()) { for (int i = 0; i < fi.sliceLabels.length; i++) stack.setSliceLabel(fi.sliceLabels[i], i + 1); } ImagePlus imp = new ImagePlus(fi.fileName, stack); if (fi.info != null) imp.setProperty("Info", fi.info); if (show) imp.show(); imp.setFileInfo(fi); setCalibration(imp); ImageProcessor ip = imp.getProcessor(); if (ip.getMin() == ip.getMax()) // find stack min and max if first slice is blank setStackDisplayRange(imp); if (!silentMode) IJ.showProgress(1.0); silentMode = false; return imp; }
public void run(String arg) { imp = IJ.getImage(); Roi roi = imp.getRoi(); if (roi != null && !roi.isArea()) imp.killRoi(); // ignore any line selection ImageProcessor ip = imp.getProcessor(); if (!showDialog(ip)) return; if (ip.getWidth() > 1 && ip.getHeight() > 1) ip.setInterpolate(interpolate); else ip.setInterpolate(false); ip.setBackgroundValue(bgValue); imp.startTiming(); try { if (newWindow && imp.getStackSize() > 1 && processStack) createNewStack(imp, ip); else scale(ip); } catch (OutOfMemoryError o) { IJ.outOfMemory("Scale"); } IJ.showProgress(1.0); }
// Use double buffer to reduce flicker when drawing complex ROIs. // Author: Erik Meijering void paintDoubleBuffered(Graphics g) { final int srcRectWidthMag = (int) (srcRect.width * magnification); final int srcRectHeightMag = (int) (srcRect.height * magnification); if (offScreenImage == null || offScreenWidth != srcRectWidthMag || offScreenHeight != srcRectHeightMag) { offScreenImage = createImage(srcRectWidthMag, srcRectHeightMag); offScreenWidth = srcRectWidthMag; offScreenHeight = srcRectHeightMag; } Roi roi = imp.getRoi(); try { if (imageUpdated) { imageUpdated = false; imp.updateImage(); } Graphics offScreenGraphics = offScreenImage.getGraphics(); Java2.setBilinearInterpolation(offScreenGraphics, Prefs.interpolateScaledImages); Image img = imp.getImage(); if (img != null) offScreenGraphics.drawImage( img, 0, 0, srcRectWidthMag, srcRectHeightMag, srcRect.x, srcRect.y, srcRect.x + srcRect.width, srcRect.y + srcRect.height, null); if (overlay != null) drawOverlay(offScreenGraphics); if (showAllROIs) drawAllROIs(offScreenGraphics); if (roi != null) drawRoi(roi, offScreenGraphics); if (srcRect.width < imageWidth || srcRect.height < imageHeight) drawZoomIndicator(offScreenGraphics); if (IJ.debugMode) showFrameRate(offScreenGraphics); g.drawImage(offScreenImage, 0, 0, null); } catch (OutOfMemoryError e) { IJ.outOfMemory("Paint"); } }
public void paint(Graphics g) { Roi roi = imp.getRoi(); if (roi != null || showAllROIs || overlay != null) { if (roi != null) roi.updatePaste(); if (!IJ.isMacOSX() && imageWidth != 0) { paintDoubleBuffered(g); return; } } try { if (imageUpdated) { imageUpdated = false; imp.updateImage(); } Java2.setBilinearInterpolation(g, Prefs.interpolateScaledImages); Image img = imp.getImage(); if (img != null) g.drawImage( img, 0, 0, (int) (srcRect.width * magnification), (int) (srcRect.height * magnification), srcRect.x, srcRect.y, srcRect.x + srcRect.width, srcRect.y + srcRect.height, null); if (overlay != null) drawOverlay(g); if (showAllROIs) drawAllROIs(g); if (roi != null) drawRoi(roi, g); if (srcRect.width < imageWidth || srcRect.height < imageHeight) drawZoomIndicator(g); if (IJ.debugMode) showFrameRate(g); } catch (OutOfMemoryError e) { IJ.outOfMemory("Paint"); } }
/** Attemps to open a tiff file as a stack. Returns an ImagePlus object if successful. */ public ImagePlus openTiffStack(FileInfo[] info) { if (info.length > 1 && !allSameSizeAndType(info)) return null; FileInfo fi = info[0]; if (fi.nImages > 1) return new FileOpener(fi).open(false); // open contiguous images as stack else { ColorModel cm = createColorModel(fi); ImageStack stack = new ImageStack(fi.width, fi.height, cm); Object pixels = null; long skip = fi.getOffset(); int imageSize = fi.width * fi.height * fi.getBytesPerPixel(); if (info[0].fileType == FileInfo.GRAY12_UNSIGNED) { imageSize = (int) (fi.width * fi.height * 1.5); if ((imageSize & 1) == 1) imageSize++; // add 1 if odd } if (info[0].fileType == FileInfo.BITMAP) { int scan = (int) Math.ceil(fi.width / 8.0); imageSize = scan * fi.height; } long loc = 0L; int nChannels = 1; try { InputStream is = createInputStream(fi); ImageReader reader = new ImageReader(fi); IJ.resetEscape(); for (int i = 0; i < info.length; i++) { nChannels = 1; Object[] channels = null; if (!silentMode) IJ.showStatus("Reading: " + (i + 1) + "/" + info.length); if (IJ.escapePressed()) { IJ.beep(); IJ.showProgress(1.0); return null; } fi.stripOffsets = info[i].stripOffsets; fi.stripLengths = info[i].stripLengths; int bpp = info[i].getBytesPerPixel(); if (info[i].samplesPerPixel > 1 && !(bpp == 3 || bpp == 4 || bpp == 6)) { nChannels = fi.samplesPerPixel; channels = new Object[nChannels]; for (int c = 0; c < nChannels; c++) { pixels = reader.readPixels(is, c == 0 ? skip : 0L); channels[c] = pixels; } } else pixels = reader.readPixels(is, skip); if (pixels == null && channels == null) break; loc += imageSize * nChannels + skip; if (i < (info.length - 1)) { skip = info[i + 1].getOffset() - loc; if (info[i + 1].compression >= FileInfo.LZW) skip = 0; if (skip < 0L) { IJ.error("Opener", "Unexpected image offset"); break; } } if (fi.fileType == FileInfo.RGB48) { Object[] pixels2 = (Object[]) pixels; stack.addSlice(null, pixels2[0]); stack.addSlice(null, pixels2[1]); stack.addSlice(null, pixels2[2]); isRGB48 = true; } else if (nChannels > 1) { for (int c = 0; c < nChannels; c++) { if (channels[c] != null) stack.addSlice(null, channels[c]); } } else stack.addSlice(null, pixels); IJ.showProgress(i, info.length); } is.close(); } catch (Exception e) { IJ.handleException(e); } catch (OutOfMemoryError e) { IJ.outOfMemory(fi.fileName); stack.deleteLastSlice(); stack.deleteLastSlice(); } IJ.showProgress(1.0); if (stack.getSize() == 0) return null; if (fi.fileType == FileInfo.GRAY16_UNSIGNED || fi.fileType == FileInfo.GRAY12_UNSIGNED || fi.fileType == FileInfo.GRAY32_FLOAT || fi.fileType == FileInfo.RGB48) { ImageProcessor ip = stack.getProcessor(1); ip.resetMinAndMax(); stack.update(ip); } // if (fi.whiteIsZero) // new StackProcessor(stack, stack.getProcessor(1)).invert(); ImagePlus imp = new ImagePlus(fi.fileName, stack); new FileOpener(fi).setCalibration(imp); imp.setFileInfo(fi); if (fi.info != null) imp.setProperty("Info", fi.info); if (fi.description != null && fi.description.contains("order=zct")) new HyperStackConverter().shuffle(imp, HyperStackConverter.ZCT); int stackSize = stack.getSize(); if (nChannels > 1 && (stackSize % nChannels) == 0) { imp.setDimensions(nChannels, stackSize / nChannels, 1); imp = new CompositeImage(imp, IJ.COMPOSITE); imp.setOpenAsHyperStack(true); } else if (imp.getNChannels() > 1) imp = makeComposite(imp, fi); IJ.showProgress(1.0); return imp; } }