+ break;
+ }
+ case AV_PIX_FMT_BGRA:
+ case AV_PIX_FMT_RGBA:
+ {
+ int const this_bpp = 4;
+ 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;
+ 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;
+ tp[0] = op[0] * alpha + tp[0] * (1 - alpha);
+ tp[1] = op[1] * alpha + tp[1] * (1 - alpha);
+ tp[2] = op[2] * alpha + tp[2] * (1 - alpha);
+ tp[3] = op[3] * alpha + tp[3] * (1 - alpha);
+
+ tp += this_bpp;
+ op += other_bpp;
+ }
+ }
+ break;
+ }
+ case AV_PIX_FMT_RGB48LE:
+ {
+ 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;
+ 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;
+ /* Blend high bytes; the RGBA in op appears to be BGRA */
+ tp[1] = op[2] * alpha + tp[1] * (1 - alpha);
+ tp[3] = op[1] * alpha + tp[3] * (1 - alpha);
+ tp[5] = op[0] * alpha + tp[5] * (1 - alpha);
+
+ tp += this_bpp;
+ op += other_bpp;
+ }
+ }
+ break;
+ }
+ case AV_PIX_FMT_XYZ12LE:
+ {
+ 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) {
+ 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. 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]];
+
+ /* 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]));
+
+ /* 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;
+ }
+ }
+ break;
+ }
+ case AV_PIX_FMT_YUV420P:
+ {
+ shared_ptr<Image> yuv = other->scale (other->size(), dcp::YUV_TO_RGB_REC709, _pixel_format, false, false);
+
+ for (int i = 0; i < 3; ++i) {
+ dcp::Size const tsize = sample_size(i);
+ dcp::Size const osize = yuv->sample_size(i);
+ int const tbpp = ceil (bytes_per_pixel(i) / horizontal_factor(i));
+ int const obpp = ceil (yuv->bytes_per_pixel(i) / yuv->horizontal_factor(i));
+ int const abpp = other->bytes_per_pixel(0);
+ start_tx /= horizontal_factor (i);
+ start_ty /= vertical_factor (i);
+ start_ox /= yuv->horizontal_factor (i);
+ start_oy /= yuv->vertical_factor (i);
+ for (int ty = start_ty, oy = start_oy; ty < tsize.height && oy < osize.height; ++ty, ++oy) {
+ /* this image */
+ uint8_t* tp = data()[i] + ty * stride()[i] + start_tx * tbpp;
+ /* overlay image */
+ uint8_t* op = yuv->data()[i] + oy * yuv->stride()[i];
+ /* original RGBA for alpha channel */
+ uint8_t* ap = other->data()[0] + oy * other->stride()[0];
+ for (int tx = start_tx, ox = start_ox; tx < tsize.width && ox < osize.width; ++tx, ++ox) {
+ float const alpha = float (ap[3]) / 255;
+ *tp = *op * alpha + *tp * (1 - alpha);
+ tp += tbpp;
+ op += obpp;
+ ap += abpp;
+ }
+ }
+ }
+ break;
+ }
+ case AV_PIX_FMT_YUV420P10:
+ {
+ shared_ptr<Image> yuv = other->scale (other->size(), dcp::YUV_TO_RGB_REC709, _pixel_format, false, false);
+ component<uint16_t> (0, this, yuv, other, start_tx, start_ty, start_ox, start_oy);
+ component<uint8_t> (1, this, yuv, other, start_tx, start_ty, start_ox, start_oy);
+ component<uint8_t> (2, this, yuv, other, start_tx, start_ty, start_ox, start_oy);
+ break;
+ }
+ default:
+ throw PixelFormatError ("alpha_blend()", _pixel_format);