Leptonica 1.85.0
Image processing and image analysis suite
Loading...
Searching...
No Matches
pdfapp.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
75#ifdef HAVE_CONFIG_H
76#include <config_auto.h>
77#endif /* HAVE_CONFIG_H */
78
79#include <string.h>
80#include "allheaders.h"
81
82
83/* --------------------------------------------*/
84#if USE_PDFIO /* defined in environ.h */
85 /* --------------------------------------------*/
86
87/*---------------------------------------------------------------------*
88 * Compression of images for prog/compresspdf *
89 *---------------------------------------------------------------------*/
130l_ok
132 l_int32 onebit,
133 l_int32 savecolor,
134 l_float32 scalefactor,
135 l_int32 quality,
136 const char *title,
137 const char *fileout)
138{
139char *fname;
140l_int32 n, i, res;
141l_int32 maxsmallset = 25; /* max num images kept uncompressed in array */
142l_float32 colorfract;
143PIX *pixs, *pix1, *pix2;
144PIXA *pixa1 = NULL;
145PIXAC *pixac1 = NULL;
146
147 if (!sa)
148 return ERROR_INT("sa not defined", __func__, 1);
149 if (!fileout)
150 return ERROR_INT("fileout not defined", __func__, 1);
151 if (scalefactor <= 0) scalefactor = 1.0;
152 if (scalefactor > 2.0) {
153 L_WARNING("scalefactor %f too big; setting to 2.0\n", __func__,
154 scalefactor);
155 scalefactor = 2.0;
156 }
157 if (quality <= 0) quality = 50; /* default value */
158 if (quality < 25) {
159 L_WARNING("quality %d too low; setting to 25\n", __func__, quality);
160 quality = 25;
161 }
162 if (quality > 95) {
163 L_WARNING("quality %d too high; setting to 95\n", __func__, quality);
164 quality = 95;
165 }
166 if ((n = sarrayGetCount(sa)) == 0)
167 return ERROR_INT("sa is empty", __func__, 1);
168
169 if (n <= maxsmallset)
170 pixa1 = pixaCreate(n);
171 else
172 pixac1 = pixacompCreate(n);
173 for (i = 0; i < n; i++) {
174 if (i == 0)
175 lept_stderr("page: ");
176 else if (i % 10 == 0)
177 lept_stderr("%d . ", i);
178 fname = sarrayGetString(sa, i, L_NOCOPY);
179 pixs = pixRead(fname);
180 if (onebit) {
181 if (savecolor) {
182 pixColorFraction(pixs, 40, 224, 80, 4, NULL, &colorfract);
183 if (colorfract > 0.01) /* save the color; DCT encoding */
184 pix1 = pixClone(pixs);
185 else
186 pix1 = pixConvertTo1(pixs, 180);
187 } else { /* do not save any color; tiffg4 encoding */
188 pix1 = pixConvertTo1(pixs, 180);
189 }
190 } else { /* default encoding: tiffg4 for 1 bpp; DCT for all else */
191 pix1 = pixClone(pixs);
192 }
193 if (scalefactor == 1.0)
194 pix2 = pixClone(pix1);
195 else
196 pix2 = pixScale(pix1, scalefactor, scalefactor);
197 if (n <= maxsmallset) {
198 pixaAddPix(pixa1, pix2, L_INSERT);
199 } else {
200 pixacompAddPix(pixac1, pix2, IFF_DEFAULT);
201 pixDestroy(&pix2);
202 }
203 pixDestroy(&pixs);
204 pixDestroy(&pix1);
205 }
206
207 /* Generate the pdf. Compute the actual input resolution from
208 * the pixel dimensions of the first image. This will cause each
209 * page to be printed to cover an 8.5 x 11 inch sheet of paper. */
210 lept_stderr("\nWrite output to %s\n", fileout);
211 if (n <= maxsmallset)
212 pix1 = pixaGetPix(pixa1, 0, L_CLONE);
213 else
214 pix1 = pixacompGetPix(pixac1, 0);
215 pixInferResolution(pix1, 11.0, &res);
216 pixDestroy(&pix1);
217 if (strcmp(title, "none") == 0)
218 title = NULL;
219 if (n <= maxsmallset) {
220 pixaConvertToPdf(pixa1, res, 1.0, L_DEFAULT_ENCODE, quality,
221 title, fileout);
222 pixaDestroy(&pixa1);
223 } else {
224 pixacompConvertToPdf(pixac1, res, 1.0, L_DEFAULT_ENCODE, quality,
225 title, fileout);
226 pixacompDestroy(&pixac1);
227 }
228 return 0;
229}
230
231
232/*---------------------------------------------------------------------*
233 * Crop images for prog/croppdf *
234 *---------------------------------------------------------------------*/
269l_ok
271 l_int32 lr_clear,
272 l_int32 tb_clear,
273 l_int32 edgeclean,
274 l_int32 lr_border,
275 l_int32 tb_border,
276 l_float32 maxwiden,
277 l_int32 printwiden,
278 const char *title,
279 const char *fileout)
280{
281char *fname;
282l_int32 n, i, res;
283l_int32 maxsmallset = 200; /* max num images kept uncompressed in array */
284PIX *pixs, *pix1;
285PIXA *pixa1 = NULL;
286PIXAC *pixac1 = NULL;
287
288 if (!sa)
289 return ERROR_INT("sa not defined", __func__, 1);
290 if (!fileout)
291 return ERROR_INT("fileout not defined", __func__, 1);
292 if ((n = sarrayGetCount(sa)) == 0)
293 return ERROR_INT("sa is empty", __func__, 1);
294
295 if (n <= maxsmallset)
296 pixa1 = pixaCreate(n);
297 else
298 pixac1 = pixacompCreate(n);
299 for (i = 0; i < n; i++) {
300 if (i == 0)
301 lept_stderr("page: ");
302 else if (i % 10 == 0)
303 lept_stderr("%d . ", i);
304 fname = sarrayGetString(sa, i, L_NOCOPY);
305 pixs = pixRead(fname);
306 pix1 = pixCropImage(pixs, lr_clear, tb_clear, edgeclean,
307 lr_border, tb_border, maxwiden, printwiden,
308 NULL, NULL);
309 pixDestroy(&pixs);
310 if (!pix1) {
311 L_ERROR("pix1 not made for i = %d\n", __func__, i);
312 continue;
313 }
314 if (n <= maxsmallset)
315 pixaAddPix(pixa1, pix1, L_INSERT);
316 else
317 pixacompAddPix(pixac1, pix1, IFF_TIFF_G4);
318 }
319
320 /* Generate the pdf. Compute the actual input resolution from
321 * the pixel dimensions of the first image. This will cause each
322 * page to be printed to cover an 8.5 x 11 inch sheet of paper. */
323 lept_stderr("\nWrite output to %s\n", fileout);
324 if (n <= maxsmallset)
325 pix1 = pixaGetPix(pixa1, 0, L_CLONE);
326 else
327 pix1 = pixacompGetPix(pixac1, 0);
328 pixInferResolution(pix1, 11.0, &res);
329 pixDestroy(&pix1);
330 if (strcmp(title, "none") == 0)
331 title = NULL;
332 if (n <= maxsmallset) {
333 pixaConvertToPdf(pixa1, res, 1.0, L_G4_ENCODE, 0, title, fileout);
334 pixaDestroy(&pixa1);
335 } else {
336 pixacompConvertToPdf(pixac1, res, 1.0, L_G4_ENCODE, 0, title, fileout);
337 pixacompDestroy(&pixac1);
338 }
339 return 0;
340}
341
342
343/*---------------------------------------------------------------------*
344 * Cleanup and binarization of images for prog/cleanpdf *
345 *---------------------------------------------------------------------*/
383l_ok
385 l_int32 res,
386 l_int32 contrast,
387 l_int32 rotation,
388 l_int32 opensize,
389 const char *title,
390 const char *fileout)
391{
392char *fname;
393l_int32 n, i, scale;
394l_int32 maxsmallset = 200; /* max num images kept uncompressed in array */
395PIX *pixs, *pix1;
396PIXA *pixa1 = NULL;
397PIXAC *pixac1 = NULL;
398
399 if (!sa)
400 return ERROR_INT("sa not defined", __func__, 1);
401 if (!fileout)
402 return ERROR_INT("fileout not defined", __func__, 1);
403 if (res == 0) res = 300;
404 if (res != 300 && res != 600) {
405 L_ERROR("invalid res = %d; res must be in {0, 300, 600}\n",
406 __func__, res);
407 return 1;
408 }
409 if (contrast < 1 || contrast > 10) {
410 L_ERROR("invalid contrast = %d; contrast must be in [1...10]\n",
411 __func__, contrast);
412 return 1;
413 }
414 if (rotation < 0 || rotation > 3) {
415 L_ERROR("invalid rotation = %d; rotation must be in {0,1,2,3}\n",
416 __func__, rotation);
417 return 1;
418 }
419 if (opensize > 3) {
420 L_ERROR("invalid opensize = %d; opensize must be <= 3\n",
421 __func__, opensize);
422 return 1;
423 }
424 scale = (res == 300) ? 1 : 2;
425 if ((n = sarrayGetCount(sa)) == 0)
426 return ERROR_INT("sa is empty", __func__, 1);
427
428 if (n <= maxsmallset)
429 pixa1 = pixaCreate(n);
430 else
431 pixac1 = pixacompCreate(n);
432 for (i = 0; i < n; i++) {
433 if (i == 0)
434 lept_stderr("page: ");
435 else if (i % 10 == 0)
436 lept_stderr("%d . ", i);
437 fname = sarrayGetString(sa, i, L_NOCOPY);
438 if ((pixs = pixRead(fname)) == NULL) {
439 L_ERROR("pixs not read from %s\n", __func__, fname);
440 continue;
441 }
442
443 pix1 = pixCleanImage(pixs, contrast, rotation, scale, opensize);
444 if (n <= maxsmallset) {
445 pixaAddPix(pixa1, pix1, L_INSERT);
446 } else {
447 pixacompAddPix(pixac1, pix1, IFF_TIFF_G4);
448 pixDestroy(&pix1);
449 }
450 pixDestroy(&pixs);
451 }
452
453 /* Generate the pdf. Compute the actual input resolution from
454 * the pixel dimensions of the first image. This will cause each
455 * page to be printed to cover an 8.5 x 11 inch sheet of paper. */
456 lept_stderr("Write output to %s\n", fileout);
457 if (n <= maxsmallset)
458 pix1 = pixaGetPix(pixa1, 0, L_CLONE);
459 else
460 pix1 = pixacompGetPix(pixac1, 0);
461 pixInferResolution(pix1, 11.0, &res);
462 pixDestroy(&pix1);
463 if (strcmp(title, "none") == 0)
464 title = NULL;
465
466 if (n <= maxsmallset) {
467 pixaConvertToPdf(pixa1, res, 1.0, L_G4_ENCODE, 0, title, fileout);
468 pixaDestroy(&pixa1);
469 } else {
470 pixacompConvertToPdf(pixac1, res, 1.0, L_G4_ENCODE, 0, title, fileout);
471 pixacompDestroy(&pixac1);
472 }
473 return 0;
474}
475
476/* --------------------------------------------*/
477#endif /* USE_PDFIO */
478/* --------------------------------------------*/
@ L_DEFAULT_ENCODE
Definition imageio.h:158
@ L_G4_ENCODE
Definition imageio.h:160
l_ok cropFilesToPdf(SARRAY *sa, l_int32 lr_clear, l_int32 tb_clear, l_int32 edgeclean, l_int32 lr_border, l_int32 tb_border, l_float32 maxwiden, l_int32 printwiden, const char *title, const char *fileout)
cropFilesToPdf()
Definition pdfapp.c:270
l_ok cleanTo1bppFilesToPdf(SARRAY *sa, l_int32 res, l_int32 contrast, l_int32 rotation, l_int32 opensize, const char *title, const char *fileout)
cleanTo1bppFilesToPdf()
Definition pdfapp.c:384
l_ok compressFilesToPdf(SARRAY *sa, l_int32 onebit, l_int32 savecolor, l_float32 scalefactor, l_int32 quality, const char *title, const char *fileout)
compressFilesToPdf()
Definition pdfapp.c:131
@ L_CLONE
Definition pix.h:506
@ L_NOCOPY
Definition pix.h:503
@ L_INSERT
Definition pix.h:504