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
105 MagickLib::Image *image_list = copy_image_list(images);
107 RemoveDuplicateLayers(&image_list, &exceptionInfo);
108 insertImages(&images, image_list);
110 linkImages(images.begin(), images.end());
111 OptimizeImageTransparency(images.begin()->image(),&exceptionInfo);
112 unlinkImages(images.begin(), images.end());
114 synfig::info("not optimizing images");
115 // DeconstructImages is available in ImageMagic 6.2.* but it doesn't take
116 // the 'dispose' method into account, so for frames with transparency where
117 // nothing is moving, we end up with objects disappearing when they shouldn't
119 // linkImages(images.begin(), images.end());
120 // MagickLib::Image* new_images = DeconstructImages(images.begin()->image(),&exceptionInfo);
121 // unlinkImages(images.begin(), images.end());
123 // insertImages(&images, new_images);
128 // if we can't write multiple images to a file of this type,
129 // include '%04d' in the filename, so the files will be numbered
130 // with a fixed width, '0'-padded number
131 synfig::info("can't join images of this type - numbering instead");
132 filename = (filename_sans_extension(filename) + ".%04d" + filename_extension(filename));
135 synfig::info("writing %d images to %s", images.size(), filename.c_str());
136 Magick::writeImages(images.begin(), images.end(), filename);
138 if (buffer != NULL) delete [] buffer;
139 if (color_buffer != NULL) delete [] color_buffer;
143 magickpp_trgt::set_rend_desc(RendDesc *given_desc)
150 magickpp_trgt::init()
152 width = desc.get_w();
153 height = desc.get_h();
155 buffer = new unsigned char[4*width*height];
159 color_buffer = new Color[width];
160 if (color_buffer == NULL)
170 magickpp_trgt::end_frame()
172 Magick::Image image(width, height, "RGBA", Magick::CharPixel, buffer);
173 if (transparent) image.gifDisposeMethod(Magick::PreviousDispose);
174 images.push_back(image);
178 magickpp_trgt::start_frame(synfig::ProgressCallback *callback)
180 buffer_pointer = buffer;
186 magickpp_trgt::start_scanline(int scanline)
192 magickpp_trgt::end_scanline()
194 convert_color_format(buffer_pointer, color_buffer, width, PF_RGB|PF_A, gamma());
197 for (int i = 0; i < width; i++)
198 if (buffer[i*4 + 3] < 128)
204 buffer_pointer += 4 * width;