From: Carl Hetherington Date: Mon, 22 Aug 2016 13:26:28 +0000 (+0100) Subject: Do full gamma correction etc. when alpha-blending subtitles X-Git-Tag: v2.9.15~4 X-Git-Url: https://git.carlh.net/gitweb/?a=commitdiff_plain;ds=sidebyside;h=ba7f8c6363e881fdc0044fbbbb36535996d6dbf7;p=dcpomatic.git Do full gamma correction etc. when alpha-blending subtitles onto XYZ images. This fixes colour tints on subtitles burnt into existing DCPs. --- diff --git a/ChangeLog b/ChangeLog index 754e1ccfa..ca0523397 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2016-08-22 c.hetherington + + * Fix colour tint in subtitles burnt into existing DCP + content. + 2016-08-22 Carl Hetherington * Version 2.9.14 released. diff --git a/src/lib/image.cc b/src/lib/image.cc index 1735bb38a..13ef0db19 100644 --- a/src/lib/image.cc +++ b/src/lib/image.cc @@ -28,6 +28,8 @@ #include "rect.h" #include "util.h" #include "dcpomatic_socket.h" +#include +#include extern "C" { #include #include @@ -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 other, Position position) } case AV_PIX_FMT_XYZ12LE: { - boost::numeric::ublas::matrix 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 (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; } } diff --git a/test/burnt_subtitle_test.cc b/test/burnt_subtitle_test.cc index d7ccbc659..01e80c783 100644 --- a/test/burnt_subtitle_test.cc +++ b/test/burnt_subtitle_test.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2014 Carl Hetherington + Copyright (C) 2014-2016 Carl Hetherington This file is part of DCP-o-matic. @@ -22,17 +22,31 @@ * @brief Test the burning of subtitles into the DCP. */ -#include #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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include 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 = 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 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 sub = dynamic_pointer_cast ( + 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 pic = dynamic_pointer_cast ( + dcp.cpls().front()->reels().front()->main_picture() + )->mono_asset(); + BOOST_REQUIRE (pic); + shared_ptr frame = pic->start_read()->get_frame (12); + shared_ptr xyz = frame->xyz_image (); + BOOST_CHECK_EQUAL (xyz->size().width, 1998); + BOOST_CHECK_EQUAL (xyz->size().height, 1080); + + /* XXX: check the output ... */ +} diff --git a/test/data b/test/data index a6a8263ed..6966026bc 160000 --- a/test/data +++ b/test/data @@ -1 +1 @@ -Subproject commit a6a8263ed232d8a9a2dc478b073f835d256d05b6 +Subproject commit 6966026bc74f121a681b24b04d69c97a196b1355