2 Copyright (C) 2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 #include "dcpomatic_assert.h"
23 #include "exceptions.h"
30 using std::shared_ptr;
37 Memory (Memory const&) = delete;
38 Memory& operator= (Memory const&) = delete;
45 uint8_t* data = nullptr;
51 png_write_data (png_structp png_ptr, png_bytep data, png_size_t length)
53 auto mem = reinterpret_cast<Memory*>(png_get_io_ptr(png_ptr));
54 size_t size = mem->size + length;
57 mem->data = reinterpret_cast<uint8_t*>(realloc(mem->data, size));
59 mem->data = reinterpret_cast<uint8_t*>(malloc(size));
63 throw EncodeError (N_("could not allocate memory for PNG"));
66 memcpy (mem->data + mem->size, data, length);
72 png_flush (png_structp)
79 png_error_fn (png_structp, char const * message)
81 throw EncodeError (String::compose("Error during PNG write: %1", message));
86 image_as_png (shared_ptr<const Image> image)
88 DCPOMATIC_ASSERT (image->bytes_per_pixel(0) == 4);
89 DCPOMATIC_ASSERT (image->planes() == 1);
90 if (image->pixel_format() != AV_PIX_FMT_RGBA) {
91 return image_as_png(image->convert_pixel_format(dcp::YUVToRGB::REC709, AV_PIX_FMT_RGBA, Image::Alignment::PADDED, false));
95 auto png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, reinterpret_cast<void*>(const_cast<Image*>(image.get())), png_error_fn, 0);
97 throw EncodeError (N_("could not create PNG write struct"));
102 png_set_write_fn (png_ptr, &state, png_write_data, png_flush);
104 auto info_ptr = png_create_info_struct(png_ptr);
106 png_destroy_write_struct (&png_ptr, &info_ptr);
107 throw EncodeError (N_("could not create PNG info struct"));
110 int const width = image->size().width;
111 int const height = image->size().height;
113 png_set_IHDR (png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
115 auto row_pointers = reinterpret_cast<png_byte **>(png_malloc(png_ptr, image->size().height * sizeof(png_byte *)));
116 auto const data = image->data()[0];
117 auto const stride = image->stride()[0];
118 for (int i = 0; i < height; ++i) {
119 row_pointers[i] = reinterpret_cast<png_byte *>(data + i * stride);
122 png_write_info (png_ptr, info_ptr);
123 png_write_image (png_ptr, row_pointers);
124 png_write_end (png_ptr, info_ptr);
126 png_destroy_write_struct (&png_ptr, &info_ptr);
127 png_free (png_ptr, row_pointers);
129 return dcp::ArrayData (state.data, state.size);