JD2022-TU1/main/extern/Camcam/Tools/ImageTools.cpp

418 lines
No EOL
9.1 KiB
C++

#include "SCENE.h"
#define TEST_ZONE 50
#define PERCENT_TO_CUT 30
#define BLUR_RADIUS 4
#define BLUR_DERIVE 1
#define ECART_CONTRAST 3
#define GET_R(color) ((int)((color & 0xFF0000) >> 16))
#define GET_G(color) ((int)((color & 0x00FF00) >> 8))
#define GET_B(color) ((int)(color & 0x0000FF))
#define GET_C(color, pos) ( (pos == 0) ? ((int)((color & 0xFF0000) >> 16)) : ((pos == 1) ? ((int)((color & 0x00FF00) >> 8)) : ((int)(color & 0x0000FF))))
/***************************************/
/****** IMAGE CONVERSION FUNCTION ******/
/***************************************/
int CENTER_MAP(MAP *srcMap)
{
long long mid;
int counterXY;
int result;
s32 *pBASE = srcMap->GetBase();
mid = 0;
for (counterXY=0; counterXY < srcMap->SY*srcMap->PITCH; counterXY++)
{
mid += pBASE[counterXY];
}
mid /= srcMap->SY*srcMap->PITCH;
for (int counterXY=0; counterXY < srcMap->SY*srcMap->PITCH; counterXY++)
{
pBASE[counterXY] = (pBASE[counterXY] - mid);
}
return mid;
}
void NORMALIZE_MAP(MAP *srcMap, BOOL advance = true);
void NORMALIZE_MAP(MAP *srcMap, BOOL advance)
{
long long mid;
double ecart;
double ecartDouble;
int counterXY;
int result;
s32 *pBASE = srcMap->GetBase();
mid = 0;
for (counterXY=0; counterXY < srcMap->SY*srcMap->PITCH; counterXY++)
{
mid += pBASE[counterXY];
}
mid /= srcMap->SY*srcMap->PITCH;
if (advance)
{
ecart = 0.0;
for (int counterXY=0; counterXY < srcMap->SY*srcMap->PITCH; counterXY++)
{
ecart += (pBASE[counterXY] - mid)*(pBASE[counterXY] - mid);
}
ecartDouble = (double)ecart/(srcMap->SY*srcMap->PITCH);
ecart = (int)sqrt(ecartDouble);
if (ecart == 0)
return;
for (int counterXY=0; counterXY < srcMap->SY*srcMap->PITCH; counterXY++)
{
result = 512 + (pBASE[counterXY] - mid)*512/(ECART_CONTRAST*ecart);
if (result >= 1024) result = 1023;
if (result < 0) result = 0;
pBASE[counterXY] = result;
}
} else
{
for (int counterXY=0; counterXY < srcMap->SY*srcMap->PITCH; counterXY++)
{
result = 512 + (pBASE[counterXY] - mid);
if (result >= 1024) result = 1023;
if (result < 0) result = 0;
pBASE[counterXY] = result;
}
}
}
void NORMALIZE_ZONE(MAP *srcMap, int X, int Y, int SX, int SY, BOOL advance)
{
long long mid;
double ecart;
double ecartDouble;
int counterX, counterY;
int result;
s32 *pBASE = srcMap->GetBase();
mid = 0;
for (counterY=Y; counterY < Y+SY; counterY++)
{
for (counterX=X; counterX < X+SX; counterX++)
{
mid += pBASE[counterX + counterY*srcMap->PITCH];
}
}
mid /= SX*SY;
if (advance)
{
ecart = 0.0;
for (counterY=Y; counterY < Y+SY; counterY++)
{
for (counterX=X; counterX < X+SX; counterX++)
{
ecart += (pBASE[counterX + counterY*srcMap->PITCH] - mid)*(pBASE[counterX + counterY*srcMap->PITCH] - mid);
}
}
ecartDouble = (double)ecart/(SX*SY);
ecart = (int)sqrt(ecartDouble);
if (ecart == 0)
return;
for (counterY=Y; counterY < Y+SY; counterY++)
{
for (counterX=X; counterX < X+SX; counterX++)
{
result = 512 + (pBASE[counterX + counterY*srcMap->PITCH] - mid)*512/(ECART_CONTRAST*ecart);
if (result >= 1024) result = 1023;
if (result < 0) result = 0;
pBASE[counterX + counterY*srcMap->PITCH] = result;
}
}
} else
{
for (counterY=Y; counterY < Y+SY; counterY++)
{
for (counterX=X; counterX < X+SX; counterX++)
{
result = 512 + (pBASE[counterX + counterY*srcMap->PITCH] - mid);
if (result >= 1024) result = 1023;
if (result < 0) result = 0;
pBASE[counterX + counterY*srcMap->PITCH] = result;
}
}
}
}
void
HIGH_FREQUENCIES(MAP *srcMap, MAP *bufferMap, MAP *bufferMap2)
{
int result;
s32 *pSrcBASE = srcMap->GetBase();
s32 *pBufBASE = bufferMap->GetBase();
LF_GaussianFilter(srcMap, bufferMap, bufferMap2, BLUR_RADIUS, BLUR_DERIVE);
for (int counterXY=0; counterXY < srcMap->SY*srcMap->PITCH; counterXY++)
{
result = 512 + pSrcBASE[counterXY] - pBufBASE[counterXY];
if (result > 1023) result = 1023;
if (result < 0) result = 0;
pSrcBASE[counterXY] = result;
}
}
void
INVERT_BW(MAP *srcMap, MAP *dstMap)
{
s32 *pSrcBASE = srcMap->GetBase();
s32 *pDstBASE = dstMap->GetBase();
for (int counterXY=0; counterXY < srcMap->SY*srcMap->PITCH; counterXY++)
{
pDstBASE[counterXY] = 1023 - pSrcBASE[counterXY];
}
}
void COPY_II_IN(MAP *MapSRC,MAP *MapDST)
{
s32 *LAST,*DST,*SRC;
DST = MapDST->GetBase();
SRC = MapSRC->GetBase();
LAST = DST + MapDST->SX;
while (DST < LAST)
{
*(DST++) = 0;
*(SRC++) &= 0xfffffffe;
}
SRC+=MapSRC->PITCH-MapSRC->SX;
DST+=MapDST->PITCH-MapDST->SX;
for (s32 SizeY = 1; SizeY < MapSRC->SY ;SizeY ++)
{
LAST = DST + MapDST->SX ;
*(DST++) = 0;
*(SRC++) &= 0xfffffffe;
while (DST < LAST)
{
*(DST) =*(SRC);
*(DST) += *(DST-MapDST->PITCH);
*(DST) += *(DST-1) - *(DST-1-MapDST->PITCH);
*(DST++);
*(SRC++) &= 0xfffffffe;
}
SRC+=MapSRC->PITCH-MapSRC->SX;
DST+=MapDST->PITCH-MapDST->SX;
}
}
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
void MoveRect(RECT *rect, int x, int y)
{
rect->left += x;
rect->right += x;
rect->top += y;
rect->bottom += y;
}
u32 ABS_DIFF(MAP *patch, MAP *base, POINT offset)
{
s32 *LAST,*pBASE,*PATCH;
u32 result = 0;
int localResult;
pBASE = base->GetBase() + offset.x + offset.y*base->PITCH;
PATCH= patch->GetBase();
for (s32 SizeY = 0; SizeY < patch->SY ;SizeY ++)
{
LAST = PATCH + patch->SX;
while (PATCH < LAST)
{
localResult = *(PATCH++) - *(pBASE++);
if (localResult < 0) localResult = -localResult;
result += localResult;
}
PATCH += patch->PITCH-patch->SX;
pBASE += base->PITCH -patch->SX;
}
return result;
}
POINT TRACK_PATCH(MAP *patch, MAP *source, POINT origin, SIZE searchWin)
{
u32 curDiff;
u32 minDiff;
POINT offset;
POINT offsetEnd;
POINT newOrigin = origin;
minDiff = -1;
offsetEnd.x = origin.x + searchWin.cx/2;
if (offsetEnd.x > source->SX) offsetEnd.x = source->SX;
offsetEnd.y = origin.y + searchWin.cy/2;
if (offsetEnd.y > source->SY) offsetEnd.y = source->SY;
for (offset.y=origin.y-searchWin.cy/2; offset.y < offsetEnd.y; offset.y++)
{
if (offset.y < 0) continue;
for (offset.x=origin.x-searchWin.cx/2; offset.x < offsetEnd.x; offset.x++)
{
if (offset.x < 0) continue;
curDiff = ABS_DIFF(patch, source, offset);
if (minDiff > curDiff)
{
minDiff = curDiff;
newOrigin = offset;
}
}
}
return newOrigin;
}
void COPY_SUB(MAP *dst, MAP *src, POINT offset)
{
s32 *LAST,*SRC,*DST;
SRC = src->GetBase() + offset.x + offset.y*src->PITCH;
DST = dst->GetBase();
for (s32 SizeY = 0; SizeY < dst->SY ;SizeY ++)
{
memcpy(DST, SRC, dst->SX*sizeof(int));
SRC += src->PITCH;
DST += dst->PITCH;
}
}
/*******************************************************************************/
/*******************************************************************************/
/******************************** ADA BOOST ***********************************/
/*******************************************************************************/
/*******************************************************************************/
//// version 1 patch binaire
typedef struct Patch_
{
char x;
char y;
char width;
char height;
char isWhite;
char isOpositeLeft;
} Patch;
inline int GetPatchResult(MAP *srcII, Patch *patch)
{
int z1, z2;
s32 *pBASE = srcII->GetBase();
z1 =pBASE[patch->x + patch->width + (patch->y + patch->height )*srcII->PITCH] -
pBASE[patch->x + (patch->y + patch->height )*srcII->PITCH] -
pBASE[patch->x + patch->width + (patch->y )*srcII->PITCH] +
pBASE[patch->x + (patch->y )*srcII->PITCH];
if (patch->isOpositeLeft)
{
z2 =pBASE[patch->x + 2*patch->width + (patch->y + patch->height )*srcII->PITCH] -
pBASE[patch->x + patch->width + (patch->y + patch->height )*srcII->PITCH] -
pBASE[patch->x + 2*patch->width + (patch->y )*srcII->PITCH] +
pBASE[patch->x + patch->width + (patch->y )*srcII->PITCH];
} else
{
z2 =pBASE[patch->x + patch->width + (patch->y + 2*patch->height )*srcII->PITCH] -
pBASE[patch->x + + (patch->y + 2*patch->height )*srcII->PITCH] -
pBASE[patch->x + patch->width + (patch->y + patch->height )*srcII->PITCH] +
pBASE[patch->x + + (patch->y + patch->height )*srcII->PITCH];
}
if (patch->isWhite)
return z1 < z2;
else
return z1 > z2;
}
float getPatchGlobalResultErrors(Patch *patch, float *diFactor, MAP **positivIIList, int nbPositiv, MAP **negativIIList, int nbNegativ)
{
int counter;
int errorCount;
double result = 0;
for (counter = 0; counter < nbPositiv; counter++)
{
if (!GetPatchResult(positivIIList[counter], patch))
result += diFactor[counter];
}
for (counter = 0; counter < nbNegativ; counter++)
{
if (GetPatchResult(negativIIList[counter], patch))
result += diFactor[counter+nbPositiv];
}
return (float)result;
}
void SearchBestPatch(float *diFactor, MAP **positivIIList, int nbPositiv, MAP **negativIIList, int nbNegativ)
{
int maxWidth;
int maxHeight;
}