📄 image.cpp
字号:
return(Image);
}
BOOL CImage::WriteImageFile(CString filename, int row, int col, unsigned char **data)
{
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) flag=1; //pic
if(S1.Compare("tif")==0) flag=2; //tif
// if(S1.Compare("bmp")==0) flag=3; //bmp
if(flag==0)
{
MessageBox(NULL,"the file's external must be *.tif,*.pic!",NULL,MB_ABORTRETRYIGNORE);
return FALSE;
}
if(flag==1)
{
//it is for save pic Img
CFile PicFile(filename, CFile::modeCreate | CFile::modeWrite );
unsigned char head[64];
for(i=0;i<64;i++)
head[i] = 0;
head[0] = 'I'; /* image type. 'I':b/w image, 'C':color image */
head[1] = 'M';
head[2] = head[3] = 0; /* comment space */
head[4] = col % 256;
head[5] = col / 256; /* column size */
head[6] = row % 256;
head[7] = row / 256; /* row size */
head[8] = head[9] = 0; /* column start */
head[10] = head[11] = 0; /* row start */
head[12] = head[13] =0;
head[14] = 1%256; //type%256
head[15] = 1/256; //type/256 /* data type. 1:unsigned char, 2:int, */
/* 3:float, 4:double */
PicFile.Write(head,64);
if(data==NULL)
return FALSE;
PicFile.Seek(64,CFile::begin);
for(i=0;i<row;i++)
PicFile.WriteHuge(data[i],col);
PicFile.Close();
}
if(flag==2) //it is for save as tif Img
{
OutputTifImageWithName(data,row,col,(const char*)filename);
}
return TRUE;
}
//////////////////////////////////////////////
/*write a TIFF header*/
void WriteTifHeader(FILE *fp)
{
fputWord(fp,'II');
fputWord(fp,42);
fputLong(fp,0L);
}
/*write a TIFF IFD*/
void WriteTifDict(FILE *fp,int deep,int width)
{
long l;
l=ftell(fp);//remember where we parked
fputWord(fp,TagNum);//there will be six tags
WriteTifTag(fp,ImageWidth,TIFFshort,1L,(long)width);//(long)width col
WriteTifTag(fp,ImageLength,TIFFshort,1L,(long)deep);//(long)deep);row
WriteTifTag(fp,BitsPerSample,TIFFshort,1L,8L);//
WriteTifTag(fp,Compression,TIFFshort,1L,1);//without compression
WriteTifTag(fp,PhotometricInterp,TIFFshort,1L,1);//0
WriteTifTag(fp,StripOffsets,TIFFlong,1L,BeforeData);//86);
TRACE("width=%d,deep=%d\n",width,deep);
fputLong(fp,0L);//end of the IFD
fseek(fp,4L,SEEK_SET); //point to offset field of header
fputWord(fp,(unsigned int)l);//point to the start of the IFD
fseek(fp,BeforeData,SEEK_SET);//point to image data block.
}
/*write one TIFF tag to the IFD*/
void WriteTifTag(FILE *fp,int tag,int type,long length,long offset)
{
fputWord(fp,tag);
fputWord(fp,type);
fputLong(fp,length);
fputLong(fp,offset);
}
void fputWord(FILE *fp,int n)
{
fputc(n,fp);
fputc((n>>8),fp);
}
void fputLong(FILE *fp,long n)
{
fputc(n,fp);
fputc((n>>8),fp);
fputc((n>>16),fp);
fputc((n>>24),fp);
}
/* To open a file to write data in */
FILE *WriteFileHead(int row,int col,const char *filen)
{
FILE *fp;
if ((fp=fopen(filen,"wb"))==NULL) {
//printf(" The file %s is not created [1]\n",filen);
exit(1);}
WriteTifHeader(fp);
WriteTifDict(fp,row,col);
return(fp);
}
/*save the tif file with name*/
void OutputTifImageWithName(unsigned char **Image,int Row,int Col,const char*FileName)
{
int RowNo;
FILE *fp;
fp = WriteFileHead(Row,Col,FileName);
for(RowNo=0;RowNo<Row;RowNo++)
{
fwrite(Image[RowNo],sizeof(unsigned char),Col,fp);
}
//printf("\nOutput file in %s\n",FileName);
fclose(fp);
}
//////////////////////////
//open BMP file and read image data
unsigned char*** ReadBmpData(int *row,int *col,const char* filename)
{
CFile bmpFile;
BITMAPFILEHEADER FileHeader;
BITMAPINFOHEADER InfoHeader;
bmpFile.Open(filename,CFile::modeRead);
bmpFile.Seek(0,CFile::begin);
bmpFile.Read((char *)&FileHeader,sizeof(BITMAPFILEHEADER));
bmpFile.Read((char *)&InfoHeader,sizeof(BITMAPINFOHEADER));
int Height=(unsigned)InfoHeader.biHeight;
int Width=(unsigned)InfoHeader.biWidth;
int linebytes,i,j,k,rest,p;
linebytes=(DWORD)WIDTHBYTES(Width*InfoHeader.biBitCount);
unsigned char*lineData;
lineData=new unsigned char[linebytes];
unsigned char ***pixel;
pixel=new unsigned char**[Height];
if(pixel!=NULL)
{
for(i=0;i<Height;i++)
{
pixel[i]=new unsigned char*[Width];
if(pixel[i]!=NULL)
{
for(j=0;j<Width;j++)
pixel[i][j]=new unsigned char [3];
}
else
{
delete pixel[i];
exit(1);//return FALSE;
}
}
}
else
{
delete pixel;
exit(1);//return FALSE;
}
TRACE("bi.BitCount=%d",InfoHeader.biBitCount);
TRACE("biXPelsPerMeter=%d",InfoHeader.biXPelsPerMeter);
TRACE("biYPelsPerMeter=%d",InfoHeader.biYPelsPerMeter);
TRACE("biSize=%d",InfoHeader.biSize);
TRACE("biSizeImage=%d",InfoHeader.biSizeImage);
TRACE("bfOffBits=%d\n",FileHeader.bfOffBits);
TRACE("bfSize=%d\n",FileHeader.bfSize);
switch(InfoHeader.biBitCount)
{
case 1:
case 4:
case 8:
case 24:
rest=4-((Width*3)%(4));//Mod
if(rest==4)
rest=0;
for(i=0;i<Height;i++)
{
bmpFile.Read(lineData,linebytes);
p=0;
for(j=0;j<Width;j++)
for(k=0;k<3;k++)
{
pixel[Height-1-i][j][k]=lineData[p];
p++;
}
}
break;
default:
break;
}
*row=Height;
*col=Width;
if(lineData!=NULL)
delete lineData;
bmpFile.Close();
return(pixel);
}
//save as bmp file
void OutputBmpWithName(unsigned char ***Image,int Row,int Col,const char *filename)
{
//only for the 真彩色biBitCount=24
CFile bmpFile;
BITMAPFILEHEADER bf;
BITMAPINFOHEADER bi;
bf.bfType=(unsigned short)"BM";
bf.bfReserved1=0;
bf.bfReserved2=0;
bf.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
bi.biSize=40;//sizeof(BITMAPINFOHEADER);
bi.biWidth=Col;
bi.biHeight=Row;
bi.biPlanes=1;
bi.biBitCount=24;//no pallete
bi.biCompression=0;
bi.biXPelsPerMeter=0;
bi.biYPelsPerMeter=0;
bi.biClrImportant=0;
bi.biClrUsed=0;
// bf.bfSize=(DWORD)(bi.biSizeImage+bf.bfOffBits); ///
int linebytes,rest;
unsigned char*lineData;
switch(bi.biBitCount)
{
case 1:
break;
case 4:
break;
case 8:
break;
case 16:
break;
case 24:
////////
rest=4-((Col*3)%(4));
if(rest==4)
rest=0;
bi.biSizeImage=Row*(Col*3+rest);
linebytes=(DWORD)(Col*3+rest);//(DWORD)WIDTHBYTES(Col*bi.biBitCount);///
break;
default:
break;
}
bf.bfSize=(DWORD)(bi.biSizeImage+bf.bfOffBits); ///
bmpFile.Open(filename,CFile::modeCreate|CFile::modeWrite);
bmpFile.Seek(0,CFile::begin);
bmpFile.Write((char *)&bf,sizeof(BITMAPFILEHEADER));
bmpFile.Write((char *)&bi,sizeof(BITMAPINFOHEADER));
lineData=new unsigned char[linebytes];///
if(lineData==NULL)
delete lineData;
int i=0,j=0,p=0;
switch(bi.biBitCount)
{
case 1:
break;
case 4:
break;
case 8:
break;
case 16:
break;
case 24:
for(i=0;i<linebytes;i++)
lineData[i]=0;
p=0;
for(i=0;i<Row;i++)
{
p=0;
for(j=0;j<Col;j++)
{
lineData[p++]=Image[Row-1-i][j][0];
lineData[p++]=Image[Row-1-i][j][1];
lineData[p++]=Image[Row-1-i][j][2];
}
bmpFile.Write(lineData,linebytes);
}
break;
default:
break;
}
if(lineData!=NULL)
delete lineData;
bmpFile.Close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -