Leptonica 1.84.1
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 *---------------------------------------------------------------------*/
129l_ok
131 l_int32 onebit,
132 l_int32 savecolor,
133 l_float32 scalefactor,
134 l_int32 quality,
135 const char *title,
136 const char *fileout)
137{
138char *fname;
139l_int32 n, i, res;
140l_int32 maxsmallset = 25; /* max num images kept uncompressed in array */
141l_float32 colorfract;
142PIX *pixs, *pix1, *pix2;
143PIXA *pixa1 = NULL;
144PIXAC *pixac1 = NULL;
145
146 if (!sa)
147 return ERROR_INT("sa not defined", __func__, 1);
148 if (!fileout)
149 return ERROR_INT("fileout not defined", __func__, 1);
150 if (scalefactor <= 0) scalefactor = 1.0;
151 if (quality <= 0) quality = 50; /* default value */
152 if (quality < 25) {
153 L_WARNING("quality %d too low; setting to 25\n", __func__, quality);
154 quality = 25;
155 }
156 if (quality > 95) {
157 L_WARNING("quality %d too high; setting to 95\n", __func__, quality);
158 quality = 95;
159 }
160 if ((n = sarrayGetCount(sa)) == 0)
161 return ERROR_INT("sa is empty", __func__, 1);
162
163 if (n <= maxsmallset)
164 pixa1 = pixaCreate(n);
165 else
166 pixac1 = pixacompCreate(n);
167 for (i = 0; i < n; i++) {
168 if (i == 0)
169 lept_stderr("page: ");
170 else if (i % 10 == 0)
171 lept_stderr("%d . ", i);
172 fname = sarrayGetString(sa, i, L_NOCOPY);
173 pixs = pixRead(fname);
174 if (onebit) {
175 if (savecolor) {
176 pixColorFraction(pixs, 40, 224, 80, 4, NULL, &colorfract);
177 if (colorfract > 0.01) /* save the color; DCT encoding */
178 pix1 = pixClone(pixs);
179 else
180 pix1 = pixConvertTo1(pixs, 180);
181 } else { /* do not save any color; tiffg4 encoding */
182 pix1 = pixConvertTo1(pixs, 180);
183 }
184 } else { /* default encoding: tiffg4 for 1 bpp; DCT for all else */
185 pix1 = pixClone(pixs);
186 }
187 if (scalefactor == 1.0)
188 pix2 = pixClone(pix1);
189 else
190 pix2 = pixScale(pix1, scalefactor, scalefactor);
191 if (n <= maxsmallset) {
192 pixaAddPix(pixa1, pix2, L_INSERT);
193 } else {
194 pixacompAddPix(pixac1, pix2, IFF_DEFAULT);
195 pixDestroy(&pix2);
196 }
197 pixDestroy(&pixs);
198 pixDestroy(&pix1);
199 }
200
201 /* Generate the pdf. Compute the actual input resolution from
202 * the pixel dimensions of the first image. This will cause each
203 * page to be printed to cover an 8.5 x 11 inch sheet of paper. */
204 lept_stderr("\nWrite output to %s\n", fileout);
205 if (n <= maxsmallset)
206 pix1 = pixaGetPix(pixa1, 0, L_CLONE);
207 else
208 pix1 = pixacompGetPix(pixac1, 0);
209 pixInferResolution(pix1, 11.0, &res);
210 pixDestroy(&pix1);
211 if (strcmp(title, "none") == 0)
212 title = NULL;
213 if (n <= maxsmallset) {
214 pixaConvertToPdf(pixa1, res, 1.0, L_DEFAULT_ENCODE, quality,
215 title, fileout);
216 pixaDestroy(&pixa1);
217 } else {
218 pixacompConvertToPdf(pixac1, res, 1.0, L_DEFAULT_ENCODE, quality,
219 title, fileout);
220 pixacompDestroy(&pixac1);
221 }
222 return 0;
223}
224
225
226/*---------------------------------------------------------------------*
227 * Crop images for prog/croppdf *
228 *---------------------------------------------------------------------*/
258l_ok
260 l_int32 lr_clear,
261 l_int32 tb_clear,
262 l_int32 edgeclean,
263 l_int32 lr_add,
264 l_int32 tb_add,
265 l_float32 maxwiden,
266 const char *title,
267 const char *fileout)
268{
269char *fname;
270l_int32 n, i, res;
271l_int32 maxsmallset = 200; /* max num images kept uncompressed in array */
272PIX *pixs, *pix1;
273PIXA *pixa1 = NULL;
274PIXAC *pixac1 = NULL;
275
276 if (!sa)
277 return ERROR_INT("sa not defined", __func__, 1);
278 if (!fileout)
279 return ERROR_INT("fileout not defined", __func__, 1);
280 if ((n = sarrayGetCount(sa)) == 0)
281 return ERROR_INT("sa is empty", __func__, 1);
282
283 if (n <= maxsmallset)
284 pixa1 = pixaCreate(n);
285 else
286 pixac1 = pixacompCreate(n);
287 for (i = 0; i < n; i++) {
288 if (i == 0)
289 lept_stderr("page: ");
290 else if (i % 10 == 0)
291 lept_stderr("%d . ", i);
292 fname = sarrayGetString(sa, i, L_NOCOPY);
293 pixs = pixRead(fname);
294 pix1 = pixCropImage(pixs, lr_clear, tb_clear, edgeclean,
295 lr_add, tb_add, maxwiden, NULL, NULL);
296 if (n <= maxsmallset)
297 pixaAddPix(pixa1, pix1, L_INSERT);
298 else
299 pixacompAddPix(pixac1, pix1, IFF_TIFF_G4);
300 pixDestroy(&pixs);
301 }
302
303 /* Generate the pdf. Compute the actual input resolution from
304 * the pixel dimensions of the first image. This will cause each
305 * page to be printed to cover an 8.5 x 11 inch sheet of paper. */
306 lept_stderr("\nWrite output to %s\n", fileout);
307 if (n <= maxsmallset)
308 pix1 = pixaGetPix(pixa1, 0, L_CLONE);
309 else
310 pix1 = pixacompGetPix(pixac1, 0);
311 pixInferResolution(pix1, 11.0, &res);
312 pixDestroy(&pix1);
313 if (strcmp(title, "none") == 0)
314 title = NULL;
315 if (n <= maxsmallset) {
316 pixaConvertToPdf(pixa1, res, 1.0, L_G4_ENCODE, 0, title, fileout);
317 pixaDestroy(&pixa1);
318 } else {
319 pixacompConvertToPdf(pixac1, res, 1.0, L_G4_ENCODE, 0, title, fileout);
320 pixacompDestroy(&pixac1);
321 }
322 return 0;
323}
324
325
326/*---------------------------------------------------------------------*
327 * Cleanup and binarization of images for prog/cleanpdf *
328 *---------------------------------------------------------------------*/
366l_ok
368 l_int32 res,
369 l_int32 contrast,
370 l_int32 rotation,
371 l_int32 opensize,
372 const char *title,
373 const char *fileout)
374{
375char *fname;
376l_int32 n, i, scale;
377l_int32 maxsmallset = 200; /* max num images kept uncompressed in array */
378PIX *pixs, *pix1;
379PIXA *pixa1 = NULL;
380PIXAC *pixac1 = NULL;
381
382 if (!sa)
383 return ERROR_INT("sa not defined", __func__, 1);
384 if (!fileout)
385 return ERROR_INT("fileout not defined", __func__, 1);
386 if (res == 0) res = 300;
387 if (res != 300 && res != 600) {
388 L_ERROR("invalid res = %d; res must be in {0, 300, 600}\n",
389 __func__, res);
390 return 1;
391 }
392 if (contrast < 1 || contrast > 10) {
393 L_ERROR("invalid contrast = %d; contrast must be in [1...10]\n",
394 __func__, contrast);
395 return 1;
396 }
397 if (rotation < 0 || rotation > 3) {
398 L_ERROR("invalid rotation = %d; rotation must be in {0,1,2,3}\n",
399 __func__, rotation);
400 return 1;
401 }
402 if (opensize > 3) {
403 L_ERROR("invalid opensize = %d; opensize must be <= 3\n",
404 __func__, opensize);
405 return 1;
406 }
407 scale = (res == 300) ? 1 : 2;
408 if ((n = sarrayGetCount(sa)) == 0)
409 return ERROR_INT("sa is empty", __func__, 1);
410
411 if (n <= maxsmallset)
412 pixa1 = pixaCreate(n);
413 else
414 pixac1 = pixacompCreate(n);
415 for (i = 0; i < n; i++) {
416 if (i == 0)
417 lept_stderr("page: ");
418 else if (i % 10 == 0)
419 lept_stderr("%d . ", i);
420 fname = sarrayGetString(sa, i, L_NOCOPY);
421 if ((pixs = pixRead(fname)) == NULL) {
422 L_ERROR("pixs not read from %s\n", __func__, fname);
423 continue;
424 }
425
426 pix1 = pixCleanImage(pixs, contrast, rotation, scale, opensize);
427 if (n <= maxsmallset) {
428 pixaAddPix(pixa1, pix1, L_INSERT);
429 } else {
430 pixacompAddPix(pixac1, pix1, IFF_TIFF_G4);
431 pixDestroy(&pix1);
432 }
433 pixDestroy(&pixs);
434 }
435
436 /* Generate the pdf. Compute the actual input resolution from
437 * the pixel dimensions of the first image. This will cause each
438 * page to be printed to cover an 8.5 x 11 inch sheet of paper. */
439 lept_stderr("Write output to %s\n", fileout);
440 if (n <= maxsmallset)
441 pix1 = pixaGetPix(pixa1, 0, L_CLONE);
442 else
443 pix1 = pixacompGetPix(pixac1, 0);
444 pixInferResolution(pix1, 11.0, &res);
445 pixDestroy(&pix1);
446 if (strcmp(title, "none") == 0)
447 title = NULL;
448
449 if (n <= maxsmallset) {
450 pixaConvertToPdf(pixa1, res, 1.0, L_G4_ENCODE, 0, title, fileout);
451 pixaDestroy(&pixa1);
452 } else {
453 pixacompConvertToPdf(pixac1, res, 1.0, L_G4_ENCODE, 0, title, fileout);
454 pixacompDestroy(&pixac1);
455 }
456 return 0;
457}
458
459/* --------------------------------------------*/
460#endif /* USE_PDFIO */
461/* --------------------------------------------*/
@ 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_add, l_int32 tb_add, l_float32 maxwiden, const char *title, const char *fileout)
cropFilesToPdf()
Definition pdfapp.c:259
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:367
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:130
@ L_CLONE
Definition pix.h:506
@ L_NOCOPY
Definition pix.h:503
@ L_INSERT
Definition pix.h:504