- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Some update:
I added a code to print out the sizeof() values and these are the results: cc: BITMAPHEADER = 14 BITMAPINFO = 44 Nios II IDE: BITMAPHEADER = 16 BITMAPINFO = 44 Note the difference in the values of sizeof(BITMAPHEADER). Does anyone know of a way around this?- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
One possible way around this problem is to write each structure element to the file one at a time.
Another thing to watch out for is endianess. Off the top of my head, I don't remember what endianess NIOS or BMPs use, but you should double check this. If they are different, you will need to take this into account.- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You are correct that the packing is different. The first short is not taking 2 bytes of space in the struct, it's taking 4. When the data is read back in, there is going to be garbage in every struct member after bfType because every read thereafter is off by 2 bytes.
I was able to use# pragma pack in my version of the Nios II tools. Does this code work for you?# include <stdio.h># pragma pack(2) 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; int main() { printf("Hello from Nios II!\n"); printf("Size: %d\n", sizeof(BITMAPFILEHEADER)); return 0; } -edj- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi edj,
Did you compile this code in Nios II IDE or Nios II SBT for Eclipse? I tried in Nios II IDE, but failed. I am wondering if pragma pack is supported in IDE? -caridee
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page