Mercurial > hg > digilib-old
comparison common-imagej/src/main/java/digilib/image/ImageJ1DocuImage.java @ 960:b2d97b842612
moved DocuImage implementations with non-standard toolkits (JAI, ImgeJ) into separate Maven modules.
default build uses jai-imageio (now only one additional JAR in webapp!).
| author | robcast |
|---|---|
| date | Wed, 25 Jan 2012 15:13:06 +0100 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 959:f197e7760154 | 960:b2d97b842612 |
|---|---|
| 1 /** | |
| 2 * | |
| 3 */ | |
| 4 package digilib.image; | |
| 5 | |
| 6 import ij.IJ; | |
| 7 import ij.ImagePlus; | |
| 8 import ij.plugin.JpegWriter; | |
| 9 import ij.plugin.PNG_Writer; | |
| 10 import ij.process.ImageProcessor; | |
| 11 | |
| 12 import java.io.File; | |
| 13 import java.io.FileInputStream; | |
| 14 import java.io.IOException; | |
| 15 import java.io.OutputStream; | |
| 16 import java.util.Arrays; | |
| 17 import java.util.Iterator; | |
| 18 | |
| 19 import digilib.io.FileOpException; | |
| 20 import digilib.io.FileOps; | |
| 21 import digilib.io.ImageInput; | |
| 22 import digilib.util.ImageSize; | |
| 23 | |
| 24 /** Implementation of DocuImage using ImageJ version 1. | |
| 25 * | |
| 26 * @author casties | |
| 27 * | |
| 28 */ | |
| 29 public class ImageJ1DocuImage extends ImageInfoDocuImage { | |
| 30 | |
| 31 protected ImagePlus img; | |
| 32 protected ImageProcessor proc; | |
| 33 | |
| 34 /* returns a list of supported image formats */ | |
| 35 public Iterator<String> getSupportedFormats() { | |
| 36 String[] formats = new String[] {"JPG","PNG","TIFF"}; | |
| 37 return Arrays.asList(formats).iterator(); | |
| 38 } | |
| 39 | |
| 40 /* Check image size and type and store in ImageInput */ | |
| 41 public ImageInput identify(ImageInput input) throws IOException { | |
| 42 // try parent method first | |
| 43 ImageInput ii = super.identify(input); | |
| 44 if (ii != null) { | |
| 45 return ii; | |
| 46 } | |
| 47 logger.debug("identifying (ImageJ1) " + input); | |
| 48 String path = input.getFile().getAbsolutePath(); | |
| 49 img = IJ.openImage(path); | |
| 50 // set size | |
| 51 ImageSize d = new ImageSize(img.getWidth(), img.getHeight()); | |
| 52 input.setSize(d); | |
| 53 // set mime type | |
| 54 if (input.getMimetype() == null) { | |
| 55 String t = FileOps.mimeForFile(input.getFile()); | |
| 56 input.setMimetype(t); | |
| 57 } | |
| 58 return input; | |
| 59 } | |
| 60 | |
| 61 /* (non-Javadoc) | |
| 62 * @see digilib.image.DocuImageImpl#loadImage(digilib.io.ImageInput) | |
| 63 */ | |
| 64 @Override | |
| 65 public void loadImage(ImageInput ii) throws FileOpException { | |
| 66 this.input = ii; | |
| 67 String path = ii.getFile().getAbsolutePath(); | |
| 68 img = IJ.openImage(path); | |
| 69 proc = img.getProcessor(); | |
| 70 } | |
| 71 | |
| 72 /* (non-Javadoc) | |
| 73 * @see digilib.image.DocuImageImpl#crop(int, int, int, int) | |
| 74 */ | |
| 75 @Override | |
| 76 public void crop(int xoff, int yoff, int width, int height) | |
| 77 throws ImageOpException { | |
| 78 proc.setRoi(xoff, yoff, width, height); | |
| 79 ImageProcessor croppedProc = proc.crop(); | |
| 80 proc = croppedProc; | |
| 81 } | |
| 82 | |
| 83 /* (non-Javadoc) | |
| 84 * @see digilib.image.DocuImageImpl#scale(double, double) | |
| 85 */ | |
| 86 @Override | |
| 87 public void scale(double scaleX, double scaleY) throws ImageOpException { | |
| 88 int newWidth = (int) Math.round(proc.getWidth() * scaleX); | |
| 89 int newHeight = (int) Math.round(proc.getHeight() * scaleY); | |
| 90 ImageProcessor scaledProc = proc.resize(newWidth, newHeight, false); | |
| 91 proc = scaledProc; | |
| 92 } | |
| 93 | |
| 94 /* (non-Javadoc) | |
| 95 * @see digilib.image.DocuImageImpl#writeImage(java.lang.String, java.io.OutputStream) | |
| 96 */ | |
| 97 @Override | |
| 98 public void writeImage(String mt, OutputStream ostream) | |
| 99 throws ImageOpException, FileOpException { | |
| 100 File outFile; | |
| 101 String filext = ".jpg"; | |
| 102 if (mt.equals("image/png")) { | |
| 103 filext = ".png"; | |
| 104 } | |
| 105 try { | |
| 106 outFile = File.createTempFile("imgj_temp", filext); | |
| 107 } catch (IOException e) { | |
| 108 throw new FileOpException(e.toString()); | |
| 109 } | |
| 110 // save image to file | |
| 111 logger.debug("writeImage: mt="+mt); | |
| 112 if (mt.equals("image/png")) { | |
| 113 PNG_Writer writer = new PNG_Writer(); | |
| 114 try { | |
| 115 img = new ImagePlus("Image", proc); | |
| 116 writer.writeImage(img, outFile.getAbsolutePath(), 0); | |
| 117 } catch (Exception e) { | |
| 118 // TODO Auto-generated catch block | |
| 119 e.printStackTrace(); | |
| 120 } | |
| 121 } else { | |
| 122 img = new ImagePlus("Image", proc); | |
| 123 JpegWriter.save(img, outFile.getAbsolutePath(), 70); | |
| 124 } | |
| 125 // now send file | |
| 126 FileInputStream inFile = null; | |
| 127 try { | |
| 128 inFile = new FileInputStream(outFile); | |
| 129 byte dataBuffer[] = new byte[4096]; | |
| 130 int len; | |
| 131 while ((len = inFile.read(dataBuffer)) != -1) { | |
| 132 // copy out file | |
| 133 ostream.write(dataBuffer, 0, len); | |
| 134 } | |
| 135 } catch (IOException e) { | |
| 136 throw new FileOpException(e.toString()); | |
| 137 } finally { | |
| 138 try { | |
| 139 if (inFile != null) { | |
| 140 inFile.close(); | |
| 141 } | |
| 142 } catch (IOException e) { | |
| 143 // nothing to do | |
| 144 } | |
| 145 } | |
| 146 } | |
| 147 | |
| 148 } |
