[cairo] "data" in cairo_image_surface_get_data ()

Nguyen Vu Hung vuhung16plus at gmail.com
Tue Nov 27 08:02:23 PST 2007


2007/11/27, Baz <brian.ewins at gmail.com>:
> Looking at your code, I think it might be better if you tell us what
> you are trying to achieve here. Cairo's ARGB32 format uses
> premultiplied alpha; you seem to be multiplying by the alpha again? (I
> presume this is ARGB32 only because it'll zero every pixel of an RGB24
> image). Its possible that there is already a fast path in pixman to do
> what you want.
>
What I am try to do is converting a cairo surface to leptonica's PIX format [1]

What I've shown you is a part of my code that looks similar to
leptonica's pixReadStreamPng[2]

And I think I have use unmultiply_data instead of multply_data before
passing it to a PIX structure.

Btw, is there any better way to convert a cairo surface to leptonica's
PIX struct?
Can I make use of cairo_surface_write_to_png_stream [3]?

[1] http://aoclife.ddo.jp/~vuhung/tmp/leptonlib-1.51/src/pix.h
[2] http://aoclife.ddo.jp/~vuhung/tmp/leptonlib-1.51/src/pngio.c
[3] http://cairographics.org/manual/cairo-PNG-Support.html#cairo-surface-write-to-png-stream

> > # I am sorry it this is a FAQ.
>
> No worries, questions are welcome.
>
> >
> >  unsigned char *data_cairo = cairo_image_surface_get_data (m_pSurface);
> >  U32 width = cairo_image_surface_get_width(m_pSurface);
> >  U32 height = cairo_image_surface_get_height(m_pSurface);
> >  U32 stride = cairo_image_surface_get_stride(m_pSurface);
> >
> > int pixel_size = 4;
> >
> >     U32 i,j,k; // unsigned long
> >
> >     row_pointers =  (png_byte **) malloc (height * sizeof(char *));
> >
> >     if ( row_pointers == NULL ){
> >         return IMAGE_RESULT_NOERR;
> >     }
> >
> >     for (i = 0; i < height; i++)
> >         row_pointers[i] = &data_cairo[i * width * pixel_size];
> >
> >         // h = height , w = width
> >     for (i = 0; i < h; i++) {
> >          // data is of U32 *
> >          ppixel = (U32 *)( data + i * wpl);
> >          rowptr = row_pointers[i];
> >
> >          for (j = k = 0; j < w; j++) {
> >
> >              U8 alpha = rowptr[k++];
> >
> >              if ( alpha == 0 ) {
> >                 SET_DATA_BYTE(ppixel, COLOR_RED, 0);
> >                 SET_DATA_BYTE(ppixel, COLOR_GREEN, 0);
> >                 SET_DATA_BYTE(ppixel, COLOR_BLUE, 0);
> >             } else {
> >                 SET_DATA_BYTE(ppixel, COLOR_RED,
> > multiply_alpha(alpha, rowptr[k++]));
> >                 SET_DATA_BYTE(ppixel, COLOR_GREEN,
> > multiply_alpha(alpha, rowptr[k++]));
> >                 SET_DATA_BYTE(ppixel, COLOR_BLUE,
> > multiply_alpha(alpha, rowptr[k++]));
> >        }
> >        ppixel++;
> >      }
> >     }
> >
> >


-- 
Best Regards,
Nguyen Hung Vu
vuhung16plus{remove}@gmail.dot.com
An inquisitive look at Harajuku
http://www.flickr.com/photos/vuhung/sets/72157600109218238/


More information about the cairo mailing list