Added copyright lines for files I've edited this year.
[synfig.git] / synfig-core / trunk / src / modules / mod_magickpp / trgt_magickpp.cpp
index 9c55627..bcfd31a 100644 (file)
@@ -5,7 +5,7 @@
 **     $Id$
 **
 **     \legal
-**     Copyright 2007 Chris Moore
+**     Copyright (c) 2007, 2008 Chris Moore
 **
 **     This package is free software; you can redistribute it and/or
 **     modify it under the terms of the GNU General Public License as
@@ -64,14 +64,23 @@ MagickLib::Image* copy_image_list(Container& container)
        MagickLib::GetExceptionInfo(&exceptionInfo);
        for (Iter iter = container.begin(); iter != container.end(); ++iter)
        {
-               MagickLib::Image* current = CloneImage(iter->image(), 0, 0, Magick::MagickTrue, &exceptionInfo);
-               if (!first) first = current;
+               MagickLib::Image* current;
 
-               current->previous = previous;
-               current->next     = 0;
+               try
+               {
+                       current = CloneImage(iter->image(), 0, 0, Magick::MagickTrue, &exceptionInfo);
+
+                       if (!first) first = current;
+
+                       current->previous = previous;
+                       current->next     = 0;
 
-               if ( previous != 0) previous->next = current;
-               previous = current;
+                       if ( previous != 0) previous->next = current;
+                       previous = current;
+               }
+               catch(Magick::Warning warning) {
+                       synfig::warning("exception '%s'", warning.what());
+               }
        }
 
        return first;
@@ -82,77 +91,126 @@ magickpp_trgt::~magickpp_trgt()
        MagickLib::ExceptionInfo exceptionInfo;
        MagickLib::GetExceptionInfo(&exceptionInfo);
 
-       // check whether this file format supports multiple-image files
-       Magick::Image image(*(images.begin()));
-       image.fileName(filename);
-       SetImageInfo(image.imageInfo(),Magick::MagickTrue,&exceptionInfo);
-
-       // the file type is now in image.imageInfo()->magick and
-       // image.adjoin() tells us whether we can write to a single file
-       if (image.adjoin())
+       try
        {
-               synfig::info("joining images");
-               unsigned int delay = round_to_int(100.0 / desc.get_frame_rate());
-               for_each(images.begin(), images.end(), Magick::animationDelayImage(delay));
+               bool multiple_images = images.size() != 1;
+               bool can_adjoin = false;
+
+               if (multiple_images)
+               {
+                       // check whether this file format supports multiple-image files
+                       Magick::Image image(*(images.begin()));
+                       image.fileName(filename);
+                       try
+                       {
+                               SetImageInfo(image.imageInfo(),Magick::MagickTrue,&exceptionInfo);
+                               can_adjoin = image.adjoin();
+                       }
+                       catch(Magick::Warning warning) {
+                               synfig::warning("exception '%s'", warning.what());
+                       }
+               }
+
+               // the file type is now in image.imageInfo()->magick and
+               // image.adjoin() tells us whether we can write to a single file
+               if (can_adjoin)
+               {
+                       synfig::info("joining images");
+                       unsigned int delay = round_to_int(100.0 / desc.get_frame_rate());
+                       for_each(images.begin(), images.end(), Magick::animationDelayImage(delay));
 
-               // optimize the images (only write the pixels that change from frame to frame
+                       // optimize the images (only write the pixels that change from frame to frame
 #ifdef HAVE_MAGICK_OPTIMIZE
-               // make a completely new image list
-               // this is required because:
-               //   RemoveDuplicateLayers wants a linked list of images, and removes some of them
-               //   when it removes an image, it invalidates it using DeleteImageFromList, but we still have it in our container
-               //   when we destroy our container, the image is re-freed, failing an assertion
+                       // make a completely new image list
+                       // this is required because:
+                       //   RemoveDuplicateLayers wants a linked list of images, and removes some of them
+                       //   when it removes an image, it invalidates it using DeleteImageFromList, but we still have it in our container
+                       //   when we destroy our container, the image is re-freed, failing an assertion
 
-               synfig::info("copying image list");
-               MagickLib::Image *image_list = copy_image_list(images);
+                       synfig::info("copying image list");
+                       MagickLib::Image *image_list = copy_image_list(images);
 
-               synfig::info("clearing old image list");
-               images.clear();
+                       synfig::info("clearing old image list");
+                       images.clear();
 
-               if (!getenv("SYNFIG_DISABLE_REMOVE_DUPS"))
-               {
-                       synfig::info("removing duplicate frames");
-                       RemoveDuplicateLayers(&image_list, &exceptionInfo);
-               }
+                       if (!getenv("SYNFIG_DISABLE_REMOVE_DUPS"))
+                       {
+                               synfig::info("removing duplicate frames");
+                               try
+                               {
+                                       RemoveDuplicateLayers(&image_list, &exceptionInfo);
+                               }
+                               catch(Magick::Warning warning) {
+                                       synfig::warning("exception '%s'", warning.what());
+                               }
+                       }
 
-               if (!getenv("SYNFIG_DISABLE_OPTIMIZE"))
+                       if (!getenv("SYNFIG_DISABLE_OPTIMIZE"))
+                       {
+                               synfig::info("optimizing layers");
+                               try
+                               {
+                                       image_list = OptimizeImageLayers(image_list,&exceptionInfo);
+                               }
+                               catch(Magick::Warning warning) {
+                                       synfig::warning("exception '%s'", warning.what());
+                               }
+                       }
+
+                       if (!getenv("SYNFIG_DISABLE_OPTIMIZE_TRANS"))
+                       {
+                               synfig::info("optimizing layer transparency");
+                               try
+                               {
+                                       OptimizeImageTransparency(image_list,&exceptionInfo);
+                               }
+                               catch(Magick::Warning warning) {
+                                       synfig::warning("exception '%s'", warning.what());
+                               }
+                       }
+
+                       synfig::info("recreating image list");
+                       insertImages(&images, image_list);
+#else
+                       synfig::info("not optimizing images");
+                       // DeconstructImages is available in ImageMagic 6.2.* but it doesn't take
+                       // the 'dispose' method into account, so for frames with transparency where
+                       // nothing is moving, we end up with objects disappearing when they shouldn't
+
+                       // linkImages(images.begin(), images.end());
+                       // MagickLib::Image* new_images = DeconstructImages(images.begin()->image(),&exceptionInfo);
+                       // unlinkImages(images.begin(), images.end());
+                       // images.clear();
+                       // insertImages(&images, new_images);
+#endif
+               }
+               else if (multiple_images)
                {
-                       synfig::info("optimizing layers");
-                       image_list = OptimizeImageLayers(image_list,&exceptionInfo);
+                       // if we can't write multiple images to a file of this type,
+                       // include '%04d' in the filename, so the files will be numbered
+                       // with a fixed width, '0'-padded number
+                       synfig::info("can't join images of this type - numbering instead");
+                       filename = (filename_sans_extension(filename) + ".%04d" + filename_extension(filename));
                }
 
-               if (!getenv("SYNFIG_DISABLE_OPTIMIZE_TRANS"))
+               synfig::info("writing %d image%s to %s", images.size(), images.size() == 1 ? "" : "s", filename.c_str());
+               try
                {
-                       synfig::info("optimizing layer transparency");
-                       OptimizeImageTransparency(image_list,&exceptionInfo);
+                       Magick::writeImages(images.begin(), images.end(), filename);
+               }
+               catch(Magick::Warning warning) {
+                       synfig::warning("exception '%s'", warning.what());
                }
-
-               synfig::info("recreating image list");
-               insertImages(&images, image_list);
-#else
-               synfig::info("not optimizing images");
-               // DeconstructImages is available in ImageMagic 6.2.* but it doesn't take
-               // the 'dispose' method into account, so for frames with transparency where
-               // nothing is moving, we end up with objects disappearing when they shouldn't
-
-               // linkImages(images.begin(), images.end());
-               // MagickLib::Image* new_images = DeconstructImages(images.begin()->image(),&exceptionInfo);
-               // unlinkImages(images.begin(), images.end());
-               // images.clear();
-               // insertImages(&images, new_images);
-#endif
        }
-       else
-       {
-               // if we can't write multiple images to a file of this type,
-               // include '%04d' in the filename, so the files will be numbered
-               // with a fixed width, '0'-padded number
-               synfig::info("can't join images of this type - numbering instead");
-               filename = (filename_sans_extension(filename) + ".%04d" + filename_extension(filename));
+       catch(Magick::Warning warning) {
+               synfig::warning("exception '%s'", warning.what());
+       }
+       catch(Magick::Error error) {
+               synfig::error("exception '%s'", error.what());
+       }
+       catch(...) {
+               synfig::error("unknown exception");
        }
-
-       synfig::info("writing %d images to %s", images.size(), filename.c_str());
-       Magick::writeImages(images.begin(), images.end(), filename);
 
        if (buffer1 != NULL) delete [] buffer1;
        if (buffer2 != NULL) delete [] buffer2;
@@ -206,7 +264,7 @@ magickpp_trgt::end_frame()
 }
 
 bool
-magickpp_trgt::start_frame(synfig::ProgressCallback *callback)
+magickpp_trgt::start_frame(synfig::ProgressCallback *callback __attribute__ ((unused)))
 {
        previous_buffer_pointer = start_pointer;
 
@@ -220,7 +278,7 @@ magickpp_trgt::start_frame(synfig::ProgressCallback *callback)
 }
 
 Color*
-magickpp_trgt::start_scanline(int scanline)
+magickpp_trgt::start_scanline(int scanline __attribute__ ((unused)))
 {
        return color_buffer;
 }