I have a problem with some fwrite that fail in way I do not understand.
In this piece of code both fwrite fail and give tmp = 0.
if ((file = fopen(filenameout, "wb")) != NULL)
{
bmpheader->filesz += bmpdib->bmp_bytesz;
bmpdib->bmp_bytesz *= 2;
bmpdib->height *= 2;
tmp = fwrite(bmpheader, sizeof(bmp_header_t), 1, file);
tmp = fwrite(bmpdib, sizeof(bmp_dib_t), 1, file);
tmp = fwrite(bmpdata1, bmpdib->bmp_bytesz, 1, file);
tmp = fwrite(bmpdata2, bmpdib->bmp_bytesz, 1, file);
fclose(file);
}
else
return 1;
But if I comment one single line of code everything goes fine:
if ((file = fopen(filenameout, "wb")) != NULL)
{
bmpheader->filesz += bmpdib->bmp_bytesz;
// bmpdib->bmp_bytesz *= 2;
bmpdib->height *= 2;
tmp = fwrite(bmpheader, sizeof(bmp_header_t), 1, file);
tmp = fwrite(bmpdib, sizeof(bmp_dib_t), 1, file);
tmp = fwrite(bmpdata1, bmpdib->bmp_bytesz, 1, file);
tmp = fwrite(bmpdata2, bmpdib->bmp_bytesz, 1, file);
fclose(file);
}
else
return 1;
I double checked every detail in my code:
- bmpdata1 and bmpdata2 contain valid bmp bitmap data, they produce both a correct image if used alone.
- bmpheader and bmpdib are data structures read from a valid bmp file and they are untouched until my code.
- all files have been opened in binary mode "rb" for input and "wb" for output.
- bmpdata1 and bmpdata2 are copies of the same image, so their byte size is identical.
The goal of my code is to write an image plus a modified copy of it to obtain a final image twice higher than the original one. The original bitmap is a 570 x 363 x 24bit bmp image.
What am I doing wrong?
Any help is very appreciated, thanks.
This solved the problem. Thanks to Art.
I erroneously used the updated bitmap data size to write each part of the final image. The resulting image has double the height because it is composed bay two stacked images which are written each in a separate 'fwrite' operation with half of the resulting bitmap data size each.