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
27 /*
28 * htmlviewer.c
29 *
30 * This takes a directory of image files, optionally scales them,
31 * and generates html files to view the scaled images (and thumbnails).
32 *
33 * Input: dirin: directory of input image files
34 * dirout: directory for output files
35 * rootname: root name for output files
36 * thumbwidth: width of thumb images, in pixels; use 0 for default
37 * viewwidth: max width of view images, in pixels; use 0 for default
38 *
39 * Example:
40 * mkdir /tmp/lept/lion-in
41 * mkdir /tmp/lept/lion-out
42 * cp lion-page* /tmp/lept/lion-in
43 * htmlviewer /tmp/lept/lion-in /tmp/lept/lion-out lion 200 600
44 * ==> output:
45 * /tmp/lept/lion-out/lion.html (main html file)
46 * /tmp/lept/lion-out/lion-links.html (html file of links)
47 */
48
49 #include <string.h>
50 #include "allheaders.h"
51
52 #ifdef _WIN32
53 #include <windows.h> /* for CreateDirectory() */
54 #endif
55
56 static const l_int32 DEFAULT_THUMB_WIDTH = 120;
57 static const l_int32 DEFAULT_VIEW_WIDTH = 800;
58 static const l_int32 MIN_THUMB_WIDTH = 50;
59 static const l_int32 MIN_VIEW_WIDTH = 300;
60
61 static l_int32 pixHtmlViewer(const char *dirin, const char *dirout,
62 const char *rootname, l_int32 thumbwidth,
63 l_int32 viewwidth);
64 static void WriteFormattedPix(char *fname, PIX *pix);
65
66
main(int argc,char ** argv)67 int main(int argc,
68 char **argv)
69 {
70 char *dirin, *dirout, *rootname;
71 l_int32 thumbwidth, viewwidth;
72 static char mainName[] = "htmlviewer";
73
74 if (argc != 6)
75 return ERROR_INT(
76 " Syntax: htmlviewer dirin dirout rootname thumbwidth viewwidth",
77 mainName, 1);
78 dirin = argv[1];
79 dirout = argv[2];
80 rootname = argv[3];
81 thumbwidth = atoi(argv[4]);
82 viewwidth = atoi(argv[5]);
83 setLeptDebugOK(1);
84
85 pixHtmlViewer(dirin, dirout, rootname, thumbwidth, viewwidth);
86 return 0;
87 }
88
89
90 /*---------------------------------------------------------------------*
91 * Generate smaller images for viewing and write html *
92 *---------------------------------------------------------------------*/
93 /*!
94 * \brief pixHtmlViewer()
95 *
96 * \param[in] dirin directory of input image files
97 * \param[in] dirout directory for output files
98 * \param[in] rootname root name for output files
99 * \param[in] thumbwidth width of thumb images in pixels; use 0 for default
100 * \param[in] viewwidth maximum width of view images no up-scaling
101 * in pixels; use 0 for default
102 * \return 0 if OK; 1 on error
103 *
104 * <pre>
105 * Notes:
106 * (1) The thumb and view reduced images are generated,
107 * along with two html files:
108 * <rootname>.html and <rootname>-links.html
109 * (2) The thumb and view files are named
110 * <rootname>_thumb_xxx.jpg
111 * <rootname>_view_xxx.jpg
112 * With this naming scheme, any number of input directories
113 * of images can be processed into views and thumbs
114 * and placed in the same output directory.
115 * </pre>
116 */
117 static l_int32
pixHtmlViewer(const char * dirin,const char * dirout,const char * rootname,l_int32 thumbwidth,l_int32 viewwidth)118 pixHtmlViewer(const char *dirin,
119 const char *dirout,
120 const char *rootname,
121 l_int32 thumbwidth,
122 l_int32 viewwidth)
123 {
124 char *fname, *fullname, *outname;
125 char *mainname, *linkname, *linknameshort;
126 char *viewfile, *thumbfile;
127 char *shtml, *slink;
128 char buf[512];
129 char htmlstring[] = "<html>";
130 char framestring[] = "</frameset></html>";
131 l_int32 i, nfiles, index, w, d, nimages, ret;
132 l_float32 factor;
133 PIX *pix, *pixthumb, *pixview;
134 SARRAY *safiles, *sathumbs, *saviews, *sahtml, *salink;
135
136 PROCNAME("pixHtmlViewer");
137
138 if (!dirin)
139 return ERROR_INT("dirin not defined", procName, 1);
140 if (!dirout)
141 return ERROR_INT("dirout not defined", procName, 1);
142 if (!rootname)
143 return ERROR_INT("rootname not defined", procName, 1);
144
145 if (thumbwidth == 0)
146 thumbwidth = DEFAULT_THUMB_WIDTH;
147 if (thumbwidth < MIN_THUMB_WIDTH) {
148 L_WARNING("thumbwidth too small; using min value\n", procName);
149 thumbwidth = MIN_THUMB_WIDTH;
150 }
151 if (viewwidth == 0)
152 viewwidth = DEFAULT_VIEW_WIDTH;
153 if (viewwidth < MIN_VIEW_WIDTH) {
154 L_WARNING("viewwidth too small; using min value\n", procName);
155 viewwidth = MIN_VIEW_WIDTH;
156 }
157
158 /* Make the output directory if it doesn't already exist */
159 #ifndef _WIN32
160 snprintf(buf, sizeof(buf), "mkdir -p %s", dirout);
161 ret = system(buf);
162 #else
163 ret = CreateDirectory(dirout, NULL) ? 0 : 1;
164 #endif /* !_WIN32 */
165 if (ret) {
166 L_ERROR("output directory %s not made\n", procName, dirout);
167 return 1;
168 }
169
170 /* Capture the filenames in the input directory */
171 if ((safiles = getFilenamesInDirectory(dirin)) == NULL)
172 return ERROR_INT("safiles not made", procName, 1);
173
174 /* Generate output text file names */
175 snprintf(buf, sizeof(buf), "%s/%s.html", dirout, rootname);
176 mainname = stringNew(buf);
177 snprintf(buf, sizeof(buf), "%s/%s-links.html", dirout, rootname);
178 linkname = stringNew(buf);
179 linknameshort = stringJoin(rootname, "-links.html");
180
181 /* Generate the thumbs and views */
182 sathumbs = sarrayCreate(0);
183 saviews = sarrayCreate(0);
184 nfiles = sarrayGetCount(safiles);
185 index = 0;
186 for (i = 0; i < nfiles; i++) {
187 fname = sarrayGetString(safiles, i, L_NOCOPY);
188 fullname = genPathname(dirin, fname);
189 fprintf(stderr, "name: %s\n", fullname);
190 if ((pix = pixRead(fullname)) == NULL) {
191 fprintf(stderr, "file %s not a readable image\n", fullname);
192 lept_free(fullname);
193 continue;
194 }
195 lept_free(fullname);
196
197 /* Make and store the thumbnail images */
198 pixGetDimensions(pix, &w, NULL, &d);
199 factor = (l_float32)thumbwidth / (l_float32)w;
200 pixthumb = pixScale(pix, factor, factor);
201 snprintf(buf, sizeof(buf), "%s_thumb_%03d", rootname, index);
202 sarrayAddString(sathumbs, buf, L_COPY);
203 outname = genPathname(dirout, buf);
204 WriteFormattedPix(outname, pixthumb);
205 lept_free(outname);
206 pixDestroy(&pixthumb);
207
208 /* Make and store the view images */
209 factor = (l_float32)viewwidth / (l_float32)w;
210 if (factor >= 1.0)
211 pixview = pixClone(pix); /* no upscaling */
212 else
213 pixview = pixScale(pix, factor, factor);
214 snprintf(buf, sizeof(buf), "%s_view_%03d", rootname, index);
215 sarrayAddString(saviews, buf, L_COPY);
216 outname = genPathname(dirout, buf);
217 WriteFormattedPix(outname, pixview);
218 lept_free(outname);
219 pixDestroy(&pixview);
220 pixDestroy(&pix);
221 index++;
222 }
223
224 /* Generate the main html file */
225 sahtml = sarrayCreate(0);
226 sarrayAddString(sahtml, htmlstring, L_COPY);
227 snprintf(buf, sizeof(buf), "<frameset cols=\"%d, *\">", thumbwidth + 30);
228 sarrayAddString(sahtml, buf, L_COPY);
229 snprintf(buf, sizeof(buf), "<frame name=\"thumbs\" src=\"%s\">",
230 linknameshort);
231 sarrayAddString(sahtml, buf, L_COPY);
232 snprintf(buf, sizeof(buf), "<frame name=\"views\" src=\"%s\">",
233 sarrayGetString(saviews, 0, L_NOCOPY));
234 sarrayAddString(sahtml, buf, L_COPY);
235 sarrayAddString(sahtml, framestring, L_COPY);
236 shtml = sarrayToString(sahtml, 1);
237 l_binaryWrite(mainname, "w", shtml, strlen(shtml));
238 fprintf(stderr, "******************************************\n"
239 "Writing html file: %s\n"
240 "******************************************\n", mainname);
241 lept_free(shtml);
242 lept_free(mainname);
243
244 /* Generate the link html file */
245 nimages = sarrayGetCount(saviews);
246 fprintf(stderr, "num. images = %d\n", nimages);
247 salink = sarrayCreate(0);
248 for (i = 0; i < nimages; i++) {
249 viewfile = sarrayGetString(saviews, i, L_NOCOPY);
250 thumbfile = sarrayGetString(sathumbs, i, L_NOCOPY);
251 snprintf(buf, sizeof(buf),
252 "<a href=\"%s\" TARGET=views><img src=\"%s\"></a>",
253 viewfile, thumbfile);
254 sarrayAddString(salink, buf, L_COPY);
255 }
256 slink = sarrayToString(salink, 1);
257 l_binaryWrite(linkname, "w", slink, strlen(slink));
258 lept_free(slink);
259 lept_free(linkname);
260 lept_free(linknameshort);
261 sarrayDestroy(&safiles);
262 sarrayDestroy(&sathumbs);
263 sarrayDestroy(&saviews);
264 sarrayDestroy(&sahtml);
265 sarrayDestroy(&salink);
266 return 0;
267 }
268
269 static void
WriteFormattedPix(char * fname,PIX * pix)270 WriteFormattedPix(char *fname,
271 PIX *pix)
272 {
273 l_int32 d;
274
275 d = pixGetDepth(pix);
276 if (d == 1 || pixGetColormap(pix))
277 pixWrite(fname, pix, IFF_PNG);
278 else
279 pixWrite(fname, pix, IFF_JFIF_JPEG);
280 return;
281 }
282
283