Returns a copy the current image or stack slice, cropped if there is a selection. @see ij.ImagePlus#crop @see ij.ImagePlus#crop(String)
(ImagePlus imp)
| 312 | * @see ij.ImagePlus#crop(String) |
| 313 | */ |
| 314 | public ImagePlus crop(ImagePlus imp) { |
| 315 | if (imp.getNChannels()>1 && imp.getCompositeMode()==IJ.COMPOSITE) { |
| 316 | int z = imp.getSlice(); |
| 317 | int t = imp.getFrame(); |
| 318 | return run(imp, 1, imp.getNChannels(), z, z, t, t); |
| 319 | } |
| 320 | boolean hyperstack = imp.isHyperStack(); |
| 321 | int displayMode = imp.isComposite()?imp.getDisplayMode():0; |
| 322 | ImageProcessor ip = imp.getProcessor(); |
| 323 | ImageProcessor ip2 = ip.crop(); |
| 324 | ImagePlus imp2 = imp.createImagePlus(); |
| 325 | imp2.setProcessor("DUP_"+imp.getTitle(), ip2); |
| 326 | String info = (String)imp.getProperty("Info"); |
| 327 | if (info!=null) |
| 328 | imp2.setProperty("Info", info); |
| 329 | imp2.setProperties(imp.getPropertiesAsArray()); |
| 330 | if (imp.hasImageStack()) { |
| 331 | ImageStack stack = imp.getStack(); |
| 332 | String label = stack.getSliceLabel(imp.getCurrentSlice()); |
| 333 | if (label!=null) { |
| 334 | if (label.length()>250 && label.indexOf('\n')>0 && label.contains("0002,")) |
| 335 | imp2.setProperty("Info", label); // DICOM metadata |
| 336 | else |
| 337 | imp2.setProp("Slice_Label", label); |
| 338 | } |
| 339 | if (imp.isComposite()) { |
| 340 | LUT lut = ((CompositeImage)imp).getChannelLut(); |
| 341 | if (displayMode==IJ.GRAYSCALE) |
| 342 | imp2.getProcessor().setColorModel(null); |
| 343 | else |
| 344 | imp2.getProcessor().setColorModel(lut); |
| 345 | } |
| 346 | } else { |
| 347 | String label = imp.getProp("Slice_Label"); |
| 348 | if (label!=null) |
| 349 | imp2.setProp("Slice_Label", label); |
| 350 | } |
| 351 | Overlay overlay = imp.getOverlay(); |
| 352 | if (overlay!=null && !imp.getHideOverlay()) { |
| 353 | Overlay overlay2 = overlay.crop(ip.getRoi()); |
| 354 | if (imp.getStackSize()>1) { |
| 355 | if (hyperstack) { |
| 356 | int c = imp.getC(); |
| 357 | int z = imp.getZ(); |
| 358 | int t = imp.getT(); |
| 359 | overlay2.crop(c,c,z,z,t,t); |
| 360 | } else |
| 361 | overlay2.crop(imp.getCurrentSlice(), imp.getCurrentSlice()); |
| 362 | } |
| 363 | imp2.setOverlay(overlay2); |
| 364 | } |
| 365 | return imp2; |
| 366 | } |
| 367 | |
| 368 | /** Returns a new stack containing a subrange of the specified stack. */ |
| 369 | public ImagePlus run(ImagePlus imp, int firstSlice, int lastSlice) { |
no test coverage detected