A program for converting rgb565 to bitmap

Source: Internet
Author: User
/*  All Rights Reserved  Author:fanping.deng@gmail.com  Function: convert RGB565 to BMP  Build by g++
*/# Include <unistd. h> # include <stdio. h> # include <errno. h> # include <sys/types. h> # include <sys/STAT. h> # include <fcntl. h> # include <stdlib. h> # include <string. h> typedef short word; typedef int DWORD; typedef int long; typedef char byte; // bitmap file header infotypedef struct tagbitmapfileheader {word bftype; DWORD bfsize; Word bfreserved1; Word bfreserved2; DWORD bfoffbits; }__ attribute _ (packed) bitmapfi Leheader; // bitmap info headertypedef struct tagbitmapinfoheader {// bmih DWORD bisize; long biwidth; // The Image Width in pixels long biheight; // The length of the image in pixels: Word biplanes; Word bibitcount; // DWORD bicompression; DWORD bisizeimage; long bixpelspermeter; long biypelspermeter; DWORD biclrused; DWORD biclrimportant ;} __attribute _ (packed) bitmapinfoheader; typedef struct tagpalettergb {byte B; byte g; byte R; byte reserved; }__ attribute _ (packed) palettergb; int g_len = sizeof (bitmapfileheader) + sizeof (bitmapinfoheader); // + sizeof (palettergb ); void initbmpheader (bitmapfileheader * pbmpfileheader) {pbmpfileheader-> bftype = 0x4d42; // magic number pbmpfileheader-> bfsize = g_len + 320*480*3; // 320*480, screen size, hardcoded. you shoshould change it for different size pbmpfileheader-> bfreserved1 = 0x00; PBM Pfileheader-> bfreserved2 = 0x00; pbmpfileheader-> bfoffbits = g_len;} void initbmp infoheader (bitmapinfoheader * pbmpinfoheader) {pbmpinfoheader-> bisize = sizeof (bitmapinfoheader ); pbmpinfoheader-> biwidth = 320; // This part is written to death, because at that time the mobile phone screen was tested to be pbmpinfoheader-> biheight = 480; pbmpinfoheader-> biplanes = 1; pbmpinfoheader-> bibitcount = 24; pbmpinfoheader-> bicompression = 0; pbmpinfoheader-> bisizeimage = 320*480*3; p BMP infoheader-> bixpelspermeter = pbmpinfoheader-> biypelspermeter = 0; pbmpinfoheader-> biclrused = pbmpinfoheader-> Signature = 0;} void convetbmp (int fs, int FD, short * psrc1_buffer, char * pdstintbuffer); int main (INT argc, char * argv []) {If (argc <3) {printf ("error use BMP generator, we need the Source Path "); return-1;} bitmapfileheader BMP fileheader; initbmpheader (& BMP fileheader); bitmapinfo Header BMP infoheader; initbmp infoheader (& BMP infoheader); palettergb BMP palette; memset (& BMP palette, 0, sizeof (palettergb); int FD = open (argv [1], o_creat | o_wronly ); if (FD =-1) {printf ("fail to create DST file % s \ n", argv [1]); exit (-1 );} int FS = open (argv [2], o_rdonly); If (FS =-1) {printf ("fail to open SRC file % s \ n ", argv [2]); exit (-1);} // write head info to the destination file // Note: The converted BMP data The format is rgb888, so no palette information is required. For information about BMP, you can search for write (FD, & BMP fileheader, sizeof (bitmapfileheader); write (FD, & BMP infoheader, sizeof (bitmapinfoheader); // write (FD, & BMP palette, sizeof (palettergb); // 320*480 // the row-based char * pdstintbuffer = new char [3*320]; memset (pdstintbuffer, 0, sizeof (char) * 320); short * psrc0000buffer = new short [320]; memset (psrc0000buffer, 0, sizeof (short) * 320); convetbmp (FS, FD, psrc0000buffer, pdstintbuffer); close (FD); close (FS); Delete [] psrc1_buffer; Delete [] pdstintbuffer; printf ("BMP generated \ n"); Return 0 ;} void convetbmp (int fs, int FD, short * psrc1_buffer, char * pdstintbuffer) {int Height = 480; int srclinelength = sizeof (short) * 320; int dstlienlength = sizeof (char) * 3*320; while (height> 0) {lseek (FS, (height-1) * srclinelength, seek_set); read (FS, (char *) psrc1_buffer, srclinelength ); for (INT x = 0; x <320; X ++) {short srcpixel = * (psrc1_buffer + x ); // first change rgb565 to rgb888 char srcr = (srcpixel & 0xf800)> 11; char srcg = (srcpixel & 0x07e0)> 5; char SRCB = srcpixel & 0x001f; pdstintbuffer [x * 3 + 0] = SRCB; pdstintbuffer [x * 3 + 1] = srcg; pdstintbuffer [x * 3 + 2] = srcr; pdstintbuffer [x * 3 + 0] <= 3; pdstintbuffer [x * 3 + 1] <= 2; pdstintbuffer [x * 3 + 2] <= 3; // pdstintbuffer [x * 3 + 0] | = (SRCB & 0x07); // pdstintbuffer [x * 3 + 1] | = (srcg & 0x03 ); // pdstintbuffer [x * 3 + 2] | = (srcr & 0x07);} write (FD, (char *) pdstintbuffer, dstlienlength); -- height ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.