Files
Work/mdiwin/CONVEX.CPP
2024-08-07 09:16:27 -04:00

91 lines
2.2 KiB
C++

#include <math.h>
#include <mdiwin/main.hpp>
#include <mdiwin/convex.hpp>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
Convex::Convex()
: mIsInThread(TRUE)
{
}
Convex::~Convex()
{
}
HGLOBAL Convex::performConvex(WORD &width,WORD &height,UHUGE *lpImage)
{
HGLOBAL hGlobalSource;
HGLOBAL hGlobalImage;
UHUGE *ptrSource;
UHUGE *ptrImage;
POINT tempPoint;
double scaleFactor;
double tempFactor;
mWidth=width;
mHeight=height;
mxHalf=mWidth/2;
myHalf=mHeight/2;
if(!lpImage)return 0;
hGlobalSource=Main::upsideDown(width,height,lpImage);
ptrSource=(UHUGE*)::GlobalLock(hGlobalSource);
hGlobalImage=::GlobalAlloc(GMEM_FIXED,(LONG)width*(LONG)height);
ptrImage=(UHUGE *)::GlobalLock(hGlobalImage);
Main::hmemset(ptrImage,0,(LONG)width*(LONG)height);
for(int y=0;y<mHeight;y++)
{
for(int x=0;x<mWidth;x++)
{
setPoint(x,y,tempPoint);
cartesianPoint(tempPoint);
tempFactor=::sqrt(((LONG)tempPoint.x*(LONG)tempPoint.x)+((LONG)tempPoint.y*(LONG)tempPoint.y));
tempFactor*=(M_PI/180.00);
if(tempFactor)scaleFactor=(::tan(::atan(tempFactor)/2.00))/tempFactor;
else scaleFactor=0.00;
scaleFactor*=2;
tempFactor=scaleFactor*(double)tempPoint.x;
tempFactor+=(tempFactor<0?-.5:.5);
tempPoint.x=tempFactor;
tempFactor=scaleFactor*(double)tempPoint.y;
tempFactor+=(tempFactor<0?-.5:.5);
tempPoint.y=tempFactor;
imagePoint(tempPoint);
if(tempPoint.x>=width)continue;
if(tempPoint.y>=height)continue;
*(ptrImage+((LONG)tempPoint.y*(LONG)width)+(LONG)tempPoint.x)=
*(ptrSource+((LONG)y*(LONG)width)+(LONG)x);
if(!yieldTask())return emergencyCleanup(hGlobalSource,hGlobalImage);
}
}
::GlobalUnlock(hGlobalSource);
::GlobalFree(hGlobalSource);
hGlobalSource=Main::upsideDown(width,height,ptrImage);
::GlobalUnlock(hGlobalImage);
::GlobalFree(hGlobalImage);
return hGlobalSource;
}
WORD Convex::yieldTask(void)const
{
MSG msg;
while(::PeekMessage((MSG far *)&msg,0,0,0,PM_REMOVE))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return mIsInThread;
}
HGLOBAL Convex::emergencyCleanup(HGLOBAL hGlobalSource,HGLOBAL hGlobal)const
{
::GlobalUnlock(hGlobalSource);
::GlobalFree(hGlobalSource);
::GlobalUnlock(hGlobal);
::GlobalFree(hGlobal);
return FALSE;
}