1 // Description:
2 //   Helper to save a SDL surface as a PNG.
3 //
4 // Copyright (C) 2001 Frank Becker
5 //
6 // This program is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free Software
8 // Foundation;  either version 2 of the License,  or (at your option) any  later
9 // version.
10 //
11 // This program is distributed in the hope that it will be useful,  but  WITHOUT
12 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
14 //
15 #include <string>
16 #include <png.h>
17 #include "SDL.h"
18 
19 using std::string;
20 
21 class PNG
22 {
23 public:
24     //Contruct with or without alpha
PNG(bool alpha=true)25     PNG( bool alpha=true):
26         _alpha( alpha)
27     {
28     }
29 
30     //Quick way to save snapshot (no alpha)
Snapshot(SDL_Surface * img,const string & filename)31     static bool Snapshot( SDL_Surface *img, const string &filename)
32     {
33         PNG png(false);
34         return png.Save( img, filename);
35     }
36 
37     //Save SDL surface as png
38     bool Save( SDL_Surface *img, const string &filename);
39 
40 private:
41     bool _alpha;
42     png_structp _png;
43     png_infop _info;
44 
45     bool init( FILE *fp, int width, int height);
46     void close( FILE *fp);
47     static void writeData( png_structp png, png_bytep data, png_size_t length);
48 };
49