I wrote a code that is supposed to apply the Floyd–Steinberg dithering on an image, but i keep getting an error, can someone plz check my code, and tell me where im wrong at.
Thanks in advance
the code is here:
PImage img;
void setup ()
{
size (300, 300);
img = loadImage("baboon.gif");
img.resize(255, 255);
}
void draw ()
{
background (255);
toGrayscale();
display(10, 10);
}
void toGrayscale ()
{
img.loadPixels();
for (int i = 0; i < img.pixels.length; i++)
{
float r = 0.30 * red(img.pixels[i]);
float g = 0.59 * green(img.pixels[i]);
float b = 0.11 * blue(img.pixels[i]);
img.pixels[i] = color (r + g + b);
}
img.updatePixels();
}
void display (float x, float y)
{
float e;
img.loadPixels();
for (int i = 0; i < img.height; i++)
{
for (int j = 0; j < img.width; j++)
{
int loc = i + (j * img.width);
float t = red(img.pixels[loc]);
if (t < 127)
{
e = t - 0;
img.pixels[loc] = color (0);
}else{
e = t - 255;
img.pixels[loc] = color (255);
}
int p1 = i + ((j + 1) * img.width);
int p2 = (i + 1) + ((j - 1) * img.width);
int p3 = (i + 1) + (j * img.width);
int p4 = (i + 1) + ((j + 1) * img.width);
float p1C = red(img.pixels[p1]);
float p2C = red(img.pixels[p2]); // array of the pixels is giving error out of bounds