| 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 "unionimage.h" |
| 22 |
#include <FreeImage.h> |
| 23 |
//#include "giflib/cmanagerattributeservice.h" |
| 24 |
|
| 25 |
#include <QObject> |
| 26 |
#include <QMutex> |
| 27 |
#include <QMutexLocker> |
| 28 |
#include <QDate> |
| 29 |
#include <QTime> |
| 30 |
#include <QtMath> |
| 31 |
#include <QMovie> |
| 32 |
#include <QMatrix> |
| 33 |
#include <QPainter> |
| 34 |
#include <QSvgGenerator> |
| 35 |
#include <QImageReader> |
| 36 |
#include <QtSvg/QSvgRenderer> |
| 37 |
|
| 38 |
|
| 39 |
#define SAVE_QUAITY_VALUE 100 |
| 40 |
|
| 41 |
const QString DATETIME_FORMAT_NORMAL = "yyyy.MM.dd"; |
| 42 |
const QString DATETIME_FORMAT_EXIF = "yyyy:MM:dd HH:mm"; |
| 43 |
|
| 44 |
namespace UnionImage_NameSpace { |
| 45 |
|
| 46 |
// enum SupportFormat { |
| 47 |
// UNKNOWN = -1, |
| 48 |
// BMP = 0, |
| 49 |
// ICO = 1, |
| 50 |
// JPG = 2, |
| 51 |
// JPE = 2, |
| 52 |
// JPS = 2, |
| 53 |
// JPEG = 2, |
| 54 |
// JNG = 3, |
| 55 |
// KOALA = 4, |
| 56 |
// KOA = KOALA, |
| 57 |
// LBM = 5, |
| 58 |
// IFF = LBM, |
| 59 |
// MNG = 6, |
| 60 |
// PBM = 7, |
| 61 |
// PBMRAW = 8, |
| 62 |
// PCD = 9, |
| 63 |
// PCX = 10, |
| 64 |
// PGM = 11, |
| 65 |
// PGMRAW = 12, |
| 66 |
// PNG = 13, |
| 67 |
// PPM = 14, |
| 68 |
// PPMRAW = 15, |
| 69 |
// RAS = 16, |
| 70 |
// TGA = 17, |
| 71 |
// TARGA = 17, |
| 72 |
// TIFF = 18, |
| 73 |
// WBMP = 19, |
| 74 |
// PSD = 20, |
| 75 |
// CUT = 21, |
| 76 |
// XBM = 22, |
| 77 |
// XPM = 23, |
| 78 |
// DDS = 24, |
| 79 |
// GIF = 25, |
| 80 |
// FHDR = 26, |
| 81 |
// FAXG3 = 27, |
| 82 |
// SGI = 28, |
| 83 |
// EXR = 29, |
| 84 |
// J2K = 30, |
| 85 |
// J2C = 30, |
| 86 |
// JPC = 30, |
| 87 |
// JP2 = 31, |
| 88 |
// PFM = 32, |
| 89 |
// PCT = 33, |
| 90 |
// PIC = 33, |
| 91 |
// PICT = 33, |
| 92 |
// RAW = 34, |
| 93 |
// WEBP = 35, |
| 94 |
// JXR = 36 |
| 95 |
// }; |
| 96 |
|
| 97 |
|
| 98 |
class UnionImage_Private |
| 99 |
{ |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public: |
| 104 |
UnionImage_Private() |
| 105 |
{ |
| 106 |
//FreeImage_Initialise(true); |
| 107 |
//m_freeiamge_formats["UNKNOWN"] = -1; |
| 108 |
m_freeiamge_formats["BMP"] = FIF_BMP; |
| 109 |
m_freeiamge_formats["ICO"] = FIF_ICO; |
| 110 |
m_freeiamge_formats["JPG"] = FIF_JPEG; |
| 111 |
m_freeiamge_formats["JPE"] = FIF_JPEG; |
| 112 |
m_freeiamge_formats["JPS"] = FIF_JPEG; |
| 113 |
m_freeiamge_formats["JPEG"] = FIF_JPEG; |
| 114 |
m_freeiamge_formats["JNG"] = FIF_JNG; |
| 115 |
m_freeiamge_formats["KOALA"] = FIF_KOALA; |
| 116 |
m_freeiamge_formats["KOA"] = FIF_KOALA; |
| 117 |
m_freeiamge_formats["LBM"] = FIF_LBM; |
| 118 |
// m_freeiamge_formats["IFF"] = FIF_LBM; |
| 119 |
m_freeiamge_formats["MNG"] = FIF_MNG; |
| 120 |
m_freeiamge_formats["PBM"] = FIF_PBM; |
| 121 |
m_freeiamge_formats["PNM"] = FIF_PBM; |
| 122 |
m_freeiamge_formats["PBMRAW"] = FIF_PBMRAW; |
| 123 |
m_freeiamge_formats["PCD"] = FIF_PCD; |
| 124 |
m_freeiamge_formats["PCX"] = FIF_PCX; |
| 125 |
m_freeiamge_formats["PGM"] = FIF_PGM; |
| 126 |
m_freeiamge_formats["PGMRAW"] = FIF_PGMRAW; |
| 127 |
m_freeiamge_formats["PNG"] = FIF_PNG; |
| 128 |
m_freeiamge_formats["PPM"] = FIF_PPM; |
| 129 |
m_freeiamge_formats["PPMRAW"] = FIF_PPMRAW; |
| 130 |
m_freeiamge_formats["RAS"] = FIF_RAS; |
| 131 |
m_freeiamge_formats["TGA"] = FIF_TARGA; |
| 132 |
m_freeiamge_formats["TARGA"] = FIF_TARGA; |
| 133 |
m_freeiamge_formats["TIFF"] = FIF_TIFF;//use qt |
| 134 |
m_freeiamge_formats["TIF"] = FIF_TIFF;//use qt |
| 135 |
m_freeiamge_formats["WBMP"] = FIF_WBMP; |
| 136 |
m_freeiamge_formats["PSD"] = FIF_PSD; |
| 137 |
m_freeiamge_formats["CUT"] = FIF_CUT; |
| 138 |
m_freeiamge_formats["XBM"] = FIF_XBM; |
| 139 |
m_freeiamge_formats["XPM"] = FIF_XPM; |
| 140 |
m_freeiamge_formats["DDS"] = FIF_DDS; |
| 141 |
m_freeiamge_formats["GIF"] = FIF_GIF; |
| 142 |
//m_freeiamge_formats["HDR"] = FIF_HDR;//FHDR covert failed |
| 143 |
m_freeiamge_formats["FAX"] = FIF_FAXG3; |
| 144 |
m_freeiamge_formats["G3"] = FIF_FAXG3;//FAXG3 |
| 145 |
m_freeiamge_formats["SGI"] = FIF_SGI; |
| 146 |
m_freeiamge_formats["EXR"] = FIF_EXR; |
| 147 |
//m_freeiamge_formats["J2K"] = FIF_J2K; |
| 148 |
m_freeiamge_formats["J2C"] = FIF_J2K; |
| 149 |
m_freeiamge_formats["JPC"] = FIF_J2K; |
| 150 |
//m_freeiamge_formats["JP2"] = FIF_JP2; |
| 151 |
//m_freeiamge_formats["PFM"] = FIF_PFM;covert failed |
| 152 |
m_freeiamge_formats["PCT"] = FIF_PICT; |
| 153 |
m_freeiamge_formats["PIC"] = FIF_PICT; |
| 154 |
m_freeiamge_formats["PICT"] = FIF_PICT; |
| 155 |
m_freeiamge_formats["PIC"] = FIF_PICT; |
| 156 |
m_freeiamge_formats["RAW"] = FIF_RAW; |
| 157 |
m_freeiamge_formats["WEBP"] = FIF_WEBP; |
| 158 |
m_freeiamge_formats["JXR"] = FIF_JXR; |
| 159 |
m_movie_formats["MNG"] = FIF_MNG; |
| 160 |
m_movie_formats["GIF"] = FIF_GIF; |
| 161 |
m_movie_formats["WEBP"] = FIF_WEBP; |
| 162 |
|
| 163 |
m_qtSupported << "BMP" << "JPG" << "JPEG" << "PNG" << "PBM" |
| 164 |
<< "PGM" << "PPM" << "PNM" << "WBMP" << "WEBP" |
| 165 |
<< "SVG" << "ICNS" << "GIF" << "MNG" << "TIF" |
| 166 |
<< "TIFF" << "BMP" << "XPM" << "MRW" << "DNG" |
| 167 |
<< "RAF" << "CR2" << "MEF" << "RAW" << "ORF" |
| 168 |
<< "NEF" ; |
| 169 |
|
| 170 |
m_canSave << "BMP" << "JPG" << "JPEG" << "PNG" << "PBM" |
| 171 |
<< "PGM" << "PPM" << "PNM" << "WBMP" << "WEBP" |
| 172 |
<< "SVG" << "TGA" << "XPM" << "ICO" << "J2C" |
| 173 |
<< "J2K" << "JNG" << "JP2" ; |
| 174 |
|
| 175 |
m_qtrotate << "ICNS" << "JPG" << "JPEG"; |
| 176 |
} |
| 177 |
~UnionImage_Private() |
| 178 |
{ |
| 179 |
|
| 180 |
} |
| 181 |
QMutex freeimage_mutex; |
| 182 |
QStringList m_qtSupported; |
| 183 |
QHash<QString, int> m_freeiamge_formats; |
| 184 |
QHash<QString, int> m_movie_formats; |
| 185 |
QStringList m_canSave; |
| 186 |
QStringList m_qtrotate; |
| 187 |
}; |
| 188 |
|
| 189 |
static UnionImage_Private union_image_private; |
| 190 |
|
| 191 |
FREE_IMAGE_FORMAT detectImageFormat_f(const QString &path); |
| 192 |
|
| 193 |
/** |
| 194 |
* @brief noneQImage |
| 195 |
* @return QImage |
| 196 |
* 返回空图片 |
| 197 |
*/ |
| 198 |
UNIONIMAGESHARED_EXPORT QImage noneQImage() |
| 199 |
{ |
| 200 |
static QImage none(0, 0, QImage::Format_Invalid); |
| 201 |
return none; |
| 202 |
} |
| 203 |
|
| 204 |
UNIONIMAGESHARED_EXPORT const QStringList unionImageSupportFormat() |
| 205 |
{ |
| 206 |
static QStringList res; |
| 207 |
if (res.empty()) { |
| 208 |
QStringList list = union_image_private.m_freeiamge_formats.keys(); |
| 209 |
for (const QString &i : union_image_private.m_freeiamge_formats.keys()) { |
| 210 |
if (!list.contains(i)) |
| 211 |
list.append(i.toLower()); |
| 212 |
} |
| 213 |
for (const QString &i : union_image_private.m_qtSupported) { |
| 214 |
if (!list.contains(i)) |
| 215 |
list.append(i); |
| 216 |
} |
| 217 |
for (const QString &i : union_image_private.m_qtSupported) { |
| 218 |
if (!list.contains(i)) |
| 219 |
list.append(i.toLower()); |
| 220 |
} |
| 221 |
res.append(list); |
| 222 |
res.append(union_image_private.m_movie_formats.keys()); |
| 223 |
} |
| 224 |
return res; |
| 225 |
} |
| 226 |
|
| 227 |
UNIONIMAGESHARED_EXPORT const QStringList supportStaticFormat() |
| 228 |
{ |
| 229 |
return (union_image_private.m_freeiamge_formats.keys() << union_image_private.m_qtSupported); |
| 230 |
} |
| 231 |
|
| 232 |
UNIONIMAGESHARED_EXPORT const QStringList supportMovieFormat() |
| 233 |
{ |
| 234 |
return (union_image_private.m_movie_formats.keys()); |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* @brief size2Human |
| 239 |
* @param bytes |
| 240 |
* @author LMH |
| 241 |
* @return QString |
| 242 |
* 照片尺寸转化为QString格式 |
| 243 |
*/ |
| 244 |
UNIONIMAGESHARED_EXPORT QString size2Human(const qlonglong bytes) |
| 245 |
{ |
| 246 |
qlonglong kb = 1024; |
| 247 |
if (bytes < kb) { |
| 248 |
return QString::number(bytes) + " B"; |
| 249 |
} else if (bytes < kb * kb) { |
| 250 |
QString vs = QString::number(static_cast<double>(bytes) / kb, 'f', 1); |
| 251 |
if (qCeil(vs.toDouble()) == qFloor(vs.toDouble())) { |
| 252 |
return QString::number(static_cast<int>(vs.toDouble())) + " KB"; |
| 253 |
} else { |
| 254 |
return vs + " KB"; |
| 255 |
} |
| 256 |
} else if (bytes < kb * kb * kb) { |
| 257 |
QString vs = QString::number(static_cast<double>(bytes) / kb / kb, 'f', 1); |
| 258 |
if (qCeil(vs.toDouble()) == qFloor(vs.toDouble())) { |
| 259 |
return QString::number(static_cast<int>(vs.toDouble())) + " MB"; |
| 260 |
} else { |
| 261 |
return vs + " MB"; |
| 262 |
} |
| 263 |
} else { |
| 264 |
//修改了当超过一个G的图片,应该用G返回,不应该返回一堆数字,bug68094 |
| 265 |
QString vs = QString::number(static_cast<double>(bytes) / kb / kb / kb, 'f', 1); |
| 266 |
if (qCeil(vs.toDouble()) == qFloor(vs.toDouble())) { |
| 267 |
return QString::number(static_cast<int>(vs.toDouble())) + " GB"; |
| 268 |
} else { |
| 269 |
return vs + " GB"; |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* @brief getFileFormat |
| 276 |
* @param path |
| 277 |
* @author LMH |
| 278 |
* @return QString |
| 279 |
* 文件路径获取文件后缀名 |
| 280 |
*/ |
| 281 |
UNIONIMAGESHARED_EXPORT const QString getFileFormat(const QString &path) |
| 282 |
{ |
| 283 |
QFileInfo fi(path); |
| 284 |
QString suffix = fi.suffix(); |
| 285 |
return suffix; |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* @brief string2DateTime |
| 290 |
* @param time |
| 291 |
* @author LMH |
| 292 |
* @return QDateTime |
| 293 |
* string转换时间 |
| 294 |
*/ |
| 295 |
UNIONIMAGESHARED_EXPORT QDateTime string2DateTime(const QString &time) |
| 296 |
{ |
| 297 |
QDateTime dt = QDateTime::fromString(time, DATETIME_FORMAT_EXIF); |
| 298 |
if (! dt.isValid()) { |
| 299 |
dt = QDateTime::fromString(time, DATETIME_FORMAT_NORMAL); |
| 300 |
} |
| 301 |
return dt; |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* @brief getMetaData |
| 306 |
* @param model |
| 307 |
* @param dib |
| 308 |
* @author LMH |
| 309 |
* @return QMap<QString, QString> |
| 310 |
* 内部接口获取图片的信息 |
| 311 |
*/ |
| 312 |
UNIONIMAGESHARED_EXPORT QMap<QString, QString> getMetaData(FREE_IMAGE_MDMODEL model, FIBITMAP *dib) |
| 313 |
{ |
| 314 |
QMap<QString, QString> mdMap; // key-data |
| 315 |
FITAG *tag = nullptr; |
| 316 |
FIMETADATA *mdhandle = nullptr; |
| 317 |
mdhandle = FreeImage_FindFirstMetadata(model, dib, &tag); |
| 318 |
if (mdhandle) { |
| 319 |
do { |
| 320 |
mdMap.insert(FreeImage_GetTagKey(tag), |
| 321 |
FreeImage_TagToString(model, tag)); |
| 322 |
} while (FreeImage_FindNextMetadata(mdhandle, &tag)); |
| 323 |
FreeImage_FindCloseMetadata(mdhandle); |
| 324 |
} |
| 325 |
return mdMap; |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* @brief FIBitmapToQImage |
| 330 |
* @param dib |
| 331 |
* @return QImage |
| 332 |
* 由FreeImage转到QImage |
| 333 |
*/ |
| 334 |
UNIONIMAGESHARED_EXPORT QImage FIBitmap2QImage(FIBITMAP *dib) |
| 335 |
{ |
| 336 |
if (!dib || FreeImage_GetImageType(dib) == FIT_UNKNOWN) |
| 337 |
return noneQImage(); |
| 338 |
int width = static_cast<int>(FreeImage_GetWidth(dib)); |
| 339 |
int height = static_cast<int>(FreeImage_GetHeight(dib)); |
| 340 |
int depth = static_cast<int>(FreeImage_GetBPP(dib)); |
| 341 |
switch (depth) { |
| 342 |
case 1: { |
| 343 |
QImage result(width, height, QImage::Format_Mono); |
| 344 |
FreeImage_ConvertToRawBits( |
| 345 |
result.scanLine(0), dib, result.bytesPerLine(), 1, 0, 0, 0, true |
| 346 |
); |
| 347 |
return result; |
| 348 |
} |
| 349 |
case 4: { /* NOTE: QImage do not support 4-bit, convert it to 8-bit */ |
| 350 |
QImage result(width, height, QImage::Format_Indexed8); |
| 351 |
FreeImage_ConvertToRawBits( |
| 352 |
result.scanLine(0), dib, result.bytesPerLine(), 8, 0, 0, 0, true |
| 353 |
); |
| 354 |
return result; |
| 355 |
} |
| 356 |
case 8: { |
| 357 |
QImage result(width, height, QImage::Format_Indexed8); |
| 358 |
FreeImage_ConvertToRawBits( |
| 359 |
result.scanLine(0), dib, result.bytesPerLine(), 8, 0, 0, 0, true |
| 360 |
); |
| 361 |
return result; |
| 362 |
} |
| 363 |
case 16: |
| 364 |
if ( // 5-5-5 |
| 365 |
(FreeImage_GetRedMask(dib) == FI16_555_RED_MASK) && |
| 366 |
(FreeImage_GetGreenMask(dib) == FI16_555_GREEN_MASK) && |
| 367 |
(FreeImage_GetBlueMask(dib) == FI16_555_BLUE_MASK)) { |
| 368 |
QImage result(width, height, QImage::Format_RGB555); |
| 369 |
FreeImage_ConvertToRawBits( |
| 370 |
result.scanLine(0), dib, result.bytesPerLine(), 16, |
| 371 |
FI16_555_RED_MASK, FI16_555_GREEN_MASK, FI16_555_BLUE_MASK, |
| 372 |
true |
| 373 |
); |
| 374 |
return result; |
| 375 |
} else { // 5-6-5 |
| 376 |
QImage result(width, height, QImage::Format_RGB16); |
| 377 |
FreeImage_ConvertToRawBits( |
| 378 |
result.scanLine(0), dib, result.bytesPerLine(), 16, |
| 379 |
FI16_565_RED_MASK, FI16_565_GREEN_MASK, FI16_565_BLUE_MASK, |
| 380 |
true |
| 381 |
); |
| 382 |
return result; |
| 383 |
} |
| 384 |
case 24: { |
| 385 |
QImage result(width, height, QImage::Format_RGB32); |
| 386 |
FreeImage_ConvertToRawBits( |
| 387 |
result.scanLine(0), dib, result.bytesPerLine(), 32, |
| 388 |
FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, |
| 389 |
true |
| 390 |
); |
| 391 |
return result; |
| 392 |
} |
| 393 |
case 32: { |
| 394 |
QImage result(width, height, QImage::Format_ARGB32); |
| 395 |
FreeImage_ConvertToRawBits( |
| 396 |
result.scanLine(0), dib, result.bytesPerLine(), 32, |
| 397 |
FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, |
| 398 |
true |
| 399 |
); |
| 400 |
return result; |
| 401 |
} |
| 402 |
case 48: |
| 403 |
case 64: |
| 404 |
case 96: |
| 405 |
//128位暂不支持 |
| 406 |
case 128: |
| 407 |
default: |
| 408 |
break; |
| 409 |
} |
| 410 |
return noneQImage(); |
| 411 |
} |
| 412 |
|
| 413 |
/** |
| 414 |
* @brief QImgeToFIBitMap |
| 415 |
* @param img |
| 416 |
* @return FIBITMAP* |
| 417 |
* 由QImage转到FreeImage |
| 418 |
*/ |
| 419 |
|
| 420 |
UNIONIMAGESHARED_EXPORT FIBITMAP *QImge2FIBitMap(QImage img) |
| 421 |
{ |
| 422 |
if (img.isNull()) { |
| 423 |
return nullptr; |
| 424 |
} |
| 425 |
int width = img.width(); |
| 426 |
int height = img.height(); |
| 427 |
uint depth = static_cast<uint>(img.depth()); |
| 428 |
QImage::Format format = img.format(); |
| 429 |
switch (format) { |
| 430 |
//The image is invalid. |
| 431 |
case QImage::Format_Invalid: |
| 432 |
return nullptr; |
| 433 |
//The image is stored using 1-bit per pixel.(MSB) |
| 434 |
case QImage::Format_Mono: |
| 435 |
//The image is stored using 1-bit per pixel.(LSB) |
| 436 |
case QImage::Format_MonoLSB: { |
| 437 |
FIBITMAP *res = FreeImage_ConvertFromRawBits( |
| 438 |
img.scanLine(0), width, height, img.bytesPerLine(), depth, 0, 0, 0, |
| 439 |
true); |
| 440 |
return res; |
| 441 |
} |
| 442 |
/* NOTE: QImage do not support 4-bit*/ |
| 443 |
|
| 444 |
//可能会将彩色图转为灰度图 |
| 445 |
//The image is stored using an 8-bit alpha only format. |
| 446 |
case QImage::Format_Alpha8: |
| 447 |
//The image is stored using an 8-bit grayscale format. |
| 448 |
case QImage::Format_Grayscale8: |
| 449 |
//The image is stored using 8-bit indexes into a colormap. |
| 450 |
case QImage::Format_Indexed8: { |
| 451 |
FIBITMAP *res = FreeImage_ConvertFromRawBits( |
| 452 |
img.scanLine(0), width, height, img.bytesPerLine(), depth, 0, 0, 0, |
| 453 |
true); |
| 454 |
return res; |
| 455 |
} |
| 456 |
//The image is stored using a 16-bit RGB format (5-6-5) |
| 457 |
case QImage::Format_RGB16: { |
| 458 |
FIBITMAP *res = FreeImage_ConvertFromRawBits( |
| 459 |
img.scanLine(0), width, height, img.bytesPerLine(), 16, |
| 460 |
FI16_565_RED_MASK, FI16_565_GREEN_MASK, FI16_565_BLUE_MASK, |
| 461 |
true); |
| 462 |
return res; |
| 463 |
} |
| 464 |
//The image is stored using a 16-bit RGB format (5-5-5). The unused most significant bit is always zero. |
| 465 |
case QImage::Format_RGB555: { |
| 466 |
FIBITMAP *res = FreeImage_ConvertFromRawBits( |
| 467 |
img.scanLine(0), width, height, img.bytesPerLine(), 16, |
| 468 |
FI16_555_RED_MASK, FI16_555_GREEN_MASK, FI16_555_BLUE_MASK, |
| 469 |
true); |
| 470 |
return res; |
| 471 |
} |
| 472 |
|
| 473 |
//32-bit |
| 474 |
case QImage::Format_RGB32: { |
| 475 |
FIBITMAP *res = FreeImage_ConvertFromRawBits( |
| 476 |
img.scanLine(0), width, height, img.bytesPerLine(), depth, |
| 477 |
FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, |
| 478 |
true); |
| 479 |
return res; |
| 480 |
} |
| 481 |
case QImage::Format_ARGB32: { |
| 482 |
FIBITMAP *res = FreeImage_ConvertFromRawBits( |
| 483 |
img.scanLine(0), width, height, img.bytesPerLine(), depth, |
| 484 |
FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, |
| 485 |
true); |
| 486 |
return res; |
| 487 |
} |
| 488 |
//24-bit |
| 489 |
case QImage::Format_RGB666: |
| 490 |
case QImage::Format_ARGB8555_Premultiplied: |
| 491 |
case QImage::Format_ARGB8565_Premultiplied: |
| 492 |
case QImage::Format_ARGB6666_Premultiplied: |
| 493 |
case QImage::Format_RGB888: { |
| 494 |
FIBITMAP *res = FreeImage_ConvertFromRawBits( |
| 495 |
img.scanLine(0), width, height, img.bytesPerLine(), depth, |
| 496 |
FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, |
| 497 |
true); |
| 498 |
return res; |
| 499 |
} |
| 500 |
|
| 501 |
//FreeImage is not support |
| 502 |
//The image is stored using a 16-bit RGB format (4-4-4). The unused bits are always zero. |
| 503 |
case QImage::Format_RGB444: |
| 504 |
case QImage::Format_ARGB4444_Premultiplied: |
| 505 |
default: |
| 506 |
break; |
| 507 |
} |
| 508 |
return nullptr; |
| 509 |
} |
| 510 |
|
| 511 |
/** |
| 512 |
* @brief readFile2FIBITMAP |
| 513 |
* @param path |
| 514 |
* @param flags |
| 515 |
* @author LMH |
| 516 |
* @return FIBITMAP |
| 517 |
* 由QString路径转换成FIBITMAP指针 |
| 518 |
*/ |
| 519 |
UNIONIMAGESHARED_EXPORT FIBITMAP *readFile2FIBITMAP(const QString &path, int flags FI_DEFAULT(0)) |
| 520 |
{ |
| 521 |
QByteArray b; |
| 522 |
b.append(path); |
| 523 |
const char *pc = b.data(); |
| 524 |
QString().toStdString(); |
| 525 |
const FREE_IMAGE_FORMAT fif = detectImageFormat_f(path); |
| 526 |
if ((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)) { |
| 527 |
FIBITMAP *dib = FreeImage_Load(fif, pc, flags); |
| 528 |
return dib; |
| 529 |
} |
| 530 |
return nullptr; |
| 531 |
} |
| 532 |
|
| 533 |
UNIONIMAGESHARED_EXPORT bool canSave(const QString &path) |
| 534 |
{ |
| 535 |
QFileInfo info(path); |
| 536 |
if (!info.exists() || !QFile::permissions(path).testFlag(QFile::WriteUser)) { |
| 537 |
return false; |
| 538 |
} |
| 539 |
QImageReader r(path); |
| 540 |
if (r.imageCount() > 1) { |
| 541 |
return false; |
| 542 |
} |
| 543 |
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; |
| 544 |
// Try to guess the file format from the file extension |
| 545 |
fif = FreeImage_GetFIFFromFilename(path.toUtf8().data()); |
| 546 |
if (fif != FIF_UNKNOWN) { |
| 547 |
// Check that the dib can be saved in this format |
| 548 |
if (union_image_private.m_canSave.contains(union_image_private.m_freeiamge_formats.key(fif))) { |
| 549 |
return true; |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
if (union_image_private.m_canSave.contains(info.suffix().toUpper())) |
| 554 |
return true; |
| 555 |
return false; |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
/** |
| 561 |
* @brief writeFIBITMAPToFile |
| 562 |
* @param dib |
| 563 |
* @param path |
| 564 |
* @param flag |
| 565 |
* @return |
| 566 |
*/ |
| 567 |
UNIONIMAGESHARED_EXPORT bool writeFIBITMAPToFile(FIBITMAP *dib, const QString &path, FREE_IMAGE_FORMAT fif, int flag = 0) |
| 568 |
{ |
| 569 |
BOOL bSuccess = FALSE; |
| 570 |
const QByteArray ba = path.toUtf8(); |
| 571 |
const char *pc = ba.data(); |
| 572 |
if (fif != FIF_UNKNOWN) { |
| 573 |
bSuccess = FreeImage_Save(fif, dib, pc, flag); |
| 574 |
} |
| 575 |
return bSuccess; |
| 576 |
} |
| 577 |
|
| 578 |
UNIONIMAGESHARED_EXPORT QString unionImageVersion() |
| 579 |
{ |
| 580 |
QString ver; |
| 581 |
ver.append("UnionImage Version:"); |
| 582 |
ver.append("0.0.4"); |
| 583 |
ver.append("\n"); |
| 584 |
return ver; |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
UNIONIMAGESHARED_EXPORT bool creatNewImage(QImage &res, int width, int height, int depth, SupportType type) |
| 590 |
{ |
| 591 |
Q_UNUSED(type); |
| 592 |
FIBITMAP *dib = FreeImage_Allocate(width, height, depth); |
| 593 |
res = FIBitmap2QImage(dib); |
| 594 |
FreeImage_Unload(dib); |
| 595 |
return true; |
| 596 |
} |
| 597 |
|
| 598 |
QString PrivateDetectImageFormat(const QString &filepath); |
| 599 |
UNIONIMAGESHARED_EXPORT bool loadStaticImageFromFile(const QString &path, QImage &res, QString &errorMsg, const QString &format_bar) |
| 600 |
{ |
| 601 |
QFileInfo file_info(path); |
| 602 |
if (file_info.size() == 0) { |
| 603 |
res = QImage(); |
| 604 |
errorMsg = "error file!"; |
| 605 |
return false; |
| 606 |
} |
| 607 |
QString file_suffix_upper = file_info.suffix().toUpper(); |
| 608 |
QByteArray temp_path; |
| 609 |
temp_path.append(path.toUtf8()); |
| 610 |
FREE_IMAGE_FORMAT f = FreeImage_GetFileType(temp_path.data()); |
| 611 |
if (f != FIF_UNKNOWN && f != union_image_private.m_freeiamge_formats[file_suffix_upper]) { |
| 612 |
file_suffix_upper = union_image_private.m_freeiamge_formats.key(f); |
| 613 |
} |
| 614 |
if (f == FIF_TIFF) { |
| 615 |
file_suffix_upper = "TIFF"; |
| 616 |
} |
| 617 |
QString file_suffix_lower = file_suffix_upper.toLower(); |
| 618 |
if (union_image_private.m_qtSupported.contains(file_suffix_upper)) { |
| 619 |
QImageReader reader; |
| 620 |
QImage res_qt; |
| 621 |
reader.setFileName(path); |
| 622 |
if (format_bar.isEmpty()) { |
| 623 |
reader.setFormat(file_suffix_lower.toLatin1()); |
| 624 |
} else { |
| 625 |
reader.setFormat(format_bar.toLatin1()); |
| 626 |
} |
| 627 |
reader.setAutoTransform(true); |
| 628 |
if (reader.imageCount() > 0 || file_suffix_upper != "ICNS") { |
| 629 |
res_qt = reader.read(); |
| 630 |
if (res_qt.isNull()) { |
| 631 |
//try old loading method |
| 632 |
QString format = PrivateDetectImageFormat(path); |
| 633 |
QImageReader readerF(path, format.toLatin1()); |
| 634 |
QImage try_res; |
| 635 |
readerF.setAutoTransform(true); |
| 636 |
if (readerF.canRead() && readerF.imageCount() > 0) { |
| 637 |
try_res = readerF.read(); |
| 638 |
} else { |
| 639 |
errorMsg = "can't read image:" + readerF.errorString() + format; |
| 640 |
try_res = QImage(); |
| 641 |
} |
| 642 |
if (try_res.isNull()) { |
| 643 |
errorMsg = "load image by qt faild, use format:" + reader.format() + " ,path:" + path; |
| 644 |
res = QImage(); |
| 645 |
return false; |
| 646 |
} |
| 647 |
errorMsg = "use old method to load QImage"; |
| 648 |
res = try_res; |
| 649 |
return true; |
| 650 |
} |
| 651 |
} |
| 652 |
errorMsg = "use QImage"; |
| 653 |
res = res_qt; |
| 654 |
return true; |
| 655 |
} else { |
| 656 |
if (f != FREE_IMAGE_FORMAT::FIF_UNKNOWN || union_image_private.m_freeiamge_formats.contains(file_suffix_upper)) { |
| 657 |
if (f == FREE_IMAGE_FORMAT::FIF_UNKNOWN) |
| 658 |
f = FREE_IMAGE_FORMAT(union_image_private.m_freeiamge_formats[file_suffix_upper]); |
| 659 |
FIBITMAP *dib = FreeImage_Load(f, temp_path.data()); |
| 660 |
if (nullptr == dib) { |
| 661 |
errorMsg = "image load faild, format:" + union_image_private.m_freeiamge_formats.key(f) + " ,path:" + temp_path; |
| 662 |
//FreeImage_Unload(dib); |
| 663 |
res = QImage(); |
| 664 |
return false; |
| 665 |
} |
| 666 |
// uint depth = FreeImage_GetBPP(dib); //just for test |
| 667 |
// Q_UNUSED(depth); |
| 668 |
//32位以上图片qImage不支持,强行读取和转换可能会乱码 |
| 669 |
res = QImage(FIBitmap2QImage(dib)); |
| 670 |
if (res.isNull()) { |
| 671 |
errorMsg = "convert to QImage faild" + union_image_private.m_freeiamge_formats.key(f) + " ,path:" + temp_path; |
| 672 |
FreeImage_Unload(dib); |
| 673 |
res = QImage(); |
| 674 |
return false; |
| 675 |
} |
| 676 |
FreeImage_Unload(dib); |
| 677 |
errorMsg = ""; |
| 678 |
return true; |
| 679 |
} |
| 680 |
return false; |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
UNIONIMAGESHARED_EXPORT QString detectImageFormat(const QString &path) |
| 685 |
{ |
| 686 |
QFileInfo file_info(path); |
| 687 |
QString file_suffix_upper = file_info.suffix().toUpper(); |
| 688 |
QByteArray temp_path; |
| 689 |
temp_path.append(path.toUtf8()); |
| 690 |
FREE_IMAGE_FORMAT f = FreeImage_GetFileType(temp_path.data()); |
| 691 |
if (f != FIF_UNKNOWN && f != union_image_private.m_freeiamge_formats[file_suffix_upper]) { |
| 692 |
file_suffix_upper = union_image_private.m_freeiamge_formats.key(f); |
| 693 |
} |
| 694 |
if (f == FIF_TIFF) { |
| 695 |
file_suffix_upper = "TIFF"; |
| 696 |
} |
| 697 |
QString res = file_suffix_upper; |
| 698 |
if (res.isEmpty()) { |
| 699 |
QFile file(path); |
| 700 |
if (!file.open(QIODevice::ReadOnly)) { |
| 701 |
return ""; |
| 702 |
} |
| 703 |
|
| 704 |
// const QByteArray data = file.read(1024); |
| 705 |
const QByteArray data = file.read(64); |
| 706 |
|
| 707 |
// Check bmp file. |
| 708 |
if (data.startsWith("BM")) { |
| 709 |
return "BMP"; |
| 710 |
} |
| 711 |
|
| 712 |
// Check dds file. |
| 713 |
if (data.startsWith("DDS")) { |
| 714 |
return "DDS"; |
| 715 |
} |
| 716 |
|
| 717 |
// Check gif file. |
| 718 |
if (data.startsWith("GIF8")) { |
| 719 |
return "GIF"; |
| 720 |
} |
| 721 |
|
| 722 |
// Check Max OS icons file. |
| 723 |
if (data.startsWith("icns")) { |
| 724 |
return "ICNS"; |
| 725 |
} |
| 726 |
|
| 727 |
// Check jpeg file. |
| 728 |
if (data.startsWith("\xff\xd8")) { |
| 729 |
return "JPG"; |
| 730 |
} |
| 731 |
|
| 732 |
// Check mng file. |
| 733 |
if (data.startsWith("\x8a\x4d\x4e\x47\x0d\x0a\x1a\x0a")) { |
| 734 |
return "MNG"; |
| 735 |
} |
| 736 |
|
| 737 |
// Check net pbm file (BitMap). |
| 738 |
if (data.startsWith("P1") || data.startsWith("P4")) { |
| 739 |
return "PBM"; |
| 740 |
} |
| 741 |
|
| 742 |
// Check pgm file (GrayMap). |
| 743 |
if (data.startsWith("P2") || data.startsWith("P5")) { |
| 744 |
return "PGM"; |
| 745 |
} |
| 746 |
|
| 747 |
// Check ppm file (PixMap). |
| 748 |
if (data.startsWith("P3") || data.startsWith("P6")) { |
| 749 |
return "PPM"; |
| 750 |
} |
| 751 |
|
| 752 |
// Check png file. |
| 753 |
if (data.startsWith("\x89PNG\x0d\x0a\x1a\x0a")) { |
| 754 |
return "PNG"; |
| 755 |
} |
| 756 |
|
| 757 |
// Check svg file. |
| 758 |
if (data.indexOf("<svg") > -1) { |
| 759 |
return "SVG"; |
| 760 |
} |
| 761 |
|
| 762 |
// TODO(xushaohua): tga file is not supported yet. |
| 763 |
|
| 764 |
// Check tiff file. |
| 765 |
if (data.startsWith("MM\x00\x2a") || data.startsWith("II\x2a\x00")) { |
| 766 |
// big-endian, little-endian. |
| 767 |
return "TIFF"; |
| 768 |
} |
| 769 |
|
| 770 |
// TODO(xushaohua): Support wbmp file. |
| 771 |
|
| 772 |
// Check webp file. |
| 773 |
if (data.startsWith("RIFFr\x00\x00\x00WEBPVP")) { |
| 774 |
return "WEBP"; |
| 775 |
} |
| 776 |
|
| 777 |
// Check xbm file. |
| 778 |
if (data.indexOf("#define max_width ") > -1 && |
| 779 |
data.indexOf("#define max_height ") > -1) { |
| 780 |
return "XBM"; |
| 781 |
} |
| 782 |
|
| 783 |
// Check xpm file. |
| 784 |
if (data.startsWith("/* XPM */")) { |
| 785 |
return "XPM"; |
| 786 |
} |
| 787 |
return ""; |
| 788 |
} |
| 789 |
return res; |
| 790 |
} |
| 791 |
|
| 792 |
FREE_IMAGE_FORMAT detectImageFormat_f(const QString &path) |
| 793 |
{ |
| 794 |
QFileInfo file_info(path); |
| 795 |
QString file_suffix_upper = file_info.suffix().toUpper(); |
| 796 |
QByteArray temp_path; |
| 797 |
temp_path.append(path.toUtf8()); |
| 798 |
FREE_IMAGE_FORMAT f = FreeImage_GetFileType(temp_path.data()); |
| 799 |
if (f != FIF_UNKNOWN && f != union_image_private.m_freeiamge_formats[file_suffix_upper]) { |
| 800 |
file_suffix_upper = union_image_private.m_freeiamge_formats.key(f); |
| 801 |
} |
| 802 |
if (f == FIF_TIFF) { |
| 803 |
file_suffix_upper = "TIFF"; |
| 804 |
} |
| 805 |
if (file_suffix_upper.isEmpty()) { |
| 806 |
QFile file(path); |
| 807 |
if (!file.open(QIODevice::ReadOnly)) { |
| 808 |
return FIF_UNKNOWN; |
| 809 |
} |
| 810 |
|
| 811 |
// const QByteArray data = file.read(1024); |
| 812 |
const QByteArray data = file.read(64); |
| 813 |
|
| 814 |
// Check bmp file. |
| 815 |
if (data.startsWith("BM")) { |
| 816 |
return FIF_BMP; |
| 817 |
} |
| 818 |
|
| 819 |
// Check dds file. |
| 820 |
if (data.startsWith("DDS")) { |
| 821 |
return FIF_DDS; |
| 822 |
} |
| 823 |
|
| 824 |
// Check gif file. |
| 825 |
if (data.startsWith("GIF8")) { |
| 826 |
return FIF_GIF; |
| 827 |
} |
| 828 |
|
| 829 |
// Check Max OS icons file. |
| 830 |
if (data.startsWith("icns")) { |
| 831 |
return FIF_UNKNOWN; |
| 832 |
} |
| 833 |
|
| 834 |
// Check jpeg file. |
| 835 |
if (data.startsWith("\xff\xd8")) { |
| 836 |
return FIF_JPEG; |
| 837 |
} |
| 838 |
|
| 839 |
// Check mng file. |
| 840 |
if (data.startsWith("\x8a\x4d\x4e\x47\x0d\x0a\x1a\x0a")) { |
| 841 |
return FIF_MNG; |
| 842 |
} |
| 843 |
|
| 844 |
// Check net pbm file (BitMap). |
| 845 |
if (data.startsWith("P1") || data.startsWith("P4")) { |
| 846 |
return FIF_PBM; |
| 847 |
} |
| 848 |
|
| 849 |
// Check pgm file (GrayMap). |
| 850 |
if (data.startsWith("P2") || data.startsWith("P5")) { |
| 851 |
return FIF_PGM; |
| 852 |
} |
| 853 |
|
| 854 |
// Check ppm file (PixMap). |
| 855 |
if (data.startsWith("P3") || data.startsWith("P6")) { |
| 856 |
return FIF_PPM; |
| 857 |
} |
| 858 |
|
| 859 |
// Check png file. |
| 860 |
if (data.startsWith("\x89PNG\x0d\x0a\x1a\x0a")) { |
| 861 |
return FIF_PNG; |
| 862 |
} |
| 863 |
|
| 864 |
// Check svg file. |
| 865 |
if (data.indexOf("<svg") > -1) { |
| 866 |
return FIF_UNKNOWN; |
| 867 |
} |
| 868 |
|
| 869 |
// TODO(xushaohua): tga file is not supported yet. |
| 870 |
|
| 871 |
// Check tiff file. |
| 872 |
if (data.startsWith("MM\x00\x2a") || data.startsWith("II\x2a\x00")) { |
| 873 |
// big-endian, little-endian. |
| 874 |
return FIF_TIFF; |
| 875 |
} |
| 876 |
|
| 877 |
// TODO(xushaohua): Support wbmp file. |
| 878 |
|
| 879 |
// Check webp file. |
| 880 |
if (data.startsWith("RIFFr\x00\x00\x00WEBPVP")) { |
| 881 |
return FIF_WEBP; |
| 882 |
} |
| 883 |
|
| 884 |
// Check xbm file. |
| 885 |
if (data.indexOf("#define max_width ") > -1 && |
| 886 |
data.indexOf("#define max_height ") > -1) { |
| 887 |
return FIF_XBM; |
| 888 |
} |
| 889 |
|
| 890 |
// Check xpm file. |
| 891 |
if (data.startsWith("/* XPM */")) { |
| 892 |
return FIF_XPM; |
| 893 |
} |
| 894 |
return FIF_UNKNOWN; |
| 895 |
} |
| 896 |
f = static_cast<FREE_IMAGE_FORMAT>(union_image_private.m_freeiamge_formats[file_suffix_upper]); |
| 897 |
return f >= 0 ? f : FIF_UNKNOWN; |
| 898 |
} |
| 899 |
|
| 900 |
UNIONIMAGESHARED_EXPORT bool isNoneQImage(const QImage &qi) |
| 901 |
{ |
| 902 |
return (qi == noneQImage()); |
| 903 |
} |
| 904 |
|
| 905 |
UNIONIMAGESHARED_EXPORT bool rotateImage(int angel, QImage &image) |
| 906 |
{ |
| 907 |
if (angel % 90 != 0) |
| 908 |
return false; |
| 909 |
if (image.isNull()) { |
| 910 |
return false; |
| 911 |
} |
| 912 |
QImage image_copy(image); |
| 913 |
if (!image_copy.isNull()) { |
| 914 |
QMatrix rotatematrix; |
| 915 |
rotatematrix.rotate(angel); |
| 916 |
image = image_copy.transformed(rotatematrix, Qt::SmoothTransformation); |
| 917 |
return true; |
| 918 |
} |
| 919 |
return false; |
| 920 |
} |
| 921 |
|
| 922 |
UNIONIMAGESHARED_EXPORT bool rotateImageFile(int angel, const QString &path, QString &erroMsg) |
| 923 |
{ |
| 924 |
if (angel % 90 != 0) { |
| 925 |
erroMsg = "unsupported angel"; |
| 926 |
return false; |
| 927 |
} |
| 928 |
QString format = detectImageFormat(path); |
| 929 |
if (format == "SVG") { |
| 930 |
QImage image_copy; |
| 931 |
if (!loadStaticImageFromFile(path, image_copy, erroMsg)) { |
| 932 |
erroMsg = "rotate load QImage faild, path:" + path + " ,format:+" + format; |
| 933 |
return false; |
| 934 |
} |
| 935 |
QPixmap svgpixmap(path); |
| 936 |
QMatrix matrix; |
| 937 |
matrix.rotate(angel); |
| 938 |
svgpixmap = svgpixmap.transformed(QTransform(matrix)); |
| 939 |
QSvgGenerator generator; |
| 940 |
generator.setFileName(path); |
| 941 |
generator.setViewBox(svgpixmap.rect()); |
| 942 |
QPainter rotatePainter; |
| 943 |
rotatePainter.begin(&generator); |
| 944 |
rotatePainter.drawPixmap(svgpixmap.rect(), svgpixmap); |
| 945 |
rotatePainter.end(); |
| 946 |
return true; |
| 947 |
} else if (union_image_private.m_qtrotate.contains(format)) { |
| 948 |
QPixmap image_copy(path); |
| 949 |
if (!image_copy.isNull()) { |
| 950 |
QMatrix rotatematrix; |
| 951 |
rotatematrix.rotate(angel); |
| 952 |
image_copy = image_copy.transformed(rotatematrix, Qt::SmoothTransformation); |
| 953 |
if (image_copy.save(path, format.toLatin1().data(), SAVE_QUAITY_VALUE)) |
| 954 |
return true; |
| 955 |
else { |
| 956 |
return false; |
| 957 |
} |
| 958 |
} |
| 959 |
erroMsg = "rotate by qt failed"; |
| 960 |
return false; |
| 961 |
} |
| 962 |
FIBITMAP *dib = readFile2FIBITMAP(path); |
| 963 |
if (nullptr == dib) { |
| 964 |
erroMsg = "unsupported format"; |
| 965 |
FreeImage_Unload(dib); |
| 966 |
return false; |
| 967 |
} |
| 968 |
FIBITMAP *rotateRes = FreeImage_Rotate(dib, -angel); |
| 969 |
if (rotateRes) { |
| 970 |
// Regenerate thumbnail if it's exits |
| 971 |
// Image formats that currently support thumbnail saving are |
| 972 |
// JPEG (JFIF formats), EXR, TGA and TIFF. |
| 973 |
if (FreeImage_GetThumbnail(dib)) { |
| 974 |
FIBITMAP *thumb = FreeImage_GetThumbnail(dib); |
| 975 |
FIBITMAP *rotateThumb = FreeImage_Rotate(thumb, -angel); |
| 976 |
FreeImage_SetThumbnail(rotateRes, rotateThumb); |
| 977 |
FreeImage_Unload(rotateThumb); |
| 978 |
} |
| 979 |
} |
| 980 |
QByteArray temp_path; |
| 981 |
temp_path.append(path); |
| 982 |
FREE_IMAGE_FORMAT f = FREE_IMAGE_FORMAT(union_image_private.m_freeiamge_formats[QFileInfo(path).suffix().toUpper()]); |
| 983 |
if (f == FREE_IMAGE_FORMAT::FIF_UNKNOWN) { |
| 984 |
erroMsg = "rotate image format error"; |
| 985 |
FreeImage_Unload(dib); |
| 986 |
FreeImage_Unload(rotateRes); |
| 987 |
return false; |
| 988 |
} |
| 989 |
if (!writeFIBITMAPToFile(rotateRes, path, f)) { |
| 990 |
erroMsg = "rotate image save faild, unkown format"; |
| 991 |
FreeImage_Unload(dib); |
| 992 |
FreeImage_Unload(rotateRes); |
| 993 |
return false; |
| 994 |
} |
| 995 |
FreeImage_Unload(dib); |
| 996 |
FreeImage_Unload(rotateRes); |
| 997 |
erroMsg = ""; |
| 998 |
return true; |
| 999 |
} |
| 1000 |
|
| 1001 |
UNIONIMAGESHARED_EXPORT bool rotateImageFileWithImage(int angel, QImage &img, const QString &path, QString &erroMsg) |
| 1002 |
{ |
| 1003 |
Q_UNUSED(img) |
| 1004 |
if (angel % 90 != 0) { |
| 1005 |
erroMsg = "unsupported angel"; |
| 1006 |
return false; |
| 1007 |
} |
| 1008 |
|
| 1009 |
QString format = detectImageFormat(path); |
| 1010 |
if (format == "SVG") { |
| 1011 |
QImage image_copy; |
| 1012 |
if (!loadStaticImageFromFile(path, image_copy, erroMsg)) { |
| 1013 |
erroMsg = "rotate load QImage faild, path:" + path + " ,format:+" + format; |
| 1014 |
return false; |
| 1015 |
} |
| 1016 |
QSvgGenerator generator; |
| 1017 |
generator.setFileName(path); |
| 1018 |
generator.setViewBox(QRect(0, 0, image_copy.width(), image_copy.height())); |
| 1019 |
QPainter rotatePainter; |
| 1020 |
rotatePainter.begin(&generator); |
| 1021 |
rotatePainter.resetTransform(); |
| 1022 |
rotatePainter.setRenderHint(QPainter::HighQualityAntialiasing, true); |
| 1023 |
int realangel = angel / 90; |
| 1024 |
if (realangel > 0) { |
| 1025 |
for (int i = 0; i < qAbs(realangel); i++) { |
| 1026 |
rotatePainter.translate(image_copy.width(), 0); |
| 1027 |
rotatePainter.rotate(90 * (realangel / qAbs(realangel))); |
| 1028 |
} |
| 1029 |
} else { |
| 1030 |
for (int i = 0; i < qAbs(realangel); i++) { |
| 1031 |
rotatePainter.translate(0, image_copy.height()); |
| 1032 |
rotatePainter.rotate(90 * (realangel / qAbs(realangel))); |
| 1033 |
} |
| 1034 |
} |
| 1035 |
rotatePainter.drawImage(image_copy.rect(), image_copy.scaled(image_copy.width(), image_copy.height())); |
| 1036 |
rotatePainter.resetTransform(); |
| 1037 |
generator.setSize(QSize(image_copy.width(), image_copy.height())); |
| 1038 |
rotatePainter.end(); |
| 1039 |
return true; |
| 1040 |
} else if (union_image_private.m_qtrotate.contains(format)) { |
| 1041 |
QPixmap image_copy(path); |
| 1042 |
if (!image_copy.isNull()) { |
| 1043 |
QMatrix rotatematrix; |
| 1044 |
rotatematrix.rotate(angel); |
| 1045 |
image_copy = image_copy.transformed(rotatematrix, Qt::SmoothTransformation); |
| 1046 |
return image_copy.save(path, format.toLatin1().data(), SAVE_QUAITY_VALUE); |
| 1047 |
} |
| 1048 |
erroMsg = "rotate by qt failed"; |
| 1049 |
return false; |
| 1050 |
} |
| 1051 |
FIBITMAP *dib = readFile2FIBITMAP(path); |
| 1052 |
if (nullptr == dib) { |
| 1053 |
erroMsg = "unsupported format"; |
| 1054 |
FreeImage_Unload(dib); |
| 1055 |
return false; |
| 1056 |
} |
| 1057 |
FIBITMAP *rotateRes = FreeImage_Rotate(dib, -angel); |
| 1058 |
if (rotateRes) { |
| 1059 |
// Regenerate thumbnail if it's exits |
| 1060 |
// Image formats that currently support thumbnail saving are |
| 1061 |
// JPEG (JFIF formats), EXR, TGA and TIFF. |
| 1062 |
if (FreeImage_GetThumbnail(dib)) { |
| 1063 |
FIBITMAP *thumb = FreeImage_GetThumbnail(dib); |
| 1064 |
FIBITMAP *rotateThumb = FreeImage_Rotate(thumb, -angel); |
| 1065 |
FreeImage_SetThumbnail(rotateRes, rotateThumb); |
| 1066 |
FreeImage_Unload(rotateThumb); |
| 1067 |
} |
| 1068 |
} |
| 1069 |
QByteArray temp_path; |
| 1070 |
temp_path.append(path); |
| 1071 |
FREE_IMAGE_FORMAT f = FREE_IMAGE_FORMAT(union_image_private.m_freeiamge_formats[QFileInfo(path).suffix().toUpper()]); |
| 1072 |
if (f == FREE_IMAGE_FORMAT::FIF_UNKNOWN) { |
| 1073 |
erroMsg = "rotate image format error"; |
| 1074 |
FreeImage_Unload(dib); |
| 1075 |
FreeImage_Unload(rotateRes); |
| 1076 |
return false; |
| 1077 |
} |
| 1078 |
|
| 1079 |
if (!writeFIBITMAPToFile(rotateRes, path, f)) { |
| 1080 |
erroMsg = "rotate image save faild, unkown format"; |
| 1081 |
FreeImage_Unload(dib); |
| 1082 |
FreeImage_Unload(rotateRes); |
| 1083 |
return false; |
| 1084 |
} |
| 1085 |
FreeImage_Unload(dib); |
| 1086 |
FreeImage_Unload(rotateRes); |
| 1087 |
erroMsg = ""; |
| 1088 |
return true; |
| 1089 |
} |
| 1090 |
|
| 1091 |
UNIONIMAGESHARED_EXPORT QMap<QString, QString> getAllMetaData(const QString &path) |
| 1092 |
{ |
| 1093 |
//QMutexLocker mutex(&union_image_private.freeimage_mutex); |
| 1094 |
|
| 1095 |
//qDebug() << "threadid:" << QThread::currentThread() << "getAllMetaData locking ...."; |
| 1096 |
|
| 1097 |
FIBITMAP *dib = readFile2FIBITMAP(path, FIF_LOAD_NOPIXELS); |
| 1098 |
QMap<QString, QString> admMap; |
| 1099 |
admMap.unite(getMetaData(FIMD_EXIF_MAIN, dib)); |
| 1100 |
admMap.unite(getMetaData(FIMD_EXIF_EXIF, dib)); |
| 1101 |
admMap.unite(getMetaData(FIMD_EXIF_GPS, dib)); |
| 1102 |
admMap.unite(getMetaData(FIMD_EXIF_MAKERNOTE, dib)); |
| 1103 |
admMap.unite(getMetaData(FIMD_EXIF_INTEROP, dib)); |
| 1104 |
admMap.unite(getMetaData(FIMD_IPTC, dib)); |
| 1105 |
//移除秒 2020/6/5 DJH |
| 1106 |
//需要转义才能读出:或者/ 2020/8/21 DJH |
| 1107 |
QFileInfo info(path); |
| 1108 |
if (admMap.contains("DateTime")) { |
| 1109 |
QDateTime time = QDateTime::fromString(admMap["DateTime"], "yyyy:MM:dd hh:mm:ss"); |
| 1110 |
admMap["DateTimeOriginal"] = time.toString("yyyy/MM/dd hh:mm"); |
| 1111 |
} else { |
| 1112 |
admMap.insert("DateTimeOriginal", info.lastModified().toString("yyyy/MM/dd HH:mm")); |
| 1113 |
} |
| 1114 |
admMap.insert("DateTimeDigitized", info.lastModified().toString("yyyy/MM/dd HH:mm")); |
| 1115 |
// if (admMap.isEmpty()) { |
| 1116 |
// QDateTime emptyTime(QDate(0, 0, 0), QTime(0, 0)); |
| 1117 |
// admMap.insert("DateTimeOriginal", emptyTime.toString("yyyy/MM/dd HH:mm")); |
| 1118 |
// admMap.insert("DateTimeDigitized", info.lastModified().toString("yyyy/MM/dd HH:mm")); |
| 1119 |
// } |
| 1120 |
/* else { |
| 1121 |
QString qsdto = admMap.value("DateTimeOriginal"); |
| 1122 |
QString qsdtd = admMap.value("DateTimeDigitized"); |
| 1123 |
QDateTime ot = QDateTime::fromString(qsdto, "yyyy/MM/dd HH:mm"); |
| 1124 |
QDateTime dt = QDateTime::fromString(qsdtd, "yyyy/MM/dd HH:mm"); |
| 1125 |
if (! ot.isValid()) { |
| 1126 |
// Exif version 0221 |
| 1127 |
QString qsdt = admMap.value("DateTime"); |
| 1128 |
ot = QDateTime::fromString(qsdt, "yyyy/MM/dd HH:mm"); |
| 1129 |
dt = ot; |
| 1130 |
|
| 1131 |
// NO valid date information |
| 1132 |
if (! ot.isValid()) { |
| 1133 |
// admMap.insert("DateTimeOriginal", info.created().toString("yyyy/MM/dd HH:mm:dd")); |
| 1134 |
admMap.insert("DateTimeOriginal", info.birthTime().toString("yyyy/MM/dd HH:mm")); |
| 1135 |
admMap.insert("DateTimeDigitized", info.lastModified().toString("yyyy/MM/dd HH:mm")); |
| 1136 |
} else { |
| 1137 |
admMap.insert("DateTimeOriginal", ot.toString("yyyy/MM/dd HH:mm")); |
| 1138 |
admMap.insert("DateTimeDigitized", dt.toString("yyyy/MM/dd HH:mm")); |
| 1139 |
} |
| 1140 |
} |
| 1141 |
}*/ |
| 1142 |
|
| 1143 |
// // The value of width and height might incorrect |
| 1144 |
QImageReader reader(path); |
| 1145 |
int w = reader.size().width(); |
| 1146 |
w = w > 0 ? w : static_cast<int>(FreeImage_GetWidth(dib)); |
| 1147 |
int h = reader.size().height(); |
| 1148 |
h = h > 0 ? h : static_cast<int>(FreeImage_GetHeight(dib)); |
| 1149 |
admMap.insert("Dimension", QString::number(w) + "x" + QString::number(h)); |
| 1150 |
|
| 1151 |
admMap.insert("FileName", info.fileName()); |
| 1152 |
admMap.insert("FileFormat", detectImageFormat(path)); |
| 1153 |
admMap.insert("FileSize", size2Human(info.size())); |
| 1154 |
FreeImage_Unload(dib); |
| 1155 |
|
| 1156 |
return admMap; |
| 1157 |
} |
| 1158 |
|
| 1159 |
UNIONIMAGESHARED_EXPORT bool isImageSupportRotate(const QString &path) |
| 1160 |
{ |
| 1161 |
return canSave(path); |
| 1162 |
} |
| 1163 |
|
| 1164 |
UNIONIMAGESHARED_EXPORT bool isSupportsReading(const QString &path) |
| 1165 |
{ |
| 1166 |
const FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(path.toUtf8().data()); |
| 1167 |
return (fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif); |
| 1168 |
} |
| 1169 |
|
| 1170 |
UNIONIMAGESHARED_EXPORT bool isSupportsWriting(const QString &path) |
| 1171 |
{ |
| 1172 |
FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(path.toUtf8().data()); |
| 1173 |
return (fif != FIF_UNKNOWN) && FreeImage_FIFSupportsWriting(fif); |
| 1174 |
} |
| 1175 |
|
| 1176 |
UNIONIMAGESHARED_EXPORT const QString getOrientation(const QString &path) |
| 1177 |
{ |
| 1178 |
FIBITMAP *dib = readFile2FIBITMAP(path, FIF_LOAD_NOPIXELS); |
| 1179 |
auto datas = getMetaData(FIMD_EXIF_MAIN, dib); |
| 1180 |
if (datas.isEmpty()) { |
| 1181 |
return QString(); |
| 1182 |
} |
| 1183 |
FreeImage_Unload(dib); |
| 1184 |
return datas["Orientation"]; |
| 1185 |
} |
| 1186 |
|
| 1187 |
bool getThumbnail(QImage &res, const QString &path) |
| 1188 |
{ |
| 1189 |
FIBITMAP *dib = readFile2FIBITMAP(path); |
| 1190 |
res = FIBitmap2QImage(FreeImage_GetThumbnail(dib)); |
| 1191 |
FreeImage_Unload(dib); |
| 1192 |
return true; |
| 1193 |
} |
| 1194 |
|
| 1195 |
class UnionMovieImagePrivate : public QObject |
| 1196 |
{ |
| 1197 |
protected: |
| 1198 |
explicit UnionMovieImagePrivate(UnionMovieImage *parent): q_ptr(parent) |
| 1199 |
{ |
| 1200 |
Q_UNUSED(padding); |
| 1201 |
} |
| 1202 |
~UnionMovieImagePrivate() |
| 1203 |
{ |
| 1204 |
// CManagerAttributeService::getInstance()->setCouldRun(false); |
| 1205 |
// CManagerAttributeService::getInstance()->GifFreeFile(); |
| 1206 |
} |
| 1207 |
|
| 1208 |
|
| 1209 |
void setPathAndBegin(const QString &path) |
| 1210 |
{ |
| 1211 |
Q_UNUSED(path) |
| 1212 |
// CManagerAttributeService::getInstance()->setfilePathWithSignalPlay(path); |
| 1213 |
// QObject::connect(CManagerAttributeService::getInstance(), &CManagerAttributeService::emitImageSignal, this, [ = ](QImage image, bool isFirst) { |
| 1214 |
// Q_UNUSED(isFirst); |
| 1215 |
// res = image; |
| 1216 |
// }); |
| 1217 |
} |
| 1218 |
|
| 1219 |
int getCurrent() |
| 1220 |
{ |
| 1221 |
return currentIndex; |
| 1222 |
} |
| 1223 |
|
| 1224 |
void reset() |
| 1225 |
{ |
| 1226 |
// CManagerAttributeService::getInstance()->setCouldRun(false); |
| 1227 |
// CManagerAttributeService::getInstance()->GifFreeFile(); |
| 1228 |
|
| 1229 |
delete r; |
| 1230 |
r = nullptr; |
| 1231 |
errMsg = ""; |
| 1232 |
res = QImage(); |
| 1233 |
currentFormat = FIF_UNKNOWN; |
| 1234 |
currentIndex = 0; |
| 1235 |
frames = 0; |
| 1236 |
} |
| 1237 |
|
| 1238 |
void setIndex(int i) |
| 1239 |
{ |
| 1240 |
currentIndex = i; |
| 1241 |
} |
| 1242 |
private: |
| 1243 |
UnionMovieImage *const q_ptr; |
| 1244 |
Q_DECLARE_PUBLIC(UnionMovieImage) |
| 1245 |
QImageReader *r = nullptr; |
| 1246 |
QString errMsg = ""; |
| 1247 |
QImage res; |
| 1248 |
FREE_IMAGE_FORMAT currentFormat = FIF_UNKNOWN; |
| 1249 |
int currentIndex = 0; |
| 1250 |
int frames = 0; |
| 1251 |
char padding[4]; |
| 1252 |
}; |
| 1253 |
|
| 1254 |
UnionMovieImage::UnionMovieImage(): d_ptr(new UnionMovieImagePrivate(this)) |
| 1255 |
{ |
| 1256 |
|
| 1257 |
} |
| 1258 |
|
| 1259 |
UnionMovieImage::~UnionMovieImage() |
| 1260 |
{ |
| 1261 |
Q_D(UnionMovieImage); |
| 1262 |
delete d; |
| 1263 |
} |
| 1264 |
|
| 1265 |
void UnionMovieImage::setFileName(const QString &path) |
| 1266 |
{ |
| 1267 |
Q_D(UnionMovieImage); |
| 1268 |
d->reset(); |
| 1269 |
QString errMsg; |
| 1270 |
QFileInfo file_info(path); |
| 1271 |
QString file_suffix_upper = file_info.suffix().toUpper(); |
| 1272 |
QByteArray temp_path; |
| 1273 |
temp_path.append(path.toUtf8()); |
| 1274 |
FREE_IMAGE_FORMAT f = FreeImage_GetFileType(temp_path.data()); |
| 1275 |
if (!union_image_private.m_movie_formats.contains(file_suffix_upper) || !union_image_private.m_movie_formats.values().contains(f)) { |
| 1276 |
errMsg = "static Image"; |
| 1277 |
} else { |
| 1278 |
switch (f) { |
| 1279 |
case FIF_GIF: { |
| 1280 |
d->setPathAndBegin(path); |
| 1281 |
d->currentFormat = FIF_GIF; |
| 1282 |
} |
| 1283 |
break; |
| 1284 |
case FIF_WEBP: |
| 1285 |
case FIF_MNG: { |
| 1286 |
d->r = new QImageReader; |
| 1287 |
d->r->setFileName(path); |
| 1288 |
if (d->r->canRead()) { |
| 1289 |
d->currentFormat = FIF_MNG; |
| 1290 |
d->frames = d->r->imageCount(); |
| 1291 |
} |
| 1292 |
} |
| 1293 |
break; |
| 1294 |
default: |
| 1295 |
break; |
| 1296 |
} |
| 1297 |
} |
| 1298 |
} |
| 1299 |
|
| 1300 |
QImage UnionMovieImage::next() |
| 1301 |
{ |
| 1302 |
Q_D(UnionMovieImage); |
| 1303 |
switch (d->currentFormat) { |
| 1304 |
case FIF_GIF: { |
| 1305 |
return d->res; |
| 1306 |
} |
| 1307 |
case FIF_WEBP: |
| 1308 |
case FIF_MNG: { |
| 1309 |
int temp = d->currentIndex; |
| 1310 |
d->setIndex(temp + 1); |
| 1311 |
if (temp + 1 >= d->frames) { |
| 1312 |
d->setIndex(0); |
| 1313 |
} |
| 1314 |
d->res = d->r->read(); |
| 1315 |
d->r->jumpToNextImage(); |
| 1316 |
break; |
| 1317 |
} |
| 1318 |
default: |
| 1319 |
break; |
| 1320 |
} |
| 1321 |
return d->res; |
| 1322 |
} |
| 1323 |
|
| 1324 |
QString PrivateDetectImageFormat(const QString &filepath) |
| 1325 |
{ |
| 1326 |
QFile file(filepath); |
| 1327 |
if (!file.open(QIODevice::ReadOnly)) { |
| 1328 |
return ""; |
| 1329 |
} |
| 1330 |
|
| 1331 |
const QByteArray data = file.read(1024); |
| 1332 |
|
| 1333 |
// Check bmp file. |
| 1334 |
if (data.startsWith("BM")) { |
| 1335 |
return "bmp"; |
| 1336 |
} |
| 1337 |
|
| 1338 |
// Check dds file. |
| 1339 |
if (data.startsWith("DDS")) { |
| 1340 |
return "dds"; |
| 1341 |
} |
| 1342 |
|
| 1343 |
// Check gif file. |
| 1344 |
if (data.startsWith("GIF8")) { |
| 1345 |
return "gif"; |
| 1346 |
} |
| 1347 |
|
| 1348 |
// Check Max OS icons file. |
| 1349 |
if (data.startsWith("icns")) { |
| 1350 |
return "icns"; |
| 1351 |
} |
| 1352 |
|
| 1353 |
// Check jpeg file. |
| 1354 |
if (data.startsWith("\xff\xd8")) { |
| 1355 |
return "jpg"; |
| 1356 |
} |
| 1357 |
|
| 1358 |
// Check mng file. |
| 1359 |
if (data.startsWith("\x8a\x4d\x4e\x47\x0d\x0a\x1a\x0a")) { |
| 1360 |
return "mng"; |
| 1361 |
} |
| 1362 |
|
| 1363 |
// Check net pbm file (BitMap). |
| 1364 |
if (data.startsWith("P1") || data.startsWith("P4")) { |
| 1365 |
return "pbm"; |
| 1366 |
} |
| 1367 |
|
| 1368 |
// Check pgm file (GrayMap). |
| 1369 |
if (data.startsWith("P2") || data.startsWith("P5")) { |
| 1370 |
return "pgm"; |
| 1371 |
} |
| 1372 |
|
| 1373 |
// Check ppm file (PixMap). |
| 1374 |
if (data.startsWith("P3") || data.startsWith("P6")) { |
| 1375 |
return "ppm"; |
| 1376 |
} |
| 1377 |
|
| 1378 |
// Check png file. |
| 1379 |
if (data.startsWith("\x89PNG\x0d\x0a\x1a\x0a")) { |
| 1380 |
return "png"; |
| 1381 |
} |
| 1382 |
|
| 1383 |
// Check svg file. |
| 1384 |
if (data.indexOf("<svg") > -1) { |
| 1385 |
return "svg"; |
| 1386 |
} |
| 1387 |
|
| 1388 |
// TODO(xushaohua): tga file is not supported yet. |
| 1389 |
|
| 1390 |
// Check tiff file. |
| 1391 |
if (data.startsWith("MM\x00\x2a") || data.startsWith("II\x2a\x00")) { |
| 1392 |
// big-endian, little-endian. |
| 1393 |
return "tiff"; |
| 1394 |
} |
| 1395 |
|
| 1396 |
// TODO(xushaohua): Support wbmp file. |
| 1397 |
|
| 1398 |
// Check webp file. |
| 1399 |
if (data.startsWith("RIFFr\x00\x00\x00WEBPVP")) { |
| 1400 |
return "webp"; |
| 1401 |
} |
| 1402 |
|
| 1403 |
// Check xbm file. |
| 1404 |
if (data.indexOf("#define max_width ") > -1 && |
| 1405 |
data.indexOf("#define max_height ") > -1) { |
| 1406 |
return "xbm"; |
| 1407 |
} |
| 1408 |
|
| 1409 |
// Check xpm file. |
| 1410 |
if (data.startsWith("/* XPM */")) { |
| 1411 |
return "xpm"; |
| 1412 |
} |
| 1413 |
return ""; |
| 1414 |
} |
| 1415 |
|
| 1416 |
}; |
| 1417 |
|
| 1418 |
|
| 1419 |
|