my task is to scale bmp picture with the given factor:
stream: base64 bmp image code:
Qk1CAAAAAAAAADYAAAAoAAAABAAAAAEAAAABABgAAAAAAAwAAAAjLgAAIy4AAAAAAAAAAAAA/////wAAAP8AAAAA
scale.c
struct bmp_image* scale(const struct bmp_image* image, float factor){
if(image == NULL || factor <= 0){
return NULL;
}
.........
int width = bmp->header->width;
int height = bmp->header->height;
int widthNew = (int)round((float)width * factor);
int heightNew = (int)round((float)height * factor);
for (uint32_t y = 0; y < heightNew; y++){
for(uint32_t z = 0; z< widthNew; z++){
bmp->data[(y*widthNew)+z].red=image->data[(int)(floor((float)y/(float)factor)*width+floor((float)z/(float)factor))].red;
bmp->data[(y*widthNew)+z].green=image->data[(int)(floor((float)y/(float)factor)*width+floor((float)z/(float)factor))].green;
bmp->data[(y*widthNew)+z].blue=image->data[(int)(floor((float)y/(float)factor)*width+floor((float)z/(float)factor))].blue;
}
}
return bmp;
}
main.c
FILE *output_p8 = fopen("scale.bmp", "w");
struct bmp_image* newimage8 = NULL;
newimage8 = scale(image, 1.025656);
free_bmp_image(image);
write_bmp(output_p8,newimage8);
free_bmp_image(newimage8);
fclose(output_p8);
the data output what I got after that:
ffffff ffffff ff0000 00ff00
the data what I should have:
ffffff ff0000 00ff00 000000
any suggest?
When you calculate the new dimensions, you perform a floating-point calculation and then round to the nearest integer:
int widthNew = round(width * factor);
(You don't need the casts: factor
is a float
and the result of a multiplication of an integer with a float
is also a float
; widthNew
is an integer, so the float
result must be converted to an integer.)
Because you round, your effective scaling factor (let's call it factor_eff
) may differ from the nominal factor
:
float factor_eff = (float) widthNew / width;
In your case:
factor == 1.025656;
width == 4;
widthNew == round(1.025656 * 4) == round(4.102624) == 4;
factor_eff = 4.0 / 4 == 1.0;
But you do your index calculations with the old factor:
int zNew = floor(z / factor);
(Again, you don't need the cast.) For your four z indces, this will yield:
z == 0; zNew == floor(0 / 1.025656) == floor(0.0) == 0; // ok
z == 1; zNew == floor(1 / 1.025656) ~~ floor(0.9749858) == 0; // ! should be 1
z == 2; zNew == floor(2 / 1.025656) ~~ floor(1.9499715) == 1; // ! should be 2
z == 3; zNew == floor(3 / 1.025656) ~~ floor(2.9249573) == 2; // ! should be 3
You could fix the floating-point factor, but I think it is better (and faster) to calculate your new indices with integer arithmetic:
int zNew = z * width / widthNew;
int yNew = y * height / heightNew;
Make sure you do the multiplication first. This method should be safe to use for dimensions up to abour 46,000.