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#if AVIF_VERSION >= 110000
623 res = avifImageAllocatePlanes(avif, AVIF_PLANES_YUV);
624 if (res != AVIF_RESULT_OK) {
625 qWarning("ERROR in avifImageAllocatePlanes: %s", avifResultToString(res));
626 return false;
627 }
628#else
629 avifImageAllocatePlanes(avif, AVIF_PLANES_YUV);
630#endif
631
632 if (tmpgrayimage.colorSpace().isValid()) {
633 avif->colorPrimaries = (avifColorPrimaries)1;
634 avif->matrixCoefficients = (avifMatrixCoefficients)1;
635
636 switch (tmpgrayimage.colorSpace().transferFunction()) {
637 case QColorSpace::TransferFunction::Linear:
638 /* AVIF_TRANSFER_CHARACTERISTICS_LINEAR */
639 avif->transferCharacteristics = (avifTransferCharacteristics)8;
640 break;
641 case QColorSpace::TransferFunction::SRgb:
642 /* AVIF_TRANSFER_CHARACTERISTICS_SRGB */
643 avif->transferCharacteristics = (avifTransferCharacteristics)13;
644 break;
645 default:
646 /* AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED */
647 break;
648 }
649 }
650
651 if (save_depth > 8) { // QImage::Format_Grayscale16
652 for (int y = 0; y < tmpgrayimage.height(); y++) {
653 const uint16_t *src16bit = reinterpret_cast<const uint16_t *>(tmpgrayimage.constScanLine(y));
654 uint16_t *dest16bit = reinterpret_cast<uint16_t *>(avif->yuvPlanes[0] + y * avif->yuvRowBytes[0]);
655 for (int x = 0; x < tmpgrayimage.width(); x++) {
656 int tmp_pixelval = (int)(((float)(*src16bit) / 65535.0f) * 1023.0f + 0.5f); // downgrade to 10 bits
657 *dest16bit = qBound(0, tmp_pixelval, 1023);
658 dest16bit++;
659 src16bit++;
660 }
661 }
662 } else { // QImage::Format_Grayscale8
663 for (int y = 0; y < tmpgrayimage.height(); y++) {
664 const uchar *src8bit = tmpgrayimage.constScanLine(y);
665 uint8_t *dest8bit = avif->yuvPlanes[0] + y * avif->yuvRowBytes[0];
666 for (int x = 0; x < tmpgrayimage.width(); x++) {
667 *dest8bit = *src8bit;
668 dest8bit++;
669 src8bit++;
670 }
671 }
672 }
673
674 } else { // we are going to save color image
675 if (save_depth > 8) {
676 if (image.hasAlphaChannel()) {
677 tmpformat = QImage::Format_RGBA64;
678 } else {
679 tmpformat = QImage::Format_RGBX64;
680 }
681 } else { // 8bit depth
682 if (image.hasAlphaChannel()) {
683 tmpformat = QImage::Format_RGBA8888;
684 } else {
685 tmpformat = QImage::Format_RGB888;
686 }
687 }
688
689 QImage tmpcolorimage = image.convertToFormat(tmpformat);
690
691 avifPixelFormat pixel_format = AVIF_PIXEL_FORMAT_YUV420;
692 if (m_quality >= KIMG_AVIF_QUALITY_HIGH) {
693 if (m_quality >= KIMG_AVIF_QUALITY_BEST) {
694 pixel_format = AVIF_PIXEL_FORMAT_YUV444; // best quality
695 } else {
696 pixel_format = AVIF_PIXEL_FORMAT_YUV422; // high quality
697 }
698 }
699
700 avifMatrixCoefficients matrix_to_save = (avifMatrixCoefficients)1; // default for Qt 5.12 and 5.13;
701
702 avifColorPrimaries primaries_to_save = (avifColorPrimaries)2;
703 avifTransferCharacteristics transfer_to_save = (avifTransferCharacteristics)2;
704 QByteArray iccprofile;
705
706 if (tmpcolorimage.colorSpace().isValid()) {
707 switch (tmpcolorimage.colorSpace().primaries()) {
708 case QColorSpace::Primaries::SRgb:
709 /* AVIF_COLOR_PRIMARIES_BT709 */
710 primaries_to_save = (avifColorPrimaries)1;
711 /* AVIF_MATRIX_COEFFICIENTS_BT709 */
712 matrix_to_save = (avifMatrixCoefficients)1;
713 break;
714 case QColorSpace::Primaries::DciP3D65:
715 /* AVIF_NCLX_COLOUR_PRIMARIES_P3, AVIF_NCLX_COLOUR_PRIMARIES_SMPTE432 */
716 primaries_to_save = (avifColorPrimaries)12;
717 /* AVIF_MATRIX_COEFFICIENTS_CHROMA_DERIVED_NCL */
718 matrix_to_save = (avifMatrixCoefficients)12;
719 break;
720 default:
721 /* AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED */
722 primaries_to_save = (avifColorPrimaries)2;
723 /* AVIF_MATRIX_COEFFICIENTS_UNSPECIFIED */
724 matrix_to_save = (avifMatrixCoefficients)2;
725 break;
726 }
727
728 switch (tmpcolorimage.colorSpace().transferFunction()) {
729 case QColorSpace::TransferFunction::Linear:
730 /* AVIF_TRANSFER_CHARACTERISTICS_LINEAR */
731 transfer_to_save = (avifTransferCharacteristics)8;
732 break;
733 case QColorSpace::TransferFunction::Gamma:
734 if (qAbs(tmpcolorimage.colorSpace().gamma() - 2.2f) < 0.1f) {
735 /* AVIF_TRANSFER_CHARACTERISTICS_BT470M */
736 transfer_to_save = (avifTransferCharacteristics)4;
737 } else if (qAbs(tmpcolorimage.colorSpace().gamma() - 2.8f) < 0.1f) {
738 /* AVIF_TRANSFER_CHARACTERISTICS_BT470BG */
739 transfer_to_save = (avifTransferCharacteristics)5;
740 } else {
741 /* AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED */
742 transfer_to_save = (avifTransferCharacteristics)2;
743 }
744 break;
745 case QColorSpace::TransferFunction::SRgb:
746 /* AVIF_TRANSFER_CHARACTERISTICS_SRGB */
747 transfer_to_save = (avifTransferCharacteristics)13;
748 break;
749 default:
750 /* AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED */
751 transfer_to_save = (avifTransferCharacteristics)2;
752 break;
753 }
754
755 // in case primaries or trc were not identified
756 if ((primaries_to_save == 2) || (transfer_to_save == 2)) {
757 if (lossless) {
758 iccprofile = tmpcolorimage.colorSpace().iccProfile();
759 } else {
760 // upgrade image to higher bit depth
761 if (save_depth == 8) {
762 save_depth = 10;
763 if (tmpcolorimage.hasAlphaChannel()) {
764 tmpcolorimage.convertTo(QImage::Format_RGBA64);
765 } else {
766 tmpcolorimage.convertTo(QImage::Format_RGBX64);
767 }
768 }
769
770 if ((primaries_to_save == 2) && (transfer_to_save != 2)) { // other primaries but known trc
771 primaries_to_save = (avifColorPrimaries)1; // AVIF_COLOR_PRIMARIES_BT709
772 matrix_to_save = (avifMatrixCoefficients)1; // AVIF_MATRIX_COEFFICIENTS_BT709
773
774 switch (transfer_to_save) {
775 case 8: // AVIF_TRANSFER_CHARACTERISTICS_LINEAR
776 tmpcolorimage.convertToColorSpace(QColorSpace(QColorSpace::Primaries::SRgb, QColorSpace::TransferFunction::Linear));
777 break;
778 case 4: // AVIF_TRANSFER_CHARACTERISTICS_BT470M
779 tmpcolorimage.convertToColorSpace(QColorSpace(QColorSpace::Primaries::SRgb, 2.2f));
780 break;
781 case 5: // AVIF_TRANSFER_CHARACTERISTICS_BT470BG
782 tmpcolorimage.convertToColorSpace(QColorSpace(QColorSpace::Primaries::SRgb, 2.8f));
783 break;
784 default: // AVIF_TRANSFER_CHARACTERISTICS_SRGB + any other
785 tmpcolorimage.convertToColorSpace(QColorSpace(QColorSpace::Primaries::SRgb, QColorSpace::TransferFunction::SRgb));
786 transfer_to_save = (avifTransferCharacteristics)13;
787 break;
788 }
789 } else if ((primaries_to_save != 2) && (transfer_to_save == 2)) { // recognized primaries but other trc
790 transfer_to_save = (avifTransferCharacteristics)13;
791 tmpcolorimage.convertToColorSpace(tmpcolorimage.colorSpace().withTransferFunction(QColorSpace::TransferFunction::SRgb));
792 } else { // unrecognized profile
793 primaries_to_save = (avifColorPrimaries)1; // AVIF_COLOR_PRIMARIES_BT709
794 transfer_to_save = (avifTransferCharacteristics)13;
795 matrix_to_save = (avifMatrixCoefficients)1; // AVIF_MATRIX_COEFFICIENTS_BT709
796 tmpcolorimage.convertToColorSpace(QColorSpace(QColorSpace::Primaries::SRgb, QColorSpace::TransferFunction::SRgb));
797 }
798 }
799 }
800 } else { // profile is unsupported by Qt
801 iccprofile = tmpcolorimage.colorSpace().iccProfile();
802 if (iccprofile.size() > 0) {
803 matrix_to_save = (avifMatrixCoefficients)6;
804 }
805 }
806
807 if (lossless && pixel_format == AVIF_PIXEL_FORMAT_YUV444) {
808 matrix_to_save = (avifMatrixCoefficients)0;
809 }
810 avif = avifImageCreate(tmpcolorimage.width(), tmpcolorimage.height(), save_depth, pixel_format);
811 avif->matrixCoefficients = matrix_to_save;
812
813 avif->colorPrimaries = primaries_to_save;
814 avif->transferCharacteristics = transfer_to_save;
815
816 if (iccprofile.size() > 0) {
817#if AVIF_VERSION >= 1000000
818 res = avifImageSetProfileICC(avif, reinterpret_cast<const uint8_t *>(iccprofile.constData()), iccprofile.size());
819 if (res != AVIF_RESULT_OK) {
820 qWarning("ERROR in avifImageSetProfileICC: %s", avifResultToString(res));
821 return false;
822 }
823#else
824 avifImageSetProfileICC(avif, reinterpret_cast<const uint8_t *>(iccprofile.constData()), iccprofile.size());
825#endif
826 }
827
828 avifRGBImage rgb;
829 avifRGBImageSetDefaults(&rgb, avif);
830 rgb.rowBytes = tmpcolorimage.bytesPerLine();
831 rgb.pixels = const_cast<uint8_t *>(tmpcolorimage.constBits());
832
833 if (save_depth > 8) { // 10bit depth
834 rgb.depth = 16;
835
836 if (!tmpcolorimage.hasAlphaChannel()) {
837 rgb.ignoreAlpha = AVIF_TRUE;
838 }
839
840 rgb.format = AVIF_RGB_FORMAT_RGBA;
841 } else { // 8bit depth
842 rgb.depth = 8;
843
844 if (tmpcolorimage.hasAlphaChannel()) {
845 rgb.format = AVIF_RGB_FORMAT_RGBA;
846 } else {
847 rgb.format = AVIF_RGB_FORMAT_RGB;
848 }
849 }
850
851 res = avifImageRGBToYUV(avif, &rgb);
852 if (res != AVIF_RESULT_OK) {
853 qWarning("ERROR in avifImageRGBToYUV: %s", avifResultToString(res));
854 return false;
855 }
856 }
857
858 avifRWData raw = AVIF_DATA_EMPTY;
859 avifEncoder *encoder = avifEncoderCreate();
860 encoder->maxThreads = qBound(1, QThread::idealThreadCount(), 64);
861
862#if AVIF_VERSION < 1000000
863 encoder->minQuantizer = minQuantizer;
864 encoder->maxQuantizer = maxQuantizer;
865
866 if (image.hasAlphaChannel()) {
867 encoder->minQuantizerAlpha = AVIF_QUANTIZER_LOSSLESS;
868 encoder->maxQuantizerAlpha = maxQuantizerAlpha;
869 }
870#else
871 encoder->quality = m_quality;
872
873 if (image.hasAlphaChannel()) {
874 if (m_quality >= KIMG_AVIF_QUALITY_LOW) {
875 encoder->qualityAlpha = 100;
876 } else {
877 encoder->qualityAlpha = 100 - (KIMG_AVIF_QUALITY_LOW - m_quality) / 2;
878 }
879 }
880#endif
881
882 encoder->speed = 6;
883
884 res = avifEncoderWrite(encoder, avif, &raw);
885 avifEncoderDestroy(encoder);
886 avifImageDestroy(avif);
887
888 if (res == AVIF_RESULT_OK) {
889 qint64 status = device()->write(reinterpret_cast<const char *>(raw.data), raw.size);
890 avifRWDataFree(&raw);
891
892 if (status > 0) {
893 return true;
894 } else if (status == -1) {
895 qWarning("Write error: %s", qUtf8Printable(device()->errorString()));
896 return false;
897 }
898 } else {
899 qWarning("ERROR: Failed to encode: %s", avifResultToString(res));
900 }
901
902 return false;
903}
904
905QVariant QAVIFHandler::option(ImageOption option) const
906{
907 if (option == Quality) {
908 return m_quality;
909 }
910
911 if (!supportsOption(option) || !ensureParsed()) {
912 return QVariant();
913 }
914
915 switch (option) {
916 case Size:
917 return m_estimated_dimensions;
918 case Animation:
919 if (imageCount() >= 2) {
920 return true;
921 } else {
922 return false;
923 }
924 default:
925 return QVariant();
926 }
927}
928
929void QAVIFHandler::setOption(ImageOption option, const QVariant &value)
930{
931 switch (option) {
932 case Quality:
933 m_quality = value.toInt();
934 if (m_quality > 100) {
935 m_quality = 100;
936 } else if (m_quality < 0) {
937 m_quality = KIMG_AVIF_DEFAULT_QUALITY;
938 }
939 return;
940 default:
941 break;
942 }
943 QImageIOHandler::setOption(option, value);
944}
945
946bool QAVIFHandler::supportsOption(ImageOption option) const
947{
948 return option == Quality || option == Size || option == Animation;
949}
950
951int QAVIFHandler::imageCount() const
952{
953 if (!ensureParsed()) {
954 return 0;
955 }
956
957 if (m_decoder->imageCount >= 1) {
958 return m_decoder->imageCount;
959 }
960 return 0;
961}
962
963int QAVIFHandler::currentImageNumber() const
964{
965 if (m_parseState == ParseAvifNotParsed) {
966 return -1;
967 }
968
969 if (m_parseState == ParseAvifError || !m_decoder) {
970 return 0;
971 }
972
973 if (m_parseState == ParseAvifMetadata) {
974 if (m_decoder->imageCount >= 2) {
975 return -1;
976 } else {
977 return 0;
978 }
979 }
980
981 return m_decoder->imageIndex;
982}
983
984bool QAVIFHandler::jumpToNextImage()
985{
986 if (!ensureParsed()) {
987 return false;
988 }
989
990 avifResult decodeResult;
991
992 if (m_decoder->imageIndex >= 0) {
993 if (m_decoder->imageCount < 2) {
994 m_parseState = ParseAvifSuccess;
995 return true;
996 }
997
998 if (m_decoder->imageIndex >= m_decoder->imageCount - 1) { // start from beginning
999 decodeResult = avifDecoderReset(m_decoder);
1000 if (decodeResult != AVIF_RESULT_OK) {
1001 qWarning("ERROR in avifDecoderReset: %s", avifResultToString(decodeResult));
1002 m_parseState = ParseAvifError;
1003 return false;
1004 }
1005 }
1006 }
1007
1008 decodeResult = avifDecoderNextImage(m_decoder);
1009
1010 if (decodeResult != AVIF_RESULT_OK) {
1011 qWarning("ERROR: Failed to decode Next image in sequence: %s", avifResultToString(decodeResult));
1012 m_parseState = ParseAvifError;
1013 return false;
1014 }
1015
1016 if ((m_container_width != m_decoder->image->width) || (m_container_height != m_decoder->image->height)) {
1017 qWarning("Decoded image sequence size (%dx%d) do not match first image size (%dx%d)!",
1018 m_decoder->image->width,
1019 m_decoder->image->height,
1020 m_container_width,
1021 m_container_height);
1022
1023 m_parseState = ParseAvifError;
1024 return false;
1025 }
1026
1027 if (decode_one_frame()) {
1028 m_parseState = ParseAvifSuccess;
1029 return true;
1030 } else {
1031 m_parseState = ParseAvifError;
1032 return false;
1033 }
1034}
1035
1036bool QAVIFHandler::jumpToImage(int imageNumber)
1037{
1038 if (!ensureParsed()) {
1039 return false;
1040 }
1041
1042 if (m_decoder->imageCount < 2) { // not an animation
1043 if (imageNumber == 0) {
1044 if (ensureOpened()) {
1045 m_parseState = ParseAvifSuccess;
1046 return true;
1047 }
1048 }
1049 return false;
1050 }
1051
1052 if (imageNumber < 0 || imageNumber >= m_decoder->imageCount) { // wrong index
1053 return false;
1054 }
1055
1056 if (imageNumber == m_decoder->imageIndex) { // we are here already
1057 m_must_jump_to_next_image = false;
1058 m_parseState = ParseAvifSuccess;
1059 return true;
1060 }
1061
1062 avifResult decodeResult = avifDecoderNthImage(m_decoder, imageNumber);
1063
1064 if (decodeResult != AVIF_RESULT_OK) {
1065 qWarning("ERROR: Failed to decode %d th Image in sequence: %s", imageNumber, avifResultToString(decodeResult));
1066 m_parseState = ParseAvifError;
1067 return false;
1068 }
1069
1070 if ((m_container_width != m_decoder->image->width) || (m_container_height != m_decoder->image->height)) {
1071 qWarning("Decoded image sequence size (%dx%d) do not match declared container size (%dx%d)!",
1072 m_decoder->image->width,
1073 m_decoder->image->height,
1074 m_container_width,
1075 m_container_height);
1076
1077 m_parseState = ParseAvifError;
1078 return false;
1079 }
1080
1081 if (decode_one_frame()) {
1082 m_parseState = ParseAvifSuccess;
1083 return true;
1084 } else {
1085 m_parseState = ParseAvifError;
1086 return false;
1087 }
1088}
1089
1090int QAVIFHandler::nextImageDelay() const
1091{
1092 if (!ensureOpened()) {
1093 return 0;
1094 }
1095
1096 if (m_decoder->imageCount < 2) {
1097 return 0;
1098 }
1099
1100 int delay_ms = 1000.0 * m_decoder->imageTiming.duration;
1101 if (delay_ms < 1) {
1102 delay_ms = 1;
1103 }
1104 return delay_ms;
1105}
1106
1107int QAVIFHandler::loopCount() const
1108{
1109 if (!ensureParsed()) {
1110 return 0;
1111 }
1112
1113 if (m_decoder->imageCount < 2) {
1114 return 0;
1115 }
1116
1117#if AVIF_VERSION >= 1000000
1118 if (m_decoder->repetitionCount >= 0) {
1119 return m_decoder->repetitionCount;
1120 }
1121#endif
1122 // Endless loop to work around https://github.com/AOMediaCodec/libavif/issues/347
1123 return -1;
1124}
1125
1126QPointF QAVIFHandler::CompatibleChromacity(qreal chrX, qreal chrY)
1127{
1128 chrX = qBound(qreal(0.0), chrX, qreal(1.0));
1129 chrY = qBound(qreal(DBL_MIN), chrY, qreal(1.0));
1130
1131 if ((chrX + chrY) > qreal(1.0)) {
1132 chrX = qreal(1.0) - chrY;
1133 }
1134
1135 return QPointF(chrX, chrY);
1136}
1137
1138QImageIOPlugin::Capabilities QAVIFPlugin::capabilities(QIODevice *device, const QByteArray &format) const
1139{
1140 static const bool isAvifDecoderAvailable(avifCodecName(AVIF_CODEC_CHOICE_AUTO, AVIF_CODEC_FLAG_CAN_DECODE) != nullptr);
1141 static const bool isAvifEncoderAvailable(avifCodecName(AVIF_CODEC_CHOICE_AUTO, AVIF_CODEC_FLAG_CAN_ENCODE) != nullptr);
1142
1143 if (format == "avif") {
1144 Capabilities format_cap;
1145 if (isAvifDecoderAvailable) {
1146 format_cap |= CanRead;
1147 }
1148 if (isAvifEncoderAvailable) {
1149 format_cap |= CanWrite;
1150 }
1151 return format_cap;
1152 }
1153
1154 if (format == "avifs") {
1155 Capabilities format_cap;
1156 if (isAvifDecoderAvailable) {
1157 format_cap |= CanRead;
1158 }
1159 return format_cap;
1160 }
1161
1162 if (!format.isEmpty()) {
1163 return {};
1164 }
1165 if (!device->isOpen()) {
1166 return {};
1167 }
1168
1169 Capabilities cap;
1170 if (device->isReadable() && QAVIFHandler::canRead(device) && isAvifDecoderAvailable) {
1171 cap |= CanRead;
1172 }
1173 if (device->isWritable() && isAvifEncoderAvailable) {
1174 cap |= CanWrite;
1175 }
1176 return cap;
1177}
1178
1179QImageIOHandler *QAVIFPlugin::create(QIODevice *device, const QByteArray &format) const
1180{
1181 QImageIOHandler *handler = new QAVIFHandler;
1182 handler->setDevice(device);
1183 handler->setFormat(format);
1184 return handler;
1185}
1186
1187#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)
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)
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 Fri Oct 11 2024 12:15:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.