MagickCore - creating PNG8 by pixels with transparency

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Post Reply
Posts: 3
Joined: 2011-04-19T12:09:45-07:00
Authentication code: 8675308

MagickCore - creating PNG8 by pixels with transparency

Post by F_S »


I want to create PNG8 file with MagickCore (not Wand).
Default color (background) must be transparent.
Colour pixels must be set individually and manually.
Also it's not clear how to work with color tables.

Template for what I wanted to get is like this:

Code: Select all

int main (int argc, char *argv[])
	char filename[...] = ...;
	int width = 512, height = 512;
	Image *image;
	ImageInfo *image_info;
	ExceptionInfo *exception;
	MagickBooleanType status;
	MagickCoreGenesis((char *)NULL, MagickFalse);

	// Get and Initialize an exception info
	exception = AcquireExceptionInfo();
	// Imageinfo & image
	image_info = AcquireImageInfo();
	CopyMagickString(image_info->filename, filename, MaxTextExtent);
	CopyMagickString(image_info->magick, "png", MaxTextExtent);
//*** Is it right "png" ?
//*** Or i must specify some like "png8" ?
//*** ... and.. may be some more CopyMagickString's ?
	image_info->file = fopen(filename, "wb");
	image_info->adjoin=MagickTrue;// MagickWriteImage does this so I do it too.
	image = AcquireImage(image_info);
	SetImageExtent(image, width, height);
	SetImageStorageClass(image, DirectClass);
//*** Technique is similar to RGB workaround?
//*** Or... How we must work with indexed colors?
	PixelPacket *pixels;
	register PixelPacket *ppix;
	pixels = GetImagePixels(image, 0, 0, width, height);

	// Colortable
//*** HOW TO Color Table ?!

	// Working with pixels
	ppix = pixels;
	for(int i=0; i<height; i++){
		for(int j=0; j<width; j++){
			//ppix->red = some_red_intensity;
			ppix->WHAT = needed_color_index;
	// Finalizing
	WriteImage(image_info, image);
Or we can't directly set pixels by color indexes and must set as rgb-values, and color table will be created automatically when saving file at end?.. But how to be with transparent pixels?..
There are too insufficient examples for using MagickCore in plain C.

Hope i find help there.
Posts: 3
Joined: 2011-04-19T12:09:45-07:00
Authentication code: 8675308

Re: MagickCore - creating PNG8 by pixels with transparency

Post by F_S »

Also, it's interesting how I can get there PNG32 (with full alpha-channel)?
Pixels will also be setted manually and individually. And at end of setting all array W x H pixels will be pushed to IMagic by SyncImagePixels.
Post Reply