Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- cairo_status_t
- Image::decodeJPEGIntoSurface(jpeg_decompress_struct *info) {
- int stride = width * 4;
- cairo_status_t status;
- uint8_t *data = (uint8_t *) malloc(width * height * 4);
- if (!data) {
- jpeg_abort_decompress(info);
- jpeg_destroy_decompress(info);
- return CAIRO_STATUS_NO_MEMORY;
- }
- uint8_t *src = (uint8_t *) malloc(width * info->output_components);
- if (!src) {
- free(data);
- jpeg_abort_decompress(info);
- jpeg_destroy_decompress(info);
- return CAIRO_STATUS_NO_MEMORY;
- }
- for (int y = 0; y < height; ++y) {
- jpeg_read_scanlines(info, &src, 1);
- uint32_t *row = (uint32_t *)(data + stride * y);
- for (int x = 0; x < width; ++x) {
- if (info->jpeg_color_space == 1) {
- uint32_t *pixel = row + x;
- *pixel = 255 << 24
- | src[x] << 16
- | src[x] << 8
- | src[x];
- } else {
- int bx = 3 * x;
- uint32_t *pixel = row + x;
- *pixel = 255 << 24
- | src[bx + 0] << 16
- | src[bx + 1] << 8
- | src[bx + 2];
- }
- }
- }
- _surface = cairo_image_surface_create_for_data(
- data
- , CAIRO_FORMAT_ARGB32
- , width
- , height
- , cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width));
- jpeg_finish_decompress(info);
- jpeg_destroy_decompress(info);
- status = cairo_surface_status(_surface);
- if (status) {
- free(data);
- free(src);
- return status;
- }
- free(src);
- _data = data;
- return CAIRO_STATUS_SUCCESS;
- }
Add Comment
Please, Sign In to add comment