uint8_t * writeImageToPngInMemory(const uint8_t * image, const int w, const int h, const int numChans, size_t * lenOut, const unsigned long compressionLevel, const bool flip) { assert(image != nullptr); assert(lenOut != nullptr); assert(w > 0 && h > 0); return reinterpret_cast<uint8_t *>(tdefl_write_image_to_png_file_in_memory_ex( image, w, h, numChans, lenOut, static_cast<mz_uint>(compressionLevel), flip)); }
static int encode_png(lua_State *L) { am_check_nargs(L, 1); am_image_buffer *img = am_get_userdata(L, am_image_buffer, 1); size_t len; void *png_data = tdefl_write_image_to_png_file_in_memory_ex( img->buffer->data, img->width, img->height, 4, &len, MZ_DEFAULT_LEVEL, 1); am_buffer *buf = am_new_userdata(L, am_buffer); buf->size = len; buf->data = (uint8_t*)png_data; return 1; }
static int save_image_as_png(lua_State *L) { am_check_nargs(L, 2); am_image_buffer *img = am_get_userdata(L, am_image_buffer, 1); const char *filename = luaL_checkstring(L, 2); size_t len; void *png_data = tdefl_write_image_to_png_file_in_memory_ex( img->buffer->data, img->width, img->height, 4, &len, MZ_DEFAULT_LEVEL, 1); FILE *f = fopen(filename, "wb"); if (f == NULL) return luaL_error(L, "cannot open %s for writing", filename); fwrite(png_data, len, 1, f); fclose(f); free(png_data); return 0; }
static bool resize_image(void *img_data, int in_w, int in_h, const char *dir, const char *filename, int out_w, int out_h) { void *out_data = malloc(out_w * out_h * 4); double in_aspect = (double)in_w / (double)in_h; double out_aspect = (double)out_w / (double)out_h; if (fabs(in_aspect - out_aspect) < 0.05) { stbir_resize_uint8((const unsigned char*)img_data, in_w, in_h, 0, (unsigned char *)out_data, out_w, out_h, 0, 4); } else { uint32_t *pxl_data = (uint32_t*)out_data; uint32_t bg = ((uint32_t*)img_data)[0]; for (int i = 0; i < out_w * out_h; i++) { pxl_data[i] = bg; } double scaling_x = (double)out_w / (double)in_w; double scaling_y = (double)out_h / (double)in_h; double scaling = fmin(scaling_x, scaling_y); int scaled_w = (int)floor((double)in_w * scaling); int scaled_h = (int)floor((double)in_h * scaling); int x_os = (out_w - scaled_w) / 2; int y_os = (out_h - scaled_h) / 2; void *tmp_data = malloc(scaled_w * scaled_h * 4); stbir_resize_uint8((const unsigned char*)img_data, in_w, in_h, 0, (unsigned char *)tmp_data, scaled_w, scaled_h, 0, 4); uint32_t *tmp_pxl_data = (uint32_t*)tmp_data; for (int i = 0; i < scaled_w; i++) { for (int j = 0; j < scaled_h; j++) { pxl_data[(i + x_os) + (j + y_os) * out_w] = tmp_pxl_data[i + j * scaled_w]; } } free(tmp_data); } size_t len; void *png_data = tdefl_write_image_to_png_file_in_memory_ex( out_data, out_w, out_h, 4, &len, MZ_DEFAULT_LEVEL, 0); free(out_data); char *path = am_format("%s%c%s", dir, AM_PATH_SEP, filename); FILE *f = fopen(path, "wb"); free(path); if (f == NULL) { fprintf(stderr, "Error: cannot open %s for writing\n", filename); return false; } fwrite(png_data, len, 1, f); fclose(f); free(png_data); return true; }
void framecapture_capframe(const void* pixels) { #if 0 if (!framecapture_inited) { framecapture_init(); framecapture_inited = 1; } static int frame = -3; static int page = 1; if (frame >= 0) { framesbuffer = (uint8_t *)memalign(4096, FRAMESIZE); memcpy_arm(framesbuffer, map_base + page * FRAMESIZE, FRAMESIZE); framelinkedlist_add_to_list(nr_frames, nr_frames + 1, page, framesbuffer, true); nr_frames++; } frame++; page++; if (page == 3) page = 0; #endif void* pPNG_data = 0; size_t png_data_size = 0; pPNG_data = tdefl_write_image_to_png_file_in_memory_ex(pixels, 1280, 720, 4, &png_data_size, 6, 1); framelinkedlist_add_to_list(nr_frames, nr_frames + 1, pPNG_data, png_data_size, true); nr_frames++; }