55*/
66
77#include < fstream>
8- #include < vector>
9- #include < memory>
108
119#include < poppler-config.h>
12- #include < PDFDoc.h>
13- #include < goo/ImgWriter.h>
14- #include < goo/PNGWriter.h>
15- #include < goo/JpegWriter.h>
10+ #include < splash/SplashErrorCodes.h>
1611
1712#include " Base64Stream.h"
18- #include " util/const.h"
19-
2013#include " SplashBackgroundRenderer.h"
2114
2215namespace pdf2htmlEX {
2316
2417using std::string;
2518using std::ifstream;
26- using std::vector;
27- using std::unique_ptr;
2819
2920const SplashColor SplashBackgroundRenderer::white = {255 ,255 ,255 };
3021
@@ -35,6 +26,7 @@ SplashBackgroundRenderer::SplashBackgroundRenderer(const string & imgFormat, HTM
3526 , format(imgFormat)
3627{
3728bool supported =false ;
29+ // ENABLE_LIBPNG and ENABLE_LIBJPEG are defines coming in from poppler-config.h
3830#ifdef ENABLE_LIBPNG
3931if (format.empty ())
4032 format =" png" ;
@@ -47,7 +39,7 @@ SplashBackgroundRenderer::SplashBackgroundRenderer(const string & imgFormat, HTM
4739#endif
4840if (!supported)
4941 {
50- throw string (" Image format not supported:" ) + format;
42+ throw string (" Image format not supported by Poppler :" ) + format;
5143 }
5244}
5345
@@ -124,27 +116,26 @@ bool SplashBackgroundRenderer::render_page(PDFDoc * doc, int pageno)
124116
125117void SplashBackgroundRenderer::embed_image (int pageno)
126118{
127- // xmin->xmax is top->bottom
128- int xmin, xmax, ymin, ymax;
129- // poppler-0.84.0 hack to recover from the removal of *ModRegion tracking
130- //
131119auto * bitmap =getBitmap ();
132- xmin =0 ;
133- xmax = bitmap->getWidth ();
134- ymin =0 ;
135- ymax = bitmap->getHeight ();
136- //
137- // end of hack
138-
139120// dump the background image only when it is not empty
140- if ((xmin <= xmax) &&(ymin <= ymax) )
121+ if (bitmap-> getWidth () >= 0 &&bitmap-> getHeight () >= 0 )
141122 {
142123 {
143124auto fn = html_renderer->str_fmt (" %s/bg%x.%s" , (param.embed_image ? param.tmp_dir : param.dest_dir ).c_str (), pageno, format.c_str ());
144125if (param.embed_image )
145- html_renderer->tmp_files .add ((char *)fn);
126+ html_renderer->tmp_files .add ((const char *)fn);
127+
128+ SplashImageFileFormat splashImageFileFormat;
129+ if (format ==" png" )
130+ splashImageFileFormat = splashFormatPng;
131+ else if (format ==" jpg" )
132+ splashImageFileFormat = splashFormatJpeg;
133+ else
134+ throw string (" Image format not supported:" ) + format;
146135
147- dump_image ((char *)fn, xmin, ymin, xmax, ymax);
136+ SplashError e = bitmap->writeImgFile (splashImageFileFormat, (const char *)fn, param.actual_dpi , param.actual_dpi );
137+ if (e != splashOk)
138+ throw string (" Cannot write background image. SplashErrorCode:" ) +std::to_string (e);
148139 }
149140
150141double h_scale = html_renderer->text_zoom_factor () * DEFAULT_DPI / param.actual_dpi ;
@@ -154,10 +145,10 @@ void SplashBackgroundRenderer::embed_image(int pageno)
154145auto & all_manager = html_renderer->all_manager ;
155146
156147 f_page <<" <img class=\" " << CSS::BACKGROUND_IMAGE_CN
157- <<" " << CSS::LEFT_CN << all_manager.left .install ((( double )xmin) * h_scale )
158- <<" " << CSS::BOTTOM_CN << all_manager.bottom .install ((( double ) getBitmapHeight () - 1 - ymax) * v_scale )
159- <<" " << CSS::WIDTH_CN << all_manager.width .install ((( double )(xmax - xmin + 1 )) * h_scale )
160- <<" " << CSS::HEIGHT_CN << all_manager.height .install ((( double )(ymax - ymin + 1 )) * v_scale )
148+ <<" " << CSS::LEFT_CN << all_manager.left .install (0 . 0L )
149+ <<" " << CSS::BOTTOM_CN << all_manager.bottom .install (0 . 0L )
150+ <<" " << CSS::WIDTH_CN << all_manager.width .install (h_scale * bitmap-> getWidth () )
151+ <<" " << CSS::HEIGHT_CN << all_manager.height .install (v_scale * bitmap-> getHeight () )
161152 <<" \" alt=\"\" src=\" " ;
162153
163154if (param.embed_image )
@@ -182,68 +173,4 @@ void SplashBackgroundRenderer::embed_image(int pageno)
182173 }
183174}
184175
185- // There might be mem leak when exception is thrown !
186- void SplashBackgroundRenderer::dump_image (const char * filename,int x1,int y1,int x2,int y2)
187- {
188- int width = x2 - x1 +1 ;
189- int height = y2 - y1 +1 ;
190- if ((width <=0 ) || (height <=0 ))
191- throw " Bad metric for background image" ;
192-
193- FILE * f =fopen (filename," wb" );
194- if (!f)
195- throw string (" Cannot open file for background image" ) + filename;
196-
197- // use unique_ptr to auto delete the object upon exception
198- unique_ptr<ImgWriter> writer;
199-
200- if (false ) { }
201- #ifdef ENABLE_LIBPNG
202- else if (format ==" png" )
203- {
204- writer = unique_ptr<ImgWriter>(new PNGWriter);
205- }
206- #endif
207- #ifdef ENABLE_LIBJPEG
208- else if (format ==" jpg" )
209- {
210- writer = unique_ptr<ImgWriter>(new JpegWriter);
211- }
212- #endif
213- else
214- {
215- throw string (" Image format not supported:" ) + format;
216- }
217-
218- if (!writer->init (f, width, height, param.actual_dpi , param.actual_dpi ))
219- throw " Cannot initialize image writer" ;
220-
221- auto * bitmap =getBitmap ();
222- assert (bitmap->getMode () == splashModeRGB8);
223-
224- SplashColorPtr data = bitmap->getDataPtr ();
225- int row_size = bitmap->getRowSize ();
226-
227- vector<unsigned char *> pointers;
228- pointers.reserve (height);
229- SplashColorPtr p = data + y1 * row_size + x1 *3 ;
230- for (int i =0 ; i < height; ++i)
231- {
232- pointers.push_back (p);
233- p += row_size;
234- }
235-
236- if (!writer->writePointers (pointers.data (), height))
237- {
238- throw " Cannot write background image" ;
239- }
240-
241- if (!writer->close ())
242- {
243- throw " Cannot finish background image" ;
244- }
245-
246- fclose (f);
247- }
248-
249176}// namespace pdf2htmlEX