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
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
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__)
38 _abortf(const char *s
, ...)
42 vfprintf(stderr
, s
, args
);
43 fprintf(stderr
, "\n");
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)
58 piglit_write_png(const char *filename
,
65 #ifndef PIGLIT_HAS_PNG
66 aborts("Piglit not built with libpng support.");
76 switch (base_format
) {
78 color_type
= PNG_COLOR_TYPE_RGB_ALPHA
;
82 color_type
= PNG_COLOR_TYPE_RGB
;
86 abortf("unknown format %04x", base_format
);
90 fp
= fopen(filename
, "wb");
92 abortf("failed to open `%s'", filename
);
94 png
= png_create_write_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
96 aborts("png_create_write_struct() failed");
98 info
= png_create_info_struct(png
);
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
);
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
);
118 row
= data
+ (height
* width
* bytes
);
119 for (y
= 0; y
< height
; ++y
) {
120 row
-= width
* bytes
;
121 png_write_row(png
, row
);
125 for (y
= 0; y
< height
; ++y
) {
126 png_write_row(png
, row
);
127 row
+= width
* bytes
;
131 png_write_end(png
, 0);