Changeset 4851


Ignore:
Timestamp:
02/26/09 18:29:17 (11 years ago)
Author:
curtis
Message:

First cut at Bio-Formats ITK Image IO plugin implementation.
Numerous symbol lookup errors prevent it from being functional.

Location:
trunk/components/native/itk-plugin
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/components/native/itk-plugin/itkBioFormatsImageIO.cxx

    r4824 r4851  
    4444#include <vnl/vnl_cross.h> 
    4545 
    46 #include "jace/StaticVmLoader.h" 
    47 using jace::StaticVmLoader; 
    48  
    49 #include "jace/OptionList.h" 
    50 using jace::OptionList; 
    51  
    52 #include "bioformats.h" 
    53  
    5446#include <cmath> 
    5547 
    5648#include <stdio.h> 
    5749#include <stdlib.h> 
    58  
    59 //------------------------------- 
    60  
    61 static 
    62 std::string 
    63 GetExtension( const std::string& filename) 
    64 { 
    65   const std::string::size_type pos = filename.find_last_of("."); 
    66   std::string extension(filename, pos+1, filename.length()); 
    67   return extension; 
    68 } 
    6950 
    7051//-------------------------------------- 
     
    7859  BioFormatsImageIO::BioFormatsImageIO() 
    7960  { 
    80     this->SetNumberOfDimensions(3); 
    81     const unsigned int uzero = 0; 
    82     m_Dimensions[0] = uzero; 
    83     m_Dimensions[1] = uzero; 
    84     m_Dimensions[2] = uzero; 
    85  
    86     if ( ByteSwapper<int>::SystemIsBigEndian()) 
    87       m_ByteOrder = BigEndian; 
    88     else 
    89       m_ByteOrder = LittleEndian; 
     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    // NB: Use full path for now, to ensure Java libraries can be found. 
     71    std::string jarPath = 
     72      "/home/curtis/src/itk/InsightToolkit-3.10.2/build/bin"; 
     73    list.push_back(jace::ClassPath( 
     74      jarPath + "/jace-runtime.jar:" + 
     75      jarPath + "/bio-formats.jar:" + 
     76      jarPath + "/loci_tools.jar" 
     77    )); 
     78    list.push_back(jace::CustomOption("-Xcheck:jni")); 
     79    list.push_back(jace::CustomOption("-Xmx256m")); 
     80    //list.push_back(jace::CustomOption("-verbose:jni")); 
     81    jace::helper::createVm(loader, list, false); 
     82    PRINT("JVM created."); 
     83 
     84    PRINT("Creating Bio-Formats objects..."); 
     85    reader = new ChannelSeparator; 
     86    writer = new ImageWriter; 
     87    PRINT("Created reader and writer."); 
    9088  } 
    9189 
    9290  BioFormatsImageIO::~BioFormatsImageIO() 
    9391  { 
     92    delete reader; 
     93    delete writer; 
    9494  } 
    9595 
     
    9797  BioFormatsImageIO::CanReadFile(const char* FileNameToRead) 
    9898  { 
    99     std::cout << "BioFormatsImageIO::CanReadFile: FileNameToRead=" << FileNameToRead << std::endl;//TEMP 
     99    PRINT("BioFormatsImageIO::CanReadFile: FileNameToRead=" << FileNameToRead); 
    100100    std::string filename(FileNameToRead); 
    101101 
     
    106106    } 
    107107 
    108     // check if the correct extension is given by the user 
    109     std::string extension = GetExtension(filename); 
    110     if ( extension == std::string("zvi") || extension == std::string("lif") ) 
    111     { 
    112       return true; 
    113     } 
    114  
    115     return false; 
     108    // call Bio-Formats to check file type 
     109 
     110    // NB: Calling reader->isThisType() causes a symbol lookup error on: 
     111    //     _ZNK4jace5proxy5types8JBooleancvaEv 
     112    /* 
     113    bool isType = reader->isThisType(filename); 
     114    PRINT("BioFormatsImageIO::CanReadFile: isType=" << isType); 
     115    return isType; 
     116    */ 
     117 
     118    return true; 
    116119  } 
    117120 
     
    119122  BioFormatsImageIO::ReadImageInformation() 
    120123  { 
    121     std::cout << "BioFormatsImageIO::ReadImageInformation" << std::endl;//TEMP 
    122  
    123     // initialize the Java virtual machine 
    124     std::cout << "Creating JVM..." << std::endl;//TEMP 
    125     StaticVmLoader loader(JNI_VERSION_1_4); 
    126     OptionList list; 
    127     list.push_back(jace::ClassPath( 
    128       "jace-runtime.jar:bio-formats.jar:loci_tools.jar")); 
    129     list.push_back(jace::CustomOption("-Xcheck:jni")); 
    130     list.push_back(jace::CustomOption("-Xmx256m")); 
    131     //list.push_back(jace::CustomOption("-verbose:jni")); 
    132     jace::helper::createVm(loader, list, false); 
    133     std::cout << "JVM created." << std::endl; 
    134  
    135     //ImageReader r; 
    136     //std::cout << "Created ImageReader:" << r << std::endl;//TEMP 
     124    PRINT("BioFormatsImageIO::ReadImageInformation: m_FileName=" << m_FileName); 
     125 
     126    // attach OME metadata object 
     127    IMetadata omeMeta = MetadataTools::createOMEXMLMetadata(); 
     128    reader->setMetadataStore(omeMeta); 
     129 
     130    // initialize dataset 
     131    PRINT("Initializing..."); 
     132    reader->setId(m_FileName); 
     133    PRINT("Initialized."); 
     134 
     135    int seriesCount = reader->getSeriesCount(); 
     136    PRINT("\tSeriesCount = " << seriesCount); 
     137 
     138    // get byte order 
     139 
     140    // NB: Calling reader->isLittleEndian() causes a symbol lookup error on: 
     141    //     _ZNK4jace5proxy5types8JBooleancvaEv 
     142    /* 
     143    bool little = reader->isLittleEndian(); 
     144    if (little) SetByteOrderToLittleEndian(); 
     145    else SetByteOrderToBigEndian(); 
     146    */ 
     147    SetByteOrderToBigEndian(); // m_ByteOrder 
     148 
     149    // get component type 
     150 
     151    // NB: Calling FormatTools::UINT8() causes a symbol lookup error on: 
     152    //     _ZN4jace6helper15deleteGlobalRefEP10_Jv_JNIEnvP9__jobject 
     153    int pixelType = reader->getPixelType(); 
     154    int bpp = FormatTools::getBytesPerPixel(pixelType); 
     155    PRINT("\tBytes per pixel = " << bpp); 
     156    /* 
     157    IOComponentType componentType; 
     158    if (pixelType == FormatTools::UINT8()) 
     159      componentType = UCHAR; 
     160    else if (pixelType == FormatTools::INT8()) 
     161      componentType = CHAR; 
     162    if (pixelType == FormatTools::UINT16()) 
     163      componentType = USHORT; 
     164    else if (pixelType == FormatTools::INT16()) 
     165      componentType = SHORT; 
     166    if (pixelType == FormatTools::UINT32()) 
     167      componentType = UINT; 
     168    else if (pixelType == FormatTools::INT32()) 
     169      componentType = INT; 
     170    if (pixelType == FormatTools::FLOAT()) 
     171      componentType = FLOAT; 
     172    else if (pixelType == FormatTools::DOUBLE()) 
     173      componentType = DOUBLE; 
     174    else 
     175      componentType = UNKNOWNCOMPONENTTYPE; 
     176    SetComponentType(componentType); // m_ComponentType 
     177    if (componentType == UNKNOWNCOMPONENTTYPE) 
     178    { 
     179      itkExceptionMacro(<<"Unknown pixel type: " << pixelType); 
     180    } 
     181    */ 
     182 
     183    // TEMP - for now we assume 8-bit unsigned integer data 
     184    SetComponentType(UCHAR); 
     185 
     186    // get pixel resolution and dimensional extents 
     187    int sizeX = reader->getSizeX(); 
     188    int sizeY = reader->getSizeY(); 
     189    int sizeZ = reader->getSizeZ(); 
     190    int sizeC = reader->getSizeC(); 
     191    int sizeT = reader->getSizeT(); 
     192 
     193    // NB: ITK does not seem to provide a facility for multidimensional 
     194    //     data beyond multichannel 3D? Need to investigate further. 
     195 
     196    int imageCount = reader->getImageCount(); 
     197    SetNumberOfDimensions(imageCount > 1 ? 3 : 2); 
     198    m_Dimensions[0] = sizeX; 
     199    m_Dimensions[1] = sizeY; 
     200    if (imageCount > 1) m_Dimensions[2] = imageCount; 
     201 
     202    PRINT("\tSizeX = " << sizeX); 
     203    PRINT("\tSizeY = " << sizeY); 
     204    PRINT("\tSizeZ = " << sizeZ); 
     205    PRINT("\tSizeC = " << sizeC); 
     206    PRINT("\tSizeT = " << sizeT); 
     207    PRINT("\tImage Count = " << imageCount); 
     208 
     209    // get physical resolution 
     210 
     211    // NB: Jace interface proxies do not inherit from superinterfaces. 
     212    //     E.g., IMetadata does not possess methods from MetadataRetrieve. 
     213    //     Need to find a way around this, or improve Jace. 
     214    //float physX = omeMeta.getDimensionsPhysicalSizeX(0, 0); 
     215    //float physY = omeMeta.getDimensionsPhysicalSizeY(0, 0); 
     216    //m_Spacing[0] = physX; 
     217    //m_Spacing[1] = physY; 
     218    //if (imageCount > 1) m_Spacing[2] = 1; 
     219 
     220    //PRINT("\tPhysicalSizeX = " << physX); 
     221    //PRINT("\tPhysicalSizeY = " << physY); 
    137222  } 
    138223 
     
    140225  BioFormatsImageIO::Read(void* pData) 
    141226  { 
    142     std::cout << "BioFormatsImageIO::Read" << std::endl;//TEMP 
     227    char* data = (char*) pData; 
     228    PRINT("BioFormatsImageIO::Read"); 
     229 
     230    typedef JArray<JByte> ByteArray; 
     231 
     232    int pixelType = reader->getPixelType(); 
     233    int bpp = FormatTools::getBytesPerPixel(pixelType); 
     234 
     235    // check IO region to identify the planar extents desired 
     236    ImageIORegion region = GetIORegion(); 
     237    int regionDim = region.GetImageDimension(); 
     238    int xIndex = region.GetIndex(0); 
     239    int xCount = region.GetSize(0); 
     240    int yIndex = region.GetIndex(1); 
     241    int yCount = region.GetSize(1); 
     242    int pIndex = 0, pCount = 1; 
     243    if (regionDim > 2) { 
     244      pIndex = region.GetIndex(2); 
     245      pCount = region.GetSize(2); 
     246    } 
     247    int bytesPerSubPlane = xCount * yCount * bpp; 
     248 
     249    PRINT("\tRegion dimension = " << regionDim); 
     250    PRINT("\tX index = " << xIndex); 
     251    PRINT("\tX count = " << xCount); 
     252    PRINT("\tY index = " << yIndex); 
     253    PRINT("\tY count = " << yCount); 
     254    PRINT("\tPlane index = " << pIndex); 
     255    PRINT("\tPlane count = " << pCount); 
     256    PRINT("\tBytes per plane = " << bytesPerSubPlane); 
     257 
     258    int p = 0; 
     259    for (int no=pIndex; no<pIndex+pCount; no++) 
     260    { 
     261      PRINT("Reading image plane " << 
     262        (no + 1) << "/" << reader->getImageCount()); 
     263      ByteArray buf = reader->openBytes(no, xIndex, xCount, yIndex, yCount); 
     264 
     265      // NB: Using brackets with a JArray causes a symbol lookup error on: 
     266      //     _ZN4jace6helper12newGlobalRefEP10_Jv_JNIEnvP9__jobject 
     267      //for (int i=0; i<bytesPerSubPlane; i++) data[p++] = buf[i]; 
     268 
     269      // TEMP - for now we populate the buffer with dummy data 
     270      for (int i=0; i<bytesPerSubPlane; i++) data[p++] = 255 - no; 
     271    } 
     272 
     273    reader->close(); 
     274    PRINT("Done."); 
    143275  } // end Read function 
    144276 
     
    146278  BioFormatsImageIO::CanWriteFile(const char* name) 
    147279  { 
    148     std::cout << "BioFormatsImageIO::CanWriteFile: name=" << name << std::endl;//TEMP 
     280    PRINT("BioFormatsImageIO::CanWriteFile: name=" << name); 
    149281    std::string filename(name); 
    150282 
     
    155287    } 
    156288 
    157     std::string extension = GetExtension(filename); 
    158     if ( extension != std::string("zvi") && extension != std::string("lif") ) 
    159       return false; 
    160  
    161     return true; 
     289    // call Bio-Formats to check file type 
     290    ImageWriter writer; 
     291    bool isType = writer.isThisType(filename); 
     292    PRINT("BioFormatsImageIO::CanWriteFile: isType=" << isType); 
     293 
     294    return isType; 
    162295  } 
    163296 
     
    165298  BioFormatsImageIO::WriteImageInformation() 
    166299  { 
    167     std::cout << "BioFormatsImageIO::WriteImageInformation" << std::endl;//TEMP 
     300    PRINT("BioFormatsImageIO::WriteImageInformation"); 
    168301  } 
    169302 
     
    171304  BioFormatsImageIO::Write(const void* buffer) 
    172305  { 
    173     std::cout << "BioFormatsImageIO::Write" << std::endl;//TEMP 
    174   } 
     306    PRINT("BioFormatsImageIO::Write"); 
     307    // CTR TODO - implmeent Write function 
     308  } // end Write function 
    175309 
    176310} // end NAMESPACE ITK 
  • trunk/components/native/itk-plugin/itkBioFormatsImageIO.h

    r4824 r4851  
    4545//#include "itkBioFormatsIOWin32Header.h" 
    4646 
     47#include "bioformats.h" 
     48 
     49// for debugging 
     50#define BF_ITK_DEBUG 
     51#ifdef BF_ITK_DEBUG 
     52#define PRINT(x) \ 
     53  std::cout << x << std::endl; 
     54#else 
     55#define PRINT(x) 
     56#endif 
     57 
    4758namespace itk 
    4859{ 
     
    6879 
    6980    /**---------------Write the data------------------**/ 
    70  
    7181    virtual bool CanWriteFile(const char* FileNameToWrite); 
    7282    /* Set the spacing and dimension information for the set file name */ 
     
    7888    BioFormatsImageIO(); 
    7989    ~BioFormatsImageIO(); 
    80     /* 
    81     void PrintSelf(std::ostream& os, Indent indent) const; 
    82  
    83     void ReadVolumeHeader(gzFile fp); 
    8490 
    8591  private: 
    86     // processes the actual data buffer 
    87     void SwapBytesIfNecessary(void* buffer, unsigned long numberOfPixels); 
    88     // examines the direction cosines and creates encapsulation data 
    89     //void MriDirCos(); 
    90  
    91     void WriteUncompressedHeader(); 
    92     void WriteCompressedHeader(gzFile fp); 
    93  
    94     void WriteUncompressedData(const void* buffer); 
    95     void WriteCompressedData(gzFile fp, const void* buffer); 
    96  
    97     void PermuteFrameValues(const void* buffer, char* tempmemory); 
    98  
    99     unsigned int GetComponentSize() const; 
    100  
    101     std::string GetOrientation( itk::Matrix< double > directions ); 
    102     */ 
    103  
     92    ChannelSeparator* reader; 
     93    ImageWriter* writer; 
    10494  }; 
    10595 
Note: See TracChangeset for help on using the changeset viewer.