Page 1 of 1

Managing alpha transparency using C and core interface.

Posted: 2010-11-02T12:07:11-07:00
by Duchess
Hi,

I am trying to manipulate a png image through C code and the core library interface. Basically the source png image, created by someone else, has a transparent background and a filled in shape. I would like to reduce the opacity of the filled in shape so that when the png image is overlaid over another image in the web browser, the content underneath shows through the filled area (like a highlighter).

As well as being part of a larger legacy system, the manipulation involves a large amount of these images and a very convoluted file naming and tree structure, hence the need to do this in C.

I have used IM some years ago but find myself very rusty on the current incarnation and struggling to get a foothold on where to actually start on this. Since all of the examples actually use the command line tools and there appears to be little in the way of C code examples, could someone point me to the C api calls I need to look at to manage the transparency/alpha please?

My thanks in advance.

Penny

Re: Managing alpha transparency using C and core interface.

Posted: 2010-11-02T12:32:24-07:00
by magick
Most image processing is taking an image, applying a transformation to the pixels, and creating an output image. See near the bottom of http://www.imagemagick.org/script/archi ... .php#cache. It shows you how to do this in MagickCore:

Code: Select all

  const PixelPacket
    *p;

  PixelPacket
    *q;

  ssize_t
    x,
    y;

  destination=CloneImage(source,source->columns,source->rows,MagickTrue,exception);
  if (destination  == (Image *) NULL)
    { /* an exception was thrown */ }
  for (y=0; y < (long) source->rows; y++)
  {
    p=GetVirtualPixels(source,0,y,source->columns,1,exception);
    q=GetAuthenticPixels(destination,0,y,destination->columns,1,exception);
    if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL)
      break;
    for (x=0; x < (long) source->columns; x++)
    {
      q->red=90*p->red/100;
      q->green=90*p->green/100;
      q->blue=90*p->blue/100;
      q->opacity=90*p->opacity/100;
      p++;
      q++;
    }
    if (SyncAuthenticPixels(destination,exception) == MagickFalse)
      break;
  }
  if (y < (long) source->rows)
    { /* an exception was thrown */ }

Download http://www.imagemagick.org/source/core/ ... contrast.c for a complete example.

If you have a specific MagickCore API question, post it here.

Re: Managing alpha transparency using C and core interface.

Posted: 2010-11-03T06:51:21-07:00
by Duchess
My thanks for this, exactly what I needed :-)

Basically I compared the existing opacity of each pixel and if set to transparent I left it intact, if set to opaque I adjusted it to give the desired effect.

Penny