MFC下用sdl 显示bmp、rgb、yuv

#include <libsdl/SDL.h>
//#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 colour cube), but if the image is palettized as well we can
     * use that palette for a nicer colour 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("Could 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't set 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);

    screen = SDL_SetVideoMode(720, 576, 32, SDL_SWSURFACE|SDL_ANYFORMAT);
    if ( screen == NULL ) {
        str.Format( "Couldn't set 720*576 video mode: %s",SDL_GetError());
        m_list.AddString(str);
    }

    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\\1.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't set 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 = SDL_CreateRGBSurface(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;//存放位图信息的结构

m_hBitmap = (HBITMAP)::LoadImage(NULL,"d:\\temp\\bao.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);   //装载位图
if(m_bitmap.m_hObject)
m_bitmap.DeleteObject();
m_bitmap.Attach(m_hBitmap);//将句柄与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);
}

void CSdl_testDlg::OnButton4() 
{
// 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);

    screen = SDL_SetVideoMode(720, 576, 32, SDL_SWSURFACE|SDL_ANYFORMAT);
    if ( screen == NULL ) {
        str.Format( "Couldn't set 720*576 video mode: %s",SDL_GetError());
        m_list.AddString(str);
    }

    SDL_Overlay* overlay = SDL_CreateYUVOverlay(w, h, SDL_YUY2_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*2);
//    pU = (unsigned char*)malloc(w*h/4);
//    pV = (unsigned char*)malloc(w*h/4);

   
        SDL_LockSurface(screen);
        SDL_LockYUVOverlay(overlay);

 
        fp = fopen("d:\\TEMP\\6082.dat", "rb");
        if (fp == NULL)
        {
            fprintf(stderr, "open file error!\n");
            exit(1);
        }
while (!feof(fp))
{
        fread(pY, 1, w*h*2, fp);

        memcpy(overlay->pixels[0], pY, w*h*2);

       

        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);
}

http://download.csdn.net/detail/mao0514/8202701

时间: 2024-09-30 21:01:12

MFC下用sdl 显示bmp、rgb、yuv的相关文章

位图显示-MFC中OnPaint函数显示BMP图片的问题

问题描述 MFC中OnPaint函数显示BMP图片的问题 为什么我在MFC对话框中,在OnPaint函数中加入以下代码,BMP图像不能显示啊~~· BITMAP bm; CBitmap bmp; bmp.LoadBitmap(IDB_BITMAP1); CDC memdc; CDC dc; memdc.CreateCompatibleDC(&dc);/ bmp.GetBitmap(&bm); CBitmap *bmpold=memdc.SelectObject(&bmp);/ dc

嵌入式linux------SDL移植(am335x下显示bmp图片)

#include<stdio.h> #include "/usr/local/ffmpeg_arm/include/SDL/SDL.h" char *bmp_name[3] = {"000.bmp","111.bmp","222.bmp"}; int main() { int i=0; //The images SDL_Surface* hello = NULL; SDL_Surface* screen = NUL

【数字图像处理】一.MFC详解显示BMP格式图片

本文主要是讲述<数字图像处理>系列栏目中的第一篇文章.主要详细介绍了BMP图片格式,同时使用C++和MFC显示BMP格式,主要结合自己的<数字图像处理>课程和以前的项目叙述讲解. 一.BMP图片格式定义 BMP文件格式是Windows操作系统推荐和支持的标准图像文件格式,是一种将内存或显示器的图像数据不经过压缩而直接按位存盘的文件格式,故称位图(bitmap),其扩展名为BMP.BMP图像通常有4个部分组成:位图文件头.位图信息头.颜色表.位图数据.如下图所示: 第一部分为位图文件

MFC下socket的收到的数据是如何区别显示在不同的窗口中的呢,求大神们指导!

问题描述 MFC下socket的收到的数据是如何区别显示在不同的窗口中的呢,求大神们指导! MFC下socket的收到的数据是如何区别显示在不同的窗口中的呢,本人是MFC,小菜鸟!求大神们指导! 解决方案 收到数据了,自己按格式得到不同字段数据,然后就是操作窗口各个控件,把数据显示上去.

最简单的视音频播放示例7:SDL2播放RGB/YUV

本文记录SDL播放视频的技术.在这里使用的版本是SDL2.实际上SDL本身并不提供视音频播放的功能,它只是封装了视音频播放的底层API.在Windows平台下,SDL封装了Direct3D这类的API用于播放视频:封装了DirectSound这类的API用于播放音频.因为SDL的编写目的就是简化视音频播放的开发难度,所以使用SDL播放视频(YUV/RGB)和音频(PCM)数据非常的容易.下文记录一下使用SDL播放视频数据的技术.   SDL简介 SDL(Simple DirectMedia La

在你的MFC应用程序中显示一个JPG文件

在VB中,我可以通过创建一个图像控件来显示一个JPG或GIF文件,但是我如何在我的MFC应用程序中显示一个JGP文件呢? 好问题!有时使用VB的程序员觉得这个很容易.只要往你的表中拖入一个图像控件,然后你就可以往下做了--然而C++程序员就不得不感到烦恼和头疼.那我们要做些什么呢,编写我们自己的JPG解压函数吗? 当然不是这样的!事实上,C/C++程序员能够使用与VB程序员所使用的非常类似(可以说是差不多)的图像控件.我并没有开玩笑.VB图像控件是基于一个叫"IPicture"的系统C

最简单的视音频播放示例5:OpenGL播放RGB/YUV

本文记录OpenGL播放视频的技术.OpenGL是一个和Direct3D同一层面的技术.相比于Direct3D,OpenGL具有跨平台的优势.尽管在游戏领域,DirectX的影响力已渐渐超越OpenGL并被大多数PC游戏开发商所采用,但在专业高端绘图领域,OpenGL因为色彩准确,仍然是不能被取代的主角. OpenGL简介 从网上搜集了一些有关OpenGL简介方面的知识,在这里列出来.开放图形库(英语:Open Graphics Library,缩写为OpenGL)是个定义了一个跨编程语言.跨平

EmguCV(OpenCV)实现高效显示视频(YUV)叠加包括汉字

原文:EmguCV(OpenCV)实现高效显示视频(YUV)叠加包括汉字   视频处理中,往往需要在上面增加文字包括汉字英文字母数字标点等,Emgu.CV/opencv 绘图 线面文字包括中文 这篇里也有相关介绍,但是这篇里根据逐像素修改rgb值的方法效率太低    查了很多资料,基本上opencv叠加汉字的方法都起源于这里 http://wenku.baidu.com/link?url=g1dCXwRbSpy7XUhsStRLANQRmvAXKSAa1ohrphx1R3XSZozi68WrTO

VC对话框中利用Picture Control事件显示BMP图片

VC对话框初始时,Picture Control显示BMP图片: BOOL CLoginDog::OnInitDialog() { CDialog::OnInitDialog(); HBITMAP hBitmap; //添加登陆窗口中的图片 //关联图片ID hBitmap =(HBITMAP)LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BITMAP_LOGIN_PIC), IMAGE_BITMAP,0,0, LR_LOADMAP3