GUAC-236: Implement JPEG.

This commit is contained in:
Michael Jumper 2016-03-12 19:55:17 -08:00
parent c16832f11a
commit be0a9e728f

View File

@ -22,13 +22,66 @@
#include "config.h" #include "config.h"
#include "jpeg.h" #include "jpeg.h"
#include "log.h"
#include <stdio.h>
#include <unistd.h>
#include <cairo/cairo.h> #include <cairo/cairo.h>
#include <jpeglib.h>
#include <stdlib.h> #include <stdlib.h>
cairo_surface_t* guacenc_jpeg_decoder(unsigned char* data, int length) { cairo_surface_t* guacenc_jpeg_decoder(unsigned char* data, int length) {
/* STUB */
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
/* Create decompressor with standard error handling */
jpeg_create_decompress(&cinfo);
cinfo.err = jpeg_std_error(&jerr);
/* Read JPEG directly from memory buffer */
jpeg_mem_src(&cinfo, data, length);
/* Read and validate JPEG header */
if (!jpeg_read_header(&cinfo, TRUE)) {
guacenc_log(GUAC_LOG_WARNING, "Invalid JPEG data");
jpeg_destroy_decompress(&cinfo);
return NULL; return NULL;
} }
/* Begin decompression */
cinfo.out_color_space = JCS_EXT_BGRX;
jpeg_start_decompress(&cinfo);
/* Pull JPEG dimensions from decompressor */
int width = cinfo.output_width;
int height = cinfo.output_height;
/* Create blank Cairo surface (no transparency in JPEG) */
cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
width, height);
/* Pull underlying buffer and its stride */
int stride = cairo_image_surface_get_stride(surface);
unsigned char* row = cairo_image_surface_get_data(surface);
/* Read JPEG into surface */
while (cinfo.output_scanline < height) {
unsigned char* buffers[1] = { row };
jpeg_read_scanlines(&cinfo, buffers, 1);
row += stride;
}
/* End decompression */
jpeg_finish_decompress(&cinfo);
/* Free decompressor */
jpeg_destroy_decompress(&cinfo);
/* JPEG was read successfully */
return surface;
}