Krita Source Code Documentation
Loading...
Searching...
No Matches
KisZip Namespace Reference

Functions

int compress (const char *input, int unpacked_len, char *dst, int maxout)
 
QByteArray compress (const QByteArray &data)
 
QByteArray decompress (const QByteArray &data, int expected_length)
 
QByteArray psd_unzip_with_prediction (const QByteArray &src, int dst_len, int row_size, int color_depth)
 
template<typename T >
void psd_unzip_with_prediction (QByteArray &dst_buf, int row_size)
 
template<>
void psd_unzip_with_prediction< uint16_t > (QByteArray &dst_buf, const int row_size)
 
template<>
void psd_unzip_with_prediction< uint8_t > (QByteArray &dst_buf, const int row_size)
 
int psd_unzip_without_prediction (const char *src, int packed_len, char *dst, int unpacked_len)
 
QByteArray psd_zip_with_prediction (const QByteArray &src, int row_size, int color_depth)
 
template<typename T >
void psd_zip_with_prediction (QByteArray &dst_buf, int row_size)
 
template<>
void psd_zip_with_prediction< uint16_t > (QByteArray &dst_buf, const int row_size)
 
template<>
void psd_zip_with_prediction< uint8_t > (QByteArray &dst_buf, const int row_size)
 

Function Documentation

◆ compress() [1/2]

int KisZip::compress ( const char * input,
int unpacked_len,
char * dst,
int maxout )

Definition at line 152 of file compression.cpp.

153{
154 z_stream stream{};
155 int state;
156
157 stream.data_type = Z_BINARY;
158 stream.next_in = const_cast<Bytef *>(reinterpret_cast<const Bytef *>(input));
159 stream.avail_in = static_cast<uInt>(unpacked_len);
160 stream.next_out = reinterpret_cast<Bytef *>(dst);
161 stream.avail_out = static_cast<uInt>(maxout);
162
163 dbgFile << "Expected unpacked length:" << unpacked_len << ", maxout:" << maxout;
164
165 if (deflateInit(&stream, -1) != Z_OK) {
166 dbgFile << "Failed deflate initialization";
167 return 0;
168 }
169
170 int flush = Z_PARTIAL_FLUSH;
171
172 do {
173 state = deflate(&stream, flush);
174 if (state == Z_STREAM_END) {
175 dbgFile << "Finished deflating";
176 flush = Z_FINISH;
177 } else if (state != Z_OK) {
178 dbgFile << "Error deflating" << state << stream.msg;
179 break;
180 }
181 } while (stream.avail_in > 0);
182
183 if (state != Z_OK || stream.avail_in > 0) {
184 dbgFile << "Failed deflating" << state << stream.msg;
185 return 0;
186 }
187
188 dbgFile << "Success, deflated size:" << stream.total_out;
189
190 return static_cast<int>(stream.total_out);
191}
#define dbgFile
Definition kis_debug.h:53

References dbgFile.

◆ compress() [2/2]

QByteArray KisZip::compress ( const QByteArray & data)

Definition at line 193 of file compression.cpp.

194{
195 QByteArray output(data.length() * 4, '\0');
196 const int result = KisZip::compress(data.constData(), data.size(), output.data(), output.size());
197 output.resize(result);
198 return output;
199}
int compress(const char *input, int unpacked_len, char *dst, int maxout)

References compress().

◆ decompress()

QByteArray KisZip::decompress ( const QByteArray & data,
int expected_length )

Definition at line 365 of file compression.cpp.

366{
367 QByteArray output(expected_length, '\0');
368 const int result = psd_unzip_without_prediction(data.constData(), data.size(), output.data(), expected_length);
369 if (result == 0)
370 return QByteArray();
371 else
372 return output;
373}
int psd_unzip_without_prediction(const char *src, int packed_len, char *dst, int unpacked_len)

References psd_unzip_without_prediction().

◆ psd_unzip_with_prediction() [1/2]

QByteArray KisZip::psd_unzip_with_prediction ( const QByteArray & src,
int dst_len,
int row_size,
int color_depth )

Definition at line 285 of file compression.cpp.

286{
287 QByteArray dst_buf = Compression::uncompress(dst_len, src, psd_compression_type::ZIP);
288
289 if (dst_buf.size() == 0)
290 return {};
291
292 if (color_depth == 32) {
293 // Placeholded for future implementation.
294 errKrita << "Unsupported bit depth for prediction";
295 return {};
296 } else if (color_depth == 16) {
297 psd_unzip_with_prediction<quint16>(dst_buf, row_size);
298 } else {
299 psd_unzip_with_prediction<quint8>(dst_buf, row_size);
300 }
301
302 return dst_buf;
303}
static QByteArray uncompress(int unpacked_len, QByteArray bytes, psd_compression_type compressionType, int row_size=0, int color_depth=0)
#define errKrita
Definition kis_debug.h:107
@ ZIP
Definition psd.h:42

References errKrita, Compression::uncompress(), and ZIP.

◆ psd_unzip_with_prediction() [2/2]

template<typename T >
void KisZip::psd_unzip_with_prediction ( QByteArray & dst_buf,
int row_size )
inline

◆ psd_unzip_with_prediction< uint16_t >()

template<>
void KisZip::psd_unzip_with_prediction< uint16_t > ( QByteArray & dst_buf,
const int row_size )
inline

Definition at line 267 of file compression.cpp.

268{
269 auto *buf = reinterpret_cast<uint8_t *>(dst_buf.data());
270 int len = 0;
271 int dst_len = dst_buf.size();
272
273 while (dst_len > 0) {
274 len = row_size;
275 while (--len) {
276 buf[2] += buf[0] + ((buf[1] + buf[3]) >> 8);
277 buf[3] += buf[1];
278 buf += 2;
279 }
280 buf += 2;
281 dst_len -= row_size * 2;
282 }
283}

◆ psd_unzip_with_prediction< uint8_t >()

template<>
void KisZip::psd_unzip_with_prediction< uint8_t > ( QByteArray & dst_buf,
const int row_size )
inline

Definition at line 249 of file compression.cpp.

250{
251 auto *buf = reinterpret_cast<uint8_t *>(dst_buf.data());
252 int len = 0;
253 int dst_len = dst_buf.size();
254
255 while (dst_len > 0) {
256 len = row_size;
257 while (--len) {
258 *(buf + 1) += *buf;
259 buf++;
260 }
261 buf++;
262 dst_len -= row_size;
263 }
264}

◆ psd_unzip_without_prediction()

int KisZip::psd_unzip_without_prediction ( const char * src,
int packed_len,
char * dst,
int unpacked_len )

Definition at line 208 of file compression.cpp.

209{
210 z_stream stream{};
211 int state;
212
213 stream.data_type = Z_BINARY;
214 stream.next_in = const_cast<Bytef *>(reinterpret_cast<const Bytef *>(src));
215 stream.avail_in = static_cast<uInt>(packed_len);
216 stream.next_out = reinterpret_cast<Bytef *>(dst);
217 stream.avail_out = static_cast<uInt>(unpacked_len);
218
219 if (inflateInit(&stream) != Z_OK)
220 return 0;
221
222 int flush = Z_PARTIAL_FLUSH;
223
224 do {
225 state = inflate(&stream, flush);
226 if (state == Z_STREAM_END) {
227 dbgFile << "Finished inflating";
228 break;
229 } else if (state == Z_DATA_ERROR) {
230 dbgFile << "Error inflating" << state << stream.msg;
231 if (inflateSync(&stream) != Z_OK)
232 return 0;
233 continue;
234 }
235 } while (stream.avail_out > 0);
236
237 if ((state != Z_STREAM_END && state != Z_OK) || stream.avail_out > 0) {
238 dbgFile << "Failed inflating" << state << stream.msg;
239 return 0;
240 }
241
242 return static_cast<int>(stream.total_out);
243}

References dbgFile.

◆ psd_zip_with_prediction() [1/2]

QByteArray KisZip::psd_zip_with_prediction ( const QByteArray & src,
int row_size,
int color_depth )

Definition at line 349 of file compression.cpp.

350{
351 QByteArray dst_buf(src);
352 if (color_depth == 32) {
353 // Placeholded for future implementation.
354 errKrita << "Unsupported bit depth for prediction";
355 return {};
356 } else if (color_depth == 16) {
357 psd_zip_with_prediction<quint16>(dst_buf, row_size);
358 } else {
359 psd_zip_with_prediction<quint8>(dst_buf, row_size);
360 }
361
363}
static QByteArray compress(QByteArray bytes, psd_compression_type compressionType, int row_size=0, int color_depth=0)

References Compression::compress(), errKrita, and ZIP.

◆ psd_zip_with_prediction() [2/2]

template<typename T >
void KisZip::psd_zip_with_prediction ( QByteArray & dst_buf,
int row_size )
inline

◆ psd_zip_with_prediction< uint16_t >()

template<>
void KisZip::psd_zip_with_prediction< uint16_t > ( QByteArray & dst_buf,
const int row_size )
inline

Definition at line 331 of file compression.cpp.

332{
333 auto *buf = reinterpret_cast<uint8_t *>(dst_buf.data());
334 int len = 0;
335 int dst_len = dst_buf.size();
336
337 while (dst_len > 0) {
338 len = row_size;
339 while (--len) {
340 buf[2] -= buf[0] + ((buf[1] + buf[3]) >> 8);
341 buf[3] -= buf[1];
342 buf += 2;
343 }
344 buf += 2;
345 dst_len -= row_size * 2;
346 }
347}

◆ psd_zip_with_prediction< uint8_t >()

template<>
void KisZip::psd_zip_with_prediction< uint8_t > ( QByteArray & dst_buf,
const int row_size )
inline

Definition at line 313 of file compression.cpp.

314{
315 auto *buf = reinterpret_cast<uint8_t *>(dst_buf.data());
316 int len = 0;
317 int dst_len = dst_buf.size();
318
319 while (dst_len > 0) {
320 len = row_size;
321 while (--len) {
322 *(buf + 1) -= *buf;
323 buf++;
324 }
325 buf++;
326 dst_len -= row_size;
327 }
328}