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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
////////////////////////////////////////////////////
//
// Transparent support for JPEG files in Qt Pixmaps,
// using IJG JPEG library.
//
// Sirtaj Kang, Oct 1996.
//
// $Id: jpeg.cpp,v 1.4 1998/03/09 20:24:00 kulow Exp $
#include<stdio.h>
#include<assert.h>
#include"qimage.h"
#include"qdstream.h"
#include"qcolor.h"
#include"qpixmap.h"
#include"jpeg.h"
extern "C"
{
#include"jpeglib.h"
}
/////////////////////
//
// No JPEG save support yet
//
void write_jpeg_jfif(QImageIO *)
{
fprintf(stderr, "JPEG saving unimplemented.\n");
return;
}
///////////
//
// Plug-in to read files from JPEG into QImage
//
void read_jpeg_jfif(QImageIO * iio)
{
QIODevice *d = iio->ioDevice();
QImage image;
QDataStream s(d);
JSAMPROW buffer[1];
unsigned int *ui_row;
unsigned char *uc_row, *uc_row_index;
unsigned depth;
unsigned col;
// We need to know if the display can handle 32-bit images
depth = QPixmap::defaultDepth();
// Init jpeg decompression structures
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
qimageio_jpeg_src(&cinfo, &s);
jpeg_read_header(&cinfo, TRUE);
// If we're in an 8bit display, we want a colourmap.
if ((depth < 32) && (cinfo.out_color_space == JCS_RGB)) {
cinfo.quantize_colors = TRUE;
cinfo.dither_mode = JDITHER_ORDERED;
}
jpeg_start_decompress(&cinfo);
if (cinfo.quantize_colors == TRUE) {
image.create(cinfo.output_width, cinfo.output_height,
8, cinfo.actual_number_of_colors,
QImage::LittleEndian);
// Read colourmap
for (col = 0; col < cinfo.actual_number_of_colors; col++) {
image.setColor(col, qRgb(cinfo.colormap[0][col],
cinfo.colormap[1][col],
cinfo.colormap[2][col]));
}
} else if (cinfo.out_color_space == JCS_GRAYSCALE) {
image.create(cinfo.output_width, cinfo.output_height,
8, 256, QImage::LittleEndian);
// Read colourmap
for (col = 0; col < 256; col++) {
image.setColor(col, qRgb(col, col, col));
}
} else {
image.create(cinfo.output_width, cinfo.output_height, 32);
}
// Alloc one-row buffer for scanline
buffer[0] = new JSAMPLE[cinfo.output_width * cinfo.output_components];
//
// Perform decompression
//
// Decompress with colormap
if (cinfo.quantize_colors == TRUE || cinfo.out_color_space != JCS_RGB) {
while (cinfo.output_scanline < cinfo.output_height) {
uc_row_index = image.scanLine(cinfo.output_scanline);
uc_row = buffer[0];
jpeg_read_scanlines(&cinfo, buffer, 1);
for (col = 0; col < cinfo.output_width; col++)
*uc_row_index++ = *uc_row++;
}
// Decompress 24-bit
} else {
while (cinfo.output_scanline < cinfo.output_height) {
ui_row = (unsigned int *)
image.scanLine(cinfo.output_scanline);
uc_row = buffer[0];
jpeg_read_scanlines(&cinfo, buffer, 1);
for (col = 0; col < cinfo.output_width; col++)
*ui_row++ = qRgb(*uc_row++, *uc_row++, *uc_row++);
}
}
// Clean up JPEG structs
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
// Bind new image to screen
iio->setImage(image);
iio->setStatus(0);
delete [] buffer[0];
}
/////////
//
// JPEG plug in routines for QDataStream as input.
//
// Registers stream routines for use by IJG JPEG library
void qimageio_jpeg_src(j_decompress_ptr cinfo, QDataStream * image)
{
qimageio_jpeg_source_mgr *src;
// Set up buffer for the first time
if (cinfo->src == NULL) {
cinfo->src = (struct jpeg_source_mgr *)
(*cinfo->mem->alloc_small)
((j_common_ptr) cinfo,
JPOOL_PERMANENT,
sizeof(qimageio_jpeg_source_mgr));
src = (qimageio_jpeg_source_mgr *) cinfo->src;
src->buffer = (JOCTET *)
(*cinfo->mem->alloc_small)
((j_common_ptr) cinfo,
JPOOL_PERMANENT,
INPUT_BUFFER_SIZE * sizeof(JOCTET));
}
// Register methods
src = (qimageio_jpeg_source_mgr *) cinfo->src;
src->pub.init_source = qimageio_init_source;
src->pub.fill_input_buffer = qimageio_fill_input_buffer;
src->pub.skip_input_data = qimageio_skip_input_data;
src->pub.resync_to_restart = jpeg_resync_to_restart;
src->pub.term_source = qimageio_term_source;
// This is potentially dangerous, as it has chance of being
// misinterpreted. Saves effort, tho!
src->infile = image;
src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
src->pub.next_input_byte = NULL; /* until buffer loaded */
}
/////////
//
// Intializes data source
//
void qimageio_init_source(j_decompress_ptr cinfo)
{
qimageio_jpeg_source_mgr *ptr = (qimageio_jpeg_source_mgr *) cinfo->src;
ptr->start_of_file = TRUE;
} /////////
//
// Reads data from stream into JPEG working buffer
//
int qimageio_fill_input_buffer(j_decompress_ptr cinfo)
{
qimageio_jpeg_source_mgr *ptr = (qimageio_jpeg_source_mgr *) cinfo->src;
size_t nbytes;
nbytes = ptr->infile->device()->readBlock((char *) (ptr->buffer),
INPUT_BUFFER_SIZE);
if (nbytes <= 0) {
if (ptr->start_of_file) {
fprintf(stderr, "error: file empty.\n");
return FALSE;
}
fprintf(stderr, "warning: premature EOF in file.\n");
/* Insert a fake EOI marker */
ptr->buffer[0] = (JOCTET) 0xFF;
ptr->buffer[1] = (JOCTET) JPEG_EOI;
nbytes = 2;
}
ptr->pub.next_input_byte = ptr->buffer;
ptr->pub.bytes_in_buffer = nbytes;
ptr->start_of_file = FALSE;
return TRUE;
}
//////////
//
// Jumps over large chunks of data. Can probably be done quicker. (fseek()
// style).
void qimageio_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
{
qimageio_jpeg_source_mgr *ptr = (qimageio_jpeg_source_mgr *) cinfo->src;
if (num_bytes > 0) {
while (num_bytes > (long) ptr->pub.bytes_in_buffer) {
num_bytes -= (long) ptr->pub.bytes_in_buffer;
(void) qimageio_fill_input_buffer(cinfo);
} ptr->pub.next_input_byte += (size_t) num_bytes;
ptr->pub.bytes_in_buffer -= (size_t) num_bytes;
}
}
/////////
//
// Clean up. Doesn't really do anything, but required for compat.
//
void qimageio_term_source(j_decompress_ptr)
{
return;
}
|