2
* Copyright (C) 2010, Pino Toscano <pino@kde.org>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2, or (at your option)
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
19
#include "poppler-image.h"
21
#include "poppler-image-private.h"
24
#include "ImgWriter.h"
25
#if defined(ENABLE_LIBPNG)
26
#include "PNGWriter.h"
28
#if defined(ENABLE_LIBJPEG)
29
#include "JpegWriter.h"
41
inline FileCloser(FILE *ff)
46
{ if (f) { const int c = fclose(f); f = 0; return c == 0; } return true; }
51
int calc_bytes_per_row(int width, poppler::image::format_enum format)
54
case poppler::image::format_invalid:
56
case poppler::image::format_mono:
57
return (width + 7) >> 3;
58
case poppler::image::format_rgb24:
60
case poppler::image::format_argb32:
68
using namespace poppler;
70
image_private::image_private(int iwidth, int iheight, image::format_enum iformat)
82
image_private::~image_private()
89
image_private *image_private::create_data(int width, int height, image::format_enum format)
91
if (width <= 0 || height <= 0) {
95
int bpr = calc_bytes_per_row(width, format);
100
std::auto_ptr<image_private> d(new image_private(width, height, format));
101
d->bytes_num = bpr * height;
102
d->data = reinterpret_cast<char *>(std::malloc(d->bytes_num));
107
d->bytes_per_row = bpr;
112
image_private *image_private::create_data(char *data, int width, int height, image::format_enum format)
114
if (width <= 0 || height <= 0 || !data) {
118
int bpr = calc_bytes_per_row(width, format);
123
std::auto_ptr<image_private> d(new image_private(width, height, format));
124
d->bytes_num = bpr * height;
127
d->bytes_per_row = bpr;
133
\class poppler::image poppler-image.h "poppler/cpp/poppler-image.h"
135
A simple representation of image, with direct access to the data.
137
This class uses implicit sharing for the internal data, so it can be used as
138
value class. This also means any non-const operation will make sure that the
139
data used by current instance is not shared with other instances (ie
140
\em detaching), copying the shared data.
146
\enum poppler::image::format_enum
148
The possible formats for an image.
153
Construct an invalid image.
161
Construct a new image.
163
It allocates the storage needed for the image data; if the allocation fails,
164
the image is an invalid one.
166
\param iwidth the width for the image
167
\param iheight the height for the image
168
\param iformat the format for the bits of the image
170
image::image(int iwidth, int iheight, image::format_enum iformat)
171
: d(image_private::create_data(iwidth, iheight, iformat))
176
Construct a new image.
178
It uses the provide data buffer for the image, so you \b must ensure it
179
remains valid for the whole lifetime of the image.
181
\param idata the buffer to use for the image
182
\param iwidth the width for the image
183
\param iheight the height for the image
184
\param iformat the format for the bits of the image
186
image::image(char *idata, int iwidth, int iheight, image::format_enum iformat)
187
: d(image_private::create_data(idata, iwidth, iheight, iformat))
194
image::image(const image &pt)
207
if (d && !--d->ref) {
213
Image validity check.
215
\returns whether the image is valid.
217
bool image::is_valid() const
219
return d && d->format != format_invalid;
223
\returns the format of the image
225
image::format_enum image::format() const
227
return d ? d->format : format_invalid;
231
\returns whether the width of the image
233
int image::width() const
235
return d ? d->width : 0;
239
\returns whether the height of the image
241
int image::height() const
243
return d ? d->height : 0;
247
\returns the number of bytes in each row of the image
249
int image::bytes_per_row() const
251
return d ? d->bytes_per_row : 0;
255
Access to the image bits.
257
This function will detach and copy the shared data.
259
\returns the pointer to the first pixel
272
Access to the image bits.
274
This function provides const access to the data.
276
\returns the pointer to the first pixel
278
const char *image::const_data() const
280
return d ? d->data : 0;
284
Copy of a slice of the image.
286
\param r the sub-area of this image to copy; if empty, the whole image is
289
\returns a new image representing the specified part of the current image
291
image image::copy(const rect &r) const
304
Saves the current image to file.
306
Using this function it is possible to save the image to the specified
307
\p file_name, in the specified \p out_format and with a resolution of the
310
Image formats commonly supported are:
312
\li JPEG: \c jpeg, \c jpg
314
If an image format is not supported (check the result of
315
supported_image_formats()), the saving fails.
317
\returns whether the saving succeeded
319
bool image::save(const std::string &file_name, const std::string &out_format, int dpi) const
321
if (!is_valid() || file_name.empty() || out_format.empty()) {
325
std::string fmt = out_format;
326
std::transform(fmt.begin(), fmt.end(), fmt.begin(), tolower);
328
std::auto_ptr<ImgWriter> w;
329
const int actual_dpi = dpi == -1 ? 75 : dpi;
332
#if defined(ENABLE_LIBPNG)
333
else if (fmt == "png") {
334
w.reset(new PNGWriter());
337
#if defined(ENABLE_LIBJPEG)
338
else if (fmt == "jpeg" || fmt == "jpg") {
339
w.reset(new JpegWriter());
345
FILE *f = fopen(file_name.c_str(), "w");
349
const FileCloser fc(f);
350
if (!w->init(f, d->width, d->height, actual_dpi, actual_dpi)) {
359
char *hptr = d->data;
360
for (int y = 0; y < d->height; ++y) {
361
if (!w->writeRow(reinterpret_cast<unsigned char **>(&hptr))) {
364
hptr += d->bytes_per_row;
368
case format_argb32: {
369
std::vector<unsigned char> row(3 * d->width);
370
char *hptr = d->data;
371
for (int y = 0; y < d->height; ++y) {
372
unsigned char *rowptr = &row[0];
373
for (int x = 0; x < d->width; ++x, rowptr += 3) {
374
const unsigned int pixel = *reinterpret_cast<unsigned int *>(hptr + x * 4);
375
rowptr[0] = (pixel >> 16) & 0xff;
376
rowptr[1] = (pixel >> 8) & 0xff;
377
rowptr[2] = pixel & 0xff;
380
if (!w->writeRow(&rowptr)) {
383
hptr += d->bytes_per_row;
397
\returns a list of the supported image formats
399
std::vector<std::string> image::supported_image_formats()
401
std::vector<std::string> formats;
402
#if defined(ENABLE_LIBPNG)
403
formats.push_back("png");
405
#if defined(ENABLE_LIBJPEG)
406
formats.push_back("jpeg");
407
formats.push_back("jpg");
415
image& image::operator=(const image &pt)
420
image_private *old_d = d;
422
if (old_d && !--old_d->ref) {
434
image_private *old_d = d;
435
d = image_private::create_data(old_d->width, old_d->height, old_d->format);
437
std::memcpy(d->data, old_d->data, old_d->bytes_num);