예제 #1
0
int Orient( char *filename, int setflags, int orientation )
{
   PBITMAP        tiff;


   tiff = ReadTiff( filename, 0 );
   
   if ( !tiff )
   {
      fprintf( stderr, "Can't read %s\n", filename );
      return 1;
   }
   
   if ( tiff->sampleformat!=BITMAP_UINT8 
	&& tiff->sampleformat!=BITMAP_UINT16
	&& tiff->sampleformat!=BITMAP_IEEE )
   {
      fprintf( stderr, 
            "TIFF file %s is not a \"r\", \"rgb\", \"rgba\" or IEEE image\n", 
            filename );
      goto Error;
   }
   
   if (setflags)
   {
      tiff->orientation = orientation;
   }
   else
   {
      tiff = OrientBitmap( tiff, orientation );
   }

   if (WriteTiff( tiff, filename ))
   {
      fprintf( stderr, "Can't write file: \"%s\"\n", filename );
      goto Error;
   }
   
   DestroyBitmap( tiff );
   return 0;

 Error:
   DestroyBitmap( tiff );
   return 1;
}
예제 #2
0
int main(int argc, char **argv) 
{
   PBITMAP  tiff;

   if ( argc < 3 || (argc > 1 && argv[1][0]=='-') )
   {
      /* User tried to specify an option, so just print help text. */
      printf( "tiff2pix tiff_filename X_filename\n"                      \
          "   tiff_filename     TIFF file to read from.\n"               \
          "   X_filename        X windows bitmap file.\n" );
      return 1;
   }
   
   /* Check that the input file name doesn't match the output file name. */
   if (!strcmp(argv[1],argv[2]))
   {
      fprintf( stderr, "Filenames can not refer to the same file: \"%s\".\n", 
              argv[1] );
      return 1;            
   }

   tiff = ReadTiff( argv[1], 0 );
   if (!tiff)
   {
      fprintf( stderr, "Can't read %s\n", argv[1] );
      return 1;
   }
   tiff = OrientBitmap( tiff, BITMAP_TOPLEFT );

   if (tiff->nsamples!=1 && tiff->bitspersample!=1)
   {
      fprintf( stderr, "Requires data to be 1 bit per pixel.\n" );
      return 1;
   }     

   if (WriteX( tiff, argv[2] ))
   {
      fprintf( stderr, "Can't write %s\n", argv[2] );
      return 1;
   }

   DestroyBitmap( tiff );
   
   return 0;
}
예제 #3
0
int ReadImage(const char *filename,
              std::vector<unsigned char> * ptr,
              int * w,
              int * h,
              int * depth){
  const Format f = GetFormat(filename);

  switch (f) {
    case Pnm:
      return ReadPnm(filename, ptr, w, h, depth);
    case Png:
      return ReadPng(filename, ptr, w, h, depth);
    case Jpg:
      return ReadJpg(filename, ptr, w, h, depth);
    case Tiff:
      return ReadTiff(filename, ptr, w, h, depth);
    default:
      return 0;
  };
}
예제 #4
0
int main(int argc, char **argv) 
{
   PBITMAP      tiff;
   uint32       n;
   uint8        *p8;
   uint16       *p16;
   auto uint32  i,j,x,y;


   if ( argc < 2 || (argc > 1 && argv[1][0]=='-') )
   {
      /* User tried to specify an option, so just print help text. */
      printf( "tiffinvert tiff_filename1 ...\n"                         \
          "   tiff_filename1 ...   TIFF file(s) to overwrite with an\n" \
          "                        an inverted version of their image.\n" );
      return 1;
   }

   for ( i=1; i<argc; i++ )
   {
      tiff = ReadTiff( argv[i], 0 );
   
      if ( !tiff )
      {
	 fprintf( stderr, "Can't read %s\n", argv[i] );
	 return 1;
      }

      if ( tiff->sampleformat!=BITMAP_UINT8 
	   && tiff->sampleformat!=BITMAP_UINT16 )
      {
	 fprintf( stderr, 
		  "TIFF file %s is not an \"r\", \"rgb\" or \"rgba\" image\n", 
		  argv[i] );
	 goto Skip;
      }

      if ( tiff->sampleformat==BITMAP_UINT8 )
      {
	 n = tiff->yres * tiff->rowbytes;
	 p8 = tiff->pbits;
	 for ( j=0; j<n; j++ )
	    *p8++ = 0xff - *p8;	 
      }
      else /* tiff->sampleformat==BITMAP_UINT16 */
      {
	 n = tiff->xres * tiff->nsamples;
	 p16 = (uint16*)tiff->pbits;
	 for ( y=0; y<tiff->yres; y++ )
	    for ( x=0; x<n; x++ )
	       *p16++ = 0xffff - *p16;	 
      }

      if (WriteTiff( tiff, argv[i] ))
      {
	 fprintf( stderr, "Can't write file: \"%s\"\n", argv[i] );
	 goto Error;
      }

   Skip:
      DestroyBitmap( tiff );
   }

   return 0;

 Error:
   DestroyBitmap( tiff );
   return 1;
}
예제 #5
0
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;
}
예제 #6
0
int main(int argc, char **argv) 
{
   int      forced;
   int      filetype;
   int      i;
   PBITMAP  pBmp;
   pid_t    pid;


   if ( argc==1 )
   {
      PrintHelp();
      return 1;      
   }

   filetype = UNKNOWN_FILETYPE;
   forced = 0;
   i = 1;
   while ( i < argc )
   {
      if ( argv[i][0]=='-' )
      {
         if (!strcmp(&argv[i][1],"pix"))
         {
            filetype = PIX_FILETYPE;
            forced = 1;
         }
         else if (!strcmp(&argv[i][1],"tiff") || !strcmp(&argv[i][1],"tif"))
         {
            filetype = TIFF_FILETYPE;
            forced = 1;
         }
         else if (!strcmp(&argv[i][1],"tga"))
         {
            filetype = TGA_FILETYPE;
            forced = 1;
         }
         else if (!strcmp(&argv[i][1],"iff"))
         {
            filetype = IFF_FILETYPE;
            forced = 1;
         }
         else if (!strcmp(&argv[i][1],"auto"))
         {
            filetype = UNKNOWN_FILETYPE;
            forced = 0;
         }
         else
         {
            PrintHelp();
            return 1;
         }
      }
      else
      {
         if ( !filetype )
         {
            filetype = GetFileType( argv[i] );
         }
         if ( filetype )
         {
            switch ( filetype )
            {
             case PIX_FILETYPE:
               pBmp = ReadPix( argv[i] );
               break;
             case TIFF_FILETYPE:
               pBmp = ReadTiff( argv[i], READTIFF_CONVERT16TO8 );
               break;
             case TGA_FILETYPE:
               pBmp = ReadTga( argv[i] );
               break;
             case IFF_FILETYPE:
               pBmp = ReadIff( argv[i], READIFF_CONVERT16TO8 );
               break;
             default:
               exit(1);
            }
   
            pid = fork();
            if ( pid < 0 )
            {
               printf( "Error:  fork()\n" );
               return 1;
            }
            else if ( pid==0 )
            {
               DisplayBitmap( pBmp, argv[i] );
               exit(1);
            }
            if (!forced)
            {
               filetype = UNKNOWN_FILETYPE;
            }
         }
         else
         {
            printf( "ERROR:  Unknown file type: %s.\n", argv[i] );
            PrintHelp();
         }
      }     
      i++;
   }

   return 0;
}
예제 #7
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;
}