1

I'm trying to implement the floyd algorithm but it seems it doesn't works.

I used the pseudocode algorithm from wikipedia without floyd http://img15.hostingpics.net/pics/298623Capturedcran20140226165024.png

with floyd http://img15.hostingpics.net/pics/968250Capturedcran20140226165035.png

This is my code :

template<>
Image<ubyte>* Image<ubyte>::floydSteinberg() const
{
    Image<ubyte>* tmp = new Image<ubyte>(this->width, this->height);

    for (int i=0; i < width*height; i++)
            tmp->array[i]= this->array[i];


    for (int y = 0; y< this->height; y++){

        for (int x = 1; x<this->width; x++){

            ubyte oldpixel = tmp->pixel(x, y);
            ubyte newpixel  = (oldpixel > 128) ? 255 : 0;
            tmp->pixel(x,y) = newpixel;
            ubyte propagationErreur  = oldpixel - newpixel;

            tmp->pixel(x+1,y) =tmp->pixel(x+1,y) + 7.0/16 * propagationErreur;
            tmp->pixel(x-1,y+1) = tmp->pixel(x-1,y+1)  + 3.0/16 * propagationErreur ;
            tmp->pixel(x,y+1) = tmp->pixel(x,y+1)  + 5.0/16 * propagationErreur ;
            tmp->pixel(x+1,y+1) = tmp->pixel(x+1,y+1)  + 1.0/16 * propagationErreur ;

        }
    }


    return tmp;
}
Pépito
  • 57
  • 4
  • 11

1 Answers1

3
ubyte newpixel  = (oldpixel > 128) ? 0 : 255;

must be

ubyte newpixel  = (oldpixel > 128) ? 255 : 0;

Another possible issue: I suggest that propagationErreur should be signed type

MBo
  • 77,366
  • 5
  • 53
  • 86
  • Yes I already dit it. I wanted to do a test and forgot to update it. It is the result : http://zupimages.net/up/14/09/cz98.png – Pépito Feb 26 '14 at 16:25
  • @Pépito It seems that picture cz98.png - negative image, before correction to 255:0, isn't it? And see my new edit – MBo Feb 26 '14 at 16:52
  • Thanks. It seems the signed type help. So it look like http://zupimages.net/up/14/09/sct8.png – Pépito Feb 26 '14 at 18:48