2 part problem second part below Problem1 BMPmainc BMPfnsc B
2 part problem, second part below.
Problem1: BMPmain.c, BMPfns.c, BMPfns.h, Makefile
The file \'BMPmain.c\' contains a main() function which is already written for you and attached with this homework. When appropriate functions are provided, main() will create a .bmp image file. Your job is to write \'BMPfns.c\' which contains the functions which main() uses to create .bmp image files. You must also write the header file \'BMPfns.h\' to #include in BMPmain.c and which contains prototypes of the functions defined in BMPfns.c . Given code:
//BMPmain.c 150S17 HW1 Problem1: main() of a program which builds a .bmp image
// see Lancaster\'s \"Exploring the .BMP File Format\" for an accessible description of the bmp file format.
#define NROWS 252 //width in pixels of the image to be built
#define NCOLS 252 //height --\"--
#include <stdio.h>
#include <stdlib.h>
#include \"BMPfns.h\"
int main(void)
{
FILE *fp;
int row,col;
if( (fp= fopen(\"myBMP.bmp\",\"wb\")) == NULL ) {printf(\"Couldn\'t open \'myBMP.bmp\'\ \"); exit(0); }
//-- building a bmp header for a NROWS x NCOLS 24-bit/pixel image using Lancaster\'s header info
//BMP marker
fputc((int)\'B\',fp); fputc((int)\'M\',fp);
//write total length of file in bytes= (size of header + 3*number of pixels) + (number of rows * size of padding)
write4LE(54 + NROWS*(3*NCOLS+padding(NCOLS)),fp);
//reserved mystery value
write4LE(0,fp);
//offset to pixel data
write4LE(54,fp);
//size of data header
write4LE(40,fp);
//width of bitmap in pixels
write4LE(NCOLS,fp);
//height of bitmap in pixels
write4LE(NROWS,fp);
//number of color planes
write2LE(1,fp);
//number of bits per pixel
write2LE(24,fp);
//compression mode
write4LE(0,fp);
//size of stored pixel data in bytes
write4LE(NROWS*(3*NCOLS),fp); //required
//width resolution in pixels/meter
write4LE(0,fp); //doesn\'t seem to matter
//height resolution in pixels/meter
write4LE(0,fp); //\"\"
//height resolution in pixels/meter
write4LE(0,fp); //\"\"
//height resolution in pixels/meter
write4LE(0,fp); //\"\"
//--here with header built; ready to fill data
//color plan is to increment red at each row increment, blue at each column increment, and keep green at 0; image starts at bottom left
for(row=0; row < NROWS; row++) {
for(col=0; col < NCOLS; col++) {
writePIXEL(row, 0, col, fp); //writePIXEL(int red, int grn, int blu, FILE *fp)
}
writePAD(padding(NCOLS),fp);
}
//here with (we hope) the bmp file built
fclose(fp);
return 0;
}
Problem2: BMPcheckerboard.c, BMPfns.c, BMPfns.h, Makefile
Write a new main() in BMPcheckerboard.c which will draw a checkerboard of size 296 x 296 pixels using two attractive colors of your own choosing. You should be able to bring BMPfns.c and BMPfns.h from Problem1 over to Problem2 as-is.
Solution
Read, Write, and Query Image Files
Working with Image Formats
In its native form, a graphics file format image is not stored as a MATLAB® matrix, or even necessarily as a matrix. Most graphics files begin with a header containing format-specific information tags, and continue with bitmap data that can be read as a continuous stream. For this reason, you cannot use the standard MATLAB I/O commands load and save to read and write a graphics file format image.
Call special MATLAB functions to read and write image data from graphics file formats:
To read a graphics file format image use imread.
To write a graphics file format image, use imwrite.
To obtain information about the nature of a graphics file format image, use imfinfo.
This table gives a clearer picture of which MATLAB commands should be used with which image types.
Procedure
Functions to Use
Load or save a matrix as a MAT-file.
load
save
Load or save graphics file format image, e.g., BMP, TIFF.
imread
imwrite
Display any image loaded into the MATLAB workspace.
image
imagesc
Utilities
imfinfo
ind2rgb
Reading a Graphics Image
The imread function reads an image from any supported graphics image file in any of the supported bit depths. Most of the images that you read are 8-bit. When these are read into memory, they are stored as class uint8. The main exception to this rule is MATLAB support for 16-bit data for PNG and TIFF images; if you read a 16-bit PNG or TIFF image, it is stored as class uint16.
Note For indexed images, imread always reads the colormap into an array of class double, even though the image array itself can be of class uint8 or uint16.
The following commands read the image ngc6543a.jpg into the workspace variable RGB and then displays the image using the image function:
You can write (save) image data using the imwrite function. The statements
create a BMP file containing the clown image.
Writing a Graphics Image
When you save an image using imwrite, the default behavior is to automatically reduce the bit depth to uint8. Many of the images used in MATLAB are 8-bit, and most graphics file format images do not require double-precision data. One exception to the rule for saving the image data as uint8 is that PNG and TIFF images can be saved as uint16. Because these two formats support 16-bit data, you can override the MATLAB default behavior by specifying uint16 as the data type for imwrite. The following example shows writing a 16-bit PNG file using imwrite.
Subsetting a Graphics Image (Cropping)
Sometimes you want to work with only a portion of an image file or you want to break it up into subsections. Specify the intrinsic coordinates of the rectangular subsection you want to work with and save it to a file from the command line. If you do not know the coordinates of the corner points of the subsection, choose them interactively, as the following example shows:
If you know what the image corner coordinates should be, you can manually define sp in the preceding example rather than using ginput.
You can also display a \"rubber band box\" as you interact with the image to subset it. See the code example for rbbox for details. For further information, see the documentation for the ginput and imagefunctions.
Obtaining Information About Graphics Files
The imfinfo function enables you to obtain information about graphics files in any of the standard formats listed earlier. The information you obtain depends on the type of file, but it always includes at least the following:
Name of the file, including the folder path if the file is not in the current folder
File format
Version number of the file format
File modification date
File size in bytes
Image width in pixels
Image height in pixels
Number of bits per pixel
Image type: RGB (truecolor), intensity (grayscale), or indexed
| Procedure | Functions to Use | 
|---|---|
| Load or save a matrix as a MAT-file. | load save | 
| Load or save graphics file format image, e.g., BMP, TIFF. | imread imwrite | 
| Display any image loaded into the MATLAB workspace. | image imagesc | 
| Utilities | imfinfo ind2rgb | 





