-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImgJPEGItem.cpp
195 lines (163 loc) · 6.01 KB
/
ImgJPEGItem.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "ImgJPEGItem.h"
#include "ImgItemHelper.h"
void ImgJPEGItem::Load()
{
status_ = Status::Loading;
tjhandle jpegdecompressor{ nullptr };
PBYTE buffer{ nullptr };
try
{
FileMapView jpegfilemap(filepath_, FileMapView::Mode::Read);
if (jpegfilemap.filesize().HighPart > 0)
{
status_ = Status::Error;
goto done;
}
jpegdecompressor = turbojpeg::InitDecompress();
if (jpegdecompressor == NULL)
{
status_ = Status::Error;
goto done;
}
turbojpeg::SaveMarkers(jpegdecompressor, EXIF_MARKER);
turbojpeg::SaveMarkers(jpegdecompressor, ICC_MARKER);
INT jpegSubsamp, jpegColorspace;
if (turbojpeg::DecompressHeader(jpegdecompressor, jpegfilemap.data(), jpegfilemap.filesize().LowPart,
&width_, &height_, &jpegSubsamp, &jpegColorspace, TJFLAG_NOCLEANUP))
{
errorstring_ = turbojpeg::GetErrorStr();
status_ = Status::Error;
goto done;
}
INT pixelformat = TJPF_BGR;
if (jpegColorspace == TJCS::TJCS_CMYK || jpegColorspace == TJCS::TJCS_YCCK)
{
pixelformat = TJPF_CMYK;
PBYTE iccprofiledata;
INT iccprofiledatabytecount;
if (turbojpeg::ReadICCProfile(jpegdecompressor, &iccprofiledata, &iccprofiledatabytecount))
{
OpenICCProfile(iccprofiledata, iccprofiledatabytecount);
free(iccprofiledata);
}
}
Gdiplus::RotateFlipType rotateflip{ Gdiplus::RotateNoneFlipNone };
PBYTE exifdata;
const auto exifdatabytecount = turbojpeg::LocateEXIFSegment(jpegdecompressor, &exifdata);
if (exifdatabytecount > 0)
{
rotateflip = ImgItemHelper::GetRotateFlipTypeFromExifOrientation(
ImgItemHelper::GetExifOrientationFromData(exifdata, exifdatabytecount));
}
turbojpeg::AbortDecompress(jpegdecompressor);
INT targetwidth{}, targetheight{};
if (rotateflip == Gdiplus::Rotate90FlipNone || rotateflip == Gdiplus::Rotate270FlipNone
|| rotateflip == Gdiplus::Rotate90FlipX || rotateflip == Gdiplus::Rotate270FlipX)
{
targetwidth = targetheight_;
targetheight = targetwidth_;
}
else
{
targetwidth = targetwidth_;
targetheight = targetheight_;
}
BOOL resize = FALSE;
INT decompresswidth{}, decompressheight{};
const auto scalingfactorindex = GetScalingFactorIndex(width_, height_, targetwidth, targetheight);
if (scalingfactorindex >= scalingfactorcount_)
{
resize = TRUE;
}
else
{
decompresswidth = TJSCALED(width_, scalingfactors_[scalingfactorindex]);
decompressheight = TJSCALED(height_, scalingfactors_[scalingfactorindex]);
const auto widthdiff = targetwidth - decompresswidth;
const auto heightdiff = targetheight - decompressheight;
if ((widthdiff >= heightdiff && heightdiff > (kResizePercentThreshold * targetheight))
|| (heightdiff >= widthdiff && widthdiff > (kResizePercentThreshold * targetwidth)))
{
resize = TRUE;
}
}
if (resize)
{
decompresswidth = TJSCALED(width_, scalingfactors_[scalingfactorindex - 1]);
decompressheight = TJSCALED(height_, scalingfactors_[scalingfactorindex - 1]);
}
auto stride = TJPAD(decompresswidth * tjPixelSize[pixelformat]);
const auto buffersize = stride * decompressheight;
buffer = reinterpret_cast<PBYTE>(HeapAlloc(heap_, 0, buffersize));
INT decompressflags{ TJFLAG_FASTDCT };
if (!resize)
{
decompressflags |= TJFLAG_BOTTOMUP;
}
turbojpeg::SkipMarkers(jpegdecompressor, EXIF_MARKER);
turbojpeg::SkipMarkers(jpegdecompressor, ICC_MARKER);
if (turbojpeg::Decompress(jpegdecompressor, jpegfilemap.data(), jpegfilemap.filesize().LowPart, buffer,
decompresswidth, stride, decompressheight, pixelformat, decompressflags))
{
errorstring_ = turbojpeg::GetErrorStr();
status_ = Status::Error;
goto done;
}
if (pixelformat == TJPF_CMYK)
{
const auto newstride = TJPAD(decompresswidth * tjPixelSize[TJPF_BGR]);
if (!TranformCMYK8ColorsToBGR8(decompresswidth, decompressheight, stride, newstride, &buffer))
{
status_ = Status::Error;
goto done;
}
stride = newstride;
}
if (resize)
{
displaybuffer_ = ImgItemHelper::ResizeAndRotate24bppRGBImage(decompresswidth, decompressheight, buffer,
targetwidth, targetheight, rotateflip);
}
else if (rotateflip != Gdiplus::RotateNoneFlipNone)
{
displaybuffer_ = ImgItemHelper::Rotate24bppRGBImage(decompresswidth, decompressheight, buffer, rotateflip);
}
else
{
displaybuffer_.WriteData(decompresswidth, decompressheight, stride, buffer);
}
}
catch (...)
{
status_ = Status::Error;
goto done;
}
SetupDisplayParameters();
status_ = Status::Ready;
done:
if (jpegdecompressor != nullptr)
{
turbojpeg::Destroy(jpegdecompressor);
}
if (buffer != nullptr)
{
HeapFree(heap_, 0, buffer);
}
CloseICCProfile();
SetEvent(loadedevent_);
}
INT ImgJPEGItem::GetScalingFactorIndex(INT width, INT height, INT targetwidth, INT targetheight) const
{
INT i{}, scaledwidth{}, scaledheight{};
while (i < scalingfactorcount_)
{
scaledwidth = TJSCALED(width, scalingfactors_[i]);
scaledheight = TJSCALED(height, scalingfactors_[i]);
if (scaledwidth <= targetwidth && scaledheight <= targetheight)
{
break;
}
++i;
}
return i;
}