Page 1 of 1

MagickCore - creating PNG8 by pixels with transparency

Posted: 2012-12-05T21:46:48-07:00
by F_S
Hello.

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();
	GetExceptionInfo(exception);
	
	// 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;
			///////
			ppix++;
		}
	}
	//-----------------------------------------------------+
	
	// Finalizing
	SyncImagePixels(image);
	
	WriteImage(image_info, image);
	
	DestroyImage(image);
	DestroyImageInfo(image_info);
	DestroyExceptionInfo(exception);
	MagickCoreTerminus();
}
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.

Re: MagickCore - creating PNG8 by pixels with transparency

Posted: 2012-12-07T05:44:55-07:00
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.
?