1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
// @------------- Pixelation X
bool filter_pixelation(float percentageX, float percentageY, Picture& pic)
{
// http://www.mathsisfun.com/percentage.html
/* 50% of 80 = (50/100) * 80 = 40 */
size_t perX = percentageX * (float)pic.width;
size_t perY = percentageY * (float)pic.height;
if((perX <= 0 || perY <= 0) || (perX > pic.width || perY > pic.height))
return 0;
RGBA avg {0,0,0,0};
// @-- Calculate the colours
// Add up all the pixel values
for(size_t y = 0; y < pic.height; y += perY)
{
for(size_t x = 0; x < pic.width; x += perX)
{
int offset = getIndex(x, y, pic.width);
for(size_t yi = 0; yi < perY; yi++)
{
for(size_t xi = 0; xi < perX; xi++)
{
int i = getIndex(xi, yi, pic.width);
addColour(pic.pixel[i + offset].rgba, avg);
}
}
// Gets the average colour.
divideColourBy(avg, perY * perX);
// Applies the average value to the picture
for(size_t yi = 0; yi < perY; yi++)
{
for(size_t xi = 0; xi < perX; xi++)
{
int i = getIndex(xi, yi, pic.width);
setColour(avg, pic.pixel[i + offset].rgba);
}
}
avg = {0,0,0,0};
}
}
return true;
}
|