Getting started with audio and video – 05-rgb-to-bmp using open source library

Time:2020-5-28

Audio and video introduction article directory

Rgb-to-bmp review

Convert RGB data to BMP image:

  1. Understand BMP file format
  2. Prepare BMP file header information
  3. Prepare BMP header
  4. The order in which BMP stores RGB is b-g-r
  5. For the biheight field in BITMAPINFOHEADER, biheight is positive, bitmaps are scanned from bottom to top, biheight is negative, and bitmaps are scanned from top to bottom.
  6. Bmp image four byte alignment

In the actual development process, the third-party open source library is usually used to simplify development, shield some of the underlying complexity, and save a lot of time to write code.

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
        0xfff00, // yellow
        0x00ff00, // Green
        0x007fff, // Green
        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 分量
        u_int8_t R = (currentColor & 0xFF0000) >> 16;
        //Current color G 分量
        u_int8_t G = (currentColor & 0x00FF00) >> 8;
        //Current color B 分量
        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
        0xfff00, // yellow
        0x00ff00, // Green
        0x007fff, // Green
        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 分量
        u_int8_t R = (currentColor & 0xFF0000) >> 16;
        //Current color G 分量
        u_int8_t G = (currentColor & 0x00FF00) >> 8;
        //Current color B 分量
        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
        0xfff00, // yellow
        0x00ff00, // Green
        0x007fff, // Green
        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
        0xfff00, // yellow
        0x00ff00, // Green
        0x007fff, // Green
        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 分量
        u_int8_t R = (currentColor & 0xFF0000) >> 16;
        //Current color G 分量
        u_int8_t G = (currentColor & 0x00FF00) >> 8;
        //Current color B 分量
        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
        0xfff00, // yellow
        0x00ff00, // Green
        0x007fff, // Green
        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 分量
        u_int8_t R = (currentColor & 0xFF0000) >> 16;
        //Current color G 分量
        u_int8_t G = (currentColor & 0x00FF00) >> 8;
        //Current color B 分量
        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
        0xfff00, // yellow
        0x00ff00, // Green
        0x007fff, // Green
        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 分量
        u_int8_t R = (currentColor & 0xFF0000) >> 16;
        //Current color G 分量
        u_int8_t G = (currentColor & 0x00FF00) >> 8;
        //Current color B 分量
        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 library

Six open source libraries can greatly reduce our workload!

Getting started with audio and video - 05-rgb-to-bmp using open source library


code:

rgb-to-bmp

reference material:

libbmp

google libbmp

bmp

qdbmp

easybmp

bitmap

Wrong content? Contact author:

Getting started with audio and video - 05-rgb-to-bmp using open source library


Recommended Today

The way of nonlinear optimization

Mathematical knowledge 1、 Nonlinear functionLinear function is another name of a function of first degree, then nonlinear function means that the function image is not a function of a straight line.Nonlinear functions include exponential function, power function, logarithmic function, polynomial function and so on. 2、 Taylor expansion1. Taylor formula:Taylor’s formula is to add a_ The […]