SDL显示YUV格式图片

#include <stdlib.h>
#include <SDL/SDL.h>

int main(int argc , char* argv[])
{
    int w = 352;
    int h = 288;
    char c = 'n';

    FILE* fp;
    char filename[64];
    unsigned char* pY;
    unsigned char* pU;
    unsigned char* pV;
    SDL_Rect rect;

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

    sprintf(filename, "/home/ly/文档/testSDL/test2/3.yuv");

    fp = fopen(filename, "rb");
    if (fp == NULL){
        fprintf(stderr, "open file error!\n");
        exit(1);
    }

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

    fclose(fp);

    SDL_UnlockYUVOverlay(overlay);
    SDL_UnlockSurface(screen);

    rect.w = w;
    rect.h = h;
    rect.x = rect.y = 0;
    SDL_DisplayYUVOverlay(overlay, &rect);

    SDL_Delay(2000);

        
    

    free(pY);
    free(pU);
    free(pV);

    while (c != 'q')
        scanf("%c", &c);

    SDL_FreeYUVOverlay(overlay);
    SDL_FreeSurface(screen);

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值