Fwd: New example to illustrate data buffer manipulation

Steve Moreau <[email protected]>
Newsgroups gmane.comp.lib.cairo
Message-ID <CAD9K4eCjJAjiB1W+Arf+tf7aXyB5BcPERy7gXAixrrZZnXx-Sw@mail.gmail.com>
Hi all,

Following the question I asked here :
http://lists.freedesktop.org/archives/cairo/2012-November/023716.html, I
tried to make an example to illustrate :
- several layers flattening with transparency
- direct data buffer manipulation

As far I know, there is no example to do that (
http://cairographics.org/samples/ ?), so feel free to made it available if
you think it could help someone.

$ gcc -Wall -I/usr/include/cairo -o DirectBufferManipulation
DirectBufferManipulation.c -lcairo &&  ./DirectBufferManipulation

or

$ gcc -Wall `pkg-config --cflags cairo` -o DirectBufferManipulation
DirectBufferManipulation.c `pkg-config --libs cairo` &&
./DirectBufferManipulation

See you,

Steve

[image: Images intégrées 3]

-- 
cairo mailing list
[email protected]
http://lists.cairographics.org/mailman/listinfo/cairo
image.png (image/png, 2.1 KB) - not displayed
DirectBufferManipulation.c (text/x-csrc, 2.3 KB)
#include <stdlib.h>
#include <cairo.h>

int main(int argc, char* argv[])
{
  int width = 350;
  int height = 300;
  int stride;
  int x, y;
  unsigned char r, g, b, alpha;
  unsigned char* pDataBuf;
  unsigned char* pImageBuffer;
  
  cairo_surface_t* pLayer0Surface;
  cairo_t* pLayer0Context;

  cairo_surface_t* pLayer1Surface;
  cairo_t* pLayer1Context;

  cairo_surface_t* pLayer2Surface;  
  
  /* ---------- LAYER 0 : draw a white background fo example ---------- */
  pLayer0Surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
  pLayer0Context = cairo_create(pLayer0Surface);
  stride = cairo_image_surface_get_stride(pLayer0Surface);

  /* ---------- LAYER 1 : draw a dark blue rectangle ---------- */
  pLayer1Surface = cairo_surface_create_similar(cairo_get_target(pLayer0Context), CAIRO_CONTENT_COLOR_ALPHA, width, height);
  pLayer1Context = cairo_create(pLayer1Surface);
  cairo_set_source_rgba(pLayer1Context, 0, 0, 0.4, 1);
  cairo_rectangle(pLayer1Context, 50, 0, 250, 250);
  cairo_fill(pLayer1Context);

  /* ---------- LAYER 2 // DIRECT BUFFER MANIPULATION : draw a green to transparent gradient manually ---------- */
  pDataBuf = (unsigned char*) malloc(stride*height);
  pLayer2Surface = cairo_image_surface_create_for_data (pDataBuf, CAIRO_FORMAT_ARGB32, width, height, stride);
  for (y=0; y<height; y++)
  {
    for (x=0; x<width; x++)
    {
      r = 0;
      g = 0x80;
      b = 0;
      alpha = y*255/height;
      
      r = r*alpha/255;
      g = g*alpha/255;
      b = b*alpha/255;
      
      *((unsigned int*) &pDataBuf[y*stride + x*4]) = alpha << 24 | r << 16 | g << 8 | b;
    }
  }
  
  cairo_set_source_surface(pLayer1Context, pLayer2Surface, 0, 0);
  cairo_paint(pLayer1Context);

  cairo_set_source_surface(pLayer0Context, pLayer1Surface, 0, 0);
  cairo_paint(pLayer0Context);
  
  /* ---------- FINALIZE // dump to PNG or access final buffer ---------- */
  pImageBuffer = cairo_image_surface_get_data(pLayer0Surface);
  /* ... do whatever with pImageBuffer */

  cairo_surface_write_to_png(pLayer0Surface, "image.png");
  
  cairo_surface_destroy(pLayer0Surface);
  cairo_surface_destroy(pLayer1Surface);
  cairo_surface_destroy(pLayer2Surface);
  free(pDataBuf);
  
  cairo_destroy(pLayer0Context);
  cairo_destroy(pLayer1Context);
  
  return 0;
}
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.