File: interface.cpp

package info (click to toggle)
charls 1.1.0+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 400 kB
  • sloc: cpp: 4,058; makefile: 17; ansic: 6
file content (234 lines) | stat: -rw-r--r-- 6,180 bytes parent folder | download
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//
// (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) && defined(CHARLS_DLL)
#define CHARLS_IMEXPORT(returntype) __declspec(dllexport) returntype __stdcall
#else
#define CHARLS_IMEXPORT(returntype) __attribute__ ((visibility ("default"))) returntype
#endif

#include "config.h"
#include "util.h"
#include "interface.h"
#include "header.h"
#include "jpegstreamwriter.h"
#include <sstream>


static JLS_ERROR CheckInput(ByteStreamInfo uncompressedStream, const JlsParameters* pparams)
{
	if (pparams == NULL)
		return InvalidJlsParameters;

	if (uncompressedStream.rawStream == NULL && uncompressedStream.rawData == NULL)
		return InvalidJlsParameters;

	if (pparams->width < 1 || pparams->width > 65535)
		return ParameterValueNotSupported;

	if (pparams->height < 1 || pparams->height > 65535)
		return ParameterValueNotSupported;

	if (uncompressedStream.rawData != NULL)
	{
		if (uncompressedStream.count < size_t(pparams->height * pparams->width * pparams->components * (pparams->bitspersample > 8 ? 2 : 1)))
			return UncompressedBufferTooSmall;
	}
	else if (uncompressedStream.rawStream == NULL)
		return InvalidJlsParameters;

	return CheckParameterCoherent(pparams);
}


CHARLS_IMEXPORT(JLS_ERROR) JpegLsEncodeStream(ByteStreamInfo compressedStreamInfo, size_t* pcbyteWritten, ByteStreamInfo rawStreamInfo, struct JlsParameters* pparams)
{
	if (pcbyteWritten == NULL)
		return InvalidJlsParameters;

	JLS_ERROR parameterError = CheckInput(rawStreamInfo, pparams);
	if (parameterError != OK)
		return parameterError;

	try
	{
		JlsParameters info = *pparams;
		if (info.bytesperline == 0)
		{
			info.bytesperline = info.width * ((info.bitspersample + 7)/8);
			if (info.ilv != ILV_NONE)
			{
				info.bytesperline *= info.components;
			}
		}

		Size size = Size(info.width, info.height);

		JpegStreamWriter writer(info.jfif, size, info.bitspersample, info.components);

		if (info.colorTransform != 0)
		{
			writer.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)
			{
				writer.AddScan(rawStreamInfo, &info);
				SkipBytes(&rawStreamInfo, cbyteComp);
			}
		}
		else
		{
			writer.AddScan(rawStreamInfo, &info);
		}
	
		writer.Write(compressedStreamInfo);
		*pcbyteWritten = writer.GetBytesWritten();
		return OK;
	}
	catch (const JlsException& e)
	{
		return e._error;
	}
}


CHARLS_IMEXPORT(JLS_ERROR) JpegLsDecodeStream(ByteStreamInfo rawStream, ByteStreamInfo compressedStream, JlsParameters* info)
{
	JpegMarkerReader reader(compressedStream);

	if (info != NULL)
	{
		reader.SetInfo(info);
	}

	try
	{
		reader.Read(rawStream);
		return OK;
	}
	catch (const JlsException& e)
	{
		return e._error;
	}
}


CHARLS_IMEXPORT(JLS_ERROR) JpegLsReadHeaderStream(ByteStreamInfo rawStreamInfo, JlsParameters* pparams)
{
	try
	{
		JpegMarkerReader reader(rawStreamInfo);
		reader.ReadHeader();
		reader.ReadStartOfScan(true);
		JlsParameters info = reader.GetMetadata();
		*pparams = info;
		return OK;
	}
	catch (const JlsException& e)
	{
		return e._error;
	}
}

extern "C"
{
	CHARLS_IMEXPORT(JLS_ERROR) JpegLsEncode(void* compressedData, size_t compressedLength, size_t* pcbyteWritten, const void* uncompressedData, size_t uncompressedLength, struct JlsParameters* pparams)
	{
		ByteStreamInfo rawStreamInfo = FromByteArray(uncompressedData, uncompressedLength);
		ByteStreamInfo compressedStreamInfo = FromByteArray(compressedData, compressedLength);

		return JpegLsEncodeStream(compressedStreamInfo, pcbyteWritten, rawStreamInfo, pparams);
	}


	CHARLS_IMEXPORT(JLS_ERROR) JpegLsDecode(void* uncompressedData, size_t uncompressedLength, const void* compressedData, size_t compressedLength, JlsParameters* info)
	{
		ByteStreamInfo compressedStream = FromByteArray(compressedData, compressedLength);
		ByteStreamInfo rawStreamInfo = FromByteArray(uncompressedData, uncompressedLength);

		return JpegLsDecodeStream(rawStreamInfo, compressedStream, info);
	}

	
	CHARLS_IMEXPORT(JLS_ERROR) JpegLsReadHeader(const void* compressedData, size_t compressedLength, JlsParameters* pparams)
	{
		return JpegLsReadHeaderStream(FromByteArray(compressedData, compressedLength), pparams);
	}


	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;

		ByteStreamInfo rawStreamInfo = FromByteArray(uncompressedData, uncompressedLength);

		error = CheckInput(rawStreamInfo, &info);

		if (error != OK)
			return error;

		Size size = Size(info.width, info.height);

		JpegStreamWriter writer(info.jfif, size, info.bitspersample, info.components);

		if (info.ilv == ILV_NONE)
		{
			LONG fieldLength = size.cx*size.cy*((info.bitspersample +7)/8);
			for (LONG component = 0; component < info.components; ++component)
			{
				writer.AddScan(rawStreamInfo, &info);
				SkipBytes(&rawStreamInfo, fieldLength);
			}
		}
		else
		{
			writer.AddScan(rawStreamInfo, &info);
		}

		std::vector<BYTE> rgbyteCompressed(compressedLength + 16);

		memcpy(&rgbyteCompressed[0], compressedData, compressedLength);

		writer.EnableCompare(true);
		writer.Write(FromByteArray(&rgbyteCompressed[0], rgbyteCompressed.size()));

		return OK;
	}


	CHARLS_IMEXPORT(JLS_ERROR) JpegLsDecodeRect(void* uncompressedData, size_t uncompressedLength, const void* compressedData, size_t compressedLength, JlsRect roi, JlsParameters* info)
	{
		ByteStreamInfo compressedStream = FromByteArray(compressedData, compressedLength);
		JpegMarkerReader reader(compressedStream);

		ByteStreamInfo rawStreamInfo = FromByteArray(uncompressedData, uncompressedLength);

		if (info != NULL)
		{
			reader.SetInfo(info);
		}

		reader.SetRect(roi);

		try
		{
			reader.Read(rawStreamInfo);
			return OK;
		}
		catch (const JlsException& e)
		{
			return e._error;
		}
	}
}