-
Notifications
You must be signed in to change notification settings - Fork 5
/
spatial_imagedata.cpp.txt
153 lines (132 loc) · 3.48 KB
/
spatial_imagedata.cpp.txt
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
#include <sbml/compress/CompressCommon.h>
#ifdef USE_ZLIB
#include <zlib.h>
#endif
/**
* Returns the data of this image as uncompressed array of integers
*
* @param data the output array of integers (it will be allocated using
* malloc and will have to be freed using free)
* @param length the output lenght of the array
*
*/
void
ImageData::getUncompressedData(int* &data, int& length)
{
if (mUncompressedSamples == NULL)
{
uncompress();
}
copySampleArrays(data, length, mUncompressedSamples, mUncompressedLength);
return;
}
void
ImageData::uncompress()
{
freeUncompressed();
if (mDataType.empty() || mDataType == "compressed")
{
char* csamples = (char*)malloc(sizeof(char)*mSamplesLength);
for (unsigned int i = 0 ; i < mSamplesLength; ++i)
csamples[i] = mSamples[i];
ImageData::uncompress_data(csamples, mSamplesLength, mUncompressedSamples, mUncompressedLength);
free(csamples);
if (mUncompressedSamples == 0)
copySampleArrays(mUncompressedSamples, mUncompressedLength, mSamples, mSamplesLength);
}
else
{
copySampleArrays(mUncompressedSamples, mUncompressedLength, mSamples, mSamplesLength);
}
}
unsigned int
ImageData::getUncompressedLength()
{
if (mUncompressedSamples == NULL)
uncompress();
return mUncompressedLength;
}
void
ImageData::getUncompressed(int* outputSamples)
{
if (outputSamples == NULL) return;
if (mUncompressedSamples == NULL)
uncompress();
memcpy(outputSamples , mUncompressedSamples, sizeof(int)*mUncompressedLength);
}
void
ImageData::freeUncompressed()
{
if (mUncompressedSamples == NULL) return;
mUncompressedLength = 0;
free(mUncompressedSamples);
mUncompressedSamples = NULL;
}
void
ImageData::uncompress_data(void *data, size_t length, int*& result, int& outLength)
{
#ifndef USE_ZLIB
// throwing an exception won't help our users, better set the result array and length to NULL.
// throw ZlibNotLinked();
outLength = 0;
result = NULL;
#else
std::vector<char> buffer;
const size_t BUFSIZE = 128 * 1024;
Bytef temp_buffer[BUFSIZE];
z_stream strm;
strm.zalloc = 0;
strm.zfree = 0;
strm.next_in = reinterpret_cast<Bytef *>(data);
strm.avail_in = length;
strm.next_out = reinterpret_cast<Bytef *>(temp_buffer);
strm.avail_out = BUFSIZE;
int res = inflateInit(&strm);
while (strm.avail_in != 0)
{
res = inflate(&strm, Z_NO_FLUSH);
if (res < 0)
{
outLength = 0;
result = NULL;
break;
}
if (strm.avail_out == 0)
{
buffer.insert(buffer.end(), temp_buffer, temp_buffer + BUFSIZE);
strm.next_out = reinterpret_cast<Bytef *>(temp_buffer);
strm.avail_out = BUFSIZE;
}
}
res = Z_OK;
while (res == Z_OK)
{
if (strm.avail_out == 0)
{
buffer.insert(buffer.end(), temp_buffer, temp_buffer + BUFSIZE);
strm.next_out = reinterpret_cast<Bytef *>(temp_buffer);
strm.avail_out = BUFSIZE;
}
res = inflate(&strm, Z_FINISH);
if (res < 0)
{
outLength = 0;
result = NULL;
}
}
buffer.insert(buffer.end(), temp_buffer, temp_buffer + BUFSIZE - strm.avail_out);
deflateEnd(&strm);
outLength = buffer.size();
result = (int*) malloc(sizeof(int)*outLength);
for (int i = 0; i < outLength; i++)
result[i] = buffer[i];
#endif
}
void
ImageData::copySampleArrays(int* &target, int& targetLength, int* source, int sourceLength)
{
targetLength = sourceLength;
target = (int*)malloc(sizeof(int)*sourceLength);
memset(target, 0, sizeof(int)*sourceLength);
memcpy(target, source, sizeof(int)*sourceLength);
}