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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
/*
SPDX-FileCopyrightText: 1994-1996 Thomas G. Lane.
SPDX-FileCopyrightText: 2009-2010 Patrick Spendrin <ps_ml@gmx.de>
SPDX-FileCopyrightText: 2007-2018 Gilles Caulier <caulier dot gilles at gmail dot com>
This file is based on jdatadst.c from libjpeg.
SPDX-License-Identifier: GPL-2.0-or-later
*/
//#define ENABLE_DEBUG_MESSAGES 1
#include "kipiwritehelp.h"
// Qt includes
#include <QIODevice>
#include <QDebug>
// LibJPEG includes
extern "C"
{
#include <jerror.h>
}
/* choose an efficiently fwrite'able size */
#define BUFFER_SIZE 4096
namespace KXMLKipiCmd
{
//-- JPG helper methods ---------------------------------------------------------------------
/**
Expanded data destination object for input/output for jpeg
*/
typedef struct
{
struct jpeg_destination_mgr pub; /* public fields */
QIODevice* outDevice; /* target stream */
JOCTET* buffer; /* start of buffer */
} my_destination_mgr;
typedef my_destination_mgr* my_dest_ptr;
typedef struct
{
struct jpeg_source_mgr pub; /* public fields */
QIODevice* inDevice;
JOCTET buffer[BUFFER_SIZE];
} my_source_mgr;
/**
* Initialize destination --- called by jpeg_start_compress
* before any data is actually written.
*/
void init_destination (j_compress_ptr cinfo)
{
my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
/* Allocate the output buffer --- it will be released when done with image */
dest->buffer = (JOCTET*) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
BUFFER_SIZE * (size_t)sizeof(JOCTET));
dest->pub.next_output_byte = dest->buffer;
dest->pub.free_in_buffer = BUFFER_SIZE;
}
/**
* Empty the output buffer --- called whenever buffer fills up.
*/
boolean empty_output_buffer (j_compress_ptr cinfo)
{
my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
if (dest->outDevice->write((char*)dest->buffer, BUFFER_SIZE) != (size_t) BUFFER_SIZE)
ERREXIT(cinfo, JERR_FILE_WRITE);
dest->pub.next_output_byte = dest->buffer;
dest->pub.free_in_buffer = BUFFER_SIZE;
return true;
}
/**
* Terminate destination --- called by jpeg_finish_compress
* after all data has been written. Usually needs to flush buffer.
*
* NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
* application must deal with any cleanup that should happen even
* for error exit.
*/
void term_destination (j_compress_ptr cinfo)
{
my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
size_t datacount = BUFFER_SIZE - dest->pub.free_in_buffer;
/* Write any data remaining in the buffer */
if (datacount > 0)
{
if ((size_t)dest->outDevice->write((char*)dest->buffer, datacount) != (size_t)datacount)
ERREXIT(cinfo, JERR_FILE_WRITE);
}
}
void kipi_jpeg_qiodevice_dest (j_compress_ptr cinfo, QIODevice* const outDevice)
{
my_dest_ptr dest;
/* The destination object is made permanent so that multiple JPEG images
* can be written to the same file without re-executing jpeg_stdio_dest.
* This makes it dangerous to use this manager and a different destination
* manager serially with the same JPEG object, because their private object
* sizes may be different. Caveat programmer.
*/
if (cinfo->dest == nullptr)
{
/* first time for this JPEG object? */
cinfo->dest = (struct jpeg_destination_mgr*)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
(size_t)sizeof(my_destination_mgr));
}
dest = (my_dest_ptr) cinfo->dest;
dest->pub.init_destination = init_destination;
dest->pub.empty_output_buffer = empty_output_buffer;
dest->pub.term_destination = term_destination;
dest->outDevice = outDevice;
}
boolean fill_input_buffer(j_decompress_ptr cinfo)
{
my_source_mgr* const src = (my_source_mgr*)cinfo->src;
Q_ASSERT(src->inDevice);
int readSize = src->inDevice->read((char*)src->buffer, BUFFER_SIZE);
if (readSize > 0)
{
src->pub.next_input_byte = src->buffer;
src->pub.bytes_in_buffer = readSize;
}
else
{
/**
* JPEG file is broken. We feed the decoder with fake EOI, as specified
* in the libjpeg documentation.
*/
static JOCTET fakeEOI[2] = { JOCTET(0xFF), JOCTET(JPEG_EOI)};
qWarning() << "Image is incomplete";
src->pub.next_input_byte = fakeEOI;
src->pub.bytes_in_buffer = 2;
}
return true;
}
void init_source(j_decompress_ptr cinfo)
{
fill_input_buffer(cinfo);
}
void skip_input_data(j_decompress_ptr cinfo, long num_bytes)
{
my_source_mgr* const src = (my_source_mgr*)cinfo->src;
if (num_bytes > 0)
{
while (num_bytes > (long) src->pub.bytes_in_buffer)
{
num_bytes -= (long) src->pub.bytes_in_buffer;
fill_input_buffer(cinfo);
/**
* we assume that fill_input_buffer will never return FALSE, so
* suspension need not be handled.
*/
}
src->pub.next_input_byte += (size_t) num_bytes;
src->pub.bytes_in_buffer -= (size_t) num_bytes;
}
}
void term_source(j_decompress_ptr)
{
}
void kipi_jpeg_qiodevice_src(j_decompress_ptr cinfo, QIODevice* const ioDevice)
{
Q_ASSERT(!cinfo->src);
my_source_mgr* const src = (my_source_mgr*)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
sizeof(my_source_mgr));
cinfo->src = (jpeg_source_mgr*)src;
src->pub.init_source = init_source;
src->pub.fill_input_buffer = fill_input_buffer;
src->pub.skip_input_data = skip_input_data;
src->pub.resync_to_restart = jpeg_resync_to_restart;
src->pub.term_source = term_source;
src->inDevice = ioDevice;
}
//-- PNG helper methods ---------------------------------------------------------------------
void kipi_png_write_fn(png_structp png_ptr, png_bytep data, png_size_t length)
{
QIODevice* const out = (QIODevice*)png_get_io_ptr(png_ptr);
uint nr = out->write((char*)data, length);
if (nr != length)
{
png_error(png_ptr, "Write Error");
return;
}
}
void kipi_png_flush_fn(png_structp png_ptr)
{
Q_UNUSED(png_ptr);
}
//-- TIF helper methods ---------------------------------------------------------------------
void kipi_tiff_warning(const char* module, const char* format, va_list warnings)
{
#ifdef ENABLE_DEBUG_MESSAGES
char message[4096];
vsnprintf(message, 4096, format, warnings);
qDebug() << module << "::" << message ;
#else
Q_UNUSED(module);
Q_UNUSED(format);
Q_UNUSED(warnings);
#endif
}
void kipi_tiff_error(const char* module, const char* format, va_list errors)
{
#ifdef ENABLE_DEBUG_MESSAGES
char message[4096];
vsnprintf(message, 4096, format, errors);
qDebug() << module << "::" << message ;
#else
Q_UNUSED(module);
Q_UNUSED(format);
Q_UNUSED(errors);
#endif
}
} // namespace KXMLKipiCmd
|