-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageReadWrite.cpp
83 lines (56 loc) · 1.7 KB
/
ImageReadWrite.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "ImageReadWrite.h"
#include "HelperFunctions.h"
#ifdef _HAVE_IMAGEMAGICK
#include <Magick++.h>
#include <QVector>
using namespace Magick;
ImageReadWrite::ImageReadWrite(QObject *parent)
: QObject(parent)
{
}
ImageReadWrite::~ImageReadWrite()
{
}
cv::Mat ImageReadWrite::readImage(QString filename, QString& jsondata)
{
// need to build imagemagick debug DLL's
try {
Magick::Image im;
std::string fn = filename.toLocal8Bit().data();
im.read(filename.toUtf8().constData());
jsondata = QString::fromStdString(im.comment());
if(jsondata != "")
emit LOGCONSOLE("image data: " + jsondata);
int w = im.columns();
int h = im.rows();
cv::Mat opencvImage(h, w, CV_8UC4);
cv::Mat outputMat;
im.write(0, 0, w, h, "BGRA", Magick::CharPixel, opencvImage.data);
cvtColor(opencvImage, outputMat, CV_BGRA2BGR);
DBOUT("opencvImage type " << HelperFunctions::type2str(opencvImage.type()).toLocal8Bit().data());
DBOUT("outputMat type " << HelperFunctions::type2str(outputMat.type()).toLocal8Bit().data());
return outputMat;
}
catch (Magick::Exception &error_)
{
const char* perr = error_.what();
QString err = QString::fromLocal8Bit(perr);
emit LOGCONSOLE("Exception reading image: " + err);
return cv::Mat();
}
}
void ImageReadWrite::writeImage(QString filename, cv::Mat image, QString jsondata)
{
try {
Magick::Image im(image.cols, image.rows, "BGR", Magick::CharPixel, (char *)image.data);
im.comment(jsondata.toLocal8Bit().data());
const std::string fn = filename.toLocal8Bit().data();
im.write(fn);
}
catch (Magick::Exception &error_)
{
QString err = QString::fromUtf8(error_.what());
emit LOGCONSOLE("Exception writing image: " + err);
}
}
#endif