Replaces the image, if any, with the one specified. Throws an IllegalStateException if an error occurs while loading the image.
(Image image)
| 728 | * while loading the image. |
| 729 | */ |
| 730 | public void setImage(Image image) { |
| 731 | if (image instanceof BufferedImage) { |
| 732 | BufferedImage bi = (BufferedImage)image; |
| 733 | int nBands = bi.getSampleModel().getNumBands(); |
| 734 | int type = bi.getType(); |
| 735 | boolean rgb = type==BufferedImage.TYPE_3BYTE_BGR||type==BufferedImage.TYPE_INT_RGB |
| 736 | ||type==BufferedImage.TYPE_4BYTE_ABGR||type==BufferedImage.TYPE_BYTE_INDEXED |
| 737 | ||type==BufferedImage.TYPE_INT_ARGB||type==BufferedImage.TYPE_INT_BGR; |
| 738 | int dataType = bi.getSampleModel().getDataType(); |
| 739 | if (IJ.debugMode) IJ.log("setImage: type="+type+", bands="+nBands+", rgb="+rgb+", datatype="+dataType); |
| 740 | if (nBands>1 && !rgb) { |
| 741 | ImageStack biStack = new ImageStack(bi.getWidth(), bi.getHeight()); |
| 742 | for (int b=0; b<nBands; b++) |
| 743 | biStack.addSlice(convertToImageProcessor(bi, b)); |
| 744 | setImage(new ImagePlus(getTitle(), biStack)); |
| 745 | return; |
| 746 | } |
| 747 | if (bi.getType()==BufferedImage.TYPE_USHORT_GRAY) { |
| 748 | setProcessor(null, new ShortProcessor(bi)); |
| 749 | return; |
| 750 | } else if (bi.getType()==BufferedImage.TYPE_BYTE_GRAY) { |
| 751 | setProcessor(null, new ByteProcessor(bi)); |
| 752 | return; |
| 753 | } |
| 754 | } |
| 755 | roi = null; |
| 756 | errorLoadingImage = false; |
| 757 | waitForImage(image); |
| 758 | if (errorLoadingImage) |
| 759 | throw new IllegalStateException ("Error loading image"); |
| 760 | int newWidth = image.getWidth(ij); |
| 761 | int newHeight = image.getHeight(ij); |
| 762 | boolean dimensionsChanged = newWidth!=width || newHeight!=height; |
| 763 | width = newWidth; |
| 764 | height = newHeight; |
| 765 | setStackNull(); |
| 766 | LookUpTable lut = new LookUpTable(image); |
| 767 | int type = lut.getMapSize()>0?GRAY8:COLOR_RGB; |
| 768 | if (image!=null && type==COLOR_RGB) |
| 769 | ip = new ColorProcessor(image); |
| 770 | if (ip==null && image!=null) |
| 771 | ip = new ByteProcessor(image); |
| 772 | setType(type); |
| 773 | this.img = ip.createImage(); |
| 774 | if (win!=null) { |
| 775 | if (dimensionsChanged) |
| 776 | win = new ImageWindow(this); |
| 777 | else |
| 778 | repaintWindow(); |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | /** |
| 783 | * Extract pixels as an ImageProcessor from a single band of a BufferedImage. |
no test coverage detected