1 /* === S Y N F I G ========================================================= */
2 /*! \file trgt_magickpp.cpp
3 ** \brief Magick++ Target Module
8 ** Copyright 2007 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 ======================================================= */
37 #include "trgt_magickpp.h"
41 /* === M A C R O S ========================================================= */
43 using namespace synfig;
47 /* === G L O B A L S ======================================================= */
49 SYNFIG_TARGET_INIT(magickpp_trgt);
50 SYNFIG_TARGET_SET_NAME(magickpp_trgt,"magick++");
51 SYNFIG_TARGET_SET_EXT(magickpp_trgt,"gif");
52 SYNFIG_TARGET_SET_VERSION(magickpp_trgt,"0.1");
53 SYNFIG_TARGET_SET_CVS_ID(magickpp_trgt,"$Id$");
55 /* === M E T H O D S ======================================================= */
57 template <class Container>
58 MagickLib::Image* copy_image_list(Container& container)
60 typedef typename Container::iterator Iter;
61 MagickLib::Image* previous = 0;
62 MagickLib::Image* first = NULL;
63 MagickLib::ExceptionInfo exceptionInfo;
64 MagickLib::GetExceptionInfo(&exceptionInfo);
65 for (Iter iter = container.begin(); iter != container.end(); ++iter)
67 MagickLib::Image* current = CloneImage(iter->image(), 0, 0, Magick::MagickTrue, &exceptionInfo);
68 if (!first) first = current;
70 current->previous = previous;
73 if ( previous != 0) previous->next = current;
80 magickpp_trgt::~magickpp_trgt()
82 MagickLib::ExceptionInfo exceptionInfo;
83 MagickLib::GetExceptionInfo(&exceptionInfo);
85 // check whether this file format supports multiple-image files
86 Magick::Image image(*(images.begin()));
87 image.fileName(filename);
88 SetImageInfo(image.imageInfo(),Magick::MagickTrue,&exceptionInfo);
90 // the file type is now in image.imageInfo()->magick and
91 // image.adjoin() tells us whether we can write to a single file
94 synfig::info("joining images");
95 unsigned int delay = round_to_int(100.0 / desc.get_frame_rate());
96 for_each(images.begin(), images.end(), Magick::animationDelayImage(delay));
98 // optimize the images (only write the pixels that change from frame to frame
99 #ifdef HAVE_MAGICK_OPTIMIZE
100 // make a completely new image list
101 // this is required because:
102 // RemoveDuplicateLayers wants a linked list of images, and removes some of them
103 // when it removes an image, it invalidates it using DeleteImageFromList, but we still have it in our container
104 // when we destroy our container, the image is re-freed, failing an assertion
106 synfig::info("copying image list");
107 MagickLib::Image *image_list = copy_image_list(images);
109 synfig::info("clearing old image list");
112 if (!getenv("SYNFIG_DISABLE_REMOVE_DUPS"))
114 synfig::info("removing duplicate frames");
115 RemoveDuplicateLayers(&image_list, &exceptionInfo);
118 if (!getenv("SYNFIG_DISABLE_OPTIMIZE"))
120 synfig::info("optimizing layers");
121 image_list = OptimizeImageLayers(image_list,&exceptionInfo);
124 if (!getenv("SYNFIG_DISABLE_OPTIMIZE_TRANS"))
126 synfig::info("optimizing layer transparency");
127 OptimizeImageTransparency(image_list,&exceptionInfo);
130 synfig::info("recreating image list");
131 insertImages(&images, image_list);
133 synfig::info("not optimizing images");
134 // DeconstructImages is available in ImageMagic 6.2.* but it doesn't take
135 // the 'dispose' method into account, so for frames with transparency where
136 // nothing is moving, we end up with objects disappearing when they shouldn't
138 // linkImages(images.begin(), images.end());
139 // MagickLib::Image* new_images = DeconstructImages(images.begin()->image(),&exceptionInfo);
140 // unlinkImages(images.begin(), images.end());
142 // insertImages(&images, new_images);
147 // if we can't write multiple images to a file of this type,
148 // include '%04d' in the filename, so the files will be numbered
149 // with a fixed width, '0'-padded number
150 synfig::info("can't join images of this type - numbering instead");
151 filename = (filename_sans_extension(filename) + ".%04d" + filename_extension(filename));
154 synfig::info("writing %d images to %s", images.size(), filename.c_str());
155 Magick::writeImages(images.begin(), images.end(), filename);
157 if (buffer1 != NULL) delete [] buffer1;
158 if (buffer2 != NULL) delete [] buffer2;
159 if (color_buffer != NULL) delete [] color_buffer;
163 magickpp_trgt::set_rend_desc(RendDesc *given_desc)
170 magickpp_trgt::init()
172 width = desc.get_w();
173 height = desc.get_h();
175 start_pointer = NULL;
177 buffer1 = new unsigned char[4*width*height];
181 buffer2 = new unsigned char[4*width*height];
188 color_buffer = new Color[width];
189 if (color_buffer == NULL)
200 magickpp_trgt::end_frame()
202 Magick::Image image(width, height, "RGBA", Magick::CharPixel, start_pointer);
203 if (transparent && images.begin() != images.end())
204 (images.end()-1)->gifDisposeMethod(Magick::BackgroundDispose);
205 images.push_back(image);
209 magickpp_trgt::start_frame(synfig::ProgressCallback *callback)
211 previous_buffer_pointer = start_pointer;
213 if (start_pointer == buffer1)
214 start_pointer = buffer_pointer = buffer2;
216 start_pointer = buffer_pointer = buffer1;
223 magickpp_trgt::start_scanline(int scanline)
229 magickpp_trgt::end_scanline()
231 convert_color_format(buffer_pointer, color_buffer,
232 width, PF_RGB|PF_A, gamma());
235 for (int i = 0; i < width; i++)
236 if (previous_buffer_pointer && // this isn't the first frame
237 buffer_pointer[i*4 + 3] < 128 && // our pixel is transparent
238 !(previous_buffer_pointer[i*4 + 3] < 128)) // the previous frame's pixel wasn't
244 buffer_pointer += 4 * width;
246 if (previous_buffer_pointer)
247 previous_buffer_pointer += 4 * width;