w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

viewfiles.c
Go to the documentation of this file.
1 /*====================================================================*
2  - Copyright (C) 2001 Leptonica. All rights reserved.
3  - This software is distributed in the hope that it will be
4  - useful, but with NO WARRANTY OF ANY KIND.
5  - No author or distributor accepts responsibility to anyone for the
6  - consequences of using this software, or for whether it serves any
7  - particular purpose or works at all, unless he or she says so in
8  - writing. Everyone is granted permission to copy, modify and
9  - redistribute this source code, for commercial or non-commercial
10  - purposes, with the following restrictions: (1) the origin of this
11  - source code must not be misrepresented; (2) modified versions must
12  - be plainly marked as such; and (3) this notice may not be removed
13  - or altered from any source or modified source distribution.
14  *====================================================================*/
15 
16 /*
17  * viewfiles.c
18  *
19  * Generate smaller images for viewing and write html
20  * l_int32 pixHtmlViewer()
21  *
22  * Utility function for getting filenames in a directory
23  * SARRAY *getFilenamesInDirectory()
24  *
25  * Utility function for getting sorted full pathnames
26  * SARRAY *getSortedPathnamesInDirectory()
27  *
28  * Note: this is specialized for unix, not M$
29  */
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <dirent.h> /* unix only */
35 #include "allheaders.h"
36 
37 static const l_int32 BUF_SIZE = 512;
38 static const l_int32 DEFAULT_THUMB_WIDTH = 120;
39 static const l_int32 DEFAULT_VIEW_WIDTH = 800;
40 static const l_int32 MIN_THUMB_WIDTH = 50;
41 static const l_int32 MIN_VIEW_WIDTH = 300;
42 
43 
44 /*---------------------------------------------------------------------*
45  * Generate smaller images for viewing and write html *
46  *---------------------------------------------------------------------*/
47 /*!
48  * pixHtmlViewer()
49  *
50  * Input: dirin: directory of input image files
51  * dirout: directory for output files
52  * rootname: root name for output files
53  * thumbwidth: width of thumb images
54  * (in pixels; use 0 for default)
55  * viewwidth: maximum width of view images (no up-scaling)
56  * (in pixels; use 0 for default)
57  * copyorig: 1 to copy originals to dirout; 0 otherwise
58  * Return: 0 if OK; 1 on error
59  *
60  * Notes:
61  * The thumb and view reduced images are generated,
62  * along with two html files:
63  * <rootname>.html and <rootname>-links.html
64  * The thumb and view files are named
65  * <rootname>_thumb_xxx.jpg
66  * <rootname>_view_xxx.jpg
67  * With this naming scheme, any number of input directories
68  * of images can be processed into views and thumbs
69  * and placed in the same output directory.
70  */
71 l_int32
72 pixHtmlViewer(const char *dirin,
73  const char *dirout,
74  const char *rootname,
75  l_int32 thumbwidth,
76  l_int32 viewwidth,
77  l_int32 copyorig)
78 {
79 char *fname, *fullname, *outname;
80 char *mainname, *linkname, *linknameshort;
81 char *viewfile, *thumbfile;
82 char *shtml, *slink;
83 char charbuf[BUF_SIZE];
84 l_int32 i, nfiles, index, w, nimages;
86 PIX *pix, *pixthumb, *pixview;
87 SARRAY *safiles, *sathumbs, *saviews, *sahtml, *salink;
88 
89  PROCNAME("pixHtmlViewer");
90 
91  if (!dirin)
92  return ERROR_INT("dirin not defined", procName, 1);
93  if (!dirout)
94  return ERROR_INT("dirout not defined", procName, 1);
95  if (!rootname)
96  return ERROR_INT("rootname not defined", procName, 1);
97 
98  if (thumbwidth == 0)
99  thumbwidth = DEFAULT_THUMB_WIDTH;
100  if (thumbwidth < MIN_THUMB_WIDTH) {
101  L_WARNING("thumbwidth too small; using min value", procName);
102  thumbwidth = MIN_THUMB_WIDTH;
103  }
104  if (viewwidth == 0)
105  viewwidth = DEFAULT_VIEW_WIDTH;
106  if (viewwidth < MIN_VIEW_WIDTH) {
107  L_WARNING("viewwidth too small; using min value", procName);
108  viewwidth = MIN_VIEW_WIDTH;
109  }
110 
111  /* make the output directory if it doesn't already exist */
112  sprintf(charbuf, "mkdir -p %s", dirout);
113  system(charbuf);
114 
115  /* capture the filenames in the input directory */
116  if ((safiles = getFilenamesInDirectory(dirin)) == NULL)
117  return ERROR_INT("safiles not made", procName, 1);
118 
119  /* generate output text file names */
120  sprintf(charbuf, "%s/%s.html", dirout, rootname);
121  mainname = stringNew(charbuf);
122  sprintf(charbuf, "%s/%s-links.html", dirout, rootname);
123  linkname = stringNew(charbuf);
124  linknameshort = stringJoin(rootname, "-links.html");
125 
126  if ((sathumbs = sarrayCreate(0)) == NULL)
127  return ERROR_INT("sathumbs not made", procName, 1);
128  if ((saviews = sarrayCreate(0)) == NULL)
129  return ERROR_INT("saviews not made", procName, 1);
130 
131  /* generate the thumbs and views */
132  nfiles = sarrayGetCount(safiles);
133  index = 0;
134  for (i = 0; i < nfiles; i++) {
135  fname = sarrayGetString(safiles, i, 0);
136  fullname = genPathname(dirin, fname);
137  fprintf(stderr, "name: %s\n", fullname);
138  if ((pix = pixRead(fullname)) == NULL) {
139  fprintf(stderr, "file %s not a readable image\n", fullname);
140  FREE((void *)fullname);
141  continue;
142  }
143  FREE((void *)fullname);
144  if (copyorig) {
145  outname = genPathname(dirout, fname);
147  FREE((void *)outname);
148  }
149 
150  /* make and store the thumb */
151  w = pixGetWidth(pix);
152  factor = (l_float32)thumbwidth / (l_float32)w;
153  if ((pixthumb = pixScale(pix, factor, factor)) == NULL)
154  return ERROR_INT("pixthumb not made", procName, 1);
155  sprintf(charbuf, "%s_thumb_%03d.jpg", rootname, index);
156  sarrayAddString(sathumbs, charbuf, 1);
157  outname = genPathname(dirout, charbuf);
158  pixWrite(outname, pixthumb, IFF_JFIF_JPEG);
159  FREE((void *)outname);
160  pixDestroy(&pixthumb);
161 
162  /* make and store the view */
163  factor = (l_float32)viewwidth / (l_float32)w;
164  if (factor >= 1.0)
165  pixview = pixClone(pix); /* no upscaling */
166  else {
167  if ((pixview = pixScale(pix, factor, factor)) == NULL)
168  return ERROR_INT("pixview not made", procName, 1);
169  }
170  sprintf(charbuf, "%s_view_%03d.jpg", rootname, index);
171  sarrayAddString(saviews, charbuf, 1);
172  outname = genPathname(dirout, charbuf);
173  pixWrite(outname, pixview, IFF_JFIF_JPEG);
174  FREE((void *)outname);
175  pixDestroy(&pixview);
176 
177  pixDestroy(&pix);
178  index++;
179  }
180 
181  /* generate the main html file */
182  if ((sahtml = sarrayCreate(0)) == NULL)
183  return ERROR_INT("sahtml not made", procName, 1);
184  sarrayAddString(sahtml, "<html>", 1);
185  sprintf(charbuf, "<frameset cols=\"%d, *\">", thumbwidth + 30);
186  sarrayAddString(sahtml, charbuf, 1);
187  sprintf(charbuf, "<frame name=\"thumbs\" src=\"%s\">", linknameshort);
188  sarrayAddString(sahtml, charbuf, 1);
189  sprintf(charbuf, "<frame name=\"views\" src=\"%s\">",
190  sarrayGetString(saviews, 0, 0));
191  sarrayAddString(sahtml, charbuf, 1);
192  sarrayAddString(sahtml, "</frameset></html>", 1);
193  shtml = sarrayToString(sahtml, 1);
194  arrayWrite(mainname, "w", shtml, strlen(shtml));
195  FREE((void *)shtml);
196  FREE((void *)mainname);
197 
198  /* generate the link html file */
199  nimages = sarrayGetCount(saviews);
200  fprintf(stderr, "num. images = %d\n", nimages);
201  if ((salink = sarrayCreate(0)) == NULL)
202  return ERROR_INT("salink not made", procName, 1);
203  for (i = 0; i < nimages; i++) {
204  viewfile = sarrayGetString(saviews, i, 0);
205  thumbfile = sarrayGetString(sathumbs, i, 0);
206  sprintf(charbuf, "<a href=\"%s\" TARGET=views><img src=\"%s\"></a>",
207  viewfile, thumbfile);
208  sarrayAddString(salink, charbuf, 1);
209  }
210  slink = sarrayToString(salink, 1);
211  arrayWrite(linkname, "w", slink, strlen(slink));
212  FREE((void *)slink);
213  FREE((void *)linkname);
214  FREE((void *)linknameshort);
215 
216  sarrayDestroy(&safiles);
217  sarrayDestroy(&sathumbs);
218  sarrayDestroy(&saviews);
219  sarrayDestroy(&sahtml);
220  sarrayDestroy(&salink);
221 
222  return 0;
223 }
224 
225 
226 /*---------------------------------------------------------------------*
227  * Utility function for getting filenames in a directory *
228  *---------------------------------------------------------------------*/
229 /*!
230  * getFilenamesInDirectory()
231  *
232  * Input: directory name
233  * Return: sarray of file names, or NULL on error
234  *
235  * Note:
236  * - This uses the Posix C library commands for handling directories.
237  * It will NOT work on Windows.
238  * - It will compile under MINGW, except for d_type member in dirent.
239  */
240 SARRAY *
242 {
243 SARRAY *safiles;
244 DIR *pdir;
245 struct dirent *pdirentry;
246 
247  PROCNAME("getFilenamesInDirectory");
248 
249  if (!dirname)
250  return (SARRAY *)ERROR_PTR("dirname not defined", procName, NULL);
251 
252  if ((safiles = sarrayCreate(0)) == NULL)
253  return (SARRAY *)ERROR_PTR("safiles not made", procName, NULL);
254  if ((pdir = opendir(dirname)) == NULL)
255  return (SARRAY *)ERROR_PTR("pdir not opened", procName, NULL);
256  while ((pdirentry = readdir(pdir))) {
257 #if !defined (__MINGW32__)
258  if (pdirentry->d_type == DT_DIR) /* ignore directories */
259  continue;
260 #endif
261  sarrayAddString(safiles, pdirentry->d_name, 1);
262  }
263  closedir(pdir);
264 
265  return safiles;
266 }
267 
268 
269 /*---------------------------------------------------------------------*
270  * Utility function for getting sorted full pathnames *
271  *---------------------------------------------------------------------*/
272 /*!
273  * getSortedPathnamesInDirectory()
274  *
275  * Input: directory name
276  * firstpage (0-based)
277  * npages (use 0 for all to the end)
278  * Return: sarray of sorted pathnames, or NULL on error
279  *
280  * Notes:
281  * (1) This implicitly uses the Posix C library commands for
282  * handling directories. It will NOT work on Windows.
283  * (2) The files in the directory are lexically sorted, and
284  * the full pathnames (i.e., including the directory) are
285  * returned for the requested sequence.
286  */
287 SARRAY *
290  l_int32 npages)
291 {
292 char *fname, *fullname;
293 l_int32 i, nfiles, lastpage;
294 SARRAY *safiles, *saout;
295 
296  PROCNAME("getSortedPathnamesInDirectory");
297 
298  if (!dirname)
299  return (SARRAY *)ERROR_PTR("dirname not defined", procName, NULL);
300 
301  if ((safiles = getFilenamesInDirectory(dirname)) == NULL)
302  return (SARRAY *)ERROR_PTR("safiles not made", procName, NULL);
303  sarraySort(safiles, safiles, L_SORT_INCREASING);
304  nfiles = sarrayGetCount(safiles);
305 
306  firstpage = L_MIN(L_MAX(firstpage, 0), nfiles - 1);
307  if (npages == 0)
308  npages = nfiles - firstpage;
309  lastpage = L_MIN(firstpage + npages - 1, nfiles - 1);
310 
311  saout = sarrayCreate(lastpage - firstpage + 1);
312  for (i = firstpage; i <= lastpage; i++) {
313  fname = sarrayGetString(safiles, i, 0);
314  fullname = genPathname(dirname, fname);
315  sarrayAddString(saout, fullname, 0); /* insert */
316  }
317 
318  sarrayDestroy(&safiles);
319  return saout;
320 }
321 
322 
static char * dirname(mrb_state *mrb, const char *path)
Definition: apilist.c:64
static int npages
Definition: backend_pdf.c:75
int charbuf
Definition: devnag.c:357
char rootname[1024]
Definition: dvi2.c:64
int w
Definition: dviconv.c:26
printlisttype * lastpage
Definition: dvi2tty.c:97
printlisttype * firstpage
Definition: dvi2tty.c:96
#define PROCNAME(name)
Definition: environ.h:131
#define ERROR_PTR(a, b, c)
Definition: environ.h:132
#define L_WARNING(a, b)
Definition: environ.h:136
#define ERROR_INT(a, b, c)
Definition: environ.h:133
int l_int32
Definition: environ.h:32
float l_float32
Definition: environ.h:34
const unsigned char FREE
Definition: image.cpp:34
@ IFF_JFIF_JPEG
Definition: imageio.h:30
pix
Definition: in_pcx.cpp:383
#define NULL
Definition: ftobjs.h:61
small capitals from c petite p scientific i
Definition: afcover.h:80
float factor
Definition: dimen.c:22
static char * outname
Definition: pngtest.c:1818
struct dirent * readdir(DIR *dirp)
Definition: dirent.c:138
int closedir(DIR *dirp)
Definition: dirent.c:123
DIR * opendir(char *fname)
Definition: dirent.c:60
#define system(p)
Definition: win32lib.h:269
#define fprintf
Definition: mendex.h:64
@ DT_DIR
Definition: dirent.h:46
#define sprintf
Definition: snprintf.c:44
char * stringNew(const char *src)
Definition: utils.c:374
void pixDestroy(PIX **ppix)
Definition: pix1.c:225
SARRAY * sarrayCreate(l_int32 n)
Definition: sarray.c:117
PIX * pixScale(PIX *pixs, l_float32 scalex, l_float32 scaley)
Definition: scale.c:131
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
Definition: sarray.c:485
l_int32 arrayWrite(const char *filename, const char *operation, void *data, l_int32 nbytes)
Definition: utils.c:873
SARRAY * sarraySort(SARRAY *saout, SARRAY *sain, l_int32 sortorder)
Definition: sarray.c:802
l_int32 sarrayGetCount(SARRAY *sa)
Definition: sarray.c:436
void sarrayDestroy(SARRAY **psa)
Definition: sarray.c:270
char * genPathname(const char *dir, const char *fname)
Definition: utils.c:1150
PIX * pixRead(const char *filename)
Definition: readfile.c:64
l_int32 pixWrite(const char *filename, PIX *pix, l_int32 format)
Definition: writefile.c:113
l_int32 sarrayAddString(SARRAY *sa, char *string, l_int32 copyflag)
Definition: sarray.c:333
char * stringJoin(const char *src1, const char *src2)
Definition: utils.c:445
PIX * pixClone(PIX *pixs)
Definition: pix1.c:197
char * sarrayToString(SARRAY *sa, l_int32 addnlflag)
Definition: sarray.c:528
l_int32 pixGetWidth(PIX *pix)
Definition: pix1.c:389
#define L_MIN
Definition: manifests.h:69
#define L_MAX
Definition: manifests.h:80
@ L_SORT_INCREASING
Definition: pix.h:373
char * fname
Definition: plain2.c:121
#define index(s, c)
Definition: plain2.h:351
Definition: dirent.h:44
Definition: pix.h:51
Definition: array.h:70
Definition: dirent.h:34
char d_name[260]
Definition: dirent.h:37
unsigned char d_type
Definition: dirent.h:64
Definition: mendex.h:20
*job_name strlen((char *) job_name) - 4)
static const l_int32 MIN_THUMB_WIDTH
Definition: viewfiles.c:40
static const l_int32 MIN_VIEW_WIDTH
Definition: viewfiles.c:41
SARRAY * getSortedPathnamesInDirectory(const char *dirname, l_int32 firstpage, l_int32 npages)
Definition: viewfiles.c:288
SARRAY * getFilenamesInDirectory(const char *dirname)
Definition: viewfiles.c:241
static const l_int32 DEFAULT_THUMB_WIDTH
Definition: viewfiles.c:38
static const l_int32 BUF_SIZE
Definition: viewfiles.c:37
l_int32 pixHtmlViewer(const char *dirin, const char *dirout, const char *rootname, l_int32 thumbwidth, l_int32 viewwidth, l_int32 copyorig)
Definition: viewfiles.c:72
static const l_int32 DEFAULT_VIEW_WIDTH
Definition: viewfiles.c:39