Do full gamma correction etc. when alpha-blending subtitles
authorCarl Hetherington <cth@carlh.net>
Mon, 22 Aug 2016 13:26:28 +0000 (14:26 +0100)
committerCarl Hetherington <cth@carlh.net>
Mon, 22 Aug 2016 13:26:28 +0000 (14:26 +0100)
onto XYZ images.  This fixes colour tints on subtitles burnt
into existing DCPs.

ChangeLog
src/lib/image.cc
test/burnt_subtitle_test.cc
test/data

index 754e1ccfa36d472282c2006faad9fd31055b1670..ca05233979024b6e46a58bae0ebd993b0daadd0c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2016-08-22  c.hetherington  <cth@carlh.net>
+
+       * Fix colour tint in subtitles burnt into existing DCP
+       content.
+
 2016-08-22  Carl Hetherington  <cth@carlh.net>
 
        * Version 2.9.14 released.
index 1735bb38ad81433a9e482e3628b5a4c0175dde46..13ef0db19872cada4e8f89cc18b0496c55e1fe4f 100644 (file)
@@ -28,6 +28,8 @@
 #include "rect.h"
 #include "util.h"
 #include "dcpomatic_socket.h"
+#include <dcp/rgb_xyz.h>
+#include <dcp/transfer_function.h>
 extern "C" {
 #include <libswscale/swscale.h>
 #include <libavutil/pixfmt.h>
@@ -40,6 +42,7 @@ extern "C" {
 
 using std::string;
 using std::min;
+using std::max;
 using std::cout;
 using std::cerr;
 using std::list;
@@ -495,25 +498,34 @@ Image::alpha_blend (shared_ptr<const Image> other, Position<int> position)
        }
        case AV_PIX_FMT_XYZ12LE:
        {
-               boost::numeric::ublas::matrix<double> matrix = dcp::ColourConversion::srgb_to_xyz().rgb_to_xyz();
+               dcp::ColourConversion conv = dcp::ColourConversion::srgb_to_xyz();
+               double fast_matrix[9];
+               dcp::combined_rgb_to_xyz (conv, fast_matrix);
+               double const * lut_in = conv.in()->lut (8, false);
+               double const * lut_out = conv.out()->lut (16, true);
                int const this_bpp = 6;
                for (int ty = start_ty, oy = start_oy; ty < size().height && oy < other->size().height; ++ty, ++oy) {
-                       uint8_t* tp = data()[0] + ty * stride()[0] + start_tx * this_bpp;
+                       uint16_t* tp = reinterpret_cast<uint16_t*> (data()[0] + ty * stride()[0] + start_tx * this_bpp);
                        uint8_t* op = other->data()[0] + oy * other->stride()[0];
                        for (int tx = start_tx, ox = start_ox; tx < size().width && ox < other->size().width; ++tx, ++ox) {
                                float const alpha = float (op[3]) / 255;
 
-                               /* Convert sRGB to XYZ; op is BGRA */
-                               int const x = matrix(0, 0) * op[2] + matrix(0, 1) * op[1] + matrix(0, 2) * op[0];
-                               int const y = matrix(1, 0) * op[2] + matrix(1, 1) * op[1] + matrix(1, 2) * op[0];
-                               int const z = matrix(2, 0) * op[2] + matrix(2, 1) * op[1] + matrix(2, 2) * op[0];
+                               /* Convert sRGB to XYZ; op is BGRA.  First, input gamma LUT */
+                               double const r = lut_in[op[2]];
+                               double const g = lut_in[op[1]];
+                               double const b = lut_in[op[0]];
 
-                               /* Blend high bytes */
-                               tp[1] = min (x, 255) * alpha + tp[1] * (1 - alpha);
-                               tp[3] = min (y, 255) * alpha + tp[3] * (1 - alpha);
-                               tp[5] = min (z, 255) * alpha + tp[5] * (1 - alpha);
+                               /* RGB to XYZ, including Bradford transform and DCI companding */
+                               double const x = max (0.0, min (65535.0, r * fast_matrix[0] + g * fast_matrix[1] + b * fast_matrix[2]));
+                               double const y = max (0.0, min (65535.0, r * fast_matrix[3] + g * fast_matrix[4] + b * fast_matrix[5]));
+                               double const z = max (0.0, min (65535.0, r * fast_matrix[6] + g * fast_matrix[7] + b * fast_matrix[8]));
 
-                               tp += this_bpp;
+                               /* Out gamma LUT and blend */
+                               tp[0] = lrint(lut_out[lrint(x)] * 65535) * alpha + tp[0] * (1 - alpha);
+                               tp[1] = lrint(lut_out[lrint(y)] * 65535) * alpha + tp[1] * (1 - alpha);
+                               tp[2] = lrint(lut_out[lrint(z)] * 65535) * alpha + tp[2] * (1 - alpha);
+
+                               tp += this_bpp / 2;
                                op += other_bpp;
                        }
                }
index d7ccbc6590e3788362da8e620e14fbf364dc916f..01e80c783c49b4e56e87fbb08bb238621119ec0c 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2014-2016 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
  *  @brief Test the burning of subtitles into the DCP.
  */
 
-#include <boost/test/unit_test.hpp>
 #include "lib/text_subtitle_content.h"
 #include "lib/dcp_subtitle_content.h"
 #include "lib/film.h"
 #include "lib/ratio.h"
 #include "lib/dcp_content_type.h"
+#include "lib/subtitle_content.h"
+#include "lib/content_factory.h"
 #include "test.h"
+#include <dcp/dcp.h>
+#include <dcp/cpl.h>
+#include <dcp/reel.h>
+#include <dcp/j2k.h>
+#include <dcp/mono_picture_asset.h>
+#include <dcp/mono_picture_asset_reader.h>
+#include <dcp/mono_picture_frame.h>
+#include <dcp/openjpeg_image.h>
+#include <dcp/reel_picture_asset.h>
+#include <dcp/reel_mono_picture_asset.h>
+#include <boost/test/unit_test.hpp>
 #include <iostream>
 
 using std::cout;
+using std::map;
 using boost::shared_ptr;
+using boost::dynamic_pointer_cast;
 
 /** Build a small DCP with no picture and a single subtitle overlaid onto it from a SubRip file */
 BOOST_AUTO_TEST_CASE (burnt_subtitle_test_subrip)
@@ -69,3 +83,48 @@ BOOST_AUTO_TEST_CASE (burnt_subtitle_test_dcp)
 
        check_dcp ("test/data/burnt_subtitle_test_dcp", film->dir (film->dcp_name ()));
 }
+
+/** Burn some subtitles into an existing DCP to check the colour conversion */
+BOOST_AUTO_TEST_CASE (burnt_subtitle_test_onto_dcp)
+{
+       shared_ptr<Film> film = new_test_film ("burnt_subtitle_test_onto_dcp");
+       film->set_container (Ratio::from_id ("185"));
+       film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
+       film->set_name ("frobozz");
+       film->examine_and_add_content (content_factory (film, "test/data/flat_white.png"));
+       wait_for_jobs ();
+       film->make_dcp ();
+       wait_for_jobs ();
+
+       shared_ptr<Film> film2 = new_test_film ("burnt_subtitle_test_onto_dcp2");
+       film2->set_container (Ratio::from_id ("185"));
+       film2->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
+       film2->set_name ("frobozz");
+       film2->examine_and_add_content (content_factory (film2, film->dir (film->dcp_name ())));
+       shared_ptr<TextSubtitleContent> sub = dynamic_pointer_cast<TextSubtitleContent> (
+               content_factory (film2, "test/data/subrip2.srt")
+               );
+       sub->subtitle->set_burn (true);
+       sub->subtitle->set_outline (true);
+       film2->examine_and_add_content (sub);
+       wait_for_jobs ();
+       film2->make_dcp ();
+       wait_for_jobs ();
+
+       dcp::DCP dcp (film2->dir (film2->dcp_name ()));
+       dcp.read ();
+       BOOST_REQUIRE_EQUAL (dcp.cpls().size(), 1);
+       BOOST_REQUIRE_EQUAL (dcp.cpls().front()->reels().size(), 1);
+       BOOST_REQUIRE (dcp.cpls().front()->reels().front()->main_picture());
+       BOOST_REQUIRE (dcp.cpls().front()->reels().front()->main_picture()->asset());
+       shared_ptr<const dcp::MonoPictureAsset> pic = dynamic_pointer_cast<dcp::ReelMonoPictureAsset> (
+               dcp.cpls().front()->reels().front()->main_picture()
+               )->mono_asset();
+       BOOST_REQUIRE (pic);
+       shared_ptr<const dcp::MonoPictureFrame> frame = pic->start_read()->get_frame (12);
+       shared_ptr<dcp::OpenJPEGImage> xyz = frame->xyz_image ();
+       BOOST_CHECK_EQUAL (xyz->size().width, 1998);
+       BOOST_CHECK_EQUAL (xyz->size().height, 1080);
+
+       /* XXX: check the output ... */
+}
index a6a8263ed232d8a9a2dc478b073f835d256d05b6..6966026bc74f121a681b24b04d69c97a196b1355 160000 (submodule)
--- a/test/data
+++ b/test/data
@@ -1 +1 @@
-Subproject commit a6a8263ed232d8a9a2dc478b073f835d256d05b6
+Subproject commit 6966026bc74f121a681b24b04d69c97a196b1355