add jpeg support

This commit is contained in:
lxsang 2018-09-20 00:04:32 +02:00
parent 3739769c71
commit c81bd71534
3 changed files with 104 additions and 40 deletions

View File

@ -13,9 +13,9 @@ ifeq ($(UNAME_S),Darwin)
DEP= -I/usr/local/opt/jpeg-turbo/include -L/usr/local/opt/jpeg-turbo/lib DEP= -I/usr/local/opt/jpeg-turbo/include -L/usr/local/opt/jpeg-turbo/lib
endif endif
PLUGINLIBS = libantd.$(EXT) -lvncclient -lpthread -lz# -lsqlite3 PLUGINLIBS = libantd.$(EXT) -lvncclient -lpthread -lz -ljpeg# -lsqlite3
PCFLAGS=-W -Wall -g -D DEBUG $(PPF_FLAG) $(DEP) -D USE_ZLIB PCFLAGS=-W -Wall -g -D DEBUG $(PPF_FLAG) $(DEP) -D USE_JPEG #-D USE_ZLIB
main: $(PLUGINSDEP) $(PLUGINS) #lib main: $(PLUGINSDEP) $(PLUGINS) #lib

View File

@ -1 +1,5 @@
# antd-wvnc-plugin # antd-wvnc-plugin
## Dependencies
* Zlib
* libjpeg-turbo

104
wvnc.c
View File

@ -5,6 +5,9 @@
#ifdef USE_ZLIB #ifdef USE_ZLIB
#include <zlib.h> #include <zlib.h>
#endif #endif
#ifdef USE_JPEG
#include <jpeglib.h>
#endif
#include "plugin.h" #include "plugin.h"
#define get_user_data(x) ((wvnc_user_data_t *)x) #define get_user_data(x) ((wvnc_user_data_t *)x)
#define R_SHIFT(x) (0) #define R_SHIFT(x) (0)
@ -47,8 +50,9 @@ static rfbCredential *get_credential(rfbClient *cl, int credentialType);
static void update(rfbClient *cl, int x, int y, int w, int h); static void update(rfbClient *cl, int x, int y, int w, int h);
#ifdef USE_ZLIB #ifdef USE_ZLIB
int buff_compress(uint8_t* src, uint8_t* dest, int len) int zlib_compress(uint8_t *src, int len)
{ {
uint8_t *dest = (uint8_t *)malloc(len);
z_stream defstream; z_stream defstream;
defstream.zalloc = Z_NULL; defstream.zalloc = Z_NULL;
defstream.zfree = Z_NULL; defstream.zfree = Z_NULL;
@ -63,9 +67,65 @@ int buff_compress(uint8_t* src, uint8_t* dest, int len)
deflateInit(&defstream, Z_BEST_COMPRESSION); deflateInit(&defstream, Z_BEST_COMPRESSION);
deflate(&defstream, Z_FINISH); deflate(&defstream, Z_FINISH);
deflateEnd(&defstream); deflateEnd(&defstream);
memcpy(src, dest, defstream.total_out);
free(dest);
return defstream.total_out; return defstream.total_out;
} }
#endif #endif
#ifdef USE_JPEG
int jpeg_compress(uint8_t *buff, int w, int h, int bbp)
{
uint8_t *tmp = buff;
/*if(bbp == 4)
{
tmp = (uint8_t*)malloc(w*h*(bbp-1));
for(int i = 0; i < w*h; i++)
{
memcpy(tmp + (i*(bbp-1)), buff+ i*bbp, bbp-1 );
}
}*/
struct jpeg_compress_struct cinfo = {0};
struct jpeg_error_mgr jerror = {0};
jerror.trace_level = 10;
cinfo.err = jpeg_std_error(&jerror);
jerror.trace_level = 10;
cinfo.err->trace_level = 10;
jpeg_create_compress(&cinfo);
uint8_t *out = NULL;
unsigned long outbuffer_size = 0;
jpeg_mem_dest(&cinfo, &out, &outbuffer_size);
cinfo.image_width = w;
cinfo.image_height = h;
cinfo.input_components = bbp;
cinfo.in_color_space = bbp==4?JCS_EXT_RGBA:JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, 50, true);
jpeg_start_compress(&cinfo, true);
//unsigned counter = 0;
JSAMPROW row_pointer[1];
while (cinfo.next_scanline < cinfo.image_height)
{
row_pointer[0] = (JSAMPROW)(&tmp[cinfo.next_scanline * w * bbp]);
unsigned return_code = jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
//LOG("before %d after %d\n", w*h*bbp, );
if(outbuffer_size < w*h*bbp)
{
memcpy(buff, out, outbuffer_size);
}
else
{
outbuffer_size = 0;
}
//if(bbp == 4) free(tmp);
free(out);
return outbuffer_size;
}
#endif
int get_pixel_format(uint8_t deep, wvnc_pixel_format_t *d) int get_pixel_format(uint8_t deep, wvnc_pixel_format_t *d)
{ {
switch (deep) switch (deep)
@ -187,7 +247,7 @@ static rfbBool resize(rfbClient *client)
client->updateRect.x = client->updateRect.y = 0; client->updateRect.x = client->updateRect.y = 0;
client->updateRect.w = width; client->updateRect.w = width;
client->updateRect.h = height; client->updateRect.h = height;
//depth = 32; //depth = 24;
void *data = rfbClientGetClientData(client, identify()); void *data = rfbClientGetClientData(client, identify());
wvnc_user_data_t *user_data = get_user_data(data); wvnc_user_data_t *user_data = get_user_data(data);
//client->width = sdl->pitch / (depth / 8); //client->width = sdl->pitch / (depth / 8);
@ -237,14 +297,8 @@ static void update(rfbClient *client, int x, int y, int w, int h)
uint8_t bbp = (uint8_t)client->format.bitsPerPixel / 8; uint8_t bbp = (uint8_t)client->format.bitsPerPixel / 8;
int size = w * h * bbp; int size = w * h * bbp;
uint8_t *cmd = (uint8_t *)malloc(size + 10); // + 9 uint8_t *cmd = (uint8_t *)malloc(size + 10); // + 9
#ifdef USE_ZLIB uint8_t *tmp = cmd + 10;
uint8_t* buff = (uint8_t *)malloc(size); uint8_t flag = 0;
if (!buff)
{
vnc_fatal(user_data, "Cannot allocate data for update");
return;
}
#endif
if (!cmd) if (!cmd)
{ {
vnc_fatal(user_data, "Cannot allocate data for update"); vnc_fatal(user_data, "Cannot allocate data for update");
@ -255,11 +309,7 @@ static void update(rfbClient *client, int x, int y, int w, int h)
LOG("Client frame buffe data not found\n"); LOG("Client frame buffe data not found\n");
return; return;
} }
#ifdef USE_ZLIB uint8_t *dest_ptr = tmp;
uint8_t *dest_ptr = buff;
#else
uint8_t *dest_ptr = cmd + 10;
#endif
uint8_t *src_ptr; uint8_t *src_ptr;
//cpy line by line //cpy line by line
@ -282,16 +332,26 @@ static void update(rfbClient *client, int x, int y, int w, int h)
cmd[6] = (uint8_t)(w >> 8); cmd[6] = (uint8_t)(w >> 8);
cmd[7] = (uint8_t)(h & 0xFF); cmd[7] = (uint8_t)(h & 0xFF);
cmd[8] = (uint8_t)(h >> 8); cmd[8] = (uint8_t)(h >> 8);
cmd[9] = 0x0;
#ifdef USE_ZLIB #ifdef USE_JPEG
cmd[9] = 0x1; if(bbp == 3 || bbp == 4)
size = buff_compress(buff, cmd+10, size); {
int ret = jpeg_compress(tmp, w, h, bbp);
if(ret > 0)
{
flag |= 0x01;
size = ret;
}
}
#endif #endif
#ifdef USE_ZLIB
flag |= 0x02;
size = zlib_compress(tmp, size);
#endif
cmd[9] = flag;
ws_b(user_data->client, cmd, size + 10); ws_b(user_data->client, cmd, size + 10);
free(cmd); free(cmd);
#ifdef USE_ZLIB
free(buff);
#endif
} }
static rfbCredential *get_credential(rfbClient *cl, int credentialType) static rfbCredential *get_credential(rfbClient *cl, int credentialType)