1 /* === S Y N F I G ========================================================= */
2 /*! \file trgt_magickpp.cpp
3 ** \brief Magick++ Target Module
8 ** Copyright (c) 2007, 2008 Chris Moore
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 ** === N O T E S ===========================================================
23 ** ========================================================================= */
25 /* === H E A D E R S ======================================================= */
35 #include "trgt_magickpp.h"
39 /* === M A C R O S ========================================================= */
41 using namespace synfig;
45 /* === G L O B A L S ======================================================= */
47 SYNFIG_TARGET_INIT(magickpp_trgt);
48 SYNFIG_TARGET_SET_NAME(magickpp_trgt,"magick++");
49 SYNFIG_TARGET_SET_EXT(magickpp_trgt,"gif");
50 SYNFIG_TARGET_SET_VERSION(magickpp_trgt,"0.1");
51 SYNFIG_TARGET_SET_CVS_ID(magickpp_trgt,"$Id$");
53 /* === M E T H O D S ======================================================= */
55 template <class Container>
56 MagickLib::Image* copy_image_list(Container& container)
58 typedef typename Container::iterator Iter;
59 MagickLib::Image* previous = 0;
60 MagickLib::Image* first = NULL;
61 MagickLib::ExceptionInfo exceptionInfo;
62 MagickLib::GetExceptionInfo(&exceptionInfo);
63 for (Iter iter = container.begin(); iter != container.end(); ++iter)
65 MagickLib::Image* current;
69 current = CloneImage(iter->image(), 0, 0, Magick::MagickTrue, &exceptionInfo);
71 if (!first) first = current;
73 current->previous = previous;
76 if ( previous != 0) previous->next = current;
79 catch(Magick::Warning warning) {
80 synfig::warning("exception '%s'", warning.what());
87 magickpp_trgt::~magickpp_trgt()
89 MagickLib::ExceptionInfo exceptionInfo;
90 MagickLib::GetExceptionInfo(&exceptionInfo);
94 bool multiple_images = images.size() != 1;
95 bool can_adjoin = false;
99 // check whether this file format supports multiple-image files
100 Magick::Image image(*(images.begin()));
101 image.fileName(filename);
104 SetImageInfo(image.imageInfo(),Magick::MagickTrue,&exceptionInfo);
105 can_adjoin = image.adjoin();
107 catch(Magick::Warning warning) {
108 synfig::warning("exception '%s'", warning.what());
112 // the file type is now in image.imageInfo()->magick and
113 // image.adjoin() tells us whether we can write to a single file
116 synfig::info("joining images");
117 unsigned int delay = round_to_int(100.0 / desc.get_frame_rate());
118 for_each(images.begin(), images.end(), Magick::animationDelayImage(delay));
120 // optimize the images (only write the pixels that change from frame to frame
121 #ifdef HAVE_MAGICK_OPTIMIZE
122 // make a completely new image list
123 // this is required because:
124 // RemoveDuplicateLayers wants a linked list of images, and removes some of them
125 // when it removes an image, it invalidates it using DeleteImageFromList, but we still have it in our container
126 // when we destroy our container, the image is re-freed, failing an assertion
128 synfig::info("copying image list");
129 MagickLib::Image *image_list = copy_image_list(images);
131 synfig::info("clearing old image list");
134 if (!getenv("SYNFIG_DISABLE_REMOVE_DUPS"))
136 synfig::info("removing duplicate frames");
139 RemoveDuplicateLayers(&image_list, &exceptionInfo);
141 catch(Magick::Warning warning) {
142 synfig::warning("exception '%s'", warning.what());
146 if (!getenv("SYNFIG_DISABLE_OPTIMIZE"))
148 synfig::info("optimizing layers");
151 image_list = OptimizeImageLayers(image_list,&exceptionInfo);
153 catch(Magick::Warning warning) {
154 synfig::warning("exception '%s'", warning.what());
158 if (!getenv("SYNFIG_DISABLE_OPTIMIZE_TRANS"))
160 synfig::info("optimizing layer transparency");
163 OptimizeImageTransparency(image_list,&exceptionInfo);
165 catch(Magick::Warning warning) {
166 synfig::warning("exception '%s'", warning.what());
170 synfig::info("recreating image list");
171 insertImages(&images, image_list);
173 synfig::info("not optimizing images");
174 // DeconstructImages is available in ImageMagic 6.2.* but it doesn't take
175 // the 'dispose' method into account, so for frames with transparency where
176 // nothing is moving, we end up with objects disappearing when they shouldn't
178 // linkImages(images.begin(), images.end());
179 // MagickLib::Image* new_images = DeconstructImages(images.begin()->image(),&exceptionInfo);
180 // unlinkImages(images.begin(), images.end());
182 // insertImages(&images, new_images);
185 else if (multiple_images)
187 // if we can't write multiple images to a file of this type,
188 // include '%04d' in the filename, so the files will be numbered
189 // with a fixed width, '0'-padded number
190 synfig::info("can't join images of this type - numbering instead");
191 filename = (filename_sans_extension(filename) + ".%04d" + filename_extension(filename));
194 synfig::info("writing %d image%s to %s", images.size(), images.size() == 1 ? "" : "s", filename.c_str());
197 Magick::writeImages(images.begin(), images.end(), filename);
198 synfig::info("done");
200 catch(Magick::Warning warning) {
201 synfig::warning("exception '%s'", warning.what());
204 catch(Magick::Warning warning) {
205 synfig::warning("exception '%s'", warning.what());
207 catch(Magick::Error error) {
208 synfig::error("exception '%s'", error.what());
211 synfig::error("unknown exception");
214 if (buffer1 != NULL) delete [] buffer1;
215 if (buffer2 != NULL) delete [] buffer2;
216 if (color_buffer != NULL) delete [] color_buffer;
220 magickpp_trgt::set_rend_desc(RendDesc *given_desc)
227 magickpp_trgt::init()
229 width = desc.get_w();
230 height = desc.get_h();
232 start_pointer = NULL;
234 buffer1 = new unsigned char[4*width*height];
238 buffer2 = new unsigned char[4*width*height];
245 color_buffer = new Color[width];
246 if (color_buffer == NULL)
257 magickpp_trgt::end_frame()
259 Magick::Image image(width, height, "RGBA", Magick::CharPixel, start_pointer);
260 if (transparent && images.begin() != images.end())
261 (images.end()-1)->gifDisposeMethod(Magick::BackgroundDispose);
262 images.push_back(image);
266 magickpp_trgt::start_frame(synfig::ProgressCallback *callback __attribute__ ((unused)))
268 previous_buffer_pointer = start_pointer;
270 if (start_pointer == buffer1)
271 start_pointer = buffer_pointer = buffer2;
273 start_pointer = buffer_pointer = buffer1;
280 magickpp_trgt::start_scanline(int scanline __attribute__ ((unused)))
286 magickpp_trgt::end_scanline()
288 convert_color_format(buffer_pointer, color_buffer,
289 width, PF_RGB|PF_A, gamma());
292 for (int i = 0; i < width; i++)
293 if (previous_buffer_pointer && // this isn't the first frame
294 buffer_pointer[i*4 + 3] < 128 && // our pixel is transparent
295 !(previous_buffer_pointer[i*4 + 3] < 128)) // the previous frame's pixel wasn't
301 buffer_pointer += 4 * width;
303 if (previous_buffer_pointer)
304 previous_buffer_pointer += 4 * width;