📄 dib.cpp
字号:
else
k2 =0;
lOffset = this->PixelOffset(i+1,j+1,wBytesPerLine);
if(*(lpDIBBits+lOffset)==0)
k3=1;
else
k3 =0;
lOffset = this->PixelOffset(i,j-1,wBytesPerLine);
if(*(lpDIBBits+lOffset)==0)
k4=1;
else
k4 =0;
lOffset = this->PixelOffset(i,j+1,wBytesPerLine);
if(*(lpDIBBits+lOffset)==0)
k5=1;
else
k5 =0;
lOffset = this->PixelOffset(i-1,j-1,wBytesPerLine);
if(*(lpDIBBits+lOffset)==0)
k6=1;
else
k6 =0;
lOffset = this->PixelOffset(i-1,j,wBytesPerLine);
if(*(lpDIBBits+lOffset)==0)
k7=1;
else
k7 =0;
lOffset = this->PixelOffset(i-1,j+1,wBytesPerLine);
if(*(lpDIBBits+lOffset)==0)
k8=1;
else
k8 =0;
int judge;
judge = k1+k2*2+k3*4+k4*8+k5*16+k6*32+k7*64+k8*128;
if(erasetable[judge]==1)
{
lOffset = this->PixelOffset(i,j,wBytesPerLine);
*(lpDIBBits+lOffset++)=0;
*(lpDIBBits+lOffset++)=0;
*(lpDIBBits+lOffset++)=0;
j++;
end =0;
}
}
}
}
for(int j=1;j<width-1;j++)
for(int i=1;i<height-1;i++)
{
lOffset = this->PixelOffset(i,j,wBytesPerLine);
if(*(lpDIBBits+lOffset)==0)
continue;
else
{
int colorleft,colorright;
lOffset =this->PixelOffset(i,j-1,wBytesPerLine);
colorleft = *(lpDIBBits+lOffset);
lOffset = this->PixelOffset(i,j+1,wBytesPerLine);
colorright = *(lpDIBBits+lOffset);
if((colorleft==255)&&(colorright==255))
continue;
else
{
int k1,k2,k3,k4,k5,k6,k7,k8;
lOffset = this->PixelOffset(i+1,j-1,wBytesPerLine);
if(*(lpDIBBits+lOffset)==0)
k1=1;
else
k1 =0;
lOffset = this->PixelOffset(i+1,j,wBytesPerLine);
if(*(lpDIBBits+lOffset)==0)
k2=1;
else
k2 =0;
lOffset = this->PixelOffset(i+1,j+1,wBytesPerLine);
if(*(lpDIBBits+lOffset)==0)
k3=1;
else
k3 =0;
lOffset = this->PixelOffset(i,j-1,wBytesPerLine);
if(*(lpDIBBits+lOffset)==0)
k4=1;
else
k4 =0;
lOffset = this->PixelOffset(i,j+1,wBytesPerLine);
if(*(lpDIBBits+lOffset)==0)
k5=1;
else
k5 =0;
lOffset = this->PixelOffset(i-1,j-1,wBytesPerLine);
if(*(lpDIBBits+lOffset)==0)
k6=1;
else
k6 =0;
lOffset = this->PixelOffset(i-1,j,wBytesPerLine);
if(*(lpDIBBits+lOffset)==0)
k7=1;
else
k7 =0;
lOffset = this->PixelOffset(i-1,j+1,wBytesPerLine);
if(*(lpDIBBits+lOffset)==0)
k8=1;
else
k8 =0;
int judge;
judge = k1+k2*2+k3*4+k4*8+k5*16+k6*32+k7*64+k8*128;
if(erasetable[judge]==1)
{
lOffset = this->PixelOffset(i,j,wBytesPerLine);
*(lpDIBBits+lOffset++)=0;
*(lpDIBBits+lOffset++)=0;
*(lpDIBBits+lOffset++)=0;
j++;
end =0;
}
}
}
}
}
}
#define LINENUM 200
HANDLE DIB:: AdjestDIB(HANDLE hDib)
{
int iheight,iwidth;
LPBYTE lpData;
LPBITMAPINFOHEADER lpbi;
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
long line[LINENUM ];
long linesum[201];
int lineno=0 ;
int iwidthcenter;
float decrate;
int BytesPerLine;
for(int k =0;k<LINENUM ;k++)
line[k] = 0;
for(k = 0;k<201;k++)
linesum[k] = 0;
// int step;
iheight = lpbi->biHeight;
iwidth = lpbi->biWidth;
lpData = this->FindDIBBits(hDib);
iwidthcenter = iwidth/2;
BytesPerLine = WIDTHBYTES(iwidth*24);
if(!hDib)
return NULL ;
int centerline[2000];
float xf;
int x;
int y ;
long Offset ;
int gray;
for(int j =-100;j<=100;j+=1)
{
if(j !=0)
decrate = iheight/(float)j;
for(int k =0;k<LINENUM ;k++)
line[k] = 0;
for( y = 0;y<iheight;y++)
{
if(j!=0)
xf = y/decrate;
else
xf = 0;
if(j>0){
x = (int) xf;
if((xf - (float)x)>=0.5) //分两中情况
x++;
}
if(j<0)
{
x = (int)xf;
if(float(x)-xf>0.5)
x--;
}
if(j==0)
x =0;
Offset = this->PixelOffset(y,x,BytesPerLine);
gray = *(lpData+Offset);
line[0] +=gray;
centerline[y] = x+iwidthcenter;//记录基准直线的横坐标
}
for(int lines =1;lines<LINENUM ;lines++)//查看100条相同斜率的直线的灰度
{
for(y =0;y<iheight;y++)
{
x = centerline[y]+lines;
Offset = this->PixelOffset(y,x,BytesPerLine);
gray = *(lpData+Offset);
line[lines]+=gray;
}
}
for( k =0;k<LINENUM ;k++)
for(int kk =0;kk<LINENUM -k;kk++)//直线按照灰度值排序
{
if(line[kk]>line[kk+1])
{
long temp = line[kk];
line[kk] = line[kk+1];
line[kk+1] = temp;
}
}
for(k = 0;k<=19;k++)//得到一定斜率的直线的前10条的灰度和
linesum[lineno] += line[k];
lineno++;
}
long tempnum = linesum[0];
int num = 0 ;
for(int number = 0;number <lineno-1;number++){
if(tempnum >linesum[number])
{
tempnum = linesum[number];
num = number;
}
}
decrate = (-100+num)/(float)iheight;//得到所求斜率
if(decrate!=0)
{
if(decrate<0)
{
//decrate = decrate*0.9;
double anotherdecrate;
decrate = (float)(fabs(1.0/decrate));
float *pointjudge = new (float[iheight]);
for(int y =0;y<iheight;y++)
{
float delta = (iheight-y) /decrate;
//int x = (int )delta;
//if(delta-(float)x >=0.5)
// x++;
anotherdecrate = float(y)/(iwidth-delta);
// anotherdecrate = -anotherdecrate;
decrate = -decrate;
if(decrate*anotherdecrate ==-1.0)
pointjudge[y] = 90.0;
else
pointjudge[y] =(float)(fabs(atan((decrate-anotherdecrate)/(decrate*anotherdecrate+1.0))*180.0/3.1415926) );
}
decrate= (float)(fabs(decrate));
int numpoint=0;
float mindel =90.0;
for(y =0;y<iheight;y++)
if(mindel>fabs(pointjudge[y]-90.0))
{
numpoint = y;
mindel = (float)(fabs(pointjudge[y]-90.0));
}
delete [] pointjudge;
//计算新图象的信息
//decrate = - 1.0/anotherdecrate;
//numpoint = iheight - numpoint;
int newhight,newwidth;
newhight =(int)( (iheight-numpoint)/sqrt(decrate*decrate/(decrate*decrate+1)));
newwidth = (int )(numpoint /sqrt(1.0/(decrate*decrate+1)));
newhight -=2;
newwidth -=2;//由上面的公式计算时候会有误差 故减去2
HDIB hNewDIB;
hNewDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, (DWORD)((newwidth*24+31)/32*4*newhight + sizeof(BITMAPINFOHEADER)));
if(!hNewDIB)
return NULL;
LPBYTE lpDest;
LPBITMAPINFOHEADER lpb;
lpb = (LPBITMAPINFOHEADER)GlobalLock(hNewDIB);
lpDest = (LPBYTE)lpb;
memcpy((void*)lpDest,(void *)lpbi,sizeof(BITMAPINFOHEADER));
lpb->biWidth = newwidth;
lpb->biHeight = newhight;
lpb->biSizeImage = (newwidth*24+31)/32*4*newhight;
lpDest = lpDest+sizeof(BITMAPINFOHEADER);
int newBytesPerLine = WIDTHBYTES(newwidth*24);
//计算基准线得xy坐标
int *baselinex = new(int [newwidth]);
int *baseliney = new(int [newwidth]);
float coef1 = (float)(sqrt(decrate*decrate/(decrate*decrate+1.0)));
float coef2 =(float)(1.0/ sqrt(decrate*decrate+1.0));
for(int kk=0;kk<newwidth;kk++)
{
float temp = (float)kk * coef1;
int tem = (int )temp;
if(temp-(float)tem>=0.5)
baselinex[kk] = iwidth -tem-1-1;
else
baselinex[kk] = iwidth-tem-1;
temp = (float)kk*coef2;
tem = (int)temp;
if(temp-(float)tem>=0.5)
baseliney[kk] = numpoint-tem-1;
else
baseliney[kk] = numpoint-tem;
}
//开始填充新的矩形
for(int i =0;i<newhight;i++)
for(int j =0;j<newwidth;j++)
{
float tempx = i*coef2;
int temp = (int)tempx;
if(tempx-(float)temp>=0.5)
temp++;
int x = baselinex[newwidth-j]-temp;
float tempy = i*coef1;
temp = (int)tempy;
if(tempy-(float)tempy>=0.5)
temp++;
int y = temp+baseliney[newwidth-j];
if(x <0){
x = 0;
// AfxMessageBox("x<0");
}
if(x>=iwidth){
x = iwidth-1;
// AfxMessageBox("x>iwidth");
}
if(y<0){
y =0;
//AfxMessageBox("y<0");
}
if(y>=iheight)
{
y = iheight-1;
//AfxMessageBox("y>iheight");
}
Offset = this->PixelOffset(y,x,BytesPerLine);
int color = *(lpData+Offset);
Offset = this->PixelOffset(i,j,newBytesPerLine);
*(lpDest+Offset++) = color;
*(lpDest+Offset++) = color;
*(lpDest+Offset++) = color;
}
GlobalUnlock(hNewDIB);
GlobalUnlock(hDib);
LPBITMAPINFOHEADER lptest;
lptest = (LPBITMAPINFOHEADER)GlobalLock(hNewDIB);
int wei = lptest->biWidth;
int hei = lptest->biHeight;
return hNewDIB;
}
else
{
decrate = (float)(fabs(1.0/decrate));
float *pointjudge = new (float[iheight]);
for(int y =0;y<iheight;y++)
{
float delta = (iheight-y) /decrate;
int x = (int )delta;
if(delta-(float)x >=0.5)
x++;
double anotherdecrate;
anotherdecrate = float(y)/(iwidth-x);
anotherdecrate = -anotherdecrate;
if(decrate*anotherdecrate ==-1.0)
pointjudge[y] = 90.0;
else
pointjudge[y] = (float)(fabs(atan((decrate-anotherdecrate)/(decrate*anotherdecrate+1.0))*180.0/3.1415926) );
}
int numpoint=0;
float mindel =90.0;
for(y =0;y<iheight;y++)
if(mindel>fabs(pointjudge[y]-90.0))
{
numpoint = y;
mindel = (float)(fabs(pointjudge[y]-90.0));
}
delete [] pointjudge;
//计算新图象的信息
int newhight,newwidth;
newhight =(int)( (iheight-numpoint)/sqrt(decrate*decrate/(decrate*decrate+1)));
newwidth = (int )(numpoint /sqrt(1.0/(decrate*decrate+1)));
newhight -=2;
newwidth -=2;//由上面的公式计算时候会有误差 故减去2
HDIB hNewDIB;
hNewDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, (DWORD)((newwidth*24+31)/32*4*newhight + sizeof(BITMAPINFOHEADER)));
if(!hNewDIB)
return NULL;
LPBYTE lpDest;
LPBITMAPINFOHEADER lpb;
lpb = (LPBITMAPINFOHEADER)GlobalLock(hNewDIB);
lpDest = (LPBYTE)lpb;
memcpy((void*)lpDest,(void *)lpbi,sizeof(BITMAPINFOHEADER));
lpb->biWidth = newwidth;
lpb->biHeight = newhight;
lpb->biSizeImage = (newwidth*24+31)/32*4*newhight;
lpDest = (LPBYTE) lpb;
lpDest = lpDest+sizeof(BITMAPINFOHEADER);
int newBytesPerLine = WIDTHBYTES(newwidth*24);
//计算基准线得xy坐标
int *baselinex = new(int [newwidth]);
int *baseliney = new(int [newwidth]);
float coef1 = (float)(sqrt(decrate*decrate/(decrate*decrate+1.0)));
float coef2 =(float)(1.0/ sqrt(decrate*decrate+1.0));
for(int kk=0;kk<newwidth;kk++)
{
float temp = (float)kk * coef1;
int tem = (int )temp;
if(temp-(float)tem>=0.5)
baselinex[kk] = tem+1;
else
baselinex[kk] = tem;
temp = (float)kk*coef2;
tem = (int)temp;
if(temp-(float)tem>=0.5)
baseliney[kk] = numpoint-tem-1;
else
baseliney[kk] = numpoint-tem;
}
//开始填充新的矩形
for(int i =0;i<newhight;i++)
for(int j =0;j<newwidth;j++)
{
float tempx = i*coef2;
int temp = (int)tempx;
if(tempx-(float)temp>=0.5)
temp++;
int x = temp + baselinex[j];
float tempy = i*coef1;
temp = (int)tempy;
if(tempy-(float)tempy>=0.5)
temp++;
int y = temp+baseliney[j];
if(x <0)
x = 0;
if(x>=iwidth)
x = iwidth-1;
if(y<0)
y =0;
if(y>=iheight)
y = iheight-1;
Offset = this->PixelOffset(y,x,BytesPerLine);
int color = *(lpData+Offset);
Offset = this->PixelOffset(i,j,newBytesPerLine);
*(lpDest+Offset++) = color;
*(lpDest+Offset++) = color;
*(lpDest+Offset++) = color;
}
GlobalUnlock(hNewDIB);
GlobalUnlock(hDib);
return hNewDIB;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -