1

I have a simple double for to convert rgb image to grayscale but I have some odd behavior on windows using VS compiler. I noticed that two of my implementation have different results. The first one :

int ok = 0, nok = 0;
for(int y = 0; y < img.height(); y++){
    uchar *line = img.getLine(y);
    for(int x = 0; x < img.width(); x++){
        *(destinationPtr++) = ((*(line++)) + (*(line++)) + (*(line++)))/3;
    }
}

And the second one :

int ok = 0, nok = 0;
for(int y = 0; y < img.height(); y++){
    uchar *line = img.getLine(y);
    for(int x = 0; x < img.width(); x++){
        uchar r = *(line++);
        uchar g = *(line++);
        uchar b = *(line++);
        *(destinationPtr++) = (r+g+b)/3;
    }
}

After a few tests I think the problem in my first implementation, the code is executed with this order :

*(destinationPtr) = ((*(line)) + (*(line)) + (*(line)))/3;
destinationPtr++;
line++;
line++;
line++;

And on my Linux compilation with GCC I don't have this issue, do you have the reason of this ? the post-increment is not a standard thing ?

Bitmap
  • 12,402
  • 16
  • 64
  • 91
  • 1
    I would highly suggest when you do your next Linux compilation of enabling `-Pedandic -Werror -Wall -Wextra` you have some major undefined behavior here – Mgetz Feb 19 '14 at 13:43

0 Answers0