📄 omlib.cpp
字号:
// omlib.cpp: implementation of the Comlib class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "linjunjuan.h"
#ifndef COMLIB_H
#define COMLIB_H
#include "omlib.h"
#endif
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#include "RawDlg.h"
//===========================TIFF=====================================
//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));
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;
///////////////////////////////////////////////
///////////////////////////////////////////////
//////////////////////////////////////////////////
//add by ZouLaMei for the tif Image read and write
//////////////////////////////////////////////////
//存取tif格式图象所包含的子函数
void WriteTifHeader(FILE *fp);
void WriteTifDict(FILE *fp,int deep,int width); ///
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,char*filen);
void OutputTifImageWithName(unsigned char **Image,int Row,int Col,char*FileName);
//读取tif格式图象所包含的子函数
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,char*FileName);
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Comlib::Comlib()
{
}
Comlib::~Comlib()
{
}
static BOOL ASSERTVALIDPOINTER(void * pointer, CString message)
{
if(pointer == NULL)
{
#ifdef _DEBUG
afxDump << "Error Message " << message << "\n";
#endif
return FALSE;
}
return TRUE;
}
FILE *wfwrite1(int row,int col,int type, CString filename)
{
int i;
unsigned char head_byte[64];
FILE *fp = fopen(filename,"wb+");
CString err;
err.Format(" %s is not created!\n" ,filename);
if(! ASSERTVALIDPOINTER(fp, err)) return NULL;
for(i=0;i<64;i++) head_byte[i] = 0;
head_byte[0] = 'I'; /* image type. 'I':b/w image, 'C':color image */
head_byte[1] = 'M';
head_byte[2] = head_byte[3] = 0; /* comment space */
head_byte[4] = col % 256;
head_byte[5] = col / 256; /* column size */
head_byte[6] = row % 256;
head_byte[7] = row / 256; /* row size */
head_byte[8] = head_byte[9] = 0; /* column start */
head_byte[10] = head_byte[11] = 0; /* row start */
head_byte[12] = head_byte[13] =0;
head_byte[14] = type % 256;
head_byte[15] = type / 256; /* data type. 1:unsigned char, 2:int, */
/* 3:float, 4:double */
if(col*row != 0) fwrite(head_byte,64,1,fp);
return(fp);
}
BOOL OutputImageWithName(unsigned char **Image,int Row,int Col, CString FileName)
{
int RowNo;
FILE *fp;
if(Image ==NULL) return FALSE;
fp = wfwrite1(Row,Col,1,FileName);
if(fp == NULL) return FALSE;
long aa=0;
for(RowNo=0;RowNo<Row;RowNo++)
aa += fwrite(Image[RowNo],sizeof(unsigned char),Col,fp);
fclose(fp);
return TRUE;
}
BOOL normalize(int **input,int *row,int *col,Picture output)
{
int max=0;
for (int i=0;i<*row;i++)
for (int j=0;j<*col;j++)
{
if (input[i][j]>max)
{
max=input[i][j];
}
}
if (max!=0)
{
for (i=0;i<*row;i++)
for (int j=0;j<*col;j++)
{
output.p[i][j]=(unsigned char )(input[i][j]*255/max);
}
return TRUE;
}
return FALSE;
}
/*
BOOL juanji(Picture input,KERNEL kerx,KERNEL kery,Picture output)
{
int tempx,tempy;
for (int i=1;i<input.row-1;i++)
for (int j=1;j<input.col-1;j++)
{
tempx=0;tempy=0;
for (int k=0;k<3;k++)
for (int l=0;l<3;l++)
{
tempx+=input.p[i+k-1][j+l-1]*kerx.kernel[k*3+l];
tempy+=input.p[i+k-1][j+l-1]*kery.kernel[k*3+l];
}
output.p[i][j]=abs(tempx)+abs(tempy);
}
return TRUE;
}
*/
void Writebmp(CDC *pdc,unsigned char **Picture,int row,int col)
{
HBITMAP hbmp;
HDC memDC;
BYTE* pHeader;
pHeader=new BYTE[sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD)];
BITMAPINFOHEADER* lpbmih;
lpbmih=(BITMAPINFOHEADER*)pHeader;
BITMAPINFO* lpbmi;
lpbmi=(BITMAPINFO*)pHeader;
lpbmih->biBitCount=8;
lpbmih->biClrImportant=0;
lpbmih->biClrUsed=0;
lpbmih->biCompression=BI_RGB;
lpbmih->biHeight=row;
lpbmih->biPlanes=1;
lpbmih->biSize=sizeof(BITMAPINFOHEADER);
lpbmih->biWidth=col;
lpbmih->biXPelsPerMeter=80;
lpbmih->biYPelsPerMeter=80;
for(int i=0;i<256;i++)
{
lpbmi->bmiColors[i].rgbBlue=
lpbmi->bmiColors[i].rgbGreen=
lpbmi->bmiColors[i].rgbRed=(BYTE)i;
lpbmi->bmiColors[i].rgbReserved=0;
}
BYTE* lpSrcdata;
unsigned int nwidth;
nwidth=((col+3)/4)*4;
lpSrcdata=new BYTE[row*nwidth];
BYTE temp;
for(i=0;i<row;i++)
for(int j=0;j<col;j++)
{
temp=Picture[i][j];
lpSrcdata[(row-1-i)*nwidth+j]=temp;
}
memDC=CreateCompatibleDC(pdc->GetSafeHdc());
hbmp=CreateDIBitmap(pdc->GetSafeHdc(),lpbmih,CBM_INIT,lpSrcdata,lpbmi,DIB_RGB_COLORS);
SelectObject(memDC,hbmp);
StretchBlt(pdc->GetSafeHdc(),0,0,col,row,memDC,0,0,col,row,SRCCOPY);
DeleteDC(memDC);
}
BOOL Laplacian(Picture input,KERNEL ker,Picture output)
{
int temp;
int **Temp;
Temp= new int *[input.row];
for (int s=0;s<input.row;s++)
{
Temp[s]=new int [input.col];
ZeroMemory(Temp[s],input.col*sizeof(int));
}
if (!Temp)
return FALSE;
for (int i=1;i<input.row-1;i++)
for (int j=1;j<input.col-1;j++)
{
temp=0;
for (int k=0;k<3;k++)
for (int l=0;l<3;l++)
{
temp+=input.p[i+k-1][j+l-1]*ker.kernel[k*3+l];
}
Temp[i][j]=abs(temp);
}
normalize(Temp,&input.row,&input.col,output);
return TRUE;
}
BOOL Sobel(Picture input,KERNEL kx,KERNEL ky,Picture output)
{
int tempx,tempy;
int **Temp;
Temp= new int *[input.row];
for (int s=0;s<input.row;s++)
{
Temp[s]=new int [input.col];
ZeroMemory(Temp[s],input.col*sizeof(int));
}
if (!Temp)
return FALSE;
for (int i=1;i<input.row-1;i++)
for (int j=1;j<input.col-1;j++)
{
tempx=0;tempy=0;
for (int k=0;k<3;k++)
for (int l=0;l<3;l++)
{
tempx+=input.p[i+k-1][j+l-1]*kx.kernel[k*3+l];
tempy+=input.p[i+k-1][j+l-1]*ky.kernel[k*3+l];
}
Temp[i][j]=abs(tempx)+abs(tempy);
}
normalize(Temp,&input.row,&input.col,output);
return TRUE;
}
BOOL Noclear(Picture input,Picture output)
{
int temp;
int **Temp;
Temp= new int *[input.row];
for (int s=0;s<input.row;s++)
{
Temp[s]=new int [input.col];
ZeroMemory(Temp[s],input.col*sizeof(int));
}
if (!Temp)
return FALSE;
for (int i=2;i<input.row-2;i++)
for (int j=2;j<input.col-2;j++)
{
temp=0;
for (int k=0;k<5;k++)
for (int l=0;l<5;l++)
{
temp+=input.p[i+k-2][j+l-2];
}
Temp[i][j]=temp;
}
normalize(Temp,&input.row,&input.col,output);
return TRUE;
}
BOOL Noise(Picture oldpic,Picture dealpic,int num,int gray)
{
int x,y;
for (int i=0;i<oldpic.row;i++)
for (int j=0;j<oldpic.col;j++)
{
dealpic.p[i][j]=oldpic.p[i][j];
}
for (i=0;i<num;i++)
{
x=rand()%oldpic.row;
y=rand()%oldpic.col;
dealpic.p[x][y]=gray;
}
return TRUE;
}
/*
BOOL Line_Deal(Picture oldpic,Picture dealpic)
{
return TRUE;
}*/
//============= 申请空间函数=========//
unsigned char **fspace_2d(int row,int col )
{
unsigned char **pic;
pic=new unsigned char *[row];
for (int i=0;i<row;i++)
{
pic[i]=new unsigned char [col];
ZeroMemory(pic[i],col);
}
return pic;
}
//=============释放空间函数==========//
void dspace_2d(unsigned char **data,int row,int col)
{
for (int i=0;i<row;i++)
delete [] data[i];
delete [] data;
data=NULL;
}
unsigned char **InputImageWithName(CString FileName,int &row ,int &col )
{
unsigned char **output=NULL;
FILE *fp;
unsigned char *str;
int begin;//,col1;
char first,second;
int colorbytes;
str=new unsigned char[40];
FileName.MakeLower();
if ((fp=fopen(FileName,"rb"))==NULL)
{
return NULL;
}
else
{
fread(str,1,40,fp); fclose(fp); first=str[0];
second=str[1];
//=========================读取BMP格式文件===========
if (first=='B'&&second=='M')
{
col=str[18]+str[19]*256;
row=str[22]+str[23]*256;
begin=str[10]+str[11]*256+str[12]*32768;
colorbytes=str[28]+str[29]*16;
delete []str;
/*
if (colorbytes!=8)
{
AfxMessageBox("不是灰度图象!");
return NULL;
}*/
if ((fp=fopen(FileName,"rb"))==NULL)
{
return NULL;
}
if (colorbytes==8)
{
fseek(fp,begin,SEEK_SET);
unsigned char *lpSrcdata;
int nwidth=((col+3)/4)*4;
lpSrcdata=new BYTE[row*nwidth];
fread(lpSrcdata,sizeof(unsigned char),row*nwidth,fp);
fclose(fp);
output = fspace_2d(row,col);
if (!output)
return NULL;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
{
output[i][j]=lpSrcdata[(row-1-i)*nwidth+j];
}
delete []lpSrcdata;
return(output);
}
else if (colorbytes==24)
{
fseek(fp,begin,SEEK_SET);
unsigned char *lpSrcdata;
int nwidth=col*3;
nwidth=((nwidth+3)/4)*4;
int temp;
lpSrcdata=new BYTE[row*nwidth];
fread(lpSrcdata,sizeof(unsigned char),row*nwidth,fp);
fclose(fp);
output = fspace_2d(row,col);
if (!output)
return NULL;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
{
temp=0;
for (int k=0;k<3;k++)
{
temp+=lpSrcdata[(row-1-i)*nwidth+j*3+k];
}
output[i][j]=(int)((float)temp/(float)3);
}
delete lpSrcdata;
return(output);
}
else
{
AfxMessageBox("不是灰度图象或真彩!");
return NULL;
}
}
//====================读取PIC格式图片=============
else if (first=='I'&&second=='M')
{
col=str[4]+str[5]*256;
row=str[6]+str[7]*256;
output = (unsigned char **)fspace_2d(row,col);
if ((fp=fopen(FileName,"rb"))==NULL)
{
return NULL;
}
else
{
fseek(fp,64,SEEK_SET);
for (int RowNo=0;RowNo<row;RowNo++)
fread(output[RowNo],sizeof(unsigned char),col,fp);
fclose(fp);
return(output);
}
}
}
//========================================
//=====================读取TIF格式图片==================
if(FileName.Find(_T(".tif"))!=-1)
{
char *filename;
filename = new char [FileName.GetLength()];
strcpy(filename,FileName);
output=InputTifImgWithName(&row,&col,filename);
delete []filename;
return (output);
}
if(FileName.Find(_T(".raw"))!=-1)
{
CRawDlg dlg;
if (dlg.DoModal()==IDOK)
{
row=dlg.m_row;
col=dlg.m_col;
if ((fp=fopen(FileName,"rb"))==NULL)
{
return NULL;
}
else
{
fseek(fp,0,SEEK_END);
int wholesize;
wholesize=ftell(fp);
if(wholesize==-1L)
return NULL;
else
{
if(wholesize!=dlg.m_col*dlg.m_row)
{
int state=MessageBox(NULL,"原始图象大小和给出的大小不符,确定打开?","警告",MB_OKCANCEL);
if (state==IDCANCEL)
return NULL;
}
}
output = fspace_2d(dlg.m_row,dlg.m_col);
fseek(fp,0,SEEK_SET);
for (int RowNo=0;RowNo<dlg.m_row;RowNo++)
fread(output[RowNo],sizeof(unsigned char),dlg.m_col,fp);
fclose(fp);
return(output);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -