How to resize PNG screenshot before saving?

Hi, I’m currently looking at native sharing dialogues to share a screenshot when a player dies for example. Things seem pretty straight forward using Defold-Screenshot and Defold-Sharing (thanks @britzl).

It would be nice if the screenshot could be reduced in size as they’re pretty large when taken from devices these days.

Is this possible at all (maybe using Defold-PNG and buffers or something)?

Thanks

Should be easy to do a nearest step based reduction of the buffer data. You can apply bilinear filtering to each sample too.

Ah, that would be nice functionality to have as an extension. It could either go into the screenshot extension itself, or perhaps added to Defold-Imp (Image Processing).

I’m happily accepting pull requests! :wink:

1 Like

Over the past few days I’ve had a go at implementing https://github.com/nothings/stb/blob/master/stb_image_resize.h into imp as suggested.

Having never used c / c++ it all came as a bit of a shock! This is also my first attempt at creating anything NE related.

Here’s where I shamefully am after 4 days (pretty much exactly where I was on day one):

Include stb_image_resize in imp.cpp

#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "stb_image_resize.h"

Added a resize function to imp.cpp

static int Resize(lua_State* L) {
dmLogInfo("Imp resize");

int top = lua_gettop(L);

dmBuffer::HBuffer src_buffer = check_and_validate_buffer(L, 1);
uint8_t* bytes = 0x0;
uint32_t size = 0;
unsigned char* out_bytes = nullptr;

dmBuffer::Result r = dmBuffer::GetBytes(src_buffer, (void**)&bytes, &size);

if (r == dmBuffer::RESULT_OK) {
	stbir_resize_uint8(bytes, 960, 540, 0, out_bytes, 480, 270, 0, 4);
} else {
	// handle error
}

// make a buffer to return later

assert(top == lua_gettop(L));
return 0;

}

In lua:

local png_shot, w, h = screenshot.png()
local src_buffer, w, h = png.decode_rgba(png_shot)
pprint(src_buffer)
imp.resize(src_buffer)

All builds ok but the engine simply closes when the resize function is called (on mouse press).

Any pointers would be greatly appreciated. I’ve cleaned up all my crap from the test project and attached if anyone fancies a look.

Thanks

imp_resize.zip (300.0 KB)

1 Like

In game related libraries like these, they try to leave most of the memory management to the user, since it’s so important. And this is such a case.

Here’s the corresponding example:

1 Like