1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2007-02-03
 * Description : Loading parameters for multithreaded loading
 *
 * SPDX-FileCopyrightText: 2006-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
 * SPDX-FileCopyrightText: 2012-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "loadingdescription.h"

// Local includes

#include "icctransform.h"
#include "thumbnailinfo.h"
#include "thumbnailsize.h"

namespace Digikam
{

bool LoadingDescription::PreviewParameters::operator==(const PreviewParameters& other) const
{
    return (
            (type             == other.type)            &&
            (size             == other.size)            &&
            (flags            == other.flags)           &&
            (previewSettings  == other.previewSettings) &&
            (extraParameter   == other.extraParameter)  &&
            (storageReference == other.storageReference)
           );
}

bool LoadingDescription::PreviewParameters::onlyPregenerate() const
{
    return (flags & OnlyPregenerate);
}

bool LoadingDescription::PreviewParameters::onlyFromStorage() const
{
    return (flags & OnlyFromStorage);
}

bool LoadingDescription::PostProcessingParameters::operator==(const PostProcessingParameters& other) const
{
    return (colorManagement == other.colorManagement);
}

bool LoadingDescription::PostProcessingParameters::needsProcessing() const
{
    return (colorManagement != NoColorConversion);
}

void LoadingDescription::PostProcessingParameters::setTransform(const IccTransform& transform)<--- Shadow argument
{
    iccData = QVariant::fromValue<IccTransform>(transform);
}

bool LoadingDescription::PostProcessingParameters::hasTransform() const
{
    return (!iccData.isNull() && iccData.canConvert<IccTransform>());
}

IccTransform LoadingDescription::PostProcessingParameters::transform() const
{
    return iccData.value<IccTransform>();
}

void LoadingDescription::PostProcessingParameters::setProfile(const IccProfile& profile)<--- Shadow argument
{
    iccData = QVariant::fromValue<IccProfile>(profile);
}

bool LoadingDescription::PostProcessingParameters::hasProfile() const
{
    return (!iccData.isNull() && iccData.canConvert<IccProfile>());
}

IccProfile LoadingDescription::PostProcessingParameters::profile() const
{
    return iccData.value<IccProfile>();
}

// ----------------------------------------------------------------------------

LoadingDescription::LoadingDescription()
    : filePath                (QString()),
      rawDecodingSettings     (DRawDecoding()),
      previewParameters       (PreviewParameters()),
      postProcessingParameters(PostProcessingParameters())
{
}

LoadingDescription::LoadingDescription(const QString& filePath,
                                       ColorManagementSettings cm)
    : filePath           (filePath),
      rawDecodingSettings(DRawDecoding()),
      previewParameters  (PreviewParameters())
{
      postProcessingParameters.colorManagement = cm;
}

LoadingDescription::LoadingDescription(const QString& filePath,
                                       const DRawDecoding& settings,
                                       RawDecodingHint hint,
                                       ColorManagementSettings cm)
    : filePath           (filePath),
      rawDecodingSettings(settings),
      rawDecodingHint    (hint),
      previewParameters  (PreviewParameters())
{
      postProcessingParameters.colorManagement = cm;
}

LoadingDescription::LoadingDescription(const QString& filePath,
                                       const PreviewSettings& previewSettings,
                                       int size,
                                       ColorManagementSettings cm,
                                       LoadingDescription::PreviewParameters::PreviewType type)
    : filePath           (filePath),
      rawDecodingSettings(DRawDecoding())
{
    previewParameters.type                   = type;
    previewParameters.size                   = size;
    previewParameters.previewSettings        = previewSettings;
    postProcessingParameters.colorManagement = cm;
}

QString LoadingDescription::cacheKey() const
{
    // Here we have the knowledge which LoadingDescriptions / RawFileDecodingSettings
    // must be cached separately.

    // Thumbnail loading. This one is easy.

    if      (previewParameters.type == PreviewParameters::Thumbnail)
    {
        QString fileRef = filePath.isEmpty() ? (QLatin1String("id:/") + previewParameters.storageReference.toString())
                                             : filePath;

        return (fileRef + QLatin1String("-thumbnail-") + QString::number(previewParameters.size));
    }
    else if (previewParameters.type == PreviewParameters::DetailThumbnail)
    {
        QString fileRef    = filePath.isEmpty() ? (QLatin1String("id:/") + previewParameters.storageReference.toString())
                                                : filePath;
        QRect rect         =  previewParameters.extraParameter.toRect();
        QString rectString = QString::fromLatin1("%1,%2-%3x%4-")
                             .arg(rect.x())
                             .arg(rect.y())
                             .arg(rect.width())
                             .arg(rect.height());

        return (fileRef + QLatin1String("-thumbnail-") + rectString + QString::number(previewParameters.size));
    }

    // DImg loading

    if (previewParameters.type == PreviewParameters::NoPreview)
    {
        // Assumption: Full loading. For Raw images, we need to check all parameters here.
        // Non-raw images will always be loaded full-size.
        // NOTE: do not identify these by cache key only, check the settings!

        if      (rawDecodingHint == RawDecodingGlobalSettings)
        {
            return (filePath + QLatin1String("-globalraw"));
        }
        else if (rawDecodingHint == RawDecodingCustomSettings)
        {
            return (filePath + QLatin1String("-customraw"));
        }
    }
    else
    {
        // Assumption: Size-limited previews are always eight bit and do not care for raw settings.

        if (previewParameters.size)
        {
            return (filePath + QLatin1String("-previewImage-") + QString::number(previewParameters.size));
        }
        else
        {
            return (filePath + QLatin1String("-previewImage"));
        }
    }

    QString suffix;

    // Assumption: Time-optimized loading is used for previews and non-previews

    if (rawDecodingHint == RawDecodingTimeOptimized)
    {
        // Assumption: With time-optimized, we can have 8 or 16bit and halfSize or demosaiced.

        suffix += QLatin1String("-timeoptimized");

        if (!rawDecodingSettings.rawPrm.sixteenBitsImage)
        {
            suffix += QLatin1String("-8");
        }

        if (rawDecodingSettings.rawPrm.halfSizeColorImage)
        {
            suffix += QLatin1String("-halfSize");
        }
    }

    return (filePath + suffix);
}

QStringList LoadingDescription::lookupCacheKeys() const
{
    // Build a hierarchy which cache entries may be used for this LoadingDescription.

    // Thumbnail loading. No other cache key included!

    if ((previewParameters.type == PreviewParameters::Thumbnail) ||
        (previewParameters.type == PreviewParameters::DetailThumbnail))
    {
        return QStringList() << cacheKey();
    }

    // DImg loading.
    // Typically, the first is the best. An actual loading operation may use a
    // lower-quality loading and will effectively only add the last entry of the
    // list to the cache, although it can accept the first if already available.
    // Hierarchy:
    //  Raw with GlobalSettings and CustomSettings
    //  Raw with optimized loading, 8 or 16bit
    //      full size
    //      halfSize
    //  "Normal" image (default raw parameters)
    //  Preview image
    //      full size
    //      reduced size

    QStringList cacheKeys;

    if (previewParameters.type != PreviewParameters::NoPreview)
    {
        if (previewParameters.size)
        {
            cacheKeys << filePath + QLatin1String("-previewImage-") + QString::number(previewParameters.size);
        }

        // full size preview

        cacheKeys << filePath + QLatin1String("-previewImage");
    }

    if (rawDecodingHint == RawDecodingDefaultSettings)
    {
        cacheKeys << filePath;
    }

    if (rawDecodingHint == RawDecodingTimeOptimized)
    {
        if (rawDecodingSettings.rawPrm.sixteenBitsImage)
        {
            cacheKeys << filePath + QLatin1String("-timeoptimized");

            if (rawDecodingSettings.rawPrm.halfSizeColorImage)
            {
                cacheKeys << filePath + QLatin1String("-timeoptimized-halfSize");
            }
        }
        else
        {
            cacheKeys << filePath + QLatin1String("-timeoptimized-8");

            if (rawDecodingSettings.rawPrm.halfSizeColorImage)
            {
                cacheKeys << filePath + QLatin1String("-timeoptimized-8-halfSize");
            }
        }
    }

    if      (rawDecodingHint == RawDecodingGlobalSettings)
    {
        cacheKeys << filePath + QLatin1String("-globalraw");
    }
    else if (rawDecodingHint == RawDecodingCustomSettings)
    {
        cacheKeys << filePath + QLatin1String("-customraw");
    }

    return cacheKeys;
}

bool LoadingDescription::needCheckRawDecoding() const
{
    return ((rawDecodingHint == RawDecodingGlobalSettings) ||
            (rawDecodingHint == RawDecodingCustomSettings));
}

bool LoadingDescription::isReducedVersion() const
{
    // return true if this loads anything but the full version

    return (rawDecodingSettings.rawPrm.halfSizeColorImage ||
            (previewParameters.type != PreviewParameters::NoPreview));
}

bool LoadingDescription::operator==(const LoadingDescription& other) const
{
    return ((filePath                 == other.filePath)                   &&
            (rawDecodingSettings      == other.rawDecodingSettings)        &&
            (previewParameters        == other.previewParameters)          &&
            (postProcessingParameters == other.postProcessingParameters));
}

bool LoadingDescription::operator!=(const LoadingDescription& other) const
{
    return (!operator==(other));
}

bool LoadingDescription::equalsIgnoreReducedVersion(const LoadingDescription& other) const
{
    return (filePath == other.filePath);
}

bool LoadingDescription::equalsOrBetterThan(const LoadingDescription& other) const
{
    // This method is similar to operator==. But it returns true as well if this
    // loads a "better" version than <other>.
    // Preview parameters must have the same size, or other has no size restriction.
    // Comparing raw decoding settings is complicated. We allow to be loaded with optimizeTimeLoading().

    DRawDecoding fast = rawDecodingSettings;
    fast.optimizeTimeLoading();

    return (
            (filePath == other.filePath) &&
            (
                (rawDecodingSettings == other.rawDecodingSettings) ||
                (fast                == other.rawDecodingSettings)
            ) &&
            (
                (previewParameters.size == other.previewParameters.size) ||
                other.previewParameters.size
            )
           );
}

bool LoadingDescription::isThumbnail() const
{
    return ((previewParameters.type == PreviewParameters::Thumbnail) ||
            (previewParameters.type == PreviewParameters::DetailThumbnail));
}

bool LoadingDescription::isPreviewImage() const
{
    return (previewParameters.type == PreviewParameters::PreviewImage);
}

ThumbnailIdentifier LoadingDescription::thumbnailIdentifier() const
{
    ThumbnailIdentifier id;

    if (!isThumbnail())
    {
        return id;
    }

    id.filePath = filePath;
    id.id       = previewParameters.storageReference.toLongLong();

    return id;
}

QStringList LoadingDescription::possibleCacheKeys(const QString& filePath)
{
    QStringList keys;
    keys << filePath ;
    keys << filePath + QLatin1String("-timeoptimized-8-halfSize");
    keys << filePath + QLatin1String("-timeoptimized-8");
    keys << filePath + QLatin1String("-timeoptimized-halfSize");
    keys << filePath + QLatin1String("-timeoptimized");
    keys << filePath + QLatin1String("-customraw");
    keys << filePath + QLatin1String("-globalraw");

    for (int i = 1 ; i <= ThumbnailSize::HD ; ++i)
    {
        keys << filePath + QLatin1String("-previewImage-") + QString::number(i);
    }

    return keys;
}

QStringList LoadingDescription::possibleThumbnailCacheKeys(const QString& filePath)
{
    // FIXME: With details, there is an endless number of possible cache keys. Need different approach.

    QStringList keys;

    // there are (ThumbnailSize::HD) possible keys...

    QString path = filePath + QLatin1String("-thumbnail-");

    for (int i = 1 ; i <= ThumbnailSize::HD ; ++i)
    {
        keys << path + QString::number(i);
    }

    return keys;
}

} // namespace Digikam