Skip to content

libobs: Add gs_save_png_file - #777

Merged
summeroff merged 4 commits into
streamlabsfrom
graphics/save-png
Sep 29, 2026
Merged

summeroff merged 4 commits into
streamlabsfrom
graphics/save-png

Conversation

@summeroff

Copy link
Copy Markdown

Problem: obs-studio-node needs to write a screenshot PNG (streamlabs/obs-studio-node#1786). libobs can decode images but not write them, so that PR vendors stb_image_write.

Change: export gs_save_png_file(file, data, format, cx, cy, linesize) next to gs_create_texture_file_data, encoding with the FFmpeg PNG encoder libobs already links. Accepts RGBA/BGRA/BGRX (BGRX -> opaque), honours row pitch, writes via os_fopen for UTF-8 paths, and removes a partial file only if it created it.

Effect: no behaviour change for existing code; osn can drop stb once this ships.

Testing:

  • cmake --build build_x64 --config RelWithDebInfo --target libobs (Windows x64): 0 warnings/errors
  • Throwaway harness against obs.dll: RGBA/BGRA/BGRX round-trip via gs_create_texture_file_data with padded pitch, 6 invalid-arg rejections, non-ASCII filename, existing file preserved on rejected call -- 11/11 pass
  • macOS not built locally

🤖 Generated with Claude Code

Lets callers without Qt (obs-studio-node's screenshot IPC) save a PNG using libobs' existing FFmpeg encoder instead of vendoring an image writer.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Integer overflow and narrowing can bypass input validation and pass invalid dimensions or strides to FFmpeg.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Adds PNG encoding to libobs using FFmpeg, enabling consumers to save texture data without bundling another encoder.

Changes:

  • Exports gs_save_png_file.
  • Supports RGBA, BGRA, BGRX, padded row pitches, and UTF-8 paths.
File Description
libobs/​graphics/​graphics.h Declares the PNG-saving API.
libobs/​graphics/​graphics-ffmpeg.c Implements validation, conversion, encoding, writing, and cleanup.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread libobs/graphics/graphics-ffmpeg.c Outdated
cx * 4 could wrap in uint32_t and bypass the pitch check, and values above INT_MAX were narrowed into invalid FFmpeg arguments.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Failure cleanup can unlink a pre-existing destination file.

Review effort: Balanced
Findings: 1 High severity

Open (1)
Resolved since last review (1)

Comment thread libobs/graphics/graphics-ffmpeg.c
A failure after opening the destination unlinked it even when it already existed. Writing to <file>.tmp and replacing on success leaves an existing file untouched on failure.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The fixed temporary filename can destroy an existing file or cause concurrent saves to corrupt each other.

Review effort: Balanced
Findings: 1 High severity

Open (1)
Resolved since last review (1)

Comment thread libobs/graphics/graphics-ffmpeg.c Outdated
A direct write is enough for screenshots: "wb" already truncates an existing destination, so removing the partial file on failure loses nothing.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@summeroff
summeroff merged commit a9e214c into streamlabs Sep 29, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants