Don't say "writing 1 images".
[synfig.git] / synfig-core / trunk / 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 2007 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                 catch(Magick::Warning warning) {
74                         synfig::warning("exception '%s'", warning.what());
75                 }
76                         
77                 if (!first) first = current;
78
79                 current->previous = previous;
80                 current->next     = 0;
81
82                 if ( previous != 0) previous->next = current;
83                 previous = current;
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                 // check whether this file format supports multiple-image files
97                 Magick::Image image(*(images.begin()));
98                 image.fileName(filename);
99                 try
100                 {
101                         SetImageInfo(image.imageInfo(),Magick::MagickTrue,&exceptionInfo);
102                 }
103                 catch(Magick::Warning warning) {
104                         synfig::warning("exception '%s'", warning.what());
105                 }
106
107                 // the file type is now in image.imageInfo()->magick and
108                 // image.adjoin() tells us whether we can write to a single file
109                 if (image.adjoin())
110                 {
111                         synfig::info("joining images");
112                         unsigned int delay = round_to_int(100.0 / desc.get_frame_rate());
113                         for_each(images.begin(), images.end(), Magick::animationDelayImage(delay));
114
115                         // optimize the images (only write the pixels that change from frame to frame
116 #ifdef HAVE_MAGICK_OPTIMIZE
117                         // make a completely new image list
118                         // this is required because:
119                         //   RemoveDuplicateLayers wants a linked list of images, and removes some of them
120                         //   when it removes an image, it invalidates it using DeleteImageFromList, but we still have it in our container
121                         //   when we destroy our container, the image is re-freed, failing an assertion
122
123                         synfig::info("copying image list");
124                         MagickLib::Image *image_list = copy_image_list(images);
125
126                         synfig::info("clearing old image list");
127                         images.clear();
128
129                         if (!getenv("SYNFIG_DISABLE_REMOVE_DUPS"))
130                         {
131                                 synfig::info("removing duplicate frames");
132                                 try
133                                 {
134                                         RemoveDuplicateLayers(&image_list, &exceptionInfo);
135                                 }
136                                 catch(Magick::Warning warning) {
137                                         synfig::warning("exception '%s'", warning.what());
138                                 }
139                         }
140
141                         if (!getenv("SYNFIG_DISABLE_OPTIMIZE"))
142                         {
143                                 synfig::info("optimizing layers");
144                                 try
145                                 {
146                                         image_list = OptimizeImageLayers(image_list,&exceptionInfo);
147                                 }
148                                 catch(Magick::Warning warning) {
149                                         synfig::warning("exception '%s'", warning.what());
150                                 }
151                         }
152
153                         if (!getenv("SYNFIG_DISABLE_OPTIMIZE_TRANS"))
154                         {
155                                 synfig::info("optimizing layer transparency");
156                                 try
157                                 {
158                                         OptimizeImageTransparency(image_list,&exceptionInfo);
159                                 }
160                                 catch(Magick::Warning warning) {
161                                         synfig::warning("exception '%s'", warning.what());
162                                 }
163                         }
164
165                         synfig::info("recreating image list");
166                         insertImages(&images, image_list);
167 #else
168                         synfig::info("not optimizing images");
169                         // DeconstructImages is available in ImageMagic 6.2.* but it doesn't take
170                         // the 'dispose' method into account, so for frames with transparency where
171                         // nothing is moving, we end up with objects disappearing when they shouldn't
172
173                         // linkImages(images.begin(), images.end());
174                         // MagickLib::Image* new_images = DeconstructImages(images.begin()->image(),&exceptionInfo);
175                         // unlinkImages(images.begin(), images.end());
176                         // images.clear();
177                         // insertImages(&images, new_images);
178 #endif
179                 }
180                 else
181                 {
182                         // if we can't write multiple images to a file of this type,
183                         // include '%04d' in the filename, so the files will be numbered
184                         // with a fixed width, '0'-padded number
185                         synfig::info("can't join images of this type - numbering instead");
186                         filename = (filename_sans_extension(filename) + ".%04d" + filename_extension(filename));
187                 }
188
189                 synfig::info("writing %d image%s to %s", images.size(), images.size() == 1 ? "" : "s", filename.c_str());
190                 try
191                 {
192                         Magick::writeImages(images.begin(), images.end(), filename);
193                 }
194                 catch(Magick::Warning warning) {
195                         synfig::warning("exception '%s'", warning.what());
196                 }
197         }
198         catch(Magick::Warning warning) {
199                 synfig::warning("exception '%s'", warning.what());
200         }
201         catch(Magick::Error error) {
202                 synfig::error("exception '%s'", error.what());
203         }
204         catch(...) {
205                 synfig::error("unknown exception");
206         }
207
208         if (buffer1 != NULL) delete [] buffer1;
209         if (buffer2 != NULL) delete [] buffer2;
210         if (color_buffer != NULL) delete [] color_buffer;
211 }
212
213 bool
214 magickpp_trgt::set_rend_desc(RendDesc *given_desc)
215 {
216         desc = *given_desc;
217         return true;
218 }
219
220 bool
221 magickpp_trgt::init()
222 {
223         width = desc.get_w();
224         height = desc.get_h();
225
226         start_pointer = NULL;
227
228         buffer1 = new unsigned char[4*width*height];
229         if (buffer1 == NULL)
230                 return false;
231
232         buffer2 = new unsigned char[4*width*height];
233         if (buffer2 == NULL)
234         {
235                 delete [] buffer1;
236                 return false;
237         }
238
239         color_buffer = new Color[width];
240         if (color_buffer == NULL)
241         {
242                 delete [] buffer1;
243                 delete [] buffer2;
244                 return false;
245         }
246
247         return true;
248 }
249
250 void
251 magickpp_trgt::end_frame()
252 {
253         Magick::Image image(width, height, "RGBA", Magick::CharPixel, start_pointer);
254         if (transparent && images.begin() != images.end())
255                 (images.end()-1)->gifDisposeMethod(Magick::BackgroundDispose);
256         images.push_back(image);
257 }
258
259 bool
260 magickpp_trgt::start_frame(synfig::ProgressCallback *callback)
261 {
262         previous_buffer_pointer = start_pointer;
263
264         if (start_pointer == buffer1)
265                 start_pointer = buffer_pointer = buffer2;
266         else
267                 start_pointer = buffer_pointer = buffer1;
268
269         transparent = false;
270         return true;
271 }
272
273 Color*
274 magickpp_trgt::start_scanline(int scanline)
275 {
276         return color_buffer;
277 }
278
279 bool
280 magickpp_trgt::end_scanline()
281 {
282         convert_color_format(buffer_pointer, color_buffer,
283                                                  width, PF_RGB|PF_A, gamma());
284
285         if (!transparent)
286                 for (int i = 0; i < width; i++)
287                         if (previous_buffer_pointer &&                                  // this isn't the first frame
288                                 buffer_pointer[i*4 + 3] < 128 &&                        // our pixel is transparent
289                                 !(previous_buffer_pointer[i*4 + 3] < 128))      // the previous frame's pixel wasn't
290                         {
291                                 transparent = true;
292                                 break;
293                         }
294
295         buffer_pointer += 4 * width;
296
297         if (previous_buffer_pointer)
298                 previous_buffer_pointer += 4 * width;
299
300         return true;
301 }