Use LinkableValueNode members functions when possible in the derived valuenodes.
[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 #ifdef USING_PCH
28 #       include "pch.h"
29 #else
30 #ifdef HAVE_CONFIG_H
31 #       include <config.h>
32 #endif
33
34 #include <ETL/misc>
35 #include "trgt_magickpp.h"
36
37 #endif
38
39 /* === M A C R O S ========================================================= */
40
41 using namespace synfig;
42 using namespace std;
43 using namespace etl;
44
45 /* === G L O B A L S ======================================================= */
46
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$");
52
53 /* === M E T H O D S ======================================================= */
54
55 template <class Container>
56 MagickLib::Image* copy_image_list(Container& container)
57 {
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)
64         {
65                 MagickLib::Image* current;
66
67                 try
68                 {
69                         current = CloneImage(iter->image(), 0, 0, Magick::MagickTrue, &exceptionInfo);
70
71                         if (!first) first = current;
72
73                         current->previous = previous;
74                         current->next     = 0;
75
76                         if ( previous != 0) previous->next = current;
77                         previous = current;
78                 }
79                 catch(Magick::Warning warning) {
80                         synfig::warning("exception '%s'", warning.what());
81                 }
82         }
83
84         return first;
85 }
86
87 magickpp_trgt::~magickpp_trgt()
88 {
89         MagickLib::ExceptionInfo exceptionInfo;
90         MagickLib::GetExceptionInfo(&exceptionInfo);
91
92         try
93         {
94                 bool multiple_images = images.size() != 1;
95                 bool can_adjoin = false;
96
97                 if (multiple_images)
98                 {
99                         // check whether this file format supports multiple-image files
100                         Magick::Image image(*(images.begin()));
101                         image.fileName(filename);
102                         try
103                         {
104                                 SetImageInfo(image.imageInfo(),Magick::MagickTrue,&exceptionInfo);
105                                 can_adjoin = image.adjoin();
106                         }
107                         catch(Magick::Warning warning) {
108                                 synfig::warning("exception '%s'", warning.what());
109                         }
110                 }
111
112                 // the file type is now in image.imageInfo()->magick and
113                 // image.adjoin() tells us whether we can write to a single file
114                 if (can_adjoin)
115                 {
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));
119
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
127
128                         synfig::info("copying image list");
129                         MagickLib::Image *image_list = copy_image_list(images);
130
131                         synfig::info("clearing old image list");
132                         images.clear();
133
134                         if (!getenv("SYNFIG_DISABLE_REMOVE_DUPS"))
135                         {
136                                 synfig::info("removing duplicate frames");
137                                 try
138                                 {
139                                         RemoveDuplicateLayers(&image_list, &exceptionInfo);
140                                 }
141                                 catch(Magick::Warning warning) {
142                                         synfig::warning("exception '%s'", warning.what());
143                                 }
144                         }
145
146                         if (!getenv("SYNFIG_DISABLE_OPTIMIZE"))
147                         {
148                                 synfig::info("optimizing layers");
149                                 try
150                                 {
151                                         image_list = OptimizeImageLayers(image_list,&exceptionInfo);
152                                 }
153                                 catch(Magick::Warning warning) {
154                                         synfig::warning("exception '%s'", warning.what());
155                                 }
156                         }
157
158                         if (!getenv("SYNFIG_DISABLE_OPTIMIZE_TRANS"))
159                         {
160                                 synfig::info("optimizing layer transparency");
161                                 try
162                                 {
163                                         OptimizeImageTransparency(image_list,&exceptionInfo);
164                                 }
165                                 catch(Magick::Warning warning) {
166                                         synfig::warning("exception '%s'", warning.what());
167                                 }
168                         }
169
170                         synfig::info("recreating image list");
171                         insertImages(&images, image_list);
172 #else
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
177
178                         // linkImages(images.begin(), images.end());
179                         // MagickLib::Image* new_images = DeconstructImages(images.begin()->image(),&exceptionInfo);
180                         // unlinkImages(images.begin(), images.end());
181                         // images.clear();
182                         // insertImages(&images, new_images);
183 #endif
184                 }
185                 else if (multiple_images)
186                 {
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));
192                 }
193
194                 synfig::info("writing %d image%s to %s", images.size(), images.size() == 1 ? "" : "s", filename.c_str());
195                 try
196                 {
197                         Magick::writeImages(images.begin(), images.end(), filename);
198                         synfig::info("done");
199                 }
200                 catch(Magick::Warning warning) {
201                         synfig::warning("exception '%s'", warning.what());
202                 }
203         }
204         catch(Magick::Warning warning) {
205                 synfig::warning("exception '%s'", warning.what());
206         }
207         catch(Magick::Error error) {
208                 synfig::error("exception '%s'", error.what());
209         }
210         catch(...) {
211                 synfig::error("unknown exception");
212         }
213
214         if (buffer1 != NULL) delete [] buffer1;
215         if (buffer2 != NULL) delete [] buffer2;
216         if (color_buffer != NULL) delete [] color_buffer;
217 }
218
219 bool
220 magickpp_trgt::set_rend_desc(RendDesc *given_desc)
221 {
222         desc = *given_desc;
223         return true;
224 }
225
226 bool
227 magickpp_trgt::init()
228 {
229         width = desc.get_w();
230         height = desc.get_h();
231
232         start_pointer = NULL;
233
234         buffer1 = new unsigned char[4*width*height];
235         if (buffer1 == NULL)
236                 return false;
237
238         buffer2 = new unsigned char[4*width*height];
239         if (buffer2 == NULL)
240         {
241                 delete [] buffer1;
242                 return false;
243         }
244
245         color_buffer = new Color[width];
246         if (color_buffer == NULL)
247         {
248                 delete [] buffer1;
249                 delete [] buffer2;
250                 return false;
251         }
252
253         return true;
254 }
255
256 void
257 magickpp_trgt::end_frame()
258 {
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);
263 }
264
265 bool
266 magickpp_trgt::start_frame(synfig::ProgressCallback *callback __attribute__ ((unused)))
267 {
268         previous_buffer_pointer = start_pointer;
269
270         if (start_pointer == buffer1)
271                 start_pointer = buffer_pointer = buffer2;
272         else
273                 start_pointer = buffer_pointer = buffer1;
274
275         transparent = false;
276         return true;
277 }
278
279 Color*
280 magickpp_trgt::start_scanline(int scanline __attribute__ ((unused)))
281 {
282         return color_buffer;
283 }
284
285 bool
286 magickpp_trgt::end_scanline()
287 {
288         convert_color_format(buffer_pointer, color_buffer,
289                                                  width, PF_RGB|PF_A, gamma());
290
291         if (!transparent)
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
296                         {
297                                 transparent = true;
298                                 break;
299                         }
300
301         buffer_pointer += 4 * width;
302
303         if (previous_buffer_pointer)
304                 previous_buffer_pointer += 4 * width;
305
306         return true;
307 }