Remove unused Image::crop.
[dcpomatic.git] / src / lib / image.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file src/image.cc
21  *  @brief A class to describe a video image.
22  */
23
24 #include "image.h"
25 #include "exceptions.h"
26 #include "timer.h"
27 #include "rect.h"
28 #include "util.h"
29 #include "md5_digester.h"
30 #include "dcpomatic_socket.h"
31 extern "C" {
32 #include <libswscale/swscale.h>
33 #include <libavutil/pixfmt.h>
34 #include <libavutil/pixdesc.h>
35 }
36 #include <iostream>
37
38 #include "i18n.h"
39
40 using std::string;
41 using std::min;
42 using std::cout;
43 using std::cerr;
44 using std::list;
45 using boost::shared_ptr;
46 using dcp::Size;
47
48 int
49 Image::line_factor (int n) const
50 {
51         if (n == 0) {
52                 return 1;
53         }
54
55         AVPixFmtDescriptor const * d = av_pix_fmt_desc_get(_pixel_format);
56         if (!d) {
57                 throw PixelFormatError ("line_factor()", _pixel_format);
58         }
59
60         return pow (2.0f, d->log2_chroma_h);
61 }
62
63 /** @param n Component index.
64  *  @return Number of lines in the image for the given component.
65  */
66 int
67 Image::lines (int n) const
68 {
69         return rint (ceil (static_cast<double>(size().height) / line_factor (n)));
70 }
71
72 /** @return Number of components */
73 int
74 Image::components () const
75 {
76         AVPixFmtDescriptor const * d = av_pix_fmt_desc_get(_pixel_format);
77         if (!d) {
78                 throw PixelFormatError ("components()", _pixel_format);
79         }
80
81         if ((d->flags & PIX_FMT_PLANAR) == 0) {
82                 return 1;
83         }
84
85         return d->nb_components;
86 }
87
88 /** Crop this image, scale it to `inter_size' and then place it in a black frame of `out_size' */
89 shared_ptr<Image>
90 Image::crop_scale_window (
91         Crop crop, dcp::Size inter_size, dcp::Size out_size, dcp::YUVToRGB yuv_to_rgb, AVPixelFormat out_format, bool out_aligned
92         ) const
93 {
94         /* Empirical testing suggests that sws_scale() will crash if
95            the input image is not aligned.
96         */
97         DCPOMATIC_ASSERT (aligned ());
98
99         DCPOMATIC_ASSERT (out_size.width >= inter_size.width);
100         DCPOMATIC_ASSERT (out_size.height >= inter_size.height);
101
102         /* Here's an image of out_size */
103         shared_ptr<Image> out (new Image (out_format, out_size, out_aligned));
104         out->make_black ();
105
106         /* Size of the image after any crop */
107         dcp::Size const cropped_size = crop.apply (size ());
108
109         /* Scale context for a scale from cropped_size to inter_size */
110         struct SwsContext* scale_context = sws_getContext (
111                         cropped_size.width, cropped_size.height, pixel_format(),
112                         inter_size.width, inter_size.height, out_format,
113                         SWS_BICUBIC, 0, 0, 0
114                 );
115
116         if (!scale_context) {
117                 throw StringError (N_("Could not allocate SwsContext"));
118         }
119
120         DCPOMATIC_ASSERT (yuv_to_rgb < dcp::YUV_TO_RGB_COUNT);
121         int const lut[dcp::YUV_TO_RGB_COUNT] = {
122                 SWS_CS_ITU601,
123                 SWS_CS_ITU709
124         };
125
126         sws_setColorspaceDetails (
127                 scale_context,
128                 sws_getCoefficients (lut[yuv_to_rgb]), 0,
129                 sws_getCoefficients (lut[yuv_to_rgb]), 0,
130                 0, 1 << 16, 1 << 16
131                 );
132
133         AVPixFmtDescriptor const * desc = av_pix_fmt_desc_get (_pixel_format);
134         if (!desc) {
135                 throw PixelFormatError ("crop_scale_window()", _pixel_format);
136         }
137
138         /* Prepare input data pointers with crop */
139         uint8_t* scale_in_data[components()];
140         for (int c = 0; c < components(); ++c) {
141                 /* To work out the crop in bytes, start by multiplying
142                    the crop by the (average) bytes per pixel.  Then
143                    round down so that we don't crop a subsampled pixel until
144                    we've cropped all of its Y-channel pixels.
145                 */
146                 int const x = int (rint (bytes_per_pixel(c) * crop.left)) & ~ ((int) desc->log2_chroma_w);
147                 scale_in_data[c] = data()[c] + x + stride()[c] * (crop.top / line_factor(c));
148         }
149
150         /* Corner of the image within out_size */
151         Position<int> const corner ((out_size.width - inter_size.width) / 2, (out_size.height - inter_size.height) / 2);
152
153         uint8_t* scale_out_data[out->components()];
154         for (int c = 0; c < out->components(); ++c) {
155                 scale_out_data[c] = out->data()[c] + int (rint (out->bytes_per_pixel(c) * corner.x)) + out->stride()[c] * corner.y;
156         }
157
158         sws_scale (
159                 scale_context,
160                 scale_in_data, stride(),
161                 0, cropped_size.height,
162                 scale_out_data, out->stride()
163                 );
164
165         sws_freeContext (scale_context);
166
167         return out;
168 }
169
170 shared_ptr<Image>
171 Image::scale (dcp::Size out_size, dcp::YUVToRGB yuv_to_rgb, AVPixelFormat out_format, bool out_aligned) const
172 {
173         /* Empirical testing suggests that sws_scale() will crash if
174            the input image is not aligned.
175         */
176         DCPOMATIC_ASSERT (aligned ());
177
178         shared_ptr<Image> scaled (new Image (out_format, out_size, out_aligned));
179
180         struct SwsContext* scale_context = sws_getContext (
181                 size().width, size().height, pixel_format(),
182                 out_size.width, out_size.height, out_format,
183                 SWS_BICUBIC, 0, 0, 0
184                 );
185
186         DCPOMATIC_ASSERT (yuv_to_rgb < dcp::YUV_TO_RGB_COUNT);
187         int const lut[dcp::YUV_TO_RGB_COUNT] = {
188                 SWS_CS_ITU601,
189                 SWS_CS_ITU709
190         };
191
192         sws_setColorspaceDetails (
193                 scale_context,
194                 sws_getCoefficients (lut[yuv_to_rgb]), 0,
195                 sws_getCoefficients (lut[yuv_to_rgb]), 0,
196                 0, 1 << 16, 1 << 16
197                 );
198
199         sws_scale (
200                 scale_context,
201                 data(), stride(),
202                 0, size().height,
203                 scaled->data(), scaled->stride()
204                 );
205
206         sws_freeContext (scale_context);
207
208         return scaled;
209 }
210
211 /** Blacken a YUV image whose bits per pixel is rounded up to 16 */
212 void
213 Image::yuv_16_black (uint16_t v, bool alpha)
214 {
215         memset (data()[0], 0, lines(0) * stride()[0]);
216         for (int i = 1; i < 3; ++i) {
217                 int16_t* p = reinterpret_cast<int16_t*> (data()[i]);
218                 for (int y = 0; y < lines(i); ++y) {
219                         /* We divide by 2 here because we are writing 2 bytes at a time */
220                         for (int x = 0; x < line_size()[i] / 2; ++x) {
221                                 p[x] = v;
222                         }
223                         p += stride()[i] / 2;
224                 }
225         }
226
227         if (alpha) {
228                 memset (data()[3], 0, lines(3) * stride()[3]);
229         }
230 }
231
232 uint16_t
233 Image::swap_16 (uint16_t v)
234 {
235         return ((v >> 8) & 0xff) | ((v & 0xff) << 8);
236 }
237
238 void
239 Image::make_black ()
240 {
241         /* U/V black value for 8-bit colour */
242         static uint8_t const eight_bit_uv =     (1 << 7) - 1;
243         /* U/V black value for 9-bit colour */
244         static uint16_t const nine_bit_uv =     (1 << 8) - 1;
245         /* U/V black value for 10-bit colour */
246         static uint16_t const ten_bit_uv =      (1 << 9) - 1;
247         /* U/V black value for 16-bit colour */
248         static uint16_t const sixteen_bit_uv =  (1 << 15) - 1;
249
250         switch (_pixel_format) {
251         case PIX_FMT_YUV420P:
252         case PIX_FMT_YUV422P:
253         case PIX_FMT_YUV444P:
254         case PIX_FMT_YUV411P:
255                 memset (data()[0], 0, lines(0) * stride()[0]);
256                 memset (data()[1], eight_bit_uv, lines(1) * stride()[1]);
257                 memset (data()[2], eight_bit_uv, lines(2) * stride()[2]);
258                 break;
259
260         case PIX_FMT_YUVJ420P:
261         case PIX_FMT_YUVJ422P:
262         case PIX_FMT_YUVJ444P:
263                 memset (data()[0], 0, lines(0) * stride()[0]);
264                 memset (data()[1], eight_bit_uv + 1, lines(1) * stride()[1]);
265                 memset (data()[2], eight_bit_uv + 1, lines(2) * stride()[2]);
266                 break;
267
268         case PIX_FMT_YUV422P9LE:
269         case PIX_FMT_YUV444P9LE:
270                 yuv_16_black (nine_bit_uv, false);
271                 break;
272
273         case PIX_FMT_YUV422P9BE:
274         case PIX_FMT_YUV444P9BE:
275                 yuv_16_black (swap_16 (nine_bit_uv), false);
276                 break;
277
278         case PIX_FMT_YUV422P10LE:
279         case PIX_FMT_YUV444P10LE:
280                 yuv_16_black (ten_bit_uv, false);
281                 break;
282
283         case PIX_FMT_YUV422P16LE:
284         case PIX_FMT_YUV444P16LE:
285                 yuv_16_black (sixteen_bit_uv, false);
286                 break;
287
288         case PIX_FMT_YUV444P10BE:
289         case PIX_FMT_YUV422P10BE:
290                 yuv_16_black (swap_16 (ten_bit_uv), false);
291                 break;
292
293         case AV_PIX_FMT_YUVA420P9BE:
294         case AV_PIX_FMT_YUVA422P9BE:
295         case AV_PIX_FMT_YUVA444P9BE:
296                 yuv_16_black (swap_16 (nine_bit_uv), true);
297                 break;
298
299         case AV_PIX_FMT_YUVA420P9LE:
300         case AV_PIX_FMT_YUVA422P9LE:
301         case AV_PIX_FMT_YUVA444P9LE:
302                 yuv_16_black (nine_bit_uv, true);
303                 break;
304
305         case AV_PIX_FMT_YUVA420P10BE:
306         case AV_PIX_FMT_YUVA422P10BE:
307         case AV_PIX_FMT_YUVA444P10BE:
308                 yuv_16_black (swap_16 (ten_bit_uv), true);
309                 break;
310
311         case AV_PIX_FMT_YUVA420P10LE:
312         case AV_PIX_FMT_YUVA422P10LE:
313         case AV_PIX_FMT_YUVA444P10LE:
314                 yuv_16_black (ten_bit_uv, true);
315                 break;
316
317         case AV_PIX_FMT_YUVA420P16BE:
318         case AV_PIX_FMT_YUVA422P16BE:
319         case AV_PIX_FMT_YUVA444P16BE:
320                 yuv_16_black (swap_16 (sixteen_bit_uv), true);
321                 break;
322
323         case AV_PIX_FMT_YUVA420P16LE:
324         case AV_PIX_FMT_YUVA422P16LE:
325         case AV_PIX_FMT_YUVA444P16LE:
326                 yuv_16_black (sixteen_bit_uv, true);
327                 break;
328
329         case PIX_FMT_RGB24:
330         case PIX_FMT_ARGB:
331         case PIX_FMT_RGBA:
332         case PIX_FMT_ABGR:
333         case PIX_FMT_BGRA:
334         case PIX_FMT_RGB555LE:
335         case PIX_FMT_RGB48LE:
336         case PIX_FMT_RGB48BE:
337                 memset (data()[0], 0, lines(0) * stride()[0]);
338                 break;
339
340         case PIX_FMT_UYVY422:
341         {
342                 int const Y = lines(0);
343                 int const X = line_size()[0];
344                 uint8_t* p = data()[0];
345                 for (int y = 0; y < Y; ++y) {
346                         for (int x = 0; x < X / 4; ++x) {
347                                 *p++ = eight_bit_uv; // Cb
348                                 *p++ = 0;            // Y0
349                                 *p++ = eight_bit_uv; // Cr
350                                 *p++ = 0;            // Y1
351                         }
352                 }
353                 break;
354         }
355
356         default:
357                 throw PixelFormatError ("make_black()", _pixel_format);
358         }
359 }
360
361 void
362 Image::make_transparent ()
363 {
364         if (_pixel_format != PIX_FMT_RGBA) {
365                 throw PixelFormatError ("make_transparent()", _pixel_format);
366         }
367
368         memset (data()[0], 0, lines(0) * stride()[0]);
369 }
370
371 void
372 Image::alpha_blend (shared_ptr<const Image> other, Position<int> position)
373 {
374         DCPOMATIC_ASSERT (other->pixel_format() == PIX_FMT_RGBA);
375         int const other_bpp = 4;
376
377         int start_tx = position.x;
378         int start_ox = 0;
379
380         if (start_tx < 0) {
381                 start_ox = -start_tx;
382                 start_tx = 0;
383         }
384
385         int start_ty = position.y;
386         int start_oy = 0;
387
388         if (start_ty < 0) {
389                 start_oy = -start_ty;
390                 start_ty = 0;
391         }
392
393         switch (_pixel_format) {
394         case PIX_FMT_RGB24:
395         {
396                 int const this_bpp = 3;
397                 for (int ty = start_ty, oy = start_oy; ty < size().height && oy < other->size().height; ++ty, ++oy) {
398                         uint8_t* tp = data()[0] + ty * stride()[0] + start_tx * this_bpp;
399                         uint8_t* op = other->data()[0] + oy * other->stride()[0];
400                         for (int tx = start_tx, ox = start_ox; tx < size().width && ox < other->size().width; ++tx, ++ox) {
401                                 float const alpha = float (op[3]) / 255;
402                                 tp[0] = op[0] * alpha + tp[0] * (1 - alpha);
403                                 tp[1] = op[1] * alpha + tp[1] * (1 - alpha);
404                                 tp[2] = op[2] * alpha + tp[2] * (1 - alpha);
405
406                                 tp += this_bpp;
407                                 op += other_bpp;
408                         }
409                 }
410                 break;
411         }
412         case PIX_FMT_BGRA:
413         case PIX_FMT_RGBA:
414         {
415                 int const this_bpp = 4;
416                 for (int ty = start_ty, oy = start_oy; ty < size().height && oy < other->size().height; ++ty, ++oy) {
417                         uint8_t* tp = data()[0] + ty * stride()[0] + start_tx * this_bpp;
418                         uint8_t* op = other->data()[0] + oy * other->stride()[0];
419                         for (int tx = start_tx, ox = start_ox; tx < size().width && ox < other->size().width; ++tx, ++ox) {
420                                 float const alpha = float (op[3]) / 255;
421                                 tp[0] = op[0] * alpha + tp[0] * (1 - alpha);
422                                 tp[1] = op[1] * alpha + tp[1] * (1 - alpha);
423                                 tp[2] = op[2] * alpha + tp[2] * (1 - alpha);
424                                 tp[3] = op[3] * alpha + tp[3] * (1 - alpha);
425
426                                 tp += this_bpp;
427                                 op += other_bpp;
428                         }
429                 }
430                 break;
431         }
432         case PIX_FMT_RGB48LE:
433         {
434                 int const this_bpp = 6;
435                 for (int ty = start_ty, oy = start_oy; ty < size().height && oy < other->size().height; ++ty, ++oy) {
436                         uint8_t* tp = data()[0] + ty * stride()[0] + start_tx * this_bpp;
437                         uint8_t* op = other->data()[0] + oy * other->stride()[0];
438                         for (int tx = start_tx, ox = start_ox; tx < size().width && ox < other->size().width; ++tx, ++ox) {
439                                 float const alpha = float (op[3]) / 255;
440                                 /* Blend high bytes */
441                                 tp[1] = op[0] * alpha + tp[1] * (1 - alpha);
442                                 tp[3] = op[1] * alpha + tp[3] * (1 - alpha);
443                                 tp[5] = op[2] * alpha + tp[5] * (1 - alpha);
444
445                                 tp += this_bpp;
446                                 op += other_bpp;
447                         }
448                 }
449                 break;
450         }
451         default:
452                 DCPOMATIC_ASSERT (false);
453         }
454 }
455
456 void
457 Image::copy (shared_ptr<const Image> other, Position<int> position)
458 {
459         /* Only implemented for RGB24 onto RGB24 so far */
460         DCPOMATIC_ASSERT (_pixel_format == PIX_FMT_RGB24 && other->pixel_format() == PIX_FMT_RGB24);
461         DCPOMATIC_ASSERT (position.x >= 0 && position.y >= 0);
462
463         int const N = min (position.x + other->size().width, size().width) - position.x;
464         for (int ty = position.y, oy = 0; ty < size().height && oy < other->size().height; ++ty, ++oy) {
465                 uint8_t * const tp = data()[0] + ty * stride()[0] + position.x * 3;
466                 uint8_t * const op = other->data()[0] + oy * other->stride()[0];
467                 memcpy (tp, op, N * 3);
468         }
469 }
470
471 void
472 Image::read_from_socket (shared_ptr<Socket> socket)
473 {
474         for (int i = 0; i < components(); ++i) {
475                 uint8_t* p = data()[i];
476                 for (int y = 0; y < lines(i); ++y) {
477                         socket->read (p, line_size()[i]);
478                         p += stride()[i];
479                 }
480         }
481 }
482
483 void
484 Image::write_to_socket (shared_ptr<Socket> socket) const
485 {
486         for (int i = 0; i < components(); ++i) {
487                 uint8_t* p = data()[i];
488                 for (int y = 0; y < lines(i); ++y) {
489                         socket->write (p, line_size()[i]);
490                         p += stride()[i];
491                 }
492         }
493 }
494
495
496 float
497 Image::bytes_per_pixel (int c) const
498 {
499         AVPixFmtDescriptor const * d = av_pix_fmt_desc_get(_pixel_format);
500         if (!d) {
501                 throw PixelFormatError ("bytes_per_pixel()", _pixel_format);
502         }
503
504         if (c >= components()) {
505                 return 0;
506         }
507
508         float bpp[4] = { 0, 0, 0, 0 };
509
510         bpp[0] = floor ((d->comp[0].depth_minus1 + 1 + 7) / 8);
511         if (d->nb_components > 1) {
512                 bpp[1] = floor ((d->comp[1].depth_minus1 + 1 + 7) / 8) / pow (2.0f, d->log2_chroma_w);
513         }
514         if (d->nb_components > 2) {
515                 bpp[2] = floor ((d->comp[2].depth_minus1 + 1 + 7) / 8) / pow (2.0f, d->log2_chroma_w);
516         }
517         if (d->nb_components > 3) {
518                 bpp[3] = floor ((d->comp[3].depth_minus1 + 1 + 7) / 8) / pow (2.0f, d->log2_chroma_w);
519         }
520
521         if ((d->flags & PIX_FMT_PLANAR) == 0) {
522                 /* Not planar; sum them up */
523                 return bpp[0] + bpp[1] + bpp[2] + bpp[3];
524         }
525
526         return bpp[c];
527 }
528
529 /** Construct a Image of a given size and format, allocating memory
530  *  as required.
531  *
532  *  @param p Pixel format.
533  *  @param s Size in pixels.
534  */
535 Image::Image (AVPixelFormat p, dcp::Size s, bool aligned)
536         : _size (s)
537         , _pixel_format (p)
538         , _aligned (aligned)
539 {
540         allocate ();
541 }
542
543 void
544 Image::allocate ()
545 {
546         _data = (uint8_t **) wrapped_av_malloc (4 * sizeof (uint8_t *));
547         _data[0] = _data[1] = _data[2] = _data[3] = 0;
548
549         _line_size = (int *) wrapped_av_malloc (4 * sizeof (int));
550         _line_size[0] = _line_size[1] = _line_size[2] = _line_size[3] = 0;
551
552         _stride = (int *) wrapped_av_malloc (4 * sizeof (int));
553         _stride[0] = _stride[1] = _stride[2] = _stride[3] = 0;
554
555         for (int i = 0; i < components(); ++i) {
556                 _line_size[i] = ceil (_size.width * bytes_per_pixel(i));
557                 _stride[i] = stride_round_up (i, _line_size, _aligned ? 32 : 1);
558
559                 /* The assembler function ff_rgb24ToY_avx (in libswscale/x86/input.asm)
560                    uses a 16-byte fetch to read three bytes (R/G/B) of image data.
561                    Hence on the last pixel of the last line it reads over the end of
562                    the actual data by 1 byte.  If the width of an image is a multiple
563                    of the stride alignment there will be no padding at the end of image lines.
564                    OS X crashes on this illegal read, though other operating systems don't
565                    seem to mind.  The nasty + 1 in this malloc makes sure there is always a byte
566                    for that instruction to read safely.
567
568                    Further to the above, valgrind is now telling me that ff_rgb24ToY_ssse3
569                    over-reads by more then _avx.  I can't follow the code to work out how much,
570                    so I'll just over-allocate by 32 bytes and have done with it.  Empirical
571                    testing suggests that it works.
572                 */
573                 _data[i] = (uint8_t *) wrapped_av_malloc (_stride[i] * lines (i) + 32);
574         }
575 }
576
577 Image::Image (Image const & other)
578         : _size (other._size)
579         , _pixel_format (other._pixel_format)
580         , _aligned (other._aligned)
581 {
582         allocate ();
583
584         for (int i = 0; i < components(); ++i) {
585                 uint8_t* p = _data[i];
586                 uint8_t* q = other._data[i];
587                 for (int j = 0; j < lines(i); ++j) {
588                         memcpy (p, q, _line_size[i]);
589                         p += stride()[i];
590                         q += other.stride()[i];
591                 }
592         }
593 }
594
595 Image::Image (AVFrame* frame)
596         : _size (frame->width, frame->height)
597         , _pixel_format (static_cast<AVPixelFormat> (frame->format))
598         , _aligned (true)
599 {
600         allocate ();
601
602         for (int i = 0; i < components(); ++i) {
603                 uint8_t* p = _data[i];
604                 uint8_t* q = frame->data[i];
605                 for (int j = 0; j < lines(i); ++j) {
606                         memcpy (p, q, _line_size[i]);
607                         p += stride()[i];
608                         /* AVFrame's linesize is what we call `stride' */
609                         q += frame->linesize[i];
610                 }
611         }
612 }
613
614 Image::Image (shared_ptr<const Image> other, bool aligned)
615         : _size (other->_size)
616         , _pixel_format (other->_pixel_format)
617         , _aligned (aligned)
618 {
619         allocate ();
620
621         for (int i = 0; i < components(); ++i) {
622                 DCPOMATIC_ASSERT (line_size()[i] == other->line_size()[i]);
623                 uint8_t* p = _data[i];
624                 uint8_t* q = other->data()[i];
625                 for (int j = 0; j < lines(i); ++j) {
626                         memcpy (p, q, line_size()[i]);
627                         p += stride()[i];
628                         q += other->stride()[i];
629                 }
630         }
631 }
632
633 Image&
634 Image::operator= (Image const & other)
635 {
636         if (this == &other) {
637                 return *this;
638         }
639
640         Image tmp (other);
641         swap (tmp);
642         return *this;
643 }
644
645 void
646 Image::swap (Image & other)
647 {
648         std::swap (_size, other._size);
649         std::swap (_pixel_format, other._pixel_format);
650
651         for (int i = 0; i < 4; ++i) {
652                 std::swap (_data[i], other._data[i]);
653                 std::swap (_line_size[i], other._line_size[i]);
654                 std::swap (_stride[i], other._stride[i]);
655         }
656
657         std::swap (_aligned, other._aligned);
658 }
659
660 /** Destroy a Image */
661 Image::~Image ()
662 {
663         for (int i = 0; i < components(); ++i) {
664                 av_free (_data[i]);
665         }
666
667         av_free (_data);
668         av_free (_line_size);
669         av_free (_stride);
670 }
671
672 uint8_t * const *
673 Image::data () const
674 {
675         return _data;
676 }
677
678 int *
679 Image::line_size () const
680 {
681         return _line_size;
682 }
683
684 int const *
685 Image::stride () const
686 {
687         return _stride;
688 }
689
690 dcp::Size
691 Image::size () const
692 {
693         return _size;
694 }
695
696 bool
697 Image::aligned () const
698 {
699         return _aligned;
700 }
701
702 PositionImage
703 merge (list<PositionImage> images)
704 {
705         if (images.empty ()) {
706                 return PositionImage ();
707         }
708
709         if (images.size() == 1) {
710                 return images.front ();
711         }
712
713         dcpomatic::Rect<int> all (images.front().position, images.front().image->size().width, images.front().image->size().height);
714         for (list<PositionImage>::const_iterator i = images.begin(); i != images.end(); ++i) {
715                 all.extend (dcpomatic::Rect<int> (i->position, i->image->size().width, i->image->size().height));
716         }
717
718         shared_ptr<Image> merged (new Image (images.front().image->pixel_format (), dcp::Size (all.width, all.height), true));
719         merged->make_transparent ();
720         for (list<PositionImage>::const_iterator i = images.begin(); i != images.end(); ++i) {
721                 merged->alpha_blend (i->image, i->position - all.position());
722         }
723
724         return PositionImage (merged, all.position ());
725 }
726
727 bool
728 operator== (Image const & a, Image const & b)
729 {
730         if (a.components() != b.components() || a.pixel_format() != b.pixel_format() || a.aligned() != b.aligned()) {
731                 return false;
732         }
733
734         for (int c = 0; c < a.components(); ++c) {
735                 if (a.lines(c) != b.lines(c) || a.line_size()[c] != b.line_size()[c] || a.stride()[c] != b.stride()[c]) {
736                         return false;
737                 }
738
739                 uint8_t* p = a.data()[c];
740                 uint8_t* q = b.data()[c];
741                 for (int y = 0; y < a.lines(c); ++y) {
742                         if (memcmp (p, q, a.line_size()[c]) != 0) {
743                                 return false;
744                         }
745
746                         p += a.stride()[c];
747                         q += b.stride()[c];
748                 }
749         }
750
751         return true;
752 }
753
754 /** Fade the image.
755  *  @param f Amount to fade by; 0 is black, 1 is no fade.
756  */
757 void
758 Image::fade (float f)
759 {
760         switch (_pixel_format) {
761         case PIX_FMT_YUV420P:
762         case PIX_FMT_YUV422P:
763         case PIX_FMT_YUV444P:
764         case PIX_FMT_YUV411P:
765         case PIX_FMT_YUVJ420P:
766         case PIX_FMT_YUVJ422P:
767         case PIX_FMT_YUVJ444P:
768         case PIX_FMT_RGB24:
769         case PIX_FMT_ARGB:
770         case PIX_FMT_RGBA:
771         case PIX_FMT_ABGR:
772         case PIX_FMT_BGRA:
773         case PIX_FMT_RGB555LE:
774                 /* 8-bit */
775                 for (int c = 0; c < 3; ++c) {
776                         uint8_t* p = data()[c];
777                         for (int y = 0; y < lines(c); ++y) {
778                                 uint8_t* q = p;
779                                 for (int x = 0; x < line_size()[c]; ++x) {
780                                         *q = int (float (*q) * f);
781                                         ++q;
782                                 }
783                                 p += stride()[c];
784                         }
785                 }
786                 break;
787
788         case PIX_FMT_YUV422P9LE:
789         case PIX_FMT_YUV444P9LE:
790         case PIX_FMT_YUV422P10LE:
791         case PIX_FMT_YUV444P10LE:
792         case PIX_FMT_YUV422P16LE:
793         case PIX_FMT_YUV444P16LE:
794         case AV_PIX_FMT_YUVA420P9LE:
795         case AV_PIX_FMT_YUVA422P9LE:
796         case AV_PIX_FMT_YUVA444P9LE:
797         case AV_PIX_FMT_YUVA420P10LE:
798         case AV_PIX_FMT_YUVA422P10LE:
799         case AV_PIX_FMT_YUVA444P10LE:
800         case AV_PIX_FMT_RGB48LE:
801                 /* 16-bit little-endian */
802                 for (int c = 0; c < 3; ++c) {
803                         int const stride_pixels = stride()[c] / 2;
804                         int const line_size_pixels = line_size()[c] / 2;
805                         uint16_t* p = reinterpret_cast<uint16_t*> (data()[c]);
806                         for (int y = 0; y < lines(c); ++y) {
807                                 uint16_t* q = p;
808                                 for (int x = 0; x < line_size_pixels; ++x) {
809                                         *q = int (float (*q) * f);
810                                         ++q;
811                                 }
812                                 p += stride_pixels;
813                         }
814                 }
815                 break;
816
817         case PIX_FMT_YUV422P9BE:
818         case PIX_FMT_YUV444P9BE:
819         case PIX_FMT_YUV444P10BE:
820         case PIX_FMT_YUV422P10BE:
821         case AV_PIX_FMT_YUVA420P9BE:
822         case AV_PIX_FMT_YUVA422P9BE:
823         case AV_PIX_FMT_YUVA444P9BE:
824         case AV_PIX_FMT_YUVA420P10BE:
825         case AV_PIX_FMT_YUVA422P10BE:
826         case AV_PIX_FMT_YUVA444P10BE:
827         case AV_PIX_FMT_YUVA420P16BE:
828         case AV_PIX_FMT_YUVA422P16BE:
829         case AV_PIX_FMT_YUVA444P16BE:
830         case AV_PIX_FMT_RGB48BE:
831                 /* 16-bit big-endian */
832                 for (int c = 0; c < 3; ++c) {
833                         int const stride_pixels = stride()[c] / 2;
834                         int const line_size_pixels = line_size()[c] / 2;
835                         uint16_t* p = reinterpret_cast<uint16_t*> (data()[c]);
836                         for (int y = 0; y < lines(c); ++y) {
837                                 uint16_t* q = p;
838                                 for (int x = 0; x < line_size_pixels; ++x) {
839                                         *q = swap_16 (int (float (swap_16 (*q)) * f));
840                                         ++q;
841                                 }
842                                 p += stride_pixels;
843                         }
844                 }
845                 break;
846
847         case PIX_FMT_UYVY422:
848         {
849                 int const Y = lines(0);
850                 int const X = line_size()[0];
851                 uint8_t* p = data()[0];
852                 for (int y = 0; y < Y; ++y) {
853                         for (int x = 0; x < X; ++x) {
854                                 *p = int (float (*p) * f);
855                                 ++p;
856                         }
857                 }
858                 break;
859         }
860
861         default:
862                 throw PixelFormatError ("fade()", _pixel_format);
863         }
864 }