Debian Codegraph @

src/album/utils/imageutils.cpp

raw
1 /*
2 * Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd.
3 *
4 * Author: ZhangYong <[email protected]>
5 *
6 * Maintainer: ZhangYong <[email protected]>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include "utils/baseutils.h"
22 #include "utils/imageutils.h"
23 //#include "utils/imageutils_libexif.h"
24 #include "utils/unionimage.h"
25 #include <QBuffer>
26 #include <QCryptographicHash>
27 #include <QDebug>
28 #include <QDir>
29 #include <QDirIterator>
30 #include <QFileInfo>
31 #include <QImage>
32 #include <QImageReader>
33 #include <QtSvg>
34 #include <QMimeDatabase>
35 #include <QMutexLocker>
36 #include <QPixmapCache>
37 #include <QProcess>
38 #include <QReadWriteLock>
39 #include <QUrl>
40 #include <QApplication>
41 #include <QMovie>
42 #include <fstream>
43 #include <iostream>
44
45 namespace utils {
46
47 namespace image {
48
49 bool imageSupportRead(const QString &path)
50 {
51 const QString suffix = QFileInfo(path).suffix();
52
53 //FIXME: file types below will cause freeimage to crash on loading,
54 // take them here for good.
55 QStringList errorList;
56 errorList << "X3F";
57 if (errorList.indexOf(suffix.toUpper()) != -1) {
58 return false;
59 }
60 //return QImageReader::supportedImageFormats().contains(suffix.toUtf8());
61 return UnionImage_NameSpace::unionImageSupportFormat().contains(suffix.toUpper());
62 }
63
64 //bool imageSupportSave(const QString &path)
65 //{
66 // const QString suffix = QFileInfo(path).suffix();
67
68 // // RAW image decode is too slow, and most of these does not support saving
69 // // RAW formats render incorrectly by freeimage
70 // const QStringList raws = QStringList()
71 // << "CR2" << "CRW" // Canon cameras
72 // << "DCR" << "KDC" // Kodak cameras
73 // << "MRW" // Minolta cameras
74 // << "NEF" // Nikon cameras
75 // << "ORF" // Olympus cameras
76 // << "PEF" // Pentax cameras
77 // << "RAF" // Fuji cameras
78 // << "SRF" // Sony cameras
79 // << "PSD"
80 // << "ICO"
81 // << "TGA"
82 // << "WEBP"
83 // << "PBM"
84 // << "XPM"
85 // << "PPM"
86 // << "PGM"
87 // << "X3F" // Sigma cameras
88 // << "SVG"; // need support SVG
89
90 // //dynamic image can not be supported
91 // if (QMovie::supportedFormats().contains(suffix.toLower().toUtf8().data())) {
92 // QMovie movie(path);
93 // return movie.frameCount() == 1 ? true : false;
94 // }
95
96 // //some images that decode slow also should be written.
97 // if (raws.indexOf(suffix.toUpper()) != -1
98 // || (QImageReader(path).imageCount() > 1 )) {
99 // return true;
100 // } else {
101 // return UnionImage_NameSpace::canSave(path);
102 // }
103 //}
104
105 void getAllFileInDir(const QDir &dir, QFileInfoList &result)
106 {
107 QDir root(dir);
108 auto list = root.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
109 for (const auto &eachInfo : list) {
110 if (eachInfo.isDir()) {
111 getAllFileInDir(eachInfo.absoluteFilePath(), result);
112 } else {
113 result.push_back(eachInfo);
114 }
115 }
116 }
117
118 const QFileInfoList getImagesAndVideoInfo(const QString &dir, bool recursive)
119 {
120 QFileInfoList infos;
121
122 if (! recursive) {
123 QFileInfoList nsl;
124 getAllFileInDir(dir, nsl);
125 for (QFileInfo info : nsl) {
126 if (imageSupportRead(info.absoluteFilePath()) ||
127 utils::base::isVideo(info.absoluteFilePath())) {
128 infos << info;
129 }
130 }
131 return infos;
132 }
133
134 QDirIterator dirIterator(dir,
135 QDir::Files,
136 QDirIterator::Subdirectories);
137 while (dirIterator.hasNext()) {
138 dirIterator.next();
139 if ( imageSupportRead(dirIterator.fileInfo().absoluteFilePath())
140 || utils::base::isVideo(dirIterator.fileInfo().absoluteFilePath())) {
141 infos << dirIterator.fileInfo();
142 }
143 }
144
145 return infos;
146 }
147
148 QStringList supportedImageFormats()
149 {
150 return UnionImage_NameSpace::unionImageSupportFormat();
151 }
152
153 //bool checkFileType(const QString &path)
154 //{
155 // if (imageSupportRead(path)) {
156 // QFileInfo info(path);
157 // QMimeDatabase db;
158 // QMimeType mt = db.mimeTypeForFile(info.filePath(), QMimeDatabase::MatchContent);
159 // QString str = info.suffix().toLower();
160 // if (mt.name().startsWith("image/*") || mt.name().startsWith("video/x-mng")) {
161 // if (utils::image::supportedImageFormats().contains(str, Qt::CaseInsensitive)) {
162 // return true;
163 // }
164 // }
165 // /**
166 // * 2020/4/29
167 // * QMimeType能识别的图片类型有限,相册能支持的格式已经不止这些,使用ImageSupporter后替换该函数
168 // */
169 // if (utils::image::supportedImageFormats().contains(str, Qt::CaseInsensitive)) {
170 // return true;
171 // }
172 // }
173 // return false;
174 //}
175
176 QPixmap getDamagePixmap(bool bLight)
177 {
178 static QPixmap pix_light, pix_dark;
179 if (bLight) {
180 if (pix_light.isNull())
181 pix_light = utils::base::renderSVG(view::LIGHT_DAMAGEICON, QSize(150, 150));
182 return pix_light;
183 } else {
184 if (pix_dark.isNull())
185 pix_dark = utils::base::renderSVG(view::DARK_DAMAGEICON, QSize(150, 150));
186 return pix_dark;
187 }
188 }
189
190 } // namespace image
191
192 } //namespace utils