diff --git a/src/PngWriter.cpp b/src/PngWriter.cpp index 76e1c098fe3..6007e82af32 100644 --- a/src/PngWriter.cpp +++ b/src/PngWriter.cpp @@ -8,9 +8,15 @@ #include -namespace { -void write_png(FileSystem::FileSourceFS &fs, const std::string &path, const Uint8 *bytes, int width, int height, int stride, int bytes_per_pixel, bool strip_alpha) +void write_screenshot(const Graphics::ScreendumpState &sd, const char *destFile) { + const Uint8 *bytes = sd.pixels.get(); + const int width = sd.width; + const int height = sd.height; + const int stride = sd.stride; + const int bytes_per_pixel = sd.bpp; + bool strip_alpha = true; + // Set up the pixel format color masks for RGB(A) byte arrays. Uint32 rmask, gmask, bmask, amask; #if SDL_BYTEORDER == SDL_BIG_ENDIAN @@ -28,12 +34,9 @@ void write_png(FileSystem::FileSourceFS &fs, const std::string &path, const Uint int dest_bpp = bytes_per_pixel; if (strip_alpha) { - if (amask == 0) - { + if (amask == 0) { strip_alpha = false; - } - else - { + } else { amask = 0; dest_bpp--; } @@ -46,29 +49,20 @@ void write_png(FileSystem::FileSourceFS &fs, const std::string &path, const Uint if (!strip_alpha) { // optimized pass when not stripping alpha. - for (int y = 0; y < height; ++y) - { - Uint8 *dest = static_cast(surface->pixels) + (height - (y+1)) * surface->pitch; + for (int y = 0; y < height; ++y) { + Uint8 *dest = static_cast(surface->pixels) + (height - (y + 1)) * surface->pitch; const Uint8 *source = bytes + y * stride; memcpy(dest, source, stride); } } else { - for (int y = 0; y < height; ++y) - { - Uint8 *dest = static_cast(surface->pixels) + (height - (y+1)) * surface->pitch; - const Uint8 *source = bytes + y * stride; - for (int x = 0; x < width; ++x) - { - // We could assume bpp is 4/3 here and just do: - //*dest = *source++; - //*dest = *source++; - //*dest = *source++; - //++source; + for (int y = 0; y < height; ++y) { + Uint8 *dest = static_cast(surface->pixels) + (height - (y + 1)) * surface->pitch; + const Uint8 *source = bytes + y * stride; + for (int x = 0; x < width; ++x) { - for (int channel = 0; channel < dest_bpp; ++channel) - { + for (int channel = 0; channel < dest_bpp; ++channel) { dest[channel] = source[channel]; } source += bytes_per_pixel; @@ -78,22 +72,17 @@ void write_png(FileSystem::FileSourceFS &fs, const std::string &path, const Uint } // do the actual saving - const std::string fname = FileSystem::JoinPathBelow(fs.GetRoot(), path); + const std::string dir = "screenshots"; + FileSystem::userFiles.MakeDirectory(dir); + + const std::string relative_path = FileSystem::JoinPathBelow(dir, destFile); + + const std::string fname = FileSystem::JoinPathBelow(FileSystem::userFiles.GetRoot(), relative_path); IMG_SavePNG(surface, fname.c_str()); //cleanup after ourselves SDL_FreeSurface(surface); surface = nullptr; -} -} //namespace - -void write_screenshot(const Graphics::ScreendumpState &sd, const char *destFile) -{ - const std::string dir = "screenshots"; - FileSystem::userFiles.MakeDirectory(dir); - const std::string fname = FileSystem::JoinPathBelow(dir, destFile); - - write_png(FileSystem::userFiles, fname, sd.pixels.get(), sd.width, sd.height, sd.stride, sd.bpp, true); - Output("Screenshot %s saved\n", fname.c_str()); + Output("Screenshot %s saved\n", relative_path.c_str()); }