Writing BMP image in pure c/c++ without other libraries Writing BMP image in pure c/c++ without other libraries c c

Writing BMP image in pure c/c++ without other libraries


See if this works for you...In this code, I had 3 2-dimensional arrays, called red,green and blue. Each one was of size [width][height], and each element corresponded to a pixel - I hope this makes sense!

FILE *f;unsigned char *img = NULL;int filesize = 54 + 3*w*h;  //w is your image width, h is image height, both intimg = (unsigned char *)malloc(3*w*h);memset(img,0,3*w*h);for(int i=0; i<w; i++){    for(int j=0; j<h; j++)    {        x=i; y=(h-1)-j;        r = red[i][j]*255;        g = green[i][j]*255;        b = blue[i][j]*255;        if (r > 255) r=255;        if (g > 255) g=255;        if (b > 255) b=255;        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);    }}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};unsigned char bmppad[3] = {0,0,0};bmpfileheader[ 2] = (unsigned char)(filesize    );bmpfileheader[ 3] = (unsigned char)(filesize>> 8);bmpfileheader[ 4] = (unsigned char)(filesize>>16);bmpfileheader[ 5] = (unsigned char)(filesize>>24);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);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);}free(img);fclose(f);


Without the use of any other library you can look at the BMP file format. I've implemented it in the past and it can be done without too much work.

Bitmap-File Structures

Each bitmap file contains a bitmap-file header, a bitmap-information header, a color table, and an array of bytes that defines the bitmap bits. The file has the following form:

BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;
RGBQUAD aColors[];
BYTE aBitmapBits[];

... see the file format for more details


Clean C Code for Bitmap (BMP) Image Generation

Generated Image:

bitmap image


The code does not use any library other than stdio.h. So, the code can be easily incorporated in other languages of C-Family, like- C++, C#, Java.


#include <stdio.h>const int BYTES_PER_PIXEL = 3; /// red, green, & blueconst int FILE_HEADER_SIZE = 14;const int INFO_HEADER_SIZE = 40;void generateBitmapImage(unsigned char* image, int height, int width, char* imageFileName);unsigned char* createBitmapFileHeader(int height, int stride);unsigned char* createBitmapInfoHeader(int height, int width);int main (){    int height = 361;    int width = 867;    unsigned char image[height][width][BYTES_PER_PIXEL];    char* imageFileName = (char*) "bitmapImage.bmp";    int i, j;    for (i = 0; i < height; i++) {        for (j = 0; j < width; j++) {            image[i][j][2] = (unsigned char) ( i * 255 / height );             ///red            image[i][j][1] = (unsigned char) ( j * 255 / width );              ///green            image[i][j][0] = (unsigned char) ( (i+j) * 255 / (height+width) ); ///blue        }    }    generateBitmapImage((unsigned char*) image, height, width, imageFileName);    printf("Image generated!!");}void generateBitmapImage (unsigned char* image, int height, int width, char* imageFileName){    int widthInBytes = width * BYTES_PER_PIXEL;    unsigned char padding[3] = {0, 0, 0};    int paddingSize = (4 - (widthInBytes) % 4) % 4;    int stride = (widthInBytes) + paddingSize;    FILE* imageFile = fopen(imageFileName, "wb");    unsigned char* fileHeader = createBitmapFileHeader(height, stride);    fwrite(fileHeader, 1, FILE_HEADER_SIZE, imageFile);    unsigned char* infoHeader = createBitmapInfoHeader(height, width);    fwrite(infoHeader, 1, INFO_HEADER_SIZE, imageFile);    int i;    for (i = 0; i < height; i++) {        fwrite(image + (i*widthInBytes), BYTES_PER_PIXEL, width, imageFile);        fwrite(padding, 1, paddingSize, imageFile);    }    fclose(imageFile);}unsigned char* createBitmapFileHeader (int height, int stride){    int fileSize = FILE_HEADER_SIZE + INFO_HEADER_SIZE + (stride * height);    static unsigned char fileHeader[] = {        0,0,     /// signature        0,0,0,0, /// image file size in bytes        0,0,0,0, /// reserved        0,0,0,0, /// start of pixel array    };    fileHeader[ 0] = (unsigned char)('B');    fileHeader[ 1] = (unsigned char)('M');    fileHeader[ 2] = (unsigned char)(fileSize      );    fileHeader[ 3] = (unsigned char)(fileSize >>  8);    fileHeader[ 4] = (unsigned char)(fileSize >> 16);    fileHeader[ 5] = (unsigned char)(fileSize >> 24);    fileHeader[10] = (unsigned char)(FILE_HEADER_SIZE + INFO_HEADER_SIZE);    return fileHeader;}unsigned char* createBitmapInfoHeader (int height, int width){    static unsigned char infoHeader[] = {        0,0,0,0, /// header size        0,0,0,0, /// image width        0,0,0,0, /// image height        0,0,     /// number of color planes        0,0,     /// bits per pixel        0,0,0,0, /// compression        0,0,0,0, /// image size        0,0,0,0, /// horizontal resolution        0,0,0,0, /// vertical resolution        0,0,0,0, /// colors in color table        0,0,0,0, /// important color count    };    infoHeader[ 0] = (unsigned char)(INFO_HEADER_SIZE);    infoHeader[ 4] = (unsigned char)(width      );    infoHeader[ 5] = (unsigned char)(width >>  8);    infoHeader[ 6] = (unsigned char)(width >> 16);    infoHeader[ 7] = (unsigned char)(width >> 24);    infoHeader[ 8] = (unsigned char)(height      );    infoHeader[ 9] = (unsigned char)(height >>  8);    infoHeader[10] = (unsigned char)(height >> 16);    infoHeader[11] = (unsigned char)(height >> 24);    infoHeader[12] = (unsigned char)(1);    infoHeader[14] = (unsigned char)(BYTES_PER_PIXEL*8);    return infoHeader;}