Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #include #include #define BYTES_PER_PIXEL 3 #define BITS_PER_PIXEL (BYTES_PER_PIXEL*8) #define NUMBER_PLANES 1 #define PIX_PER_METRE 2835 #define MAGIC_NUMBER 0x4d42 #define NO_COMPRESSION 0 #define OFFSET 54 #define DIB_HEADER_SIZE 40 #define NUM_COLORS 0 #define SIZE 16 #define BMP_FILE "black.bmp" void writeHeader (FILE *file); void generateData(FILE *file); void writePixel(unsigned char b, unsigned char r, unsigned char g, FILE * f);

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); }

image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MFDBS 91 3rd Symposium On Mathematical Fundamentals Of Database And Knowledge Base Systems Rostock Germany May 6 9 1991

Authors: Bernhard Thalheim ,Janos Demetrovics ,Hans-Detlef Gerhardt

1991st Edition

3540540091, 978-3540540090

More Books

Students also viewed these Databases questions