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

📄 image.cpp

📁 本程序主要完成pic到tif图像格式的转换(由图象格式转换菜单实现)并且完成了读写pic,tif图像文件的源代码。File/open菜单完成在原始图中获取任意位置和大小的图像。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Image.cpp: implementation of the CImage class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "convert.h"
#include "Image.h"
#include "math.h"

#define WIDTHBYTES(i)    ((i+31)/32*4)
    //TIFF size
#define TIFFbyte 1
#define TIFFascii 2
#define TIFFshort 3
#define TIFFlong 4
#define TIFFrational 5

#define TagNum 6
////////////////////
#define SubfileType 254//...

#define ImageWidth 256
#define ImageLength 257
#define SamplesPerPixel 277  //...
#define BitsPerSample 258
#define Compression   259
#define PhotometricInterp 262
#define StripOffsets    273
//////////////////////
////tif compression types
#define COMPnone  1
#define COMPhuff  2
#define COMPfax3  3
#define COMPfax4  4
#define COMPwrd1  0x8003
#define COMPmpnt  0x8005


long BeforeData=(long)(8+(2+TagNum*12+4));

void WriteTifHeader(FILE *fp);
void WriteTifDict(FILE *fp);
void WriteTifTag(FILE *fp,int tag,int type,long length,long offset);
void fputWord(FILE *fp,int n);
void fputLong(FILE *fp,long n);
FILE *WriteFileHead(int row,int col,const char *filen);
void OutputTifImageWithName(unsigned char **Image,int Row,int Col,const char *FileName);

unsigned int TIFFversion;
unsigned int TIFFentries;
unsigned int TIFFsubfile;
unsigned int TIFFsamples;
unsigned int TIFFbitspersamples;
unsigned int TIFFsamples2;
unsigned int TIFFplancfg;
unsigned int TIFFcompres;
unsigned int TIFFphotmet;

unsigned long TIFFoffset;
unsigned long TIFFrowstrip;
unsigned long TIFFstripoff;
unsigned long TIFFstripcnt;
unsigned long TIFFbytecntoff;
unsigned long TIFFbytecnt;
unsigned long PaletteOff;

unsigned long imageStart=0L;
unsigned long imageSize=0L;
unsigned int bytes=0;
unsigned int fgetWord(FILE *fp);
unsigned long fgetLong(FILE *fp);
unsigned int pixels2bytes(unsigned int n);
void DecodeTag(FILE *fp,int *width, int *depth);
unsigned char **InputTifImgWithName(int *Row,int *Col,const char *FileName);

unsigned char ***ReadBmpData(int *row,int *col,const char *filename);
void OutputBmpWithName(unsigned char ***Image,int Row,int Col,const char *FileName);//save as bmp file

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CImage::CImage()
{
     m_ImageHeight=NULL;
	 m_ImageWidth=NULL;
	 dwPosition=NULL;
	 image=NULL;
}

CImage::~CImage()
{
 
 if(image!=NULL)//............
		{    
		for(int i=0;i<m_ImageHeight;i++) 
			if(image[i]!=NULL)
				delete image[i];		
	    delete image;
		}//..................
}

BOOL CImage::ReadImageData(CString filename)
{
	CString strFile=filename;///
	CString S1=filename;
	int number=S1.GetLength();  
	int posit=0;
	for(int i=0;i<number;i++)///
	{
		if((S1.GetAt(i))!='.') posit++;
	    if((S1.GetAt(i))=='.') break;
	}
	S1.Delete(0,posit+1);    ///
	   
	int flag=0;
	if((S1.Compare("pic")==0)||(S1.Compare("PIC")==0)) flag=1;    //pic
	if((S1.Compare("tif")==0)||(S1.Compare("tif")==0)) flag=2;    //tif
	
	if(flag==0)
	{
		MessageBox(NULL,"the file's external must be *.tif,*.pic!!",NULL,MB_ABORTRETRYIGNORE);
		return FALSE;
	}
	//read the different type of the picture  flag=1..pic; 2..tif;

    if (flag==1)
	{
	CFile ImageFile;
    ImageFile.Open(filename,CFile::modeRead);
    unsigned char head[64];
    ImageFile.Seek(0,CFile::begin);////
   	ImageFile.Read(head,64);
	dwPosition=ImageFile.GetPosition();
	
	m_ImageWidth=head[4]+(head[5]<<8);
	m_ImageHeight=head[6]+(head[7]<<8);
	TRACE("col=%d,row=%d\n",m_ImageWidth,m_ImageHeight);
	
    ImageFile.Seek(dwPosition,CFile::begin);//64,CFile::begin);
    
    image=new unsigned char* [m_ImageHeight];
	for(i=0;i<m_ImageHeight;i++)
		 image[i]=new unsigned char [m_ImageWidth];
	if(image==NULL)
	{    
		for(i=0;i<m_ImageHeight;i++)  delete image[i];
	delete image;
	return FALSE;
	}

   	for(i=0;i<m_ImageHeight;i++)
	     ImageFile.ReadHuge(image[i],m_ImageWidth);

 	ImageFile.Close();
	}

	if(flag==2)
	{
		image=InputTifImgWithName(&m_ImageHeight,&m_ImageWidth,(const char*)filename);

	} 
	return TRUE;

}

/*read tif_img with name and return img data*/
unsigned int fgetWord(FILE *fp)
{
   unsigned int i,j;

   i=(unsigned int)(fgetc(fp)&0xff);
   j=(unsigned int)((fgetc(fp)&0xff)<<8);

   return(i+j);
}
unsigned long fgetLong(FILE *fp)
{
	unsigned long i,j,l,k;

	i=(unsigned long)(fgetc(fp)&0xff);
	j=(unsigned long)((fgetc(fp)&0xff)<<8);
	k=(unsigned long)((fgetc(fp)&0xff)<<16);
	l=(unsigned long)((fgetc(fp)&0xff)<<24);

	return(i+j+k+l);
}
unsigned int pixels2bytes(unsigned int n)
{
	if(n&0x007)  return((n>>3)+1);
	else return(n>>3);
}

void DecodeTag(FILE *fp,int *width, int *depth)
{
	unsigned long length,offset,offset2;
	int tag,type;

	tag=fgetWord(fp);
	type=fgetWord(fp);
	
	if(type==TIFFlong)
	{
		length=fgetLong(fp);
		offset=fgetLong(fp);
	}
	else
	{
		length=(unsigned long)fgetWord(fp);
		fgetWord(fp);
		offset=(unsigned long)fgetWord(fp);
		offset2=(unsigned long)fgetWord(fp);
	}
	switch(tag){
	case SubfileType:
		TIFFsubfile=(unsigned int)offset;
		break;
	case ImageWidth:
		*width=(int)offset;
		bytes=(unsigned int)pixels2bytes((unsigned int)*width);////..
		break;
	case ImageLength:
		*depth=(int)offset;
		break;

	case PhotometricInterp:
		TIFFphotmet=(unsigned int)offset;
		if(TIFFphotmet!=3&&TIFFphotmet!=1&&TIFFphotmet!=0)
		{
            MessageBox(NULL,"this TIFF format is not surported!",NULL,MB_ABORTRETRYIGNORE);
	        fclose(fp);
			exit(1);
		}
		break;
	case StripOffsets:
		if(type==TIFFlong){
			TIFFstripoff=offset;
			TIFFstripcnt=length;
		}
		else{
			TIFFstripoff=offset&0xffffL;
			TIFFstripcnt=length&0xffffL;
		}
//		if(offset2)
//			TIFFstripoff+=offset2<<16;
		break;
	case SamplesPerPixel:
		TIFFsamples=(unsigned int)offset;
		break;
	case BitsPerSample:
        TIFFbitspersamples=(unsigned int)offset;
		TIFFsamples2=(unsigned int)length;
		break;
	case Compression:
		TIFFcompres=(unsigned int)offset;
		if(TIFFcompres!=COMPnone&&TIFFcompres!=COMPmpnt)
		{
            MessageBox(NULL,"this TIFF format is not surported!",NULL,MB_ABORTRETRYIGNORE);
	        fclose(fp);
			exit(1);
		}
		break;
	default:
		break;
	}

}

unsigned char **InputTifImgWithName(int *Row,int *Col,const char *FileName)
{
  unsigned char **Image;
  FILE          *fp;
  
  if((fp=fopen(FileName,"rb"))==NULL){
    MessageBox(NULL,"this file can not be opened!!",NULL,MB_ABORTRETRYIGNORE);
    exit(1);}
  
  fseek(fp,0,SEEK_SET);
  if(fgetWord(fp)!=(unsigned)0x4949)
  {
	  MessageBox(NULL,"this file is not an invalide TIFF file!",NULL,MB_ABORTRETRYIGNORE);
	  exit(1);
  }
  if((TIFFversion=fgetWord(fp))!=42)
  {
	  MessageBox(NULL,"this file is not an invalide TIFF file!...",NULL,MB_ABORTRETRYIGNORE);
	  exit(1);
  }
  
  TIFFoffset=fgetLong(fp);
  fseek(fp,TIFFoffset,SEEK_SET);
  TIFFentries=fgetWord(fp);//
 
  for(unsigned int p=0;p<TIFFentries;p++)
  {
	  DecodeTag(fp,Col,Row);
  }
  TRACE("  Samples or column: %d . Lines or row: %d\n",*Col,*Row);

  unsigned long nextfile=fgetLong(fp);/////////
  if(nextfile!=0)
  {   
	  fclose(fp);
      MessageBox(NULL,"TIFF file has subfile and this program is not surport it!",NULL,MB_ABORTRETRYIGNORE);
	  exit(1); 
  }                     ////////
  if(TIFFcompres!=1)
  {
	 fclose(fp);
     MessageBox(NULL,"this program does not surport compress !",NULL,MB_ABORTRETRYIGNORE);
	  exit(1); 
  }

   Image=new unsigned char* [*Row];
	for(int i=0;i<*Row;i++)
		 Image[i]=new unsigned char [*Col];
	if(Image==NULL)
	{    
		for(i=0;i<*Row;i++)  delete Image[i];
	delete Image;
	exit(1);
	} 
  fseek(fp,TIFFstripoff,SEEK_SET);
  for(int RowNo=0;RowNo<*Row;RowNo++)
	fread(Image[RowNo],sizeof(unsigned char),*Col,fp);
 /* for(i=0;i<*Row;i++)
	  for(int j=0;j<*Col;j++)
	  {
		  if(TIFFphotmet==1)
			  Image[i][j]=~Image[i][j];

	  }*/
  fclose(fp);

⌨️ 快捷键说明

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