White space.
[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 float
496 Image::bytes_per_pixel (int c) const
497 {
498         AVPixFmtDescriptor const * d = av_pix_fmt_desc_get(_pixel_format);
499         if (!d) {
500                 throw PixelFormatError ("bytes_per_pixel()", _pixel_format);
501         }
502
503         if (c >= components()) {
504                 return 0;
505         }
506
507         float bpp[4] = { 0, 0, 0, 0 };
508
509         bpp[0] = floor ((d->comp[0].depth_minus1 + 1 + 7) / 8);
510         if (d->nb_components > 1) {
511                 bpp[1] = floor ((d->comp[1].depth_minus1 + 1 + 7) / 8) / pow (2.0f, d->log2_chroma_w);
512         }
513         if (d->nb_components > 2) {
514                 bpp[2] = floor ((d->comp[2].depth_minus1 + 1 + 7) / 8) / pow (2.0f, d->log2_chroma_w);
515         }
516         if (d->nb_components > 3) {
517                 bpp[3] = floor ((d->comp[3].depth_minus1 + 1 + 7) / 8) / pow (2.0f, d->log2_chroma_w);
518         }
519
520         if ((d->flags & PIX_FMT_PLANAR) == 0) {
521                 /* Not planar; sum them up */
522                 return bpp[0] + bpp[1] + bpp[2] + bpp[3];
523         }
524
525         return bpp[c];
526 }
527
528 /** Construct a Image of a given size and format, allocating memory
529  *  as required.
530  *
531  *  @param p Pixel format.
532  *  @param s Size in pixels.
533  */
534 Image::Image (AVPixelFormat p, dcp::Size s, bool aligned)
535         : _size (s)
536         , _pixel_format (p)
537         , _aligned (aligned)
538 {
539         allocate ();
540 }
541
542 void
543 Image::allocate ()
544 {
545         _data = (uint8_t **) wrapped_av_malloc (4 * sizeof (uint8_t *));
546         _data[0] = _data[1] = _data[2] = _data[3] = 0;
547
548         _line_size = (int *) wrapped_av_malloc (4 * sizeof (int));
549         _line_size[0] = _line_size[1] = _line_size[2] = _line_size[3] = 0;
550
551         _stride = (int *) wrapped_av_malloc (4 * sizeof (int));
552         _stride[0] = _stride[1] = _stride[2] = _stride[3] = 0;
553
554         for (int i = 0; i < components(); ++i) {
555                 _line_size[i] = ceil (_size.width * bytes_per_pixel(i));
556                 _stride[i] = stride_round_up (i, _line_size, _aligned ? 32 : 1);
557
558                 /* The assembler function ff_rgb24ToY_avx (in libswscale/x86/input.asm)
559                    uses a 16-byte fetch to read three bytes (R/G/B) of image data.
560                    Hence on the last pixel of the last line it reads over the end of
561                    the actual data by 1 byte.  If the width of an image is a multiple
562                    of the stride alignment there will be no padding at the end of image lines.
563                    OS X crashes on this illegal read, though other operating systems don't
564                    seem to mind.  The nasty + 1 in this malloc makes sure there is always a byte
565                    for that instruction to read safely.
566
567                    Further to the above, valgrind is now telling me that ff_rgb24ToY_ssse3
568                    over-reads by more then _avx.  I can't follow the code to work out how much,
569                    so I'll just over-allocate by 32 bytes and have done with it.  Empirical
570                    testing suggests that it works.
571                 */
572                 _data[i] = (uint8_t *) wrapped_av_malloc (_stride[i] * lines (i) + 32);
573         }
574 }
575
576 Image::Image (Image const & other)
577         : _size (other._size)
578         , _pixel_format (other._pixel_format)
579         , _aligned (other._aligned)
580 {
581         allocate ();
582
583         for (int i = 0; i < components(); ++i) {
584                 uint8_t* p = _data[i];
585                 uint8_t* q = other._data[i];
586                 for (int j = 0; j < lines(i); ++j) {
587                         memcpy (p, q, _line_size[i]);
588                         p += stride()[i];
589                         q += other.stride()[i];
590                 }
591         }
592 }
593
594 Image::Image (AVFrame* frame)
595         : _size (frame->width, frame->height)
596         , _pixel_format (static_cast<AVPixelFormat> (frame->format))
597         , _aligned (true)
598 {
599         allocate ();
600
601         for (int i = 0; i < components(); ++i) {
602                 uint8_t* p = _data[i];
603                 uint8_t* q = frame->data[i];
604                 for (int j = 0; j < lines(i); ++j) {
605                         memcpy (p, q, _line_size[i]);
606                         p += stride()[i];
607                         /* AVFrame's linesize is what we call `stride' */
608                         q += frame->linesize[i];
609                 }
610         }
611 }
612
613 Image::Image (shared_ptr<const Image> other, bool aligned)
614         : _size (other->_size)
615         , _pixel_format (other->_pixel_format)
616         , _aligned (aligned)
617 {
618         allocate ();
619
620         for (int i = 0; i < components(); ++i) {
621                 DCPOMATIC_ASSERT (line_size()[i] == other->line_size()[i]);
622                 uint8_t* p = _data[i];
623                 uint8_t* q = other->data()[i];
624                 for (int j = 0; j < lines(i); ++j) {
625                         memcpy (p, q, line_size()[i]);
626                         p += stride()[i];
627                         q += other->stride()[i];
628                 }
629         }
630 }
631
632 Image&
633 Image::operator= (Image const & other)
634 {
635         if (this == &other) {
636                 return *this;
637         }
638
639         Image tmp (other);
640         swap (tmp);
641         return *this;
642 }
643
644 void
645 Image::swap (Image & other)
646 {
647         std::swap (_size, other._size);
648         std::swap (_pixel_format, other._pixel_format);
649
650         for (int i = 0; i < 4; ++i) {
651                 std::swap (_data[i], other._data[i]);
652                 std::swap (_line_size[i], other._line_size[i]);
653                 std::swap (_stride[i], other._stride[i]);
654         }
655
656         std::swap (_aligned, other._aligned);
657 }
658
659 /** Destroy a Image */
660 Image::~Image ()
661 {
662         for (int i = 0; i < components(); ++i) {
663                 av_free (_data[i]);
664         }
665
666         av_free (_data);
667         av_free (_line_size);
668         av_free (_stride);
669 }
670
671 uint8_t * const *
672 Image::data () const
673 {
674         return _data;
675 }
676
677 int *
678 Image::line_size () const
679 {
680         return _line_size;
681 }
682
683 int const *
684 Image::stride () const
685 {
686         return _stride;
687 }
688
689 dcp::Size
690 Image::size () const
691 {
692         return _size;
693 }
694
695 bool
696 Image::aligned () const
697 {
698         return _aligned;
699 }
700
701 PositionImage
702 merge (list<PositionImage> images)
703 {
704         if (images.empty ()) {
705                 return PositionImage ();
706         }
707
708         if (images.size() == 1) {
709                 return images.front ();
710         }
711
712         dcpomatic::Rect<int> all (images.front().position, images.front().image->size().width, images.front().image->size().height);
713         for (list<PositionImage>::const_iterator i = images.begin(); i != images.end(); ++i) {
714                 all.extend (dcpomatic::Rect<int> (i->position, i->image->size().width, i->image->size().height));
715         }
716
717         shared_ptr<Image> merged (new Image (images.front().image->pixel_format (), dcp::Size (all.width, all.height), true));
718         merged->make_transparent ();
719         for (list<PositionImage>::const_iterator i = images.begin(); i != images.end(); ++i) {
720                 merged->alpha_blend (i->image, i->position - all.position());
721         }
722
723         return PositionImage (merged, all.position ());
724 }
725
726 bool
727 operator== (Image const & a, Image const & b)
728 {
729         if (a.components() != b.components() || a.pixel_format() != b.pixel_format() || a.aligned() != b.aligned()) {
730                 return false;
731         }
732
733         for (int c = 0; c < a.components(); ++c) {
734                 if (a.lines(c) != b.lines(c) || a.line_size()[c] != b.line_size()[c] || a.stride()[c] != b.stride()[c]) {
735                         return false;
736                 }
737
738                 uint8_t* p = a.data()[c];
739                 uint8_t* q = b.data()[c];
740                 for (int y = 0; y < a.lines(c); ++y) {
741                         if (memcmp (p, q, a.line_size()[c]) != 0) {
742                                 return false;
743                         }
744
745                         p += a.stride()[c];
746                         q += b.stride()[c];
747                 }
748         }
749
750         return true;
751 }
752
753 /** Fade the image.
754  *  @param f Amount to fade by; 0 is black, 1 is no fade.
755  */
756 void
757 Image::fade (float f)
758 {
759         switch (_pixel_format) {
760         case PIX_FMT_YUV420P:
761         case PIX_FMT_YUV422P:
762         case PIX_FMT_YUV444P:
763         case PIX_FMT_YUV411P:
764         case PIX_FMT_YUVJ420P:
765         case PIX_FMT_YUVJ422P:
766         case PIX_FMT_YUVJ444P:
767         case PIX_FMT_RGB24:
768         case PIX_FMT_ARGB:
769         case PIX_FMT_RGBA:
770         case PIX_FMT_ABGR:
771         case PIX_FMT_BGRA:
772         case PIX_FMT_RGB555LE:
773                 /* 8-bit */
774                 for (int c = 0; c < 3; ++c) {
775                         uint8_t* p = data()[c];
776                         for (int y = 0; y < lines(c); ++y) {
777                                 uint8_t* q = p;
778                                 for (int x = 0; x < line_size()[c]; ++x) {
779                                         *q = int (float (*q) * f);
780                                         ++q;
781                                 }
782                                 p += stride()[c];
783                         }
784                 }
785                 break;
786
787         case PIX_FMT_YUV422P9LE:
788         case PIX_FMT_YUV444P9LE:
789         case PIX_FMT_YUV422P10LE:
790         case PIX_FMT_YUV444P10LE:
791         case PIX_FMT_YUV422P16LE:
792         case PIX_FMT_YUV444P16LE:
793         case AV_PIX_FMT_YUVA420P9LE:
794         case AV_PIX_FMT_YUVA422P9LE:
795         case AV_PIX_FMT_YUVA444P9LE:
796         case AV_PIX_FMT_YUVA420P10LE:
797         case AV_PIX_FMT_YUVA422P10LE:
798         case AV_PIX_FMT_YUVA444P10LE:
799         case AV_PIX_FMT_RGB48LE:
800                 /* 16-bit little-endian */
801                 for (int c = 0; c < 3; ++c) {
802                         int const stride_pixels = stride()[c] / 2;
803                         int const line_size_pixels = line_size()[c] / 2;
804                         uint16_t* p = reinterpret_cast<uint16_t*> (data()[c]);
805                         for (int y = 0; y < lines(c); ++y) {
806                                 uint16_t* q = p;
807                                 for (int x = 0; x < line_size_pixels; ++x) {
808                                         *q = int (float (*q) * f);
809                                         ++q;
810                                 }
811                                 p += stride_pixels;
812                         }
813                 }
814                 break;
815
816         case PIX_FMT_YUV422P9BE:
817         case PIX_FMT_YUV444P9BE:
818         case PIX_FMT_YUV444P10BE:
819         case PIX_FMT_YUV422P10BE:
820         case AV_PIX_FMT_YUVA420P9BE:
821         case AV_PIX_FMT_YUVA422P9BE:
822         case AV_PIX_FMT_YUVA444P9BE:
823         case AV_PIX_FMT_YUVA420P10BE:
824         case AV_PIX_FMT_YUVA422P10BE:
825         case AV_PIX_FMT_YUVA444P10BE:
826         case AV_PIX_FMT_YUVA420P16BE:
827         case AV_PIX_FMT_YUVA422P16BE:
828         case AV_PIX_FMT_YUVA444P16BE:
829         case AV_PIX_FMT_RGB48BE:
830                 /* 16-bit big-endian */
831                 for (int c = 0; c < 3; ++c) {
832                         int const stride_pixels = stride()[c] / 2;
833                         int const line_size_pixels = line_size()[c] / 2;
834                         uint16_t* p = reinterpret_cast<uint16_t*> (data()[c]);
835                         for (int y = 0; y < lines(c); ++y) {
836                                 uint16_t* q = p;
837                                 for (int x = 0; x < line_size_pixels; ++x) {
838                                         *q = swap_16 (int (float (swap_16 (*q)) * f));
839                                         ++q;
840                                 }
841                                 p += stride_pixels;
842                         }
843                 }
844                 break;
845
846         case PIX_FMT_UYVY422:
847         {
848                 int const Y = lines(0);
849                 int const X = line_size()[0];
850                 uint8_t* p = data()[0];
851                 for (int y = 0; y < Y; ++y) {
852                         for (int x = 0; x < X; ++x) {
853                                 *p = int (float (*p) * f);
854                                 ++p;
855                         }
856                 }
857                 break;
858         }
859
860         default:
861                 throw PixelFormatError ("fade()", _pixel_format);
862         }
863 }