int length = width*height;
rgbPixels = new unsigned char[length*3];
This tells you that the rgbPixels array has size = width * height * 3 (RGB values)
your greyimage has only the size = width * height * 1 (greyscale value)
That’s why you get to see this weird scaling effect, the RGB values are spread across the greyscale array.
for example this is how you would manually create a greyscale array from the rgbPixels you get: (but I’m sure there’s a faster cleaner way to do this)
unsigned char greyPixels[w * h];
for(int i = 0; i < w*h; i++){
greyPixels[i] = rgbPixels[3*i]; // using the red value, use rgbPixels[3*i+1] for green, etc..
}
greyImage.setFromPixels(greyPixels, w, h);