Show what's going on with messages to stdout. Fix another problem with transparent...
[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 = CloneImage(iter->image(), 0, 0, Magick::MagickTrue, &exceptionInfo);
68                 if (!first) first = current;
69
70                 current->previous = previous;
71                 current->next     = 0;
72
73                 if ( previous != 0) previous->next = current;
74                 previous = current;
75         }
76
77         return first;
78 }
79
80 magickpp_trgt::~magickpp_trgt()
81 {
82         MagickLib::ExceptionInfo exceptionInfo;
83         MagickLib::GetExceptionInfo(&exceptionInfo);
84
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);
89
90         // the file type is now in image.imageInfo()->magick and
91         // image.adjoin() tells us whether we can write to a single file
92         if (image.adjoin())
93         {
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));
97
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
105
106                 synfig::info("copying image list");
107                 MagickLib::Image *image_list = copy_image_list(images);
108
109                 synfig::info("clearing old image list");
110                 images.clear();
111
112                 synfig::info("removing duplicate frames");
113                 RemoveDuplicateLayers(&image_list, &exceptionInfo);
114
115                 synfig::info("optimizing frames");
116                 OptimizeImageTransparency(image_list,&exceptionInfo);
117
118                 synfig::info("recreating image list");
119                 insertImages(&images, image_list);
120 #else
121                 synfig::info("not optimizing images");
122                 // DeconstructImages is available in ImageMagic 6.2.* but it doesn't take
123                 // the 'dispose' method into account, so for frames with transparency where
124                 // nothing is moving, we end up with objects disappearing when they shouldn't
125
126                 // linkImages(images.begin(), images.end());
127                 // MagickLib::Image* new_images = DeconstructImages(images.begin()->image(),&exceptionInfo);
128                 // unlinkImages(images.begin(), images.end());
129                 // images.clear();
130                 // insertImages(&images, new_images);
131 #endif
132         }
133         else
134         {
135                 // if we can't write multiple images to a file of this type,
136                 // include '%04d' in the filename, so the files will be numbered
137                 // with a fixed width, '0'-padded number
138                 synfig::info("can't join images of this type - numbering instead");
139                 filename = (filename_sans_extension(filename) + ".%04d" + filename_extension(filename));
140         }
141
142         synfig::info("writing %d images to %s", images.size(), filename.c_str());
143         Magick::writeImages(images.begin(), images.end(), filename);
144
145         if (buffer != NULL) delete [] buffer;
146         if (color_buffer != NULL) delete [] color_buffer;
147 }
148
149 bool
150 magickpp_trgt::set_rend_desc(RendDesc *given_desc)
151 {
152         desc = *given_desc;
153         return true;
154 }
155
156 bool
157 magickpp_trgt::init()
158 {
159         width = desc.get_w();
160         height = desc.get_h();
161
162         buffer = new unsigned char[4*width*height];
163         if (buffer == NULL)
164                 return false;
165
166         color_buffer = new Color[width];
167         if (color_buffer == NULL)
168         {
169                 delete [] buffer;
170                 return false;
171         }
172
173         return true;
174 }
175
176 void
177 magickpp_trgt::end_frame()
178 {
179         Magick::Image image(width, height, "RGBA", Magick::CharPixel, buffer);
180         if (transparent && images.begin() != images.end())
181                 (images.end()-1)->gifDisposeMethod(Magick::BackgroundDispose);
182         images.push_back(image);
183 }
184
185 bool
186 magickpp_trgt::start_frame(synfig::ProgressCallback *callback)
187 {
188         buffer_pointer = buffer;
189         transparent = false;
190         return true;
191 }
192
193 Color*
194 magickpp_trgt::start_scanline(int scanline)
195 {
196         return color_buffer;
197 }
198
199 bool
200 magickpp_trgt::end_scanline()
201 {
202         convert_color_format(buffer_pointer, color_buffer, width, PF_RGB|PF_A, gamma());
203
204         if (!transparent)
205                 for (int i = 0; i < width; i++)
206                         if (buffer[i*4 + 3] < 128)
207                         {
208                                 transparent = true;
209                                 break;
210                         }
211
212         buffer_pointer += 4 * width;
213
214         return true;
215 }