/* * sarrayConvertFilesToPS() * * Input: sarray (of full path names) * res (typ. 300 or 600 ppi) * fileout (output ps file) * Return: 0 if OK, 1 on error * * Notes: * (1) See convertFilesToPS() */ l_int32 sarrayConvertFilesToPS(SARRAY *sa, l_int32 res, const char *fileout) { char *fname; l_int32 i, nfiles, index, firstfile, ret, format; PROCNAME("sarrayConvertFilesToPS"); if (!sa) return ERROR_INT("sa not defined", procName, 1); if (!fileout) return ERROR_INT("fileout not defined", procName, 1); if (res <= 0) { L_INFO("setting res to 300 ppi", procName); res = 300; } if (res < 10 || res > 4000) L_WARNING("res is typically in the range 300-600 ppi", procName); nfiles = sarrayGetCount(sa); firstfile = TRUE; for (i = 0, index = 0; i < nfiles; i++) { fname = sarrayGetString(sa, i, L_NOCOPY); ret = pixReadHeader(fname, &format, NULL, NULL, NULL, NULL, NULL); if (ret) continue; if (format == IFF_UNKNOWN) continue; writeImageCompressedToPSFile(fname, fileout, res, &firstfile, &index); } return 0; }
main(int argc, char **argv) { char *filein, *fileout; l_int32 format; PIX *pixs; static char mainName[] = "converttobmp"; if (argc != 2 && argc != 3) { fprintf(stderr, "Syntax: converttobmp filein [fileout]\n"); return 1; } filein = argv[1]; if (argc == 2) fileout = (char *)"/tmp/fileout.bmp"; else /* argc == 3 */ fileout = argv[2]; pixReadHeader(filein, &format, NULL, NULL, NULL, NULL, NULL); if (format == IFF_BMP) return 1; /* no conversion required */ pixs = pixRead(filein); pixWrite(fileout, pixs, IFF_BMP); return 0; }
/* Retrieve header data from file and from array in memory */ static l_int32 get_header_data(const char *filename) { l_uint8 *data; l_int32 ret1, ret2, format1, format2; l_int32 w1, w2, h1, h2, d1, d2, bps1, bps2, spp1, spp2, iscmap1, iscmap2; size_t nbytes1, nbytes2; /* Read header from file */ nbytes1 = nbytesInFile(filename); ret1 = pixReadHeader(filename, &format1, &w1, &h1, &bps1, &spp1, &iscmap1); d1 = bps1 * spp1; if (d1 == 24) d1 = 32; if (ret1) fprintf(stderr, "Error: couldn't read header data from file: %s\n", filename); else { fprintf(stderr, "Format data for image %s with format %s:\n" " nbytes = %ld, size (w, h, d) = (%d, %d, %d)\n" " bps = %d, spp = %d, iscmap = %d\n", filename, ImageFileFormatExtensions[format1], nbytes1, w1, h1, d1, bps1, spp1, iscmap1); if (format1 != IFF_PNG) { fprintf(stderr, "Error: format is %d; should be %d\n", format1, IFF_PNG); ret1 = 1; } } /* Read header from array in memory */ ret2 = 0; #if HAVE_FMEMOPEN data = l_binaryRead(filename, &nbytes2); ret2 = pixReadHeaderMem(data, nbytes2, &format2, &w2, &h2, &bps2, &spp2, &iscmap2); lept_free(data); d2 = bps2 * spp2; if (d2 == 24) d2 = 32; if (ret2) fprintf(stderr, "Error: couldn't mem-read header data: %s\n", filename); else { if (nbytes1 != nbytes2 || format1 != format2 || w1 != w2 || h1 != h2 || d1 != d2 || bps1 != bps2 || spp1 != spp2 || iscmap1 != iscmap2) { fprintf(stderr, "Incomsistency reading image %s with format %s\n", filename, ImageFileFormatExtensions[IFF_PNG]); ret2 = 1; } } #endif /* HAVE_FMEMOPEN */ return ret1 || ret2; }
/* * sarrayConvertFilesFittedToPS() * * Input: sarray (of full path names) * xpts, ypts (desired size in printer points; use 0 for default) * fileout (output ps file) * Return: 0 if OK, 1 on error * * Notes: * (1) See convertFilesFittedToPS() */ l_int32 sarrayConvertFilesFittedToPS(SARRAY *sa, l_float32 xpts, l_float32 ypts, const char *fileout) { char *fname; l_int32 ret, i, w, h, nfiles, index, firstfile, format, res; PROCNAME("sarrayConvertFilesFittedToPS"); if (!sa) return ERROR_INT("sa not defined", procName, 1); if (!fileout) return ERROR_INT("fileout not defined", procName, 1); if (xpts <= 0.0) { L_INFO("setting xpts to 612.0", procName); xpts = 612.0; } if (ypts <= 0.0) { L_INFO("setting ypts to 792.0", procName); ypts = 792.0; } if (xpts < 100.0 || xpts > 2000.0 || ypts < 100.0 || ypts > 2000.0) L_WARNING("xpts,ypts are typically in the range 500-800", procName); nfiles = sarrayGetCount(sa); firstfile = TRUE; for (i = 0, index = 0; i < nfiles; i++) { fname = sarrayGetString(sa, i, L_NOCOPY); ret = pixReadHeader(fname, &format, &w, &h, NULL, NULL, NULL); if (ret) continue; if (format == IFF_UNKNOWN) continue; /* Be sure the entire image is wrapped */ if (xpts * h < ypts * w) res = (l_int32)((l_float32)w * 72.0 / xpts); else res = (l_int32)((l_float32)h * 72.0 / ypts); writeImageCompressedToPSFile(fname, fileout, res, &firstfile, &index); } return 0; }
/* Retrieve header data from file */ static l_int32 get_header_data(const char *filename, l_int32 true_format) { char buf[64]; l_uint8 *data; l_int32 ret1, ret2, format1, format2; l_int32 w1, w2, h1, h2, d1, d2, bps1, bps2, spp1, spp2, iscmap1, iscmap2; size_t size1, size2; /* Fail silently if library is not available */ #if !HAVE_LIBJPEG if (true_format == IFF_JFIF_JPEG) return 0; #endif /* !HAVE_LIBJPEG */ #if !HAVE_LIBPNG if (true_format == IFF_PNG) return 0; #endif /* !HAVE_LIBPNG */ #if !HAVE_LIBTIFF if (true_format == IFF_TIFF_G3 || true_format == IFF_TIFF_G4 || true_format == IFF_TIFF_ZIP || true_format == IFF_TIFF_LZW || true_format == IFF_TIFF_PACKBITS || true_format == IFF_TIFF_RLE || true_format == IFF_TIFF) return 0; #endif /* !HAVE_LIBTIFF */ /* Read header from file */ size1 = nbytesInFile(filename); ret1 = pixReadHeader(filename, &format1, &w1, &h1, &bps1, &spp1, &iscmap1); d1 = bps1 * spp1; if (d1 == 24) d1 = 32; if (ret1) fprintf(stderr, "Error: couldn't read header data: %s\n", filename); else { if (format1 > IFF_PNG && format1 < IFF_PNM) { get_tiff_compression_name(buf, format1); fprintf(stderr, "Format data for image %s with format %s:\n" " nbytes = %ld, size (w, h, d) = (%d, %d, %d)\n" " bps = %d, spp = %d, iscmap = %d\n", filename, buf, size1, w1, h1, d1, bps1, spp1, iscmap1); } else { fprintf(stderr, "Format data for image %s with format %s:\n" " nbytes = %ld, size (w, h, d) = (%d, %d, %d)\n" " bps = %d, spp = %d, iscmap = %d\n", filename, ImageFileFormatExtensions[format1], size1, w1, h1, d1, bps1, spp1, iscmap1); } if (format1 != true_format) { fprintf(stderr, "Error: format is %d; should be %d\n", format1, true_format); ret1 = 1; } } /* Read header from array in memory */ data = l_binaryRead(filename, &size2); ret2 = pixReadHeaderMem(data, size2, &format2, &w2, &h2, &bps2, &spp2, &iscmap2); lept_free(data); d2 = bps2 * spp2; if (d2 == 24) d2 = 32; if (ret2) fprintf(stderr, "Error: couldn't mem-read header data: %s\n", filename); else { if (size1 != size2 || format1 != format2 || w1 != w2 || h1 != h2 || d1 != d2 || bps1 != bps2 || spp1 != spp2 || iscmap1 != iscmap2) { fprintf(stderr, "Incomsistency reading image %s with format %s\n", filename, buf); ret2 = 1; } } return ret1 || ret2; }
main(int argc, char **argv) { char *text; l_int32 w, h, d, wpl, count, npages, color, format, bps, spp, iscmap; FILE *fp; PIX *pix; PIXCMAP *cmap; char *filein; static char mainName[] = "fileinfo"; if (argc != 2) exit(ERROR_INT(" Syntax: fileinfo filein", mainName, 1)); filein = argv[1]; l_pngSetStrip16To8(0); /* to preserve 16 bpp if format is png */ /* Read the full image */ if ((pix = pixRead(filein)) == NULL) exit(ERROR_INT("image not returned from file", mainName, 1)); format = pixGetInputFormat(pix); pixGetDimensions(pix, &w, &h, &d); wpl = pixGetWpl(pix); fprintf(stderr, "Reading the full image:\n"); fprintf(stderr, " Input image format type: %s\n", ImageFileFormatExtensions[format]); fprintf(stderr, " w = %d, h = %d, d = %d, wpl = %d\n", w, h, d, wpl); fprintf(stderr, " xres = %d, yres = %d\n", pixGetXRes(pix), pixGetYRes(pix)); text = pixGetText(pix); if (text) /* not null */ fprintf(stderr, " Text: %s\n", text); cmap = pixGetColormap(pix); if (cmap) { pixcmapHasColor(cmap, &color); if (color) fprintf(stderr, " Colormap exists and has color values:"); else fprintf(stderr, " Colormap exists and has only gray values:"); pixcmapWriteStream(stderr, pixGetColormap(pix)); } else fprintf(stderr, " Colormap does not exist.\n"); if (format == IFF_TIFF || format == IFF_TIFF_G4 || format == IFF_TIFF_G3 || format == IFF_TIFF_PACKBITS) { fprintf(stderr, " Tiff header information:\n"); fp = lept_fopen(filein, "rb"); tiffGetCount(fp, &npages); lept_fclose(fp); if (npages == 1) fprintf(stderr, " One page in file\n"); else fprintf(stderr, " %d pages in file\n", npages); fprintTiffInfo(stderr, filein); } if (d == 1) { pixCountPixels(pix, &count, NULL); fprintf(stderr, " 1 bpp: pixel ratio ON/OFF = %6.3f\n", (l_float32)count / (l_float32)(pixGetWidth(pix) * pixGetHeight(pix))); } pixDestroy(&pix); /* Test pixReadHeader() */ if (pixReadHeader(filein, &format, &w, &h, &bps, &spp, &iscmap)) { fprintf(stderr, "Failure to read header!\n"); return 1; } fprintf(stderr, "Reading just the header:\n"); fprintf(stderr, " Input image format type: %s\n", ImageFileFormatExtensions[format]); fprintf(stderr, " w = %d, h = %d, d = %d, bps = %d, spp = %d, iscmap = %d\n", w, h, d, bps, spp, iscmap); return 0; }
main(int argc, char **argv) { char buf[256]; size_t nbytes; l_int32 i, w, h, success, display; l_int32 format, bps, spp, iscmap, format2, w2, h2, bps2, spp2, iscmap2; l_uint8 *data; l_uint32 *data32, *data32r; BOX *box; FILE *fp; PIX *pixs, *pixt, *pixt2, *pixd; if (regTestSetup(argc, argv, &fp, &display, &success, NULL)) return 1; /* Test basic serialization/deserialization */ for (i = 0; i < nfiles; i++) { pixs = pixRead(filename[i]); /* Serialize to memory */ pixSerializeToMemory(pixs, &data32, &nbytes); /* Just for fun, write and read back from file */ arrayWrite("/tmp/array", "w", data32, nbytes); data32r = (l_uint32 *)arrayRead("/tmp/array", (l_int32 *)(&nbytes)); /* Deserialize */ pixd = pixDeserializeFromMemory(data32r, nbytes); regTestComparePix(fp, argv, pixs, pixd, i, &success); pixDestroy(&pixd); pixDestroy(&pixs); FREE(data32); FREE(data32r); } /* Test read/write fileio interface */ for (i = 0; i < nfiles; i++) { pixs = pixRead(filename[i]); pixGetDimensions(pixs, &w, &h, NULL); box = boxCreate(0, 0, L_MIN(150, w), L_MIN(150, h)); pixt = pixClipRectangle(pixs, box, NULL); boxDestroy(&box); snprintf(buf, sizeof(buf), "/tmp/pixs.%d", i); pixWrite(buf, pixt, IFF_SPIX); regTestCheckFile(fp, argv, buf, i, &success); pixt2 = pixRead(buf); regTestComparePix(fp, argv, pixt, pixt2, nfiles + i, &success); pixDestroy(&pixs); pixDestroy(&pixt); pixDestroy(&pixt2); } /* Test read header. Note that for rgb input, spp = 3, * but for 32 bpp spix, we set spp = 4. */ for (i = 0; i < nfiles; i++) { pixs = pixRead(filename[i]); pixWriteMem(&data, &nbytes, pixs, IFF_SPIX); pixReadHeader(filename[i], &format, &w, &h, &bps, &spp, &iscmap); pixReadHeaderMem(data, nbytes, &format2, &w2, &h2, &bps2, &spp2, &iscmap2); if (format2 != 16 || w != w2 || h != h2 || bps != bps2 || iscmap != iscmap2) { if (fp) fprintf(fp, "Failure comparing data"); else fprintf(stderr, "Failure comparing data"); success = FALSE; } pixDestroy(&pixs); FREE(data); } #if 0 /* Do timing */ for (i = 0; i < nfiles; i++) { pixs = pixRead(filename[i]); startTimer(); pixSerializeToMemory(pixs, &data32, &nbytes); pixd = pixDeserializeFromMemory(data32, nbytes); fprintf(stderr, "Time for %s: %7.3f sec\n", filename[i], stopTimer()); FREE(data32); pixDestroy(&pixs); pixDestroy(&pixd); } #endif regTestCleanup(argc, argv, fp, success, NULL); return 0; }
int main(int argc, char **argv) { char buf[256]; size_t size; l_int32 i, w, h; l_int32 format, bps, spp, iscmap, format2, w2, h2, bps2, spp2, iscmap2; l_uint8 *data; l_uint32 *data32, *data32r; BOX *box; PIX *pixs, *pixt, *pixt2, *pixd; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; /* Test basic serialization/deserialization */ data32 = NULL; for (i = 0; i < nfiles; i++) { pixs = pixRead(filename[i]); /* Serialize to memory */ pixSerializeToMemory(pixs, &data32, &size); /* Just for fun, write and read back from file */ l_binaryWrite("/tmp/regout/array", "w", data32, size); data32r = (l_uint32 *)l_binaryRead("/tmp/regout/array", &size); /* Deserialize */ pixd = pixDeserializeFromMemory(data32r, size); regTestComparePix(rp, pixs, pixd); /* i */ pixDestroy(&pixd); pixDestroy(&pixs); lept_free(data32); lept_free(data32r); } /* Test read/write fileio interface */ for (i = 0; i < nfiles; i++) { pixs = pixRead(filename[i]); pixGetDimensions(pixs, &w, &h, NULL); box = boxCreate(0, 0, L_MIN(150, w), L_MIN(150, h)); pixt = pixClipRectangle(pixs, box, NULL); boxDestroy(&box); snprintf(buf, sizeof(buf), "/tmp/regout/pixs.%d.spix", rp->index + 1); pixWrite(buf, pixt, IFF_SPIX); regTestCheckFile(rp, buf); /* nfiles + 2 * i */ pixt2 = pixRead(buf); regTestComparePix(rp, pixt, pixt2); /* nfiles + 2 * i + 1 */ pixDestroy(&pixs); pixDestroy(&pixt); pixDestroy(&pixt2); } /* Test read header. Note that for rgb input, spp = 3, * but for 32 bpp spix, we set spp = 4. */ data = NULL; for (i = 0; i < nfiles; i++) { pixs = pixRead(filename[i]); pixWriteMem(&data, &size, pixs, IFF_SPIX); pixReadHeader(filename[i], &format, &w, &h, &bps, &spp, &iscmap); pixReadHeaderMem(data, size, &format2, &w2, &h2, &bps2, &spp2, &iscmap2); if (format2 != IFF_SPIX || w != w2 || h != h2 || bps != bps2 || iscmap != iscmap2) { if (rp->fp) fprintf(rp->fp, "Failure comparing data"); else fprintf(stderr, "Failure comparing data"); } pixDestroy(&pixs); lept_free(data); } #if 0 /* Do timing */ for (i = 0; i < nfiles; i++) { pixs = pixRead(filename[i]); startTimer(); pixSerializeToMemory(pixs, &data32, &size); pixd = pixDeserializeFromMemory(data32, size); fprintf(stderr, "Time for %s: %7.3f sec\n", filename[i], stopTimer()); lept_free(data32); pixDestroy(&pixs); pixDestroy(&pixd); } #endif return regTestCleanup(rp); }