OpenClonk
StdPNG.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2013-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 // png file reading functionality
17 
18 #ifndef INC_STDPNG
19 #define INC_STDPNG
20 
21 #include <png.h>
22 
23 class CPNGFile
24 {
25 private:
26  BYTE *pFile; // loaded file in mem
27  bool fpFileOwned; // whether file ptr was allocated by this class
28  int iFileSize; // size of file in mem
29  int iPixSize; // size of one pixel in image data mem
30  FILE *fp; // opened file for writing
31 
32  BYTE *pFilePtr; // current pos in file
33 
34  bool fWriteMode; // if set, the following png-structs are write structs
35  png_structp png_ptr; // png main struct
36  png_infop info_ptr, end_info; // png file info
37 
38  BYTE *pImageData; // uncompressed image in memory
39  int iRowSize; // size of one row of data (equals pitch)
40 
41  void Read(BYTE *pData, int iLength); // read from file
42  bool DoLoad(); // perform png-file loading after file data ptr has been set
43 public:
44  unsigned long iWdt, iHgt; // image size
45  int iBPC, iClrType, iIntrlcType, iCmprType, iFltrType; // image data info
46 public:
47  CPNGFile(); // ctor
48  ~CPNGFile(); // dtor
49 
50  void ClearPngStructs(); // clear internal png structs (png_tr, info_ptr etc.);
51  void Default(); // zero fields
52  void Clear(); // clear loaded file
53  bool Load(BYTE *pFile, int iSize); // load from file that is completely in mem
54  DWORD GetPix(int iX, int iY); // get pixel value (rgba) - note that NO BOUNDS CHECKS ARE DONE due to performance reasons!
55  // Use ONLY for PNG_COLOR_TYPE_RGB_ALPHA!
56  uint32_t * GetRow(int iY)
57  {
58  return reinterpret_cast<uint32_t *>(pImageData+iY*iRowSize);
59  }
60  bool Create(int iWdt, int iHgt, bool fAlpha); // create empty image
61  bool SetPix(int iX, int iY, DWORD dwValue); // set pixel value
62  bool Save(const char *szFilename); // save current image to file; saving to mem is not supported because C4Group doesn't support streamed writing anyway...
63 
64  BYTE *GetImageData() { return pImageData; } // return raw image data
65  int GetBitsPerPixel(); // return number of bits per pixel in raw image data
66 
67  static void ScheduleSaving(CPNGFile *png, const char *filename); // start a background thread to save the png file. then free the passed png.
68  static void WaitForSaves(); // wait until all pending saves are finished
69 
70 private:
71  static void PNGAPI CPNGReadFn(png_structp png_ptr, png_bytep data, size_t length); // reading proc (callback)
72 };
73 #endif //INC_STDPNG
uint8_t BYTE
uint32_t DWORD
int iSize
Definition: TstC4NetIO.cpp:32
void Clear()
Definition: StdPNG.cpp:136
bool Create(int iWdt, int iHgt, bool fAlpha)
Definition: StdPNG.cpp:191
void ClearPngStructs()
Definition: StdPNG.cpp:121
~CPNGFile()
Definition: StdPNG.cpp:115
int iCmprType
Definition: StdPNG.h:45
int iIntrlcType
Definition: StdPNG.h:45
uint32_t * GetRow(int iY)
Definition: StdPNG.h:56
unsigned long iHgt
Definition: StdPNG.h:44
int iBPC
Definition: StdPNG.h:45
int iClrType
Definition: StdPNG.h:45
int iFltrType
Definition: StdPNG.h:45
CPNGFile()
Definition: StdPNG.cpp:96
bool SetPix(int iX, int iY, DWORD dwValue)
Definition: StdPNG.cpp:216
BYTE * GetImageData()
Definition: StdPNG.h:64
bool Save(const char *szFilename)
Definition: StdPNG.cpp:236
static void ScheduleSaving(CPNGFile *png, const char *filename)
Definition: StdPNG.cpp:354
bool Load(BYTE *pFile, int iSize)
Definition: StdPNG.cpp:155
unsigned long iWdt
Definition: StdPNG.h:44
void Default()
Definition: StdPNG.cpp:101
DWORD GetPix(int iX, int iY)
Definition: StdPNG.cpp:175
static void WaitForSaves()
Definition: StdPNG.cpp:362
int GetBitsPerPixel()
Definition: StdPNG.cpp:292