Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_jpeg_destination.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2009 Adrian Page <adrian@pagenet.plus.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
8
9#include <jerror.h>
10
11#include <QIODevice>
12
13#include "kis_debug.h"
14
15namespace
16{
17
18const qint64 OUTPUT_BUFFER_SIZE = 4096;
19
20struct KisJPEGDestinationManager : public jpeg_destination_mgr
21{
22 void writeData(j_compress_ptr cinfo, const qint64 numBytesToWrite)
23 {
24 if (output->write(reinterpret_cast<const char*>(buffer), numBytesToWrite) != numBytesToWrite) {
25 ERREXIT(cinfo, JERR_FILE_WRITE);
26 }
27 }
28
29 QIODevice* output;
30 JOCTET* buffer;
31};
32
33typedef KisJPEGDestinationManager* KisJPEGDestinationManagerPtr;
34
35extern "C"
36{
37
38void init_destination(j_compress_ptr cinfo)
39{
40 KisJPEGDestinationManagerPtr dest = (KisJPEGDestinationManagerPtr)cinfo->dest;
41
42 dest->buffer = (JOCTET *)
43 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
44 OUTPUT_BUFFER_SIZE*sizeof(JOCTET));
45
46 dest->next_output_byte = dest->buffer;
47 dest->free_in_buffer = OUTPUT_BUFFER_SIZE;
48}
49
50boolean empty_output_buffer(j_compress_ptr cinfo)
51{
52 KisJPEGDestinationManagerPtr dest = (KisJPEGDestinationManagerPtr)cinfo->dest;
53
54 dest->writeData(cinfo, OUTPUT_BUFFER_SIZE);
55 dest->next_output_byte = dest->buffer;
56 dest->free_in_buffer = OUTPUT_BUFFER_SIZE;
57
58 return (boolean)true;
59}
60
61void term_destination(j_compress_ptr cinfo)
62{
63 KisJPEGDestinationManagerPtr dest = (KisJPEGDestinationManagerPtr)cinfo->dest;
64 const qint64 numBytesToWrite = OUTPUT_BUFFER_SIZE-(qint64)dest->free_in_buffer;
65
66 if (numBytesToWrite > 0) {
67 dest->writeData(cinfo, numBytesToWrite);
68 }
69}
70
71}
72}
73
75{
76
77void setDestination(j_compress_ptr cinfo, QIODevice* destinationDevice)
78{
79 if (cinfo->dest == 0) {
80 cinfo->dest = (struct jpeg_destination_mgr *)
81 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
82 sizeof(KisJPEGDestinationManager));
83 }
84
85 KisJPEGDestinationManagerPtr dest = (KisJPEGDestinationManagerPtr)cinfo->dest;
86
87 dest->init_destination = init_destination;
88 dest->empty_output_buffer = empty_output_buffer;
89 dest->term_destination = term_destination;
90 dest->output = destinationDevice;
91}
92
93}
94
static void writeData(KisPaintDeviceSP pd, const QRect &bounds, QDataStream &out_stream)
void setDestination(j_compress_ptr cinfo, QIODevice *destinationDevice)