|
5 | 5 | #include "inc/mongoose/mongoose.h" |
6 | 6 | #include "inc/mINI/ini.h" |
7 | 7 | #include "inc/subprocess/subprocess.h" |
| 8 | +#include "inc/lodepng/lodepng.h" |
8 | 9 |
|
9 | 10 | #include <iostream> |
10 | 11 | #include <filesystem> |
11 | 12 | #include <algorithm> |
12 | 13 | #include <X11/Xlib.h> |
13 | 14 |
|
| 15 | +#include <libimagequant.h> |
| 16 | + |
14 | 17 | static VPXDisplayServer* g_pServer = NULL; |
15 | 18 |
|
16 | 19 | void VPXDisplayServer::Forward(struct mg_http_message *hm, struct mg_connection *c) |
@@ -234,7 +237,7 @@ void VPXDisplayServer::Capture(struct mg_connection *c, void *ev_data) |
234 | 237 | int process_return; |
235 | 238 | if (!subprocess_join(&subprocess, &process_return)) { |
236 | 239 | PLOGI.printf("process return: %d", process_return); |
237 | | - if (!process_return) { |
| 240 | + if (!process_return && ProcessPNG(szCapture)) { |
238 | 241 | mg_http_reply(c, 200, "", ""); |
239 | 242 | return; |
240 | 243 | } |
@@ -281,9 +284,9 @@ void VPXDisplayServer::CaptureES(struct mg_connection *c, void *ev_data) |
281 | 284 | int process_return; |
282 | 285 | if (!subprocess_join(&subprocess, &process_return)) { |
283 | 286 | PLOGI.printf("process returned: %d", process_return); |
284 | | - if (!process_return) { |
| 287 | + if (!process_return && ProcessPNG(szPath)) { |
285 | 288 | struct mg_http_serve_opts opts = {}; |
286 | | - mg_http_serve_file(c, hm, szPath.c_str(), &opts); |
| 289 | + mg_http_serve_file(c, hm, szPath.c_str(), &opts); |
287 | 290 | return; |
288 | 291 | } |
289 | 292 | } |
@@ -454,6 +457,63 @@ void VPXDisplayServer::LoadINI() |
454 | 457 | } |
455 | 458 | } |
456 | 459 |
|
| 460 | +bool VPXDisplayServer::ProcessPNG(const std::string& filename) |
| 461 | +{ |
| 462 | + unsigned int width; |
| 463 | + unsigned int height; |
| 464 | + unsigned char* pPixels; |
| 465 | + |
| 466 | + if (lodepng_decode32_file(&pPixels, &width, &height, filename.c_str())) |
| 467 | + return false; |
| 468 | + |
| 469 | + liq_attr* pHandle = liq_attr_create(); |
| 470 | + liq_image* pImage = liq_image_create_rgba(pHandle, pPixels, width, height, 0); |
| 471 | + |
| 472 | + liq_result* pQuantizationResult; |
| 473 | + if (liq_image_quantize(pImage, pHandle, &pQuantizationResult) != LIQ_OK) |
| 474 | + return false; |
| 475 | + |
| 476 | + size_t pixelsSize = width * height; |
| 477 | + unsigned char* pPixels8 = (unsigned char *)malloc(pixelsSize); |
| 478 | + liq_set_dithering_level(pQuantizationResult, 1.0); |
| 479 | + |
| 480 | + liq_write_remapped_image(pQuantizationResult, pImage, pPixels8, pixelsSize); |
| 481 | + const liq_palette* pPalette = liq_get_palette(pQuantizationResult); |
| 482 | + |
| 483 | + LodePNGState state; |
| 484 | + lodepng_state_init(&state); |
| 485 | + state.info_raw.colortype = LCT_PALETTE; |
| 486 | + state.info_raw.bitdepth = 8; |
| 487 | + state.info_png.color.colortype = LCT_PALETTE; |
| 488 | + state.info_png.color.bitdepth = 8; |
| 489 | + |
| 490 | + for (int i = 0; i < pPalette->count; i++) { |
| 491 | + lodepng_palette_add(&state.info_png.color, pPalette->entries[i].r, pPalette->entries[i].g, pPalette->entries[i].b, pPalette->entries[i].a); |
| 492 | + lodepng_palette_add(&state.info_raw, pPalette->entries[i].r, pPalette->entries[i].g, pPalette->entries[i].b, pPalette->entries[i].a); |
| 493 | + } |
| 494 | + |
| 495 | + unsigned char* pOutput; |
| 496 | + size_t outputSize; |
| 497 | + if (lodepng_encode(&pOutput, &outputSize, pPixels8, width, height, &state)) |
| 498 | + return false; |
| 499 | + |
| 500 | + FILE *fp = fopen(filename.c_str(), "wb"); |
| 501 | + if (!fp) |
| 502 | + return false; |
| 503 | + |
| 504 | + fwrite(pOutput, 1, outputSize, fp); |
| 505 | + fclose(fp); |
| 506 | + |
| 507 | + liq_result_destroy(pQuantizationResult); |
| 508 | + liq_image_destroy(pImage); |
| 509 | + liq_attr_destroy(pHandle); |
| 510 | + |
| 511 | + free(pPixels8); |
| 512 | + lodepng_state_cleanup(&state); |
| 513 | + |
| 514 | + return true; |
| 515 | +} |
| 516 | + |
457 | 517 | int VPXDisplayServer::Start() |
458 | 518 | { |
459 | 519 | PLOGI << "Starting VPXDS..."; |
|
0 commit comments