【发布时间】:2017-05-17 13:39:14
【问题描述】:
我使用SDL 通过SDL_SaveBMP 保存窗口图像。问题是保存的图片是上下颠倒的。
保存的图片是
这是必须的
我应该如何修复代码?
screen_shot 函数:
void screen_shot(std::string filename)
{
int width = glutGet(GLUT_WINDOW_WIDTH);
int height = glutGet(GLUT_WINDOW_HEIGHT);
SDL_Surface * image = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
glReadBuffer(GL_FRONT);
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, image->pixels);
SDL_SaveBMP(image, filename.c_str());
SDL_FreeSurface(image);
}
更新
我尝试翻转像素。结果是纯黑色输出图像。有什么问题?
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <stdio.h>
#include <assert.h>
#include <iostream>
#include <GL/freeglut.h>
#include <boost/thread/thread.hpp> // for sleep
#include <SDL2/SDL.h>
Uint32 GetPixel24(SDL_Surface * surface,int x, int y)
{
Uint32 rgb;
Uint8 * pixel = (Uint8*)surface->pixels;
pixel += (y * surface->pitch) + (x * sizeof(Uint8) * 3);
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rgb=(pixel[2] << 8)+(pixel[1] << 16)+(pixel[0] << 24);
#else
rgb=(pixel[2] << 16)+(pixel[1] << 8)+(pixel[0]);
std::cout<<int(pixel[0])<<","<<int(pixel[1])<<","<<int(pixel[2])<<std::endl;
#endif
return rgb;
}
void PutPixel24_nolock(SDL_Surface * surface, int x, int y, Uint32 color)
{
Uint8 * pixel = (Uint8*)surface->pixels;
pixel += (y * surface->pitch) + (x * sizeof(Uint8) * 3);
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
pixel[0] = (color >> 24) & 0xFF;
pixel[1] = (color >> 16) & 0xFF;
pixel[2] = (color >> 8) & 0xFF;
#else
pixel[0] = color & 0xFF;
pixel[1] = (color >> 8) & 0xFF;
pixel[2] = (color >> 16) & 0xFF;
#endif
}
void PutPixel24(SDL_Surface * surface, int x, int y, Uint32 rgb)
{
if(SDL_MUSTLOCK(surface))
SDL_LockSurface(surface);
PutPixel24_nolock(surface,x,y,rgb);
if(SDL_MUSTLOCK(surface))
SDL_LockSurface(surface);
}
SDL_Surface *flip_vertical_surface(SDL_Surface *surface)
{
SDL_Surface *flipped = nullptr;
flipped = SDL_CreateRGBSurface( SDL_SWSURFACE, surface->w, surface->h, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
for(int x=0;x<surface->w;x++)
for(int y=0;y<surface->h;y++)
{
int ry=(surface->h)-1-y;
Uint32 pixel=GetPixel24(surface,x,y);
PutPixel24(flipped,x,ry,pixel);
}
return flipped;
}
void screen_shot(std::string filename)
{
int width = glutGet(GLUT_WINDOW_WIDTH);
int height = glutGet(GLUT_WINDOW_HEIGHT);
SDL_Surface * image = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
SDL_Surface *image_fliped=flip_vertical_surface(image);
if(image_fliped==nullptr)
std::cout<<"image_fliped must be different than nullptr"<<std::endl;
glReadBuffer(GL_FRONT);
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, image->pixels);
SDL_SaveBMP(image_fliped, filename.c_str());
SDL_FreeSurface(image);
SDL_FreeSurface(image_fliped);
}
void cback_render()
{
if(!glutGetWindow())
return ;
static float rotations = 0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(rotations, 0, 0, 1);
glBegin(GL_TRIANGLES);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glVertex3f(0,1,0);
glEnd();
glutSwapBuffers();
}
void timer(int )
{
if(!glutGetWindow())
return ;
static bool saved=false;
if(!saved)
{
screen_shot("image.bmp");
saved=true;
}
glutPostRedisplay();
glutMainLoopEvent();
glutTimerFunc(30, timer, 1);
}
void init()
{
int argc=1;
glutInit(&argc, nullptr);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(100, 100);
glutInitWindowSize(512, 512);
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
glutCreateWindow("freegluttest");
glutDisplayFunc (cback_render);
glutTimerFunc(30, timer, 1);
}
int main()
{
init();
glutMainLoop();
return 0;
}
【问题讨论】:
-
afaik OGL 坐标系通常使用左下角作为原点,而 BMP 使用右上角 - 您必须要么符合后者坐标系,要么在保存之前翻转图像以获取图片对。
-
@Steeve,我是 opengl 的新手。我应该如何通过代码做到这一点?
-
这是一个可以翻转SDL图像的函数:lazyfoo.net/SDL_tutorials/lesson31/index.php
-
当你说“保存的图像是[...]”时,你是如何验证这一点的?你用什么工具来显示它?这个工具是否尊重,位图是否为top-down or bottom-up?默认情况下,Windows GDI 位图是自下而上的。
-
@Steeve,此网址不适用于 24 位颜色。