Debian Codegraph @

src/util/dimagehandler.cpp

raw
1 // SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "dimagehandler.h"
6 #include "private/dimagehandlerlibs_p.h"
7
8 #include <DObjectPrivate>
9
10 #include <QSet>
11 #include <QHash>
12 #include <QFileInfo>
13 #include <QImageReader>
14 #include <QPainter>
15
16 #include <omp.h>
17 #include <cmath>
18
19 #define SAVE_QUAITY_VALUE 100
20
21 struct SupportFormats
22 {
23 QHash<QString, int> freeImageFormats;
24 QHash<QString, int> movieFormats;
25 QStringList libRawFormats;
26 QStringList qtSupportFormats;
27 QStringList saveableFormats;
28 QStringList qtRotatableFormats;
29
30 QStringList supportFormats;
31
32 SupportFormats();
33 };
34
35 Q_GLOBAL_STATIC(SupportFormats, SupportFormatsInstance)
36 #ifndef DTK_DISABLE_EX_IMAGE_FORMAT
37 Q_GLOBAL_STATIC(DLibFreeImage, DLibFreeImageInstance)
38 Q_GLOBAL_STATIC(DLibRaw, DLibRawInstance)
39 #endif
40
41 SupportFormats::SupportFormats()
42 {
43 #ifndef DTK_DISABLE_EX_IMAGE_FORMAT
44 // LibFreeImage support formats
45 freeImageFormats["BMP"] = FIF_BMP;
46 freeImageFormats["ICO"] = FIF_ICO;
47 freeImageFormats["JPG"] = FIF_JPEG;
48 freeImageFormats["JPE"] = FIF_JPEG;
49 freeImageFormats["JPS"] = FIF_JPEG;
50 freeImageFormats["JPEG"] = FIF_JPEG;
51 freeImageFormats["JNG"] = FIF_JNG;
52 freeImageFormats["KOALA"] = FIF_KOALA;
53 freeImageFormats["KOA"] = FIF_KOALA;
54 freeImageFormats["LBM"] = FIF_LBM;
55 freeImageFormats["IFF"] = FIF_LBM;
56 freeImageFormats["MNG"] = FIF_MNG;
57 freeImageFormats["PBM"] = FIF_PBM;
58 freeImageFormats["PBMRAW"] = FIF_PBMRAW;
59 freeImageFormats["PCD"] = FIF_PCD;
60 freeImageFormats["PCX"] = FIF_PCX;
61 freeImageFormats["PGM"] = FIF_PGM;
62 freeImageFormats["PGMRAW"] = FIF_PGMRAW;
63 freeImageFormats["PNG"] = FIF_PNG;
64 freeImageFormats["PPM"] = FIF_PPM;
65 freeImageFormats["PPMRAW"] = FIF_PPMRAW;
66 freeImageFormats["RAS"] = FIF_RAS;
67 freeImageFormats["TGA"] = FIF_TARGA;
68 freeImageFormats["TARGA"] = FIF_TARGA;
69 freeImageFormats["TIFF"] = FIF_TIFF;
70 freeImageFormats["TIF"] = FIF_TIFF;
71 freeImageFormats["WBMP"] = FIF_WBMP;
72 freeImageFormats["PSD"] = FIF_PSD;
73 freeImageFormats["CUT"] = FIF_CUT;
74 freeImageFormats["XBM"] = FIF_XBM;
75 freeImageFormats["XPM"] = FIF_XPM;
76 freeImageFormats["DDS"] = FIF_DDS;
77 freeImageFormats["GIF"] = FIF_GIF;
78 // Warning: Build fails in arch linux.
79 // In libfreeimage.h from arch linux freeimage3.18.0-15 package:
80 // The G3 fax format plugin is deliberately disabled in the Fedora build of
81 // FreeImage as it requires that FreeImage uses a private copy of libtiff
82 // which is a no no because of security reasons.
83 # if 0
84 freeImageFormats["FAX"] = FIF_FAXG3;
85 freeImageFormats["G3"] = FIF_FAXG3;
86 # endif
87 freeImageFormats["SGI"] = FIF_SGI;
88 freeImageFormats["EXR"] = FIF_EXR;
89 freeImageFormats["PCT"] = FIF_PICT;
90 freeImageFormats["PIC"] = FIF_PICT;
91 freeImageFormats["PICT"] = FIF_PICT;
92 freeImageFormats["PIC"] = FIF_PICT;
93 freeImageFormats["WEBP"] = FIF_WEBP;
94 freeImageFormats["JXR"] = FIF_JXR;
95 movieFormats["MNG"] = FIF_MNG;
96 movieFormats["GIF"] = FIF_GIF;
97 movieFormats["WEBP"] = FIF_WEBP;
98
99 // LibRaw support images
100 libRawFormats << "CR2"
101 << "CRW"
102 << "DCR"
103 << "KDC"
104 << "MRW"
105 << "NEF"
106 << "ORF"
107 << "PEF"
108 << "RAF"
109 << "SRF"
110 << "DNG"
111 << "RAW";
112 #endif
113
114 // For some formats, Qt will support after loading image plugins.
115 QList<QByteArray> qtReadableFormats = QImageReader::supportedImageFormats();
116 for (QByteArray &format : qtReadableFormats) {
117 qtSupportFormats << QString::fromUtf8(format).toUpper();
118 }
119 // Qt support formats internal.
120 qtSupportFormats << "PNM"
121 << "MEF"
122 << "PXM";
123
124 saveableFormats << "BMP"
125 << "JPG"
126 << "JPEG"
127 << "JPS"
128 << "JPE"
129 << "PNG"
130 << "PGM"
131 << "PPM"
132 << "PNM"
133 << "TGA"
134 << "XPM"
135 << "ICO"
136 << "JNG"
137 #ifndef ISSW_64
138 << "WBMP"
139 #endif
140 << "RAS";
141
142 // For those formats, Qt process faster than FreeImage.
143 qtRotatableFormats << "ICNS"
144 << "JPG"
145 << "JPEG"
146 << "PNG"
147 << "BMP";
148
149 QSet<QString> formats;
150
151 #ifndef DTK_DISABLE_EX_IMAGE_FORMAT
152 // Check libFreeImage load succeeded.
153 if (DLibFreeImageInstance()->isValid()) {
154 // Use FreeImage load .pcx image.
155 qtSupportFormats.removeAll("PCX");
156
157 for (auto itr = freeImageFormats.begin(); itr != freeImageFormats.end(); ++itr) {
158 formats.insert(itr.key());
159 }
160
161 for (auto itr = movieFormats.begin(); itr != movieFormats.end(); ++itr) {
162 formats.insert(itr.key());
163 }
164 }
165
166 // Add libRaw support formats.
167 if (DLibRawInstance()->isValid()) {
168 for (const QString &format : libRawFormats) {
169 formats.insert(format);
170 }
171 }
172 #endif
173
174 if (formats.isEmpty()) {
175 supportFormats = qtSupportFormats;
176 } else {
177 // Add qt default support formats.
178 for (const QString &format : qtSupportFormats) {
179 formats.insert(format);
180 }
181
182 supportFormats = formats.values();
183 }
184 }
185
186 DCORE_USE_NAMESPACE
187 DGUI_BEGIN_NAMESPACE
188
189 #ifndef DTK_DISABLE_EX_IMAGE_FORMAT
190 QString detectImageFormatInternal(const QString &fileName, FREE_IMAGE_FORMAT &format);
191 #else
192 QString detectImageFormatInternal(const QString &fileName);
193 #endif
194
195 class DImageHandlerPrivate : public DObjectPrivate
196 {
197 D_DECLARE_PUBLIC(DImageHandler)
198 public:
199 enum ImageOption { Readable = 0x1, Wirteable = 0x2, Rotatable = 0x4, SupportFreeImage = 0x8 };
200 Q_DECLARE_FLAGS(ImageOptions, ImageOption);
201
202 explicit DImageHandlerPrivate(DImageHandler *qq);
203
204 bool formatReadable(const QString &fileFormat) const;
205 bool formatWriteable(const QString &fileFormat) const;
206
207 bool loadStaticImageFromFile(const QString &fileName, QImage &image);
208 #ifndef DTK_DISABLE_EX_IMAGE_FORMAT
209 bool loadImageWithFreeImage(const QString &fileName, QImage &image, FREE_IMAGE_FORMAT fifForamt, QString fileFormat);
210 #endif
211 bool rotateImageFile(const QString &fileName, int angle);
212 bool rotateImage(QImage &image, int angle);
213
214 void adjustImageToRealOrientation(QImage &image, ExifImageOrientation orientation);
215
216 QString fileName;
217 ImageOptions options;
218 QImage cachedImage;
219 QString cachedFormat;
220 QString lastError;
221 };
222 Q_DECLARE_OPERATORS_FOR_FLAGS(DImageHandlerPrivate::ImageOptions)
223
224 DImageHandlerPrivate::DImageHandlerPrivate(DImageHandler *qq)
225 : DObjectPrivate(qq)
226 {
227 }
228
229 bool DImageHandlerPrivate::formatReadable(const QString &fileFormat) const
230 {
231 if (fileFormat.isEmpty()) {
232 return false;
233 }
234
235 #ifndef DTK_DISABLE_EX_IMAGE_FORMAT
236 if (DLibFreeImageInstance()->isValid()) {
237 if (SupportFormatsInstance()->supportFormats.contains(fileFormat)) {
238 return true;
239 }
240 }
241 #endif
242
243 return SupportFormatsInstance()->supportFormats.contains(fileFormat);
244 }
245
246 bool DImageHandlerPrivate::formatWriteable(const QString &fileFormat) const
247 {
248 if (fileFormat.isEmpty()) {
249 return false;
250 }
251
252 #ifndef DTK_DISABLE_EX_IMAGE_FORMAT
253 if (DLibFreeImageInstance()->isValid()) {
254 if (SupportFormatsInstance()->saveableFormats.contains(fileFormat)) {
255 return true;
256 }
257 }
258 #endif
259
260 return SupportFormatsInstance()->saveableFormats.contains(fileFormat);
261 }
262
263 bool DImageHandlerPrivate::loadStaticImageFromFile(const QString &fileName, QImage &image)
264 {
265 D_Q(DImageHandler);
266
267 QFileInfo fileInfo(fileName);
268 if (0 == fileInfo.size()) {
269 lastError = QString("Error file!");
270 return false;
271 }
272
273 #ifndef DTK_DISABLE_EX_IMAGE_FORMAT
274 FREE_IMAGE_FORMAT fifFormat = FIF_UNKNOWN;
275 QString fileFormat = detectImageFormatInternal(fileName, fifFormat);
276 if (DLibFreeImageInstance()->isValid()) {
277 // Using free image meta type.
278 QHash<QString, QString> dataMap = DLibFreeImageInstance()->findAllMetaData(fileName);
279 fileFormat = dataMap.value("FileFormat").toUpper();
280 }
281
282 // For some formats, need use Qt image reader load, to aviod some errors on different hardware architectures.
283 bool usingQImage = ((FIF_PICT == fifFormat) && ("PCT" != fileFormat));
284
285 if (usingQImage || SupportFormatsInstance()->qtSupportFormats.contains(fileFormat)) {
286 #else
287 QString fileFormat = detectImageFormatInternal(fileName);
288 #endif
289 QImageReader reader(fileName);
290 reader.setAutoTransform(true);
291
292 if (reader.imageCount() > 0 || "ICNS" != fileFormat) {
293 image = reader.read();
294 if (!image.isNull()) {
295 return true;
296 } else {
297 // Try loading image with a forced format.
298 reader.setFormat(fileFormat.toLower().toUtf8());
299 image = reader.read();
300 if (!image.isNull()) {
301 return true;
302
303 #ifndef DTK_DISABLE_EX_IMAGE_FORMAT
304 } else if (DLibFreeImageInstance()->isValid() &&
305 DLibFreeImageInstance()->FreeImage_FIFSupportsReading(fifFormat)) {
306 // Try load with FreeImage.
307 return loadImageWithFreeImage(fileName, image, fifFormat, fileFormat);
308 #endif
309 } else {
310 lastError = QString("Load image by qt failed, %1, use format: %2").arg(reader.errorString()).arg(fileFormat);
311 return false;
312 }
313 }
314 }
315
316 #ifndef DTK_DISABLE_EX_IMAGE_FORMAT
317 } else if (SupportFormatsInstance()->libRawFormats.contains(fileFormat) && DLibRawInstance()->isValid()) {
318 // Use libRaw load.
319 image = DLibRawInstance()->loadImage(fileName, lastError);
320 return !image.isNull();
321 } else if (DLibFreeImageInstance()->isValid()) {
322 // Use FreeImage load.
323 return loadImageWithFreeImage(fileName, image, fifFormat, fileFormat);
324 }
325 #endif
326
327 lastError = QString("Unsupport image format: %1").arg(fileFormat);
328 return false;
329 }
330
331 #ifndef DTK_DISABLE_EX_IMAGE_FORMAT
332 bool DImageHandlerPrivate::loadImageWithFreeImage(const QString &fileName,
333 QImage &image,
334 FREE_IMAGE_FORMAT fifFormat,
335 QString fileFormat)
336 {
337 if (!DLibFreeImageInstance()->isValid()) {
338 return false;
339 }
340
341 if (FIF_UNKNOWN != fifFormat || SupportFormatsInstance()->freeImageFormats.contains(fileFormat)) {
342 if (FIF_UNKNOWN == fifFormat) {
343 fifFormat = FREE_IMAGE_FORMAT(SupportFormatsInstance()->freeImageFormats.value(fileFormat, FIF_UNKNOWN));
344 }
345
346 static const int MAX_JP2_SUPPORT_SIZE = 40960000;
347 if (FIF_JP2 == fifFormat && QFileInfo(fileName).size() > MAX_JP2_SUPPORT_SIZE) {
348 lastError = QString("Load image failed, JP2 image size to big, format: %1").arg(fileFormat);
349 return false;
350 }
351
352 FIBITMAP *dib = DLibFreeImageInstance()->FreeImage_Load(fifFormat, fileName.toUtf8().data(), 0);
353 if (!dib) {
354 lastError = QString("Load image failed, format: %1").arg(fileFormat);
355 return false;
356 }
357
358 image = DLibFreeImageInstance()->FIBITMAPToQImage(dib);
359 if (image.isNull()) {
360 DLibFreeImageInstance()->FreeImage_Unload(dib);
361 lastError = QString("Convert to QImage failed: %1").arg(fileFormat);
362 return false;
363 }
364
365 DLibFreeImageInstance()->FreeImage_Unload(dib);
366 return true;
367 }
368
369 lastError = QString("Unsupport image format: %1").arg(fileFormat);
370 return false;
371 }
372 #endif
373
374 bool DImageHandlerPrivate::rotateImageFile(const QString &fileName, int angle)
375 {
376 D_Q(DImageHandler);
377
378 if (0 != (angle % 90)) {
379 lastError = QString("Unsupported angle.");
380 return false;
381 }
382
383 QString format = q->detectImageFormat(fileName);
384 if (SupportFormatsInstance()->qtRotatableFormats.contains(format)) {
385 QImage image(fileName);
386
387 #ifndef DTK_DISABLE_EX_IMAGE_FORMAT
388 // Some image formats support use EXIF info store orientation info.
389 if (DLibFreeImageInstance()->isValid()) {
390 // Using libFreeImage to get EXIF information.
391 ExifImageOrientation orientation = DLibFreeImageInstance()->imageOrientation(fileName);
392 adjustImageToRealOrientation(image, orientation);
393 }
394 #endif
395
396 if (rotateImage(image, angle)) {
397 image.save(fileName, format.toLatin1().data(), SAVE_QUAITY_VALUE);
398 return true;
399 }
400 return false;
401 }
402
403 #ifndef DTK_DISABLE_EX_IMAGE_FORMAT
404 if (DLibFreeImageInstance()->isValid()) {
405 return DLibFreeImageInstance()->rotateImageFile(fileName, angle, lastError);
406 }
407 #endif
408
409 lastError = QString("Unsupported format: %1").arg(format);
410 return false;
411 }
412
413 bool DImageHandlerPrivate::rotateImage(QImage &image, int angle)
414 {
415 if (image.isNull()) {
416 lastError = QString("Image is null.");
417 return false;
418 }
419
420 if (0 != (angle % 90)) {
421 lastError = QString("Rotate angle not base of 90, angle: %1").arg(angle);
422 return false;
423 }
424
425 QImage imageCopy(image);
426 if (!imageCopy.isNull()) {
427 QTransform rotateMatrix;
428 rotateMatrix.rotate(angle);
429 image = imageCopy.transformed(rotateMatrix, Qt::SmoothTransformation);
430 return true;
431 }
432
433 lastError = QString("Image is null.");
434 return false;
435 }
436
437 void DImageHandlerPrivate::adjustImageToRealOrientation(QImage &image, ExifImageOrientation orientation)
438 {
439 switch (orientation) {
440 // TopLeft unnecessary rotate/flip
441 case TopRight: // Horizontal flip
442 image = image.mirrored(true, false);
443 break;
444 case BottomRight:
445 rotateImage(image, 180);
446 break;
447 case BottomLeft: // Vertical flip
448 image = image.mirrored(false, true);
449 break;
450 case LeftTop: // Clockwise 90 degrees and horizontal flip
451 rotateImage(image, 90);
452 image = image.mirrored(true, false);
453 break;
454 case RightTop:
455 rotateImage(image, 90);
456 break;
457 case RightBottom: // Clockwise 90 degrees and vertical flip
458 rotateImage(image, 90);
459 image = image.mirrored(false, true);
460 break;
461 case LeftBottom: // Counterclockwise 90 degrees
462 rotateImage(image, -90);
463 break;
464 default:
465 break;
466 }
467 }
468
469 DImageHandler::DImageHandler(QObject *parent)
470 : QObject(parent)
471 , DObject(*new DImageHandlerPrivate(this))
472 {
473 }
474
475 DImageHandler::~DImageHandler() {}
476
477 void DImageHandler::setFileName(const QString &fileName)
478 {
479 D_D(DImageHandler);
480 if (fileName == d->fileName) {
481 return;
482 }
483
484 d->fileName = fileName;
485 d->options &= 0;
486 clearCache();
487
488 if (!d->fileName.isEmpty()) {
489 d->cachedFormat = detectImageFormat(fileName);
490 d->options.setFlag(DImageHandlerPrivate::Readable, d->formatReadable(d->cachedFormat));
491 if (d->formatWriteable(d->cachedFormat)) {
492 d->options.setFlag(DImageHandlerPrivate::Wirteable);
493 d->options.setFlag(DImageHandlerPrivate::Rotatable);
494 }
495 }
496 }
497
498 QString DImageHandler::fileName() const
499 {
500 D_DC(DImageHandler);
501 return d->fileName;
502 }
503
504 QImage DImageHandler::readImage()
505 {
506 D_D(DImageHandler);
507
508 if (!isReadable()) {
509 d->lastError = QString("File is not readable");
510 return QImage();
511 }
512
513 if (!d->cachedImage.isNull()) {
514 return d->cachedImage;
515 }
516
517 d->loadStaticImageFromFile(d->fileName, d->cachedImage);
518 return d->cachedImage;
519 }
520
521 QImage DImageHandler::thumbnail(const QSize &size, Qt::AspectRatioMode mode)
522 {
523 D_D(DImageHandler);
524 if (d->cachedImage.isNull()) {
525 d->loadStaticImageFromFile(d->fileName, d->cachedImage);
526 }
527
528 return d->cachedImage.scaled(size, mode);
529 }
530
531 QString DImageHandler::imageFormat() const
532 {
533 D_DC(DImageHandler);
534 return d->cachedFormat;
535 }
536
537 QSize DImageHandler::imageSize()
538 {
539 D_D(DImageHandler);
540 if (isReadable() && d->cachedImage.isNull()) {
541 d->loadStaticImageFromFile(d->fileName, d->cachedImage);
542 }
543
544 return d->cachedImage.size();
545 }
546
547 QHash<QString, QString> DImageHandler::findAllMetaData()
548 {
549 D_D(DImageHandler);
550 if (!isReadable()) {
551 return {};
552 }
553
554 #ifndef DTK_DISABLE_EX_IMAGE_FORMAT
555 if (DLibFreeImageInstance()->isValid()) {
556 return DLibFreeImageInstance()->findAllMetaData(d->fileName);
557 }
558 #endif
559
560 return {};
561 }
562
563 bool DImageHandler::saveImage(const QString &fileName, const QString &format)
564 {
565 D_D(DImageHandler);
566
567 if (d->cachedImage.isNull()) {
568 if (!d->loadStaticImageFromFile(d->fileName, d->cachedImage)) {
569 return false;
570 }
571 }
572
573 return saveImage(d->cachedImage, fileName, format);
574 }
575
576 bool DImageHandler::saveImage(const QImage &image, const QString &fileName, const QString &format)
577 {
578 D_D(DImageHandler);
579
580 QString realFormat = format.toUpper();
581 if (realFormat.isEmpty()) {
582 // Detect save file format.
583 #ifndef DTK_DISABLE_EX_IMAGE_FORMAT
584 FREE_IMAGE_FORMAT fifFormat = FIF_UNKNOWN;
585 realFormat = detectImageFormatInternal(fileName, fifFormat);
586 if (FIF_UNKNOWN != fifFormat) {
587 realFormat = SupportFormatsInstance()->freeImageFormats.key(fifFormat);
588 }
589 #else
590 realFormat = detectImageFormatInternal(fileName);
591 #endif
592 }
593
594 if (!SupportFormatsInstance()->saveableFormats.contains(realFormat.toUpper())) {
595 d->lastError = QString("Unsupport image save format: %1").arg(realFormat);
596 return false;
597 }
598
599 if (!image.save(fileName, realFormat.toUtf8().data(), SAVE_QUAITY_VALUE)) {
600 d->lastError = QString("Save image by qt failed, format: %1").arg(realFormat);
601 return false;
602 }
603 return true;
604 }
605
606 bool DImageHandler::rotateImage(QImage &image, int angle)
607 {
608 D_D(DImageHandler);
609 return d->rotateImage(image, angle);
610 }
611
612 bool DImageHandler::rotateImageFile(const QString &fileName, int angle)
613 {
614 D_D(DImageHandler);
615 return d->rotateImageFile(fileName, angle);
616 }
617
618 bool DImageHandler::isReadable() const
619 {
620 D_DC(DImageHandler);
621 return d->options.testFlag(DImageHandlerPrivate::Readable);
622 }
623
624 bool DImageHandler::isWriteable() const
625 {
626 D_DC(DImageHandler);
627 return d->options.testFlag(DImageHandlerPrivate::Wirteable);
628 }
629
630 bool DImageHandler::isRotatable() const
631 {
632 D_DC(DImageHandler);
633 return d->options.testFlag(DImageHandlerPrivate::Rotatable);
634 }
635
636 void DImageHandler::clearCache()
637 {
638 D_D(DImageHandler);
639 d->cachedImage = QImage();
640 d->cachedFormat.clear();
641 d->lastError.clear();
642 }
643
644 QString DImageHandler::lastError() const
645 {
646 return d_func()->lastError;
647 }
648
649 QStringList DImageHandler::supportFormats()
650 {
651 return SupportFormatsInstance()->supportFormats;
652 }
653
654 #ifndef DTK_DISABLE_EX_IMAGE_FORMAT
655 QString detectImageFormatInternal(const QString &fileName, FREE_IMAGE_FORMAT &format)
656 #else
657 QString detectImageFormatInternal(const QString &fileName)
658 #endif
659 {
660 QFileInfo info(fileName);
661 QString fileSuffix = info.suffix().toUpper();
662 QByteArray tempPath = fileName.toUtf8();
663
664 #ifndef DTK_DISABLE_EX_IMAGE_FORMAT
665 format = FIF_UNKNOWN;
666
667 // Check load lib FreeImage succeeded.
668 if (DLibFreeImageInstance()->isValid()) {
669 format = DLibFreeImageInstance()->FreeImage_GetFileType(tempPath.data(), 0);
670 if (FIF_UNKNOWN != format && format != SupportFormatsInstance()->freeImageFormats[fileSuffix]) {
671 // Some format don't load with FreeImage.
672 QString fifFileFormat = SupportFormatsInstance()->freeImageFormats.key(format);
673 if (!fifFileFormat.isEmpty()) {
674 fileSuffix = fifFileFormat;
675 }
676 }
677
678 if (format == FIF_TIFF) {
679 fileSuffix = "TIFF";
680 }
681 }
682 #endif
683
684 if (fileSuffix.isEmpty()) {
685 QFile file(fileName);
686 if (!file.open(QFile::ReadOnly)) {
687 return QString("");
688 }
689
690 const QByteArray data = file.read(64);
691
692 // Check bmp file.
693 if (data.startsWith("BM")) {
694 return "BMP";
695 }
696
697 // Check dds file.
698 if (data.startsWith("DDS")) {
699 return "DDS";
700 }
701
702 // Check gif file.
703 if (data.startsWith("GIF8")) {
704 return "GIF";
705 }
706
707 // Check Max OS icons file.
708 if (data.startsWith("icns")) {
709 return "ICNS";
710 }
711
712 // Check jpeg file.
713 if (data.startsWith("\xff\xd8")) {
714 return "JPG";
715 }
716
717 // Check mng file.
718 if (data.startsWith("\x8a\x4d\x4e\x47\x0d\x0a\x1a\x0a")) {
719 return "MNG";
720 }
721
722 // Check net pbm file (BitMap).
723 if (data.startsWith("P1") || data.startsWith("P4")) {
724 return "PBM";
725 }
726
727 // Check pgm file (GrayMap).
728 if (data.startsWith("P2") || data.startsWith("P5")) {
729 return "PGM";
730 }
731
732 // Check ppm file (PixMap).
733 if (data.startsWith("P3") || data.startsWith("P6")) {
734 return "PPM";
735 }
736
737 // Check png file.
738 if (data.startsWith("\x89PNG\x0d\x0a\x1a\x0a")) {
739 return "PNG";
740 }
741
742 // Check svg file.
743 if (data.indexOf("<svg") > -1) {
744 return "SVG";
745 }
746
747 // Check tiff file.
748 if (data.startsWith("MM\x00\x2a") || data.startsWith("II\x2a\x00")) {
749 // big-endian, little-endian.
750 return "TIFF";
751 }
752
753 // Check webp file.
754 if (data.startsWith("RIFFr\x00\x00\x00WEBPVP")) {
755 return "WEBP";
756 }
757
758 // Check xbm file.
759 if (data.indexOf("#define max_width ") > -1 && data.indexOf("#define max_height ") > -1) {
760 return "XBM";
761 }
762
763 // Check xpm file.
764 if (data.startsWith("/* XPM */")) {
765 return "XPM";
766 }
767
768 return QString("");
769 }
770
771 return fileSuffix;
772 }
773
774 QString DImageHandler::detectImageFormat(const QString &fileName)
775 {
776 #ifndef DTK_DISABLE_EX_IMAGE_FORMAT
777 FREE_IMAGE_FORMAT format = FIF_UNKNOWN;
778 return detectImageFormatInternal(fileName, format);
779 #else
780 return detectImageFormatInternal(fileName);
781 #endif
782 }
783
784 QImage DImageHandler::oldColorFilter(const QImage &img)
785 {
786 QImage imgCopy;
787 if (img.format() != QImage::Format_RGB888) {
788 imgCopy = QImage(img).convertToFormat(QImage::Format_RGB888);
789 } else {
790 imgCopy = QImage(img);
791 }
792 uint8_t *rgb = imgCopy.bits();
793 if (nullptr == rgb) {
794 return QImage();
795 }
796 int size = img.width() * img.height();
797
798 #pragma omp parallel for
799 for (int i = 0; i < size; i++) {
800 float r = 0.393 * rgb[i * 3] + 0.769 * rgb[i * 3 + 1] + 0.189 * rgb[i * 3 + 2];
801 float g = 0.349 * rgb[i * 3] + 0.686 * rgb[i * 3 + 1] + 0.168 * rgb[i * 3 + 2];
802 float b = 0.272 * rgb[i * 3] + 0.534 * rgb[i * 3 + 1] + 0.131 * rgb[i * 3 + 2];
803 r = qBound<float>(0, r, 255.0);
804 g = qBound<float>(0, g, 255.0);
805 b = qBound<float>(0, b, 255.0);
806 rgb[i * 3] = r;
807 rgb[i * 3 + 1] = g;
808 rgb[i * 3 + 2] = b;
809 }
810
811 return imgCopy;
812 }
813
814 QImage DImageHandler::warmColorFilter(const QImage &img, int intensity)
815 {
816 QImage imgCopy;
817 if (img.format() != QImage::Format_RGB888) {
818 imgCopy = QImage(img).convertToFormat(QImage::Format_RGB888);
819 } else {
820 imgCopy = QImage(img);
821 }
822 uint8_t *rgb = imgCopy.bits();
823 if (nullptr == rgb) {
824 return QImage();
825 }
826 QColor frontColor;
827 int size = img.width() * img.height();
828
829 #pragma omp parallel for
830 for (int i = 0; i < size; i++) {
831 int r = rgb[i * 3] + intensity;
832 int g = rgb[i * 3 + 1] + intensity;
833 int b = rgb[i * 3 + 2];
834
835 rgb[i * 3] = r > 255 ? 255 : r;
836 rgb[i * 3 + 1] = g > 255 ? 255 : g;
837 rgb[i * 3 + 2] = b > 255 ? 255 : b;
838 }
839
840 return imgCopy;
841 }
842
843 QImage DImageHandler::coolColorFilter(const QImage &img, int intensity)
844 {
845 QImage imgCopy;
846 if (img.format() != QImage::Format_RGB888) {
847 imgCopy = QImage(img).convertToFormat(QImage::Format_RGB888);
848 } else {
849 imgCopy = QImage(img);
850 }
851 uint8_t *rgb = imgCopy.bits();
852 if (nullptr == rgb) {
853 return QImage();
854 }
855 QColor frontColor;
856 int size = img.width() * img.height();
857
858 #pragma omp parallel for
859 for (int i = 0; i < size; i++) {
860 int r = rgb[i * 3];
861 int g = rgb[i * 3 + 1];
862 int b = rgb[i * 3 + 2] + intensity;
863
864 rgb[i * 3] = r > 255 ? 255 : r;
865 rgb[i * 3 + 1] = g > 255 ? 255 : g;
866 rgb[i * 3 + 2] = b > 255 ? 255 : b;
867 }
868
869 return imgCopy;
870 }
871
872 QImage DImageHandler::grayScaleColorFilter(const QImage &img)
873 {
874 QImage imgCopy;
875 if (img.format() != QImage::Format_RGB888) {
876 imgCopy = QImage(img).convertToFormat(QImage::Format_RGB888);
877 } else {
878 imgCopy = QImage(img);
879 }
880 uint8_t *rgb = imgCopy.bits();
881 if (nullptr == rgb) {
882 return QImage();
883 }
884 QColor frontColor;
885 int size = img.width() * img.height();
886
887 #pragma omp parallel for
888 for (int i = 0; i < size; i++) {
889 int average = (rgb[i * 3] + rgb[i * 3 + 1] + rgb[i * 3 + 2]) / 3;
890 rgb[i * 3] = average > 255 ? 255 : average;
891 rgb[i * 3 + 1] = average > 255 ? 255 : average;
892 rgb[i * 3 + 2] = average > 255 ? 255 : average;
893 }
894
895 return imgCopy;
896 }
897
898 QImage DImageHandler::antiColorFilter(const QImage &img)
899 {
900 QImage imgCopy;
901 if (img.format() != QImage::Format_RGB888) {
902 imgCopy = QImage(img).convertToFormat(QImage::Format_RGB888);
903 } else {
904 imgCopy = QImage(img);
905 }
906 uint8_t *rgb = imgCopy.bits();
907 if (nullptr == rgb) {
908 return QImage();
909 }
910 int size = img.width() * img.height();
911
912 #pragma omp parallel for
913 for (int i = 0; i < size; i++) {
914 rgb[i * 3] = 255 - rgb[i * 3];
915 rgb[i * 3 + 1] = 255 - rgb[i * 3 + 1];
916 rgb[i * 3 + 2] = 255 - rgb[i * 3 + 2];
917 }
918
919 return imgCopy;
920 }
921
922 QImage DImageHandler::metalColorFilter(const QImage &img)
923 {
924 QImage baseImage = QImage(img);
925 QImage darkImage = DImageHandler::changeBrightness(img, -100);
926 QImage greyImage = DImageHandler::grayScale(darkImage);
927 QPainter painter;
928
929 QImage newImage = baseImage.scaled(QSize(img.width(), img.height()));
930
931 painter.begin(&newImage);
932 painter.setOpacity(0.5);
933 painter.drawImage(0, 0, greyImage);
934 painter.end();
935
936 return newImage;
937 }
938
939 QImage DImageHandler::bilateralFilter(const QImage &img, double spatialDecay, double photometricStandardDeviation)
940 {
941 QImage imgCopy = QImage(img).convertToFormat(QImage::Format_RGB888);
942
943 double c = -0.5 / (photometricStandardDeviation * photometricStandardDeviation);
944 double mu = spatialDecay / (2 - spatialDecay);
945
946 double *exptable = new double[256];
947 double *g_table = new double[256];
948 #pragma omp parallel for
949 for (int i = 0; i <= 255; i++) {
950 exptable[i] = (1 - spatialDecay) * exp(c * i * i);
951 g_table[i] = mu * i;
952 }
953 int width = img.width();
954 int height = img.height();
955 int length = width * height;
956 double *data2Red = new double[length];
957 double *data2Green = new double[length];
958 double *data2Blue = new double[length];
959
960 int size = imgCopy.width() * imgCopy.height();
961 uint8_t *rgb = imgCopy.bits();
962 #pragma omp parallel for
963 for (int i = 0; i < size; i++) {
964 data2Red[i] = rgb[i * 3];
965 data2Green[i] = rgb[i * 3 + 1];
966 data2Blue[i] = rgb[i * 3 + 2];
967 }
968
969 double *gRed = new double[length];
970 double *pRed = new double[length];
971 double *rRed = new double[length];
972
973 double *gGreen = new double[length];
974 double *pGreen = new double[length];
975 double *rGreen = new double[length];
976
977 double *gBlue = new double[length];
978 double *pBlue = new double[length];
979 double *rBlue = new double[length];
980 memcpy(pRed, data2Red, sizeof(double) * length);
981 memcpy(rRed, data2Red, sizeof(double) * length);
982
983 memcpy(pGreen, data2Green, sizeof(double) * length);
984 memcpy(rGreen, data2Green, sizeof(double) * length);
985
986 memcpy(pBlue, data2Blue, sizeof(double) * length);
987 memcpy(rBlue, data2Blue, sizeof(double) * length);
988
989 double rho0 = 1.0 / (2 - spatialDecay);
990 #pragma omp parallel for
991 for (int k2 = 0; k2 < height; ++k2) {
992 int startIndex = k2 * width;
993 double mu2 = 0.0;
994
995 for (int k = startIndex + 1, K = startIndex + width; k < K; ++k) {
996 int div0Red = fabs(pRed[k] - pRed[k - 1]);
997 mu2 = exptable[div0Red];
998 pRed[k] = pRed[k - 1] * mu2 + pRed[k] * (1.0 - mu2);
999
1000 int div0Green = fabs(pGreen[k] - pGreen[k - 1]);
1001 mu2 = exptable[div0Green];
1002 pGreen[k] = pGreen[k - 1] * mu2 + pGreen[k] * (1.0 - mu2);
1003
1004 int div0Blue = fabs(pBlue[k] - pBlue[k - 1]);
1005 mu2 = exptable[div0Blue];
1006 pBlue[k] = pBlue[k - 1] * mu2 + pBlue[k] * (1.0 - mu2);
1007 }
1008
1009 for (int k = startIndex + width - 2; startIndex <= k; --k) {
1010 int div0Red = fabs(rRed[k] - rRed[k + 1]);
1011 mu2 = exptable[div0Red];
1012 rRed[k] = rRed[k + 1] * mu2 + rRed[k] * (1.0 - mu2);
1013
1014 int div0Green = fabs(rGreen[k] - rGreen[k + 1]);
1015 mu2 = exptable[div0Green];
1016 rGreen[k] = rGreen[k + 1] * mu2 + rGreen[k] * (1.0 - mu2);
1017
1018 int div0Blue = fabs(rBlue[k] - rBlue[k + 1]);
1019 mu2 = exptable[div0Blue];
1020 rBlue[k] = rBlue[k + 1] * mu2 + rBlue[k] * (1.0 - mu2);
1021 }
1022
1023 for (int k = startIndex, K = startIndex + width; k < K; k++) {
1024 rRed[k] = (rRed[k] + pRed[k]) * rho0 - g_table[(int)data2Red[k]];
1025 rGreen[k] = (rGreen[k] + pGreen[k]) * rho0 - g_table[(int)data2Green[k]];
1026 rBlue[k] = (rBlue[k] + pBlue[k]) * rho0 - g_table[(int)data2Blue[k]];
1027 }
1028 }
1029
1030 int m = 0;
1031
1032 for (int k2 = 0; k2 < height; k2++) {
1033 int n = k2;
1034
1035 for (int k1 = 0; k1 < width; k1++) {
1036 gRed[n] = rRed[m];
1037 gGreen[n] = rGreen[m];
1038 gBlue[n] = rBlue[m];
1039 m++;
1040 n += height;
1041 }
1042 }
1043
1044 memcpy(pRed, gRed, sizeof(double) * height * width);
1045 memcpy(rRed, gRed, sizeof(double) * height * width);
1046
1047 memcpy(pGreen, gGreen, sizeof(double) * height * width);
1048 memcpy(rGreen, gGreen, sizeof(double) * height * width);
1049
1050 memcpy(pBlue, gBlue, sizeof(double) * height * width);
1051 memcpy(rBlue, gBlue, sizeof(double) * height * width);
1052
1053 #pragma omp parallel for
1054 for (int k1 = 0; k1 < width; ++k1) {
1055 int startIndex = k1 * height;
1056 double mu1 = 0.0;
1057
1058 for (int k = startIndex + 1, K = startIndex + height; k < K; ++k) {
1059 int div0Red = fabs(pRed[k] - pRed[k - 1]);
1060 mu1 = exptable[div0Red];
1061 pRed[k] = pRed[k - 1] * mu1 + pRed[k] * (1.0 - mu1);
1062
1063 int div0Green = fabs(pGreen[k] - pGreen[k - 1]);
1064 mu1 = exptable[div0Green];
1065 pGreen[k] = pGreen[k - 1] * mu1 + pGreen[k] * (1.0 - mu1);
1066
1067 int div0Blue = fabs(pBlue[k] - pBlue[k - 1]);
1068 mu1 = exptable[div0Blue];
1069 pBlue[k] = pBlue[k - 1] * mu1 + pBlue[k] * (1.0 - mu1);
1070 }
1071
1072 for (int k = startIndex + height - 2; startIndex <= k; --k) {
1073 int div0Red = fabs(rRed[k] - rRed[k + 1]);
1074 mu1 = exptable[div0Red];
1075 rRed[k] = rRed[k + 1] * mu1 + rRed[k] * (1.0 - mu1);
1076
1077 int div0Green = fabs(rGreen[k] - rGreen[k + 1]);
1078 mu1 = exptable[div0Green];
1079 rGreen[k] = rGreen[k + 1] * mu1 + rGreen[k] * (1.0 - mu1);
1080
1081 int div0Blue = fabs(rBlue[k] - rBlue[k + 1]);
1082 mu1 = exptable[div0Blue];
1083 rBlue[k] = rBlue[k + 1] * mu1 + rBlue[k] * (1.0 - mu1);
1084 }
1085 }
1086
1087 double init_gain_mu = spatialDecay / (2 - spatialDecay);
1088
1089 #pragma omp parallel for
1090 for (int k = 0; k < length; ++k) {
1091 rRed[k] = (rRed[k] + pRed[k]) * rho0 - gRed[k] * init_gain_mu;
1092
1093 rGreen[k] = (rGreen[k] + pGreen[k]) * rho0 - gGreen[k] * init_gain_mu;
1094
1095 rBlue[k] = (rBlue[k] + pBlue[k]) * rho0 - gBlue[k] * init_gain_mu;
1096 }
1097
1098 m = 0;
1099 int nRowBytes = (width * 24 + 31) / 32 * 4;
1100 int lineNum_24 = 0;
1101
1102 for (int k1 = 0; k1 < width; ++k1) {
1103 int n = k1;
1104 for (int k2 = 0; k2 < height; ++k2) {
1105 data2Red[n] = rRed[m];
1106 data2Green[n] = rGreen[m];
1107 data2Blue[n] = rBlue[m];
1108 lineNum_24 = k2 * nRowBytes;
1109 rgb[lineNum_24 + k1 * 3] = data2Red[n];
1110 rgb[lineNum_24 + k1 * 3 + 1] = data2Green[n];
1111 rgb[lineNum_24 + k1 * 3 + 2] = data2Blue[n];
1112 m++;
1113 n += width;
1114 }
1115 }
1116 delete[] data2Red;
1117 data2Red = nullptr;
1118 delete[] data2Green;
1119 data2Green = nullptr;
1120 delete[] data2Blue;
1121 data2Blue = nullptr;
1122
1123 delete[] pRed;
1124 pRed = nullptr;
1125 delete[] rRed;
1126 rRed = nullptr;
1127 delete[] gRed;
1128 gRed = nullptr;
1129
1130 delete[] pGreen;
1131 pGreen = nullptr;
1132 delete[] rGreen;
1133 rGreen = nullptr;
1134 delete[] gGreen;
1135 gGreen = nullptr;
1136
1137 delete[] pBlue;
1138 pBlue = nullptr;
1139 delete[] rBlue;
1140 rBlue = nullptr;
1141 delete[] gBlue;
1142 gBlue = nullptr;
1143
1144 delete[] exptable;
1145 exptable = nullptr;
1146 delete[] g_table;
1147 g_table = nullptr;
1148
1149 return imgCopy;
1150 }
1151
1152 QImage DImageHandler::contourExtraction(const QImage &img)
1153 {
1154 int width = img.width();
1155 int height = img.height();
1156
1157 QImage binImg = binaryzation(img);
1158 QImage newImg = QImage(width, height, QImage::Format_RGB888);
1159 newImg.fill(Qt::white);
1160
1161 uint8_t *rgb = newImg.bits();
1162 uint8_t *binrgb = binImg.bits();
1163 int nRowBytes = (width * 24 + 31) / 32 * 4;
1164
1165 #pragma omp parallel for
1166 for (int y = 1; y < height - 1; y++) {
1167 for (int x = 1; x < width - 1; x++) {
1168 int pixel[8];
1169 memset(pixel, 0, 8 * sizeof(int));
1170 int lineNum_24 = y * nRowBytes;
1171 if (binrgb[lineNum_24 + x * 3] == 0) {
1172 rgb[lineNum_24 + x * 3] = 0;
1173 rgb[lineNum_24 + x * 3 + 1] = 0;
1174 rgb[lineNum_24 + x * 3 + 2] = 0;
1175 pixel[0] = binrgb[(y - 1) * nRowBytes + (x - 1) * 3];
1176 pixel[1] = binrgb[(y)*nRowBytes + (x - 1) * 3];
1177 pixel[2] = binrgb[(y + 1) * nRowBytes + (x - 1) * 3];
1178 pixel[3] = binrgb[(y - 1) * nRowBytes + (x)*3];
1179 pixel[4] = binrgb[(y + 1) * nRowBytes + (x)*3];
1180 pixel[5] = binrgb[(y - 1) * nRowBytes + (x + 1) * 3];
1181 pixel[6] = binrgb[(y)*nRowBytes + (x + 1) * 3];
1182 pixel[7] = binrgb[(y + 1) * nRowBytes + (x + 1) * 3];
1183
1184 if (pixel[0] + pixel[1] + pixel[2] + pixel[3] + pixel[4] + pixel[5] + pixel[6] + pixel[7] == 0) {
1185 rgb[lineNum_24 + x * 3] = 255;
1186 rgb[lineNum_24 + x * 3 + 1] = 255;
1187 rgb[lineNum_24 + x * 3 + 2] = 255;
1188 }
1189 }
1190 }
1191 }
1192
1193 return newImg;
1194 }
1195
1196 QImage DImageHandler::binaryzation(const QImage &img)
1197 {
1198 QImage imgCopy;
1199
1200 if (img.format() != QImage::Format_RGB888) {
1201 imgCopy = QImage(img).convertToFormat(QImage::Format_RGB888);
1202 } else {
1203 imgCopy = QImage(img);
1204 }
1205 uint8_t *rgb = imgCopy.bits();
1206
1207 int size = img.width() * img.height();
1208
1209 #pragma omp parallel for
1210 for (int i = 0; i < size; i++) {
1211 int gray = (rgb[i * 3] + rgb[i * 3 + 1] + rgb[i * 3 + 2]) / 3;
1212 int newGray = 0;
1213 if (gray > 128)
1214 newGray = 255;
1215 else
1216 newGray = 0;
1217 rgb[i * 3] = newGray;
1218 rgb[i * 3 + 1] = newGray;
1219 rgb[i * 3 + 2] = newGray;
1220 }
1221
1222 return imgCopy;
1223 }
1224
1225 QImage DImageHandler::grayScale(const QImage &img)
1226 {
1227 QImage imgCopy;
1228
1229 if (img.format() != QImage::Format_RGB888) {
1230 imgCopy = QImage(img).convertToFormat(QImage::Format_RGB888);
1231 } else {
1232 imgCopy = QImage(img);
1233 }
1234 uint8_t *rgb = imgCopy.bits();
1235 int size = img.width() * img.height();
1236
1237 #pragma omp parallel for
1238 for (int i = 0; i < size; i++) {
1239 int average = (rgb[i * 3] * 299 + rgb[i * 3 + 1] * 587 + rgb[i * 3 + 1] * 114 + 500) / 1000;
1240 rgb[i * 3] = average;
1241 rgb[i * 3 + 1] = average;
1242 rgb[i * 3 + 2] = average;
1243 }
1244
1245 return imgCopy;
1246 }
1247
1248 QImage DImageHandler::laplaceSharpen(const QImage &img)
1249 {
1250 QImage imgCopy;
1251 int width = img.width();
1252 int height = img.height();
1253 int window[3][3] = {0, -1, 0, -1, 4, -1, 0, -1, 0};
1254 if (img.format() != QImage::Format_RGB888) {
1255 imgCopy = QImage(width, height, QImage::Format_RGB888);
1256 } else {
1257 imgCopy = QImage(img);
1258 }
1259 QImage imgCopyrgbImg = QImage(img).convertToFormat(QImage::Format_RGB888);
1260 uint8_t *rgbImg = imgCopyrgbImg.bits();
1261 uint8_t *rgb = imgCopy.bits();
1262 int nRowBytes = (width * 24 + 31) / 32 * 4;
1263
1264 #pragma omp parallel for
1265 for (int x = 1; x < img.width(); x++) {
1266 for (int y = 1; y < img.height(); y++) {
1267 int sumR = 0;
1268 int sumG = 0;
1269 int sumB = 0;
1270 int lineNum_24 = 0;
1271
1272 for (int m = x - 1; m <= x + 1; m++)
1273 for (int n = y - 1; n <= y + 1; n++) {
1274 if (m >= 0 && m < width && n < height) {
1275 lineNum_24 = n * nRowBytes;
1276 sumR += rgbImg[lineNum_24 + m * 3] * window[n - y + 1][m - x + 1];
1277 sumG += rgbImg[lineNum_24 + m * 3 + 1] * window[n - y + 1][m - x + 1];
1278 sumB += rgbImg[lineNum_24 + m * 3 + 2] * window[n - y + 1][m - x + 1];
1279 }
1280 }
1281
1282 int old_r = rgbImg[lineNum_24 + x * 3];
1283 sumR += old_r;
1284 sumR = qBound(0, sumR, 255);
1285
1286 int old_g = rgbImg[lineNum_24 + x * 3 + 1];
1287 sumG += old_g;
1288 sumG = qBound(0, sumG, 255);
1289
1290 int old_b = rgbImg[lineNum_24 + x * 3 + 2];
1291 sumB += old_b;
1292 sumB = qBound(0, sumB, 255);
1293 lineNum_24 = y * nRowBytes;
1294 rgb[lineNum_24 + x * 3] = sumR;
1295 rgb[lineNum_24 + x * 3 + 1] = sumG;
1296 rgb[lineNum_24 + x * 3 + 2] = sumB;
1297 }
1298 }
1299
1300 return imgCopy;
1301 }
1302
1303 QImage DImageHandler::sobelEdgeDetector(const QImage &img)
1304 {
1305 double *Gx = new double[9];
1306 double *Gy = new double[9];
1307
1308 /* Sobel */
1309 Gx[0] = 1.0;
1310 Gx[1] = 0.0;
1311 Gx[2] = -1.0;
1312 Gx[3] = 2.0;
1313 Gx[4] = 0.0;
1314 Gx[5] = -2.0;
1315 Gx[6] = 1.0;
1316 Gx[7] = 0.0;
1317 Gx[8] = -1.0;
1318
1319 Gy[0] = -1.0;
1320 Gy[1] = -2.0;
1321 Gy[2] = -1.0;
1322 Gy[3] = 0.0;
1323 Gy[4] = 0.0;
1324 Gy[5] = 0.0;
1325 Gy[6] = 1.0;
1326 Gy[7] = 2.0;
1327 Gy[8] = 1.0;
1328
1329 QImage grayImage = grayScale(img);
1330 int height = grayImage.height();
1331 int width = grayImage.width();
1332 QImage imgCopy = QImage(width, height, QImage::Format_RGB888);
1333
1334 uint8_t *rgbImg = grayImage.bits();
1335 uint8_t *rgb = imgCopy.bits();
1336
1337 int nRowBytes = (width * 24 + 31) / 32 * 4;
1338
1339 float *sobel_norm = new float[width * height];
1340 float max = 0.0;
1341 QColor my_color;
1342
1343 for (int x = 0; x < width; x++) {
1344 for (int y = 0; y < height; y++) {
1345 double value_gx = 0.0;
1346 double value_gy = 0.0;
1347
1348 for (int k = 0; k < 3; k++) {
1349 for (int p = 0; p < 3; p++) {
1350 if ((x + 1 + 1 - k < width) && (y + 1 + 1 - p < height)) {
1351 int lineNum_24 = (y + 1 + 1 - p) * nRowBytes;
1352 value_gx += Gx[p * 3 + k] * rgbImg[lineNum_24 + (x + 1 + 1 - k) * 3];
1353 value_gy += Gy[p * 3 + k] * rgbImg[lineNum_24 + (x + 1 + 1 - k) * 3];
1354 }
1355 }
1356 sobel_norm[x + y * width] = abs(value_gx) + abs(value_gy);
1357
1358 max = sobel_norm[x + y * width] > max ? sobel_norm[x + y * width] : max;
1359 }
1360 }
1361 }
1362
1363 for (int i = 0; i < width; i++) {
1364 for (int j = 0; j < height; j++) {
1365 my_color.setHsv(0, 0, 255 - int(255.0 * sobel_norm[i + j * width] / max));
1366
1367 int lineNum_24 = j * nRowBytes;
1368 rgb[lineNum_24 + i * 3] = my_color.red();
1369 rgb[lineNum_24 + i * 3 + 1] = my_color.green();
1370 rgb[lineNum_24 + i * 3 + 2] = my_color.blue();
1371 }
1372 }
1373 delete[] sobel_norm;
1374
1375 return imgCopy;
1376 }
1377
1378 QImage DImageHandler::changeLightAndContrast(const QImage &img, int light, int contrast)
1379 {
1380 QImage imgCopy;
1381 if (img.format() != QImage::Format_RGB888) {
1382 imgCopy = QImage(img).convertToFormat(QImage::Format_RGB888);
1383 } else {
1384 imgCopy = QImage(img);
1385 }
1386 uint8_t *rgb = imgCopy.bits();
1387 if (nullptr == rgb) {
1388 return QImage();
1389 }
1390
1391 int size = img.width() * img.height();
1392
1393 #pragma omp parallel for
1394 for (int i = 0; i < size; i++) {
1395 int r;
1396 int g;
1397 int b;
1398 r = light * 0.01 * rgb[i * 3] - 150 + contrast;
1399 g = light * 0.01 * rgb[i * 3 + 1] - 150 + contrast;
1400 b = light * 0.01 * rgb[i * 3 + 2] - 150 + contrast;
1401
1402 rgb[i * 3] = qBound(0, r, 255);
1403 rgb[i * 3 + 1] = qBound(0, g, 255);
1404 rgb[i * 3 + 2] = qBound(0, b, 255);
1405 }
1406
1407 return imgCopy;
1408 }
1409
1410 QImage DImageHandler::changeBrightness(const QImage &img, int brightness)
1411 {
1412 QImage imgCopy;
1413
1414 if (img.format() != QImage::Format_RGB888) {
1415 imgCopy = QImage(img).convertToFormat(QImage::Format_RGB888);
1416 } else {
1417 imgCopy = QImage(img);
1418 }
1419 uint8_t *rgb = imgCopy.bits();
1420 int size = img.width() * img.height();
1421
1422 #pragma omp parallel for
1423 for (int i = 0; i < size; i++) {
1424 int r = rgb[i * 3] + brightness;
1425 int g = rgb[i * 3 + 1] + brightness;
1426 int b = rgb[i * 3 + 2] + brightness;
1427 r = qBound(0, r, 255);
1428 g = qBound(0, g, 255);
1429 b = qBound(0, b, 255);
1430 rgb[i * 3] = r;
1431 rgb[i * 3 + 1] = g;
1432 rgb[i * 3 + 2] = b;
1433 }
1434
1435 return imgCopy;
1436 }
1437
1438 QImage DImageHandler::changeTransparency(const QImage &img, int transparency)
1439 {
1440 QImage newImage(img.width(), img.height(), QImage::Format_ARGB32);
1441 QColor oldColor;
1442 int r, g, b;
1443 for (int x = 0; x < newImage.width(); x++) {
1444 for (int y = 0; y < newImage.height(); y++) {
1445 oldColor = QColor(img.pixel(x, y));
1446
1447 r = oldColor.red();
1448 g = oldColor.green();
1449 b = oldColor.blue();
1450
1451 newImage.setPixel(x, y, qRgba(r, g, b, transparency));
1452 }
1453 }
1454
1455 return newImage;
1456 }
1457
1458 QImage DImageHandler::changeStauration(const QImage &img, int saturation)
1459 {
1460 int r, g, b, rgbMin, rgbMax;
1461 float k = saturation / 100.0f * 128;
1462 int alpha = 0;
1463
1464 QImage newImage(img);
1465 QColor tmpColor;
1466
1467 for (int x = 0; x < newImage.width(); x++) {
1468 for (int y = 0; y < newImage.height(); y++) {
1469 tmpColor = QColor(img.pixel(x, y));
1470 r = tmpColor.red();
1471 g = tmpColor.green();
1472 b = tmpColor.blue();
1473
1474 rgbMin = qMin(qMin(r, g), b);
1475 rgbMax = qMax(qMax(r, g), b);
1476
1477 int delta = (rgbMax - rgbMin);
1478 int value = (rgbMax + rgbMin);
1479 if (delta == 0) {
1480 continue;
1481 }
1482 int L = value >> 1;
1483 if (k >= 0) {
1484 int S = L < 128 ? (delta << 7) / value : (delta << 7) / (510 - value);
1485 alpha = k + S >= 128 ? S : 128 - k;
1486 alpha = 128 * 128 / alpha - 128;
1487 } else {
1488 alpha = k;
1489 }
1490 r = r + ((r - L) * alpha >> 7);
1491 g = g + ((g - L) * alpha >> 7);
1492 b = b + ((b - L) * alpha >> 7);
1493 r = qBound(0, r, 255);
1494 g = qBound(0, g, 255);
1495 b = qBound(0, b, 255);
1496 newImage.setPixel(x, y, qRgb(r, g, b));
1497 }
1498 }
1499
1500 return newImage;
1501 }
1502
1503 QImage DImageHandler::replacePointColor(const QImage &img, QColor oldColor, QColor newColor)
1504 {
1505 QImage imgCopy = img;
1506 if (img.format() != QImage::Format_RGB888) {
1507 imgCopy = QImage(img).convertToFormat(QImage::Format_RGB888);
1508 } else {
1509 imgCopy = QImage(img);
1510 }
1511 uint8_t *rgb = imgCopy.bits();
1512 if (nullptr == rgb) {
1513 return QImage();
1514 }
1515 QColor frontColor;
1516
1517 for (int x = 0; x < img.width(); ++x) {
1518 for (int y = 0; y < img.height(); ++y) {
1519 if (imgCopy.pixelColor(x, y) == oldColor) {
1520 imgCopy.setPixelColor(x, y, newColor);
1521 }
1522 }
1523 }
1524
1525 return imgCopy;
1526 }
1527
1528 QImage DImageHandler::flipHorizontal(const QImage &img)
1529 {
1530 return img.mirrored(true, false);
1531 }
1532
1533 QImage DImageHandler::flipVertical(const QImage &img)
1534 {
1535 return img.mirrored(false, true);
1536 }
1537
1538 DGUI_END_NAMESPACE