1
//========================================================================
5
// This file is licensed under the GPLv2 or later
7
// Copyright (C) 2009 Stefan Thomas <thomas@eload24.com>
8
// Copyright (C) 2010 Adrian Johnson <ajohnson@redneon.com>
9
// Copyright (C) 2010 Harry Roberts <harry.roberts@midnight-labs.org>
11
//========================================================================
13
#include "JpegWriter.h"
17
#include "poppler/Error.h"
19
void outputMessage(j_common_ptr cinfo)
21
char buffer[JMSG_LENGTH_MAX];
24
(*cinfo->err->format_message) (cinfo, buffer);
26
// Send it to poppler's error handler
27
error(-1, "%s", buffer);
30
JpegWriter::JpegWriter(int q, bool p)
31
: progressive(p), quality(q)
35
JpegWriter::JpegWriter()
36
: progressive(false), quality(-1)
40
JpegWriter::~JpegWriter()
43
jpeg_destroy_compress(&cinfo);
46
bool JpegWriter::init(FILE *f, int width, int height, int hDPI, int vDPI)
48
// Setup error handler
49
cinfo.err = jpeg_std_error(&jerr);
50
jerr.output_message = &outputMessage;
53
jpeg_create_compress(&cinfo);
55
// Set destination file
56
jpeg_stdio_dest(&cinfo, f);
58
// Set libjpeg configuration
59
cinfo.image_width = width;
60
cinfo.image_height = height;
61
cinfo.density_unit = 1; // dots per inch
62
cinfo.X_density = hDPI;
63
cinfo.Y_density = vDPI;
64
cinfo.input_components = 3; /* # of color components per pixel */
65
cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
66
jpeg_set_defaults(&cinfo);
69
if( quality >= 0 && quality <= 100 ) {
70
jpeg_set_quality(&cinfo, quality, true);
73
// Use progressive mode
75
jpeg_simple_progression(&cinfo);
79
jpeg_start_compress(&cinfo, TRUE);
84
bool JpegWriter::writePointers(unsigned char **rowPointers, int rowCount)
86
// Write all rows to the file
87
jpeg_write_scanlines(&cinfo, rowPointers, rowCount);
92
bool JpegWriter::writeRow(unsigned char **row)
94
// Write the row to the file
95
jpeg_write_scanlines(&cinfo, row, 1);
100
bool JpegWriter::close()
102
jpeg_finish_compress(&cinfo);