Problem1: BMPmain.c, BMPfns.c, BMPfns.h, Makefile The file \'BMPmain.c\' contain
ID: 3793636 • Letter: P
Question
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 .
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. BMPmain.c:
//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;
}
Explanation / Answer
ans of problem1:
//BMPfns.h
#ifndef BITMAP__
#define BITMAP__
#define BIT (8*sizeof(byte))
#define BITMAP_NOTFOUND -1
typedef enum{false=0, true} bool;
typedef unsigned char byte;
bool bitmapGet (byte *, int);
void bitmapSet (byte *, int);
void bitmapReset (byte *, int);
int bitmapSearch(byte *, bool, int, int);
#endif
//BMPfns.c
#include "BMPfns.h"
static bool get (byte, byte);
static void set (byte *, byte);
static void reset(byte *, byte);
bool bitmapGet(byte *bitmap, int pos) {
return get(bitmap[pos/BIT], pos%BIT);
}
void bitmapSet(byte *bitmap, int pos) {
set(&bitmap[pos/BIT], pos%BIT);
}
void bitmapReset(byte *bitmap, int pos) {
reset(&bitmap[pos/BIT], pos%BIT);
}
int bitmapSearch(byte *bitmap, bool n, int size, int start) {
int i;
for(i = start+1, size *= BIT; i < size; i++)
if(bitmapGet(bitmap,i) == n)
return i;
return BITMAP_NOTFOUND;
}
static bool get(byte a, byte pos) {
return (a >> pos) & 1;
}
static void set(byte *a, byte pos) {
*a |= 1 << pos;
}
static void reset(byte *a, byte pos) {
*a &= ~(1 << pos);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.