-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinterface.cpp
More file actions
206 lines (154 loc) · 4.82 KB
/
interface.cpp
File metadata and controls
206 lines (154 loc) · 4.82 KB
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
196
197
198
199
200
201
202
203
204
205
206
//
// (C) Jan de Vaan 2007-2010, all rights reserved. See the accompanying "License.txt" for licensed use.
//
//implement correct linkage for win32 dlls
#if defined(_WIN32)
#define CHARLS_IMEXPORT(returntype) __declspec(dllexport) returntype __stdcall
#endif
#include "config.h"
#include "util.h"
#include "interface.h"
#include "header.h"
JLS_ERROR CheckInput(const void* compressedData, size_t compressedLength, const void* uncompressedData, size_t uncompressedLength, const JlsParameters* pparams)
{
if (pparams == NULL)
return InvalidJlsParameters;
if (compressedLength == 0)
return InvalidJlsParameters;
if (compressedData == NULL)
return InvalidJlsParameters;
if (uncompressedData == NULL)
return InvalidJlsParameters;
if (pparams->width < 1 || pparams->width > 65535)
return ParameterValueNotSupported;
if (pparams->height < 1 || pparams->height > 65535)
return ParameterValueNotSupported;
int bytesperline = pparams->bytesperline < 0 ? -pparams->bytesperline : pparams->bytesperline;
if (uncompressedLength < size_t(bytesperline * pparams->height))
return InvalidJlsParameters;
return CheckParameterCoherent(pparams);
}
extern "C"
{
CHARLS_IMEXPORT(JLS_ERROR) JpegLsEncode(void* compressedData, size_t compressedLength, size_t* pcbyteWritten, const void* uncompressedData, size_t uncompressedLength, struct JlsParameters* pparams)
{
JlsParameters info = *pparams;
if(info.bytesperline == 0)
{
info.bytesperline = info.width * ((info.bitspersample + 7)/8);
if (info.ilv != ILV_NONE)
{
info.bytesperline *= info.components;
}
}
JLS_ERROR parameterError = CheckInput(compressedData, compressedLength, uncompressedData, uncompressedLength, &info);
if (parameterError != OK)
return parameterError;
if (pcbyteWritten == NULL)
return InvalidJlsParameters;
Size size = Size(info.width, info.height);
JLSOutputStream stream;
stream.Init(size, info.bitspersample, info.components);
if (info.colorTransform != 0)
{
stream.AddColorTransform(info.colorTransform);
}
if (info.ilv == ILV_NONE)
{
LONG cbyteComp = size.cx*size.cy*((info.bitspersample +7)/8);
for (LONG component = 0; component < info.components; ++component)
{
const BYTE* compareData = static_cast<const BYTE*>(uncompressedData) + component*cbyteComp;
stream.AddScan(compareData, &info);
}
}
else
{
stream.AddScan(uncompressedData, &info);
}
stream.Write((BYTE*)compressedData, compressedLength);
*pcbyteWritten = stream.GetBytesWritten();
return OK;
}
CHARLS_IMEXPORT(JLS_ERROR) JpegLsDecode(void* uncompressedData, size_t uncompressedLength, const void* compressedData, size_t compressedLength, JlsParameters* info)
{
JLSInputStream reader((BYTE*)compressedData, compressedLength);
if(info != NULL)
{
reader.SetInfo(info);
}
try
{
reader.Read(uncompressedData, uncompressedLength);
return OK;
}
catch (JlsException& e)
{
return e._error;
}
}
CHARLS_IMEXPORT(JLS_ERROR) JpegLsDecodeRect(void* uncompressedData, size_t uncompressedLength, const void* compressedData, size_t compressedLength, JlsRect roi, JlsParameters* info)
{
JLSInputStream reader((BYTE*)compressedData, compressedLength);
if(info != NULL)
{
reader.SetInfo(info);
}
reader.SetRect(roi);
try
{
reader.Read(uncompressedData, uncompressedLength);
return OK;
}
catch (JlsException& e)
{
return e._error;
}
}
CHARLS_IMEXPORT(JLS_ERROR) JpegLsVerifyEncode(const void* uncompressedData, size_t uncompressedLength, const void* compressedData, size_t compressedLength)
{
JlsParameters info = JlsParameters();
JLS_ERROR error = JpegLsReadHeader(compressedData, compressedLength, &info);
if (error != OK)
return error;
error = CheckInput(compressedData, compressedLength, uncompressedData, uncompressedLength, &info);
if (error != OK)
return error;
Size size = Size(info.width, info.height);
JLSOutputStream stream;
stream.Init(size, info.bitspersample, info.components);
if (info.ilv == ILV_NONE)
{
LONG cbyteComp = size.cx*size.cy*((info.bitspersample +7)/8);
for (LONG component = 0; component < info.components; ++component)
{
const BYTE* compareData = static_cast<const BYTE*>(uncompressedData) + component*cbyteComp;
stream.AddScan(compareData, &info);
}
}
else
{
stream.AddScan(uncompressedData, &info);
}
std::vector<BYTE> rgbyteCompressed(compressedLength + 16);
memcpy(&rgbyteCompressed[0], compressedData, compressedLength);
stream.EnableCompare(true);
stream.Write(&rgbyteCompressed[0], compressedLength);
return OK;
}
CHARLS_IMEXPORT(JLS_ERROR) JpegLsReadHeader(const void* compressedData, size_t compressedLength, JlsParameters* pparams)
{
try
{
JLSInputStream reader((BYTE*)compressedData, compressedLength);
reader.ReadHeader();
JlsParameters info = reader.GetMetadata();
*pparams = info;
return OK;
}
catch (JlsException& e)
{
return e._error;
}
}
}