feat(screenshot): Add threaded JPEG/PNG screenshots without game stalls#1785
feat(screenshot): Add threaded JPEG/PNG screenshots without game stalls#1785bobtista wants to merge 20 commits into
Conversation
20a3df1 to
37bd840
Compare
|
Some initial thoughts:
|
|
Agree with Stubbjax. JPG 90 is big file. Better make it default 80. Replace BMP screenshot with PNG screenshot. PNG is lossless compressed and always better than BMP. Make F12 take JPG 80 screenshot. Make CTRL+F12 take PNG screenshot. Make JPG Quality adjustable. Remove the old BMP code(s) and only use the new code for screenshot. |
|
Regarding Github formatting: When you write
Then it will not close this report when this is merged. Please read up on it here: |
RE moving logic to core, I moved what I could to core - but there are a lot more files that need to be moved to core before this can be moved there eg WWVegas/WW3D2/* |
3535e1e to
efc773f
Compare
977a6dc to
f8162f3
Compare
d7e8a8d to
d197bdd
Compare
d197bdd to
9669966
Compare
|
Needs rebase. |
9669966 to
4897b0b
Compare
Done |
9c99306 to
de35e57
Compare
ee755ff to
c9f43be
Compare
| @@ -0,0 +1,20 @@ | |||
| # TheSuperHackers @build bobtista 02/11/2025 STB single-file public domain libraries for image encoding | |||
There was a problem hiding this comment.
Reworded, not sure if you wanted it removed or not
There was a problem hiding this comment.
We never added TheSuperHackers comment prefixes in cmake files because they are implicitly all ours.
| return 80; | ||
|
|
||
| Int quality = atoi(it->second.str()); | ||
| return clamp(1, quality, 100); |
There was a problem hiding this comment.
Is Quality of 1 usable? I would expect the image falls apart below 30 or so.
I think quality 100 is waste. It ceils at 95 or so.
Chat Gippy says:
The answer depends on what you mean by the "quality value," because different JPEG encoders use different scales. Assuming you're referring to the common quality setting from 0–100 (used by libraries like libjpeg, Pillow, ImageMagick, etc.):
0–20: Very low quality. Strong compression artifacts; generally only useful for thumbnails or previews.
30–50: Low quality. Noticeable blockiness and ringing, but sometimes acceptable for web previews.
60–75: Reasonable minimum for most photographs. Compression artifacts are usually visible only on close inspection.
80–90: Sweet spot for most uses. Very good visual quality with significantly smaller files than maximum quality.
90–95: Excellent quality. File size increases rapidly while visual improvements become difficult to notice.
96–100: Usually not recommended. Files become much larger with little or no visible benefit. Some encoders also disable or minimize chroma subsampling at the highest settings, causing a large jump in file size.
A practical recommendation is:
Minimum and maximum "reasonable" values
Minimum reasonable: 60 (below this, artifacts become increasingly obvious for typical photos).
Maximum reasonable: 95 (above this, file size grows disproportionately compared to the tiny quality gain).
If you're using a specific JPEG encoder (such as libjpeg-turbo, mozjpeg, Photoshop, GIMP, or Apple's APIs), the interpretation of the quality value may differ slightly, but 60–95 remains a sensible general range, with 80–90 being the best balance for most images.
| Use case | Recommended quality |
|---|---|
| Small web images | 70–80 |
| General web photos | 80–85 |
| High-quality photos | 85–90 |
| Archiving/editing (JPEG only) | 90–95 |
| Avoid | >95 unless you have a specific reason |
There was a problem hiding this comment.
Ok, clamped 1-95, I don't see the harm in letting people choose potato quality on the low end if they want
There was a problem hiding this comment.
I don't think users should be gatekeeped like that. Applications like Photoshop, Gimp and Paint.net are not clamping the values either and allow any quality between 1 and 100. We should do the same.
There was a problem hiding this comment.
Photoshop is for image professionals. Game screenshot is for gamers. Gamers will not know that JPG screenshot quality of 100 will make their images 4 times larger for no quality gain. it is best to protect them from bad decisions.
| enum ScreenshotFormat | ||
| { | ||
| SCREENSHOT_JPEG, | ||
| SCREENSHOT_PNG |
There was a problem hiding this comment.
Done, and added SCREENSHOT_FORMAT_COUNT for the extension array check
| Bool getRightMouseScrollWithAlternateMouseEnabled() const; | ||
| Bool getRetaliationModeEnabled(); | ||
| Bool getDoubleClickAttackMoveEnabled(); | ||
| Int getJPEGQuality(); |
| Source/W3DDevice/GameClient/W3DTreeBuffer.cpp | ||
| Source/W3DDevice/GameClient/W3DVideoBuffer.cpp | ||
| Source/W3DDevice/GameClient/W3DView.cpp | ||
| # Source/W3DDevice/GameClient/W3DScreenshot.cpp |
There was a problem hiding this comment.
stb_image_write is a header-only library; this TU defines STB_IMAGE_WRITE_IMPLEMENTATION so the implementation compiles once in Core then links into both games
| DEBUG_LOG(("Failed to write screenshot %s", data->pathname)); | ||
| } | ||
|
|
||
| delete [] data->imageData; |
There was a problem hiding this comment.
Maybe move that to the destructor of the struct.
| { | ||
| char leafname[_MAX_FNAME]; | ||
| char pathname[_MAX_PATH]; | ||
| const char* extension = (format == SCREENSHOT_JPEG) ? "jpg" : "png"; |
There was a problem hiding this comment.
Better make a extra function or array for this and align it with the enum values and static assert on its completeness.
There was a problem hiding this comment.
Done, with a static_assert against SCREENSHOT_FORMAT_COUNT
| GetLocalTime(&st); | ||
| sprintf(leafname, "sshot_%04d%02d%02d_%02d%02d%02d_%03d.%s", | ||
| st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, extension); | ||
| strlcpy(pathname, TheGlobalData->getPath_UserData().str(), ARRAY_SIZE(pathname)); |
There was a problem hiding this comment.
Does it save them into the User Data root? Maybe this is a good opportunity to move them into a Screenshot/Image subfolder. It removes clutter from the User Data root where the INI configs are.
| unsigned int width = surfaceDesc.Width; | ||
| unsigned int height = surfaceDesc.Height; | ||
|
|
||
| unsigned char* image = new unsigned char[3 * width * height]; |
There was a problem hiding this comment.
Does the 24 bit image work correctly on 16 bit game? I know that on Windows XP (and maybe 7) it was possible to run Generals Zero Hour in 16 bit color mode and then it is necessary to convert the 16 bit render image to 24 bit color jpg. GenTool did that and its screenshots can be taken in 16 bit color game.
Here is the code that GenTool used:
bool CScreenshot::CopyBitsAs24Bit(SImageData* pImageData, TBytes& imageBytes)
{
// Note: The conversion algorithm does flip the bit map vertically
const bool is32bit = pImageData->d3dFormat == D3DFMT_A8R8G8B8;
const bool is16bit = pImageData->d3dFormat == D3DFMT_R5G6B5;
if (!is32bit && !is16bit)
return false;
D3DLOCKED_RECT lockedRect = {0};
if (FAILED(pImageData->GetSurface()->LockRect(&lockedRect, NULL, D3DLOCK_READONLY)))
return false;
const int w = (int)pImageData->width;
const int h = (int)pImageData->height;
BYTE* pDest = &imageBytes[0];
if (is32bit)
{
// Convert A8 R8 G8 B8 (32 bit) to R8 G8 B8 (24 bit)
for (int y = h-1; y >= 0; --y)
{
// Source RGB
// r 11111111000000000000000000000000
// g 00000000111111110000000000000000
// b 00000000000000001111111100000000
BYTE* pSrcLine = static_cast<BYTE*>(lockedRect.pBits) + y*w*4;
for (int x = 0; x < w; ++x)
{
pDest[0] = pSrcLine[0];
pDest[1] = pSrcLine[1];
pDest[2] = pSrcLine[2];
pDest += 3;
pSrcLine += 4;
}
}
}
else if (is16bit)
{
// Convert R5 G6 B5 (16 bit) to R8 G8 B8 (24 bit)
for (int y = h-1; y >= 0; --y)
{
// Source RGB
// r 1111100000000000
// g 0000011111100000
// b 0000000000011111
WORD* pSrcLine = static_cast<WORD*>(lockedRect.pBits) + y*w;
for (int x = 0; x < w; ++x)
{
// Destination RGB
// 111110000000000000000000
// 000000001111110000000000
// 000000000000000011111000
const WORD rgb = *pSrcLine;
const BYTE r = static_cast<BYTE>((rgb & 0xF800) >> 8);
const BYTE g = static_cast<BYTE>((rgb & 0x07E0) >> 3);
const BYTE b = static_cast<BYTE>((rgb & 0x001F) << 3);
pDest[0] = b;
pDest[1] = g;
pDest[2] = r;
pDest += 3;
pSrcLine += 1;
}
}
}
pImageData->GetSurface()->UnlockRect();
return true;
}There was a problem hiding this comment.
It didn't, thank you for sharing the code. Added your R5G6B5 to R8G8B8 conversion, and unknown formats now bail out
| { | ||
| ScreenshotThreadData* data = (ScreenshotThreadData*)param; | ||
|
|
||
| int result = 0; |
…and return write result
…te stb.cmake comment
ec48835 to
7b46fad
Compare
|
|
||
| unsigned char* image = new unsigned char[3 * width * height]; | ||
|
|
||
| if (is32Bit) |
There was a problem hiding this comment.
For less stalling could also memcpy the lrect.pBits to a temporary buffer and then handle the conversion to R8G8B8 in screenshotThreadFunc
There was a problem hiding this comment.
This is when the high performance bros gather around the camp fire to tell the legandary tales.
| // to keep them out of the User Data root. | ||
| strlcpy(pathname, TheGlobalData->getPath_UserData().str(), ARRAY_SIZE(pathname)); | ||
| strlcat(pathname, "Screenshots\\", ARRAY_SIZE(pathname)); | ||
| CreateDirectory(pathname, nullptr); |
There was a problem hiding this comment.
This file operation better also be done on thread. The whole file pathing can be done there. Keep main thread as cheap as possible.
| strlcpy(threadData->pathname, pathname, ARRAY_SIZE(threadData->pathname)); | ||
|
|
||
| DWORD threadId; | ||
| HANDLE hThread = CreateThread(nullptr, 0, screenshotThreadFunc, threadData, 0, &threadId); |
There was a problem hiding this comment.
Spawning new thread for every image is quite an expensive approach. It would be better to have a screenshot processing thread sleeping and wake up when new work is ready. Unfortunately with c++98 we do not have good sync primitives for that so maybe the thread spawn is ok for now.




Summary
Replaces the old BMP screenshot with compressed JPEG screenshots that don't stall the game, and adds PNG support.
Closes #1555
Closes #106 ... sort of
Adds a new screenshot function using the stb_image_write library with background threading:
Notes
Testing