Merge master.
[dcpomatic.git] / src / lib / image.cc
index 926aefd3662659583f0116abcd76abb9e16439c1..d4ec6f99a6381070835b2fd54a875f414e5d7f87 100644 (file)
@@ -22,6 +22,7 @@
  */
 
 #include <iostream>
+#include <openssl/md5.h>
 extern "C" {
 #include <libswscale/swscale.h>
 #include <libavutil/pixfmt.h>
@@ -31,6 +32,7 @@ extern "C" {
 #include "exceptions.h"
 #include "scaler.h"
 #include "timer.h"
+#include "rect.h"
 
 #include "i18n.h"
 
@@ -38,6 +40,8 @@ using std::string;
 using std::min;
 using std::cout;
 using std::cerr;
+using std::list;
+using std::stringstream;
 using boost::shared_ptr;
 using dcp::Size;
 
@@ -342,6 +346,16 @@ Image::make_black ()
        }
 }
 
+void
+Image::make_transparent ()
+{
+       if (_pixel_format != PIX_FMT_RGBA) {
+               throw PixelFormatError ("make_transparent()", _pixel_format);
+       }
+
+       memset (data()[0], 0, lines(0) * stride()[0]);
+}
+
 void
 Image::alpha_blend (shared_ptr<const Image> other, Position<int> position)
 {
@@ -630,3 +644,44 @@ Image::aligned () const
        return _aligned;
 }
 
+PositionImage
+merge (list<PositionImage> images)
+{
+       if (images.empty ()) {
+               return PositionImage ();
+       }
+
+       dcpomatic::Rect<int> all (images.front().position, images.front().image->size().width, images.front().image->size().height);
+       for (list<PositionImage>::const_iterator i = images.begin(); i != images.end(); ++i) {
+               all.extend (dcpomatic::Rect<int> (i->position, i->image->size().width, i->image->size().height));
+       }
+
+       shared_ptr<Image> merged (new Image (images.front().image->pixel_format (), dcp::Size (all.width, all.height), true));
+       merged->make_transparent ();
+       for (list<PositionImage>::const_iterator i = images.begin(); i != images.end(); ++i) {
+               merged->alpha_blend (i->image, i->position);
+       }
+
+       return PositionImage (merged, all.position ());
+}
+
+string
+Image::digest () const
+{
+       MD5_CTX md5_context;
+       MD5_Init (&md5_context);
+
+       for (int i = 0; i < components(); ++i) {
+               MD5_Update (&md5_context, data()[i], line_size()[i]);
+       }
+       
+       unsigned char digest[MD5_DIGEST_LENGTH];
+       MD5_Final (digest, &md5_context);
+       
+       stringstream s;
+       for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
+               s << std::hex << std::setfill('0') << std::setw(2) << ((int) digest[i]);
+       }
+
+       return s.str ();
+}