Write me a C program that emits a .bmp that's a depiction of an italian flag to stdout.
Write me a C program that emits a .bmp that's a depiction of an italian flag to stdout.
To create a C program that generates an Italian flag BMP image and outputs it to stdout, follow this step-by-step solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #include <stdio.h> #include <stdlib.h> int main() { int width = 90; int height = 60; // Calculate row size with padding int bytes_per_row = width * 3; // 3 bytes per pixel (BGR) int padding = (4 - (bytes_per_row % 4)) % 4; int row_size = bytes_per_row + padding; // Total image size and file size int image_size = row_size * height; int file_size = 54 + image_size; // 54 bytes for headers // BMP file header (14 bytes) unsigned char file_header[14] = { 'B', 'M', // File type (unsigned char)(file_size), (unsigned char)(file_size >> 8), (unsigned char)(file_size >> 16), (unsigned char)(file_size >> 24), 0, 0, 0, 0, // Reserved 54, 0, 0, 0 // Pixel data offset }; // BMP info header (40 bytes) unsigned char info_header[40] = { 40, 0, 0, 0, // Info header size (unsigned char)(width), (unsigned char)(width >> 8), (unsigned char)(width >> 16), (unsigned char)(width >> 24), (unsigned char)(height), (unsigned char)(height >> 8), (unsigned char)(height >> 16), (unsigned char)(height >> 24), 1, 0, // Planes 24, 0, // Bits per pixel (24) 0, 0, 0, 0, // Compression (none) (unsigned char)(image_size), (unsigned char)(image_size >> 8), (unsigned char)(image_size >> 16), (unsigned char)(image_size >> 24), 0, 0, 0, 0, // Horizontal resolution 0, 0, 0, 0, // Vertical resolution 0, 0, 0, 0, // Colors in palette 0, 0, 0, 0 // Important colors }; // Write headers fwrite(file_header, 1, 14, stdout); fwrite(info_header, 1, 40, stdout); // Generate pixel data unsigned char pixel[3]; unsigned char pad_bytes[3] = {0, 0, 0}; for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { if (x < 30) { // Green stripe (BGR: 00 FF 00) pixel[0] = 0x00; pixel[1] = 0xFF; pixel[2] = 0x00; } else if (x < 60) { // White stripe (BGR: FF FF FF) pixel[0] = 0xFF; pixel[1] = 0xFF; pixel[2] = 0xFF; } else { // Red stripe (BGR: 00 00 FF) pixel[0] = 0x00; pixel[1] = 0x00; pixel[2] = 0xFF; } fwrite(pixel, 1, 3, stdout); } // Add row padding fwrite(pad_bytes, 1, padding, stdout); } return 0; } |
1 2 | gcc italian_flag.c -o italian_flag ./italian_flag > flag.bmp |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | #include <stdio.h> #include <stdlib.h> int main() { int width = 90; int height = 60; int bytes_per_row = width * 3; int padding = (4 - (bytes_per_row % 4)) % 4; int row_size = bytes_per_row + padding; int image_size = row_size * height; int file_size = 54 + image_size; unsigned char file_header[14] = { 'B', 'M', (unsigned char)(file_size), (unsigned char)(file_size >> 8), (unsigned char)(file_size >> 16), (unsigned char)(file_size >> 24), 0, 0, 0, 0, 54, 0, 0, 0 }; unsigned char info_header[40] = { 40, 0, 0, 0, (unsigned char)(width), (unsigned char)(width >> 8), (unsigned char)(width >> 16), (unsigned char)(width >> 24), (unsigned char)(height), (unsigned char)(height >> 8), (unsigned char)(height >> 16), (unsigned char)(height >> 24), 1, 0, 24, 0, 0, 0, 0, 0, (unsigned char)(image_size), (unsigned char)(image_size >> 8), (unsigned char)(image_size >> 16), (unsigned char)(image_size >> 24), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; fwrite(file_header, 1, 14, stdout); fwrite(info_header, 1, 40, stdout); unsigned char pixel[3]; unsigned char pad_bytes[3] = {0, 0, 0}; for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { if (x < 30) { pixel[0] = 0x00; pixel[1] = 0xFF; pixel[2] = 0x00; } else if (x < 60) { pixel[0] = 0xFF; pixel[1] = 0xFF; pixel[2] = 0xFF; } else { pixel[0] = 0x00; pixel[1] = 0x00; pixel[2] = 0xFF; } fwrite(pixel, 1, 3, stdout); } fwrite(pad_bytes, 1, padding, stdout); } return 0; } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | #include <stdio.h> #include <stdlib.h> int main() { int width = 90; int height = 60; int bytes_per_row = width * 3; int padding = (4 - (bytes_per_row % 4)) % 4; int row_size = bytes_per_row + padding; int image_size = row_size * height; int file_size = 54 + image_size; unsigned char file_header[14] = { 'B', 'M', (unsigned char)(file_size), (unsigned char)(file_size >> 8), (unsigned char)(file_size >> 16), (unsigned char)(file_size >> 24), 0, 0, 0, 0, 54, 0, 0, 0 }; unsigned char info_header[40] = { 40, 0, 0, 0, (unsigned char)(width), (unsigned char)(width >> 8), (unsigned char)(width >> 16), (unsigned char)(width >> 24), (unsigned char)(height), (unsigned char)(height >> 8), (unsigned char)(height >> 16), (unsigned char)(height >> 24), 1, 0, 24, 0, 0, 0, 0, 0, (unsigned char)(image_size), (unsigned char)(image_size >> 8), (unsigned char)(image_size >> 16), (unsigned char)(image_size >> 24), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; fwrite(file_header, 1, 14, stdout); fwrite(info_header, 1, 40, stdout); unsigned char pixel[3]; unsigned char pad_bytes[3] = {0, 0, 0}; for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { if (x < 30) { pixel[0] = 0x00; pixel[1] = 0xFF; pixel[2] = 0x00; } else if (x < 60) { pixel[0] = 0xFF; pixel[1] = 0xFF; pixel[2] = 0xFF; } else { pixel[0] = 0x00; pixel[1] = 0x00; pixel[2] = 0xFF; } fwrite(pixel, 1, 3, stdout); } fwrite(pad_bytes, 1, padding, stdout); } return 0; } |

What flag is shown in this image?
This is the flag of Italy.
Testing if the previous output contains the string Italian
: False
Testing if the previous output contains the string Italy
: True