OpenClonk
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::CPNGFile ( )

Definition at line 96 of file StdPNG.cpp.

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

References Default().

Here is the call graph for this function:

◆ ~CPNGFile()

CPNGFile::~CPNGFile ( )

Definition at line 115 of file StdPNG.cpp.

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

References Clear().

Here is the call graph for this function:

Member Function Documentation

◆ Clear()

void CPNGFile::Clear ( )

Definition at line 136 of file StdPNG.cpp.

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 (fpFileOwned)
144  delete [] pFile;
145  pFile=nullptr;
146  // reset fields
147  fpFileOwned=false;
148  pFilePtr=nullptr;
149  iRowSize=0;
150  iPixSize=0;
151  // close file if open
152  if (fp) { fclose(fp); fp=nullptr; }
153 }
void ClearPngStructs()
Definition: StdPNG.cpp:121

References ClearPngStructs().

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ClearPngStructs()

void CPNGFile::ClearPngStructs ( )

Definition at line 121 of file StdPNG.cpp.

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 }

Referenced by Clear(), and Save().

Here is the caller graph for this function:

◆ Create()

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

Definition at line 191 of file StdPNG.cpp.

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

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

Here is the call graph for this function:

◆ Default()

void CPNGFile::Default ( )

Definition at line 101 of file StdPNG.cpp.

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 }

Referenced by CPNGFile().

Here is the caller graph for this function:

◆ GetBitsPerPixel()

int CPNGFile::GetBitsPerPixel ( )

Definition at line 292 of file StdPNG.cpp.

293 {
294  switch (iClrType)
295  {
296  case PNG_COLOR_TYPE_RGB: return 24;
297  case PNG_COLOR_TYPE_RGB_ALPHA: return 32;
298  }
299  return 0;
300 }

References iClrType.

◆ GetImageData()

BYTE* CPNGFile::GetImageData ( )
inline

Definition at line 64 of file StdPNG.h.

64 { return pImageData; } // return raw image data

◆ GetPix()

DWORD CPNGFile::GetPix ( int  iX,
int  iY 
)

Definition at line 175 of file StdPNG.cpp.

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

References C4RGB, iClrType, and RGBA().

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetRow()

uint32_t* CPNGFile::GetRow ( int  iY)
inline

Definition at line 56 of file StdPNG.h.

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

Referenced by C4Surface::ReadPNG().

Here is the caller graph for this function:

◆ Load()

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

Definition at line 155 of file StdPNG.cpp.

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

References Clear(), and iSize.

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Save()

bool CPNGFile::Save ( const char *  szFilename)

Definition at line 236 of file StdPNG.cpp.

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

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

Here is the call graph for this function:

◆ ScheduleSaving()

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

Definition at line 354 of file StdPNG.cpp.

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

References StdThread::Start().

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetPix()

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

Definition at line 216 of file StdPNG.cpp.

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

References GetBlueValue, GetGreenValue, GetRedValue, and iClrType.

◆ WaitForSaves()

void CPNGFile::WaitForSaves ( )
static

Definition at line 362 of file StdPNG.cpp.

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

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

Referenced by C4Application::Clear().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ iBPC

int CPNGFile::iBPC

Definition at line 45 of file StdPNG.h.

Referenced by Create(), and Save().

◆ iClrType

int CPNGFile::iClrType

Definition at line 45 of file StdPNG.h.

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

◆ iCmprType

int CPNGFile::iCmprType

Definition at line 45 of file StdPNG.h.

Referenced by Create(), and Save().

◆ iFltrType

int CPNGFile::iFltrType

Definition at line 45 of file StdPNG.h.

Referenced by Create(), and Save().

◆ iHgt

unsigned long CPNGFile::iHgt

◆ iIntrlcType

int CPNGFile::iIntrlcType

Definition at line 45 of file StdPNG.h.

Referenced by Create(), and Save().

◆ iWdt

unsigned long CPNGFile::iWdt

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