ManaPlus
Functions
PngLib Namespace Reference

Functions

bool writePNG (SDL_Surface *const surface, const std::string &filename)
 

Function Documentation

◆ writePNG()

bool PngLib::writePNG ( SDL_Surface *const  surface,
const std::string &  filename 
)

< Surface is in video memory

< Use asynchronous blits if possible

< Surface is RLE encoded

< Surface is in video memory

< Use asynchronous blits if possible

< Surface is RLE encoded

Definition at line 37 of file pnglib.cpp.

39 {
40  if (surface == nullptr)
41  return false;
42 
43 
44  if (SDL_MUSTLOCK(surface))
45  SDL_LockSurface(surface);
46 
47  png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
48  nullptr, nullptr, nullptr);
49  if (png_ptr == nullptr)
50  {
51  reportAlways("Had trouble creating png_structp")
52  return false;
53  }
54 
55  png_infop info_ptr = png_create_info_struct(png_ptr);
56  if (info_ptr == nullptr)
57  {
58  png_destroy_write_struct(&png_ptr, static_cast<png_infopp>(nullptr));
59  reportAlways("Could not create png_info")
60  return false;
61  }
62 
63  if (setjmp(png_jmpbuf(png_ptr)))
64  {
65  png_destroy_write_struct(&png_ptr, &info_ptr);
66  reportAlways("problem writing to %s", filename.c_str())
67  return false;
68  }
69 
70  FILE *const fp = fopen(filename.c_str(), "wb");
71  if (fp == nullptr)
72  {
73  reportAlways("could not open file %s for writing",
74  filename.c_str())
75  return false;
76  }
77 
78  png_init_io(png_ptr, fp);
79 
80  const int colortype = (surface->format->BitsPerPixel == 24) ?
81  PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGB_ALPHA;
82 
83  png_set_IHDR(png_ptr, info_ptr, surface->w, surface->h, 8, colortype,
84  PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
85  PNG_FILTER_TYPE_DEFAULT);
86 
87  png_write_info(png_ptr, info_ptr);
88 
89  png_set_packing(png_ptr);
90 
91  png_bytep *const row_pointers
92  = new png_bytep[CAST_SIZE(surface->h)];
93 
94  for (int i = 0; i < surface->h; i++)
95  {
96  row_pointers[i] = static_cast<png_bytep>(static_cast<uint8_t *>(
97  surface->pixels) + CAST_SIZE(i * surface->pitch));
98  }
99 
100  png_write_image(png_ptr, row_pointers);
101  png_write_end(png_ptr, info_ptr);
102 
103  fclose(fp);
104 
105  delete [] row_pointers;
106 
107  png_destroy_write_struct(&png_ptr,
108  &info_ptr);
109 
110  if (SDL_MUSTLOCK(surface))
111  SDL_UnlockSurface(surface);
112 
113  return true;
114 }
#define CAST_SIZE
Definition: cast.h:34
#define reportAlways(...)
Definition: checkutils.h:253
#define new
Definition: debug_new.h:147
if(!vert) return

References CAST_SIZE, and reportAlways.

Referenced by main(), and Game::saveScreenshot().