(* IJTiff v.1.02 by Harald Hauglin (harald.hauglin@basalmed.uio.no) This package uses public domain ImageJ java classes for importing grayscale TIFF files and stacks and writing 8 and 16 bit integer and 32 bit floating point TIFF files Mathematica J/Link is used to access java classes of the ImageJ image processing library. ImageJ is maintained by Wayne Rasband at NIH and is available for free at http://rsb.info.nih.gov/ij/download.html For full source code and API documentation: http://rsb.info.nih.gov/ij/developer/ Tested with ij.jar version 1.33 Features and known bugs: ------------------------ Image stack writing not yet supported Low level access to TIFF tags not currently supported IJReadImage[path] works for all file types supported by ImageJ, but only tested on grayscale images. No type and dimension check on parameters passed. Installation instructions: -------------------------- 1. Download the file ij.jar from http://rsb.info.nih.gov/ij/download.html 2. Add the location of ij.jar to the Java class path. In a Windows XP environment it may look like AddToClassPath["C:\\documents and settings\\all users\\application data\\ImageJ"]; Insert the relevant path name in the AddToClassPath[] command in the IJTiff.m file. Revision history: ----------------- 24022005 v 1.02 Added IJWrite8BitScaledTiff[] and IJWrite16BitTiff[] Fixed image reversal bug 23022005 v 1.01 Added file- and directory exsistence checks. The IJWrite functions now create new directories if specified in the file path. If no path is specified, the default directory is the current Mathematica directory (i.e. Directory[]) 23022005 v 1.00 *) BeginPackage["IJTiff`"] IJReadImage::usage = "IJReadImage[filepath] reads a grayscale TIFF file and \ returns a 2D array of pixel values"; IJReadImageStack::usage = "IJReadImageStack[filepath] reads a grayscale TIFF \ image stack and returns a list of 2D pixel value arrays"; IJReadImage::FileNotFound = " File `1` not found"; IJReadImage::StackInfo = \ "Warning: The image file contains multiple (`1`) images - use \ IJReadImageStack[]"; IJWriteFloatTiff::usage = \ "IJWriteFloatTiff[filepath,img] saves the 2D array img as a 32 bit floating \ point Tiff file";\ IJWrite8BitScaledTiff::usage = "IJWrite8BitScaledTiff[filepath, img] writes the 2D pixel array \ img to a standard 8 bit TIFF file. The pixel values are rescaled so that the new min and max\ pixel values are 0 and 255" IJWrite16BitTiff::usage = "IJWrite16BitTiff[filepath, img] writes the 2D pixel array img to\ the file specified by filepath. No rescaling is done." IJWriteTiff::nosuccess = "Error writing file"; Needs["JLink`"] InstallJava[]; (* NOTE: Edit the class path so that it includes the directory where ij.jar is located *) AddToClassPath[ "C:\\documents and settings\\all users\\application data\\ImageJ"]; AddToClassPath[ "C:\\documents and settings\\all users.WINNT\\programdata\\ImageJ"]; Begin["`Private`"] ToMathForm[s_] := StringReplace[s, "/" -> "\\"] ToUnixForm[s_] := StringReplace[s, "\\" -> "/"] ConfigWritePath[filepath_]:= Module[{l,dir,path}, path = ToMathForm[filepath]; l = StringPosition[path, "\\"]; If[l == {}, Module[{}, path = Directory[] <> "\\" <> path; path = ToUnixForm[path]; ], Module[{}, dir = StringTake[path, Last[l][[1]]]; If[FileInformation[dir] == {}, CreateDirectory[dir]]; path = filepath; ] ]; Return[path] ] ConfigReadPath[filepath_]:= Module[{l,dir,path}, path = ToMathForm[filepath]; l = StringPosition[path, "\\"]; If[l == {}, Module[{}, path = Directory[] <> "\\" <> path; path = ToUnixForm[path]; ], path = filepath; ]; Return[path] ] IJReadImage[filepath_] := Module[{image, w, arr, nstack, imgs,path}, path=ConfigReadPath[filepath]; If[FileInformation[path] == {}, Module[{}, Message[IJReadImage::FileNotFound, path]; Return[{}] ] ]; image = JavaNew["ij.io.Opener"]@openImage[path]; arr = image@getStack[]; w = arr@getWidth[]; nstack = arr@getSize[]; imgs = arr@getImageArray[]; imgs = Table[Reverse[Partition[imgs[[i]], w]], {i, 1, nstack}]; If[nstack == 1, Return[imgs[[1]]], Module[{}, Message[IJReadImage::StackInfo, nstack]; Return[imgs[[1]]] ] ] ] IJReadImageStack[filepath_] := Module[{image, w, arr, nstack, imgs,path}, path=ConfigReadPath[filepath]; If[FileInformation[path] == {}, Module[{}, Message[IJReadImage::FileNotFound, path]; Return[{}] ] ]; image = JavaNew["ij.io.Opener"]@openImage[path]; arr = image@getStack[]; w = arr@getWidth[]; nstack = arr@getSize[]; imgs = arr@getImageArray[]; imgs = Table[Reverse[Partition[imgs[[i]], w]], {i, 1, nstack}]; Return[imgs] ] IJWriteFloatTiff[filepath_, img_] := Module[{jx, w, h, improc, finfo, implus, fsav, ok, dir, l, path}, h = Length[img]; w = Length[img[[1]]]; path=ConfigWritePath[filepath]; jx = MakeJavaObject[N[1.0Flatten[Reverse[img]]]]; improc = JavaNew["ij.process.FloatProcessor", w, h, jx]; implus = JavaNew["ij.ImagePlus", "Title", improc]; finfo = implus@getFileInfo[]; fsav = JavaNew["ij.io.FileSaver", implus]; ok = fsav@saveAsTiff[path]; If[! ok, Message[IJWriteTiff::nosuccess]] ] IJWrite16BitTiff[filepath_, img_] := Module[{jx, w, h, improc, finfo, implus, cproc, sproc, fsav, ok, dir, l, path}, h = Length[img]; w = Length[img[[1]]]; path = ConfigWritePath[filepath]; jx = MakeJavaObject[N[1.0Flatten[Reverse[img]]]]; improc = JavaNew["ij.process.FloatProcessor", w, h, jx]; cproc = JavaNew["ij.process.TypeConverter", improc, False]; sproc = cproc@convertToShort[]; implus = JavaNew["ij.ImagePlus", "Title", sproc]; finfo = implus@getFileInfo[]; fsav = JavaNew["ij.io.FileSaver", implus]; ok = fsav@saveAsTiff[path]; If[! ok, Message[IJWriteTiff::nosuccess]] ] IJWrite8BitScaledTiff[filepath_, img_] := Module[{jx, w, h, improc, finfo, \ implus, cproc, sproc, fsav, ok, dir, l, path}, h = Length[img]; w = Length[img[[1]]]; path = ConfigWritePath[filepath]; jx = MakeJavaObject[N[1.0Flatten[Reverse[img]]]]; improc = JavaNew["ij.process.FloatProcessor", w, h, jx]; cproc = JavaNew["ij.process.TypeConverter", improc, True]; sproc = cproc@convertToByte[]; implus = JavaNew["ij.ImagePlus", "Title", sproc]; finfo = implus@getFileInfo[]; fsav = JavaNew["ij.io.FileSaver", implus]; ok = fsav@saveAsTiff[path]; If[! ok, Message[IJWriteTiff::nosuccess]] ] End[ ] EndPackage[ ]