image prcoessing
in
Programming Questions
•
2 years ago
Hello,
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
- float p3C = red(img.pixels[p3]);
- float p4C = red(img.pixels[p4]);
- if ((p1C + (e * (7.0 / 16.0))) <= 0)
- {
- img.pixels[p1] = color (0);
- }else if ((p1C + (e * (7.0 / 16.0))) >= 255) {
- img.pixels[p1] = color (255);
- }
- if ((p2C + (e * (3.0 / 16.0))) <= 0)
- {
- img.pixels[p2] = color (0);
- }else if ((p2C + (e * (3.0 / 16.0))) >= 255) {
- img.pixels[p2] = color (255);
- }
- if ((p3C + (e * (5.0 / 16.0))) <= 0)
- {
- img.pixels[p3] = color (0);
- }else if ((p3C + (e * (5.0 / 16.0))) >= 255) {
- img.pixels[p3] = color (255);
- }
- if ((p4C + (e * (1.0 / 16.0))) <= 0)
- {
- img.pixels[p4] = color (0);
- }else if ((p4C + (e * (1.0 / 16.0))) >= 255) {
- img.pixels[p4] = color (255);
- }
- }
- }
- img.updatePixels();
- image(img, x, y);
- }
1