Leptonica 1.68
C Image Processing Library

splitimage2pdf.c

Go to the documentation of this file.
00001 /*====================================================================*
00002  -  Copyright (C) 2001 Leptonica.  All rights reserved.
00003  -  This software is distributed in the hope that it will be
00004  -  useful, but with NO WARRANTY OF ANY KIND.
00005  -  No author or distributor accepts responsibility to anyone for the
00006  -  consequences of using this software, or for whether it serves any
00007  -  particular purpose or works at all, unless he or she says so in
00008  -  writing.  Everyone is granted permission to copy, modify and
00009  -  redistribute this source code, for commercial or non-commercial
00010  -  purposes, with the following restrictions: (1) the origin of this
00011  -  source code must not be misrepresented; (2) modified versions must
00012  -  be plainly marked as such; and (3) this notice may not be removed
00013  -  or altered from any source or modified source distribution.
00014  *====================================================================*/
00015 
00016 /*
00017  * splitimage2pdf.c
00018  *
00019  *   Syntax:  splitimage2pdf filein nx ny fileout
00020  *
00021  *        nx = number of horizontal tiles
00022  *        ny = number of vertical tiles
00023  *
00024  *   Generates pdf of image tiles.  Rotates the image before
00025  *   tiling if the tiles otherwise will have larger width than
00026  *   height.
00027  */
00028 
00029 #include "allheaders.h"
00030 
00031     /* fill factor on 8.5 x 11 inch output page */
00032 static const l_float32   FILL_FACTOR = 0.95;
00033 
00034 
00035 main(int    argc,
00036      char **argv)
00037 {
00038 char        *filein, *fileout;
00039 char         buffer[512];
00040 const char  *psfile = "/tmp/junk_split_image.ps";
00041 l_int32      nx, ny, i, w, h, d, ws, hs, n, res, ignore;
00042 l_float32    scale;
00043 PIX         *pixs, *pixt, *pixr;
00044 PIXA        *pixa;
00045 static char  mainName[] = "splitimage2pdf";
00046 
00047     if (argc != 5)
00048         return ERROR_INT(" Syntax:  splitimage2pdf filein nx ny fileout",
00049                          mainName, 1);
00050 
00051     filein = argv[1];
00052     nx = atoi(argv[2]);
00053     ny = atoi(argv[3]);
00054     fileout = argv[4];
00055 
00056     lept_rm(NULL, "junk_split_image.ps");
00057 
00058     if ((pixs = pixRead(filein)) == NULL)
00059         exit(ERROR_INT("pixs not made", mainName, 1));
00060     d = pixGetDepth(pixs);
00061     if (d == 1 )
00062         lept_rm(NULL, "junk_split_image.tif");
00063     else if (d == 8 || d == 32)
00064         lept_rm(NULL, "junk_split_image.jpg");
00065     else
00066         return ERROR_INT("d not in {1,8,32} bpp", mainName, 1);
00067 
00068     ws = pixGetWidth(pixs);
00069     hs = pixGetHeight(pixs);
00070     if (ny * ws > nx * hs)
00071         pixr = pixRotate90(pixs, 1);
00072     else
00073         pixr = pixClone(pixs);
00074 
00075     pixa = pixaSplitPix(pixr, nx, ny, 0, 0);
00076     n = pixaGetCount(pixa);
00077     res = 300;
00078     for (i = 0; i < n; i++) {
00079         pixt = pixaGetPix(pixa, i, L_CLONE);
00080         w = pixGetWidth(pixt);
00081         h = pixGetHeight(pixt);
00082         scale = L_MIN(FILL_FACTOR * 2550 / w, FILL_FACTOR * 3300 / h);
00083         if (d == 1) {
00084             pixWrite("/tmp/junk_split_image.tif", pixt, IFF_TIFF_G4);
00085             if (i == 0)
00086                 convertG4ToPS("/tmp/junk_split_image.tif", psfile,
00087                               "w", 0, 0, 300, scale, 1, FALSE, TRUE);
00088             else
00089                 convertG4ToPS("/tmp/junk_split_image.tif", psfile,
00090                               "a", 0, 0, 300, scale, 1, FALSE, TRUE);
00091         }
00092         else {
00093             pixWrite("/tmp/junk_split_image.jpg", pixt, IFF_JFIF_JPEG);
00094             if (i == 0)
00095                 convertJpegToPS("/tmp/junk_split_image.jpg", psfile,
00096                                 "w", 0, 0, 300, scale, 1, TRUE);
00097             else
00098                 convertJpegToPS("/tmp/junk_split_image.jpg", psfile,
00099                                 "a", 0, 0, 300, scale, 1, TRUE);
00100         }
00101         pixDestroy(&pixt);
00102     }
00103 
00104     sprintf(buffer, "ps2pdf %s %s", psfile, fileout);
00105     ignore = system(buffer);
00106 
00107     pixaDestroy(&pixa);
00108     pixDestroy(&pixr);
00109     pixDestroy(&pixs);
00110     return 0;
00111 }
00112 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines