rgb888与rgb565互转实现

#include 
#include 
#include 
#include 
#include 

#include "./global_screen.h"

struct fb_var_screeninfo vinfo;
unsigned int *fbp565;
unsigned int RED_COLOR888;
unsigned int GREEN_COLOR888;
unsigned int BLUE_COLOR888;

unsigned short RGB8888ToRGB565(unsigned int n888Color)
{
unsigned short n565Color = 0;

unsigned char cRed,cGreen,cBlue; 

cRed   = (n888Color & RGB888_RED) >> 19;
cGreen = (n888Color & RGB888_GREEN) >> 10;
cBlue  = (n888Color & RGB888_BULE) >> 3;
n565Color = (cRed << 11) + (cGreen << 5) + (cBlue << 0);

return n565Color;
}

unsigned int RGB565ToRGB888(unsigned short n565Color)
{
unsigned int n888Color = 0;

unsigned char cRed,cGreen,cBlue;

cRed = (n565Color & RGB565_RED) >> 8;
cGreen = (n565Color & RGB565_GREEN) >> 3;
cBlue = (n565Color & RGB565_BLUE) << 3;

n888Color = (cRed << 16) + (cGreen << 8) + (cBlue << 0);

return n888Color;
}

int judge_bits(int * bits)
{
	vinfo.bits_per_pixel = *bits;
	int x = 0;
	int y = 0;
	if(vinfo.bits_per_pixel == 16)
    {
        printf("16 bpp framebuffer\n");
        //Red Screen
        printf("Red Screen\n");
        for(y = 0; y < vinfo.yres / 3; y++)
        {
            for(x = 0; x < vinfo.xres; x++)
            {
                *(fbp565 + y * vinfo.xres + x) = RED_COLOR565;
            }
        }
        //Green Screen
        printf("Green Screen\n");
        for(y = vinfo.yres / 3; y < (vinfo.yres * 2) / 3; y++)
        {
            for(x = 0; x < vinfo.xres; x++)
            {
                *(fbp565 + y * vinfo.xres + x) = GREEN_COLOR565;
            }
        }
        //Blue Screen
        printf("Blue Screen\n");
        for(y =    (vinfo.yres * 2) / 3; y < vinfo.yres; y++)
        {
            for(x = 0; x < vinfo.xres; x++)
            {
                *(fbp565 + y * vinfo.xres + x) = BLUE_COLOR565;
            }
        }
    }
    else if(vinfo.bits_per_pixel == 24)
    {
        RED_COLOR888     = RGB565ToRGB888(RED_COLOR565);
        GREEN_COLOR888 = RGB565ToRGB888(GREEN_COLOR565);
        BLUE_COLOR888    = RGB565ToRGB888(BLUE_COLOR565);

        printf("24 bpp framebuffer\n");
        //Red Screen
        printf("Red Screen\n");
        for(y = 0; y < vinfo.yres / 3; y++)
        {
            for(x = 0; x < vinfo.xres; x++)
            {
                *(fbp565 + y * vinfo.xres + x) = RED_COLOR888;
            }
        }
        //Green Screen
        printf("Green Screen\n");
        for(y = vinfo.yres / 3; y < (vinfo.yres * 2) / 3; y++)
        {
            for(x = 0; x < vinfo.xres; x++)
            {
                *(fbp565 + y * vinfo.xres + x) = GREEN_COLOR888;
            }
        }
        //Blue Screen
        printf("Blue Screen\n");
        for(y =    (vinfo.yres * 2) / 3; y < vinfo.yres; y++)
        {
            for(x = 0; x < vinfo.xres; x++)
            {
                *(fbp565 + y * vinfo.xres + x) = BLUE_COLOR888;
            }
        }    
    }
    else if(vinfo.bits_per_pixel == 32)
    {
        printf("32 bpp framebuffer\n");
        //Red Screen
        printf("Red Screen\n");
        for(y = 0; y < vinfo.yres / 3; y++)
        {
            for(x = 0; x < vinfo.xres; x++)
            {
                *(fbp565 + y * vinfo.xres + x) = RED_COLOR888_32bpp;
            }
        }
        //Green Screen
        printf("Green Screen\n");
        for(y = vinfo.yres / 3; y < (vinfo.yres * 2) / 3; y++)
        {
            for(x = 0; x < vinfo.xres; x++)
            {
                *(fbp565 + y * vinfo.xres + x) = GREEN_COLOR888_32bpp;
            }
        }
        //Blue Screen
        printf("Blue Screen\n");
        for(y =    (vinfo.yres * 2) / 3; y < vinfo.yres; y++)
        {
            for(x = 0; x < vinfo.xres; x++)
            {
                *(fbp565 + y * vinfo.xres + x) = BLUE_COLOR888_32bpp;
            }
        }    
    }
    else
    {
        printf("Warnning: bpp is not 16 and 24 and 32\n");
    }

	return 0;
}

你可能感兴趣的:(rgb888与rgb565互转实现)