Commit ed98ddeb authored by Nicolai Hähnle's avatar Nicolai Hähnle

texline: Write screenshot using libpng

parent 63213fc4
......@@ -4,6 +4,7 @@ project (piglit)
include(${CMAKE_ROOT}/Modules/FindOpenGL.cmake)
include(${CMAKE_ROOT}/Modules/FindTIFF.cmake)
include(${CMAKE_ROOT}/Modules/FindGLUT.cmake)
include(${CMAKE_ROOT}/Modules/FindPNG.cmake)
set(EXECUTABLE_OUTPUT_PATH ${piglit_SOURCE_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${piglit_SOURCE_DIR}/bin)
......
......@@ -31,7 +31,7 @@ First of all, you need to make sure that the following are installed:
- cmake (http://www.cmake.org)
- GL, glu and glut libraries and development packages (i.e. headers)
- X11 libraries and development packages (i.e. headers)
- libtiff
- libpng, libtiff and related development packages (i.e. headers)
Now configure the build system:
......
......@@ -10,14 +10,17 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <GL/glut.h>
#include "../util/readtex.c" /* I know, this is a hack. */
#include "../util/readtex.h"
#include "../util/writeimg.h"
#define TEXTURE_FILE "./tests/data/girl.rgb"
#define TEXTURE_FILE "mesa/girl.rgb"
static int Width = 400, Height = 300;
static int Automatic = 0;
static const char* ScreenshotFile = 0;
static void DoStar(int texture)
{
......@@ -55,7 +58,7 @@ static void DoStar(int texture)
glPopMatrix();
}
static void Display(void)
static void DoFrame(void)
{
int texture;
......@@ -96,7 +99,6 @@ static void Display(void)
glTranslatef(1, 0, 0);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_LINE_STIPPLE);
......@@ -112,6 +114,26 @@ static void Display(void)
glutSwapBuffers();
}
static void Screenshot(void)
{
GLubyte shot[400][300][3];
glReadBuffer(GL_FRONT);
glReadPixels(0, 0, 400, 300, GL_RGB, GL_UNSIGNED_BYTE, shot);
WritePNGImage(ScreenshotFile, GL_RGB, 400, 300, (GLubyte*)shot, 1);
}
static void Display(void)
{
DoFrame();
if (Automatic) {
Screenshot();
printf("PIGLIT: { }\n");
exit(0);
}
}
static void Reshape(int width, int height)
{
......@@ -142,6 +164,8 @@ static void Init( int argc, char *argv[] )
{
GLuint u;
for (u = 0; u < 2; u++) {
char filename[256];
glActiveTextureARB(GL_TEXTURE0_ARB + u);
glBindTexture(GL_TEXTURE_2D, 10+u);
if (u == 0)
......@@ -155,8 +179,16 @@ static void Init( int argc, char *argv[] )
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
if (getenv("DATADIR"))
snprintf(filename, sizeof(filename), "%s/%s",
getenv("DATADIR"), TEXTURE_FILE);
else
strcpy(filename, TEXTURE_FILE);
if (!LoadRGBMipmaps(filename, GL_RGB)) {
printf("Error: couldn't load texture image\n");
if (Automatic)
printf("PIGLIT: {'result': 'fail' }\n");
exit(1);
}
}
......@@ -175,6 +207,10 @@ static void Init( int argc, char *argv[] )
int main( int argc, char *argv[] )
{
glutInit( &argc, argv );
if (argc == 3 && !strcmp(argv[1], "-auto")) {
Automatic = 1;
ScreenshotFile = argv[2];
}
glutInitWindowPosition(0, 0);
glutInitWindowSize(Width, Height);
......
include_directories( ${OPENGL_INCLUDE_PATH} )
include_directories (
${OPENGL_INCLUDE_PATH}
${PNG_INCLUDE_DIR}
)
link_libraries (
${OPENGL_gl_LIBRARY}
${OPENGL_glu_LIBRARY}
${TIFF_LIBRARY}
${PNG_LIBRARIES}
)
add_definitions (
${PNG_DEFINITIONS}
)
add_library (mesautil
......@@ -18,4 +25,5 @@ add_library (mesautil
imagesgi.cpp
showbuffer.c
winpos.c
writeimg.c
)
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <png.h>
#include <GL/gl.h>
static void abortf(const char * s, ...)
{
va_list args;
va_start(args, s);
vfprintf(stderr, s, args);
fprintf(stderr, "\n");
va_end(args);
abort();
}
/**
* Write RGB or RGBA data to a PNG file.
* format must be GL_RGB or GL_RGBA.
*/
void WritePNGImage(const char* filename,
GLenum format, int width, int height, GLubyte* data, int reverse)
{
FILE* fp;
png_structp png;
png_infop info;
int bytes;
int colortype;
int y;
GLubyte* row;
if (format == GL_RGBA) {
colortype = PNG_COLOR_TYPE_RGB_ALPHA;
bytes = 4;
} else if (format == GL_RGB) {
colortype = PNG_COLOR_TYPE_RGB;
bytes = 3;
} else {
abortf("Unknown format %04x", format);
}
fp = fopen(filename, "wb");
if (!fp)
abortf("Failed to open %s", filename);
png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png)
abortf("png_create_write_struct failed");
info = png_create_info_struct(png);
if (!info)
abortf("png_create_info_struct failed");
if (setjmp(png_jmpbuf(png)))
abortf("png_init_io failed");
png_init_io(png, fp);
/* write header */
if (setjmp(png_jmpbuf(png)))
abortf("Write error");
png_set_IHDR(png, info, width, height,
8, colortype, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png, info);
if (reverse)
row = data + (height*width*bytes);
else
row = data;
for(y = 0; y < height; ++y) {
if (reverse)
row -= width*bytes;
png_write_row(png, row);
if (!reverse)
row += width*bytes;
}
png_write_end(png, 0);
fclose(fp);
}
#ifndef writeimg_h
#define writeimg_h
void WritePNGImage(const char* filename,
GLenum format, int width, int height, GLubyte* data, int reverse);
#endif /* writeimg_h */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment