Exemple #1
0
Nrrd* LSMReader::Convert(int t, int c, bool get_max)
{
    Nrrd *data = 0;
    FILE* pfile = 0;
    if (!WFOPEN(&pfile, m_path_name.c_str(), L"rb"))
        return 0;

    int i, j;

    if (t>=0 && t<m_time_num &&
            c>=0 && c<m_chan_num &&
            m_slice_num > 0 &&
            m_x_size > 0 &&
            m_y_size > 0 &&
            t<(int)m_lsm_info.size() &&
            c<(int)m_lsm_info[t].size())
    {
        //allocate memory for nrrd
        switch (m_datatype)
        {
        case 1://8-bit
        {
            unsigned long long mem_size = (unsigned long long)m_x_size*
                                          (unsigned long long)m_y_size*(unsigned long long)m_slice_num;
            unsigned char *val = new (std::nothrow) unsigned char[mem_size];
            ChannelInfo *cinfo = &m_lsm_info[t][c];
            for (i=0; i<(int)cinfo->size(); i++)
            {
                if (m_l4gb?
                        FSEEK64(pfile, ((uint64_t((*cinfo)[i].offset_high))<<32)+(*cinfo)[i].offset, SEEK_SET)==0:
                        fseek(pfile, (*cinfo)[i].offset, SEEK_SET)==0)
                {
                    unsigned int val_pos = m_x_size*m_y_size*i;
                    if (m_compression==1)
                        fread(val+val_pos, sizeof(unsigned char), (*cinfo)[i].size, pfile);
                    else if (m_compression==5)
                    {
                        unsigned char* tif = new (std::nothrow) unsigned char[(*cinfo)[i].size];
                        fread(tif, sizeof(unsigned char), (*cinfo)[i].size, pfile);
                        LZWDecode(tif, val+val_pos, (*cinfo)[i].size);
                        for (j=0; j<m_y_size; j++)
                            DecodeAcc8(val+val_pos+j*m_x_size, m_x_size,1);
                        delete []tif;
                    }
                }
            }
            //create nrrd
            data = nrrdNew();
            nrrdWrap(data, val, nrrdTypeUChar, 3, (size_t)m_x_size, (size_t)m_y_size, (size_t)m_slice_num);
            nrrdAxisInfoSet(data, nrrdAxisInfoSpacing, m_xspc, m_yspc, m_zspc);
            nrrdAxisInfoSet(data, nrrdAxisInfoMax, m_xspc*m_x_size, m_yspc*m_y_size, m_zspc*m_slice_num);
            nrrdAxisInfoSet(data, nrrdAxisInfoMin, 0.0, 0.0, 0.0);
            nrrdAxisInfoSet(data, nrrdAxisInfoSize, (size_t)m_x_size, (size_t)m_y_size, (size_t)m_slice_num);
        }
        break;
        case 2://16-bit
        case 3:
        {
            unsigned long long mem_size = (unsigned long long)m_x_size*
                                          (unsigned long long)m_y_size*(unsigned long long)m_slice_num;
            unsigned short *val = new (std::nothrow) unsigned short[mem_size];
            ChannelInfo *cinfo = &m_lsm_info[t][c];
            for (i=0; i<(int)cinfo->size(); i++)
            {
                if (m_l4gb?
                        FSEEK64(pfile, ((uint64_t((*cinfo)[i].offset_high))<<32)+(*cinfo)[i].offset, SEEK_SET)==0:
                        fseek(pfile, (*cinfo)[i].offset, SEEK_SET)==0)
                {
                    unsigned int val_pos = m_x_size*m_y_size*i;
                    if (m_compression==1)
                        fread(val+val_pos, sizeof(unsigned char), (*cinfo)[i].size, pfile);
                    else if (m_compression==5)
                    {
                        unsigned char* tif = new (std::nothrow) unsigned char[(*cinfo)[i].size];
                        fread(tif, sizeof(unsigned char), (*cinfo)[i].size, pfile);
                        LZWDecode(tif, (tidata_t)(val+val_pos), (*cinfo)[i].size);
                        for (j=0; j<m_y_size; j++)
                            DecodeAcc16((tidata_t)(val+val_pos+j*m_x_size), m_x_size,1);
                        delete []tif;
                    }
                }
            }
            //create nrrd
            data = nrrdNew();
            nrrdWrap(data, val, nrrdTypeUShort, 3, (size_t)m_x_size, (size_t)m_y_size, (size_t)m_slice_num);
            nrrdAxisInfoSet(data, nrrdAxisInfoSpacing, m_xspc, m_yspc, m_zspc);
            nrrdAxisInfoSet(data, nrrdAxisInfoMax, m_xspc*m_x_size, m_yspc*m_y_size, m_zspc*m_slice_num);
            nrrdAxisInfoSet(data, nrrdAxisInfoMin, 0.0, 0.0, 0.0);
            nrrdAxisInfoSet(data, nrrdAxisInfoSize, (size_t)m_x_size, (size_t)m_y_size, (size_t)m_slice_num);
        }
        break;
        }
    }

    fclose(pfile);

    return data;
}
Nrrd* OIFReader::Convert(int t, int c, bool get_max)
{
   Nrrd *data = 0;
   int sl_num = 0;

   if (t>=0 && t<m_time_num &&
         c>=0 && c<m_chan_num &&
         m_slice_num>0 &&
         m_x_size>0 &&
         m_y_size>0)
   {
      //allocate memory for nrrd
      unsigned long long mem_size = (unsigned long long)m_x_size*
         (unsigned long long)m_y_size*(unsigned long long)m_slice_num;
      unsigned short *val = new (std::nothrow) unsigned short[mem_size];

      //read the channel
      ChannelInfo *cinfo = &m_oif_info[t].dataset[c];
      int i;
      for (i=0; i<int(cinfo->size()); i++)
      {
         char *pbyData = 0;
         wstring file_name = (*cinfo)[i];

         //open file
         ifstream is;
#ifdef _WIN32
         is.open(file_name.c_str(), ios::binary);
#else
         is.open(ws2s(file_name).c_str(), ios::binary);
#endif
         if (is.is_open())
         {
            is.seekg(0, ios::end);
            size_t size = is.tellg();
            pbyData = new char[size];
            is.seekg(0, ios::beg);
            is.read(pbyData, size);
            is.close();

            //read
            ReadTiff(pbyData, val, i);

            //increase
            sl_num++;
         }

         if (pbyData)
            delete []pbyData;
      }

      //create nrrd
      if (val && sl_num == m_slice_num)
      {
         //ok
         data = nrrdNew();
         nrrdWrap(data, val, nrrdTypeUShort, 3, (size_t)m_x_size, (size_t)m_y_size, (size_t)m_slice_num);
         nrrdAxisInfoSet(data, nrrdAxisInfoSpacing, m_xspc, m_yspc, m_zspc);
         nrrdAxisInfoSet(data, nrrdAxisInfoMax, m_xspc*m_x_size, m_yspc*m_y_size, m_zspc*m_slice_num);
         nrrdAxisInfoSet(data, nrrdAxisInfoMin, 0.0, 0.0, 0.0);
         nrrdAxisInfoSet(data, nrrdAxisInfoSize, (size_t)m_x_size, (size_t)m_y_size, (size_t)m_slice_num);
      }
      else
      {
         //something is wrong
         if (val)
            delete []val;
      }
   }

   if (m_max_value > 0.0)
      m_scalar_scale = 65535.0 / m_max_value;

   m_cur_time = t;
   return data;
}
Exemple #3
0
Nrrd *OIBReader::Convert(int t, int c, bool get_max)
{
   Nrrd *data = 0;
   int sl_num = 0;
   if (t>=0 && t<m_time_num &&
         c>=0 && c<m_chan_num &&
         m_slice_num > 0 &&
         m_x_size > 0 &&
         m_y_size > 0)
   {
	   unsigned char *pbyData = 0;
       wstring path_name = m_type==0?m_path_name:m_oib_info[t].filename;
	   //storage
	   POLE::Storage pStg(ws2s(path_name).c_str()); 
	   //open
	   if (pStg.open()) {
		  //allocate memory for nrrd
		  unsigned long long mem_size = (unsigned long long)m_x_size*
			  (unsigned long long)m_y_size*(unsigned long long)m_slice_num;
		  unsigned short *val = new (std::nothrow) unsigned short[mem_size];
		  //enumerate
		  std::list<std::string> entries = 
			  pStg.entries();
		  for(std::list<std::string>::iterator it = entries.begin();
			  it != entries.end(); ++it) {
			if (pStg.isDirectory(*it)) {
				std::list<std::string> streams =  pStg.GetAllStreams(*it);
				size_t num = 0;
				ChannelInfo *cinfo = &m_oib_info[t].dataset[c];
				for(std::list<std::string>::iterator its = streams.begin();
						its != streams.end(); ++its) {
					if (num >= cinfo->size()) break;
					//fix the stream name
					std::string str_name = ws2s((*cinfo)[num].stream_name);
					std::string name = (*it) + std::string("/") + str_name;
					  
					POLE::Stream pStm(&pStg,name);

					//open
					if (!pStm.eof() && !pStm.fail())
					{
						//get stream size
						size_t sz = pStm.size();
						//allocate 
						pbyData = new unsigned char[sz];
						if (!pbyData) 
							return NULL;
						//read
						if (pStm.read(pbyData,sz)) {
									
							//copy tiff to val
							ReadTiff(pbyData, val, num);

							//increase
							sl_num++;
						}
					}

					//release
					if (pbyData)
						delete[] pbyData;
					num++;
				 }
			  }
		   }

			//create nrrd
			if (val && sl_num == m_slice_num)
			{
				//ok
				data = nrrdNew();
				nrrdWrap(data, val, nrrdTypeUShort, 3, (size_t)m_x_size, (size_t)m_y_size, 
					(size_t)m_slice_num);
				nrrdAxisInfoSet(data, nrrdAxisInfoSpacing, m_xspc, m_yspc, m_zspc);
				nrrdAxisInfoSet(data, nrrdAxisInfoMax, m_xspc*m_x_size, m_yspc*m_y_size, 
					m_zspc*m_slice_num);
				nrrdAxisInfoSet(data, nrrdAxisInfoMin, 0.0, 0.0, 0.0);
				nrrdAxisInfoSet(data, nrrdAxisInfoSize, (size_t)m_x_size,
					(size_t)m_y_size, (size_t)m_slice_num);
			} else {
				//something is wrong
				if (val)
					delete []val;
			}
			//release
			pStg.close();
	  }
    }

	if (m_max_value > 0.0)
		m_scalar_scale = 65535.0 / m_max_value;

	m_cur_time = t;
	return data;
}