Home > Backend Development > C++ > How to Create a Monochromatic BMP Image from a Boolean Matrix in Pure C/C Without External Libraries?

How to Create a Monochromatic BMP Image from a Boolean Matrix in Pure C/C Without External Libraries?

DDD
Release: 2024-11-29 17:16:10
Original
521 people have browsed it

How to Create a Monochromatic BMP Image from a Boolean Matrix in Pure C/C   Without External Libraries?

Writing BMP Image in Pure C/C Without External Libraries

When developing algorithms that require information output, it becomes necessary to generate outputs in various formats. One common format is a BMP image. This article addresses the problem of creating a monochromatic BMP image from a boolean matrix, where true elements are represented as white pixels.

BMP Header Structure

A BMP (Bitmap Image) file consists of a header section followed by the image data. The header contains vital information about the image's dimensions, color depth, and compression format. Here's a breakdown of the main components:

  • "BM" signature: Identifies the file as a BMP image (2 bytes).
  • File size: Size of the BMP file in bytes (4 bytes).
  • Offset to pixel data: Location of the image data within the file (4 bytes).
  • BITMAPINFOHEADER: Contains additional information about the image, such as width, height, number of color planes, and bit depth (40 bytes).

Code to Generate BMP Image from Boolean Matrix

The following code snippet demonstrates how to create a BMP image from a boolean matrix without relying on external libraries:

FILE *f;
unsigned char *img = NULL;
int filesize = 54 + 3*w*h;  // w and h are image width and height

// Allocate memory for image data
img = (unsigned char *)malloc(3*w*h);
memset(img,0,3*w*h);

// Fill img byte array with pixel data
for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++) {
        int x = i, y = (h-1)-j;
        int r, g, b;  // Color components

        // Set pixel color based on matrix element
        if (matrix[i][j]) {
            r = g = b = 255;  // White pixel
        } else {
            r = g = b = 0;      // Black pixel
        }

        // Write pixel color components to image data array
        img[(x+y*w)*3+2] = (unsigned char)(r);
        img[(x+y*w)*3+1] = (unsigned char)(g);
        img[(x+y*w)*3+0] = (unsigned char)(b);
    }
}

// Set BMP header values
unsigned char bmpfileheader[14] = {'B','M', 0,0,0,0, 0,0, 0,0, 54,0,0,0};
unsigned char bmpinfoheader[40] = {40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 24,0};

// Update file size
bmpfileheader[ 2] = (unsigned char)(filesize    );
bmpfileheader[ 3] = (unsigned char)(filesize>> 8);
bmpfileheader[ 4] = (unsigned char)(filesize>>16);
bmpfileheader[ 5] = (unsigned char)(filesize>>24);

// Update image width and height
bmpinfoheader[ 4] = (unsigned char)(       w    );
bmpinfoheader[ 5] = (unsigned char)(       w>> 8);
bmpinfoheader[ 6] = (unsigned char)(       w>>16);
bmpinfoheader[ 7] = (unsigned char)(       w>>24);
bmpinfoheader[ 8] = (unsigned char)(       h    );
bmpinfoheader[ 9] = (unsigned char)(       h>> 8);
bmpinfoheader[10] = (unsigned char)(       h>>16);
bmpinfoheader[11] = (unsigned char)(       h>>24);

// Save BMP image to file
f = fopen("img.bmp","wb");
fwrite(bmpfileheader, 1, 14, f);
fwrite(bmpinfoheader, 1, 40, f);
for (int i = 0; i < h; i++) {
    fwrite(img+(w*(h-i-1)*3), 3, w, f);
    fwrite(bmppad, 1, (4-(w*3)%4)%4, f);  // Pad to 4-byte boundary
}

// Free resources
free(img);
fclose(f);
Copy after login

By following the steps outlined in this code, you can successfully generate a monochromatic BMP image from a boolean matrix, providing a way to visualize and output your algorithm's results.

The above is the detailed content of How to Create a Monochromatic BMP Image from a Boolean Matrix in Pure C/C Without External Libraries?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template