📄 bmpspliter.cpp
字号:
// BmpSpliter.cpp: implementation of the CBmpSpliter class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "bmp.h"
#include "BmpSpliter.h"
extern "C" {
BOOL JpgToBmp(LPCSTR lpJpgFileNameForIn, LPCSTR lpBmpFileNameForOut,int iColorBit=24);
BOOL BmpToJpg(LPCSTR lpBmpFileNameForIn, LPCSTR lpJpgFileNameForOut,BOOL bColor, int nQuality);
BOOL DIBToJpg(HDIB hDIB, LPCSTR lpJpgFileNameForOut,BOOL bColor, int nQuality);
}
#pragma comment(lib, "jpgvsbmp.lib")
extern double xx, xy, yx, yy, xk, yk, cx, cy;
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const double EPS = 1e-8;
const double PI = acos(-1.0);
const int LINEREQUATIONSIZE = 5;
typedef double T;
typedef T Matrix[LINEREQUATIONSIZE][LINEREQUATIONSIZE];
typedef T Vector[LINEREQUATIONSIZE];
T GaussJordan(Matrix a, Vector b, int n) {
int i, j, k, id;
T tmp, ans = 1.0;
for(i = 0; i < n; i++) {
id = i;
for(j = i + 1; j < n; j++) {
if(fabs(a[j][i]) > fabs(a[id][i])) {
id = j;
}
}
if(fabs(a[id][i]) < EPS) return 0.0;
ans *= a[id][i];
if(id != i) {
swap(b[i], b[id]);
for(j = i; j < n; j++) {
swap(a[i][j], a[id][j]);
}
}
tmp = 1.0 / a[i][i];
for(j = i; j < n; j++) a[i][j] *= tmp;
b[i] *= tmp;
for(j = 0; j < n; j++) {
if(j == i) continue;
tmp = a[j][i];
for(k = i; k < n; k++) {
a[j][k] -= tmp * a[i][k];
}
b[j] -= tmp * b[i];
}
}
return ans;
}
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CBmpSpliter::CBmpSpliter()
{
hDIB = NULL;
hDIBm = NULL;
}
CBmpSpliter::~CBmpSpliter()
{
if(hDIB)
{
GlobalUnlock((HGLOBAL)hDIB);
::GlobalFree((HGLOBAL)hDIB);
}
if(hDIBm)
{
GlobalUnlock((HGLOBAL)hDIBm);
::GlobalFree((HGLOBAL)hDIBm);
}
}
int CBmpSpliter::GetWidth(HDIB hDIB)
{
int res = 0;
LPSTR p;
if(hDIB == NULL)
return 0;
p = (LPSTR)GlobalLock(hDIB);
if(p == NULL)
return 0;
res = DIBWidth(p);
GlobalUnlock(hDIB);
return res;
}
int CBmpSpliter::GetWidth()
{
return GetWidth(hDIB);
}
int CBmpSpliter::GetHeight(HDIB hDIB)
{
int res = 0;
LPSTR p;
if(hDIB == NULL)
return 0;
p = (LPSTR)GlobalLock(hDIB);
if(p == NULL)
return 0;
res = DIBHeight(p);
GlobalUnlock(hDIB);
return res;
}
int CBmpSpliter::GetHeight()
{
return GetHeight(hDIB);
}
BOOL CBmpSpliter::Load(CFile &file)
{
CString filename = file.GetFilePath();
filename.MakeLower();
if(filename.Find(".jpg") != -1) {
CString ofile = filename;
ofile.Replace(".jpg", ".bmp");
JpgToBmp(filename, ofile);
CFile cf;
cf.Open(ofile, CFile.modeRead);
hDIB = ReadDIBFile(cf);
cf.Close();
} else hDIB = ReadDIBFile(file);
CFile filem;
CString filenamem = filename;
filenamem.Replace(file.GetFileName(), "mask.bmp");
filem.Open(filenamem, CFile.modeRead);
hDIBm = ReadDIBFile(filem);
filem.Close();
FILE *fp = fopen(filename + ".coordinates", "r");
if(!fp)
{
AfxMessageBox("can't open file " + filename + ".coordinates");
return FALSE;
}
double cx1, cy1, cx2, cy2, cx3, cy3, j1, j2, j3, w1, w2, w3;
if(12 != fscanf(fp, "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",
&cx1, &cy1, &j1, &w1,
&cx2, &cy2, &j2, &w2,
&cx3, &cy3, &j3, &w3))
{
AfxMessageBox(filename + ".coordinates " + "不是预期的格式");
return FALSE;
}
Matrix m;
Vector v;
m[0][0] = j1; m[0][1] = w1; m[0][2] = 1;
m[1][0] = j2; m[1][1] = w2; m[1][2] = 1;
m[2][0] = j3; m[2][1] = w3; m[2][2] = 1;
v[0] = cx1; v[1] = cx2; v[2] = cx3;
CString str;
str.Format("%lf", GaussJordan(m, v, 3));
// AfxMessageBox(str);
xx = v[0]; xy = v[1]; xk = v[2];
// str.Format("%lf %lf %lf",
// j1 * xx + w1 * xy + xk - cx1,
// j2 * xx + w2 * xy + xk - cx2,
// j3 * xx + w3 * xy + xk - cx3);
// AfxMessageBox(str);
m[0][0] = j1; m[0][1] = w1; m[0][2] = 1;
m[1][0] = j2; m[1][1] = w2; m[1][2] = 1;
m[2][0] = j3; m[2][1] = w3; m[2][2] = 1;
v[0] = cy1; v[1] = cy2; v[2] = cy3;
str.Format("%lf", GaussJordan(m, v, 3));
// AfxMessageBox(str);
yx = v[0]; yy = v[1]; yk = v[2];
// str.Format("%lf %lf %lf",
// j1 * yx + w1 * yy + yk - cy1,
// j2 * yx + w2 * yy + yk - cy2,
// j3 * yx + w3 * yy + yk - cy3);
// AfxMessageBox(str);
// str.Format("%lf %lf %lf %lf %lf %lf", xx, xy, xk, yx, yy, yk);
// AfxMessageBox(str);
return hDIB != NULL && hDIBm != NULL;
}
BOOL CBmpSpliter::Save(CFile &file)
{
return SaveDIB(hDIB, file);
}
BOOL CBmpSpliter::Load(CString filename)
{
CFile file;
int res;
if(!file.Open(filename, CFile::modeRead))
return FALSE;
res = Load(file);
file.Close();
return res;
}
BOOL CBmpSpliter::Save(CString filename)
{
CFile file;
int res;
if(!file.Open(filename, CFile::modeCreate | CFile::modeWrite))
return FALSE;
res = SaveDIB(hDIB, file);
file.Close();
return res;
}
BOOL CBmpSpliter::Save(CString filename, CRect rect)
{
LPBITMAPINFOHEADER pH;
HDIB hOld;
LPSTR pOld, pNew;
int dwX, dwY, W, H, Wold, Hold, dwSize, res, i, j, w, wOld;
if(rect.IsRectNull() || rect.IsRectEmpty())
return FALSE;
dwX = rect.left;
dwY = rect.top;
W = RECTWIDTH(&rect);
H = RECTHEIGHT(&rect);
hOld = hDIB;
pOld = (LPSTR)GlobalLock(hOld);
pH = (LPBITMAPINFOHEADER)pOld;
wOld = WIDTHBYTES(pH->biWidth * pH->biBitCount);
Wold = pH->biWidth;
Hold = pH->biHeight;
dwSize = WIDTHBYTES(W * pH->biBitCount) * H;
hDIB = (HDIB)::GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, dwSize
+ sizeof(BITMAPINFOHEADER) + PaletteSize(pOld));
pNew = (LPSTR)GlobalLock(hDIB);
memcpy(pNew, pOld, sizeof(BITMAPINFOHEADER) + PaletteSize(pOld));
pH = (LPBITMAPINFOHEADER)pNew;
pH->biWidth = W;
pH->biHeight = H;
if(pH->biSizeImage != 0)
pH->biSizeImage = dwSize + sizeof(BITMAPINFOHEADER) + PaletteSize(pOld);
w = WIDTHBYTES(W * pH->biBitCount);
pOld = FindDIBBits(pOld);
pNew = FindDIBBits(pNew);
switch(pH->biBitCount)
{
case 1:
break;
case 4:
break;
case 8:
break;
case 24:
for(i = 0; i < H; i++)
for(j = 0; j < w; j++)
if(i + dwY >= 0 && i + dwY < Hold && j + dwX * 3 >= 0 && j + dwX * 3 < wOld)
pNew[i * w + j] = pOld[(i + dwY) * wOld + j + dwX * 3];
else
pNew[i * w + j] = (char)255;
break;
case 32:
break;
}
GlobalUnlock(hOld);
GlobalUnlock(hDIB);
res = Save(filename);
hDIB = hOld;
return res;
}
BOOL CBmpSpliter::Draw(CDC* pDC, int stX, int stY, int w, int h)
{
HDC hDC = pDC->GetSafeHdc();
if(!hDC || !hDIB) return FALSE;
RECT DCRect, DIBRect;
DIBRect.left = stX;
DIBRect.top = stY;
DIBRect.right = stX + w;
DIBRect.bottom = stY + h;
DCRect.left = 0;
DCRect.right = w;
DCRect.top = 0;
DCRect.bottom = h;
return PaintDIB(hDC, &DCRect, hDIB, &DIBRect, NULL);
}
BOOL CBmpSpliter::Drawm(CDC* pDC, int mX, int mY)
{
HDC hDC = pDC->GetSafeHdc();
if(!hDC || !hDIBm) return FALSE;
RECT DIBRect, DCRect;
int w = GetWidth(hDIBm);
int h = GetHeight(hDIBm);
DCRect.left = mX - 5;
DCRect.right = mX + 5;
DCRect.top = mY - 5;
DCRect.bottom = mY + 5;
DIBRect.left = 0;
DIBRect.top = 0;
DIBRect.right = w;
DIBRect.bottom = h;
return PaintDIB(hDC, &DCRect, hDIBm, &DIBRect, NULL);
}
BOOL CBmpSpliter::Drawr(CDC* pDC, int mX, int mY, int rX, int rY)
{
HDC hDC = pDC->GetSafeHdc();
if(!hDC || !hDIB) return FALSE;
RECT DIBRect, DCRect;
int w = GetWidth(hDIB);
int h = GetHeight(hDIB);
DCRect.left = mX - 5;
DCRect.right = mX + 5;
DCRect.top = mY - 5;
DCRect.bottom = mY + 5;
DIBRect.left = rX - 5;
DIBRect.right = rX + 5;
DIBRect.top = rY - 5;
DIBRect.bottom = rY + 5;
return PaintDIB(hDC, &DCRect, hDIB, &DIBRect, NULL);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -