Real cache displays BMP Images

Source: Internet
Author: User
Tags bmp image tty mode
Display the BMP Image Tag programmatically in framebuffer: framebuffer BMP 2006-11-10

Today, I read other people's code and know that the console can enter the graphic mode, so that the display of Shell programs will not affect the display of BMP images. So copy it and put it into your previous program, and then try the full screen display in the BMP graph of 800x600! Shuang ^ 0 ^ Shuang

/*
Showbmp. c
Allenyao 2006/11/10

*/
# Include <unistd. h>
# Include <stdio. h>
# Include <stdlib. h>
# Include <fcntl. h>
# Include <string. h>
# Include <Linux/FB. h>
# Include <Linux/KD. h>/* newly added, used for IOCTL */
# Include <sys/Mman. h>
# Include <sys/IOCTL. h>
# Include <ARPA/inet. h>

Typedef struct
{
Char cftype [2];/* file type, which must be "BM" (0x4d42 )*/
Char cfsize [4];/* file size (in bytes )*/
Char cfreserved [4];/* reserved, must be 0 */
Char cfoffbits [4];/* offset (in bytes) of the bitmap array to the file header )*/
} Bitmapfileheader;/* File Header structure */

Typedef struct
{
Char cisize [4];/* size of bitmapinfoheader */
Char ciwidth [4];/* bitmap width (pixels )*/
Char ciheight [4];/* bitmap height (pixel )*/
Char ciplanes [2];/* Number of bit planes of the target device, which must be set to 1 */
Char cibitcount [2];/* The number of digits in each pixel. Values: 1, 4, 8, or 24 */
Char cicompress [4];/* bitmap array compression method, 0 = No compression */
Char cisizeimage [4];/* image size (in bytes )*/
Char cixpelspermeter [4];/* number of pixels per meter per target device level */
Char ciypelspermeter [4];/* number of pixels per meter in the vertical direction of the target device */
Char ciclrused [4];/* Number of colors in the color table used by the bitmap */
Char ciclrimportant [4];/* Number of important color indexes */
} Bitmapinfoheader;/* bitmap information header structure */

Typedef struct
{
Char rgbblue;
Char rgbgreen;
Char rgbred;
Char rgbreserved;
} Rgbquad;

Bitmapfileheader filehead;
Bitmapinfoheader infohead;
Rgbquad;

Char * FBP = 0;
Int xres = 0;
Int yres = 0;
Int bits_per_pixel = 0;
Int tty;
Int tty_mode_was;

Int set_tty_graphics (void );
Int set_tty_text (void );
Int show_bmp (char * BMP file );
Long chartolong (char * string, int length );
/*************************************** ***************************************
*
**************************************** **************************************/
Int main (INT argc, char * argv [])
{
Int fbfd = 0;
Struct fb_var_screeninfo vinfo;
Struct fb_fix_screeninfo finfo;
Long int screensize = 0;

// Open the file for reading and writing
Fbfd = open ("/dev/fb0", o_rdwr );
If (! Fbfd)
{
Printf ("error: cannot open framebuffer device./N ");
Exit (1 );
}

// Get fixed screen information
If (IOCTL (fbfd, fbioget_fscreeninfo, & finfo ))
{
Printf ("error reading fixed information./N ");
Exit (2 );
}

// Get variable screen information
If (IOCTL (fbfd, fbioget_vscreeninfo, & vinfo ))
{
Printf ("error reading variable information./N ");
Exit (3 );
}

Printf ("% DX % d, % dbpp/N", vinfo. xres, vinfo. yres, vinfo. bits_per_pixel );
Xres = vinfo. xres;
Yres = vinfo. yres;
Bits_per_pixel = vinfo. bits_per_pixel;

// Figure out the size of the screen in bytes
Screensize = vinfo. xres * vinfo. yres * vinfo. bits_per_pixel/8;

// Map the device to memory
FBP = (char *) MMAP (0, screensize, prot_read | prot_write, map_shared,
Fbfd, 0 );
If (INT) FBP =-1)
{
Printf ("error: failed to map framebuffer device to memory./N ");
Exit (4 );
}

If (set_tty_graphics ())
{
Show_bmp (argv [1]);
}
Sleep (10 );
Set_tty_text ();
 
Munmap (FBP, screensize );
Close (fbfd );
Return 0;
}

/*************************************** ***************************************
*
**************************************** **************************************/
Int show_bmp (char * BMP file)
{
File * FP;
Int RC;
Int cibitcount, ciwidth, ciheight;
Int line_x, line_y;
Long int location = 0, bytesperline = 0;
Char TMP [1, 1024*10];

/* Open a bitmap file */
Fp = fopen (BMP file, "rb ");
If (FP = NULL)
{
Return (-1 );
}
 
/* Read the bitmap file header */
Rc = fread (& filehead, 1, sizeof (bitmapfileheader), FP );
If (RC! = Sizeof (bitmapfileheader ))
{
Fclose (FP );
Return (-2 );
}
 
/* Determine the bitmap type */
If (memcmp (filehead. cftype, "BM", 2 )! = 0)
{
Fclose (FP );
Return (-3 );
}

/* Read the Bitmap header */
Rc = fread (char *) & infohead, 1, sizeof (bitmapinfoheader), FP );
If (RC! = Sizeof (bitmapinfoheader ))
{
Fclose (FP );
Return (-4 );
}
 
Ciwidth = (INT) chartolong (infohead. ciwidth, 4 );
Ciheight = (INT) chartolong (infohead. ciheight, 4 );
Cibitcount = (INT) chartolong (infohead. cibitcount, 4 );
 
Line_x = line_y = 0;
/*
While (! Feof (FP ))
{
Rc = fread (char *) & rgbquad, 1, sizeof (rgbquad), FP );
If (RC! = Sizeof (rgbquad ))
{
Break;
}

Location = line_x * bits_per_pixel/8 + (ciheight-line_y-1) * xres * bits_per_pixel/8;

* (FBP + location) = rgbquad. rgbblue;
* (FBP + location + 1) = rgbquad. rgbgreen;
* (FBP + location + 2) = rgbquad. rgbred;
* (FBP + location + 3) = rgbquad. rgbreserved;

Line_x ++;
If (line_x = (ciwidth-1 ))
{
Line_x = 0;
Line_y ++;
}
}
*/
Bytesperline = (ciwidth * cibitcount + 31)/32*4;
 
While (! Feof (FP ))
{
Rc = fread (TMP, 1, bytesperline, FP );
If (RC! = Bytesperline)
{
Break;
}

Location = (ciheight-line_y-1) * xres * bits_per_pixel/8;
Memcpy (FBP + location), TMP, bytesperline );

Line_y ++;
}
Fclose (FP );
Return (0 );
}

/*************************************** ***************************************
*
**************************************** **************************************/
Long chartolong (char * string, int length)
{
Long number;
 
If (length <= 4)
{
Memset (& number, 0x00, sizeof (long ));
Memcpy (& number, String, length );
}
 
Return (number );
}

/*************************************** ***************************************
*
**************************************** **************************************/
Int set_tty_graphics (void)
{
Int ret = 1;

If (0> (TTY = open ("/dev/tty0", o_rdwr )))
{
Printf ("error:/dev/tty0 open failed/N ");
Ret = 0;
}
Else if (0> IOCTL (TTY, kdgetmode, & tty_mode_was ))
{
Printf ("error:/dev/tty0 get mode failed/N ");
Close (TTY );
Ret = 0;
}
Else if (kd_graphics! = Tty_mode_was)
& (0> IOCTL (TTY, kdsetmode, kd_graphics )))
{
Printf ("error:/dev/tty0 set graphics failed/N ");
Close (TTY );
Ret = 0;
}
Else
{
Printf ("tty mode was ");
Switch (tty_mode_was)
{
Case kd_text: printf ("kd_text"); break;
Case kd_graphics: printf ("kd_graphics"); break;
Default: printf ("impossible! "); Break;
}
Printf ("/N ");
Printf ("tty mode set to kd_graphics/N ");
Fflush (stdout );
Close (TTY );
}
Return (RET );
}

/*************************************** ***************************************
*
**************************************** **************************************/
Int set_tty_text (void)
{
Int ret = 1;

If (0> (TTY = open ("/dev/tty0", o_rdwr )))
{
Printf ("error:/dev/tty0 open failed/N ");
Ret = 0;
}
Else if (0> IOCTL (TTY, kdsetmode, tty_mode_was ))
{
Printf ("error:/dev/tty0 reset mode failed/N ");
Close (TTY );
Ret = 0;
}
Else
{
Printf ("tty mode set ");
Switch (tty_mode_was)
{
Case kd_text: printf ("kd_text"); break;
Case kd_graphics: printf ("kd_graphics"); break;
Default: printf ("impossible! "); Break;
}
Printf ("/N ");
Fflush (stdout );
Close (TTY );
}
Return (RET );
}

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.