如何在SDL中打印16位灰度图像?

时间:2016-03-02 14:22:43

标签: c image sdl grayscale

我的结构包含来自科学相机的16位灰度图像,结构如下:

struct qscam_image {
    int row;
    int col;
    int max_gray; // this should be 65535
    unsigned short *data;
};

然后我有程序应该每秒钟显示更新的灰度图像(取决于从相机传输数据所花费的时间)。

#include <GL/gl.h>
#include <GL/glu.h>
#include "qscapi.h" 
#include <SDL/SDL.h>

static void render(SDL_Surface * sf)
{
    SDL_Surface * screen = SDL_GetVideoSurface();
    if(SDL_BlitSurface(sf, NULL, screen, NULL) == 0)
        SDL_UpdateRect(screen, 0, 0, 0, 0);
}

int main(int argc, char **argv) 
{
//initialize camera
    struct qscam_image *image = NULL;
    qscam_create_image(&image);
    qscam_init();

//initialize SDL
    SDL_Event ev;
    int active;
    /* Initialize SDL */
    if(SDL_Init(SDL_INIT_VIDEO) != 0)
        fprintf(stderr,"Could not initialize SDL: %s\n",SDL_GetError());
    SDL_SetVideoMode(1900, 1080, 24, SDL_HWSURFACE);
    /* Main loop */
    active = 1;
    while(active)
    {
        /* Handle events */
        while(SDL_PollEvent(&ev))
        {
            if(ev.type == SDL_QUIT)
                active = 0; /* End */
        }
        qscam_get_image(0.030, &image); //get image from camera

        /* create the image variable */
        Uint32 mask = 0xff00; // gray
        SDL_Surface* print_image = SDL_CreateRGBSurfaceFrom
            (image->data, image->col, image->row,
            16, image->col*2, mask, mask, mask, 0);

        render(print_image);
    }
    /* Exit */
    qscam_disconnect();
    qscam_destroy_image(&image);
    SDL_Quit();
    return 0;
}

它在某种程度上起作用,mask=0x00ff它显示随机噪声,但这只是8位图像。使用mask=0xffff时,不会显示任何内容,并且会再次显示mask=0xff00图像,但这会导致我认为图像矩阵应该被转置。

第一个问题:使用SDL和c显示16位灰度图像(在内存中保存为unsigned short[])的正确方法是什么?

第二个问题:有没有简单的方法可以用SDL转置图像矩阵,还是必须编写单独的函数?

0 个答案:

没有答案
相关问题