[Piglit] [PATCH 1/2] util: Add a piglit_write_png() function.
Kenneth Graunke
kenneth at whitecape.org
Thu May 14 12:25:40 PDT 2015
This reintroduces libpng support, but makes it optional since I only
plan to use it in an optional feature most people won't use.
This code is based on:
commit ed98ddebb21483c40325aaa4d1caecf3860a9906
Author: Nicolai Haehnle <nhaehnle at gmail.com>
Date: Sun Mar 25 22:48:07 2007 +0200
texline: Write screenshot using libpng
Signed-off-by: Kenneth Graunke <kenneth at whitecape.org>
Cc: ben at bwidawsk.net
---
CMakeLists.txt | 5 +-
tests/util/CMakeLists.txt | 6 ++
tests/util/piglit-util-gl.h | 3 +
tests/util/piglit-util-png.c | 134 +++++++++++++++++++++++++++++++++++++++++++
4 files changed, 147 insertions(+), 1 deletion(-)
create mode 100644 tests/util/piglit-util-png.c
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2208dc7..3474f46 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -25,13 +25,16 @@ if (MSVC)
endif ()
find_package(Threads)
+find_package(PNG)
+if(PNG_FOUND)
+ add_definitions(-DPIGLIT_HAS_PNG)
+endif(PNG_FOUND)
find_package(X11)
if(X11_FOUND)
set(PIGLIT_HAS_X11 True)
add_definitions(-DPIGLIT_HAS_X11)
endif()
-
option(PIGLIT_BUILD_GL_TESTS "Build tests for OpenGL" ON)
option(PIGLIT_BUILD_GLES1_TESTS "Build tests for OpenGL ES1" OFF)
option(PIGLIT_BUILD_GLES2_TESTS "Build tests for OpenGL ES2" OFF)
diff --git a/tests/util/CMakeLists.txt b/tests/util/CMakeLists.txt
index 45fd25f..fb22ffa 100644
--- a/tests/util/CMakeLists.txt
+++ b/tests/util/CMakeLists.txt
@@ -15,6 +15,11 @@ if(HAVE_LIBCACA)
link_libraries(caca)
endif()
+if(PNG_FOUND)
+ link_libraries(${PNG_LIBRARIES})
+ include_directories(${PNG_INCLUDE_DIRS})
+endif(PNG_FOUND)
+
set(UTIL_INCLUDES
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}
@@ -41,6 +46,7 @@ set(UTIL_GL_SOURCES
piglit-matrix.c
piglit-test-pattern.cpp
piglit-util-gl.c
+ piglit-util-png.c
piglit-vbo.cpp
piglit-framework-gl.c
piglit-framework-gl/piglit_gl_framework.c
diff --git a/tests/util/piglit-util-gl.h b/tests/util/piglit-util-gl.h
index 2196e2c..3d32d40 100644
--- a/tests/util/piglit-util-gl.h
+++ b/tests/util/piglit-util-gl.h
@@ -275,6 +275,9 @@ static const GLint PIGLIT_ATTRIB_TEX = 1;
unsigned
required_gl_version_from_glsl_version(unsigned glsl_version);
+void
+piglit_write_png(const char *filename, GLenum base_format,
+ int width, int height, GLubyte *data, bool flip_y);
#ifdef __cplusplus
} /* end extern "C" */
diff --git a/tests/util/piglit-util-png.c b/tests/util/piglit-util-png.c
new file mode 100644
index 0000000..1b73bab
--- /dev/null
+++ b/tests/util/piglit-util-png.c
@@ -0,0 +1,134 @@
+/*
+ * Copyright © 2007 Nicolai Haehnle
+ * Copyright © 2012 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <GL/gl.h>
+#ifdef PIGLIT_HAS_PNG
+#include <png.h>
+#endif
+
+#define aborts(s) _abortf("piglit_write_png: %s", s)
+#define abortf(s, ...) _abortf("piglit_write_png" s, __VA_ARGS__)
+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 a PNG file.
+ *
+ * \param filename The filename to write (i.e. "foo.png")
+ * \param base_format GL_RGBA or GL_RGB
+ * \param width The width of the image
+ * \param height The height of the image
+ * \param data The image data stored as unsigned bytes
+ * \param flip_y Whether to flip the image upside down (for FBO data)
+ */
+void
+piglit_write_png(const char *filename,
+ GLenum base_format,
+ int width,
+ int height,
+ GLubyte *data,
+ bool flip_y)
+{
+#ifndef PIGLIT_HAS_PNG
+ aborts("Piglit not built with libpng support.");
+#else
+ FILE *fp;
+ png_structp png;
+ png_infop info;
+ GLubyte *row;
+ int bytes;
+ int color_type;
+ int y;
+
+ switch (base_format) {
+ case GL_RGBA:
+ color_type = PNG_COLOR_TYPE_RGB_ALPHA;
+ bytes = 4;
+ break;
+ case GL_RGB:
+ color_type = PNG_COLOR_TYPE_RGB;
+ bytes = 3;
+ break;
+ default:
+ abortf("unknown format %04x", base_format);
+ break;
+ }
+
+ fp = fopen(filename, "w");
+ if (!fp)
+ abortf("failed to open `%s'", filename);
+
+ png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+ if (!png)
+ aborts("png_create_write_struct() failed");
+
+ info = png_create_info_struct(png);
+ if (!info)
+ aborts("png_create_info_struct failed");
+
+ if (setjmp(png_jmpbuf(png)))
+ aborts("png_init_io failed");
+
+ png_init_io(png, fp);
+
+ /* write header */
+ if (setjmp(png_jmpbuf(png)))
+ aborts("Write error");
+
+ png_set_IHDR(png, info, width, height,
+ 8, color_type, PNG_INTERLACE_NONE,
+ PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
+
+ png_write_info(png, info);
+
+ if (flip_y) {
+ row = data + (height * width * bytes);
+ for (y = 0; y < height; ++y) {
+ row -= width * bytes;
+ png_write_row(png, row);
+ }
+ } else {
+ row = data;
+ for (y = 0; y < height; ++y) {
+ png_write_row(png, row);
+ row += width * bytes;
+ }
+ }
+
+ png_write_end(png, 0);
+
+ fclose(fp);
+#endif
+}
--
2.4.0
More information about the Piglit
mailing list