1 //========================================================================
2 //
3 // ImageOutputDev.h
4 //
5 // Copyright 1998-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
8 
9 //========================================================================
10 //
11 // Modified under the Poppler project - http://poppler.freedesktop.org
12 //
13 // All changes made under the Poppler project to this file are licensed
14 // under GPL version 2 or later
15 //
16 // Copyright (C) 2006 Rainer Keller <class321@gmx.de>
17 // Copyright (C) 2008 Timothy Lee <timothy.lee@siriushk.com>
18 // Copyright (C) 2009 Carlos Garcia Campos <carlosgc@gnome.org>
19 // Copyright (C) 2010 Jakob Voss <jakob.voss@gbv.de>
20 // Copyright (C) 2012, 2013, 2017 Adrian Johnson <ajohnson@redneon.com>
21 // Copyright (C) 2013 Thomas Freitag <Thomas.Freitag@alfa.de>
22 // Copyright (C) 2018, 2019, 2021 Albert Astals Cid <aacid@kde.org>
23 //
24 // To see a description of the changes please see the Changelog file that
25 // came with your tarball or type make ChangeLog if you are building from git
26 //
27 //========================================================================
28 
29 #ifndef IMAGEOUTPUTDEV_H
30 #define IMAGEOUTPUTDEV_H
31 
32 #include "poppler/poppler-config.h"
33 
34 #include <cstdio>
35 #include "goo/ImgWriter.h"
36 #include "OutputDev.h"
37 
38 class GfxState;
39 
40 //------------------------------------------------------------------------
41 // ImageOutputDev
42 //------------------------------------------------------------------------
43 
44 class ImageOutputDev : public OutputDev
45 {
46 public:
47     enum ImageType
48     {
49         imgImage,
50         imgStencil,
51         imgMask,
52         imgSmask
53     };
54     enum ImageFormat
55     {
56         imgRGB,
57         imgRGB48,
58         imgGray,
59         imgMonochrome,
60         imgCMYK
61     };
62 
63     // Create an OutputDev which will write images to files named
64     // <fileRoot>-NNN.<type> or <fileRoot>-PPP-NNN.<type>, if
65     // <pageNames> is set. Normally, all images are written as PBM
66     // (.pbm) or PPM (.ppm) files unless PNG or Tiff output is enabled
67     // (PNG is used if both are enabled).  If Jpeg is enabled, JPEG images
68     // are written as JPEG (.jpg) files.
69     ImageOutputDev(char *fileRootA, bool pageNamesA, bool listImagesA);
70 
71     // Destructor.
72     ~ImageOutputDev() override;
73 
74     // Use PNG format for output
enablePNG(bool png)75     void enablePNG(bool png) { outputPNG = png; }
76 
77     // Use TIFF format for output
enableTiff(bool tiff)78     void enableTiff(bool tiff) { outputTiff = tiff; }
79 
80     // Use Jpeg format for Jpeg files
enableJpeg(bool jpeg)81     void enableJpeg(bool jpeg) { dumpJPEG = jpeg; }
82 
83     // Use Jpeg2000 format for Jpeg2000 files
enableJpeg2000(bool jp2)84     void enableJpeg2000(bool jp2) { dumpJP2 = jp2; }
85 
86     // Use JBIG2 format for JBIG2 files
enableJBig2(bool jbig2)87     void enableJBig2(bool jbig2) { dumpJBIG2 = jbig2; }
88 
89     // Use CCITT format for CCITT files
enableCCITT(bool ccitt)90     void enableCCITT(bool ccitt) { dumpCCITT = ccitt; }
91 
92     // Check if file was successfully created.
isOk()93     virtual bool isOk() { return ok; }
94 
95     // Does this device use tilingPatternFill()?  If this returns false,
96     // tiling pattern fills will be reduced to a series of other drawing
97     // operations.
useTilingPatternFill()98     bool useTilingPatternFill() override { return true; }
99 
100     // Does this device use beginType3Char/endType3Char?  Otherwise,
101     // text in Type 3 fonts will be drawn with drawChar/drawString.
interpretType3Chars()102     bool interpretType3Chars() override { return false; }
103 
104     // Does this device need non-text content?
needNonText()105     bool needNonText() override { return true; }
106 
107     // Start a page
startPage(int pageNumA,GfxState * state,XRef * xref)108     void startPage(int pageNumA, GfxState *state, XRef *xref) override { pageNum = pageNumA; }
109 
110     //---- get info about output device
111 
112     // Does this device use upside-down coordinates?
113     // (Upside-down means (0,0) is the top left corner of the page.)
upsideDown()114     bool upsideDown() override { return true; }
115 
116     // Does this device use drawChar() or drawString()?
useDrawChar()117     bool useDrawChar() override { return false; }
118 
119     //----- path painting
120     bool tilingPatternFill(GfxState *state, Gfx *gfx, Catalog *cat, GfxTilingPattern *tPat, const double *mat, int x0, int y0, int x1, int y1, double xStep, double yStep) override;
121 
122     //----- image drawing
123     void drawImageMask(GfxState *state, Object *ref, Stream *str, int width, int height, bool invert, bool interpolate, bool inlineImg) override;
124     void drawImage(GfxState *state, Object *ref, Stream *str, int width, int height, GfxImageColorMap *colorMap, bool interpolate, const int *maskColors, bool inlineImg) override;
125     void drawMaskedImage(GfxState *state, Object *ref, Stream *str, int width, int height, GfxImageColorMap *colorMap, bool interpolate, Stream *maskStr, int maskWidth, int maskHeight, bool maskInvert, bool maskInterpolate) override;
126     void drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str, int width, int height, GfxImageColorMap *colorMap, bool interpolate, Stream *maskStr, int maskWidth, int maskHeight, GfxImageColorMap *maskColorMap,
127                              bool maskInterpolate) override;
128 
129 private:
130     // Sets the output filename with a given file extension
131     void setFilename(const char *fileExt);
132     void listImage(GfxState *state, Object *ref, Stream *str, int width, int height, GfxImageColorMap *colorMap, bool interpolate, bool inlineImg, ImageType imageType);
133     void writeImage(GfxState *state, Object *ref, Stream *str, int width, int height, GfxImageColorMap *colorMap, bool inlineImg);
134     void writeRawImage(Stream *str, const char *ext);
135     void writeImageFile(ImgWriter *writer, ImageFormat format, const char *ext, Stream *str, int width, int height, GfxImageColorMap *colorMap);
136     long getInlineImageLength(Stream *str, int width, int height, GfxImageColorMap *colorMap);
137 
138     char *fileRoot; // root of output file names
139     char *fileName; // buffer for output file names
140     bool listImages; // list images instead of dumping
141     bool dumpJPEG; // set to dump native JPEG files
142     bool dumpJP2; // set to dump native JPEG2000 files
143     bool dumpJBIG2; // set to dump native JBIG2 files
144     bool dumpCCITT; // set to dump native CCITT files
145     bool outputPNG; // set to output in PNG format
146     bool outputTiff; // set to output in TIFF format
147     bool pageNames; // set to include page number in file names
148     int pageNum; // current page number
149     int imgNum; // current image number
150     bool ok; // set up ok?
151 };
152 
153 #endif
154