1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
/*
* Save a CL_Surface in PNG format
*
* usage:
* ...
* CL_Surface surface( "gfx/image1", &res );
* WritePNG::write_png( &surface , "image.png" )
* ...
*
* This uses some code from Greg Roelofs wpng program (included in libpng)
* hence the following copyright notice.
*/
/*---------------------------------------------------------------------------
wpng - simple PNG-writing program
---------------------------------------------------------------------------
Copyright (c) 1998-2000 Greg Roelofs. All rights reserved.
This software is provided "as is," without warranty of any kind,
express or implied. In no event shall the author or contributors
be held liable for any damages arising in any way from the use of
this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute
it freely, subject to the following restrictions:
1. Redistributions of source code must retain the above copyright
notice, disclaimer, and this list of conditions.
2. Redistributions in binary form must reproduce the above copyright
notice, disclaimer, and this list of conditions in the documenta-
tion and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this
software must display the following acknowledgment:
This product includes software developed by Greg Roelofs
and contributors for the book, "PNG: The Definitive Guide,"
published by O'Reilly and Associates.
---------------------------------------------------------------------------*/
#include <ClanLib/display.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <setjmp.h> /* for jmpbuf declaration in writepng.h */
#include <time.h>
#include <iostream>
#include "writepng.h"
#include "cl_writepng.h"
mainprog_info WritePNG::wpng_info;
void WritePNG::write_png( CL_PixelBuffer pbuf, std::string path )
{
pbuf.lock();
int rc = 0;
int error = 0;
wpng_info.width = pbuf.get_width();
wpng_info.height = pbuf.get_height();
wpng_info.sample_depth = 8;
CL_PixelFormat format = pbuf.get_format();
wpng_info.bpp = format.get_depth();
wpng_info.outfile = NULL;
wpng_info.image_data = NULL;
wpng_info.row_pointers = NULL;
wpng_info.interlaced = false;
wpng_info.have_bg = false;
wpng_info.have_time = false;
wpng_info.have_text = 0;
wpng_info.gamma = 1.0;
/* open the output file, or register an error and abort */
if ((wpng_info.outfile = fopen( path.c_str(), "wb") ) == NULL)
{
std::cout << "couldn't open file \"" << path.c_str() << "\" for writing" << std::endl;
++error;
}
// allocate libpng stuff, initialize transformations, write pre-IDAT data
if ((rc = writepng_init(&wpng_info)) != 0)
{
switch (rc)
{
case 2:
fprintf(stderr, "WritePNG: libpng initialization problem (longjmp)\n");
break;
case 4:
fprintf(stderr, "WritePNG: insufficient memory\n");
break;
case 11:
fprintf(stderr, "WritePNG: internal logic error (unexpected PNM type)\n");
break;
default:
fprintf(stderr, "WritePNG: unknown writepng_init() error\n");
break;
}
exit(rc);
}
std::cout << "Encoding image data...\n";
long j;
wpng_info.image_data = (unsigned char *)malloc( pbuf.get_pitch() );
if (wpng_info.image_data == NULL)
{
fprintf(stderr, "WritePNG: insufficient memory for row data\n");
writepng_cleanup(&wpng_info);
cleanup();
exit(5);
}
error = 0;
unsigned char *data = (unsigned char*)pbuf.get_data();
for (j = wpng_info.height; j > 0L; --j)
{
memcpy( wpng_info.image_data, data, pbuf.get_pitch() );
data += pbuf.get_pitch();
unsigned char* img_data = wpng_info.image_data;
int i=0;
if( pbuf.get_format().get_depth() == 24 )
{
for( i=0; i < wpng_info.width; i++ )
{
// swap red and blue
unsigned char tmp = img_data[0];
img_data[0] = img_data[2];
img_data[2] = tmp;
img_data += 3;
}
}
else if( pbuf.get_format().get_depth() == 32 )
{
for( i=0; i < wpng_info.width; i++ )
{
// cl format: ARGB, PNG format: BGRA
unsigned char tmp[4] = {img_data[3], img_data[2], img_data[1], img_data[0]};
img_data[0] = tmp[0];
img_data[1] = tmp[1];
img_data[2] = tmp[2];
img_data[3] = tmp[3];
img_data += 4;
}
}
else
{
std::cout << "WritePNG: unsupported bit depth: " << pbuf.get_format().get_depth() << std::endl;
}
if( writepng_encode_row(&wpng_info) != 0)
{
fprintf(stderr, "WritePNG: libpng problem (longjmp) while writing row %ld\n",
wpng_info.height-j);
++error;
break;
}
}
if (error)
{
writepng_cleanup(&wpng_info);
cleanup();
exit(2);
}
if (writepng_encode_finish(&wpng_info) != 0)
{
fprintf(stderr, "WritePNG: error on final libpng call\n");
writepng_cleanup(&wpng_info);
cleanup();
exit(2);
}
/* OK, we're done (successfully): clean up all resources and quit */
pbuf.unlock();
fprintf(stderr, "Done.\n");
fflush(stderr);
writepng_cleanup(&wpng_info);
cleanup();
}
void WritePNG::cleanup()
{
if (wpng_info.outfile)
{
fclose(wpng_info.outfile);
wpng_info.outfile = NULL;
}
if (wpng_info.image_data)
{
free(wpng_info.image_data);
wpng_info.image_data = NULL;
}
}
|