I have used SDL
to save the window image via SDL_SaveBMP
. The problem is that the saved image is upside down.
The saved image is
while it has to be
How should I fix the code?
screen_shot function:
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);
}
Update
I try to flip the pixels. The result is a pure black output image. What is the problem?
#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;
}