Skip to content

Commit

Permalink
Tidy up and consolidate the screenshot code into a single function.
Browse files Browse the repository at this point in the history
  • Loading branch information
JonBooth78 committed Jan 8, 2024
1 parent 987cc11 commit 760d981
Showing 1 changed file with 24 additions and 35 deletions.
59 changes: 24 additions & 35 deletions src/PngWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@

#include <SDL_image.h>

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
Expand All @@ -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--;
}
Expand All @@ -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<Uint8*>(surface->pixels) + (height - (y+1)) * surface->pitch;
for (int y = 0; y < height; ++y) {
Uint8 *dest = static_cast<Uint8 *>(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<Uint8*>(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<Uint8 *>(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;
Expand All @@ -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());
}

0 comments on commit 760d981

Please sign in to comment.