测试YUV

#include <iostream>
#include <SDL.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
/*
 * Lesson 0: Test to make sure SDL is setup properly
 */
#undef main
 
 
const int SCREEN_WIDTH  = 640;
const int SCREEN_HEIGHT = 480;
//We'll just be using square tiles for now
const int TILE_SIZE = 40;
 
 
void logSDLError(std::ostream &os, const std::string &msg)
{
    os << msg << " error: " << SDL_GetError() << std::endl;
}
static void fill_yuv_image(uint8_t *data[4], int linesize[4],
                           int width, int height, int frame_index)
{
    int x, y;
 
 
    /* Y */
    for (y = 0; y < height; y++)
        for (x = 0; x < width; x++)
            data[0][y * linesize[0] + x] = x + y + frame_index * 3;
 
 
    /* Cb and Cr */
    for (y = 0; y < height / 2; y++) {
        for (x = 0; x < width / 2; x++) {
            data[1][y * linesize[1] + x] = 128 + y + frame_index * 2;
            data[2][y * linesize[2] + x] = 64 + x + frame_index * 5;
        }
    }
}
 
 
int main(int, char**){
 
 
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
        logSDLError(std::cout, "SDL_Init");
        return 1;
    }
 
 
    SDL_Window *window = SDL_CreateWindow("Lesson 2", 100, 100, SCREEN_WIDTH,
        SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
    if (window == nullptr){
        logSDLError(std::cout, "CreateWindow");
        SDL_Quit();
        return 1;
    }
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1,
        SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    if (renderer == nullptr){
        logSDLError(std::cout, "CreateRenderer");
        SDL_DestroyWindow(window);
        SDL_Quit();
        return 1;
    }
 
 
    SDL_RenderClear( renderer );
    SDL_RenderPresent( renderer );
    //SDL_Delay(3000);
#if 1
    int pic_h = 300;
    int pic_w = 300;
    SDL_Texture* sdlTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING,pic_w,pic_h);
 
 
 
 
    uint8_t *data[4];
    uint8_t * yuv_data = (uint8_t*)malloc(pic_h*pic_w*2);
 
 
    data[0] = yuv_data ; //(uint8_t*)malloc(pic_h*pic_w*4);    // y
    data[1] = yuv_data + pic_w*pic_h;//(uint8_t*)malloc(pic_h*pic_w);    //u (pic_h/2)*(pic_w/2)
    data[2] = yuv_data + pic_w*pic_h + (pic_w*pic_h)/4; //(uint8_t*)malloc(pic_h*pic_w);    //v  (pic_h/2)*(pic_w/2)
    data[3] = (uint8_t*)malloc(pic_h*pic_w);   // 0
    int linesize[4];
    linesize[0] = pic_w;
    linesize[1] = pic_w/2;
    linesize[2] = pic_w/2;
    linesize[3] = 0;
 
 
    fill_yuv_image(data,linesize,pic_w,pic_h,0);
    cout<< "eee"<<endl;
    SDL_UpdateTexture(sdlTexture,NULL,yuv_data ,pic_w);
    cout<< "fff"<<endl;
    SDL_RenderCopy( renderer, sdlTexture, NULL, NULL);
 
 
    SDL_RenderPresent( renderer );
    SDL_Delay(3000);
#endif
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    std::cout<<"ok"<<std::endl;
    return 0;
}

转载于:https://www.cnblogs.com/wxmcu/p/8728445.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值