File indexing completed on 2024-05-19 04:26:52

0001 /*
0002  * SPDX-FileCopyrightText: 2022 L. E. Segovia <amy@amyspark.me>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #ifndef INTEGRAL_FRAME_DURATION_H
0008 #define INTEGRAL_FRAME_DURATION_H
0009 
0010 #include <cmath>
0011 
0012 #include <klocalizedstring.h>
0013 
0014 #include <KoID.h>
0015 #include <kis_image.h>
0016 #include <kis_image_animation_interface.h>
0017 
0018 #include "KisExportCheckRegistry.h"
0019 #include "kritaimpex_export.h"
0020 
0021 class KRITAIMPEX_EXPORT IntegralFrameDurationCheck : public KisExportCheckBase
0022 {
0023 public:
0024     IntegralFrameDurationCheck(const QString &id,
0025                                Level level,
0026                                const QString &customWarning = QString())
0027         : KisExportCheckBase(id, level, customWarning)
0028     {
0029         if (customWarning.isEmpty()) {
0030             m_warning =
0031                 i18nc("image conversion warning",
0032                       "The image is animated with a frame duration in "
0033                       "<b>fractions of a millisecond</b>. The "
0034                       "format cannot represent this, and the frame "
0035                       "duration will be rounded to the nearest millisecond.");
0036         }
0037     }
0038 
0039     bool checkNeeded(KisImageSP image) const override
0040     {
0041         if (!image->animationInterface()->hasAnimation()) {
0042             return false;
0043         }
0044 
0045         const auto frameDuration = 1000.0
0046             / static_cast<double>(image->animationInterface()->framerate());
0047 
0048         return std::round(frameDuration) != frameDuration;
0049     }
0050 
0051     Level check(KisImageSP /*image*/) const override
0052     {
0053         return m_level;
0054     }
0055 };
0056 
0057 class KRITAIMPEX_EXPORT IntegralFrameDurationCheckFactory
0058     : public KisExportCheckFactory
0059 {
0060 public:
0061     IntegralFrameDurationCheckFactory() = default;
0062 
0063     ~IntegralFrameDurationCheckFactory() override = default;
0064 
0065     KisExportCheckBase *
0066     create(KisExportCheckBase::Level level,
0067            const QString &customWarning = QString()) override
0068     {
0069         return new IntegralFrameDurationCheck(id(), level, customWarning);
0070     }
0071 
0072     QString id() const override
0073     {
0074         return "IntegralFrameDurationCheck";
0075     }
0076 };
0077 
0078 #endif // INTEGRAL_FRAME_DURATION_H