Re: JPEG Support For Libcairo

Adrian Johnson <[email protected]>
Newsgroups gmane.comp.lib.cairo
Message-ID <[email protected]>
On 29/12/15 00:42, Bernhard Fischer wrote:
> I implemented two small functions which read/write JPEG into/from a Cairo 
> image surface.
> 
> Please find more info here:
> https://www.cypherpunk.at/2015/12/jpeg-support-for-libcairo/
> 
> 
> Best regards,
> Bernhard
> 

If you are going to use this code with any of the PDF/PS/SVG/Win32Print
backends it is a good idea to set the JPEG mime data for the image
surface. That way JPEG files will be embedded directly in the output
instead of the backend compressing the image data with lossless
compression. See attached patch.

-- 
cairo mailing list
[email protected]
http://lists.cairographics.org/mailman/listinfo/cairo
cairo_jpg.diff (text/x-patch, 849 B)
--- a/cairo_jpeg.c	2015-12-29 09:39:45.335384758 +1030
+++ b/cairo_jpg.c	2015-12-29 09:41:04.015631528 +1030
@@ -19,6 +19,7 @@
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <cairo.h>
 #include <jpeglib.h>
 
@@ -103,6 +104,8 @@
    FILE * infile;
    JSAMPROW row_pointer[1];
    cairo_surface_t *sfc;
+   long len;
+   void *data;
 
    // open input file
    if ((infile = fopen(filename, "rb")) == NULL)
@@ -146,6 +149,14 @@
    cairo_surface_mark_dirty(sfc);
    (void) jpeg_finish_decompress(&cinfo);
    jpeg_destroy_decompress(&cinfo);
+
+   // set jpeg mime data
+   fseek(infile, 0, SEEK_END);
+   len = ftell(infile);
+   fseek(infile, 0, SEEK_SET);
+   data = malloc(len);
+   fread(data, len, 1, infile);
+   cairo_surface_set_mime_data(sfc, CAIRO_MIME_TYPE_JPEG, data, len, free, data);
    fclose(infile);
 
    return sfc;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.