byte[] uncompress(byte[] input) { if (fi.compression == FileInfo.PACK_BITS) return packBitsUncompress(input, fi.rowsPerStrip * fi.width * fi.getBytesPerPixel()); else if (fi.compression == FileInfo.LZW || fi.compression == FileInfo.LZW_WITH_DIFFERENCING) return lzwUncompress(input); else if (fi.compression == FileInfo.ZIP) return zipUncompress(input); else return input; }
/** Returns an InputStream for the image described by this FileInfo. */ public InputStream createInputStream(FileInfo fi) throws IOException, MalformedURLException { InputStream is = null; boolean gzip = fi.fileName != null && (fi.fileName.endsWith(".gz") || fi.fileName.endsWith(".GZ")); if (fi.inputStream != null) is = fi.inputStream; else if (fi.url != null && !fi.url.equals("")) is = new URL(fi.url + fi.fileName).openStream(); else { if (fi.directory.length() > 0 && !fi.directory.endsWith(Prefs.separator)) fi.directory += Prefs.separator; File f = new File(fi.directory + fi.fileName); if (gzip) fi.compression = FileInfo.COMPRESSION_UNKNOWN; if (f == null || f.isDirectory() || !validateFileInfo(f, fi)) is = null; else is = new FileInputStream(f); } if (is != null) { if (fi.compression >= FileInfo.LZW) is = new RandomAccessStream(is); else if (gzip) is = new GZIPInputStream(is, 50000); } return is; }
static boolean validateFileInfo(File f, FileInfo fi) { long offset = fi.getOffset(); long length = 0; if (fi.width <= 0 || fi.height < 0) { error("Width or height <= 0.", fi, offset, length); return false; } if (offset >= 0 && offset < 1000L) return true; if (offset < 0L) { error("Offset is negative.", fi, offset, length); return false; } if (fi.fileType == FileInfo.BITMAP || fi.compression != FileInfo.COMPRESSION_NONE) return true; length = f.length(); long size = fi.width * fi.height * fi.getBytesPerPixel(); size = fi.nImages > 1 ? size : size / 4; if (fi.height == 1) size = 0; // allows plugins to read info of unknown length at end of file if (offset + size > length) { error("Offset + image size > file length.", fi, offset, length); return false; } return true; }
/** 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; }
Object readRGB48(InputStream in) throws IOException { if (fi.compression > FileInfo.COMPRESSION_NONE) return readCompressedRGB48(in); int channels = fi.samplesPerPixel; short[][] stack = new short[channels][nPixels]; DataInputStream dis = new DataInputStream(in); int pixel = 0; int min = 65535, max = 0; if (fi.stripLengths == null) { fi.stripLengths = new int[fi.stripOffsets.length]; fi.stripLengths[0] = width * height * bytesPerPixel; } for (int i = 0; i < fi.stripOffsets.length; i++) { if (i > 0) { long skip = (fi.stripOffsets[i] & 0xffffffffL) - (fi.stripOffsets[i - 1] & 0xffffffffL) - fi.stripLengths[i - 1]; if (skip > 0L) dis.skip(skip); } int len = fi.stripLengths[i]; int bytesToGo = (nPixels - pixel) * channels * 2; if (len > bytesToGo) len = bytesToGo; byte[] buffer = new byte[len]; dis.readFully(buffer); int value; int channel = 0; boolean intel = fi.intelByteOrder; for (int base = 0; base < len; base += 2) { if (intel) value = ((buffer[base + 1] & 0xff) << 8) | (buffer[base] & 0xff); else value = ((buffer[base] & 0xff) << 8) | (buffer[base + 1] & 0xff); if (value < min) min = value; if (value > max) max = value; stack[channel][pixel] = (short) (value); channel++; if (channel == channels) { channel = 0; pixel++; } } showProgress(i + 1, fi.stripOffsets.length); } this.min = min; this.max = max; return stack; }
static void error(String msg, FileInfo fi, long offset, long length) { IJ.error( "FileOpener", "FileInfo parameter error. \n" + msg + "\n \n" + " Width: " + fi.width + "\n" + " Height: " + fi.height + "\n" + " Offset: " + offset + "\n" + " Bytes/pixel: " + fi.getBytesPerPixel() + "\n" + (length > 0 ? " File length: " + length + "\n" : "")); }
public Properties decodeDescriptionString(FileInfo fi) { if (fi.description == null || fi.description.length() < 7) return null; if (IJ.debugMode) IJ.log("Image Description: " + new String(fi.description).replace('\n', ' ')); if (!fi.description.startsWith("ImageJ")) return null; Properties props = new Properties(); InputStream is = new ByteArrayInputStream(fi.description.getBytes()); try { props.load(is); is.close(); } catch (IOException e) { return null; } fi.unit = props.getProperty("unit", ""); Double n = getNumber(props, "cf"); if (n != null) fi.calibrationFunction = n.intValue(); double c[] = new double[5]; int count = 0; for (int i = 0; i < 5; i++) { n = getNumber(props, "c" + i); if (n == null) break; c[i] = n.doubleValue(); count++; } if (count >= 2) { fi.coefficients = new double[count]; for (int i = 0; i < count; i++) fi.coefficients[i] = c[i]; } fi.valueUnit = props.getProperty("vunit"); n = getNumber(props, "images"); if (n != null && n.doubleValue() > 1.0) fi.nImages = (int) n.doubleValue(); if (fi.nImages > 1) { double spacing = getDouble(props, "spacing"); if (spacing != 0.0) { if (spacing < 0) spacing = -spacing; fi.pixelDepth = spacing; } } return props; }
public Hashtable processData( ServletInputStream is, String boundary, String saveInDir, int clength) throws IllegalArgumentException, IOException { if (is == null) throw new IllegalArgumentException("InputStream"); if (boundary == null || boundary.trim().length() < 1) throw new IllegalArgumentException("\"" + boundary + "\" is an illegal boundary indicator"); boundary = "--" + boundary; StringTokenizer stLine = null, stFields = null; FileInfo fileInfo = null; Hashtable dataTable = new Hashtable(5); String line = null, field = null, paramName = null; boolean saveFiles = (saveInDir != null && saveInDir.trim().length() > 0); boolean isFile = false; if (saveFiles) { // Create the required directory (including parent dirs) File f = new File(saveInDir); f.mkdirs(); } line = getLine(is); if (line == null || !line.startsWith(boundary)) throw new IOException("Boundary not found; boundary = " + boundary + ", line = " + line); while (line != null) { if (line == null || !line.startsWith(boundary)) return dataTable; line = getLine(is); if (line == null) return dataTable; stLine = new StringTokenizer(line, ";\r\n"); if (stLine.countTokens() < 2) throw new IllegalArgumentException("Bad data in second line"); line = stLine.nextToken().toLowerCase(); if (line.indexOf("form-data") < 0) throw new IllegalArgumentException("Bad data in second line"); stFields = new StringTokenizer(stLine.nextToken(), "=\""); if (stFields.countTokens() < 2) throw new IllegalArgumentException("Bad data in second line"); fileInfo = new FileInfo(); stFields.nextToken(); paramName = stFields.nextToken(); isFile = false; if (stLine.hasMoreTokens()) { field = stLine.nextToken(); stFields = new StringTokenizer(field, "=\""); if (stFields.countTokens() > 1) { if (stFields.nextToken().trim().equalsIgnoreCase("filename")) { fileInfo.name = paramName; String value = stFields.nextToken(); if (value != null && value.trim().length() > 0) { fileInfo.clientFileName = value; isFile = true; } else { line = getLine(is); // Skip "Content-Type:" line line = getLine(is); // Skip blank line line = getLine(is); // Skip blank line line = getLine(is); // Position to boundary line continue; } } } else if (field.toLowerCase().indexOf("filename") >= 0) { line = getLine(is); // Skip "Content-Type:" line line = getLine(is); // Skip blank line line = getLine(is); // Skip blank line line = getLine(is); // Position to boundary line continue; } } boolean skipBlankLine = true; if (isFile) { line = getLine(is); if (line == null) return dataTable; if (line.trim().length() < 1) skipBlankLine = false; else { stLine = new StringTokenizer(line, ": "); if (stLine.countTokens() < 2) throw new IllegalArgumentException("Bad data in third line"); stLine.nextToken(); // Content-Type fileInfo.fileContentType = stLine.nextToken(); } } if (skipBlankLine) { line = getLine(is); if (line == null) return dataTable; } if (!isFile) { line = getLine(is); if (line == null) return dataTable; dataTable.put(paramName, line); // If parameter is dir, change saveInDir to dir if (paramName.equals("dir")) saveInDir = line; line = getLine(is); continue; } try { UplInfo uplInfo = new UplInfo(clength); UploadMonitor.set(fileInfo.clientFileName, uplInfo); OutputStream os = null; String path = null; if (saveFiles) os = new FileOutputStream(path = getFileName(saveInDir, fileInfo.clientFileName)); else os = new ByteArrayOutputStream(ONE_MB); boolean readingContent = true; byte previousLine[] = new byte[2 * ONE_MB]; byte temp[] = null; byte currentLine[] = new byte[2 * ONE_MB]; int read, read3; if ((read = is.readLine(previousLine, 0, previousLine.length)) == -1) { line = null; break; } while (readingContent) { if ((read3 = is.readLine(currentLine, 0, currentLine.length)) == -1) { line = null; uplInfo.aborted = true; break; } if (compareBoundary(boundary, currentLine)) { os.write(previousLine, 0, read - 2); line = new String(currentLine, 0, read3); break; } else { os.write(previousLine, 0, read); uplInfo.currSize += read; temp = currentLine; currentLine = previousLine; previousLine = temp; read = read3; } // end else } // end while os.flush(); os.close(); if (!saveFiles) { ByteArrayOutputStream baos = (ByteArrayOutputStream) os; fileInfo.setFileContents(baos.toByteArray()); } else fileInfo.file = new File(path); dataTable.put(paramName, fileInfo); uplInfo.currSize = uplInfo.totalSize; } // end try catch (IOException e) { throw e; } } return dataTable; }
/** * Reads the image from the InputStream and returns the pixel array (byte, short, int or float). * Returns null if there was an IO exception. Does not close the InputStream. */ public Object readPixels(InputStream in) { Object pixels; startTime = System.currentTimeMillis(); try { switch (fi.fileType) { case FileInfo.GRAY8: case FileInfo.COLOR8: bytesPerPixel = 1; skip(in); pixels = (Object) read8bitImage(in); break; case FileInfo.GRAY16_SIGNED: case FileInfo.GRAY16_UNSIGNED: bytesPerPixel = 2; skip(in); pixels = (Object) read16bitImage(in); break; case FileInfo.GRAY32_INT: case FileInfo.GRAY32_UNSIGNED: case FileInfo.GRAY32_FLOAT: bytesPerPixel = 4; skip(in); pixels = (Object) read32bitImage(in); break; case FileInfo.GRAY64_FLOAT: bytesPerPixel = 8; skip(in); pixels = (Object) read64bitImage(in); break; case FileInfo.RGB: case FileInfo.BGR: case FileInfo.ARGB: case FileInfo.ABGR: case FileInfo.BARG: case FileInfo.CMYK: bytesPerPixel = fi.getBytesPerPixel(); skip(in); pixels = (Object) readChunkyRGB(in); break; case FileInfo.RGB_PLANAR: bytesPerPixel = 3; skip(in); pixels = (Object) readPlanarRGB(in); break; case FileInfo.BITMAP: bytesPerPixel = 1; skip(in); pixels = (Object) read1bitImage(in); break; case FileInfo.RGB48: bytesPerPixel = 6; skip(in); pixels = (Object) readRGB48(in); break; case FileInfo.RGB48_PLANAR: bytesPerPixel = 2; skip(in); pixels = (Object) readRGB48Planar(in); break; case FileInfo.GRAY12_UNSIGNED: skip(in); short[] data = read12bitImage(in); pixels = (Object) data; break; case FileInfo.GRAY24_UNSIGNED: skip(in); pixels = (Object) read24bitImage(in); break; default: pixels = null; } showProgress(1, 1); return pixels; } catch (IOException e) { IJ.log("" + e); return null; } }
/** * Constructs a new ImageReader using a FileInfo object to describe the file to be read. * * @see ij.io.FileInfo */ public ImageReader(FileInfo fi) { this.fi = fi; width = fi.width; height = fi.height; skipCount = fi.getOffset(); }