Created
September 12, 2018 02:55
-
-
Save tamarous/d1b6d167a0d3488cad7f7f733051b63a to your computer and use it in GitHub Desktop.
save RGB24 frame to bmp file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void rgb24ToBMP(unsigned char *rgbBuffer, int width, int height, const char *bmppath) { | |
typedef struct { | |
long imageSize; | |
long blank; | |
long startPosition; | |
} BmpHead; | |
typedef struct { | |
long Length; | |
long width; | |
long height; | |
unsigned short colorPlane; | |
unsigned short bitColor; | |
long zipFormat; | |
long realSize; | |
long xPels; | |
long yPels; | |
long colorUse; | |
long colorImportant; | |
} InfoHead; | |
int i = 0, j = 0; | |
BmpHead m_BMPHeader = { 0 }; | |
InfoHead m_BMPInfoHeader = { 0 }; | |
char bfType[2] = { 'B','M' }; | |
int header_size = sizeof(bfType) + sizeof(BmpHead) + sizeof(InfoHead); | |
FILE *fp_bmp = NULL; | |
if((fp_bmp = fopen(bmppath, "wb")) == NULL) { | |
printf("Error: Cannot open output BMP file.\n"); | |
return; | |
} | |
m_BMPHeader.imageSize = 3 * width*height + header_size; | |
m_BMPHeader.startPosition = header_size; | |
m_BMPInfoHeader.Length = sizeof(InfoHead); | |
m_BMPInfoHeader.width = width; | |
//BMP storage pixel data in opposite direction of Y-axis (from bottom to top). | |
m_BMPInfoHeader.height = -height; | |
m_BMPInfoHeader.colorPlane = 1; | |
m_BMPInfoHeader.bitColor = 24; | |
m_BMPInfoHeader.realSize = 3 * width*height; | |
fwrite(bfType, 1, sizeof(bfType), fp_bmp); | |
fwrite(&m_BMPHeader, 1, sizeof(m_BMPHeader), fp_bmp); | |
fwrite(&m_BMPInfoHeader, 1, sizeof(m_BMPInfoHeader), fp_bmp); | |
for(j = 0; j < height; j++) { | |
for(i = 0; i < width; i++) { | |
char temp = rgbBuffer[(j*width + i) * 3 + 2]; | |
rgbBuffer[(j*width + i) * 3 + 2] = rgbBuffer[(j*width + i) * 3 + 0]; | |
rgbBuffer[(j*width + i) * 3 + 0] = temp; | |
} | |
} | |
fwrite(rgbBuffer, 3 * width*height, 1, fp_bmp); | |
fclose(fp_bmp); | |
printf("Finish generate %s!\n", bmppath); | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment