Try larger menu bar icon on OS X.
[dcpomatic.git] / src / lib / image.cc
index d1f1fb9d14702873db816e8ef0ef45b8bd8b710e..b82cc607249965cfbd8753c48e1421440614588b 100644 (file)
@@ -51,7 +51,7 @@ using boost::shared_ptr;
 using dcp::Size;
 
 int
-Image::line_factor (int n) const
+Image::vertical_factor (int n) const
 {
        if (n == 0) {
                return 1;
@@ -65,11 +65,8 @@ Image::line_factor (int n) const
        return pow (2.0f, d->log2_chroma_h);
 }
 
-/** @param n Component index.
- *  @return Number of samples (i.e. pixels, unless sub-sampled) in each direction for this component.
- */
-dcp::Size
-Image::sample_size (int n) const
+int
+Image::horizontal_factor (int n) const
 {
        int horizontal_factor = 1;
        if (n > 0) {
@@ -79,10 +76,18 @@ Image::sample_size (int n) const
                }
                horizontal_factor = pow (2.0f, d->log2_chroma_w);
        }
+       return horizontal_factor;
+}
 
+/** @param n Component index.
+ *  @return Number of samples (i.e. pixels, unless sub-sampled) in each direction for this component.
+ */
+dcp::Size
+Image::sample_size (int n) const
+{
        return dcp::Size (
-               lrint (ceil (static_cast<double>(size().width) / horizontal_factor)),
-               lrint (ceil (static_cast<double>(size().height) / line_factor (n)))
+               lrint (ceil (static_cast<double>(size().width) / horizontal_factor (n))),
+               lrint (ceil (static_cast<double>(size().height) / vertical_factor (n)))
                );
 }
 
@@ -191,7 +196,7 @@ Image::crop_scale_window (
                   we've cropped all of its Y-channel pixels.
                */
                int const x = lrintf (bytes_per_pixel(c) * crop.left) & ~ ((int) desc->log2_chroma_w);
-               scale_in_data[c] = data()[c] + x + stride()[c] * (crop.top / line_factor(c));
+               scale_in_data[c] = data()[c] + x + stride()[c] * (crop.top / vertical_factor(c));
        }
 
        /* Corner of the image within out_size */
@@ -424,6 +429,36 @@ Image::make_transparent ()
        memset (data()[0], 0, sample_size(0).height * stride()[0]);
 }
 
+template <class T>
+void
+component (
+       int n,
+       Image* base,
+       shared_ptr<const Image> other,
+       shared_ptr<const Image> rgba,
+       int start_base_x, int start_base_y,
+       int start_other_x, int start_other_y
+       )
+{
+       dcp::Size const base_size = base->sample_size(n);
+       dcp::Size const other_size = other->sample_size(n);
+       for (int by = start_base_y, oy = start_other_y; by < base_size.height && oy < other_size.height; ++by, ++oy) {
+               /* base image */
+               T* bp = ((T*) (base->data()[n] + by * base->stride()[n])) + start_base_x;
+               /* overlay image */
+               T* op = ((T*) (other->data()[n] + oy * other->stride()[n]));
+               /* original RGBA for alpha channel */
+               uint8_t* rp = rgba->data()[0] + oy * rgba->stride()[0];
+               for (int bx = start_base_x, ox = start_other_x; bx < base_size.width && ox < other_size.width; ++bx, ++ox) {
+                       float const alpha = float (rp[3]) / 255;
+                       *bp = *op * alpha + *bp * (1 - alpha);
+                       ++bp;
+                       ++op;
+                       rp += 4;
+               }
+       }
+}
+
 void
 Image::alpha_blend (shared_ptr<const Image> other, Position<int> position)
 {
@@ -541,6 +576,22 @@ Image::alpha_blend (shared_ptr<const Image> other, Position<int> position)
                }
                break;
        }
+       case AV_PIX_FMT_YUV420P:
+       {
+               shared_ptr<Image> yuv = other->scale (other->size(), dcp::YUV_TO_RGB_REC709, _pixel_format, false, false);
+               component<uint8_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;
+       }
+       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);
        }