83 #include "allheaders.h" 88 #define WRITE_AS_NAMED 1 93 #elif defined(__APPLE__) 100 static const l_int32 MAX_DISPLAY_WIDTH = 1000;
101 static const l_int32 MAX_DISPLAY_HEIGHT = 800;
102 static const l_int32 MAX_SIZE_FOR_PNG = 200;
105 static const l_float32 DEFAULT_SCALING = 1.0;
116 LEPT_DLL l_int32 NumImageFileFormatExtensions = 19;
117 LEPT_DLL
const char *ImageFileFormatExtensions[] =
145 { {
".bmp", IFF_BMP },
146 {
".jpg", IFF_JFIF_JPEG },
147 {
".jpeg", IFF_JFIF_JPEG },
149 {
".tif", IFF_TIFF },
150 {
".tiff", IFF_TIFF },
155 {
".pdf", IFF_LPDF },
156 {
".webp", IFF_WEBP } };
177 pixaWriteFiles(
const char *rootname,
182 l_int32 i, n, pixformat;
185 PROCNAME(
"pixaWriteFiles");
188 return ERROR_INT(
"rootname not defined", procName, 1);
190 return ERROR_INT(
"pixa not defined", procName, 1);
191 if (format < 0 || format == IFF_UNKNOWN ||
192 format >= NumImageFileFormatExtensions)
193 return ERROR_INT(
"invalid format", procName, 1);
196 for (i = 0; i < n; i++) {
198 if (format == IFF_DEFAULT)
199 pixformat = pixChooseOutputFormat(pix);
202 snprintf(bigbuf,
L_BUF_SIZE,
"%s%03d.%s", rootname, i,
203 ImageFileFormatExtensions[pixformat]);
204 pixWrite(bigbuf, pix, pixformat);
240 pixWrite(
const char *fname,
247 PROCNAME(
"pixWrite");
250 return ERROR_INT(
"pix not defined", procName, 1);
252 return ERROR_INT(
"fname not defined", procName, 1);
257 return ERROR_INT(
"stream not opened", procName, 1);
262 char *extension, *filebuf;
264 extlen = strlen(extension);
265 LEPT_FREE(extension);
267 if (format == IFF_DEFAULT || format == IFF_UNKNOWN)
268 format = pixChooseOutputFormat(pix);
270 filebuf = (
char *)LEPT_CALLOC(strlen(fname) + 10,
sizeof(char));
272 return ERROR_INT(
"filebuf not made", procName, 1);
273 strncpy(filebuf, fname, strlen(fname));
274 strcat(filebuf,
".");
275 strcat(filebuf, ImageFileFormatExtensions[format]);
277 filebuf = (
char *)fname;
281 if (filebuf != fname)
284 return ERROR_INT(
"stream not opened", procName, 1);
289 ret = pixWriteStream(fp, pix, format);
292 return ERROR_INT(
"pix not written to stream", procName, 1);
305 pixWriteAutoFormat(
const char *filename,
310 PROCNAME(
"pixWriteAutoFormat");
313 return ERROR_INT(
"pix not defined", procName, 1);
315 return ERROR_INT(
"filename not defined", procName, 1);
317 if (pixGetAutoFormat(pix, &format))
318 return ERROR_INT(
"auto format not returned", procName, 1);
319 return pixWrite(filename, pix, format);
332 pixWriteStream(FILE *fp,
336 PROCNAME(
"pixWriteStream");
339 return ERROR_INT(
"stream not defined", procName, 1);
341 return ERROR_INT(
"pix not defined", procName, 1);
343 if (format == IFF_DEFAULT)
344 format = pixChooseOutputFormat(pix);
361 case IFF_TIFF_PACKBITS:
379 return pixWriteStreamGif(fp, pix);
383 return pixWriteStreamJp2k(fp, pix, 34, 4, 0, 0);
387 return pixWriteStreamWebP(fp, pix, 80, 0);
399 return ERROR_INT(
"unknown format", procName, 1);
424 pixWriteImpliedFormat(
const char *filename,
431 PROCNAME(
"pixWriteImpliedFormat");
434 return ERROR_INT(
"filename not defined", procName, 1);
436 return ERROR_INT(
"pix not defined", procName, 1);
439 format = getImpliedFileFormat(filename);
440 if (format == IFF_UNKNOWN) {
442 }
else if (format == IFF_TIFF) {
443 if (pixGetDepth(pix) == 1)
444 format = IFF_TIFF_G4;
447 format = IFF_TIFF_LZW;
449 format = IFF_TIFF_ZIP;
453 if (format == IFF_JFIF_JPEG) {
454 quality = L_MIN(quality, 100);
455 quality = L_MAX(quality, 0);
456 if (progressive != 0 && progressive != 1) {
458 L_WARNING(
"invalid progressive; setting to baseline\n", procName);
464 pixWrite(filename, pix, format);
489 pixChooseOutputFormat(
PIX *pix)
493 PROCNAME(
"pixChooseOutputFormat");
496 return ERROR_INT(
"pix not defined", procName, 0);
498 d = pixGetDepth(pix);
499 format = pixGetInputFormat(pix);
500 if (format == IFF_UNKNOWN) {
502 format = IFF_TIFF_G4;
524 getImpliedFileFormat(
const char *filename)
528 l_int32 format = IFF_UNKNOWN;
533 numext =
sizeof(extension_map) /
sizeof(extension_map[0]);
534 for (i = 0; i < numext; i++) {
535 if (!strcmp(extension, extension_map[i].extension)) {
536 format = extension_map[i].format;
541 LEPT_FREE(extension);
565 pixGetAutoFormat(
PIX *pix,
571 PROCNAME(
"pixGetAutoFormat");
574 return ERROR_INT(
"&format not defined", procName, 0);
575 *pformat = IFF_UNKNOWN;
577 return ERROR_INT(
"pix not defined", procName, 0);
579 d = pixGetDepth(pix);
580 cmap = pixGetColormap(pix);
581 if (d == 1 && !cmap) {
582 *pformat = IFF_TIFF_G4;
583 }
else if ((d == 8 && !cmap) || d == 24 || d == 32) {
584 *pformat = IFF_JFIF_JPEG;
606 getFormatExtension(l_int32 format)
608 PROCNAME(
"getFormatExtension");
610 if (format < 0 || format >= NumImageFileFormatExtensions)
611 return (
const char *)ERROR_PTR(
"invalid format", procName, NULL);
613 return ImageFileFormatExtensions[format];
639 pixWriteMem(l_uint8 **pdata,
646 PROCNAME(
"pixWriteMem");
649 return ERROR_INT(
"&data not defined", procName, 1 );
651 return ERROR_INT(
"&size not defined", procName, 1 );
653 return ERROR_INT(
"&pix not defined", procName, 1 );
655 if (format == IFF_DEFAULT)
656 format = pixChooseOutputFormat(pix);
673 case IFF_TIFF_PACKBITS:
687 ret =
pixWriteMemPS(pdata, psize, pix, NULL, 0, DEFAULT_SCALING);
691 ret = pixWriteMemGif(pdata, psize, pix);
695 ret = pixWriteMemJp2k(pdata, psize, pix, 34, 0, 0, 0);
699 ret = pixWriteMemWebP(pdata, psize, pix, 80, 0);
711 return ERROR_INT(
"unknown format", procName, 1);
738 l_fileDisplay(
const char *fname,
745 PROCNAME(
"l_fileDisplay");
751 return ERROR_INT(
"invalid scale factor", procName, 1);
752 if ((pixs =
pixRead(fname)) == NULL)
753 return ERROR_INT(
"pixs not read", procName, 1);
758 if (scale < 1.0 && pixGetDepth(pixs) == 1)
761 pixd =
pixScale(pixs, scale, scale);
763 pixDisplay(pixd, x, y);
809 pixDisplay(
PIX *pixs,
813 return pixDisplayWithTitle(pixs, x, y, NULL, 1);
833 pixDisplayWithTitle(
PIX *pixs,
841 static l_int32 index = 0;
842 l_int32 w, h, d, spp, maxheight, opaque, threeviews, ignore;
843 l_float32 ratw, rath, ratmin;
844 PIX *pix0, *pix1, *pix2;
850 char fullpath[_MAX_PATH];
853 PROCNAME(
"pixDisplayWithTitle");
855 if (dispflag != 1)
return 0;
857 return ERROR_INT(
"pixs not defined", procName, 1);
863 return ERROR_INT(
"no program chosen for display", procName, 1);
869 if ((cmap = pixGetColormap(pixs)) != NULL)
871 spp = pixGetSpp(pixs);
872 threeviews = (spp == 4 || !opaque) ? TRUE : FALSE;
882 maxheight = (threeviews) ? MAX_DISPLAY_HEIGHT / 3 : MAX_DISPLAY_HEIGHT;
883 if (w <= MAX_DISPLAY_WIDTH && h <= maxheight) {
889 ratw = (l_float32)MAX_DISPLAY_WIDTH / (l_float32)w;
890 rath = (l_float32)maxheight / (l_float32)h;
891 ratmin = L_MIN(ratw, rath);
892 if (ratmin < 0.125 && d == 1)
894 else if (ratmin < 0.25 && d == 1)
896 else if (ratmin < 0.33 && d == 1)
898 else if (ratmin < 0.5 && d == 1)
901 pix1 =
pixScale(pix0, ratmin, ratmin);
905 return ERROR_INT(
"pix1 not made", procName, 1);
919 if (pixGetDepth(pix2) < 8 || pixGetColormap(pix2) ||
920 (w < MAX_SIZE_FOR_PNG && h < MAX_SIZE_FOR_PNG)) {
921 snprintf(buffer,
L_BUF_SIZE,
"/tmp/lept/disp/write.%03d.png", index);
922 pixWrite(buffer, pix2, IFF_PNG);
924 snprintf(buffer,
L_BUF_SIZE,
"/tmp/lept/disp/write.%03d.jpg", index);
925 pixWrite(buffer, pix2, IFF_JFIF_JPEG);
936 "xzgv --geometry %dx%d+%d+%d %s &", wt + 10, ht + 10,
941 "xli -dispgamma 1.0 -quiet -geometry +%d+%d -title \"%s\" %s &",
942 x, y, title, tempname);
945 "xli -dispgamma 1.0 -quiet -geometry +%d+%d %s &",
951 "xv -quit -geometry +%d+%d -name \"%s\" %s &",
952 x, y, title, tempname);
955 "xv -quit -geometry +%d+%d %s &", x, y, tempname);
958 snprintf(buffer,
L_BUF_SIZE,
"open %s &", tempname);
961 ignore = system(buffer);
968 _fullpath(fullpath, pathname,
sizeof(fullpath));
971 "i_view32.exe \"%s\" /pos=(%d,%d) /title=\"%s\"",
972 fullpath, x, y, title);
974 snprintf(buffer,
L_BUF_SIZE,
"i_view32.exe \"%s\" /pos=(%d,%d)",
977 ignore = system(buffer);
1001 pixSaveTiled(
PIX *pixs,
1003 l_float32 scalefactor,
1009 return pixSaveTiledOutline(pixs, pixa, scalefactor, newrow, space, 0, dp);
1052 pixSaveTiledOutline(
PIX *pixs,
1054 l_float32 scalefactor,
1060 l_int32 n, top, left, bx, by, bw, w, h, depth, bottom;
1062 PIX *pix1, *pix2, *pix3, *pix4;
1064 PROCNAME(
"pixSaveTiledOutline");
1066 if (scalefactor == 0.0)
return 0;
1069 return ERROR_INT(
"pixs not defined", procName, 1);
1071 return ERROR_INT(
"pixa not defined", procName, 1);
1076 if (dp != 8 && dp != 32) {
1077 L_WARNING(
"dp not 8 or 32 bpp; using 32\n", procName);
1084 depth = pixGetDepth(pix1);
1085 bottom = pixGetInputFormat(pix1);
1094 if (scalefactor == 1.0) {
1096 }
else if (scalefactor > 1.0) {
1097 pix2 =
pixScale(pix1, scalefactor, scalefactor);
1099 if (pixGetDepth(pix1) == 1)
1102 pix2 =
pixScale(pix1, scalefactor, scalefactor);
1122 }
else if (newrow == 1) {
1123 top = bottom + space;
1128 left = bx + bw + space;
1132 bottom = L_MAX(bottom, top + h);
1139 pixSetInputFormat(pix1, bottom);
1182 pixSaveTiledWithText(
PIX *pixs,
1189 const char *textstr,
1193 PIX *pix1, *pix2, *pix3, *pix4;
1195 PROCNAME(
"pixSaveTiledWithText");
1197 if (outwidth == 0)
return 0;
1200 return ERROR_INT(
"pixs not defined", procName, 1);
1202 return ERROR_INT(
"pixa not defined", procName, 1);
1214 pixSaveTiled(pix4, pixa, 1.0, newrow, space, 32);
1224 l_chooseDisplayProg(l_int32 selection)
1231 var_DISPLAY_PROG = selection;
1233 L_ERROR(
"invalid display program\n",
"l_chooseDisplayProg");
1262 pixDisplayWrite(
PIX *pixs,
1265 return pixDisplayWriteFormat(pixs, reduction, IFF_DEFAULT);
1306 pixDisplayWriteFormat(
PIX *pixs,
1314 static l_int32 index = 0;
1316 PROCNAME(
"pixDisplayWriteFormat");
1318 if (reduction == 0)
return 0;
1320 if (reduction < 0) {
1327 return ERROR_INT(
"pixs not defined", procName, 1);
1328 if (format != IFF_DEFAULT && format != IFF_PNG) {
1329 L_INFO(
"invalid format; using default\n", procName);
1330 format = IFF_DEFAULT;
1337 if (reduction == 1) {
1340 scale = 1. / (l_float32)reduction;
1341 if (pixGetDepth(pixs) == 1)
1344 pix1 =
pixScale(pixs, scale, scale);
1347 if (pixGetDepth(pix1) == 16) {
1349 snprintf(buf,
L_BUF_SIZE,
"file.%03d.png", index);
1350 fname =
pathJoin(
"/tmp/lept/display", buf);
1351 pixWrite(fname, pix2, IFF_PNG);
1353 }
else if (pixGetDepth(pix1) < 8 || pixGetColormap(pix1) ||
1354 format == IFF_PNG) {
1355 snprintf(buf,
L_BUF_SIZE,
"file.%03d.png", index);
1356 fname =
pathJoin(
"/tmp/lept/display", buf);
1357 pixWrite(fname, pix1, IFF_PNG);
1359 snprintf(buf,
L_BUF_SIZE,
"file.%03d.jpg", index);
1360 fname =
pathJoin(
"/tmp/lept/display", buf);
1361 pixWrite(fname, pix1, format);
1386 pixDisplayMultiple(l_int32 res,
1387 l_float32 scalefactor,
1388 const char *fileout)
1390 PROCNAME(
"pixDisplayMultiple");
1393 return ERROR_INT(
"invalid res", procName, 1);
1394 if (scalefactor <= 0.0)
1395 return ERROR_INT(
"invalid scalefactor", procName, 1);
1397 return ERROR_INT(
"fileout not defined", procName, 1);
l_int32 pixWriteStreamPS(FILE *fp, PIX *pix, BOX *box, l_int32 res, l_float32 scale)
pixWriteStreamPS()
l_int32 pixWriteStreamPng(FILE *fp, PIX *pix, l_float32 gamma)
pixWriteStreamPng()
PIX * pixRemoveColormap(PIX *pixs, l_int32 type)
pixRemoveColormap()
l_int32 lept_mkdir(const char *subdir)
lept_mkdir()
PIX * pixScaleToGray(PIX *pixs, l_float32 scalefactor)
pixScaleToGray()
PIX * pixConvertTo32(PIX *pixs)
pixConvertTo32()
char * genPathname(const char *dir, const char *fname)
genPathname()
l_int32 pixWriteMemPS(l_uint8 **pdata, size_t *psize, PIX *pix, BOX *box, l_int32 res, l_float32 scale)
pixWriteMemPS()
PIX * pixMaxDynamicRange(PIX *pixs, l_int32 type)
pixMaxDynamicRange()
l_int32 pixWriteMemJpeg(l_uint8 **pdata, size_t *psize, PIX *pix, l_int32 quality, l_int32 progressive)
pixWriteMemJpeg()
l_int32 pixWriteMemSpix(l_uint8 **pdata, size_t *psize, PIX *pix)
pixWriteMemSpix()
PIX * pixConvertTo8(PIX *pixs, l_int32 cmapflag)
pixConvertTo8()
l_int32 pixWriteMemPng(l_uint8 **pfiledata, size_t *pfilesize, PIX *pix, l_float32 gamma)
pixWriteMemPng()
PIX * pixRemoveColormapGeneral(PIX *pixs, l_int32 type, l_int32 ifnocmap)
pixRemoveColormapGeneral()
l_int32 convertFilesToPdf(const char *dirname, const char *substr, l_int32 res, l_float32 scalefactor, l_int32 type, l_int32 quality, const char *title, const char *fileout)
convertFilesToPdf()
PIX * pixScaleToGray3(PIX *pixs)
pixScaleToGray3()
PIX * pixAddBorder(PIX *pixs, l_int32 npix, l_uint32 val)
pixAddBorder()
PIX * pixScaleToGray8(PIX *pixs)
pixScaleToGray8()
l_int32 splitPathAtExtension(const char *pathname, char **pbasename, char **pextension)
splitPathAtExtension()
l_int32 pixcmapIsOpaque(PIXCMAP *cmap, l_int32 *popaque)
pixcmapIsOpaque()
static const l_int32 L_INSERT
l_int32 pixWriteMemTiff(l_uint8 **pdata, size_t *psize, PIX *pix, l_int32 comptype)
pixWriteMemTiff()
l_int32 pixaAddPix(PIXA *pixa, PIX *pix, l_int32 copyflag)
pixaAddPix()
l_int32 pixWriteStreamJpeg(FILE *fp, PIX *pixs, l_int32 quality, l_int32 progressive)
pixWriteStreamJpeg()
l_int32 pixWriteStreamPdf(FILE *fp, PIX *pix, l_int32 res, const char *title)
pixWriteStreamPdf()
l_int32 pixWriteJpeg(const char *filename, PIX *pix, l_int32 quality, l_int32 progressive)
pixWriteJpeg()
l_int32 pixWriteStreamBmp(FILE *fp, PIX *pix)
pixWriteStreamBmp()
l_int32 pixWriteStreamSpix(FILE *fp, PIX *pix)
pixWriteStreamSpix()
l_int32 pixWriteStreamPnm(FILE *fp, PIX *pix)
pixWriteStreamPnm()
PIX * pixClone(PIX *pixs)
pixClone()
PIX * pixScaleToSize(PIX *pixs, l_int32 wd, l_int32 hd)
pixScaleToSize()
l_int32 pixWriteMemBmp(l_uint8 **pfdata, size_t *pfsize, PIX *pixs)
pixWriteMemBmp()
void pixDestroy(PIX **ppix)
pixDestroy()
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
PIX * pixRead(const char *filename)
pixRead()
l_int32 pixWriteMemPnm(l_uint8 **pdata, size_t *psize, PIX *pix)
pixWriteMemPnm()
char * pathJoin(const char *dir, const char *fname)
pathJoin()
PIX * pixaGetPix(PIXA *pixa, l_int32 index, l_int32 accesstype)
pixaGetPix()
PIX * pixScaleToGray4(PIX *pixs)
pixScaleToGray4()
PIX * pixDisplayLayersRGBA(PIX *pixs, l_uint32 val, l_int32 maxw)
pixDisplayLayersRGBA()
l_int32 pixaGetBoxGeometry(PIXA *pixa, l_int32 index, l_int32 *px, l_int32 *py, l_int32 *pw, l_int32 *ph)
pixaGetBoxGeometry()
l_int32 pixWriteMemPdf(l_uint8 **pdata, size_t *pnbytes, PIX *pix, l_int32 res, const char *title)
pixWriteMemPdf()
l_int32 pixaAddBox(PIXA *pixa, BOX *box, l_int32 copyflag)
pixaAddBox()
l_int32 pixWriteStreamTiff(FILE *fp, PIX *pix, l_int32 comptype)
pixWriteStreamTiff()
PIX * pixConvert16To8(PIX *pixs, l_int32 type)
pixConvert16To8()
l_int32 pixGetDimensions(PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
l_int32 lept_rmdir(const char *subdir)
lept_rmdir()
PIX * pixScale(PIX *pixs, l_float32 scalex, l_float32 scaley)
pixScale()
PIX * pixAddSingleTextblock(PIX *pixs, L_BMF *bmf, const char *textstr, l_uint32 val, l_int32 location, l_int32 *poverflow)
pixAddSingleTextblock()
BOX * boxCreate(l_int32 x, l_int32 y, l_int32 w, l_int32 h)
boxCreate()
l_int32 pixaGetCount(PIXA *pixa)
pixaGetCount()
PIX * pixScaleToGray2(PIX *pixs)
pixScaleToGray2()
static const l_int32 L_BUF_SIZE