glx-multithread-texture: Avoid front-buffer rendering.
[piglit.git] / tests / util / piglit-util-png.c
blobc63ec7b934cfc9be0925c33f914b0db8fa451fed
1 /*
2 * Copyright © 2007 Nicolai Haehnle
3 * Copyright © 2012 Intel Corporation
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
25 #include <stdarg.h>
26 #include <stdbool.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #ifdef PIGLIT_HAS_PNG
30 #include <png.h>
31 #endif
33 #include <piglit/gl_wrap.h>
35 #define aborts(s) _abortf("piglit_write_png: %s", s)
36 #define abortf(s, ...) _abortf("piglit_write_png" s, __VA_ARGS__)
37 static void
38 _abortf(const char *s, ...)
40 va_list args;
41 va_start(args, s);
42 vfprintf(stderr, s, args);
43 fprintf(stderr, "\n");
44 va_end(args);
45 abort();
48 /* Write a PNG file.
50 * \param filename The filename to write (i.e. "foo.png")
51 * \param base_format GL_RGBA or GL_RGB
52 * \param width The width of the image
53 * \param height The height of the image
54 * \param data The image data stored as unsigned bytes
55 * \param flip_y Whether to flip the image upside down (for FBO data)
57 void
58 piglit_write_png(const char *filename,
59 GLenum base_format,
60 int width,
61 int height,
62 GLubyte *data,
63 bool flip_y)
65 #ifndef PIGLIT_HAS_PNG
66 aborts("Piglit not built with libpng support.");
67 #else
68 FILE *fp;
69 png_structp png;
70 png_infop info;
71 GLubyte *row;
72 int bytes;
73 int color_type;
74 int y;
76 switch (base_format) {
77 case GL_RGBA:
78 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
79 bytes = 4;
80 break;
81 case GL_RGB:
82 color_type = PNG_COLOR_TYPE_RGB;
83 bytes = 3;
84 break;
85 default:
86 abortf("unknown format %04x", base_format);
87 break;
90 fp = fopen(filename, "wb");
91 if (!fp)
92 abortf("failed to open `%s'", filename);
94 png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
95 if (!png)
96 aborts("png_create_write_struct() failed");
98 info = png_create_info_struct(png);
99 if (!info)
100 aborts("png_create_info_struct failed");
102 if (setjmp(png_jmpbuf(png)))
103 aborts("png_init_io failed");
105 png_init_io(png, fp);
107 /* write header */
108 if (setjmp(png_jmpbuf(png)))
109 aborts("Write error");
111 png_set_IHDR(png, info, width, height,
112 8, color_type, PNG_INTERLACE_NONE,
113 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
115 png_write_info(png, info);
117 if (flip_y) {
118 row = data + (height * width * bytes);
119 for (y = 0; y < height; ++y) {
120 row -= width * bytes;
121 png_write_row(png, row);
123 } else {
124 row = data;
125 for (y = 0; y < height; ++y) {
126 png_write_row(png, row);
127 row += width * bytes;
131 png_write_end(png, 0);
133 fclose(fp);
134 #endif