KImageFormats

avif.cpp
1/*
2 AV1 Image File Format (AVIF) support for QImage.
3
4 SPDX-FileCopyrightText: 2020 Daniel Novomesky <dnovomesky@gmail.com>
5
6 SPDX-License-Identifier: BSD-2-Clause
7*/
8
9#include <QThread>
10#include <QtGlobal>
11
12#include <QColorSpace>
13
14#include "avif_p.h"
15#include "util_p.h"
16
17#include <cfloat>
18
19/*
20Quality range - compression/subsampling
21100 - lossless RGB compression
22< KIMG_AVIF_QUALITY_BEST, 100 ) - YUV444 color subsampling
23< KIMG_AVIF_QUALITY_HIGH, KIMG_AVIF_QUALITY_BEST ) - YUV422 color subsampling
24< 0, KIMG_AVIF_QUALITY_HIGH ) - YUV420 color subsampling
25< 0, KIMG_AVIF_QUALITY_LOW ) - lossy compression of alpha channel
26*/
27
28#ifndef KIMG_AVIF_DEFAULT_QUALITY
29#define KIMG_AVIF_DEFAULT_QUALITY 68
30#endif
31
32#ifndef KIMG_AVIF_QUALITY_BEST
33#define KIMG_AVIF_QUALITY_BEST 90
34#endif
35
36#ifndef KIMG_AVIF_QUALITY_HIGH
37#define KIMG_AVIF_QUALITY_HIGH 80
38#endif
39
40#ifndef KIMG_AVIF_QUALITY_LOW
41#define KIMG_AVIF_QUALITY_LOW 51
42#endif
43
44QAVIFHandler::QAVIFHandler()
45 : m_parseState(ParseAvifNotParsed)
46 , m_quality(KIMG_AVIF_DEFAULT_QUALITY)
47 , m_container_width(0)
48 , m_container_height(0)
49 , m_rawAvifData(AVIF_DATA_EMPTY)
50 , m_decoder(nullptr)
51 , m_must_jump_to_next_image(false)
52{
53}
54
55QAVIFHandler::~QAVIFHandler()
56{
57 if (m_decoder) {
58 avifDecoderDestroy(m_decoder);
59 }
60}
61
62bool QAVIFHandler::canRead() const
63{
64 if (m_parseState == ParseAvifNotParsed && !canRead(device())) {
65 return false;
66 }
67
68 if (m_parseState != ParseAvifError) {
69 setFormat("avif");
70
71 if (m_parseState == ParseAvifFinished) {
72 return false;
73 }
74
75 return true;
76 }
77 return false;
78}
79
80bool QAVIFHandler::canRead(QIODevice *device)
81{
82 if (!device) {
83 return false;
84 }
85 QByteArray header = device->peek(144);
86 if (header.size() < 12) {
87 return false;
88 }
89
90 avifROData input;
91 input.data = reinterpret_cast<const uint8_t *>(header.constData());
92 input.size = header.size();
93
94 if (avifPeekCompatibleFileType(&input)) {
95 return true;
96 }
97 return false;
98}
99
100bool QAVIFHandler::ensureParsed() const
101{
102 if (m_parseState == ParseAvifSuccess || m_parseState == ParseAvifMetadata || m_parseState == ParseAvifFinished) {
103 return true;
104 }
105 if (m_parseState == ParseAvifError) {
106 return false;
107 }
108
109 QAVIFHandler *that = const_cast<QAVIFHandler *>(this);
110
111 return that->ensureDecoder();
112}
113
114bool QAVIFHandler::ensureOpened() const
115{
116 if (m_parseState == ParseAvifSuccess || m_parseState == ParseAvifFinished) {
117 return true;
118 }
119 if (m_parseState == ParseAvifError) {
120 return false;
121 }
122
123 QAVIFHandler *that = const_cast<QAVIFHandler *>(this);
124 if (ensureParsed()) {
125 if (m_parseState == ParseAvifMetadata) {
126 bool success = that->jumpToNextImage();
127 that->m_parseState = success ? ParseAvifSuccess : ParseAvifError;
128 return success;
129 }
130 }
131
132 that->m_parseState = ParseAvifError;
133 return false;
134}
135
136bool QAVIFHandler::ensureDecoder()
137{
138 if (m_decoder) {
139 return true;
140 }
141
142 m_rawData = device()->readAll();
143
144 m_rawAvifData.data = reinterpret_cast<const uint8_t *>(m_rawData.constData());
145 m_rawAvifData.size = m_rawData.size();
146
147 if (avifPeekCompatibleFileType(&m_rawAvifData) == AVIF_FALSE) {
148 m_parseState = ParseAvifError;
149 return false;
150 }
151
152 m_decoder = avifDecoderCreate();
153
154 m_decoder->ignoreExif = AVIF_TRUE;
155 m_decoder->ignoreXMP = AVIF_TRUE;
156
157#if AVIF_VERSION >= 80400
158 m_decoder->maxThreads = qBound(1, QThread::idealThreadCount(), 64);
159#endif
160
161#if AVIF_VERSION >= 90100
162 m_decoder->strictFlags = AVIF_STRICT_DISABLED;
163#endif
164
165#if AVIF_VERSION >= 110000
166 m_decoder->imageDimensionLimit = 65535;
167#endif
168
169 avifResult decodeResult;
170
171 decodeResult = avifDecoderSetIOMemory(m_decoder, m_rawAvifData.data, m_rawAvifData.size);
172 if (decodeResult != AVIF_RESULT_OK) {
173 qWarning("ERROR: avifDecoderSetIOMemory failed: %s", avifResultToString(decodeResult));
174
175 avifDecoderDestroy(m_decoder);
176 m_decoder = nullptr;
177 m_parseState = ParseAvifError;
178 return false;
179 }
180
181 decodeResult = avifDecoderParse(m_decoder);
182 if (decodeResult != AVIF_RESULT_OK) {
183 qWarning("ERROR: Failed to parse input: %s", avifResultToString(decodeResult));
184
185 avifDecoderDestroy(m_decoder);
186 m_decoder = nullptr;
187 m_parseState = ParseAvifError;
188 return false;
189 }
190
191 m_container_width = m_decoder->image->width;
192 m_container_height = m_decoder->image->height;
193
194 if ((m_container_width > 65535) || (m_container_height > 65535)) {
195 qWarning("AVIF image (%dx%d) is too large!", m_container_width, m_container_height);
196 m_parseState = ParseAvifError;
197 return false;
198 }
199
200 if ((m_container_width == 0) || (m_container_height == 0)) {
201 qWarning("Empty image, nothing to decode");
202 m_parseState = ParseAvifError;
203 return false;
204 }
205
206 if (m_container_width > ((16384 * 16384) / m_container_height)) {
207 qWarning("AVIF image (%dx%d) has more than 256 megapixels!", m_container_width, m_container_height);
208 m_parseState = ParseAvifError;
209 return false;
210 }
211
212 // calculate final dimensions with crop and rotate operations applied
213 int new_width = m_container_width;
214 int new_height = m_container_height;
215
216 if (m_decoder->image->transformFlags & AVIF_TRANSFORM_CLAP) {
217 if ((m_decoder->image->clap.widthD > 0) && (m_decoder->image->clap.heightD > 0) && (m_decoder->image->clap.horizOffD > 0)
218 && (m_decoder->image->clap.vertOffD > 0)) {
219 int crop_width = (int)((double)(m_decoder->image->clap.widthN) / (m_decoder->image->clap.widthD) + 0.5);
220 if (crop_width < new_width && crop_width > 0) {
221 new_width = crop_width;
222 }
223 int crop_height = (int)((double)(m_decoder->image->clap.heightN) / (m_decoder->image->clap.heightD) + 0.5);
224 if (crop_height < new_height && crop_height > 0) {
225 new_height = crop_height;
226 }
227 }
228 }
229
230 if (m_decoder->image->transformFlags & AVIF_TRANSFORM_IROT) {
231 if (m_decoder->image->irot.angle == 1 || m_decoder->image->irot.angle == 3) {
232 int tmp = new_width;
233 new_width = new_height;
234 new_height = tmp;
235 }
236 }
237
238 m_estimated_dimensions.setWidth(new_width);
239 m_estimated_dimensions.setHeight(new_height);
240
241 m_parseState = ParseAvifMetadata;
242 return true;
243}
244
245bool QAVIFHandler::decode_one_frame()
246{
247 if (!ensureParsed()) {
248 return false;
249 }
250
251 bool loadalpha;
252
253 if (m_decoder->image->alphaPlane) {
254 loadalpha = true;
255 } else {
256 loadalpha = false;
257 }
258
259 QImage::Format resultformat;
260
261 if (m_decoder->image->depth > 8) {
262 if (loadalpha) {
263 resultformat = QImage::Format_RGBA64;
264 } else {
265 resultformat = QImage::Format_RGBX64;
266 }
267 } else {
268 if (loadalpha) {
269 resultformat = QImage::Format_ARGB32;
270 } else {
271 resultformat = QImage::Format_RGB32;
272 }
273 }
274
275 QImage result = imageAlloc(m_decoder->image->width, m_decoder->image->height, resultformat);
276 if (result.isNull()) {
277 qWarning("Memory cannot be allocated");
278 return false;
279 }
280
281 QColorSpace colorspace;
282 if (m_decoder->image->icc.data && (m_decoder->image->icc.size > 0)) {
283 const QByteArray icc_data(reinterpret_cast<const char *>(m_decoder->image->icc.data), m_decoder->image->icc.size);
284 colorspace = QColorSpace::fromIccProfile(icc_data);
285 if (!colorspace.isValid()) {
286 qWarning("AVIF image has Qt-unsupported or invalid ICC profile!");
287 }
288 } else {
289 float prim[8] = {0.64f, 0.33f, 0.3f, 0.6f, 0.15f, 0.06f, 0.3127f, 0.329f};
290 // outPrimaries: rX, rY, gX, gY, bX, bY, wX, wY
291 avifColorPrimariesGetValues(m_decoder->image->colorPrimaries, prim);
292
293 const QPointF redPoint(QAVIFHandler::CompatibleChromacity(prim[0], prim[1]));
294 const QPointF greenPoint(QAVIFHandler::CompatibleChromacity(prim[2], prim[3]));
295 const QPointF bluePoint(QAVIFHandler::CompatibleChromacity(prim[4], prim[5]));
296 const QPointF whitePoint(QAVIFHandler::CompatibleChromacity(prim[6], prim[7]));
297
298 QColorSpace::TransferFunction q_trc = QColorSpace::TransferFunction::Custom;
299 float q_trc_gamma = 0.0f;
300
301 switch (m_decoder->image->transferCharacteristics) {
302 /* AVIF_TRANSFER_CHARACTERISTICS_BT470M */
303 case 4:
304 q_trc = QColorSpace::TransferFunction::Gamma;
305 q_trc_gamma = 2.2f;
306 break;
307 /* AVIF_TRANSFER_CHARACTERISTICS_BT470BG */
308 case 5:
309 q_trc = QColorSpace::TransferFunction::Gamma;
310 q_trc_gamma = 2.8f;
311 break;
312 /* AVIF_TRANSFER_CHARACTERISTICS_LINEAR */
313 case 8:
314 q_trc = QColorSpace::TransferFunction::Linear;
315 break;
316 /* AVIF_TRANSFER_CHARACTERISTICS_SRGB */
317 case 0:
318 case 2: /* AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED */
319 case 13:
320 q_trc = QColorSpace::TransferFunction::SRgb;
321 break;
322 default:
323 qWarning("CICP colorPrimaries: %d, transferCharacteristics: %d\nThe colorspace is unsupported by this plug-in yet.",
324 m_decoder->image->colorPrimaries,
325 m_decoder->image->transferCharacteristics);
326 q_trc = QColorSpace::TransferFunction::SRgb;
327 break;
328 }
329
330 if (q_trc != QColorSpace::TransferFunction::Custom) { // we create new colorspace using Qt
331 switch (m_decoder->image->colorPrimaries) {
332 /* AVIF_COLOR_PRIMARIES_BT709 */
333 case 0:
334 case 1:
335 case 2: /* AVIF_COLOR_PRIMARIES_UNSPECIFIED */
336 colorspace = QColorSpace(QColorSpace::Primaries::SRgb, q_trc, q_trc_gamma);
337 break;
338 /* AVIF_COLOR_PRIMARIES_SMPTE432 */
339 case 12:
340 colorspace = QColorSpace(QColorSpace::Primaries::DciP3D65, q_trc, q_trc_gamma);
341 break;
342 default:
343 colorspace = QColorSpace(whitePoint, redPoint, greenPoint, bluePoint, q_trc, q_trc_gamma);
344 break;
345 }
346 }
347
348 if (!colorspace.isValid()) {
349 qWarning("AVIF plugin created invalid QColorSpace from NCLX/CICP!");
350 }
351 }
352
353 result.setColorSpace(colorspace);
354
355 avifRGBImage rgb;
356 avifRGBImageSetDefaults(&rgb, m_decoder->image);
357
358#if AVIF_VERSION >= 1000000
359 rgb.maxThreads = m_decoder->maxThreads;
360#endif
361
362 if (m_decoder->image->depth > 8) {
363 rgb.depth = 16;
364 rgb.format = AVIF_RGB_FORMAT_RGBA;
365
366 if (!loadalpha && (m_decoder->image->yuvFormat == AVIF_PIXEL_FORMAT_YUV400)) {
367 resultformat = QImage::Format_Grayscale16;
368 }
369 } else {
370 rgb.depth = 8;
371#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
372 rgb.format = AVIF_RGB_FORMAT_BGRA;
373#else
374 rgb.format = AVIF_RGB_FORMAT_ARGB;
375#endif
376
377#if AVIF_VERSION >= 80400
378 if (m_decoder->imageCount > 1) {
379 /* accelerate animated AVIF */
380 rgb.chromaUpsampling = AVIF_CHROMA_UPSAMPLING_FASTEST;
381 }
382#endif
383
384 if (!loadalpha && (m_decoder->image->yuvFormat == AVIF_PIXEL_FORMAT_YUV400)) {
385 resultformat = QImage::Format_Grayscale8;
386 }
387 }
388
389 rgb.rowBytes = result.bytesPerLine();
390 rgb.pixels = result.bits();
391
392 avifResult res = avifImageYUVToRGB(m_decoder->image, &rgb);
393 if (res != AVIF_RESULT_OK) {
394 qWarning("ERROR in avifImageYUVToRGB: %s", avifResultToString(res));
395 return false;
396 }
397
398 if (m_decoder->image->transformFlags & AVIF_TRANSFORM_CLAP) {
399 if ((m_decoder->image->clap.widthD > 0) && (m_decoder->image->clap.heightD > 0) && (m_decoder->image->clap.horizOffD > 0)
400 && (m_decoder->image->clap.vertOffD > 0)) {
401 int new_width = (int)((double)(m_decoder->image->clap.widthN) / (m_decoder->image->clap.widthD) + 0.5);
402 if (new_width > result.width()) {
403 new_width = result.width();
404 }
405
406 int new_height = (int)((double)(m_decoder->image->clap.heightN) / (m_decoder->image->clap.heightD) + 0.5);
407 if (new_height > result.height()) {
408 new_height = result.height();
409 }
410
411 if (new_width > 0 && new_height > 0) {
412 int offx =
413 ((double)((int32_t)m_decoder->image->clap.horizOffN)) / (m_decoder->image->clap.horizOffD) + (result.width() - new_width) / 2.0 + 0.5;
414 if (offx < 0) {
415 offx = 0;
416 } else if (offx > (result.width() - new_width)) {
417 offx = result.width() - new_width;
418 }
419
420 int offy =
421 ((double)((int32_t)m_decoder->image->clap.vertOffN)) / (m_decoder->image->clap.vertOffD) + (result.height() - new_height) / 2.0 + 0.5;
422 if (offy < 0) {
423 offy = 0;
424 } else if (offy > (result.height() - new_height)) {
425 offy = result.height() - new_height;
426 }
427
428 result = result.copy(offx, offy, new_width, new_height);
429 }
430 }
431
432 else { // Zero values, we need to avoid 0 divide.
433 qWarning("ERROR: Wrong values in avifCleanApertureBox");
434 }
435 }
436
437 if (m_decoder->image->transformFlags & AVIF_TRANSFORM_IROT) {
439 switch (m_decoder->image->irot.angle) {
440 case 1:
441 transform.rotate(-90);
442 result = result.transformed(transform);
443 break;
444 case 2:
445 transform.rotate(180);
446 result = result.transformed(transform);
447 break;
448 case 3:
449 transform.rotate(90);
450 result = result.transformed(transform);
451 break;
452 }
453 }
454
455 if (m_decoder->image->transformFlags & AVIF_TRANSFORM_IMIR) {
456#if AVIF_VERSION > 90100 && AVIF_VERSION < 1000000
457 switch (m_decoder->image->imir.mode) {
458#else
459 switch (m_decoder->image->imir.axis) {
460#endif
461 case 0: // top-to-bottom
462 result = result.mirrored(false, true);
463 break;
464 case 1: // left-to-right
465 result = result.mirrored(true, false);
466 break;
467 }
468 }
469
470 if (resultformat == result.format()) {
471 m_current_image = result;
472 } else {
473 m_current_image = result.convertToFormat(resultformat);
474 }
475
476 m_estimated_dimensions = m_current_image.size();
477
478 m_must_jump_to_next_image = false;
479 return true;
480}
481
482bool QAVIFHandler::read(QImage *image)
483{
484 if (!ensureOpened()) {
485 return false;
486 }
487
488 if (m_must_jump_to_next_image) {
489 jumpToNextImage();
490 }
491
492 *image = m_current_image;
493 if (imageCount() >= 2) {
494 m_must_jump_to_next_image = true;
495 if (m_decoder->imageIndex >= m_decoder->imageCount - 1) {
496 // all frames in animation have been read
497 m_parseState = ParseAvifFinished;
498 }
499 } else {
500 // the static image has been read
501 m_parseState = ParseAvifFinished;
502 }
503 return true;
504}
505
506bool QAVIFHandler::write(const QImage &image)
507{
508 if (image.format() == QImage::Format_Invalid) {
509 qWarning("No image data to save!");
510 return false;
511 }
512
513 if ((image.width() > 0) && (image.height() > 0)) {
514 if ((image.width() > 65535) || (image.height() > 65535)) {
515 qWarning("Image (%dx%d) is too large to save!", image.width(), image.height());
516 return false;
517 }
518
519 if (image.width() > ((16384 * 16384) / image.height())) {
520 qWarning("Image (%dx%d) will not be saved because it has more than 256 megapixels!", image.width(), image.height());
521 return false;
522 }
523
524 if ((image.width() > 32768) || (image.height() > 32768)) {
525 qWarning("Image (%dx%d) has a dimension above 32768 pixels, saved AVIF may not work in other software!", image.width(), image.height());
526 }
527 } else {
528 qWarning("Image has zero dimension!");
529 return false;
530 }
531
532 const char *encoder_name = avifCodecName(AVIF_CODEC_CHOICE_AUTO, AVIF_CODEC_FLAG_CAN_ENCODE);
533 if (!encoder_name) {
534 qWarning("Cannot save AVIF images because libavif was built without AV1 encoders!");
535 return false;
536 }
537
538 bool lossless = false;
539 if (m_quality >= 100) {
540 if (avifCodecName(AVIF_CODEC_CHOICE_AOM, AVIF_CODEC_FLAG_CAN_ENCODE)) {
541 lossless = true;
542 } else {
543 qWarning("You are using %s encoder. It is recommended to enable libAOM encoder in libavif to use lossless compression.", encoder_name);
544 }
545 }
546
547 if (m_quality > 100) {
548 m_quality = 100;
549 } else if (m_quality < 0) {
550 m_quality = KIMG_AVIF_DEFAULT_QUALITY;
551 }
552
553#if AVIF_VERSION < 1000000
554 int maxQuantizer = AVIF_QUANTIZER_WORST_QUALITY * (100 - qBound(0, m_quality, 100)) / 100;
555 int minQuantizer = 0;
556 int maxQuantizerAlpha = 0;
557#endif
558 avifResult res;
559
560 bool save_grayscale; // true - monochrome, false - colors
561 int save_depth; // 8 or 10bit per channel
562 QImage::Format tmpformat; // format for temporary image
563
564 avifImage *avif = nullptr;
565
566 // grayscale detection
567 switch (image.format()) {
572 save_grayscale = true;
573 break;
575 save_grayscale = image.isGrayscale();
576 break;
577 default:
578 save_grayscale = false;
579 break;
580 }
581
582 // depth detection
583 switch (image.format()) {
592 save_depth = 10;
593 break;
594 default:
595 if (image.depth() > 32) {
596 save_depth = 10;
597 } else {
598 save_depth = 8;
599 }
600 break;
601 }
602
603#if AVIF_VERSION < 1000000
604 // deprecated quality settings
605 if (maxQuantizer > 20) {
606 minQuantizer = maxQuantizer - 20;
607 if (maxQuantizer > 40) { // we decrease quality of alpha channel here
608 maxQuantizerAlpha = maxQuantizer - 40;
609 }
610 }
611#endif
612
613 if (save_grayscale && !image.hasAlphaChannel()) { // we are going to save grayscale image without alpha channel
614 if (save_depth > 8) {
615 tmpformat = QImage::Format_Grayscale16;
616 } else {
617 tmpformat = QImage::Format_Grayscale8;
618 }
619 QImage tmpgrayimage = image.convertToFormat(tmpformat);
620
621 avif = avifImageCreate(tmpgrayimage.width(), tmpgrayimage.height(), save_depth, AVIF_PIXEL_FORMAT_YUV400);
622 avifImageAllocatePlanes(avif, AVIF_PLANES_YUV);
623
624 if (tmpgrayimage.colorSpace().isValid()) {
625 avif->colorPrimaries = (avifColorPrimaries)1;
626 avif->matrixCoefficients = (avifMatrixCoefficients)1;
627
628 switch (tmpgrayimage.colorSpace().transferFunction()) {
629 case QColorSpace::TransferFunction::Linear:
630 /* AVIF_TRANSFER_CHARACTERISTICS_LINEAR */
631 avif->transferCharacteristics = (avifTransferCharacteristics)8;
632 break;
633 case QColorSpace::TransferFunction::SRgb:
634 /* AVIF_TRANSFER_CHARACTERISTICS_SRGB */
635 avif->transferCharacteristics = (avifTransferCharacteristics)13;
636 break;
637 default:
638 /* AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED */
639 break;
640 }
641 }
642
643 if (save_depth > 8) { // QImage::Format_Grayscale16
644 for (int y = 0; y < tmpgrayimage.height(); y++) {
645 const uint16_t *src16bit = reinterpret_cast<const uint16_t *>(tmpgrayimage.constScanLine(y));
646 uint16_t *dest16bit = reinterpret_cast<uint16_t *>(avif->yuvPlanes[0] + y * avif->yuvRowBytes[0]);
647 for (int x = 0; x < tmpgrayimage.width(); x++) {
648 int tmp_pixelval = (int)(((float)(*src16bit) / 65535.0f) * 1023.0f + 0.5f); // downgrade to 10 bits
649 *dest16bit = qBound(0, tmp_pixelval, 1023);
650 dest16bit++;
651 src16bit++;
652 }
653 }
654 } else { // QImage::Format_Grayscale8
655 for (int y = 0; y < tmpgrayimage.height(); y++) {
656 const uchar *src8bit = tmpgrayimage.constScanLine(y);
657 uint8_t *dest8bit = avif->yuvPlanes[0] + y * avif->yuvRowBytes[0];
658 for (int x = 0; x < tmpgrayimage.width(); x++) {
659 *dest8bit = *src8bit;
660 dest8bit++;
661 src8bit++;
662 }
663 }
664 }
665
666 } else { // we are going to save color image
667 if (save_depth > 8) {
668 if (image.hasAlphaChannel()) {
669 tmpformat = QImage::Format_RGBA64;
670 } else {
671 tmpformat = QImage::Format_RGBX64;
672 }
673 } else { // 8bit depth
674 if (image.hasAlphaChannel()) {
675 tmpformat = QImage::Format_RGBA8888;
676 } else {
677 tmpformat = QImage::Format_RGB888;
678 }
679 }
680
681 QImage tmpcolorimage = image.convertToFormat(tmpformat);
682
683 avifPixelFormat pixel_format = AVIF_PIXEL_FORMAT_YUV420;
684 if (m_quality >= KIMG_AVIF_QUALITY_HIGH) {
685 if (m_quality >= KIMG_AVIF_QUALITY_BEST) {
686 pixel_format = AVIF_PIXEL_FORMAT_YUV444; // best quality
687 } else {
688 pixel_format = AVIF_PIXEL_FORMAT_YUV422; // high quality
689 }
690 }
691
692 avifMatrixCoefficients matrix_to_save = (avifMatrixCoefficients)1; // default for Qt 5.12 and 5.13;
693
694 avifColorPrimaries primaries_to_save = (avifColorPrimaries)2;
695 avifTransferCharacteristics transfer_to_save = (avifTransferCharacteristics)2;
696 QByteArray iccprofile;
697
698 if (tmpcolorimage.colorSpace().isValid()) {
699 switch (tmpcolorimage.colorSpace().primaries()) {
700 case QColorSpace::Primaries::SRgb:
701 /* AVIF_COLOR_PRIMARIES_BT709 */
702 primaries_to_save = (avifColorPrimaries)1;
703 /* AVIF_MATRIX_COEFFICIENTS_BT709 */
704 matrix_to_save = (avifMatrixCoefficients)1;
705 break;
706 case QColorSpace::Primaries::DciP3D65:
707 /* AVIF_NCLX_COLOUR_PRIMARIES_P3, AVIF_NCLX_COLOUR_PRIMARIES_SMPTE432 */
708 primaries_to_save = (avifColorPrimaries)12;
709 /* AVIF_MATRIX_COEFFICIENTS_CHROMA_DERIVED_NCL */
710 matrix_to_save = (avifMatrixCoefficients)12;
711 break;
712 default:
713 /* AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED */
714 primaries_to_save = (avifColorPrimaries)2;
715 /* AVIF_MATRIX_COEFFICIENTS_UNSPECIFIED */
716 matrix_to_save = (avifMatrixCoefficients)2;
717 break;
718 }
719
720 switch (tmpcolorimage.colorSpace().transferFunction()) {
721 case QColorSpace::TransferFunction::Linear:
722 /* AVIF_TRANSFER_CHARACTERISTICS_LINEAR */
723 transfer_to_save = (avifTransferCharacteristics)8;
724 break;
725 case QColorSpace::TransferFunction::Gamma:
726 if (qAbs(tmpcolorimage.colorSpace().gamma() - 2.2f) < 0.1f) {
727 /* AVIF_TRANSFER_CHARACTERISTICS_BT470M */
728 transfer_to_save = (avifTransferCharacteristics)4;
729 } else if (qAbs(tmpcolorimage.colorSpace().gamma() - 2.8f) < 0.1f) {
730 /* AVIF_TRANSFER_CHARACTERISTICS_BT470BG */
731 transfer_to_save = (avifTransferCharacteristics)5;
732 } else {
733 /* AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED */
734 transfer_to_save = (avifTransferCharacteristics)2;
735 }
736 break;
737 case QColorSpace::TransferFunction::SRgb:
738 /* AVIF_TRANSFER_CHARACTERISTICS_SRGB */
739 transfer_to_save = (avifTransferCharacteristics)13;
740 break;
741 default:
742 /* AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED */
743 transfer_to_save = (avifTransferCharacteristics)2;
744 break;
745 }
746
747 // in case primaries or trc were not identified
748 if ((primaries_to_save == 2) || (transfer_to_save == 2)) {
749 if (lossless) {
750 iccprofile = tmpcolorimage.colorSpace().iccProfile();
751 } else {
752 // upgrade image to higher bit depth
753 if (save_depth == 8) {
754 save_depth = 10;
755 if (tmpcolorimage.hasAlphaChannel()) {
756 tmpcolorimage.convertTo(QImage::Format_RGBA64);
757 } else {
758 tmpcolorimage.convertTo(QImage::Format_RGBX64);
759 }
760 }
761
762 if ((primaries_to_save == 2) && (transfer_to_save != 2)) { // other primaries but known trc
763 primaries_to_save = (avifColorPrimaries)1; // AVIF_COLOR_PRIMARIES_BT709
764 matrix_to_save = (avifMatrixCoefficients)1; // AVIF_MATRIX_COEFFICIENTS_BT709
765
766 switch (transfer_to_save) {
767 case 8: // AVIF_TRANSFER_CHARACTERISTICS_LINEAR
768 tmpcolorimage.convertToColorSpace(QColorSpace(QColorSpace::Primaries::SRgb, QColorSpace::TransferFunction::Linear));
769 break;
770 case 4: // AVIF_TRANSFER_CHARACTERISTICS_BT470M
771 tmpcolorimage.convertToColorSpace(QColorSpace(QColorSpace::Primaries::SRgb, 2.2f));
772 break;
773 case 5: // AVIF_TRANSFER_CHARACTERISTICS_BT470BG
774 tmpcolorimage.convertToColorSpace(QColorSpace(QColorSpace::Primaries::SRgb, 2.8f));
775 break;
776 default: // AVIF_TRANSFER_CHARACTERISTICS_SRGB + any other
777 tmpcolorimage.convertToColorSpace(QColorSpace(QColorSpace::Primaries::SRgb, QColorSpace::TransferFunction::SRgb));
778 transfer_to_save = (avifTransferCharacteristics)13;
779 break;
780 }
781 } else if ((primaries_to_save != 2) && (transfer_to_save == 2)) { // recognized primaries but other trc
782 transfer_to_save = (avifTransferCharacteristics)13;
783 tmpcolorimage.convertToColorSpace(tmpcolorimage.colorSpace().withTransferFunction(QColorSpace::TransferFunction::SRgb));
784 } else { // unrecognized profile
785 primaries_to_save = (avifColorPrimaries)1; // AVIF_COLOR_PRIMARIES_BT709
786 transfer_to_save = (avifTransferCharacteristics)13;
787 matrix_to_save = (avifMatrixCoefficients)1; // AVIF_MATRIX_COEFFICIENTS_BT709
788 tmpcolorimage.convertToColorSpace(QColorSpace(QColorSpace::Primaries::SRgb, QColorSpace::TransferFunction::SRgb));
789 }
790 }
791 }
792 } else { // profile is unsupported by Qt
793 iccprofile = tmpcolorimage.colorSpace().iccProfile();
794 if (iccprofile.size() > 0) {
795 matrix_to_save = (avifMatrixCoefficients)6;
796 }
797 }
798
799 if (lossless && pixel_format == AVIF_PIXEL_FORMAT_YUV444) {
800 matrix_to_save = (avifMatrixCoefficients)0;
801 }
802 avif = avifImageCreate(tmpcolorimage.width(), tmpcolorimage.height(), save_depth, pixel_format);
803 avif->matrixCoefficients = matrix_to_save;
804
805 avif->colorPrimaries = primaries_to_save;
806 avif->transferCharacteristics = transfer_to_save;
807
808 if (iccprofile.size() > 0) {
809 avifImageSetProfileICC(avif, reinterpret_cast<const uint8_t *>(iccprofile.constData()), iccprofile.size());
810 }
811
812 avifRGBImage rgb;
813 avifRGBImageSetDefaults(&rgb, avif);
814 rgb.rowBytes = tmpcolorimage.bytesPerLine();
815 rgb.pixels = const_cast<uint8_t *>(tmpcolorimage.constBits());
816
817 if (save_depth > 8) { // 10bit depth
818 rgb.depth = 16;
819
820 if (!tmpcolorimage.hasAlphaChannel()) {
821 rgb.ignoreAlpha = AVIF_TRUE;
822 }
823
824 rgb.format = AVIF_RGB_FORMAT_RGBA;
825 } else { // 8bit depth
826 rgb.depth = 8;
827
828 if (tmpcolorimage.hasAlphaChannel()) {
829 rgb.format = AVIF_RGB_FORMAT_RGBA;
830 } else {
831 rgb.format = AVIF_RGB_FORMAT_RGB;
832 }
833 }
834
835 res = avifImageRGBToYUV(avif, &rgb);
836 if (res != AVIF_RESULT_OK) {
837 qWarning("ERROR in avifImageRGBToYUV: %s", avifResultToString(res));
838 return false;
839 }
840 }
841
842 avifRWData raw = AVIF_DATA_EMPTY;
843 avifEncoder *encoder = avifEncoderCreate();
844 encoder->maxThreads = qBound(1, QThread::idealThreadCount(), 64);
845
846#if AVIF_VERSION < 1000000
847 encoder->minQuantizer = minQuantizer;
848 encoder->maxQuantizer = maxQuantizer;
849
850 if (image.hasAlphaChannel()) {
851 encoder->minQuantizerAlpha = AVIF_QUANTIZER_LOSSLESS;
852 encoder->maxQuantizerAlpha = maxQuantizerAlpha;
853 }
854#else
855 encoder->quality = m_quality;
856
857 if (image.hasAlphaChannel()) {
858 if (m_quality >= KIMG_AVIF_QUALITY_LOW) {
859 encoder->qualityAlpha = 100;
860 } else {
861 encoder->qualityAlpha = 100 - (KIMG_AVIF_QUALITY_LOW - m_quality) / 2;
862 }
863 }
864#endif
865
866 encoder->speed = 6;
867
868 res = avifEncoderWrite(encoder, avif, &raw);
869 avifEncoderDestroy(encoder);
870 avifImageDestroy(avif);
871
872 if (res == AVIF_RESULT_OK) {
873 qint64 status = device()->write(reinterpret_cast<const char *>(raw.data), raw.size);
874 avifRWDataFree(&raw);
875
876 if (status > 0) {
877 return true;
878 } else if (status == -1) {
879 qWarning("Write error: %s", qUtf8Printable(device()->errorString()));
880 return false;
881 }
882 } else {
883 qWarning("ERROR: Failed to encode: %s", avifResultToString(res));
884 }
885
886 return false;
887}
888
889QVariant QAVIFHandler::option(ImageOption option) const
890{
891 if (option == Quality) {
892 return m_quality;
893 }
894
895 if (!supportsOption(option) || !ensureParsed()) {
896 return QVariant();
897 }
898
899 switch (option) {
900 case Size:
901 return m_estimated_dimensions;
902 case Animation:
903 if (imageCount() >= 2) {
904 return true;
905 } else {
906 return false;
907 }
908 default:
909 return QVariant();
910 }
911}
912
913void QAVIFHandler::setOption(ImageOption option, const QVariant &value)
914{
915 switch (option) {
916 case Quality:
917 m_quality = value.toInt();
918 if (m_quality > 100) {
919 m_quality = 100;
920 } else if (m_quality < 0) {
921 m_quality = KIMG_AVIF_DEFAULT_QUALITY;
922 }
923 return;
924 default:
925 break;
926 }
927 QImageIOHandler::setOption(option, value);
928}
929
930bool QAVIFHandler::supportsOption(ImageOption option) const
931{
932 return option == Quality || option == Size || option == Animation;
933}
934
935int QAVIFHandler::imageCount() const
936{
937 if (!ensureParsed()) {
938 return 0;
939 }
940
941 if (m_decoder->imageCount >= 1) {
942 return m_decoder->imageCount;
943 }
944 return 0;
945}
946
947int QAVIFHandler::currentImageNumber() const
948{
949 if (m_parseState == ParseAvifNotParsed) {
950 return -1;
951 }
952
953 if (m_parseState == ParseAvifError || !m_decoder) {
954 return 0;
955 }
956
957 if (m_parseState == ParseAvifMetadata) {
958 if (m_decoder->imageCount >= 2) {
959 return -1;
960 } else {
961 return 0;
962 }
963 }
964
965 return m_decoder->imageIndex;
966}
967
968bool QAVIFHandler::jumpToNextImage()
969{
970 if (!ensureParsed()) {
971 return false;
972 }
973
974 if (m_decoder->imageIndex >= 0) {
975 if (m_decoder->imageCount < 2) {
976 m_parseState = ParseAvifSuccess;
977 return true;
978 }
979
980 if (m_decoder->imageIndex >= m_decoder->imageCount - 1) { // start from beginning
981 avifDecoderReset(m_decoder);
982 }
983 }
984
985 avifResult decodeResult = avifDecoderNextImage(m_decoder);
986
987 if (decodeResult != AVIF_RESULT_OK) {
988 qWarning("ERROR: Failed to decode Next image in sequence: %s", avifResultToString(decodeResult));
989 m_parseState = ParseAvifError;
990 return false;
991 }
992
993 if ((m_container_width != m_decoder->image->width) || (m_container_height != m_decoder->image->height)) {
994 qWarning("Decoded image sequence size (%dx%d) do not match first image size (%dx%d)!",
995 m_decoder->image->width,
996 m_decoder->image->height,
997 m_container_width,
998 m_container_height);
999
1000 m_parseState = ParseAvifError;
1001 return false;
1002 }
1003
1004 if (decode_one_frame()) {
1005 m_parseState = ParseAvifSuccess;
1006 return true;
1007 } else {
1008 m_parseState = ParseAvifError;
1009 return false;
1010 }
1011}
1012
1013bool QAVIFHandler::jumpToImage(int imageNumber)
1014{
1015 if (!ensureParsed()) {
1016 return false;
1017 }
1018
1019 if (m_decoder->imageCount < 2) { // not an animation
1020 if (imageNumber == 0) {
1021 if (ensureOpened()) {
1022 m_parseState = ParseAvifSuccess;
1023 return true;
1024 }
1025 }
1026 return false;
1027 }
1028
1029 if (imageNumber < 0 || imageNumber >= m_decoder->imageCount) { // wrong index
1030 return false;
1031 }
1032
1033 if (imageNumber == m_decoder->imageIndex) { // we are here already
1034 m_must_jump_to_next_image = false;
1035 m_parseState = ParseAvifSuccess;
1036 return true;
1037 }
1038
1039 avifResult decodeResult = avifDecoderNthImage(m_decoder, imageNumber);
1040
1041 if (decodeResult != AVIF_RESULT_OK) {
1042 qWarning("ERROR: Failed to decode %d th Image in sequence: %s", imageNumber, avifResultToString(decodeResult));
1043 m_parseState = ParseAvifError;
1044 return false;
1045 }
1046
1047 if ((m_container_width != m_decoder->image->width) || (m_container_height != m_decoder->image->height)) {
1048 qWarning("Decoded image sequence size (%dx%d) do not match declared container size (%dx%d)!",
1049 m_decoder->image->width,
1050 m_decoder->image->height,
1051 m_container_width,
1052 m_container_height);
1053
1054 m_parseState = ParseAvifError;
1055 return false;
1056 }
1057
1058 if (decode_one_frame()) {
1059 m_parseState = ParseAvifSuccess;
1060 return true;
1061 } else {
1062 m_parseState = ParseAvifError;
1063 return false;
1064 }
1065}
1066
1067int QAVIFHandler::nextImageDelay() const
1068{
1069 if (!ensureOpened()) {
1070 return 0;
1071 }
1072
1073 if (m_decoder->imageCount < 2) {
1074 return 0;
1075 }
1076
1077 int delay_ms = 1000.0 * m_decoder->imageTiming.duration;
1078 if (delay_ms < 1) {
1079 delay_ms = 1;
1080 }
1081 return delay_ms;
1082}
1083
1084int QAVIFHandler::loopCount() const
1085{
1086 if (!ensureParsed()) {
1087 return 0;
1088 }
1089
1090 if (m_decoder->imageCount < 2) {
1091 return 0;
1092 }
1093
1094#if AVIF_VERSION >= 1000000
1095 if (m_decoder->repetitionCount >= 0) {
1096 return m_decoder->repetitionCount;
1097 }
1098#endif
1099 // Endless loop to work around https://github.com/AOMediaCodec/libavif/issues/347
1100 return -1;
1101}
1102
1103QPointF QAVIFHandler::CompatibleChromacity(qreal chrX, qreal chrY)
1104{
1105 chrX = qBound(qreal(0.0), chrX, qreal(1.0));
1106 chrY = qBound(qreal(DBL_MIN), chrY, qreal(1.0));
1107
1108 if ((chrX + chrY) > qreal(1.0)) {
1109 chrX = qreal(1.0) - chrY;
1110 }
1111
1112 return QPointF(chrX, chrY);
1113}
1114
1115QImageIOPlugin::Capabilities QAVIFPlugin::capabilities(QIODevice *device, const QByteArray &format) const
1116{
1117 static const bool isAvifDecoderAvailable(avifCodecName(AVIF_CODEC_CHOICE_AUTO, AVIF_CODEC_FLAG_CAN_DECODE) != nullptr);
1118 static const bool isAvifEncoderAvailable(avifCodecName(AVIF_CODEC_CHOICE_AUTO, AVIF_CODEC_FLAG_CAN_ENCODE) != nullptr);
1119
1120 if (format == "avif") {
1121 Capabilities format_cap;
1122 if (isAvifDecoderAvailable) {
1123 format_cap |= CanRead;
1124 }
1125 if (isAvifEncoderAvailable) {
1126 format_cap |= CanWrite;
1127 }
1128 return format_cap;
1129 }
1130
1131 if (format == "avifs") {
1132 Capabilities format_cap;
1133 if (isAvifDecoderAvailable) {
1134 format_cap |= CanRead;
1135 }
1136 return format_cap;
1137 }
1138
1139 if (!format.isEmpty()) {
1140 return {};
1141 }
1142 if (!device->isOpen()) {
1143 return {};
1144 }
1145
1146 Capabilities cap;
1147 if (device->isReadable() && QAVIFHandler::canRead(device) && isAvifDecoderAvailable) {
1148 cap |= CanRead;
1149 }
1150 if (device->isWritable() && isAvifEncoderAvailable) {
1151 cap |= CanWrite;
1152 }
1153 return cap;
1154}
1155
1156QImageIOHandler *QAVIFPlugin::create(QIODevice *device, const QByteArray &format) const
1157{
1158 QImageIOHandler *handler = new QAVIFHandler;
1159 handler->setDevice(device);
1160 handler->setFormat(format);
1161 return handler;
1162}
1163
1164#include "moc_avif_p.cpp"
Q_SCRIPTABLE CaptureState status()
KDOCTOOLS_EXPORT QString transform(const QString &file, const QString &stylesheet, const QList< const char * > &params=QList< const char * >())
QFlags< Capability > Capabilities
const char * constData() const const
char * data()
bool isEmpty() const const
qsizetype size() const const
QColorSpace fromIccProfile(const QByteArray &iccProfile)
float gamma() const const
QByteArray iccProfile() const const
bool isValid() const const
Primaries primaries() const const
TransferFunction transferFunction() const const
QColorSpace withTransferFunction(TransferFunction transferFunction, float gamma) const const
uchar * bits()
qsizetype bytesPerLine() const const
QColorSpace colorSpace() const const
const uchar * constBits() const const
const uchar * constScanLine(int i) const const
void convertTo(Format format, Qt::ImageConversionFlags flags)
void convertToColorSpace(const QColorSpace &colorSpace)
QImage convertToFormat(Format format, Qt::ImageConversionFlags flags) &&
QImage copy(const QRect &rectangle) const const
int depth() const const
Format format() const const
bool hasAlphaChannel() const const
int height() const const
bool isGrayscale() const const
bool isNull() const const
QImage mirrored(bool horizontal, bool vertical) &&
void setColorSpace(const QColorSpace &colorSpace)
QSize size() const const
QImage transformed(const QTransform &matrix, Qt::TransformationMode mode) const const
int width() const const
void setDevice(QIODevice *device)
void setFormat(const QByteArray &format)
virtual void setOption(ImageOption option, const QVariant &value)
typedef Capabilities
bool isOpen() const const
bool isReadable() const const
bool isWritable() const const
QByteArray peek(qint64 maxSize)
QByteArray readAll()
qint64 write(const QByteArray &data)
int idealThreadCount()
int toInt(bool *ok) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:12:40 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.