-
Notifications
You must be signed in to change notification settings - Fork 0
/
ilbmcreator.cpp
127 lines (119 loc) · 2.91 KB
/
ilbmcreator.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "ilbmcreator.h"
#include <fstream>
#include <iostream>
#include <Magick++.h>
#include <QFile>
#include <QImage>
#include <QtDebug>
#include <iostream>
#include <string>
Q_LOGGING_CATEGORY(LOG_ILBM, "com.ilbm-thumb-kde")
extern "C"
{
Q_DECL_EXPORT ThumbCreator *new_creator()
{
return new IlbmCreator();
}
};
IlbmCreator::IlbmCreator() = default;
IlbmCreator::~IlbmCreator() = default;
bool IlbmCreator::create(const QString &path, int width, int height, QImage &img)
{
Magick::Image image;
Magick::Geometry outsize;
std::string path_string = path.toStdString();
char aspectx = 0, aspecty = 0;
int img_size = width * height * 4;
try
{
img_buf = new uchar[img_size];
}
catch (...)
{
return false;
}
/* Extract proper aspect ratio information */
std::ifstream input(path_string, std::ios::binary);
if (input.fail())
{
return false;
}
bool found = false;
while (!input.eof())
{
char tmp;
input.get(tmp);
if (tmp == 'B')
{
input.get(tmp);
if (tmp != 'M')
{
input.seekg(-1, input.cur);
continue;
}
input.get(tmp);
if (tmp != 'H')
{
input.seekg(-1, input.cur);
continue;
}
input.get(tmp);
if (tmp != 'D')
{
input.seekg(-1, input.cur);
continue;
}
else
{
found = true;
break;
}
}
}
if (found)
{
input.seekg(0x12, input.cur);
input.get(aspectx);
input.get(aspecty);
}
input.close();
try
{
Magick::Geometry oldsize;
image.read(path_string);
oldsize = image.size();
if (aspectx != aspecty)
{
if (aspectx < aspecty)
{
oldsize.height(oldsize.height() * aspecty / aspectx);
}
else
{
oldsize.width(oldsize.width() * aspectx / aspecty);
}
oldsize.aspect(true);
image.scale(oldsize);
}
outsize = image.size();
/* No point in scaling if the image is already smaller than requested */
if ((int)outsize.width() > width || (int)outsize.height() > height)
{
image.scale(Magick::Geometry(width, height));
outsize = image.size();
}
image.write(0, 0, outsize.width(), outsize.height(), "RGBA", Magick::CharPixel, img_buf);
}
catch(...)
{
return false;
}
img = QImage(img_buf,
outsize.width(),
outsize.height(),
QImage::Format_RGBA8888);
/* Deep copy so we can delete */
img = img.copy();
delete[] img_buf;
return true;
}