Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved png export #1669

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lua/core/screen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ _norns.screen_circle = function(x, y, r)
_norns.screen_arc(x, y, r, 0, math.pi*2)
end

--- export png
-- @param filename: saved to dust/data/(script)/(filename).png
Screen.export_png = function(filename) _norns.screen_export_png(norns.state.data..filename..'.png') end

--- display png
-- @param filename
-- @tparam number x x position
Expand Down
33 changes: 32 additions & 1 deletion matron/src/hardware/screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,38 @@ double *screen_text_extents(const char *s) {

extern void screen_export_png(const char *s) {
CHECK_CR
cairo_surface_write_to_png(surface, s);
static cairo_surface_t *png;
static cairo_t *temp; // for bg fill
// width = 640 (128*4 pixels with 64 pixel black border)
// hieght = 384 (64*4 pixels plus border)
png = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 640, 384);
Copy link
Collaborator

@tlubke tlubke Mar 29, 2023

Choose a reason for hiding this comment

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

I know I wasn't requested to review this, but I was poking around open PRs anyway. You may be able to use CAIRO_FORMAT_RGB24 and then you wouldn't have to worry about the alpha channel (pixels still fit into a uint32). That could probably allow you to lose the fill area of the code, and the bitwise OR with 0xFF for alpha.

Have you experimented with cairo_scale()? You might be able to copy the image data from surface, use create_image_surface_for_data() with the new dimensions, then apply translate and scale transformations.

EDIT: I see that PNG has a gamma metadata field, but depending on the viewing program that might not have any effect. Will it be important to match gamma in future display settings to screenshots here?

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks for taking a look! your suggestions make a ton of sense.

honestly all of my experience with graphics is on raw framebuffers... hence the low-level implementation. i'll see if i can find a minute to try out some of these ideas.

temp = cairo_create(png);
// fill
cairo_set_source_rgb(temp, 0, 0, 0);
cairo_rectangle(temp, 0, 0, 640, 384);
cairo_fill(temp);
// copy big pixles
uint32_t *src = (uint32_t *)cairo_image_surface_get_data(surface);
uint32_t *dst = (uint32_t *)cairo_image_surface_get_data(png);
if (!src || !dst) return;
dst += 64 + 640*64;
for(int y=0;y<64;y++) {
for(int x=0;x<128;x++) {
// FIXME: needs some sort of gamma correction?
uint32_t p = *src++ | 0xFF000000; // FF for alpha
for(int xx=0;xx<4;xx++) {
*(dst+1920) = p;
*(dst+1280) = p;
*(dst+640) = p;
*dst++ = p;
}
}
dst += 640*4 - 128*4;
}
// cleanup
cairo_destroy(temp);
cairo_surface_write_to_png(png, s);
cairo_surface_destroy((cairo_surface_t *)png);
}

void screen_display_png(const char *filename, double x, double y) {
Expand Down