File indexing completed on 2024-05-05 04:05:48

0001 /*
0002     SPDX-FileCopyrightText: 2009 Stefan Majewsky <majewsky@gmx.net>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "slicerpropertyset.h"
0008 #include "slicer.h"
0009 #include "slicerjob.h"
0010 #include "slicerproperty.h"
0011 
0012 #include <cmath>
0013 #include "libpala_debug.h"
0014 #include <KLocalizedString>
0015 
0016 //BEGIN Private classes
0017 
0018 class Pala::SlicerPropertySetPrivate
0019 {
0020 public:
0021     explicit SlicerPropertySetPrivate(Pala::Slicer* slicer)
0022         : m_slicer(slicer)
0023     {
0024     }
0025 
0026     Pala::Slicer* const m_slicer;
0027 };
0028 
0029 class Pala::SimpleGridPropertySetPrivate : public Pala::SlicerPropertySetPrivate
0030 {
0031 public:
0032     explicit SimpleGridPropertySetPrivate(Pala::Slicer* slicer)
0033         : SlicerPropertySetPrivate(slicer)
0034     {
0035     }
0036 };
0037 
0038 //END Private classes
0039 
0040 //BEGIN Pala::SlicerPropertySet
0041 
0042 Pala::SlicerPropertySet::SlicerPropertySet(Pala::Slicer* slicer)
0043     : SlicerPropertySet(*new Pala::SlicerPropertySetPrivate(slicer))
0044 {
0045 }
0046 
0047 Pala::SlicerPropertySet::SlicerPropertySet(SlicerPropertySetPrivate& dd)
0048     : d_ptr(&dd)
0049 {
0050 }
0051 
0052 Pala::SlicerPropertySet::~SlicerPropertySet() = default;
0053 
0054 Pala::Slicer* Pala::SlicerPropertySet::slicer() const
0055 {
0056     Q_D(const SlicerPropertySet);
0057     return d->m_slicer;
0058 }
0059 
0060 void Pala::SlicerPropertySet::addPropertyToSlicer(const QByteArray& key, Pala::SlicerProperty* property)
0061 {
0062     Q_D(SlicerPropertySet);
0063     d->m_slicer->addProperty(key, property);
0064 }
0065 
0066 //END Pala::SlicerPropertySet
0067 
0068 //BEGIN Pala::SimpleGridPropertySet
0069 
0070 Pala::SimpleGridPropertySet::SimpleGridPropertySet(Pala::Slicer* slicer)
0071     : Pala::SlicerPropertySet(*new SimpleGridPropertySetPrivate(slicer))
0072 {
0073     Pala::IntegerProperty* prop;
0074     prop = new Pala::IntegerProperty(i18n("Piece count"));
0075     prop->setRange(4, 10000);
0076     prop->setDefaultValue(100);
0077     addPropertyToSlicer("PieceCount", prop);
0078     prop = new Pala::IntegerProperty(i18n("Piece aspect ratio"));
0079     prop->setRange(0, 10);
0080     prop->setDefaultValue(5);
0081     prop->setRepresentation(Pala::IntegerProperty::Slider);
0082     addPropertyToSlicer("PieceAspect", prop);
0083 }
0084 
0085 Pala::SimpleGridPropertySet::~SimpleGridPropertySet() = default;
0086 
0087 QSize Pala::SimpleGridPropertySet::pieceCount(Pala::SlicerJob* job) const
0088 {
0089     const qreal imageAspect = qreal(job->image().width()) / qreal(job->image().height());
0090     const qreal pieceAspect = pow(2.0, qreal(job->argument("PieceAspect").toInt() - 5) * 0.2);
0091     const int count = job->argument("PieceCount").toInt();
0092 
0093     QSize bestCount(10, 10);
0094     qreal bestQ = 1.0e100;
0095 
0096     qreal aspect = imageAspect / pieceAspect;
0097     int maxX = ceil(sqrt(qreal(count) * aspect)) + 5;
0098     int maxY = ceil(sqrt(qreal(count) / aspect)) + 5;
0099     qCDebug(PALAPELI_LIBPALA_LOG) << "Determining counts for total count" << count;
0100     qCDebug(PALAPELI_LIBPALA_LOG) << "  Piece aspect ratio is" << pieceAspect;
0101     qCDebug(PALAPELI_LIBPALA_LOG) << "  Image aspect is" << imageAspect;
0102     qCDebug(PALAPELI_LIBPALA_LOG) << "  Target count aspect is" << aspect;
0103     qCDebug(PALAPELI_LIBPALA_LOG) << " Will try x <" << maxX << ", y <" << maxY;
0104 
0105     for (int x = 1; x < maxX; ++x)
0106     {
0107         for (int y = 1; y < maxY; ++y)
0108         {
0109             const int c = x * y;
0110             const qreal xa = x / aspect;
0111             const qreal a = pow((xa > y ? xa / qreal(y) : qreal(y) / xa) - 1.0, 2.0) * 20.0;
0112             const qreal dev = pow(6.0 * (count - c) / count, 2.0);
0113             const qreal q = (a + 1.0) * (dev + 1.0);
0114 
0115             if (q < bestQ)
0116             {
0117                 bestQ = q;
0118                 bestCount = QSize(x, y);
0119             }
0120         }
0121     }
0122     qCDebug(PALAPELI_LIBPALA_LOG) << "We liked " << bestCount << " ( at quality" << bestQ << ")";
0123     return bestCount;
0124 }
0125 
0126 //END Pala::SimpleGridPropertySet