Search code examples
cfloating-pointresizepixelbmp

Can a pixel be divided into smaller pixels? Or is it possible to have 1.54 pixels instead of 1 or 2?


I am doing an exercise where I have to resize an image "f" times. "f" is a float, so I have to consider 1.45, 3.54, and so on. I don't want you to solve the problem, but I have some doubts about it.

A pixel is 24bits in a BMP file, right? Because it is RGB, so it has 1 byte for red, 1 byte for green and 1 byte for blue. So how am I supposed to divide a pixel? If I have 2.67 for example, then 0.67 how would that work? Dividing a pixel means dividing an 3 bytes, but there is a limit how I can divide them, also, RGB would dissapear, because if I divided by half, then I would only have 12bits, not enough to store RGB.

Also when I am copying pixel by pixel, is it possible to copy instead of pixel by pixel, to copy 0.01 pixel each time? meaning that if it takes me 1 step to copy 1 pixel (1 pixel at a time), then if I copy 0.01 pixel each time, means it would take me 100 times the time it took me to copy a whole pixel. It sounds completely weird for me, because copying 0.01 pixel at a time means copying 0.01 byte at a time, and that may screw the image up if I am resizing (I think).

I have tried with integers, but for example, a for loop will not work in the floating point, because of all the possibilities.


Solution

  • I don't think you're being asked to split an individual pixel. It sounds like you're being asked to add or remove pixels when an image is resized. For example, suppose you have an image that is 12 x 12 pixels and you are given a factor of 1.3 to expand by. This gives you a new image size of 15.6 x 15.6, which rounds to 16 x 16.

    Then you need to perform a mapping of pixels in the original image to pixels in the resized image. A simple way to do this is to take the x and y coordinates of the larger image and multiply them (or divide them) by the scaling factor to get the corresponding coordinates in the smaller image, then copy the whole pixel from the old to the new image. Given the above example, pixel (13,14) in the larger image corresponds to x = 13/1.3 = 10 and y = 14 / 1.3 = ~10.76 (rounds to 11), so copy pixel (10,11) in the old image to (13,14) in the new image.