daily pastebin goal
16%
SHARE
TWEET

Untitled

a guest Dec 13th, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. cairo_status_t
  2. Image::decodeJPEGIntoSurface(jpeg_decompress_struct *info) {
  3.   int stride = width * 4;
  4.   cairo_status_t status;
  5.  
  6.   uint8_t *data = (uint8_t *) malloc(width * height * 4);
  7.   if (!data) {
  8.     jpeg_abort_decompress(info);
  9.     jpeg_destroy_decompress(info);
  10.     return CAIRO_STATUS_NO_MEMORY;
  11.   }
  12.  
  13.   uint8_t *src = (uint8_t *) malloc(width * info->output_components);
  14.   if (!src) {
  15.     free(data);
  16.     jpeg_abort_decompress(info);
  17.     jpeg_destroy_decompress(info);
  18.     return CAIRO_STATUS_NO_MEMORY;
  19.   }
  20.  
  21.   for (int y = 0; y < height; ++y) {
  22.     jpeg_read_scanlines(info, &src, 1);
  23.     uint32_t *row = (uint32_t *)(data + stride * y);
  24.     for (int x = 0; x < width; ++x) {
  25.       if (info->jpeg_color_space == 1) {
  26.         uint32_t *pixel = row + x;
  27.         *pixel = 255 << 24
  28.           | src[x] << 16
  29.           | src[x] << 8
  30.           | src[x];
  31.       } else {
  32.         int bx = 3 * x;
  33.         uint32_t *pixel = row + x;
  34.         *pixel = 255 << 24
  35.           | src[bx + 0] << 16
  36.           | src[bx + 1] << 8
  37.           | src[bx + 2];   
  38.       }
  39.     }
  40.   }
  41.  
  42.   _surface = cairo_image_surface_create_for_data(
  43.       data
  44.     , CAIRO_FORMAT_ARGB32
  45.     , width
  46.     , height
  47.     , cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width));
  48.  
  49.   jpeg_finish_decompress(info);
  50.   jpeg_destroy_decompress(info);
  51.   status = cairo_surface_status(_surface);
  52.  
  53.   if (status) {
  54.     free(data);
  55.     free(src);
  56.     return status;
  57.   }
  58.  
  59.   free(src);
  60.  
  61.   _data = data;
  62.  
  63.   return CAIRO_STATUS_SUCCESS;
  64. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand