2007-02-26 11:45:05 +01:00
|
|
|
#include "image.h"
|
|
|
|
|
|
|
|
namespace Image
|
|
|
|
{
|
|
|
|
void threshold(SDL_Surface *image, int lowerBound, int upperBound)
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
for (int i = 0; i < image->w * image->h; ++i)
|
|
|
|
{
|
|
|
|
int gray = GRAY(((uint32_t*)image->pixels)[i]);
|
|
|
|
if (gray > lowerBound && gray < upperBound)
|
|
|
|
((uint32_t*)image->pixels)[i] = 0xffffffff;
|
|
|
|
else
|
|
|
|
((uint32_t*)image->pixels)[i] = 0xff000000;
|
2007-03-01 14:18:20 +01:00
|
|
|
//gray | (gray << 8) | (gray << 16);
|
2007-02-26 11:45:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2007-03-01 14:18:20 +01:00
|
|
|
void dilation(SDL_Surface *dest, SDL_Surface *src, Mask::mask *mask)
|
|
|
|
{
|
|
|
|
for (int y = mask->size / 2; y < dest->h - mask->size / 2; ++y)
|
|
|
|
{
|
|
|
|
for (int x = mask->size / 2; x < dest->w - mask->size / 2; ++x)
|
|
|
|
{
|
|
|
|
bool whiteFound = false;
|
|
|
|
for (int i = 0; i < mask->size && !whiteFound; ++i)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < mask->size && !whiteFound; ++j)
|
|
|
|
{
|
|
|
|
if (mask->mask[i*mask->size + j] &&
|
|
|
|
PXR(PX(src, x - mask->size / 2 + i, y - mask->size / 2 + j))
|
|
|
|
)
|
|
|
|
{
|
|
|
|
whiteFound = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (whiteFound)
|
|
|
|
((uint32_t*)dest->pixels)[y*dest->w+x] = 0xffffffff;
|
|
|
|
else
|
|
|
|
((uint32_t*)dest->pixels)[y*dest->w+x] = 0xff000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void erosion(SDL_Surface *dest, SDL_Surface *src, Mask::mask *mask)
|
|
|
|
{
|
|
|
|
for (int y = mask->size / 2; y < dest->h - mask->size / 2; ++y)
|
|
|
|
{
|
|
|
|
for (int x = mask->size / 2; x < dest->w - mask->size / 2; ++x)
|
|
|
|
{
|
|
|
|
bool blackFound = false;
|
|
|
|
for (int i = 0; i < mask->size && !blackFound; ++i)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < mask->size && !blackFound; ++j)
|
|
|
|
{
|
|
|
|
if (mask->mask[i*mask->size + j] &&
|
|
|
|
!PXR(PX(src, x - mask->size / 2 + i, y - mask->size / 2 + j))
|
|
|
|
)
|
|
|
|
{
|
|
|
|
blackFound = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (blackFound)
|
|
|
|
((uint32_t*)dest->pixels)[y*dest->w+x] = 0xff000000;
|
|
|
|
else
|
|
|
|
((uint32_t*)dest->pixels)[y*dest->w+x] = 0xffffffff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-03-01 14:00:51 +01:00
|
|
|
|
|
|
|
SDL_Surface *copySurface(SDL_Surface *s)
|
|
|
|
{
|
|
|
|
SDL_Surface *copy = SDL_CreateRGBSurface(SDL_SWSURFACE, s->w, s->h,
|
2007-03-01 14:18:20 +01:00
|
|
|
s->format->BitsPerPixel,
|
|
|
|
s->format->Rmask, s->format->Gmask,
|
|
|
|
s->format->Bmask, s->format->Amask);
|
2007-03-01 14:00:51 +01:00
|
|
|
SDL_BlitSurface(s, NULL, copy, NULL);
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-26 11:45:05 +01:00
|
|
|
}
|