Introduction to Audio and Video-05-RGB-TO-BMP Using Open Source Library

Keywords: C++ Google

Directory of Audio and Video Introduction Articles

RGB-TO-BMP Review

Convert RGB data into BMP images:

  1. Understanding BMP file format
  2. Preparing BMP header information
  3. Prepare BMP header
  4. BMP stores RG B in B-G-R order
  5. In the biHeight field of BitmapInfoHeader, biHeight is positive, bitmap is scanned from bottom to top, biHeight is negative, and bitmap is scanned from top to bottom.
  6. Four-byte alignment of BMP images

In the actual development process, third-party open source libraries are usually used to simplify development, shield some of the underlying complexity and save a lot of coding time.

How to use open source libraries

libbmp (C library)

#include <stdio.h>
#include "libbmp/libbmp.h"

// Seven Colors of Rainbow
u_int32_t rainbowColors[] = {
        0XFF0000, // red
        0XFFA500, // orange
        0XFFFF00, // yellow
        0X00FF00, // green
        0X007FFF, // young
        0X0000FF, // blue
        0X8B00FF  // purple
};

int main() {
    int width = 711, height = 711;
    bmp_img img;
    bmp_img_init_df (&img, width, height);

    for (int i = 0; i < width; ++i) {
        // Current color
        u_int32_t currentColor = rainbowColors[0];
        if(i < 100) {
            currentColor = rainbowColors[0];
        } else if(i < 200) {
            currentColor = rainbowColors[1];
        } else if(i < 300) {
            currentColor = rainbowColors[2];
        } else if(i < 400) {
            currentColor = rainbowColors[3];
        } else if(i < 500) {
            currentColor = rainbowColors[4];
        } else if(i < 600) {
            currentColor = rainbowColors[5];
        } else if(i < 700) {
            currentColor = rainbowColors[6];
        }
        // Current color R component
        u_int8_t R = (currentColor & 0xFF0000) >> 16;
        // Current color G component
        u_int8_t G = (currentColor & 0x00FF00) >> 8;
        // Current color B component
        u_int8_t B = currentColor & 0x0000FF;
        for (int j = 0; j < height; ++j) {
            bmp_pixel_init (&img.img_pixels[i][j], R, G, B);
        }
    }

    bmp_img_write (&img, "/Users/staff/Desktop/test-libbmp.bmp");
    bmp_img_free (&img);
    return 0;
}

google libbmp (C library)

#include <stdio.h>
#include "google/libbmp/src/bmpfile.h"

// Seven Colors of Rainbow
u_int32_t rainbowColors[] = {
        0XFF0000, // red
        0XFFA500, // orange
        0XFFFF00, // yellow
        0X00FF00, // green
        0X007FFF, // young
        0X0000FF, // blue
        0X8B00FF  // purple
};

int main(int argc, char **argv) {

    int width = 711, height = 711;
    bmpfile_t *bmp;
    if ((bmp = bmp_create(width, height, 24)) == NULL) {
        return -1;
    }

    for (int i = 0; i < width; ++i) {
        // Current color
        u_int32_t currentColor = rainbowColors[0];
        if(i < 100) {
            currentColor = rainbowColors[0];
        } else if(i < 200) {
            currentColor = rainbowColors[1];
        } else if(i < 300) {
            currentColor = rainbowColors[2];
        } else if(i < 400) {
            currentColor = rainbowColors[3];
        } else if(i < 500) {
            currentColor = rainbowColors[4];
        } else if(i < 600) {
            currentColor = rainbowColors[5];
        } else if(i < 700) {
            currentColor = rainbowColors[6];
        }
        // Current color R component
        u_int8_t R = (currentColor & 0xFF0000) >> 16;
        // Current color G component
        u_int8_t G = (currentColor & 0x00FF00) >> 8;
        // Current color B component
        u_int8_t B = currentColor & 0x0000FF;

        rgb_pixel_t pixel = {B, G, R, 0};
        for (int j = 0; j < height; ++j) {
            bmp_set_pixel(bmp, j, i, pixel);
        }
    }

    bmp_save(bmp, "/Users/staff/Desktop/test-google-libbmp.bmp");
    bmp_destroy(bmp);
    return 0;
}

bmp (C library)

#include <stdio.h>
#include "bmp/bmp.h"

// Seven Colors of Rainbow
u_int32_t rainbowColors[] = {
        0XFF0000, // red
        0XFFA500, // orange
        0XFFFF00, // yellow
        0X00FF00, // green
        0X007FFF, // young
        0X0000FF, // blue
        0X8B00FF  // purple
};

int main() {

    int width = 711, height = 711;
    char bmp[BMP_SIZE(width, height)];
    bmp_init(bmp, width, height);

    for (int i = 0; i < width; ++i) {

        // Current color
        u_int32_t currentColor = rainbowColors[0];
        if(i < 100) {
            currentColor = rainbowColors[0];
        } else if(i < 200) {
            currentColor = rainbowColors[1];
        } else if(i < 300) {
            currentColor = rainbowColors[2];
        } else if(i < 400) {
            currentColor = rainbowColors[3];
        } else if(i < 500) {
            currentColor = rainbowColors[4];
        } else if(i < 600) {
            currentColor = rainbowColors[5];
        } else if(i < 700) {
            currentColor = rainbowColors[6];
        }

        for (int j = 0; j < height; ++j) {
            bmp_set(bmp, j, i, currentColor);
        }
    }

    FILE *f = fopen("/Users/hubin/Desktop/test-bmp.bmp", "wb");
    fwrite(bmp, sizeof(bmp), 1, f);
    fclose(f);
    return 0;
}

qdbmp (C library)

#include <stdio.h>
#include "qdbmp/qdbmp.h"

// Seven Colors of Rainbow
u_int32_t rainbowColors[] = {
        0XFF0000, // red
        0XFFA500, // orange
        0XFFFF00, // yellow
        0X00FF00, // green
        0X007FFF, // young
        0X0000FF, // blue
        0X8B00FF  // purple
};

int main() {
    int width = 711, height = 711;
    BMP* bmp = BMP_Create(width, height, 24);

    for (int i = 0; i < width; ++i) {
        // Current color
        u_int32_t currentColor = rainbowColors[0];
        if(i < 100) {
            currentColor = rainbowColors[0];
        } else if(i < 200) {
            currentColor = rainbowColors[1];
        } else if(i < 300) {
            currentColor = rainbowColors[2];
        } else if(i < 400) {
            currentColor = rainbowColors[3];
        } else if(i < 500) {
            currentColor = rainbowColors[4];
        } else if(i < 600) {
            currentColor = rainbowColors[5];
        } else if(i < 700) {
            currentColor = rainbowColors[6];
        }
        // Current color R component
        u_int8_t R = (currentColor & 0xFF0000) >> 16;
        // Current color G component
        u_int8_t G = (currentColor & 0x00FF00) >> 8;
        // Current color B component
        u_int8_t B = currentColor & 0x0000FF;
        for (int j = 0; j < height; ++j) {
            BMP_SetPixelRGB(bmp, j, i, R, G, B);
        }
    }

    /* Save result */
    BMP_WriteFile( bmp, "/Users/hubin/Desktop/test-qdbmp.bmp");
    BMP_CHECK_ERROR( stderr, -2 );

    /* Free all memory allocated for the image */
    BMP_Free( bmp );
    return 0;
}

easybmp (C + + Library)

#include "EasyBMP/EasyBMP.h"
using namespace std;

// Seven Colors of Rainbow
u_int32_t rainbowColors[] = {
        0XFF0000, // red
        0XFFA500, // orange
        0XFFFF00, // yellow
        0X00FF00, // green
        0X007FFF, // young
        0X0000FF, // blue
        0X8B00FF  // purple
};

int main( int argc, char* argv[]) {

    int width = 711, height = 711;
    int bitDepth = 24;

    BMP bmp;
    bmp.SetSize(width, height);
    bmp.SetBitDepth(bitDepth);

    for (int i = 0; i < width; ++i) {
        // Current color
        u_int32_t currentColor = rainbowColors[0];
        if(i < 100) {
            currentColor = rainbowColors[0];
        } else if(i < 200) {
            currentColor = rainbowColors[1];
        } else if(i < 300) {
            currentColor = rainbowColors[2];
        } else if(i < 400) {
            currentColor = rainbowColors[3];
        } else if(i < 500) {
            currentColor = rainbowColors[4];
        } else if(i < 600) {
            currentColor = rainbowColors[5];
        } else if(i < 700) {
            currentColor = rainbowColors[6];
        }
        // Current color R component
        u_int8_t R = (currentColor & 0xFF0000) >> 16;
        // Current color G component
        u_int8_t G = (currentColor & 0x00FF00) >> 8;
        // Current color B component
        u_int8_t B = currentColor & 0x0000FF;

        RGBApixel pixel;
        pixel.Red = R;
        pixel.Green = G;
        pixel.Blue = B;
        pixel.Alpha = 0;
        for (int j = 0; j < height; ++j) {
            bmp.SetPixel(j, i, pixel);
        }
    }

    bmp.WriteToFile("/Users/hubin/Desktop/test-easybmp.bmp");

    return 0;
}

bitmap (C++ library)

#include "bitmap/bitmap_image.hpp"

using namespace std;

// Seven Colors of Rainbow
u_int32_t rainbowColors[] = {
        0XFF0000, // red
        0XFFA500, // orange
        0XFFFF00, // yellow
        0X00FF00, // green
        0X007FFF, // young
        0X0000FF, // blue
        0X8B00FF  // purple
};

int main() {

    int width = 711, height = 711;

    bitmap_image image(width, height);
    image.clear();

    for (int i = 0; i < width; ++i) {
        // Current color
        u_int32_t currentColor = rainbowColors[0];
        if(i < 100) {
            currentColor = rainbowColors[0];
        } else if(i < 200) {
            currentColor = rainbowColors[1];
        } else if(i < 300) {
            currentColor = rainbowColors[2];
        } else if(i < 400) {
            currentColor = rainbowColors[3];
        } else if(i < 500) {
            currentColor = rainbowColors[4];
        } else if(i < 600) {
            currentColor = rainbowColors[5];
        } else if(i < 700) {
            currentColor = rainbowColors[6];
        }
        // Current color R component
        u_int8_t R = (currentColor & 0xFF0000) >> 16;
        // Current color G component
        u_int8_t G = (currentColor & 0x00FF00) >> 8;
        // Current color B component
        u_int8_t B = currentColor & 0x0000FF;

        for (int j = 0; j < height; ++j) {
            image.set_pixel(j, i, R, G, B);
        }
    }

    image.save_image("/Users/hubin/Desktop/test-bitmap.bmp");

    return 0;
}

View the effect of open source libraries

Six open source libraries can greatly reduce our workload!

Code:

rgb-to-bmp

Reference material:

libbmp

google libbmp

bmp

qdbmp

easybmp

bitmap

Is the content wrong? Contact author:

Posted by duke on Sun, 06 Oct 2019 13:20:52 -0700