/*! * getSortedPathnamesInDirectory() * * Input: directory name * substr (<optional> substring filter on filenames; can be NULL) * firstpage (0-based) * npages (use 0 for all to the end) * Return: sarray of sorted pathnames, or NULL on error * * Notes: * (1) If 'substr' is not NULL, only filenames that contain * the substring can be returned. If 'substr' is NULL, * none of the filenames are filtered out. * (2) The files in the directory, after optional filtering by * the substring, are lexically sorted in increasing order. * The full pathnames are returned for the requested sequence. * If no files are found after filtering, returns an empty sarray. */ SARRAY * getSortedPathnamesInDirectory(const char *dirname, const char *substr, l_int32 firstpage, l_int32 npages) { char *fname, *fullname; l_int32 i, nfiles, lastpage; SARRAY *sa, *safiles, *saout; PROCNAME("getSortedPathnamesInDirectory"); if (!dirname) return (SARRAY *)ERROR_PTR("dirname not defined", procName, NULL); if ((sa = getFilenamesInDirectory(dirname)) == NULL) return (SARRAY *)ERROR_PTR("sa not made", procName, NULL); safiles = sarraySelectBySubstring(sa, substr); sarrayDestroy(&sa); nfiles = sarrayGetCount(safiles); if (nfiles == 0) { L_WARNING("no files found", procName); return safiles; } sarraySort(safiles, safiles, L_SORT_INCREASING); firstpage = L_MIN(L_MAX(firstpage, 0), nfiles - 1); if (npages == 0) npages = nfiles - firstpage; lastpage = L_MIN(firstpage + npages - 1, nfiles - 1); saout = sarrayCreate(lastpage - firstpage + 1); for (i = firstpage; i <= lastpage; i++) { fname = sarrayGetString(safiles, i, L_NOCOPY); fullname = genPathname(dirname, fname); sarrayAddString(saout, fullname, L_INSERT); } sarrayDestroy(&safiles); return saout; }
void TestLeptCpRm(L_REGPARAMS *rp, const char *srctail, const char *newdir, const char *newtail) { char realnewdir[256], newnewdir[256]; char *realtail, *newsrc, *fname; l_int32 nfiles1, nfiles2, nfiles3; SARRAY *sa; /* Remove old version if it exists */ realtail = (newtail) ? stringNew(newtail) : stringNew(srctail); lept_rm(newdir, realtail); makeTempDirname(realnewdir, 256, newdir); if (rp->display) { fprintf(stderr, "\nInput: srctail = %s, newdir = %s, newtail = %s\n", srctail, newdir, newtail); fprintf(stderr, " realnewdir = %s, realtail = %s\n", realnewdir, realtail); } sa = getFilenamesInDirectory(realnewdir); nfiles1 = sarrayGetCount(sa); sarrayDestroy(&sa); /* Copy */ lept_cp(srctail, newdir, newtail, &fname); sa = getFilenamesInDirectory(realnewdir); nfiles2 = sarrayGetCount(sa); if (rp->display) { fprintf(stderr, " File copied to directory: %s\n", realnewdir); fprintf(stderr, " ... with this filename: %s\n", fname); fprintf(stderr, " delta files should be 1: %d\n", nfiles2 - nfiles1); } regTestCompareValues(rp, 1, nfiles2 - nfiles1, 0.0); /* '1' */ sarrayDestroy(&sa); lept_free(fname); /* Remove it */ lept_rm(newdir, realtail); sa = getFilenamesInDirectory(realnewdir); nfiles2 = sarrayGetCount(sa); if (rp->display) { fprintf(stderr, " File removed from directory: %s\n", realnewdir); fprintf(stderr, " delta files should be 0: %d\n", nfiles2 - nfiles1); } regTestCompareValues(rp, 0, nfiles2 - nfiles1, 0.0); /* '2' */ sarrayDestroy(&sa); /* Copy it again ... */ lept_cp(srctail, newdir, newtail, &fname); if (rp->display) fprintf(stderr, " File copied to: %s\n", fname); /* move it elsewhere ... */ lept_rmdir("junko"); /* clear out this directory */ lept_mkdir("junko"); newsrc = pathJoin(realnewdir, realtail); lept_mv(newsrc, "junko", NULL, &fname); if (rp->display) { fprintf(stderr, " Move file at: %s\n", newsrc); fprintf(stderr, " ... to: %s\n", fname); } lept_free(fname); lept_free(newsrc); makeTempDirname(newnewdir, 256, "junko"); if (rp->display) fprintf(stderr, " In this directory: %s\n", newnewdir); sa = getFilenamesInDirectory(newnewdir); /* check if it landed ok */ nfiles3 = sarrayGetCount(sa); if (rp->display) fprintf(stderr, " num files should be 1: %d\n", nfiles3); regTestCompareValues(rp, 1, nfiles3, 0.0); /* '3' */ sarrayDestroy(&sa); /* and verify it was removed from the original location */ sa = getFilenamesInDirectory(realnewdir); /* check if it was removed */ nfiles2 = sarrayGetCount(sa); if (rp->display) { fprintf(stderr, " In this directory: %s\n", realnewdir); fprintf(stderr, " delta files should be 0: %d\n", nfiles2 - nfiles1); } regTestCompareValues(rp, 0, nfiles2 - nfiles1, 0.0); /* '4' */ sarrayDestroy(&sa); lept_free(realtail); }
/*! * pixHtmlViewer() * * Input: dirin: directory of input image files * dirout: directory for output files * rootname: root name for output files * thumbwidth: width of thumb images * (in pixels; use 0 for default) * viewwidth: maximum width of view images (no up-scaling) * (in pixels; use 0 for default) * copyorig: 1 to copy originals to dirout; 0 otherwise * Return: 0 if OK; 1 on error * * Notes: * (1) The thumb and view reduced images are generated, * along with two html files: * <rootname>.html and <rootname>-links.html * (2) The thumb and view files are named * <rootname>_thumb_xxx.jpg * <rootname>_view_xxx.jpg * With this naming scheme, any number of input directories * of images can be processed into views and thumbs * and placed in the same output directory. */ l_int32 pixHtmlViewer(const char *dirin, const char *dirout, const char *rootname, l_int32 thumbwidth, l_int32 viewwidth, l_int32 copyorig) { char *fname, *fullname, *outname; char *mainname, *linkname, *linknameshort; char *viewfile, *thumbfile; char *shtml, *slink; char charbuf[L_BUF_SIZE]; char htmlstring[] = "<html>"; char framestring[] = "</frameset></html>"; l_int32 i, nfiles, index, w, nimages, ret; l_float32 factor; PIX *pix, *pixthumb, *pixview; SARRAY *safiles, *sathumbs, *saviews, *sahtml, *salink; PROCNAME("pixHtmlViewer"); if (!dirin) return ERROR_INT("dirin not defined", procName, 1); if (!dirout) return ERROR_INT("dirout not defined", procName, 1); if (!rootname) return ERROR_INT("rootname not defined", procName, 1); if (thumbwidth == 0) thumbwidth = DEFAULT_THUMB_WIDTH; if (thumbwidth < MIN_THUMB_WIDTH) { L_WARNING("thumbwidth too small; using min value\n", procName); thumbwidth = MIN_THUMB_WIDTH; } if (viewwidth == 0) viewwidth = DEFAULT_VIEW_WIDTH; if (viewwidth < MIN_VIEW_WIDTH) { L_WARNING("viewwidth too small; using min value\n", procName); viewwidth = MIN_VIEW_WIDTH; } /* Make the output directory if it doesn't already exist */ #ifndef _WIN32 sprintf(charbuf, "mkdir -p %s", dirout); ret = system(charbuf); #else ret = CreateDirectory(dirout, NULL) ? 0 : 1; #endif /* !_WIN32 */ if (ret) { L_ERROR("output directory %s not made\n", procName, dirout); return 1; } /* Capture the filenames in the input directory */ if ((safiles = getFilenamesInDirectory(dirin)) == NULL) return ERROR_INT("safiles not made", procName, 1); /* Generate output text file names */ sprintf(charbuf, "%s/%s.html", dirout, rootname); mainname = stringNew(charbuf); sprintf(charbuf, "%s/%s-links.html", dirout, rootname); linkname = stringNew(charbuf); linknameshort = stringJoin(rootname, "-links.html"); if ((sathumbs = sarrayCreate(0)) == NULL) return ERROR_INT("sathumbs not made", procName, 1); if ((saviews = sarrayCreate(0)) == NULL) return ERROR_INT("saviews not made", procName, 1); /* Generate the thumbs and views */ nfiles = sarrayGetCount(safiles); index = 0; for (i = 0; i < nfiles; i++) { fname = sarrayGetString(safiles, i, L_NOCOPY); fullname = genPathname(dirin, fname); fprintf(stderr, "name: %s\n", fullname); if ((pix = pixRead(fullname)) == NULL) { fprintf(stderr, "file %s not a readable image\n", fullname); FREE(fullname); continue; } FREE(fullname); if (copyorig) { outname = genPathname(dirout, fname); pixWrite(outname, pix, IFF_JFIF_JPEG); FREE(outname); } /* Make and store the thumb */ w = pixGetWidth(pix); factor = (l_float32)thumbwidth / (l_float32)w; if ((pixthumb = pixScale(pix, factor, factor)) == NULL) return ERROR_INT("pixthumb not made", procName, 1); sprintf(charbuf, "%s_thumb_%03d.jpg", rootname, index); sarrayAddString(sathumbs, charbuf, L_COPY); outname = genPathname(dirout, charbuf); pixWrite(outname, pixthumb, IFF_JFIF_JPEG); FREE(outname); pixDestroy(&pixthumb); /* Make and store the view */ factor = (l_float32)viewwidth / (l_float32)w; if (factor >= 1.0) { pixview = pixClone(pix); /* no upscaling */ } else { if ((pixview = pixScale(pix, factor, factor)) == NULL) return ERROR_INT("pixview not made", procName, 1); } sprintf(charbuf, "%s_view_%03d.jpg", rootname, index); sarrayAddString(saviews, charbuf, L_COPY); outname = genPathname(dirout, charbuf); pixWrite(outname, pixview, IFF_JFIF_JPEG); FREE(outname); pixDestroy(&pixview); pixDestroy(&pix); index++; } /* Generate the main html file */ if ((sahtml = sarrayCreate(0)) == NULL) return ERROR_INT("sahtml not made", procName, 1); sarrayAddString(sahtml, htmlstring, L_COPY); sprintf(charbuf, "<frameset cols=\"%d, *\">", thumbwidth + 30); sarrayAddString(sahtml, charbuf, L_COPY); sprintf(charbuf, "<frame name=\"thumbs\" src=\"%s\">", linknameshort); sarrayAddString(sahtml, charbuf, L_COPY); sprintf(charbuf, "<frame name=\"views\" src=\"%s\">", sarrayGetString(saviews, 0, L_NOCOPY)); sarrayAddString(sahtml, charbuf, L_COPY); sarrayAddString(sahtml, framestring, L_COPY); shtml = sarrayToString(sahtml, 1); l_binaryWrite(mainname, "w", shtml, strlen(shtml)); FREE(shtml); FREE(mainname); /* Generate the link html file */ nimages = sarrayGetCount(saviews); fprintf(stderr, "num. images = %d\n", nimages); if ((salink = sarrayCreate(0)) == NULL) return ERROR_INT("salink not made", procName, 1); for (i = 0; i < nimages; i++) { viewfile = sarrayGetString(saviews, i, L_NOCOPY); thumbfile = sarrayGetString(sathumbs, i, L_NOCOPY); sprintf(charbuf, "<a href=\"%s\" TARGET=views><img src=\"%s\"></a>", viewfile, thumbfile); sarrayAddString(salink, charbuf, L_COPY); } slink = sarrayToString(salink, 1); l_binaryWrite(linkname, "w", slink, strlen(slink)); FREE(slink); FREE(linkname); FREE(linknameshort); sarrayDestroy(&safiles); sarrayDestroy(&sathumbs); sarrayDestroy(&saviews); sarrayDestroy(&sahtml); sarrayDestroy(&salink); return 0; }
main(int argc, char **argv) { char *filein, *fileout, *str, *fname, *filename; char buffer[512]; l_int32 i, count, npages, length; FILE *fp; NUMA *naflags, *nasizes; PIX *pix, *pixd; PIXA *pixa; PIXCMAP *cmap; SARRAY *savals, *satypes, *sa; static char mainName[] = "mtifftest"; if (argc != 3) exit(ERROR_INT(" Syntax: mtifftest filein fileout", mainName, 1)); filein = argv[1]; fileout = argv[2]; #if 1 /* ------------------ Test multipage I/O -------------------*/ /* This puts every image file in the directory with a string * match to "weasel" into a multipage tiff file. * Images with 1 bpp are coded as g4; the others as zip. * It then reads back into a pix and displays. */ writeMultipageTiff(".", "weasel", "/tmp/junkout.tif"); pixa = pixaReadMultipageTiff("/tmp/junkout.tif"); pixd = pixaDisplayTiledInRows(pixa, 1, 1200, 0.5, 0, 15, 4); pixDisplay(pixd, 100, 0); pixDestroy(&pixd); pixd = pixaDisplayTiledInRows(pixa, 8, 1200, 0.8, 0, 15, 4); pixDisplay(pixd, 100, 200); pixDestroy(&pixd); pixd = pixaDisplayTiledInRows(pixa, 32, 1200, 1.2, 0, 15, 4); pixDisplay(pixd, 100, 400); pixDestroy(&pixd); pixaDestroy(&pixa); #endif #if 0 /* ------------ Test single-to-multipage I/O -------------------*/ /* Use 'filein' to specify a directory of tiff files. * Read them in and generate a multipage tiff file. * Then convert that to a G4 compressed and ascii85 encoded * PS file. */ sa = getFilenamesInDirectory(filein); sarrayWriteStream(stderr, sa); sarraySort(sa, sa, L_SORT_INCREASING); sarrayWriteStream(stderr, sa); npages = sarrayGetCount(sa); for (i = 0; i < npages; i++) { fname = sarrayGetString(sa, i, 0); filename = genPathname(filein, fname); pix = pixRead(filename); if (!pix) continue; if (i == 0) pixWriteTiff(tempmtiff, pix, IFF_TIFF_G4, "w+"); else pixWriteTiff(tempmtiff, pix, IFF_TIFF_G4, "a"); pixDestroy(&pix); lept_free(filename); } /* write it out as a PS file */ convertTiffMultipageToPS(tempmtiff, fileout, NULL, 0.95); sarrayDestroy(&sa); #endif #if 0 /* ------------------ Test multipage I/O -------------------*/ /* read count of tiff multipage */ fp = lept_fopen(filein, "rb"); if (fileFormatIsTiff(fp)) { tiffGetCount(fp, &npages); fprintf(stderr, " Tiff: %d page\n", npages); } else exit(ERROR_INT(" file not tiff", mainName, 1)); lept_fclose(fp); /* split into separate page files */ for (i = 0; i < npages + 1; i++) { /* read one beyond to catch error */ pix = pixReadTiff(filein, i); if (!pix) continue; sprintf(buffer, "/tmp/junkout.%d.tif", i); pixWrite(buffer, pix, IFF_TIFF_G4); pixDestroy(&pix); } /* read separate page files and write reversed file */ for (i = npages - 1; i >= 0; i--) { sprintf(buffer, "/tmp/junkout.%d.tif", i); pix = pixRead(buffer); if (!pix) continue; if (i == npages - 1) pixWriteTiff(tempmtiff, pix, IFF_TIFF_G4, "w+"); else pixWriteTiff(tempmtiff, pix, IFF_TIFF_G4, "a"); pixDestroy(&pix); } /* read reversed file and reverse again */ pixa = pixaCreate(npages); for (i = 0; i < 5; i++) { pix = pixReadTiff(tempmtiff, i); pixaAddPix(pixa, pix, L_INSERT); } for (i = npages - 1; i >= 0; i--) { pix = pixaGetPix(pixa, i, L_CLONE); if (i == npages - 1) pixWriteTiff(tempnewmtiff, pix, IFF_TIFF_G4, "w+"); else pixWriteTiff(tempnewmtiff, pix, IFF_TIFF_G4, "a"); pixDestroy(&pix); } pixaDestroy(&pixa); #endif #if 0 /* ----- test adding custom public tags to a tiff header ----- */ pix = pixRead(filein); naflags = numaCreate(10); savals = sarrayCreate(10); satypes = sarrayCreate(10); nasizes = numaCreate(10); /* numaAddNumber(naflags, TIFFTAG_XMLPACKET); */ /* XMP: 700 */ numaAddNumber(naflags, 700); str = "<xmp>This is a Fake XMP packet</xmp>\n<text>Guess what ...?</text>"; length = strlen(str); sarrayAddString(savals, str, 1); sarrayAddString(satypes, "char*", 1); numaAddNumber(nasizes, length); /* get it all */ numaAddNumber(naflags, 269); /* DOCUMENTNAME */ sarrayAddString(savals, "One silly title", 1); sarrayAddString(satypes, "char*", 1); numaAddNumber(naflags, 270); /* IMAGEDESCRIPTION */ sarrayAddString(savals, "One page of text", 1); sarrayAddString(satypes, "char*", 1); /* the max sample is used by rendering programs * to scale the dynamic range */ numaAddNumber(naflags, 281); /* MAXSAMPLEVALUE */ sarrayAddString(savals, "4", 1); sarrayAddString(satypes, "l_uint16", 1); /* note that date is required to be a 20 byte string */ numaAddNumber(naflags, 306); /* DATETIME */ sarrayAddString(savals, "2004:10:11 09:35:15", 1); sarrayAddString(satypes, "char*", 1); /* note that page number requires 2 l_uint16 input */ numaAddNumber(naflags, 297); /* PAGENUMBER */ sarrayAddString(savals, "1-412", 1); sarrayAddString(satypes, "l_uint16-l_uint16", 1); pixWriteTiffCustom(fileout, pix, IFF_TIFF_G4, "w", naflags, savals, satypes, nasizes); fprintTiffInfo(stderr, fileout); numaDestroy(&naflags); numaDestroy(&nasizes); sarrayDestroy(&savals); sarrayDestroy(&satypes); pixDestroy(&pix); #endif exit(0); }
int main(int argc, char **argv) { char *dirin, *fileout, *fname, *fullname; l_int32 depth, width, background, i, nfiles; l_float32 scale; SARRAY *safiles; PIX *pix, *pixt, *pixd; PIXA *pixa; static char mainName[] = "maketile"; if (argc != 7) return ERROR_INT( "Syntax: maketile dirin depth scale width background fileout", mainName, 1); dirin = argv[1]; depth = atoi(argv[2]); scale = atof(argv[3]); width = atoi(argv[4]); background = atoi(argv[5]); fileout = argv[6]; /* capture the filenames in the input directory; ignore directories */ if ((safiles = getFilenamesInDirectory(dirin)) == NULL) return ERROR_INT("safiles not made", mainName, 1); /* capture images with the requisite depth */ nfiles = sarrayGetCount(safiles); pixa = pixaCreate(nfiles); for (i = 0; i < nfiles; i++) { fname = sarrayGetString(safiles, i, 0); fullname = genPathname(dirin, fname); pix = pixRead(fullname); lept_free(fullname); if (!pix) continue; if (pixGetDepth(pix) != depth) { pixDestroy(&pix); continue; } if (pixGetHeight(pix) > 5000) { fprintf(stderr, "%s too tall\n", fname); continue; } pixt = pixScale(pix, scale, scale); pixaAddPix(pixa, pixt, L_INSERT); pixDestroy(&pix); /* fprintf(stderr, "%d..", i); */ } fprintf(stderr, "\n"); /* tile them */ pixd = pixaDisplayTiled(pixa, width, background, 15); if (depth < 8) pixWrite(fileout, pixd, IFF_PNG); else pixWrite(fileout, pixd, IFF_JFIF_JPEG); pixaDestroy(&pixa); pixDestroy(&pixd); sarrayDestroy(&safiles); return 0; }