Leptonica 1.68
C Image Processing Library

viewfiles.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  *   viewfiles.c
00018  *
00019  *     Generate smaller images for viewing and write html
00020  *        l_int32    pixHtmlViewer()
00021  */
00022 
00023 #include <string.h>
00024 #include "allheaders.h"
00025 
00026     /* MS VC++ can't handle array initialization with static consts ! */
00027 #define L_BUF_SIZE      512
00028 
00029 static const l_int32  DEFAULT_THUMB_WIDTH = 120;
00030 static const l_int32  DEFAULT_VIEW_WIDTH = 800;
00031 static const l_int32  MIN_THUMB_WIDTH = 50;
00032 static const l_int32  MIN_VIEW_WIDTH = 300;
00033 
00034 
00035 /*---------------------------------------------------------------------*
00036  *            Generate smaller images for viewing and write html       *
00037  *---------------------------------------------------------------------*/
00038 /*!
00039  *  pixHtmlViewer()
00040  *
00041  *      Input:  dirin:  directory of input image files
00042  *              dirout: directory for output files
00043  *              rootname: root name for output files
00044  *              thumbwidth:  width of thumb images
00045  *                           (in pixels; use 0 for default)
00046  *              viewwidth:  maximum width of view images (no up-scaling)
00047  *                           (in pixels; use 0 for default)
00048  *              copyorig:  1 to copy originals to dirout; 0 otherwise
00049  *      Return: 0 if OK; 1 on error
00050  *
00051  *  Notes:
00052  *      (1) The thumb and view reduced images are generated,
00053  *          along with two html files:
00054  *             <rootname>.html and <rootname>-links.html
00055  *      (2) The thumb and view files are named
00056  *             <rootname>_thumb_xxx.jpg
00057  *             <rootname>_view_xxx.jpg
00058  *          With this naming scheme, any number of input directories
00059  *          of images can be processed into views and thumbs
00060  *          and placed in the same output directory.
00061  */
00062 l_int32
00063 pixHtmlViewer(const char  *dirin,
00064               const char  *dirout,
00065               const char  *rootname,
00066               l_int32      thumbwidth,
00067               l_int32      viewwidth,
00068               l_int32      copyorig)
00069 {
00070 char      *fname, *fullname, *outname;
00071 char      *mainname, *linkname, *linknameshort;
00072 char      *viewfile, *thumbfile;
00073 char      *shtml, *slink;
00074 char       charbuf[L_BUF_SIZE];
00075 char       htmlstring[] = "<html>";
00076 char       framestring[] = "</frameset></html>";
00077 l_int32    i, nfiles, index, w, nimages, ignore;
00078 l_float32  factor;
00079 PIX       *pix, *pixthumb, *pixview;
00080 SARRAY    *safiles, *sathumbs, *saviews, *sahtml, *salink;
00081 
00082     PROCNAME("pixHtmlViewer");
00083 
00084     if (!dirin)
00085         return ERROR_INT("dirin not defined", procName, 1);
00086     if (!dirout)
00087         return ERROR_INT("dirout not defined", procName, 1);
00088     if (!rootname)
00089         return ERROR_INT("rootname not defined", procName, 1);
00090 
00091     if (thumbwidth == 0)
00092         thumbwidth = DEFAULT_THUMB_WIDTH;
00093     if (thumbwidth < MIN_THUMB_WIDTH) {
00094         L_WARNING("thumbwidth too small; using min value", procName);
00095         thumbwidth = MIN_THUMB_WIDTH;
00096     }
00097     if (viewwidth == 0)
00098         viewwidth = DEFAULT_VIEW_WIDTH;
00099     if (viewwidth < MIN_VIEW_WIDTH) {
00100         L_WARNING("viewwidth too small; using min value", procName);
00101         viewwidth = MIN_VIEW_WIDTH;
00102     }
00103 
00104         /* Make the output directory if it doesn't already exist */
00105     sprintf(charbuf, "mkdir -p %s", dirout);
00106     ignore = system(charbuf);
00107 
00108         /* Capture the filenames in the input directory */
00109     if ((safiles = getFilenamesInDirectory(dirin)) == NULL)
00110         return ERROR_INT("safiles not made", procName, 1);
00111 
00112         /* Generate output text file names */
00113     sprintf(charbuf, "%s/%s.html", dirout, rootname);
00114     mainname = stringNew(charbuf);
00115     sprintf(charbuf, "%s/%s-links.html", dirout, rootname);
00116     linkname = stringNew(charbuf);
00117     linknameshort = stringJoin(rootname, "-links.html");
00118 
00119     if ((sathumbs = sarrayCreate(0)) == NULL)
00120         return ERROR_INT("sathumbs not made", procName, 1);
00121     if ((saviews = sarrayCreate(0)) == NULL)
00122         return ERROR_INT("saviews not made", procName, 1);
00123 
00124         /* Generate the thumbs and views */
00125     nfiles = sarrayGetCount(safiles);
00126     index = 0;
00127     for (i = 0; i < nfiles; i++) {
00128         fname = sarrayGetString(safiles, i, L_NOCOPY);
00129         fullname = genPathname(dirin, fname);
00130         fprintf(stderr, "name: %s\n", fullname);
00131         if ((pix = pixRead(fullname)) == NULL) {
00132             fprintf(stderr, "file %s not a readable image\n", fullname);
00133             FREE(fullname);
00134             continue;
00135         }
00136         FREE(fullname);
00137         if (copyorig) {
00138             outname = genPathname(dirout, fname);
00139             pixWrite(outname, pix, IFF_JFIF_JPEG);
00140             FREE(outname);
00141         }
00142 
00143             /* Make and store the thumb */
00144         w = pixGetWidth(pix);
00145         factor = (l_float32)thumbwidth / (l_float32)w;
00146         if ((pixthumb = pixScale(pix, factor, factor)) == NULL)
00147             return ERROR_INT("pixthumb not made", procName, 1);
00148         sprintf(charbuf, "%s_thumb_%03d.jpg", rootname, index);
00149         sarrayAddString(sathumbs, charbuf, L_COPY);
00150         outname = genPathname(dirout, charbuf);
00151         pixWrite(outname, pixthumb, IFF_JFIF_JPEG);
00152         FREE(outname);
00153         pixDestroy(&pixthumb);
00154 
00155             /* Make and store the view */
00156         factor = (l_float32)viewwidth / (l_float32)w;
00157         if (factor >= 1.0)
00158             pixview = pixClone(pix);   /* no upscaling */
00159         else {
00160             if ((pixview = pixScale(pix, factor, factor)) == NULL)
00161                 return ERROR_INT("pixview not made", procName, 1);
00162         }
00163         sprintf(charbuf, "%s_view_%03d.jpg", rootname, index);
00164         sarrayAddString(saviews, charbuf, L_COPY);
00165         outname = genPathname(dirout, charbuf);
00166         pixWrite(outname, pixview, IFF_JFIF_JPEG);
00167         FREE(outname);
00168         pixDestroy(&pixview);
00169 
00170         pixDestroy(&pix);
00171         index++;
00172     }
00173 
00174         /* Generate the main html file */
00175     if ((sahtml = sarrayCreate(0)) == NULL)
00176         return ERROR_INT("sahtml not made", procName, 1);
00177     sarrayAddString(sahtml, htmlstring, L_COPY);
00178     sprintf(charbuf, "<frameset cols=\"%d, *\">", thumbwidth + 30);
00179     sarrayAddString(sahtml, charbuf, L_COPY);
00180     sprintf(charbuf, "<frame name=\"thumbs\" src=\"%s\">", linknameshort); 
00181     sarrayAddString(sahtml, charbuf, L_COPY);
00182     sprintf(charbuf, "<frame name=\"views\" src=\"%s\">",
00183             sarrayGetString(saviews, 0, L_NOCOPY)); 
00184     sarrayAddString(sahtml, charbuf, L_COPY);
00185     sarrayAddString(sahtml, framestring, L_COPY);
00186     shtml = sarrayToString(sahtml, 1);
00187     l_binaryWrite(mainname, "w", shtml, strlen(shtml));
00188     FREE(shtml);
00189     FREE(mainname);
00190 
00191         /* Generate the link html file */
00192     nimages = sarrayGetCount(saviews);
00193     fprintf(stderr, "num. images = %d\n", nimages);
00194     if ((salink = sarrayCreate(0)) == NULL)
00195         return ERROR_INT("salink not made", procName, 1);
00196     for (i = 0; i < nimages; i++) {
00197         viewfile = sarrayGetString(saviews, i, L_NOCOPY);
00198         thumbfile = sarrayGetString(sathumbs, i, L_NOCOPY);
00199         sprintf(charbuf, "<a href=\"%s\" TARGET=views><img src=\"%s\"></a>",
00200             viewfile, thumbfile);
00201         sarrayAddString(salink, charbuf, L_COPY);
00202     }
00203     slink = sarrayToString(salink, 1);
00204     l_binaryWrite(linkname, "w", slink, strlen(slink));
00205     FREE(slink);
00206     FREE(linkname);
00207     FREE(linknameshort);
00208 
00209     sarrayDestroy(&safiles);
00210     sarrayDestroy(&sathumbs);
00211     sarrayDestroy(&saviews);
00212     sarrayDestroy(&sahtml);
00213     sarrayDestroy(&salink);
00214 
00215     return 0;
00216 }
00217 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines