Altera_Forum
Honored Contributor
16 years agonios sw development: invalid bmp file produced
Hi,
I have encountered a problem while writing to a bitmap file. The code works in the cc compiler but fails to work properly in Nios II IDE. The bitmap file can be written, but the file written is invalid and cannot be opened in a picture viewer. The bitmap structure looks like this:typedef struct /**** BMP file header structure ****/ { unsigned short bfType; /* Magic number for file */ unsigned int bfSize; /* Size of file */ unsigned short bfReserved1; /* Reserved */ unsigned short bfReserved2; /* ... */ unsigned int bfOffBits; /* Offset to bitmap data */ } BITMAPFILEHEADER; typedef struct /**** BMP file info structure ****/ { unsigned int biSize; /* Size of info header */ int biWidth; /* Width of image */ int biHeight; /* Height of image */ unsigned short biPlanes; /* Number of color planes */ unsigned short biBitCount; /* Number of bits per pixel */ unsigned int biCompression; /* Type of compression to use */ unsigned int biSizeImage; /* Size of image data */ int biXPelsPerMeter; /* X pixels per meter */ int biYPelsPerMeter; /* Y pixels per meter */ unsigned int biClrUsed; /* Number of colors used */ unsigned int biClrImportant; /* Number of important colors */ } BITMAPINFOHEADER; [/INDENT]And this is how I write to the outfile:if (fwrite(&header, 1, sizeof(BITMAPFILEHEADER), outfile) < 1) { fclose(outfile); fprintf(stderr, "Error writing BMP header for %s\n", FileName); exit(-1); } if (fwrite(&info, 1, sizeof(BITMAPINFO), outfile) < 1) { fclose(outfile); fprintf(stderr, "Error writing BMP info for %s\n", FileName); exit(-1); } I read up somewhere in the net that different compilers have different ways of packing struct causing different results obtained for sizeof(). I think it is the inaccuracy in the sizeof() obtained that screws up the bmp file format. I tried using# pragma pack but it is not supported in Nios II IDE. Wondering if someone have encountered anything like this before.