⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 image.cpp

📁 the code environment s C++. it is in image processing field. this code Read TIFF Image then it excut
💻 CPP
📖 第 1 页 / 共 2 页
字号:
   BYTE byte2 = fgetc(pfile)&0xff;

   if(m_byteswap)
   {
      return(byte2 + (byte1 << 8));
   }
   else
   {
      return(byte1 + (byte2 << 8));
   }
}


//***********************Load Image******************************
//***************************************************************

void CImage::Load(long m_lWidth, long m_lHeight, BYTE *m_pData, long m_lDataoffset, int m_nBitcount)
 
{


   //void Unload(BYTE *m_pData);
   /* Read pixel data into buffer */
   Unload(m_pData);
   int n=0;
   cout<<" Load :m_lWidth ="<<m_lWidth<<endl;
   cout<<" Load function :m_lHeight="<<m_lHeight<<endl;
   cout<< "  Load : m_lWidth ="<<m_lWidth<<endl;
   cout<<" Load : m_lHeight ="<<m_lHeight<<endl;
   cout<<" Load : m_lDataoffset ="<<m_lDataoffset<<endl;
   cout<<" Load :m_nBitcount = "<<m_nBitcount<<endl;
   //cout<<" Load : m_byteswap = "<<m_byteswap<<endl;
 

    m_pData =(BYTE*)malloc(m_lWidth * m_lHeight * long(m_nBitcount / 8.0));
   if(m_pData == NULL)
   {
      cout<<"encountered a problem allocating a block of memory"<<endl;
      return;
   }
   



  FILE *pfile = fopen(m_strFilename, "rb");
   if(pfile == NULL)
   {
      cout<<"unable to open the file"<<endl;
	  return;
   }
   fseek(pfile, m_lDataoffset, 0);
   n=fread(m_pData, 1, m_lWidth * m_lHeight * long(m_nBitcount / 8.0), pfile);
   cout<<"n="<<n<<endl;
   cout<<"autre= "<<m_lWidth * m_lHeight * long(m_nBitcount / 8.0)<<endl;
   fclose(pfile);
   cout<<"Inside of load"<<endl;
   for (int i=10; i<15; i++)
   {
   cout<<"value load ="<<(float)m_pData[i]<<endl;
   }
   return;
}


//**************Unload image********************//
//***********************************************
void CImage::Unload(BYTE *m_pData)
{
   if(m_pData != NULL)
   {
      free(m_pData);
      m_pData=NULL;
   }
}



void CImage::Unload1Dfloat(float *m_pData)
{
   if(m_pData != NULL)
   {
      free(m_pData);
      m_pData=NULL;
   }
}
//******************Write image***************************
//********************************************************

 
 
//************************Write tif image************************
//***************************************************************

void CImage::WriteTIFFUnsignedChar(long m_lWidth, long m_lHeight, float *m_p)
{
    
    //void Unload(BYTE *m_pData);
   
   BYTE *m_pData=NULL;

   
   m_pData =(BYTE*)malloc(m_lWidth * m_lHeight * long(1.0));
   if(m_pData == NULL)
   {
      cout<<" memory allocation error:";
	  getchar();
      exit(1);
   }

   for (int i=0; i<m_lWidth * m_lHeight; i++)
   m_pData[i]=(unsigned char) m_p[i];




   FILE *pfileImage;
   unsigned short tempshort;
   unsigned long templong;
   cout<<"Please enter the name of output image: ";
   cin>>m_strFilename;

   cout<<" write:m_lWidth ="<<m_lWidth<<endl;
   cout<<" write:m_lHeight="<<m_lHeight<<endl;

   /* Open file */
   pfileImage = fopen(m_strFilename, "wb");
   if(pfileImage == NULL)
   {
      cout<<"unable to open the file to write"<<endl;
	  return;
   }

   tempshort = 0x4949;                                         
   /* identifier */
   fwrite(&tempshort, 2, 1, pfileImage);
   tempshort = 0x2a;                                           
   /* version */
   fwrite(&tempshort, 2, 1, pfileImage);
   templong = 8;                                               
   /* ifd offset */
   fwrite(&templong, 4, 1, pfileImage);
   
   /* -- Start of IFD -- */

   tempshort = 7;    
   /* number of fields */
   fwrite(&tempshort, 2, 1, pfileImage);

   /* -> subfile type field entry */

   tempshort = 0xff; 
   /* subfile type */
   fwrite(&tempshort, 2, 1, pfileImage);
   tempshort = 3;                       
   /* value is type short */
   fwrite(&tempshort, 2, 1, pfileImage);
   templong = 1;  
   /* number of values */
   fwrite(&templong, 4, 1, pfileImage);
   tempshort = 1;  
   /* value */
   fwrite(&tempshort, 2, 1, pfileImage);
   tempshort = 0;   
   /* complete field with 0's */
   fwrite(&tempshort, 2, 1, pfileImage);

   /* -> m_lWidth field entry */

   tempshort = 0x100;  
   /* image m_lWidth */
   fwrite(&tempshort, 2, 1, pfileImage);
   tempshort = 3;  
   /* value is type short */
   fwrite(&tempshort, 2, 1, pfileImage);
   templong = 1;    
   /* number of values */
   fwrite(&templong, 4, 1, pfileImage);
   tempshort = (unsigned short)m_lWidth;  
   /* value */
   fwrite(&tempshort, 2, 1, pfileImage);
   tempshort = 0;  
   /* complete field with 0's */
   fwrite(&tempshort, 2, 1, pfileImage);
   
   /* -> m_lHeight field entry */

   tempshort = 0x101; 
   /* image m_lHeight */
   fwrite(&tempshort, 2, 1, pfileImage);
   tempshort = 3;      
   /* value is type short */
   fwrite(&tempshort, 2, 1, pfileImage);
   templong = 1;     
   /* number of values */
   fwrite(&templong, 4, 1, pfileImage);
   tempshort = (unsigned short)m_lHeight; 
   /* value */
   fwrite(&tempshort, 2, 1, pfileImage);
   tempshort = 0;    
   /* complete field with 0's */
   fwrite(&tempshort, 2, 1, pfileImage);

   /* -> bits per sample field entry */

   tempshort = 0x102; 
   /* compression */
   fwrite(&tempshort, 2, 1, pfileImage);
   tempshort = 3;  
   /* value is type short */
   fwrite(&tempshort, 2, 1, pfileImage);
   templong = 1;    
   /* number of values */
   fwrite(&templong, 4, 1, pfileImage);
   tempshort = 8; 
   /* value */
   fwrite(&tempshort, 2, 1, pfileImage);
   tempshort = 1;  
   /* complete field with 0's */
   fwrite(&tempshort, 2, 1, pfileImage);

   /* -> compression field entry */

   tempshort = 0x103; 
   /* compression */
   fwrite(&tempshort, 2, 1, pfileImage);
   tempshort = 3;  
   /* value is type short */
   fwrite(&tempshort, 2, 1, pfileImage);
   templong = 1;                                       
   /* number of values */
   fwrite(&templong, 4, 1, pfileImage);
   tempshort = 1;
   /* value */
   fwrite(&tempshort, 2, 1, pfileImage);
   tempshort = 1; 
   /* complete field with 0's */
   fwrite(&tempshort, 2, 1, pfileImage);

   /* -> image type field entry */

   tempshort = 0x106; 
   /* image type */
   fwrite(&tempshort, 2, 1, pfileImage);
   tempshort = 3; 
   /* value is type short */
   fwrite(&tempshort, 2, 1, pfileImage);
   templong = 1;      
   /* number of values */
   fwrite(&templong, 4, 1, pfileImage);
   tempshort = 1; 
   /* value */
   fwrite(&tempshort, 2, 1, pfileImage);
   tempshort = 1;  
   /* complete field with 0's */
   fwrite(&tempshort, 2, 1, pfileImage);

   /* -> strip offset field entry */

   tempshort = 0x111;  
   /* strip offset */
   fwrite(&tempshort, 2, 1, pfileImage);
   tempshort = 3;  
   /* value is type short */
   fwrite(&tempshort, 2, 1, pfileImage);
   templong = 1;    
   /* number of values */
   fwrite(&templong, 4, 1, pfileImage);
   tempshort = 98;                                                 
   /* value */
   fwrite(&tempshort, 2, 1, pfileImage);
   tempshort = 0;   
   /* complete field with 0's */
   fwrite(&tempshort, 2, 1, pfileImage);

   /* -> offset to nxt ifd, 0 since there isn't one */

   templong = 0;
   fwrite(&templong, 4, 1, pfileImage);

   /* -- End of IFD -- */

   /* Write the raw pixel data */
   unsigned long numreturn = 0;
   for(int n = 0; n < m_lHeight; n++)
   {
      numreturn += fwrite(m_pData+(n*m_lWidth), 1, m_lWidth, pfileImage);
   }

   fclose(pfileImage);
   Unload(m_pData);
}


//****************SwapLong*********************************
//*********************************************************

unsigned long CImage::SwapLong(unsigned long value)
{

   unsigned long result = 0;
   ((BYTE*)&result)[0] = ((BYTE*)&value)[3];
   ((BYTE*)&result)[1] = ((BYTE*)&value)[2];
   ((BYTE*)&result)[2] = ((BYTE*)&value)[1];
   ((BYTE*)&result)[3] = ((BYTE*)&value)[0];
   return result;

}


//*********************************MallocError*************************
//*********************************************************************
void CImage::MallocError(char *info, char* file, int line)
{

	cout<<"encountered a problem allocating a block of memory"<<endl;
    
}



//************************File Open Error *************************************
//*****************************************************************************
//void CImage::FileOpenError(char* file,  int line)
//{

  // cout<<"unable to open the file"<<endl;
    
//}

 

 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -