Probably makes sense to default to checking KDM validity ranges
[dcpomatic.git] / src / lib / image.cc
index 08507ec5f8d8e1517d4bd380c8e50885dc28e22e..75345cb068ae5363416c2823b5fb9cbbdd36c2c6 100644 (file)
@@ -37,6 +37,9 @@ extern "C" {
 #include <libavutil/pixdesc.h>
 #include <libavutil/frame.h>
 }
+#if HAVE_VALGRIND_MEMCHECK_H
+#include <valgrind/memcheck.h>
+#endif
 #include <iostream>
 
 #include "i18n.h"
@@ -791,6 +794,34 @@ Image::Image (AVPixelFormat p, dcp::Size s, bool aligned, int extra_pixels)
        allocate ();
 }
 
+/** Construct an Image from some PNG data */
+Image::Image (dcp::Data png)
+{
+       Magick::Blob blob;
+       blob.update (png.data().get(), png.size());
+       Magick::Image* magick_image = new Magick::Image (blob);
+       _size = dcp::Size(magick_image->columns(), magick_image->rows());
+       _pixel_format = AV_PIX_FMT_BGRA;
+       _aligned = true;
+       _extra_pixels = 0;
+       allocate ();
+
+       /* Write line-by-line here as _image must be aligned, and write() cannot be told about strides */
+       uint8_t* p = data()[0];
+       for (int i = 0; i < _size.height; ++i) {
+#ifdef DCPOMATIC_HAVE_MAGICKCORE_NAMESPACE
+               using namespace MagickCore;
+#endif
+#ifdef DCPOMATIC_HAVE_MAGICKLIB_NAMESPACE
+               using namespace MagickLib;
+#endif
+               magick_image->write (0, i, _size.width, 1, "BGRA", CharPixel, p);
+               p += stride()[0];
+       }
+
+       delete magick_image;
+}
+
 void
 Image::allocate ()
 {
@@ -822,6 +853,12 @@ Image::allocate ()
                   testing suggests that it works.
                */
                _data[i] = (uint8_t *) wrapped_av_malloc (_stride[i] * sample_size(i).height + _extra_pixels * bytes_per_pixel(i) + 32);
+#if HAVE_VALGRIND_MEMCHECK_H
+               /* The data between the end of the line size and the stride is undefined but processed by
+                  libswscale, causing lots of valgrind errors.  Mark it all defined to quell these errors.
+               */
+               VALGRIND_MAKE_MEM_DEFINED (_data[i], _stride[i] * sample_size(i).height + _extra_pixels * bytes_per_pixel(i) + 32);
+#endif
        }
 }