Question
Modify this code so it generates a chessboard image consisting of 16x16 black and white squares. #include #include #include #define BYTES_PER_PIXEL 3 #define BITS_PER_PIXEL (BYTES_PER_PIXEL*8)
Modify this code so it generates a chessboard image consisting of 16x16 black and white squares.
#include
int main (void) { FILE *outputFile; outputFile = fopen(BMP_FILE, "wb");
if ( outputFile == NULL ){ fprintf(stderr,"Could not open file "); return EXIT_FAILURE; }
writeHeader(outputFile); generateData(outputFile); fclose(outputFile); return EXIT_SUCCESS; }
// Change this function to create different images! void generateData(FILE * f){ unsigned char blue = 0; unsigned char green = 0; unsigned char red = 0; int row = 0; int col = 0; while (row
// The colour of each pixel is determined by its // blue, green and red values. // So each pixel needs 3 bytes of data written to the file void writePixel(unsigned char b, unsigned char g, unsigned char r, FILE * f){ fwrite (&b, sizeof (unsigned char), 1, f); fwrite (&g, sizeof (unsigned char), 1, f); fwrite (&r, sizeof (unsigned char), 1, f); }
void writeHeader (FILE *file) { unsigned short magicNumber = MAGIC_NUMBER; fwrite (&magicNumber, sizeof magicNumber, 1, file); unsigned int fileSize = OFFSET + (SIZE * SIZE * BYTES_PER_PIXEL); fwrite (&fileSize, sizeof fileSize, 1, file); unsigned int reserved = 0; fwrite (&reserved, sizeof reserved, 1, file); unsigned int offset = OFFSET; fwrite (&offset, sizeof offset, 1, file); unsigned int dibHeaderSize = DIB_HEADER_SIZE; fwrite (&dibHeaderSize, sizeof dibHeaderSize, 1, file); unsigned int width = SIZE; fwrite (&width, sizeof width, 1, file); unsigned int height = SIZE; fwrite (&height, sizeof height, 1, file); unsigned short planes = NUMBER_PLANES; fwrite (&planes, sizeof planes, 1, file); unsigned short bitsPerPixel = BITS_PER_PIXEL; fwrite (&bitsPerPixel, sizeof bitsPerPixel, 1, file); unsigned int compression = NO_COMPRESSION; fwrite (&compression, sizeof compression, 1, file); unsigned int imageSize = (SIZE * SIZE * BYTES_PER_PIXEL); fwrite (&imageSize, sizeof imageSize, 1, file); unsigned int hResolution = PIX_PER_METRE; fwrite (&hResolution, sizeof hResolution, 1, file); unsigned int vResolution = PIX_PER_METRE; fwrite (&vResolution, sizeof vResolution, 1, file); unsigned int numColors = NUM_COLORS; fwrite (&numColors, sizeof numColors, 1, file); unsigned int importantColors = NUM_COLORS; fwrite (&importantColors, sizeof importantColors, 1, file); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started