| 1 |
/* ============================================================ |
| 2 |
* |
| 3 |
* This file is a part of digiKam project |
| 4 |
* https://www.digikam.org |
| 5 |
* |
| 6 |
* Date : 2007-07-20 |
| 7 |
* Description : Loader for thumbnails |
| 8 |
* |
| 9 |
* SPDX-FileCopyrightText: 2003-2005 by Renchi Raju <renchi dot raju at gmail dot com> |
| 10 |
* SPDX-FileCopyrightText: 2003-2026 by Gilles Caulier <caulier dot gilles at gmail dot com> |
| 11 |
* SPDX-FileCopyrightText: 2006-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de> |
| 12 |
* |
| 13 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 14 |
* |
| 15 |
* ============================================================ */ |
| 16 |
|
| 17 |
// C++ includes |
| 18 |
|
| 19 |
#include <cstdlib> |
| 20 |
#include <cstdio> |
| 21 |
#include <cstring> |
| 22 |
|
| 23 |
// Qt includes |
| 24 |
|
| 25 |
#include <QImage> |
| 26 |
#include <QDir> |
| 27 |
#include <QStandardPaths> |
| 28 |
#include <QCryptographicHash> |
| 29 |
#include <QUrl> |
| 30 |
|
| 31 |
// C ANSI includes |
| 32 |
|
| 33 |
extern "C" |
| 34 |
{ |
| 35 |
#ifndef Q_CC_MSVC |
| 36 |
# include <unistd.h> |
| 37 |
#endif |
| 38 |
#include <sys/stat.h> |
| 39 |
#include <sys/types.h> |
| 40 |
|
| 41 |
#ifndef Q_OS_WIN32 // krazy:exclude=cpp |
| 42 |
# include <sys/ipc.h> |
| 43 |
# include <sys/shm.h> |
| 44 |
#endif |
| 45 |
|
| 46 |
#include <time.h> |
| 47 |
#include <png.h> |
| 48 |
} |
| 49 |
|
| 50 |
// Local includes |
| 51 |
|
| 52 |
#include "thumbnailcreator_p.h" |
| 53 |
|
| 54 |
// Definitions |
| 55 |
|
| 56 |
#define PNG_BYTES_TO_CHECK 4 |
| 57 |
|
| 58 |
namespace Digikam |
| 59 |
{ |
| 60 |
|
| 61 |
// --- Static methods: Generate the thumbnail path according to FreeDesktop spec --- |
| 62 |
|
| 63 |
QString ThumbnailCreator::normalThumbnailDir() |
| 64 |
{ |
| 65 |
return (QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + |
| 66 |
QLatin1String("/thumbnails/normal/")); |
| 67 |
} |
| 68 |
|
| 69 |
QString ThumbnailCreator::largeThumbnailDir() |
| 70 |
{ |
| 71 |
return (QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + |
| 72 |
QLatin1String("/thumbnails/large/")); |
| 73 |
} |
| 74 |
|
| 75 |
QString ThumbnailCreator::thumbnailPath(const QString& filePath, const QString& basePath) |
| 76 |
{ |
| 77 |
return thumbnailPathFromUri(thumbnailUri(filePath), basePath); |
| 78 |
} |
| 79 |
|
| 80 |
QString ThumbnailCreator::thumbnailUri(const QString& filePath) |
| 81 |
{ |
| 82 |
return QUrl::fromLocalFile(filePath).url(); |
| 83 |
} |
| 84 |
|
| 85 |
QString ThumbnailCreator::thumbnailPathFromUri(const QString& uri, const QString& basePath) |
| 86 |
{ |
| 87 |
QCryptographicHash md5(QCryptographicHash::Md5); |
| 88 |
md5.addData(QFile::encodeName(uri).constData()); |
| 89 |
|
| 90 |
return (basePath + |
| 91 |
QString::fromUtf8(QFile::encodeName(QString::fromUtf8(md5.result().toHex()))) + |
| 92 |
QLatin1String(".png")); |
| 93 |
} |
| 94 |
|
| 95 |
// --- non-static methods --- |
| 96 |
|
| 97 |
void ThumbnailCreator::initThumbnailDirs() |
| 98 |
{ |
| 99 |
d->smallThumbPath = normalThumbnailDir(); |
| 100 |
d->bigThumbPath = largeThumbnailDir(); |
| 101 |
|
| 102 |
if (!QDir(d->smallThumbPath).exists()) |
| 103 |
{ |
| 104 |
if (QDir().mkpath(d->smallThumbPath)) |
| 105 |
{ |
| 106 |
QFile f(d->smallThumbPath); |
| 107 |
f.setPermissions(QFile::ReadUser | QFile::WriteUser | QFile::ExeUser); // 0700 |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
if (!QDir(d->bigThumbPath).exists()) |
| 112 |
{ |
| 113 |
if (QDir().mkpath(d->bigThumbPath)) |
| 114 |
{ |
| 115 |
QFile f(d->bigThumbPath); |
| 116 |
f.setPermissions(QFile::ReadUser | QFile::WriteUser | QFile::ExeUser); // 0700 |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
QString ThumbnailCreator::thumbnailPath(const QString& filePath) const |
| 122 |
{ |
| 123 |
QString basePath = (d->storageSize() == 128) ? d->smallThumbPath : d->bigThumbPath; |
| 124 |
|
| 125 |
return thumbnailPath(filePath, basePath); |
| 126 |
} |
| 127 |
|
| 128 |
// --- Basic PNG loading --- |
| 129 |
|
| 130 |
QImage ThumbnailCreator::loadPNG(const QString& path) const |
| 131 |
{ |
| 132 |
png_uint_32 w32, h32; |
| 133 |
int w, h; |
| 134 |
bool has_alpha = 0; |
| 135 |
png_structp png_ptr = nullptr; |
| 136 |
png_infop info_ptr = nullptr; |
| 137 |
int bit_depth, color_type, interlace_type; |
| 138 |
QImage qimage; |
| 139 |
|
| 140 |
#ifdef Q_OS_WIN |
| 141 |
|
| 142 |
FILE* const f = _wfopen((const wchar_t*)path.utf16(), L"rb"); |
| 143 |
|
| 144 |
#else |
| 145 |
|
| 146 |
FILE* const f = fopen(path.toUtf8().constData(), "rb"); |
| 147 |
|
| 148 |
#endif |
| 149 |
|
| 150 |
if (!f) |
| 151 |
{ |
| 152 |
return qimage; |
| 153 |
} |
| 154 |
|
| 155 |
unsigned char buf[PNG_BYTES_TO_CHECK]; |
| 156 |
|
| 157 |
size_t itemsRead = fread(buf, 1, PNG_BYTES_TO_CHECK, f); |
| 158 |
|
| 159 |
#if PNG_LIBPNG_VER >= 10400 |
| 160 |
|
| 161 |
if ((itemsRead != PNG_BYTES_TO_CHECK) || png_sig_cmp(buf, 0, PNG_BYTES_TO_CHECK)) |
| 162 |
|
| 163 |
#else |
| 164 |
|
| 165 |
if ((itemsRead != PNG_BYTES_TO_CHECK) || !png_check_sig(buf, PNG_BYTES_TO_CHECK)) |
| 166 |
|
| 167 |
#endif |
| 168 |
{ |
| 169 |
fclose(f); |
| 170 |
|
| 171 |
return qimage; |
| 172 |
} |
| 173 |
|
| 174 |
rewind(f); |
| 175 |
|
| 176 |
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); |
| 177 |
|
| 178 |
if (!png_ptr) |
| 179 |
{ |
| 180 |
fclose(f); |
| 181 |
|
| 182 |
return qimage; |
| 183 |
} |
| 184 |
|
| 185 |
info_ptr = png_create_info_struct(png_ptr); |
| 186 |
|
| 187 |
if (!info_ptr) |
| 188 |
{ |
| 189 |
png_destroy_read_struct(&png_ptr, nullptr, nullptr); |
| 190 |
fclose(f); |
| 191 |
|
| 192 |
return qimage; |
| 193 |
} |
| 194 |
|
| 195 |
#if PNG_LIBPNG_VER >= 10400 |
| 196 |
|
| 197 |
if (setjmp(png_jmpbuf(png_ptr))) |
| 198 |
|
| 199 |
#else |
| 200 |
|
| 201 |
if (setjmp(png_ptr->jmpbuf)) |
| 202 |
|
| 203 |
#endif |
| 204 |
{ |
| 205 |
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr); |
| 206 |
fclose(f); |
| 207 |
|
| 208 |
return qimage; |
| 209 |
} |
| 210 |
|
| 211 |
png_init_io(png_ptr, f); |
| 212 |
png_read_info(png_ptr, info_ptr); |
| 213 |
png_get_IHDR(png_ptr, info_ptr, |
| 214 |
reinterpret_cast<png_uint_32*>(&w32), |
| 215 |
reinterpret_cast<png_uint_32*>(&h32), |
| 216 |
&bit_depth, |
| 217 |
&color_type, |
| 218 |
&interlace_type, |
| 219 |
nullptr, |
| 220 |
nullptr); |
| 221 |
|
| 222 |
bool has_grey = 0; |
| 223 |
w = w32; |
| 224 |
h = h32; |
| 225 |
qimage = QImage(w, h, QImage::Format_ARGB32); |
| 226 |
|
| 227 |
if (color_type == PNG_COLOR_TYPE_PALETTE) |
| 228 |
{ |
| 229 |
png_set_expand(png_ptr); |
| 230 |
} |
| 231 |
|
| 232 |
#if PNG_LIBPNG_VER >= 10400 |
| 233 |
|
| 234 |
png_byte info_color_type = png_get_color_type(png_ptr, info_ptr); |
| 235 |
|
| 236 |
#else |
| 237 |
|
| 238 |
png_byte info_color_type = info_ptr->color_type; |
| 239 |
|
| 240 |
#endif |
| 241 |
|
| 242 |
if (info_color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 243 |
{ |
| 244 |
has_alpha = 1; |
| 245 |
} |
| 246 |
|
| 247 |
if (info_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 248 |
{ |
| 249 |
has_alpha = 1; |
| 250 |
has_grey = 1; |
| 251 |
} |
| 252 |
|
| 253 |
if (info_color_type == PNG_COLOR_TYPE_GRAY) |
| 254 |
{ |
| 255 |
has_grey = 1; |
| 256 |
} |
| 257 |
|
| 258 |
unsigned char** lines = nullptr; |
| 259 |
int i; |
| 260 |
|
| 261 |
if (has_alpha) |
| 262 |
{ |
| 263 |
png_set_expand(png_ptr); |
| 264 |
} |
| 265 |
|
| 266 |
if (QSysInfo::ByteOrder == QSysInfo::LittleEndian) // Intel |
| 267 |
{ |
| 268 |
png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER); |
| 269 |
png_set_bgr(png_ptr); |
| 270 |
} |
| 271 |
else // PPC |
| 272 |
{ |
| 273 |
png_set_swap_alpha(png_ptr); |
| 274 |
png_set_filler(png_ptr, 0xff, PNG_FILLER_BEFORE); |
| 275 |
} |
| 276 |
|
| 277 |
// 16bit color -> 8bit color |
| 278 |
|
| 279 |
if (bit_depth == 16) |
| 280 |
{ |
| 281 |
png_set_strip_16(png_ptr); |
| 282 |
} |
| 283 |
|
| 284 |
// pack all pixels to byte boundaries |
| 285 |
|
| 286 |
png_set_packing(png_ptr); |
| 287 |
|
| 288 |
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) |
| 289 |
{ |
| 290 |
png_set_expand(png_ptr); |
| 291 |
} |
| 292 |
|
| 293 |
lines = reinterpret_cast<unsigned char**>(malloc(h * sizeof(unsigned char*))); |
| 294 |
|
| 295 |
if (!lines) |
| 296 |
{ |
| 297 |
png_read_end(png_ptr, info_ptr); |
| 298 |
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) nullptr); |
| 299 |
fclose(f); |
| 300 |
|
| 301 |
return qimage; |
| 302 |
} |
| 303 |
|
| 304 |
if (has_grey) |
| 305 |
{ |
| 306 |
png_set_gray_to_rgb(png_ptr); |
| 307 |
|
| 308 |
if (png_get_bit_depth(png_ptr, info_ptr) < 8) |
| 309 |
|
| 310 |
#if PNG_LIBPNG_VER >= 10400 |
| 311 |
|
| 312 |
png_set_expand_gray_1_2_4_to_8(png_ptr); |
| 313 |
#else |
| 314 |
|
| 315 |
png_set_gray_1_2_4_to_8(png_ptr); |
| 316 |
|
| 317 |
#endif |
| 318 |
|
| 319 |
} |
| 320 |
|
| 321 |
int sizeOfUint = sizeof(unsigned int); |
| 322 |
|
| 323 |
for (i = 0 ; i < h ; ++i) |
| 324 |
{ |
| 325 |
lines[i] = (reinterpret_cast<unsigned char*>(qimage.bits())) + (i * w * sizeOfUint); |
| 326 |
} |
| 327 |
|
| 328 |
png_read_image(png_ptr, lines); |
| 329 |
free(lines); |
| 330 |
|
| 331 |
png_textp text_ptr; |
| 332 |
int num_text = 0; |
| 333 |
png_get_text(png_ptr,info_ptr, &text_ptr, &num_text); |
| 334 |
|
| 335 |
while (num_text--) |
| 336 |
{ |
| 337 |
qimage.setText(QString::fromUtf8(text_ptr->key), QString::fromUtf8(text_ptr->text)); |
| 338 |
++text_ptr; |
| 339 |
} |
| 340 |
|
| 341 |
png_read_end(png_ptr, info_ptr); |
| 342 |
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) nullptr); |
| 343 |
fclose(f); |
| 344 |
|
| 345 |
return qimage; |
| 346 |
} |
| 347 |
|
| 348 |
} // namespace Digikam |