Leptonica 1.85.0
Image processing and image analysis suite
Loading...
Searching...
No Matches
renderpdf.c
Go to the documentation of this file.
1/*====================================================================*
2 - Copyright (C) 2001 Leptonica. All rights reserved.
3 -
4 - Redistribution and use in source and binary forms, with or without
5 - modification, are permitted provided that the following conditions
6 - are met:
7 - 1. Redistributions of source code must retain the above copyright
8 - notice, this list of conditions and the following disclaimer.
9 - 2. Redistributions in binary form must reproduce the above
10 - copyright notice, this list of conditions and the following
11 - disclaimer in the documentation and/or other materials
12 - provided with the distribution.
13 -
14 - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
18 - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *====================================================================*/
26
83#ifdef HAVE_CONFIG_H
84#include <config_auto.h>
85#endif /* HAVE_CONFIG_H */
86
87#include "allheaders.h"
88
89/* --------------------------------------------*/
90#if USE_PDFIO /* defined in environ.h */
91/* --------------------------------------------*/
92
93/*-----------------------------------------------------------------*
94 * Rendering pdf files using an external library *
95 *-----------------------------------------------------------------*/
109l_ok
110l_pdfRenderFile(const char *filename,
111 l_int32 res,
112 SARRAY **psaout)
113{
114l_int32 ret;
115SARRAY *sain;
116
117 if (!psaout)
118 return ERROR_INT("&saout not defined", __func__, 1);
119 *psaout = NULL;
120 if (!filename)
121 return ERROR_INT("filename not defined", __func__, 1);
122
123 sain = sarrayCreate(1);
124 sarrayAddString(sain, filename, L_COPY);
125 ret = l_pdfRenderFiles(NULL, sain, res, psaout);
126 sarrayDestroy(&sain);
127 return ret;
128}
129
130
158l_ok
159l_pdfRenderFiles(const char *dir,
160 SARRAY *sain,
161 l_int32 res,
162 SARRAY **psaout)
163{
164char buf[256];
165char *imagedir, *firstfile, *fname, *basename, *tail;
166l_int32 i, nfiles, render_res;
167SARRAY *sa;
168
169 if (!LeptDebugOK) {
170 L_INFO("running pdftoppm is disabled; "
171 "use setLeptDebugOK(1) to enable\n", __func__);
172 return 0;
173 }
174
175 #ifdef OS_IOS /* iOS 11 does not support system() */
176 return ERROR_INT("iOS 11 does not support system()", __func__, 0);
177 #endif /* OS_IOS */
178
179 if (!psaout)
180 return ERROR_INT("&saout not defined", __func__, 1);
181 *psaout = NULL;
182 if (res == 0) res = 300;
183 if (res < 50 || res > 300)
184 return ERROR_INT("res not in range [50 ... 300]", __func__, 1);
185 if (!dir && !sain)
186 return ERROR_INT("neither dir or sain are defined", __func__, 1);
187 if (sain) {
188 sa = sarrayCopy(sain);
189 } else {
190 sa = getSortedPathnamesInDirectory(dir, "pdf", 0, 0);
191 if (!sa)
192 return ERROR_INT("no files found in dir", __func__, 1);
193 }
194 nfiles = sarrayGetCount(sa);
195
196 /* Set up directory for rendered page images. */
197 lept_rmdir("lept/renderpdf");
198 lept_mkdir("lept/renderpdf");
199 imagedir = genPathname("/tmp/lept/renderpdf", NULL);
200
201 /* Figure out the resolution to use with the image renderer.
202 This first checks the media box sizes, which give the output
203 image size in printer points (1/72 inch). The largest expected
204 output image has a max dimension of about 11 inches, corresponding
205 to 792 points. At a resolution of 300 ppi, the max image size
206 is then 3300. For robustness, use the median of media box sizes.
207 If the max dimension of this median is significantly larger than
208 792, reduce the input resolution to the renderer. Specifically:
209 * Calculate the median of the MediaBox widths and heights.
210 * If the max exceeds 850, reduce the resolution so that the max
211 dimension of the rendered image is 3300. The new resolution
212 input to the renderer is reduced from 300 by the factor:
213 (792 / medmax)
214 If the media boxes are not found, render a page using a small
215 given resolution (72) and use the max dimension to find the
216 resolution, render_res, that will produce an out with
217 3300 pixels in the largest dimension. */
218 firstfile = sarrayGetString(sa, 0, L_NOCOPY);
219 getPdfRendererResolution(firstfile, imagedir, &render_res);
220
221 /* The input %res gives the actual resolution at which the page is
222 to be rendered. If this is less than 300 ppi, reduce render_res,
223 the resolution input to pdftoppm, by the factor:
224 (res / 300) */
225 render_res = (render_res * res) / 300;
226
227 /* Rasterize: '-r res' renders output at res ppi
228 * pdftoppm -r res fname outroot */
229 for (i = 0; i < nfiles; i++) {
230 fname = sarrayGetString(sa, i, L_NOCOPY);
231 splitPathAtDirectory(fname, NULL, &tail);
232 splitPathAtExtension(tail, &basename, NULL);
233 snprintf(buf, sizeof(buf), "pdftoppm -r %d %s %s/%s",
234 render_res, fname, imagedir, basename);
235 lept_free(tail);
236 lept_free(basename);
237 lept_stderr("%s\n", buf);
238 callSystemDebug(buf); /* pdftoppm */
239 }
240 sarrayDestroy(&sa);
241
242 /* Generate the output array of image file names */
243 *psaout = getSortedPathnamesInDirectory(imagedir, NULL, 0, 0);
244 lept_free(imagedir);
245 return 0;
246}
247
248
249/* --------------------------------------------*/
250#endif /* USE_PDFIO */
251/* --------------------------------------------*/
252
253
254
255/* ------------------------------------------------------------------------- *
256 * Stubs if pdf is not supported *
257 * ------------------------------------------------------------------------- */
258
259/* -----------------------------------------------------------------*/
260#if !USE_PDFIO
261/* -----------------------------------------------------------------*/
262
263l_ok l_pdfRenderFile(const char *filename, l_int32 res, SARRAY **psaout)
264{
265 return ERROR_INT("function not present", __func__, 1);
266}
267
268/* -----------------------------------------------------------*/
269
270l_ok l_pdfRenderFiles(const char *dir, SARRAY *sain, l_int32 res,
271 SARRAY **psaout)
272{
273 return ERROR_INT("function not present", __func__, 1);
274}
275
276/* -----------------------------------------------------------------*/
277#endif /* !USE_PDFIO */
278/* -----------------------------------------------------------------*/
279
@ L_COPY
Definition pix.h:505
@ L_NOCOPY
Definition pix.h:503
l_ok l_pdfRenderFile(const char *filename, l_int32 res, SARRAY **psaout)
l_pdfRenderFile()
Definition renderpdf.c:110
l_ok l_pdfRenderFiles(const char *dir, SARRAY *sain, l_int32 res, SARRAY **psaout)
l_pdfRenderFiles()
Definition renderpdf.c:159