| File Formats Review Part 2 |
| The following review questions were covered in Labs 02 and 03 and are questions taken from the course website. NOT ALL questions from the course website were discussed in lab. Please note that the information presented here does not necessarily represent a complete answer to the review question. The information is intended to spark student's memory for those who attended lab OR provide a base (hint) from where to start for those students who did not attend lab.
| Question 1: Given the following C structure for the header of a BMP graphics file: struct bmpHeader { // Start of File Header byte magic1, magic2; //must be 'BM' long filesize; //bytes in file short res1, res2; //reserved = 0 long pixelOffset; //file position where data is // Start of Bitmap Header long bmpsize; //size of bit map info header long cols, rows; //image size short planes; //planes = 1 short bitsperpixel; //1, 4, 8, 24 long compression; //RGB, RLE4, RLE8 long cmpsize; //size of compressed image = 0 for RGB long xscale, yscale; // EG pixels per meter long colors; //#colours used long impcolors; //# of important colours } bh; Some important facts about BMP images: 1. Images in BMP files are stored 'upside down', meaning that the first row of pixel data is acutally the LAST row of the image. Column 1 is still first in each row. 2. Each row is padded to a 4-byte boundary (i.e. the pixel data for each row begins on a 4-byte bounday - word boundary - on UNIX). Assuming we have a BMP image that is a 24-bit, RGB (uncompressed), write the code necessary to read the image into memory. Declare whatever structures you require. You may assume the header has already been read in bh. You may also assume the existence of a function called getbyte() that reads and returns the next single 'raw' byte read from the file. |
|  | //open file //data structures char image[bh.rows][bh.cols][3]; //3 for RGB values char rowpadding = 4-((bh.cols *3)%4); //get to the right location in the file for( int i=0; i < bh.pixelOffset; i++ ) getbyte(); //get image information //read rows for( int j = bh.rows-1; j>=0; j-- ) { //read cols for( int k = 0; k < bh.cols; k++ ) { //read Blue value image[j][k][0] = getbyte(); //read Green value image[j][k][1] = getbyte(); //read Red value image[j][k][2] = getbyte(); } //read row padding for( int l = 0; l < rowpadding; l++ ) getbyte(); } //close file |

BACK

© Copyright 2002
Questions? Please Email: gwen@cpsc.ucalgary.ca
Last modified October 21, 2002