#include "dcpomatic_socket.h"
#include <dcp/rgb_xyz.h>
#include <dcp/transfer_function.h>
+#include <Magick++.h>
extern "C" {
#include <libswscale/swscale.h>
#include <libavutil/pixfmt.h>
*/
shared_ptr<Image>
Image::crop_scale_window (
- Crop crop, dcp::Size inter_size, dcp::Size out_size, dcp::YUVToRGB yuv_to_rgb, AVPixelFormat out_format, bool out_aligned, bool fast
+ Crop crop, dcp::Size inter_size, dcp::Size out_size, dcp::YUVToRGB yuv_to_rgb, AVPixelFormat out_format, bool out_full_range, bool out_aligned, bool fast
) const
{
/* Empirical testing suggests that sws_scale() will crash if
To get around this, we ask Image to overallocate its buffers by the overrun.
*/
- shared_ptr<Image> out (new Image (out_format, out_size, out_aligned, (out_size.width - inter_size.width) / 2));
+ shared_ptr<Image> out (new Image (out_format, out_full_range, out_size, out_aligned, (out_size.width - inter_size.width) / 2));
out->make_black ();
/* Size of the image after any crop */
sws_setColorspaceDetails (
scale_context,
- sws_getCoefficients (lut[yuv_to_rgb]), 0,
- sws_getCoefficients (lut[yuv_to_rgb]), 0,
+ sws_getCoefficients (lut[yuv_to_rgb]), _full_range,
+ sws_getCoefficients (lut[yuv_to_rgb]), out_full_range,
0, 1 << 16, 1 << 16
);
- AVPixFmtDescriptor const * desc = av_pix_fmt_desc_get (_pixel_format);
- if (!desc) {
+ AVPixFmtDescriptor const * in_desc = av_pix_fmt_desc_get (_pixel_format);
+ if (!in_desc) {
throw PixelFormatError ("crop_scale_window()", _pixel_format);
}
round down so that we don't crop a subsampled pixel until
we've cropped all of its Y-channel pixels.
*/
- int const x = lrintf (bytes_per_pixel(c) * crop.left) & ~ ((int) desc->log2_chroma_w);
+ int const x = lrintf (bytes_per_pixel(c) * crop.left) & ~ ((int) in_desc->log2_chroma_w);
scale_in_data[c] = data()[c] + x + stride()[c] * (crop.top / vertical_factor(c));
}
/* Corner of the image within out_size */
Position<int> const corner ((out_size.width - inter_size.width) / 2, (out_size.height - inter_size.height) / 2);
+ AVPixFmtDescriptor const * out_desc = av_pix_fmt_desc_get (out_format);
+ if (!out_desc) {
+ throw PixelFormatError ("crop_scale_window()", out_format);
+ }
+
uint8_t* scale_out_data[out->planes()];
for (int c = 0; c < out->planes(); ++c) {
- scale_out_data[c] = out->data()[c] + lrintf (out->bytes_per_pixel(c) * corner.x) + out->stride()[c] * (corner.y / out->vertical_factor(c));
+ /* See the note in the crop loop above */
+ int const x = lrintf (out->bytes_per_pixel(c) * corner.x) & ~ ((int) out_desc->log2_chroma_w);
+ scale_out_data[c] = out->data()[c] + x + out->stride()[c] * (corner.y / out->vertical_factor(c));
}
sws_scale (
}
shared_ptr<Image>
-Image::convert_pixel_format (dcp::YUVToRGB yuv_to_rgb, AVPixelFormat out_format, bool out_aligned, bool fast) const
+Image::convert_pixel_format (dcp::YUVToRGB yuv_to_rgb, AVPixelFormat out_format, bool out_full_range, bool out_aligned, bool fast) const
{
- return scale(size(), yuv_to_rgb, out_format, out_aligned, fast);
+ return scale(size(), yuv_to_rgb, out_format, out_full_range, out_aligned, fast);
}
/** @param out_size Size to scale to.
* fast bilinear rather than bicubic scaling.
*/
shared_ptr<Image>
-Image::scale (dcp::Size out_size, dcp::YUVToRGB yuv_to_rgb, AVPixelFormat out_format, bool out_aligned, bool fast) const
+Image::scale (dcp::Size out_size, dcp::YUVToRGB yuv_to_rgb, AVPixelFormat out_format, bool out_full_range, bool out_aligned, bool fast) const
{
/* Empirical testing suggests that sws_scale() will crash if
the input image is not aligned.
*/
DCPOMATIC_ASSERT (aligned ());
- shared_ptr<Image> scaled (new Image (out_format, out_size, out_aligned));
+ shared_ptr<Image> scaled (new Image (out_format, out_full_range, out_size, out_aligned));
struct SwsContext* scale_context = sws_getContext (
size().width, size().height, pixel_format(),
out_size.width, out_size.height, out_format,
- fast ? SWS_FAST_BILINEAR : SWS_BICUBIC, 0, 0, 0
+ (fast ? SWS_FAST_BILINEAR : SWS_BICUBIC) | SWS_ACCURATE_RND, 0, 0, 0
);
DCPOMATIC_ASSERT (yuv_to_rgb < dcp::YUV_TO_RGB_COUNT);
}
case AV_PIX_FMT_YUV420P:
{
- shared_ptr<Image> yuv = other->convert_pixel_format (dcp::YUV_TO_RGB_REC709, _pixel_format, false, false);
+ shared_ptr<Image> yuv = other->convert_pixel_format (dcp::YUV_TO_RGB_REC709, _pixel_format, _full_range, false, false);
dcp::Size const ts = size();
dcp::Size const os = yuv->size();
for (int ty = start_ty, oy = start_oy; ty < ts.height && oy < os.height; ++ty, ++oy) {
}
case AV_PIX_FMT_YUV420P10:
{
- shared_ptr<Image> yuv = other->convert_pixel_format (dcp::YUV_TO_RGB_REC709, _pixel_format, false, false);
+ shared_ptr<Image> yuv = other->convert_pixel_format (dcp::YUV_TO_RGB_REC709, _pixel_format, _full_range, false, false);
dcp::Size const ts = size();
dcp::Size const os = yuv->size();
for (int ty = start_ty, oy = start_oy; ty < ts.height && oy < os.height; ++ty, ++oy) {
}
case AV_PIX_FMT_YUV422P10LE:
{
- shared_ptr<Image> yuv = other->convert_pixel_format (dcp::YUV_TO_RGB_REC709, _pixel_format, false, false);
+ shared_ptr<Image> yuv = other->convert_pixel_format (dcp::YUV_TO_RGB_REC709, _pixel_format, _full_range, false, false);
dcp::Size const ts = size();
dcp::Size const os = yuv->size();
for (int ty = start_ty, oy = start_oy; ty < ts.height && oy < os.height; ++ty, ++oy) {
* @param aligned true to make each row of this image aligned to a 32-byte boundary.
* @param extra_pixels Amount of extra "run-off" memory to allocate at the end of each plane in pixels.
*/
-Image::Image (AVPixelFormat p, dcp::Size s, bool aligned, int extra_pixels)
+Image::Image (AVPixelFormat p, bool f, dcp::Size s, bool aligned, int extra_pixels)
: _size (s)
, _pixel_format (p)
+ , _full_range (f)
, _aligned (aligned)
, _extra_pixels (extra_pixels)
{
}
Image::Image (Image const & other)
- : _size (other._size)
+ : boost::enable_shared_from_this<Image>(other)
+ , _size (other._size)
, _pixel_format (other._pixel_format)
+ , _full_range (other._full_range)
, _aligned (other._aligned)
, _extra_pixels (other._extra_pixels)
{
q += frame->linesize[i];
}
}
+
+ switch (frame->color_range) {
+ case AVCOL_RANGE_MPEG:
+ _full_range = false;
+ break;
+ case AVCOL_RANGE_JPEG:
+ _full_range = true;
+ break;
+ default:
+ DCPOMATIC_ASSERT (false);
+ }
}
Image::Image (shared_ptr<const Image> other, bool aligned)
: _size (other->_size)
, _pixel_format (other->_pixel_format)
+ , _full_range (other->_full_range)
, _aligned (aligned)
, _extra_pixels (other->_extra_pixels)
{
{
std::swap (_size, other._size);
std::swap (_pixel_format, other._pixel_format);
+ std::swap (_full_range, other._full_range);
for (int i = 0; i < 4; ++i) {
std::swap (_data[i], other._data[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));
+ shared_ptr<Image> merged (new Image (images.front().image->pixel_format(), images.front().image->full_range(), 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 - all.position());
bool
operator== (Image const & a, Image const & b)
{
- if (a.planes() != b.planes() || a.pixel_format() != b.pixel_format() || a.aligned() != b.aligned()) {
+ if (a.planes() != b.planes() || a.pixel_format() != b.pixel_format() || a.full_range() != b.full_range() || a.aligned() != b.aligned()) {
return false;
}
}
}
-shared_ptr<Image>
-Image::ensure_aligned (shared_ptr<Image> image)
+shared_ptr<const Image>
+Image::ensure_aligned (shared_ptr<const Image> image)
{
if (image->aligned()) {
return image;
}
return m;
}
+
+dcp::Data
+Image::as_png () const
+{
+#ifdef DCPOMATIC_IMAGE_MAGICK
+ using namespace MagickCore;
+#else
+ using namespace MagickLib;
+#endif
+
+ /* XXX: take full_range into account here */
+
+ string format;
+ switch (_pixel_format) {
+ case AV_PIX_FMT_RGB24:
+ format = "RGB";
+ break;
+ case AV_PIX_FMT_BGRA:
+ format = "BGRA";
+ break;
+ default:
+ DCPOMATIC_ASSERT (false);
+ break;
+ }
+
+ shared_ptr<const Image> use;
+ if (aligned()) {
+ use.reset (new Image(shared_from_this(), false));
+ }
+
+ Magick::Image m (size().width, size().height, format, CharPixel, (void *) use->data()[0]);
+ m.magick ("PNG");
+ Magick::Blob blob;
+ m.write (&blob);
+ /* XXX: could use a subclass of Data here (storing its data in a Blob) */
+ return dcp::Data (static_cast<const uint8_t*>(blob.data()), blob.length());
+}