Display BMP, RGB, and YUV with SDL in MFC

Source: Internet
Author: User

# Include "SDL. H"

# Ifdef test_vga16/* define this if you want to test VGA 16-color video modes */
# Define num_colors 16
# Else
# Define num_colors 256
# Endif
Sdl_surface * screen;

Void display_bmp ()
{
Sdl_surface * image;
Char * file_name = "D: \ temp \ bao.bmp ";
/* Load the BMP file into a surface */
Image = sdl_loadbmp (file_name );
If (image = NULL ){
// Fprintf (stderr, "couldn't load % s: % s \ n", file_name, sdl_geterror ());
Return;
}

/*
* Palettized screen modes will have a default palette (a standard
* 8*8*4 color cube), but if the image is palettized as well we can
* Use that palette for a nicer Color Matching
*/
If (image-> Format-> palette & screen-> Format-> palette)
{
Sdl_setcolors (screen, image-> Format-> palette-> colors, 0,
Image-> Format-> palette-> ncolors );
}

/* Blit onto the screen surface */
If (sdl_blitsurface (image, null, screen, null) <0)
Fprintf (stderr, "blitsurface error: % s \ n", sdl_geterror ());

Sdl_updaterect (screen, 0, 0, image-> W, image-> H );

/* Free the allocated BMP surface */
Sdl_freesurface (image );
}
Cstring STR;
Void csdl_testdlg: onbutton1 ()
{
// Todo: add your control notification handler code here
M_list.resetcontent ();

Sdl_event event;
If (sdl_init (sdl_init_video | sdl_init_audio) =-1 )){
Str. Format ("cocould not initialize SDL: % S. \ n", sdl_geterror ());
M_list.addstring (STR );
}
 
M_list.addstring ("SDL initialized .");

/* Have a preference for 8-bit, but accept any depth */
Screen = sdl_setvideomode (720,576, 16, sdl_swsurface | sdl_anyformat );
If (screen = NULL ){
Str. Format ("Couldn 'tset 720*576 video mode: % s", sdl_geterror ());
M_list.addstring (STR );
}
Str. Format ("set 720*576 at % d bits-per-pixel mode ",
Screen-> Format-> bitsperpixel );
M_list.addstring (STR );
 
Display_bmp ();

/* Shutdown all subsystems */
//

M_list.addstring ("quiting... press image! ");

While (sdl_waitevent (& event ))
{
Switch (event. type)
{
Case sdl_mousebuttondown:
Sdl_quit ();
Return;
Break;
Case sdl_keydown:
If (event. Key. keysym. sym = sdlk_space)
{
Sdl_quit ();
Return;
Break;
}
}
}
}

Void csdl_testdlg: onbutton2 ()
{
// Todo: add your control notification handler code here
Int I = 1;
Int X, Y;
Int W = 720;
Int H = 576;
Char c = 'n ';
 
File * FP;
Char filename [64];
Unsigned char * py;
Unsigned char * Pu;
Unsigned char * PV;
Sdl_rect rect;
M_list.resetcontent ();
If (sdl_init (sdl_init_video) <0)
{
Fprintf (stderr, "can not initialize SDL: % s \ n", sdl_geterror ());
Exit (1 );
}
Atexit (sdl_quit );
 
Sdl_surface * screen = sdl_setvideomode (W, H, 0, 0 );
If (screen = NULL)
{
Fprintf (stderr, "create surface error! \ N ");
Exit (1 );
}
 
Sdl_overlay * overlay = sdl_createyuvoverlay (W, H, sdl_yv12_overlay, screen );
If (overlay = NULL)
{
Fprintf (stderr, "Create overlay error! \ N ");
Exit (1 );
}
 
Printf ("W: % D, H: % d, planes: % d \ n", overlay-> W, overlay-> H, overlay-> planes );
Printf ("pitches: % d, % d, % d \ n", overlay-> pitches [0], overlay-> pitches [1], overlay-> pitches [2]);
 
Py = (unsigned char *) malloc (w * H );
Pu = (unsigned char *) malloc (w * H/4 );
Pv = (unsigned char *) malloc (w * H/4 );
 

Sdl_locksurface (screen );
Sdl_lockyuvoverlay (overlay );

 
Fp = fopen ("D: \ temp \ test. YUV", "rb ");
If (FP = NULL)
{
Fprintf (stderr, "Open File error! \ N ");
Exit (1 );
}
While (! Feof (FP ))
{
Fread (PY, 1, w * H, FP );
Fread (PU, 1, w * H/4, FP );
Fread (PV, 1, w * H/4, FP );

Memcpy (overlay-> pixels [0], Py, w * H );
Memcpy (overlay-> pixels [1], PV, w * H/4 );
Memcpy (overlay-> pixels [2], Pu, w * H/4 );



Sdl_unlockyuvoverlay (overlay );
Sdl_unlocksurface (screen );

Rect. W = W;
Rect. H = h;
Rect. x = rect. Y = 0;
Sdl_displayyuvoverlay (overlay, & rect );

Sdl_delay (40 );

I + = 1;
Str. Format ("Current frmcnt: % d", I );
M_list.addstring (STR );
}
Fclose (FP );
Free (Py );
Free (PU );
Free (PV );
 
 
Sdl_freeyuvoverlay (overlay );
Sdl_freesurface (screen );
 

}
Unsigned char buff [720*576*3];
Unsigned int RGB [720*576];
Void csdl_testdlg: onbutton3 ()
{
// Todo: add your control notification handler code here
M_list.resetcontent ();
 
// Sdl_event event;
If (sdl_init (sdl_init_video) <0)
{
Fprintf (stderr, "can not initialize SDL: % s \ n", sdl_geterror ());
Exit (1 );
}
Atexit (sdl_quit );
 
Screen = sdl_setvideomode (720,576, 32, sdl_swsurface | sdl_anyformat );
If (screen = NULL ){
Str. Format ("Couldn 'tset 720*576 video mode: % s", sdl_geterror ());
M_list.addstring (STR );
}

Sdl_surface * image;

Uint32 rmask, gmask, bmask, amask;

/* SDL interprets each pixel as a 32-bit number, so our masks must depend
On the endianness (byte order) of the machine */
# If sdl_byteorder = sdl_big_endian
Rmask = 0xff000000;
Gmask = 0x00ff0000;
Bmask = 0x0000ff00;
Amask = 0x000000ff;
# Else
Rmask = 0x000000ff;
Gmask = 0x0000ff00;
Bmask = 0x00ff0000;
Amask = 0xff000000;
# Endif

Image = maid (sdl_swsurface, 720,576, 0,
Rmask, gmask, bmask, amask );
If (image = NULL ){
Fprintf (stderr, "creatergbsurface failed: % s \ n", sdl_geterror ());
Exit (1 );
}

// Sdl_locksurface (screen );
// Sdl_locksurface (image );
Cbitmap m_bitmap;
Hbitmap m_hbitmap;
Bitmap bm; // structure for storing bitmap Information

M_hbitmap = (hbitmap): LoadImage (null, "d :\\ Temp \ bao.bmp", image_bitmap, lr_loadfromfile); // load bitmap
If (m_bitmap.m_hobject)
M_bitmap.deleteobject ();
M_bitmap.attach (m_hbitmap); // associate the handle with cbitmap
M_bitmap.getbitmap (& BM );
M_bitmap.getbitmapbits (BM. bmheight * BM. bmwidthbytes, RGB );
//

Memcpy (screen-> pixels, RGB, 720*576*4 );
// Sdl_unlocksurface (image );
// Sdl_unlocksurface (screen );

Sdl_updaterect (screen, 0, 0, image-> W, image-> H );

/* Free the allocated BMP surface */
Sdl_freesurface (image );
}

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.