Remove ancient trunk folder from svn repository
[synfig.git] / synfig-core / src / modules / mod_magickpp / trgt_magickpp.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file trgt_magickpp.cpp
3 **      \brief Magick++ Target Module
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2007, 2008 Chris Moore
9 **
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.
14 **
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.
19 **      \endlegal
20 **
21 ** === N O T E S ===========================================================
22 **
23 ** ========================================================================= */
24
25 /* === H E A D E R S ======================================================= */
26
27 #define SYNFIG_TARGET
28
29 #ifdef USING_PCH
30 #       include "pch.h"
31 #else
32 #ifdef HAVE_CONFIG_H
33 #       include <config.h>
34 #endif
35
36 #include <ETL/misc>
37 #include "trgt_magickpp.h"
38
39 #endif
40
41 /* === M A C R O S ========================================================= */
42
43 using namespace synfig;
44 using namespace std;
45 using namespace etl;
46
47 /* === G L O B A L S ======================================================= */
48
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$");
54
55 /* === M E T H O D S ======================================================= */
56
57 template <class Container>
58 MagickLib::Image* copy_image_list(Container& container)
59 {
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)
66         {
67                 MagickLib::Image* current;
68
69                 try
70                 {
71                         current = CloneImage(iter->image(), 0, 0, Magick::MagickTrue, &exceptionInfo);
72
73                         if (!first) first = current;
74
75                         current->previous = previous;
76                         current->next     = 0;
77
78                         if ( previous != 0) previous->next = current;
79                         previous = current;
80                 }
81                 catch(Magick::Warning warning) {
82                         synfig::warning("exception '%s'", warning.what());
83                 }
84         }
85
86         return first;
87 }
88
89 magickpp_trgt::~magickpp_trgt()
90 {
91         MagickLib::ExceptionInfo exceptionInfo;
92         MagickLib::GetExceptionInfo(&exceptionInfo);
93
94         try
95         {
96                 bool multiple_images = images.size() != 1;
97                 bool can_adjoin = false;
98
99                 if (multiple_images)
100                 {
101                         // check whether this file format supports multiple-image files
102                         Magick::Image image(*(images.begin()));
103                         image.fileName(filename);
104                         try
105                         {
106                                 SetImageInfo(image.imageInfo(),Magick::MagickTrue,&exceptionInfo);
107                                 can_adjoin = image.adjoin();
108                         }
109                         catch(Magick::Warning warning) {
110                                 synfig::warning("exception '%s'", warning.what());
111                         }
112                 }
113
114                 // the file type is now in image.imageInfo()->magick and
115                 // image.adjoin() tells us whether we can write to a single file
116                 if (can_adjoin)
117                 {
118                         synfig::info("joining images");
119                         unsigned int delay = round_to_int(100.0 / desc.get_frame_rate());
120                         for_each(images.begin(), images.end(), Magick::animationDelayImage(delay));
121
122                         // optimize the images (only write the pixels that change from frame to frame
123 #ifdef HAVE_MAGICK_OPTIMIZE
124                         // make a completely new image list
125                         // this is required because:
126                         //   RemoveDuplicateLayers wants a linked list of images, and removes some of them
127                         //   when it removes an image, it invalidates it using DeleteImageFromList, but we still have it in our container
128                         //   when we destroy our container, the image is re-freed, failing an assertion
129
130                         synfig::info("copying image list");
131                         MagickLib::Image *image_list = copy_image_list(images);
132
133                         synfig::info("clearing old image list");
134                         images.clear();
135
136                         if (!getenv("SYNFIG_DISABLE_REMOVE_DUPS"))
137                         {
138                                 synfig::info("removing duplicate frames");
139                                 try
140                                 {
141                                         RemoveDuplicateLayers(&image_list, &exceptionInfo);
142                                 }
143                                 catch(Magick::Warning warning) {
144                                         synfig::warning("exception '%s'", warning.what());
145                                 }
146                         }
147
148                         if (!getenv("SYNFIG_DISABLE_OPTIMIZE"))
149                         {
150                                 synfig::info("optimizing layers");
151                                 try
152                                 {
153                                         image_list = OptimizeImageLayers(image_list,&exceptionInfo);
154                                 }
155                                 catch(Magick::Warning warning) {
156                                         synfig::warning("exception '%s'", warning.what());
157                                 }
158                         }
159
160                         if (!getenv("SYNFIG_DISABLE_OPTIMIZE_TRANS"))
161                         {
162                                 synfig::info("optimizing layer transparency");
163                                 try
164                                 {
165                                         OptimizeImageTransparency(image_list,&exceptionInfo);
166                                 }
167                                 catch(Magick::Warning warning) {
168                                         synfig::warning("exception '%s'", warning.what());
169                                 }
170                         }
171
172                         synfig::info("recreating image list");
173                         insertImages(&images, image_list);
174 #else
175                         synfig::info("not optimizing images");
176                         // DeconstructImages is available in ImageMagic 6.2.* but it doesn't take
177                         // the 'dispose' method into account, so for frames with transparency where
178                         // nothing is moving, we end up with objects disappearing when they shouldn't
179
180                         // linkImages(images.begin(), images.end());
181                         // MagickLib::Image* new_images = DeconstructImages(images.begin()->image(),&exceptionInfo);
182                         // unlinkImages(images.begin(), images.end());
183                         // images.clear();
184                         // insertImages(&images, new_images);
185 #endif
186                 }
187                 else if (multiple_images)
188                 {
189                         // if we can't write multiple images to a file of this type,
190                         // include '%04d' in the filename, so the files will be numbered
191                         // with a fixed width, '0'-padded number
192                         synfig::info("can't join images of this type - numbering instead");
193                         filename = (filename_sans_extension(filename) + ".%04d" + filename_extension(filename));
194                 }
195
196                 synfig::info("writing %d image%s to %s", images.size(), images.size() == 1 ? "" : "s", filename.c_str());
197                 try
198                 {
199                         Magick::writeImages(images.begin(), images.end(), filename);
200                         synfig::info("done");
201                 }
202                 catch(Magick::Warning warning) {
203                         synfig::warning("exception '%s'", warning.what());
204                 }
205         }
206         catch(Magick::Warning warning) {
207                 synfig::warning("exception '%s'", warning.what());
208         }
209         catch(Magick::Error error) {
210                 synfig::error("exception '%s'", error.what());
211         }
212         catch(...) {
213                 synfig::error("unknown exception");
214         }
215
216         if (buffer1 != NULL) delete [] buffer1;
217         if (buffer2 != NULL) delete [] buffer2;
218         if (color_buffer != NULL) delete [] color_buffer;
219 }
220
221 bool
222 magickpp_trgt::set_rend_desc(RendDesc *given_desc)
223 {
224         desc = *given_desc;
225         return true;
226 }
227
228 bool
229 magickpp_trgt::init()
230 {
231         width = desc.get_w();
232         height = desc.get_h();
233
234         start_pointer = NULL;
235
236         buffer1 = new unsigned char[4*width*height];
237         if (buffer1 == NULL)
238                 return false;
239
240         buffer2 = new unsigned char[4*width*height];
241         if (buffer2 == NULL)
242         {
243                 delete [] buffer1;
244                 return false;
245         }
246
247         color_buffer = new Color[width];
248         if (color_buffer == NULL)
249         {
250                 delete [] buffer1;
251                 delete [] buffer2;
252                 return false;
253         }
254
255         return true;
256 }
257
258 void
259 magickpp_trgt::end_frame()
260 {
261         Magick::Image image(width, height, "RGBA", Magick::CharPixel, start_pointer);
262         if (transparent && images.begin() != images.end())
263                 (images.end()-1)->gifDisposeMethod(Magick::BackgroundDispose);
264         images.push_back(image);
265 }
266
267 bool
268 magickpp_trgt::start_frame(synfig::ProgressCallback *callback __attribute__ ((unused)))
269 {
270         previous_buffer_pointer = start_pointer;
271
272         if (start_pointer == buffer1)
273                 start_pointer = buffer_pointer = buffer2;
274         else
275                 start_pointer = buffer_pointer = buffer1;
276
277         transparent = false;
278         return true;
279 }
280
281 Color*
282 magickpp_trgt::start_scanline(int scanline __attribute__ ((unused)))
283 {
284         return color_buffer;
285 }
286
287 bool
288 magickpp_trgt::end_scanline()
289 {
290         convert_color_format(buffer_pointer, color_buffer,
291                                                  width, PF_RGB|PF_A, gamma());
292
293         if (!transparent)
294                 for (int i = 0; i < width; i++)
295                         if (previous_buffer_pointer &&                                  // this isn't the first frame
296                                 buffer_pointer[i*4 + 3] < 128 &&                        // our pixel is transparent
297                                 !(previous_buffer_pointer[i*4 + 3] < 128))      // the previous frame's pixel wasn't
298                         {
299                                 transparent = true;
300                                 break;
301                         }
302
303         buffer_pointer += 4 * width;
304
305         if (previous_buffer_pointer)
306                 previous_buffer_pointer += 4 * width;
307
308         return true;
309 }