Esempio n. 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;
}
void OIFReader::ReadTiff(char *pbyData, unsigned short *val, int z)
{
   if (*((unsigned int*)pbyData) != 0x002A4949)
      return;

   int compression = 0;
   unsigned int offset = 0;
   //directory offset
   offset = *((unsigned int*)(pbyData+4));
   //the directory
   //entry number
   int entry_num = *((unsigned short*)(pbyData+offset));
   //strip info
   int strips = 0;
   int rows = 0;
   vector <unsigned int> strip_offsets;
   vector <unsigned int> strip_bytes;
   //get strip info
   unsigned int s_num1 = 0;
   unsigned int s_num2 = 0;
   for (int i=0; i<entry_num; i++)
   {
      //read each entry (12 bytes)
      unsigned short tag = *((unsigned short*)(pbyData+offset+2+12*i));
      switch (tag)
      {
      case 0x0103:  //259, compression
         {
            unsigned short value;
            value = *((unsigned short*)(pbyData+offset+2+12*i+8));
            compression = value<<16>>16;
         }
         break;
      case 0x0111:  //strip offsets
         {
            unsigned short type = *((unsigned short*)(pbyData+offset+2+12*i+2));
            //number of values
            s_num1 = *((unsigned int*)(pbyData+offset+2+12*i+4));
            unsigned int entry_offset = 0;
            entry_offset = *((unsigned int*)(pbyData+offset+2+12*i+8));
            for (int j=0; j<int(s_num1); j++)
            {
               if (type == 3)
               {
                  //unsigned short
                  unsigned short value;
                  value = *((unsigned short*)(pbyData+entry_offset+2*j));
                  strip_offsets.push_back((unsigned int)value);
               }
               else if (type == 4)
               {
                  //unsigned int
                  unsigned int value;
                  value = *((unsigned int*)(pbyData+entry_offset+4*j));
                  strip_offsets.push_back(value);
               }
            }
         }
         break;
      case 0x0116:  //rows per strip
         {
            unsigned short type = *((unsigned short*)(pbyData+offset+2+12*i+2));
            if (type == 3)
            {
               //unsigned short
               unsigned short value;
               value = *((unsigned short*)(pbyData+offset+2+12*i+8));
               rows = value;
            }
            else if (type == 4)
            {
               //unsigned int
               unsigned int value;
               value = *((unsigned int*)(pbyData+offset+2+12*i+8));
               rows = value;
            }
         }
         break;
      case 0x0117:  //strip byte counts
         {
            unsigned short type = *((unsigned short*)(pbyData+offset+2+12*i+2));
            //number of values
            s_num2 = *((unsigned int*)(pbyData+offset+2+12*i+4));
            unsigned int entry_offset = 0;
            entry_offset = *((unsigned int*)(pbyData+offset+2+12*i+8));
            for (int j=0; j<int(s_num2); j++)
            {
               if (type == 3)
               {
                  //unsigned short
                  unsigned short value;
                  value = *((unsigned short*)(pbyData+entry_offset+2*j));
                  strip_bytes.push_back((unsigned int)value);
               }
               else if (type == 4)
               {
                  //unsigned int
                  unsigned int value;
                  value = *((unsigned int*)(pbyData+entry_offset+4*j));
                  strip_bytes.push_back(value);
               }
            }
         }
         break;
      case 0x0119:  //max sample value
         {
            unsigned short value;
            value = *((unsigned short*)(pbyData+offset+2+12*i+8));
            if ((double)value > m_max_value)
               m_max_value = (double)value;
         }
         break;
      }
   }
   //read strips
   if (s_num1 == s_num2 &&
         strip_offsets.size() == s_num1 &&
         strip_bytes.size() == s_num2)
   {
      strips = s_num1;

      unsigned int val_pos = z*m_x_size*m_y_size;
      for (int i=0; i<strips; i++)
      {
         unsigned int data_pos = strip_offsets[i];
         unsigned int data_size = strip_bytes[i];
         if (compression == 1)//no copmression
            memcpy((void*)(val+val_pos), (void*)(pbyData+data_pos), data_size);
         else if (compression == 5)
            LZWDecode((tidata_t)(pbyData+data_pos), (tidata_t)(val+val_pos), m_x_size*rows*2);
         val_pos += rows*m_x_size;
      }
   }
}
Esempio n. 3
0
void
wxPdfParser::GetStreamBytes(wxPdfStream* stream)
{
  GetStreamBytesRaw(stream);

  // Do not decode the content of resource object streams
  if (m_useRawStream) return;

  // Check whether the stream buffer is empty
  wxMemoryOutputStream* osIn = stream->GetBuffer();
  if (osIn->GetLength() == 0) return;

  size_t j;
  wxArrayPtrVoid filters;
  wxPdfObject* filter = ResolveObject(stream->Get(_T("/Filter")));
  if (filter != NULL)
  {
    int type = filter->GetType();
    if (type == OBJTYPE_NAME)
    {
      filters.Add(filter);
    }
    else if (type == OBJTYPE_ARRAY)
    {
      wxPdfArray* filterArray = (wxPdfArray*) filter;
      size_t size = filterArray->GetSize();
      for (j = 0; j < size; j++)
      {
        filters.Add(filterArray->Get(j));
      }
    }

    // Read decode parameters if available
    wxArrayPtrVoid dp;
    wxPdfObject* dpo = ResolveObject(stream->Get(_T("/DecodeParms")));
    if (dpo == NULL || (dpo->GetType() != OBJTYPE_DICTIONARY && dpo->GetType() != OBJTYPE_ARRAY))
    {
      dpo = ResolveObject(stream->Get(_T("/DP")));
    }
    if (dpo != NULL)
    {
      if (dpo->GetType() == OBJTYPE_DICTIONARY)
      {
        dp.Add(dpo);
      }
      else if (dpo->GetType() == OBJTYPE_ARRAY)
      {
        wxPdfArray* dpArray = (wxPdfArray*) dpo;
        size_t size = dpArray->GetSize();
        for (j = 0; j < size; j++)
        {
          dp.Add(dpArray->Get(j));
        }
      }
    }

    wxPdfObject* dicParam = NULL;
    wxMemoryOutputStream* osOut = NULL;
    for (j = 0; j < filters.GetCount(); j++)
    {
      osIn = stream->GetBuffer();
      wxPdfName* name = (wxPdfName*) filters[j];
      if (name->GetName() == _T("/FlateDecode") || name->GetName() == _T("/Fl"))
      {
        osOut = FlateDecode(osIn);
        if (j < dp.GetCount())
        {
          wxMemoryOutputStream* osIn2 = osOut;
          dicParam = (wxPdfObject*) dp[j];
          osOut = DecodePredictor(osIn2, dicParam);
          if (osOut != osIn2)
          {
            delete osIn2;
          }
        }
      }
      else if(name->GetName() == _T("/ASCIIHexDecode") || name->GetName() == _T("/AHx"))
      {
        osOut = ASCIIHexDecode(osIn);
      }
      else if(name->GetName() == _T("/ASCII85Decode") || name->GetName() == _T("/A85"))
      {
        osOut = ASCII85Decode(osIn);
      }
      else if(name->GetName() == _T("/LZWDecode"))
      {
        osOut = LZWDecode(osIn);
        if (j < dp.GetCount())
        {
          wxMemoryOutputStream* osIn2 = osOut;
          dicParam = (wxPdfObject*) dp[j];
          osOut = DecodePredictor(osIn2, dicParam);
          if (osOut != osIn2)
          {
            delete osIn2;
          }
        }
      }
      else
      {
        wxLogError(wxString(_T("wxPdfParser::GetStreamBytes: Filter '")) +
                   name->GetName() + wxString(_T("' not supported")));
      }
      if (osOut != NULL)
      {
        stream->SetBuffer(osOut);
        if (osIn != osOut)
        {
          delete osIn;
        }
      }
    }
  }
}