[cairo] JPEG Support For Libcairo

Uli Schlachter psychon at znc.in
Wed Dec 30 05:48:53 PST 2015


Hi,

Am 28.12.2015 um 15:12 schrieb Bernhard Fischer:
> 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/

Let's look at some of the code:

   // check valid input format
   if (cairo_surface_get_type(sfc) != CAIRO_SURFACE_TYPE_IMAGE &&
         cairo_image_surface_get_format(sfc) != CAIRO_FORMAT_ARGB32 &&
         cairo_image_surface_get_format(sfc) != CAIRO_FORMAT_RGB24)
      return CAIRO_STATUS_INVALID_FORMAT;

If I call this code with an image surface in format A8, the individual
comparisons evaluate to (true && false && false), so the whole if still
evaluates to false. You want to do:

   if (get_type() != IMAGE || (get_format() != ARGB32 && get_format() != RGB24))
      return;

For readability, I'd suggest:

  cairo_format_t format;

  if (cairo_surface_get_type(sfc) != CAIRO_SURFACE_TYPE_IMAGE)
     return CAIRO_STATUS_INVALID_FORMAT;

  format = cairo_image_surface_get_format(sfc)
  if (format != CAIRO_FORMAT_ARGB32 && format != CAIRO_FORMAT_RGB24)
     return CAIRO_STATUS_INVALID_FORMAT;

Cheers,
Uli

P.S.: Actually... Why can't this code just copy other surfaces to an image
surface, if needed? Thanks to cairo_surface_get_content() and
cairo_surface_create_similar_image(), this should be relatively easy to do. The
only complication is to get the size of the surface, but for that you could use
cairo_create() and cairo_clip_extents() (which would still fail for unbounded
recording surfaces...).
-- 
"Do you know that books smell like nutmeg or some spice from a foreign land?"
                                                  -- Faber in Fahrenheit 451


More information about the cairo mailing list