OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
CPNGFile Class Reference

#include <StdPNG.h>

Public Member Functions

 CPNGFile ()
 
 ~CPNGFile ()
 
void ClearPngStructs ()
 
void Default ()
 
void Clear ()
 
bool Load (BYTE *pFile, int iSize)
 
DWORD GetPix (int iX, int iY)
 
uint32_t * GetRow (int iY)
 
bool Create (int iWdt, int iHgt, bool fAlpha)
 
bool SetPix (int iX, int iY, DWORD dwValue)
 
bool Save (const char *szFilename)
 
BYTEGetImageData ()
 
int GetBitsPerPixel ()
 

Static Public Member Functions

static void ScheduleSaving (CPNGFile *png, const char *filename)
 
static void WaitForSaves ()
 

Public Attributes

unsigned long iWdt
 
unsigned long iHgt
 
int iBPC
 
int iClrType
 
int iIntrlcType
 
int iCmprType
 
int iFltrType
 

Detailed Description

Definition at line 23 of file StdPNG.h.

Constructor & Destructor Documentation

CPNGFile::CPNGFile ( )

Definition at line 96 of file StdPNG.cpp.

References Default().

97 {
98  Default();
99 }
void Default()
Definition: StdPNG.cpp:101

Here is the call graph for this function:

CPNGFile::~CPNGFile ( )

Definition at line 115 of file StdPNG.cpp.

References Clear().

116 {
117  // clear
118  Clear();
119 }
void Clear()
Definition: StdPNG.cpp:136

Here is the call graph for this function:

Member Function Documentation

void CPNGFile::Clear ( )

Definition at line 136 of file StdPNG.cpp.

References ClearPngStructs().

Referenced by Create(), Load(), Save(), and ~CPNGFile().

137 {
138  // free image data
139  if (pImageData) { delete [] pImageData; pImageData=nullptr; }
140  // clear internal png ptrs
141  ClearPngStructs();
142  // free file ptr if owned
143  if (pFile && fpFileOwned) delete [] pFile; pFile=nullptr;
144  // reset fields
145  fpFileOwned=false;
146  pFilePtr=nullptr;
147  iRowSize=0;
148  iPixSize=0;
149  // close file if open
150  if (fp) { fclose(fp); fp=nullptr; }
151 }
void ClearPngStructs()
Definition: StdPNG.cpp:121

Here is the call graph for this function:

Here is the caller graph for this function:

void CPNGFile::ClearPngStructs ( )

Definition at line 121 of file StdPNG.cpp.

Referenced by Clear(), and Save().

122 {
123  // clear internal png ptrs
124  if (png_ptr || info_ptr || end_info)
125  {
126  if (fWriteMode)
127  png_destroy_write_struct(&png_ptr, &info_ptr);
128  else
129  png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
130  }
131  png_ptr=nullptr;
132  info_ptr=end_info=nullptr;
133  fWriteMode=false;
134 }

Here is the caller graph for this function:

bool CPNGFile::Create ( int  iWdt,
int  iHgt,
bool  fAlpha 
)

Definition at line 189 of file StdPNG.cpp.

References Clear(), iBPC, iClrType, iCmprType, iFltrType, iHgt, iIntrlcType, and iWdt.

190 {
191  // catch invalid size
192  if (iWdt<=0 || iHgt<=0) return false;
193  // clear current image in mem
194  Clear();
195  // set dimensions
196  this->iWdt=iWdt; this->iHgt=iHgt;
197  // set color type
198  iBPC = 8;
199  iClrType = fAlpha ? PNG_COLOR_TYPE_RGB_ALPHA : PNG_COLOR_TYPE_RGB;
200  iPixSize = fAlpha ? 4 : 3;
201  // set interlacing, filters and stuff
202  iIntrlcType = PNG_INTERLACE_NONE;
203  iCmprType = PNG_COMPRESSION_TYPE_DEFAULT;
204  iFltrType = PNG_FILTER_TYPE_DEFAULT;
205  // calculate rowbytes
206  int iBPP = (fAlpha ? 4 : 3) * iBPC;
207  iRowSize = (iBPP*iWdt+7)>>3;
208  // create image data
209  pImageData = new unsigned char[iHgt * iRowSize];
210  // success
211  return true;
212 }
int iBPC
Definition: StdPNG.h:45
int iIntrlcType
Definition: StdPNG.h:45
int iCmprType
Definition: StdPNG.h:45
void Clear()
Definition: StdPNG.cpp:136
int iClrType
Definition: StdPNG.h:45
unsigned long iHgt
Definition: StdPNG.h:44
unsigned long iWdt
Definition: StdPNG.h:44
int iFltrType
Definition: StdPNG.h:45

Here is the call graph for this function:

void CPNGFile::Default ( )

Definition at line 101 of file StdPNG.cpp.

Referenced by CPNGFile().

102 {
103  // zero fields
104  pFile=nullptr;
105  fpFileOwned=false;
106  pFilePtr=nullptr;
107  png_ptr=nullptr;
108  info_ptr=end_info=nullptr;
109  pImageData=nullptr;
110  iRowSize=0;
111  iPixSize=0;
112  fp=nullptr;
113 }

Here is the caller graph for this function:

int CPNGFile::GetBitsPerPixel ( )

Definition at line 290 of file StdPNG.cpp.

References iClrType.

291 {
292  switch (iClrType)
293  {
294  case PNG_COLOR_TYPE_RGB: return 24;
295  case PNG_COLOR_TYPE_RGB_ALPHA: return 32;
296  }
297  return 0;
298 }
int iClrType
Definition: StdPNG.h:45
BYTE* CPNGFile::GetImageData ( )
inline

Definition at line 64 of file StdPNG.h.

64 { return pImageData; } // return raw image data
DWORD CPNGFile::GetPix ( int  iX,
int  iY 
)

Definition at line 173 of file StdPNG.cpp.

References C4RGB, iClrType, and RGBA().

Referenced by C4TextureShape::Load(), C4SolidMask::LoadMaskFromFile(), and C4Surface::ReadPNG().

174 {
175  // image loaded?
176  if (!pImageData) return 0;
177  // return pixel value
178  unsigned char *pPix=pImageData+iY*iRowSize+iX*iPixSize;
179  switch (iClrType)
180  {
181  case PNG_COLOR_TYPE_RGB:
182  return C4RGB(pPix[2], pPix[1], pPix[0]);
183  case PNG_COLOR_TYPE_RGB_ALPHA:
184  return RGBA(pPix[2], pPix[1], pPix[0], pPix[3]);
185  }
186  return 0;
187 }
int iClrType
Definition: StdPNG.h:45
#define C4RGB(r, g, b)
Definition: StdColors.h:26
uint32_t RGBA(uint32_t r, uint32_t g, uint32_t b, uint32_t a)
Definition: StdColors.h:22

Here is the call graph for this function:

Here is the caller graph for this function:

uint32_t* CPNGFile::GetRow ( int  iY)
inline

Definition at line 56 of file StdPNG.h.

Referenced by C4Surface::ReadPNG().

57  {
58  return reinterpret_cast<uint32_t *>(pImageData+iY*iRowSize);
59  }

Here is the caller graph for this function:

bool CPNGFile::Load ( BYTE pFile,
int  iSize 
)

Definition at line 153 of file StdPNG.cpp.

References Clear(), and iSize.

Referenced by C4TextureShape::Load(), C4SolidMask::LoadMaskFromFile(), and C4Surface::ReadPNG().

154 {
155  // clear any previously loaded file
156  Clear();
157  // store file ptr as not owned
158  this->pFile = pFile;
159  iFileSize=iSize;
160  fpFileOwned=false;
161  // perform the loading
162  if (!DoLoad())
163  {
164  Clear();
165  return false;
166  }
167  // reset file-field
168  this->pFile = nullptr; iFileSize=0;
169  // success
170  return true;
171 }
void Clear()
Definition: StdPNG.cpp:136
int iSize
Definition: TstC4NetIO.cpp:32

Here is the call graph for this function:

Here is the caller graph for this function:

bool CPNGFile::Save ( const char *  szFilename)

Definition at line 234 of file StdPNG.cpp.

References Clear(), ClearPngStructs(), iBPC, iClrType, iCmprType, iFltrType, iHgt, iIntrlcType, and iWdt.

235 {
236  // regular file saving - first, there has to be a buffer
237  if (!pImageData) return false;
238  // open the file
239  fp = fopen(szFilename, "wb"); if (!fp) return false;
240  // clear any previously initialized png-structs (e.g. by reading)
241  ClearPngStructs();
242  // reinit them for writing
243  fWriteMode=true;
244  png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
245  if (!png_ptr) { Clear(); return false; }
246  info_ptr = png_create_info_struct(png_ptr);
247  if (!info_ptr) { Clear(); return false; }
248  // error handling
249  if (setjmp(png_jmpbuf(png_ptr))) { Clear(); return false; }
250  // io initialization
251  png_init_io(png_ptr, fp);
252  // compression stuff
253  png_set_filter(png_ptr, 0, PNG_FILTER_NONE | PNG_FILTER_SUB | PNG_FILTER_PAETH);
254  png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
255  png_set_compression_mem_level(png_ptr, 8);
256  png_set_compression_strategy(png_ptr, Z_DEFAULT_STRATEGY);
257  png_set_compression_window_bits(png_ptr, 15);
258  png_set_compression_method(png_ptr, 8);
259  // set header
260  png_set_IHDR(png_ptr, info_ptr, iWdt, iHgt, iBPC, iClrType, iIntrlcType, iCmprType, iFltrType);
261  // double-check our calculated row size
262  int iRealRowSize=png_get_rowbytes(png_ptr, info_ptr);
263  if (iRealRowSize != iRowSize)
264  {
265  // this won't go well, so better abort
266  Clear(); return false;
267  }
268  // write png header
269  png_write_info(png_ptr, info_ptr);
270  // image data is given as bgr...
271  png_set_bgr(png_ptr);
272  // create row array
273  unsigned char **ppRowBuf = new unsigned char *[iHgt];
274  unsigned char **ppRows=ppRowBuf; unsigned char *pRow=pImageData;
275  for (unsigned int i=0; i<iHgt; ++i,pRow+=iRowSize) *ppRows++=pRow;
276  // write image
277  png_write_image(png_ptr, ppRowBuf);
278  // free row buffer
279  delete [] ppRowBuf;
280  // write end struct
281  png_write_end(png_ptr, info_ptr);
282  // finally, close the file
283  fclose(fp); fp = nullptr;
284  // clear png structs
285  ClearPngStructs();
286  // success!
287  return true;
288 }
int iBPC
Definition: StdPNG.h:45
int iIntrlcType
Definition: StdPNG.h:45
int iCmprType
Definition: StdPNG.h:45
void Clear()
Definition: StdPNG.cpp:136
void ClearPngStructs()
Definition: StdPNG.cpp:121
int iClrType
Definition: StdPNG.h:45
unsigned long iHgt
Definition: StdPNG.h:44
unsigned long iWdt
Definition: StdPNG.h:44
int iFltrType
Definition: StdPNG.h:45

Here is the call graph for this function:

void CPNGFile::ScheduleSaving ( CPNGFile png,
const char *  filename 
)
static

Definition at line 352 of file StdPNG.cpp.

References StdThread::Start().

Referenced by C4GraphicsSystem::DoSaveScreenshot(), and C4Surface::SavePNG().

353 {
354  // start a background thread to save the png file
355  // thread is responsible for cleaning up
356  CPNGSaveThread *saver = new CPNGSaveThread(png, filename);
357  if (!saver->Start()) delete saver;
358 }
bool Start()

Here is the call graph for this function:

Here is the caller graph for this function:

bool CPNGFile::SetPix ( int  iX,
int  iY,
DWORD  dwValue 
)

Definition at line 214 of file StdPNG.cpp.

References GetBlueValue, GetGreenValue, GetRedValue, and iClrType.

215 {
216  // image created?
217  if (!pImageData) return false;
218  // set pixel value
219  unsigned char *pPix=pImageData+iY*iRowSize+iX*iPixSize;
220  switch (iClrType)
221  {
222  case PNG_COLOR_TYPE_RGB: // RGB: set r, g and b values
223  pPix[0] = GetBlueValue(dwValue);
224  pPix[1] = GetGreenValue(dwValue);
225  pPix[2] = GetRedValue(dwValue);
226  return true;
227  case PNG_COLOR_TYPE_RGB_ALPHA: // RGBA: simply set in mem
228  *(DWORD *) pPix = dwValue;
229  return true;
230  }
231  return false;
232 }
#define GetGreenValue(rgb)
Definition: StdColors.h:28
#define GetRedValue(rgb)
Definition: StdColors.h:29
int iClrType
Definition: StdPNG.h:45
#define GetBlueValue(rgb)
Definition: StdColors.h:27
uint32_t DWORD
void CPNGFile::WaitForSaves ( )
static

Definition at line 360 of file StdPNG.cpp.

References CPNGSaveThread::HasPendingThreads(), and LogSilent().

Referenced by C4Application::Clear().

361 {
362  // Yield main thread until all pending saves have finished.Wait for
363  bool first = true;
365  {
366  // English message because localization data is no longer loaded
367  if (first) LogSilent("Waiting for pending image files to be written to disc...");
368  first = false;
369 #ifdef HAVE_WINTHREAD
370  Sleep(100);
371 #else
372  sched_yield();
373 #endif
374  }
375 }
bool LogSilent(const char *szMessage, bool fConsole)
Definition: C4Log.cpp:124
static bool HasPendingThreads()
Definition: StdPNG.cpp:346

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

int CPNGFile::iBPC

Definition at line 45 of file StdPNG.h.

Referenced by Create(), and Save().

int CPNGFile::iClrType

Definition at line 45 of file StdPNG.h.

Referenced by Create(), GetBitsPerPixel(), GetPix(), C4Surface::ReadPNG(), Save(), and SetPix().

int CPNGFile::iCmprType

Definition at line 45 of file StdPNG.h.

Referenced by Create(), and Save().

int CPNGFile::iFltrType

Definition at line 45 of file StdPNG.h.

Referenced by Create(), and Save().

unsigned long CPNGFile::iHgt
int CPNGFile::iIntrlcType

Definition at line 45 of file StdPNG.h.

Referenced by Create(), and Save().

unsigned long CPNGFile::iWdt

The documentation for this class was generated from the following files: