source: branches/4.0.0/components/native/itk-plugin/itkBioFormatsImageIO.cxx @ 4879

Revision 4879, 8.9 KB checked in by melissa, 11 years ago (diff)

Synced 4.0.0 branch to r4878 (including r4804).

Line 
1//
2// itkBioFormatsImageIO.cxx
3//
4
5/*
6OME Bio-Formats package for reading and converting biological file formats.
7Copyright (C) 2005-@year@ UW-Madison LOCI and Glencoe Software, Inc.
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 3 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22*/
23
24/*
25Adapted from the Slicer3 project: http://www.slicer.org/
26http://viewvc.slicer.org/viewcvs.cgi/trunk/Libs/MGHImageIO/
27
28See slicer-license.txt for Slicer3's licensing information.
29
30For more information about the ITK Plugin IO mechanism, see:
31http://www.itk.org/Wiki/Plugin_IO_mechanisms
32*/
33
34#include <fstream>
35
36#include "itkBioFormatsImageIO.h"
37#include "itkIOCommon.h"
38#include "itkExceptionObject.h"
39#include "itkByteSwapper.h"
40#include "itkMetaDataObject.h"
41
42#include <vnl/vnl_matrix.h>
43#include <vnl/vnl_vector.h>
44#include <vnl/vnl_cross.h>
45
46#include <cmath>
47
48#include <stdio.h>
49#include <stdlib.h>
50
51//--------------------------------------
52//
53// BioFormatsImageIO
54//
55
56namespace itk
57{
58
59  BioFormatsImageIO::BioFormatsImageIO()
60  {
61    PRINT("BioFormatsImageIO constuctor");
62    m_PixelType = SCALAR;
63    m_FileType = Binary;
64    m_NumberOfComponents = 1; // NB: Always split channels for now.
65
66    // initialize the Java virtual machine
67    PRINT("Creating JVM...");
68    StaticVmLoader loader(JNI_VERSION_1_4);
69    OptionList list;
70    list.push_back(jace::ClassPath(
71      "jace-runtime.jar:bio-formats.jar:loci_tools.jar"
72    ));
73    list.push_back(jace::CustomOption("-Xcheck:jni"));
74    list.push_back(jace::CustomOption("-Xmx256m"));
75    //list.push_back(jace::CustomOption("-verbose:jni"));
76    jace::helper::createVm(loader, list, false);
77    PRINT("JVM created.");
78
79    PRINT("Creating Bio-Formats objects...");
80    reader = new ChannelSeparator;
81    writer = new ImageWriter;
82    PRINT("Created reader and writer.");
83  }
84
85  BioFormatsImageIO::~BioFormatsImageIO()
86  {
87    delete reader;
88    delete writer;
89  }
90
91  bool
92  BioFormatsImageIO::CanReadFile(const char* FileNameToRead)
93  {
94    PRINT("BioFormatsImageIO::CanReadFile: FileNameToRead=" << FileNameToRead);
95    std::string filename(FileNameToRead);
96
97    if ( filename == "" )
98    {
99      itkExceptionMacro(<<"A FileName must be specified.");
100      return false;
101    }
102
103    // call Bio-Formats to check file type
104
105    // NB: Calling reader->isThisType() causes a symbol lookup error on:
106    //     _ZNK4jace5proxy5types8JBooleancvaEv
107    /*
108    bool isType = reader->isThisType(filename);
109    PRINT("BioFormatsImageIO::CanReadFile: isType=" << isType);
110    return isType;
111    */
112
113    return true;
114  }
115
116  void
117  BioFormatsImageIO::ReadImageInformation()
118  {
119    PRINT("BioFormatsImageIO::ReadImageInformation: m_FileName=" << m_FileName);
120
121    // attach OME metadata object
122    IMetadata omeMeta = MetadataTools::createOMEXMLMetadata();
123    reader->setMetadataStore(omeMeta);
124
125    // initialize dataset
126    PRINT("Initializing...");
127    reader->setId(m_FileName);
128    PRINT("Initialized.");
129
130    int seriesCount = reader->getSeriesCount();
131    PRINT("\tSeriesCount = " << seriesCount);
132
133    // get byte order
134
135    // NB: Calling reader->isLittleEndian() causes a symbol lookup error on:
136    //     _ZNK4jace5proxy5types8JBooleancvaEv
137    /*
138    bool little = reader->isLittleEndian();
139    if (little) SetByteOrderToLittleEndian();
140    else SetByteOrderToBigEndian();
141    */
142    SetByteOrderToBigEndian(); // m_ByteOrder
143
144    // get component type
145
146    // NB: Calling FormatTools::UINT8() causes a symbol lookup error on:
147    //     _ZN4jace6helper15deleteGlobalRefEP10_Jv_JNIEnvP9__jobject
148    int pixelType = reader->getPixelType();
149    int bpp = FormatTools::getBytesPerPixel(pixelType);
150    PRINT("\tBytes per pixel = " << bpp);
151    /*
152    IOComponentType componentType;
153    if (pixelType == FormatTools::UINT8())
154      componentType = UCHAR;
155    else if (pixelType == FormatTools::INT8())
156      componentType = CHAR;
157    if (pixelType == FormatTools::UINT16())
158      componentType = USHORT;
159    else if (pixelType == FormatTools::INT16())
160      componentType = SHORT;
161    if (pixelType == FormatTools::UINT32())
162      componentType = UINT;
163    else if (pixelType == FormatTools::INT32())
164      componentType = INT;
165    if (pixelType == FormatTools::FLOAT())
166      componentType = FLOAT;
167    else if (pixelType == FormatTools::DOUBLE())
168      componentType = DOUBLE;
169    else
170      componentType = UNKNOWNCOMPONENTTYPE;
171    SetComponentType(componentType); // m_ComponentType
172    if (componentType == UNKNOWNCOMPONENTTYPE)
173    {
174      itkExceptionMacro(<<"Unknown pixel type: " << pixelType);
175    }
176    */
177
178    // TEMP - for now we assume 8-bit unsigned integer data
179    SetComponentType(UCHAR);
180
181    // get pixel resolution and dimensional extents
182    int sizeX = reader->getSizeX();
183    int sizeY = reader->getSizeY();
184    int sizeZ = reader->getSizeZ();
185    int sizeC = reader->getSizeC();
186    int sizeT = reader->getSizeT();
187
188    // NB: ITK does not seem to provide a facility for multidimensional
189    //     data beyond multichannel 3D? Need to investigate further.
190
191    int imageCount = reader->getImageCount();
192    SetNumberOfDimensions(imageCount > 1 ? 3 : 2);
193    m_Dimensions[0] = sizeX;
194    m_Dimensions[1] = sizeY;
195    if (imageCount > 1) m_Dimensions[2] = imageCount;
196
197    PRINT("\tSizeX = " << sizeX);
198    PRINT("\tSizeY = " << sizeY);
199    PRINT("\tSizeZ = " << sizeZ);
200    PRINT("\tSizeC = " << sizeC);
201    PRINT("\tSizeT = " << sizeT);
202    PRINT("\tImage Count = " << imageCount);
203
204    // get physical resolution
205
206    // NB: Jace interface proxies do not inherit from superinterfaces.
207    //     E.g., IMetadata does not possess methods from MetadataRetrieve.
208    //     Need to find a way around this, or improve Jace.
209    //float physX = omeMeta.getDimensionsPhysicalSizeX(0, 0);
210    //float physY = omeMeta.getDimensionsPhysicalSizeY(0, 0);
211    //m_Spacing[0] = physX;
212    //m_Spacing[1] = physY;
213    //if (imageCount > 1) m_Spacing[2] = 1;
214
215    //PRINT("\tPhysicalSizeX = " << physX);
216    //PRINT("\tPhysicalSizeY = " << physY);
217  }
218
219  void
220  BioFormatsImageIO::Read(void* pData)
221  {
222    char* data = (char*) pData;
223    PRINT("BioFormatsImageIO::Read");
224
225    typedef JArray<JByte> ByteArray;
226
227    int pixelType = reader->getPixelType();
228    int bpp = FormatTools::getBytesPerPixel(pixelType);
229
230    // check IO region to identify the planar extents desired
231    ImageIORegion region = GetIORegion();
232    int regionDim = region.GetImageDimension();
233    int xIndex = region.GetIndex(0);
234    int xCount = region.GetSize(0);
235    int yIndex = region.GetIndex(1);
236    int yCount = region.GetSize(1);
237    int pIndex = 0, pCount = 1;
238    if (regionDim > 2) {
239      pIndex = region.GetIndex(2);
240      pCount = region.GetSize(2);
241    }
242    int bytesPerSubPlane = xCount * yCount * bpp;
243
244    PRINT("\tRegion dimension = " << regionDim);
245    PRINT("\tX index = " << xIndex);
246    PRINT("\tX count = " << xCount);
247    PRINT("\tY index = " << yIndex);
248    PRINT("\tY count = " << yCount);
249    PRINT("\tPlane index = " << pIndex);
250    PRINT("\tPlane count = " << pCount);
251    PRINT("\tBytes per plane = " << bytesPerSubPlane);
252
253    int p = 0;
254    for (int no=pIndex; no<pIndex+pCount; no++)
255    {
256      PRINT("Reading image plane " <<
257        (no + 1) << "/" << reader->getImageCount());
258      ByteArray buf = reader->openBytes(no, xIndex, xCount, yIndex, yCount);
259
260      // NB: Using brackets with a JArray causes a symbol lookup error on:
261      //     _ZN4jace6helper12newGlobalRefEP10_Jv_JNIEnvP9__jobject
262      //for (int i=0; i<bytesPerSubPlane; i++) data[p++] = buf[i];
263
264      // TEMP - for now we populate the buffer with dummy data
265      for (int i=0; i<bytesPerSubPlane; i++) data[p++] = 255 - no;
266    }
267
268    reader->close();
269    PRINT("Done.");
270  } // end Read function
271
272  bool
273  BioFormatsImageIO::CanWriteFile(const char* name)
274  {
275    PRINT("BioFormatsImageIO::CanWriteFile: name=" << name);
276    std::string filename(name);
277
278    if ( filename == "" )
279    {
280      itkExceptionMacro(<<"A FileName must be specified.");
281      return false;
282    }
283
284    // call Bio-Formats to check file type
285    ImageWriter writer;
286    bool isType = writer.isThisType(filename);
287    PRINT("BioFormatsImageIO::CanWriteFile: isType=" << isType);
288
289    return isType;
290  }
291
292  void
293  BioFormatsImageIO::WriteImageInformation()
294  {
295    PRINT("BioFormatsImageIO::WriteImageInformation");
296  }
297
298  void
299  BioFormatsImageIO::Write(const void* buffer)
300  {
301    PRINT("BioFormatsImageIO::Write");
302    // CTR TODO - implmeent Write function
303  } // end Write function
304
305} // end NAMESPACE ITK
Note: See TracBrowser for help on using the repository browser.