Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_jpeg_source.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
7#include "kis_jpeg_source.h"
8
9#include <jerror.h>
10
11#include <QIODevice>
12
13#include "kis_debug.h"
14
15namespace
16{
17
18const qint64 INPUT_BUFFER_SIZE = 4096;
19
20struct KisJPEGSourceManager : public jpeg_source_mgr
21{
22 QIODevice* input;
23 JOCTET* buffer;
24 bool anyDataReceived;
25};
26
27typedef KisJPEGSourceManager* KisJPEGSourceManagerPtr;
28
29extern "C"
30{
31
32void init_source(j_decompress_ptr cinfo)
33{
34 KisJPEGSourceManagerPtr src = (KisJPEGSourceManagerPtr)cinfo->src;
35 src->anyDataReceived = false;
36}
37
38boolean fill_input_buffer(j_decompress_ptr cinfo)
39{
40 KisJPEGSourceManagerPtr src = (KisJPEGSourceManagerPtr)cinfo->src;
41 qint64 numBytesRead = src->input->read(reinterpret_cast<char*>(src->buffer), INPUT_BUFFER_SIZE);
42
43 if (numBytesRead <= 0) {
44 if (!src->anyDataReceived) {
45 /* Treat empty input file as fatal error */
46 ERREXIT(cinfo, JERR_INPUT_EMPTY);
47 }
48 WARNMS(cinfo, JWRN_JPEG_EOF);
49
50 /* Insert a fake EOI marker */
51 src->buffer[0] = (JOCTET)0xFF;
52 src->buffer[1] = (JOCTET)JPEG_EOI;
53 numBytesRead = 2;
54 }
55
56 src->next_input_byte = src->buffer;
57 src->bytes_in_buffer = numBytesRead;
58 src->anyDataReceived = true;
59
60 return (boolean)true;
61}
62
63void skip_input_data(j_decompress_ptr cinfo, long numBytes)
64{
65 KisJPEGSourceManagerPtr src = (KisJPEGSourceManagerPtr)cinfo->src;
66
67 if (numBytes > 0) {
68 while (numBytes > (long)src->bytes_in_buffer) {
69 numBytes -= (long)src->bytes_in_buffer;
70 (void)fill_input_buffer(cinfo);
71 }
72 src->next_input_byte += (size_t)numBytes;
73 src->bytes_in_buffer -= (size_t)numBytes;
74 }
75}
76
77void term_source(j_decompress_ptr cinfo)
78{
79 Q_UNUSED(cinfo);
80}
81
82}
83}
84
86{
87
88void setSource(j_decompress_ptr cinfo, QIODevice* inputDevice)
89{
90 KisJPEGSourceManagerPtr src = 0;
91
92 if (cinfo->src == 0) {
93 cinfo->src = (struct jpeg_source_mgr*)
94 (*cinfo->mem->alloc_small)((j_common_ptr)cinfo, JPOOL_PERMANENT,
95 sizeof(KisJPEGSourceManager));
96 src = (KisJPEGSourceManagerPtr)cinfo->src;
97 src->buffer = (JOCTET*)
98 (*cinfo->mem->alloc_small)((j_common_ptr)cinfo, JPOOL_PERMANENT,
99 INPUT_BUFFER_SIZE*sizeof(JOCTET));
100 }
101
102 src = (KisJPEGSourceManagerPtr)cinfo->src;
103 src->init_source = init_source;
104 src->fill_input_buffer = fill_input_buffer;
105 src->skip_input_data = skip_input_data;
106 src->resync_to_restart = jpeg_resync_to_restart;
107 src->term_source = term_source;
108 src->input = inputDevice;
109 src->bytes_in_buffer = 0;
110 src->next_input_byte = 0;
111}
112
113}
114
typedef void(QOPENGLF_APIENTRYP PFNGLINVALIDATEBUFFERDATAPROC)(GLuint buffer)
void setSource(j_decompress_ptr cinfo, QIODevice *inputDevice)