(String path)
| 20 | public class PNM_Writer implements PlugIn { |
| 21 | |
| 22 | public void run(String path) { |
| 23 | ImagePlus img=IJ.getImage(); |
| 24 | boolean isGray = false; |
| 25 | String extension = null; |
| 26 | ImageProcessor ip = img.getProcessor(); |
| 27 | if (img.getBitDepth()==24) |
| 28 | extension = ".pnm"; |
| 29 | else { |
| 30 | if (img.getBitDepth()==8&& ip.isInvertedLut()) { |
| 31 | ip = ip.duplicate(); |
| 32 | ip.invert(); |
| 33 | } |
| 34 | if (img.getBitDepth()!=16) |
| 35 | ip = ip.convertToByte(true); |
| 36 | isGray = true; |
| 37 | extension = ".pgm"; |
| 38 | } |
| 39 | String title=img.getTitle(); |
| 40 | int length=title.length(); |
| 41 | for (int i=2;i<5;i++) |
| 42 | if (length>i+1 && title.charAt(length-i)=='.') { |
| 43 | title=title.substring(0,length-i); |
| 44 | break; |
| 45 | } |
| 46 | if (path==null || path.equals("")) { |
| 47 | SaveDialog od = new SaveDialog("PNM Writer", title, extension); |
| 48 | String dir=od.getDirectory(); |
| 49 | String name=od.getFileName(); |
| 50 | if (name==null) |
| 51 | return; |
| 52 | path = dir + name; |
| 53 | } |
| 54 | IJ.showStatus("Writing PNM "+path+"..."); |
| 55 | if (img.getBitDepth()==16) { |
| 56 | save16BitImage(ip, path); |
| 57 | return; |
| 58 | } |
| 59 | try { |
| 60 | OutputStream fileOutput = new FileOutputStream(path); |
| 61 | DataOutputStream output = new DataOutputStream(fileOutput); |
| 62 | int w = img.getWidth(), h = img.getHeight(); |
| 63 | output.writeBytes((isGray ? "P5" : "P6") |
| 64 | + "\n# Written by ImageJ PNM Writer\n" |
| 65 | + w + " " + h + "\n255\n"); |
| 66 | if (isGray) |
| 67 | output.write((byte[])ip.getPixels(), 0, w*h); |
| 68 | else { |
| 69 | byte[] pixels = new byte[w * h * 3]; |
| 70 | ColorProcessor proc = |
| 71 | (ColorProcessor)ip; |
| 72 | for (int j = 0; j < h; j++) |
| 73 | for (int i = 0; i < w; i++) { |
| 74 | int c = proc.getPixel(i, j); |
| 75 | pixels[3 * (i + w * j) + 0] = |
| 76 | (byte)((c & 0xff0000) >> 16); |
| 77 | pixels[3 * (i + w * j) + 1] = |
| 78 | (byte)((c & 0xff00) >> 8); |
| 79 | pixels[3 * (i + w * j) + 2] = |
nothing calls this directly
no test coverage detected