Blame synfig-core/src/modules/mod_magickpp/trgt_magickpp.cpp

Carlos Lopez a09598
/* === S Y N F I G ========================================================= */
Carlos Lopez a09598
/*!	\file trgt_magickpp.cpp
Carlos Lopez a09598
**	\brief Magick++ Target Module
Carlos Lopez a09598
**
Carlos Lopez a09598
**	$Id$
Carlos Lopez a09598
**
Carlos Lopez a09598
**	\legal
Carlos Lopez a09598
**	Copyright (c) 2007, 2008 Chris Moore
Carlos Lopez a09598
**
Carlos Lopez a09598
**	This package is free software; you can redistribute it and/or
Carlos Lopez a09598
**	modify it under the terms of the GNU General Public License as
Carlos Lopez a09598
**	published by the Free Software Foundation; either version 2 of
Carlos Lopez a09598
**	the License, or (at your option) any later version.
Carlos Lopez a09598
**
Carlos Lopez a09598
**	This package is distributed in the hope that it will be useful,
Carlos Lopez a09598
**	but WITHOUT ANY WARRANTY; without even the implied warranty of
Carlos Lopez a09598
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Carlos Lopez a09598
**	General Public License for more details.
Carlos Lopez a09598
**	\endlegal
Carlos Lopez a09598
**
Carlos Lopez a09598
** === N O T E S ===========================================================
Carlos Lopez a09598
**
Carlos Lopez a09598
** ========================================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
/* === H E A D E R S ======================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
#ifdef USING_PCH
Carlos Lopez a09598
#	include "pch.h"
Carlos Lopez a09598
#else
Carlos Lopez a09598
#ifdef HAVE_CONFIG_H
Carlos Lopez a09598
#	include <config.h></config.h>
Carlos Lopez a09598
#endif
Carlos Lopez a09598
663b89
#include <synfig general.h=""></synfig>
663b89
Carlos Lopez a09598
#include <etl misc=""></etl>
Carlos Lopez a09598
#include "trgt_magickpp.h"
Carlos Lopez a09598
Carlos Lopez a09598
#endif
Carlos Lopez a09598
Carlos Lopez a09598
/* === M A C R O S ========================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
using namespace synfig;
Carlos Lopez a09598
using namespace std;
Carlos Lopez a09598
using namespace etl;
Carlos Lopez a09598
Carlos Lopez a09598
/* === G L O B A L S ======================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
SYNFIG_TARGET_INIT(magickpp_trgt);
Carlos Lopez a09598
SYNFIG_TARGET_SET_NAME(magickpp_trgt,"magick++");
Carlos Lopez a09598
SYNFIG_TARGET_SET_EXT(magickpp_trgt,"gif");
Carlos Lopez a09598
SYNFIG_TARGET_SET_VERSION(magickpp_trgt,"0.1");
Carlos Lopez a09598
SYNFIG_TARGET_SET_CVS_ID(magickpp_trgt,"$Id$");
Carlos Lopez a09598
Carlos Lopez a09598
/* === M E T H O D S ======================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
template <class container=""></class>
fb72f9
MagickCore::Image* copy_image_list(Container& container)
Carlos Lopez a09598
{
Carlos Lopez a09598
	typedef typename Container::iterator Iter;
fb72f9
	MagickCore::Image* previous = 0;
fb72f9
	MagickCore::Image* first = NULL;
Matthew White 017ecc
	MagickCore::ExceptionInfo* exceptionInfo = MagickCore::AcquireExceptionInfo();
Carlos Lopez a09598
	for (Iter iter = container.begin(); iter != container.end(); ++iter)
Carlos Lopez a09598
	{
fb72f9
		MagickCore::Image* current;
Carlos Lopez a09598
Carlos Lopez a09598
		try
Carlos Lopez a09598
		{
Matthew White 017ecc
			current = CloneImage(iter->image(), 0, 0, Magick::MagickTrue, exceptionInfo);
Carlos Lopez a09598
Carlos Lopez a09598
			if (!first) first = current;
Carlos Lopez a09598
Carlos Lopez a09598
			current->previous = previous;
Carlos Lopez a09598
			current->next	  = 0;
Carlos Lopez a09598
Carlos Lopez a09598
			if ( previous != 0) previous->next = current;
Carlos Lopez a09598
			previous = current;
Carlos Lopez a09598
		}
3b3e3e
		catch(Magick::Warning &warning) {
Carlos Lopez a09598
			synfig::warning("exception '%s'", warning.what());
Carlos Lopez a09598
		}
Carlos Lopez a09598
	}
Carlos Lopez a09598
Matthew White 017ecc
	exceptionInfo = MagickCore::DestroyExceptionInfo(exceptionInfo);
Carlos Lopez a09598
	return first;
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
magickpp_trgt::~magickpp_trgt()
Carlos Lopez a09598
{
Matthew White 017ecc
	MagickCore::ExceptionInfo* exceptionInfo = MagickCore::AcquireExceptionInfo();
Carlos Lopez a09598
Carlos Lopez a09598
	try
Carlos Lopez a09598
	{
a0de29
		bool multiple_images = images.size() > 1;
Carlos Lopez a09598
		bool can_adjoin = false;
Carlos Lopez a09598
Carlos Lopez a09598
		if (multiple_images)
Carlos Lopez a09598
		{
Carlos Lopez a09598
			// check whether this file format supports multiple-image files
Carlos Lopez a09598
			Magick::Image image(*(images.begin()));
Carlos Lopez a09598
			image.fileName(filename);
Carlos Lopez a09598
			try
Carlos Lopez a09598
			{
Matthew White 017ecc
				SetImageInfo(image.imageInfo(),Magick::MagickTrue,exceptionInfo);
Carlos Lopez a09598
				can_adjoin = image.adjoin();
Carlos Lopez a09598
			}
3b3e3e
			catch(Magick::Warning &warning) {
Carlos Lopez a09598
				synfig::warning("exception '%s'", warning.what());
Carlos Lopez a09598
			}
Carlos Lopez a09598
		}
Carlos Lopez a09598
Carlos Lopez a09598
		// the file type is now in image.imageInfo()->magick and
Carlos Lopez a09598
		// image.adjoin() tells us whether we can write to a single file
Carlos Lopez a09598
		if (can_adjoin)
Carlos Lopez a09598
		{
Carlos Lopez a09598
			synfig::info("joining images");
Carlos Lopez a09598
			unsigned int delay = round_to_int(100.0 / desc.get_frame_rate());
Carlos Lopez a09598
			for_each(images.begin(), images.end(), Magick::animationDelayImage(delay));
Carlos Lopez a09598
Carlos Lopez a09598
			// optimize the images (only write the pixels that change from frame to frame
Carlos Lopez a09598
			// make a completely new image list
Carlos Lopez a09598
			// this is required because:
Carlos Lopez a09598
			//   RemoveDuplicateLayers wants a linked list of images, and removes some of them
Carlos Lopez a09598
			//   when it removes an image, it invalidates it using DeleteImageFromList, but we still have it in our container
Carlos Lopez a09598
			//   when we destroy our container, the image is re-freed, failing an assertion
Carlos Lopez a09598
Carlos Lopez a09598
			synfig::info("copying image list");
fb72f9
			MagickCore::Image *image_list = copy_image_list(images);
Carlos Lopez a09598
Carlos Lopez a09598
			synfig::info("clearing old image list");
Carlos Lopez a09598
			images.clear();
Carlos Lopez a09598
Carlos Lopez a09598
			if (!getenv("SYNFIG_DISABLE_REMOVE_DUPS"))
Carlos Lopez a09598
			{
Carlos Lopez a09598
				synfig::info("removing duplicate frames");
Carlos Lopez a09598
				try
Carlos Lopez a09598
				{
Matthew White 017ecc
					RemoveDuplicateLayers(&image_list, exceptionInfo);
Carlos Lopez a09598
				}
3b3e3e
				catch(Magick::Warning &warning) {
Carlos Lopez a09598
					synfig::warning("exception '%s'", warning.what());
Carlos Lopez a09598
				}
Carlos Lopez a09598
			}
Carlos Lopez a09598
Carlos Lopez a09598
			if (!getenv("SYNFIG_DISABLE_OPTIMIZE"))
Carlos Lopez a09598
			{
Carlos Lopez a09598
				synfig::info("optimizing layers");
Carlos Lopez a09598
				try
Carlos Lopez a09598
				{
Matthew White 017ecc
					image_list = OptimizeImageLayers(image_list,exceptionInfo);
Carlos Lopez a09598
				}
3b3e3e
				catch(Magick::Warning &warning) {
Carlos Lopez a09598
					synfig::warning("exception '%s'", warning.what());
Carlos Lopez a09598
				}
Carlos Lopez a09598
			}
Carlos Lopez a09598
Carlos Lopez a09598
			if (!getenv("SYNFIG_DISABLE_OPTIMIZE_TRANS"))
Carlos Lopez a09598
			{
Carlos Lopez a09598
				synfig::info("optimizing layer transparency");
Carlos Lopez a09598
				try
Carlos Lopez a09598
				{
Matthew White 017ecc
					OptimizeImageTransparency(image_list,exceptionInfo);
Carlos Lopez a09598
				}
3b3e3e
				catch(Magick::Warning &warning) {
Carlos Lopez a09598
					synfig::warning("exception '%s'", warning.what());
Carlos Lopez a09598
				}
Carlos Lopez a09598
			}
Carlos Lopez a09598
Carlos Lopez a09598
			synfig::info("recreating image list");
Carlos Lopez a09598
			insertImages(&images, image_list);
Carlos Lopez a09598
		}
Carlos Lopez a09598
		else if (multiple_images)
Carlos Lopez a09598
		{
Carlos Lopez a09598
			// if we can't write multiple images to a file of this type,
Carlos Lopez a09598
			// include '%04d' in the filename, so the files will be numbered
Carlos Lopez a09598
			// with a fixed width, '0'-padded number
Carlos Lopez a09598
			synfig::info("can't join images of this type - numbering instead");
Carlos Lopez baadb1
			filename = (filename_sans_extension(filename) + sequence_separator + "%04d" + filename_extension(filename));
Carlos Lopez a09598
		}
Carlos Lopez a09598
Carlos Lopez a09598
		synfig::info("writing %d image%s to %s", images.size(), images.size() == 1 ? "" : "s", filename.c_str());
Carlos Lopez a09598
		try
Carlos Lopez a09598
		{
Carlos Lopez a09598
			Magick::writeImages(images.begin(), images.end(), filename);
Carlos Lopez a09598
			synfig::info("done");
Carlos Lopez a09598
		}
3b3e3e
		catch(Magick::Warning &warning) {
Carlos Lopez a09598
			synfig::warning("exception '%s'", warning.what());
Carlos Lopez a09598
		}
Carlos Lopez a09598
	}
3b3e3e
	catch(Magick::Warning &warning) {
Carlos Lopez a09598
		synfig::warning("exception '%s'", warning.what());
Carlos Lopez a09598
	}
3b3e3e
	catch(Magick::Error &error) {
Carlos Lopez a09598
		synfig::error("exception '%s'", error.what());
Carlos Lopez a09598
	}
Carlos Lopez a09598
	catch(...) {
Carlos Lopez a09598
		synfig::error("unknown exception");
Carlos Lopez a09598
	}
Carlos Lopez a09598
Carlos Lopez a09598
	if (buffer1 != NULL) delete [] buffer1;
Carlos Lopez a09598
	if (buffer2 != NULL) delete [] buffer2;
Carlos Lopez a09598
	if (color_buffer != NULL) delete [] color_buffer;
3b3e3e
	//exceptionInfo = MagickCore::DestroyExceptionInfo(exceptionInfo);
3b3e3e
	MagickCore::DestroyExceptionInfo(exceptionInfo);
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
bool
Carlos Lopez a09598
magickpp_trgt::set_rend_desc(RendDesc *given_desc)
Carlos Lopez a09598
{
Carlos Lopez a09598
	desc = *given_desc;
Carlos Lopez a09598
	return true;
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
bool
93c6c7
magickpp_trgt::init(synfig::ProgressCallback*)
Carlos Lopez a09598
{
Carlos Lopez a09598
	width = desc.get_w();
Carlos Lopez a09598
	height = desc.get_h();
Carlos Lopez a09598
Carlos Lopez a09598
	start_pointer = NULL;
Carlos Lopez a09598
Carlos Lopez a09598
	buffer1 = new unsigned char[4*width*height];
Carlos Lopez a09598
	if (buffer1 == NULL)
Carlos Lopez a09598
		return false;
Carlos Lopez a09598
Carlos Lopez a09598
	buffer2 = new unsigned char[4*width*height];
Carlos Lopez a09598
	if (buffer2 == NULL)
Carlos Lopez a09598
	{
Carlos Lopez a09598
		delete [] buffer1;
Carlos Lopez a09598
		return false;
Carlos Lopez a09598
	}
Carlos Lopez a09598
Carlos Lopez a09598
	color_buffer = new Color[width];
Carlos Lopez a09598
	if (color_buffer == NULL)
Carlos Lopez a09598
	{
Carlos Lopez a09598
		delete [] buffer1;
Carlos Lopez a09598
		delete [] buffer2;
Carlos Lopez a09598
		return false;
Carlos Lopez a09598
	}
Carlos Lopez a09598
Carlos Lopez a09598
	return true;
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
void
Carlos Lopez a09598
magickpp_trgt::end_frame()
Carlos Lopez a09598
{
Carlos Lopez a09598
	Magick::Image image(width, height, "RGBA", Magick::CharPixel, start_pointer);
Carlos Lopez a09598
	if (transparent && images.begin() != images.end())
Carlos Lopez a09598
		(images.end()-1)->gifDisposeMethod(Magick::BackgroundDispose);
Carlos Lopez a09598
	images.push_back(image);
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
bool
Carlos Lopez a09598
magickpp_trgt::start_frame(synfig::ProgressCallback *callback __attribute__ ((unused)))
Carlos Lopez a09598
{
Carlos Lopez a09598
	if (start_pointer == buffer1)
Carlos Lopez a09598
		start_pointer = buffer_pointer = buffer2;
Carlos Lopez a09598
	else
Carlos Lopez a09598
		start_pointer = buffer_pointer = buffer1;
Carlos Lopez a09598
d1898c
	previous_buffer_pointer = start_pointer;
d1898c
Carlos Lopez a09598
	transparent = false;
Carlos Lopez a09598
	return true;
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
Color*
Carlos Lopez a09598
magickpp_trgt::start_scanline(int scanline __attribute__ ((unused)))
Carlos Lopez a09598
{
Carlos Lopez a09598
	return color_buffer;
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
bool
Carlos Lopez a09598
magickpp_trgt::end_scanline()
Carlos Lopez a09598
{
d1898c
	if (previous_buffer_pointer)
a4bbdd
		color_to_pixelformat(previous_buffer_pointer, color_buffer, PF_RGB|PF_A, 0, width);
Carlos Lopez a09598
Carlos Lopez a09598
	if (!transparent)
Carlos Lopez a09598
		for (int i = 0; i < width; i++)
Carlos Lopez a09598
			if (previous_buffer_pointer &&					// this isn't the first frame
Carlos Lopez a09598
				buffer_pointer[i*4 + 3] < 128 &&			// our pixel is transparent
Carlos Lopez a09598
				!(previous_buffer_pointer[i*4 + 3] < 128))	// the previous frame's pixel wasn't
Carlos Lopez a09598
			{
Carlos Lopez a09598
				transparent = true;
Carlos Lopez a09598
				break;
Carlos Lopez a09598
			}
Carlos Lopez a09598
Carlos Lopez a09598
	buffer_pointer += 4 * width;
Carlos Lopez a09598
Carlos Lopez a09598
	if (previous_buffer_pointer)
Carlos Lopez a09598
		previous_buffer_pointer += 4 * width;
Carlos Lopez a09598
Carlos Lopez a09598
	return true;
Carlos Lopez a09598
}