[cairo] "data" in cairo_image_surface_get_data ()

Baz brian.ewins at gmail.com
Tue Nov 27 06:05:51 PST 2007


On Nov 27, 2007 1:39 PM, Nguyen Vu Hung <vuhung16plus at gmail.com> wrote:
> 2007/11/26, Baz <brian.ewins at gmail.com>:
> > The code in cairo-png.c accesses the image surface parameters
> > directly, eg cairo_image_surface_get_data (surf) is replaced by
> > surf->data, but otherwise its almost exactly what you're asking for. I
> > don't think an example using png would be that helpful though, it
> > would introduce too many complications from showing how libpng works.
> > Iterating over the samples retrieved from get_data is pretty easy
> > though:
> >
> >   int i, j;
> >   unsigned char *data = cairo_image_surface_get_data (surf);
> >   int width = cairo_image_get_width(surf);
> >   int height = cairo_image_get_height(surf);
> >   int stride = cairo_image_get_stride(surf);
> >   for (i = 0; i < height; i++) {
> >       unsigned char *row = data + i * stride;
> >       for (int j = 0; j < width; j++) {
> >          // do something with the pixel at (i, j), which lies at row +
> > j * (pixel size),
> >          // based on the result of cairo_image_get_format and platform
> > endian-ness
> >       }
> >   }
>
> Thanks Baz,
>
> I didn't think I could go that far. My code requires me to process
> each bytes :D. Yes, it seems tough to me but it is fun to do/research.
>
> The problem I have now is the endian-ness thingy.
>
> With an Intel CPU, how to swap RED, GREEN, BLUE and alpha in the following code?

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.

> # 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