90#include <config_auto.h>
94#include "allheaders.h"
100#elif defined(__APPLE__)
107static const l_int32 MaxDisplayWidth = 1000;
108static const l_int32 MaxDisplayHeight = 800;
109static const l_int32 MaxSizeForPng = 200;
112static const l_float32 DefaultScaling = 1.0;
123LEPT_DLL l_int32 NumImageFileFormatExtensions = 20;
124LEPT_DLL
const char *ImageFileFormatExtensions[] =
153 { {
".bmp", IFF_BMP },
154 {
".jpg", IFF_JFIF_JPEG },
155 {
".jpeg", IFF_JFIF_JPEG },
156 {
".JPG", IFF_JFIF_JPEG },
158 {
".tif", IFF_TIFF },
159 {
".tiff", IFF_TIFF },
160 {
".tiffg4", IFF_TIFF_G4 },
165 {
".pdf", IFF_LPDF },
166 {
".webp", IFF_WEBP } };
173static l_int32 var_JPEG_QUALITY = 75;
192l_jpegSetQuality(l_int32 new_quality)
196 prevq = var_JPEG_QUALITY;
197 newq = (new_quality == 0) ? 75 : new_quality;
198 if (newq < 1 || newq > 100)
199 L_ERROR(
"invalid jpeg quality; unchanged\n", __func__);
201 var_JPEG_QUALITY = newq;
209LEPT_DLL l_int32 LeptDebugOK = 0;
225setLeptDebugOK(l_int32 allow)
227 if (allow != 0) allow = 1;
250pixaWriteFiles(
const char *rootname,
255l_int32 i, n, pixformat;
259 return ERROR_INT(
"rootname not defined", __func__, 1);
261 return ERROR_INT(
"pixa not defined", __func__, 1);
262 if (format < 0 || format == IFF_UNKNOWN ||
263 format >= NumImageFileFormatExtensions)
264 return ERROR_INT(
"invalid format", __func__, 1);
266 n = pixaGetCount(pixa);
267 for (i = 0; i < n; i++) {
268 pix = pixaGetPix(pixa, i,
L_CLONE);
269 if (format == IFF_DEFAULT)
270 pixformat = pixChooseOutputFormat(pix);
273 snprintf(bigbuf, Bufsize,
"%s%03d.%s", rootname, i,
274 ImageFileFormatExtensions[pixformat]);
275 pixWrite(bigbuf, pix, pixformat);
301pixWriteDebug(
const char *fname,
306 return pixWrite(fname, pix, format);
308 L_INFO(
"write to named temp file %s is disabled\n", __func__, fname);
336pixWrite(
const char *fname,
344 return ERROR_INT(
"pix not defined", __func__, 1);
346 return ERROR_INT(
"fname not defined", __func__, 1);
348 if ((fp = fopenWriteStream(fname,
"wb+")) == NULL)
349 return ERROR_INT_1(
"stream not opened", fname, __func__, 1);
351 ret = pixWriteStream(fp, pix, format);
354 return ERROR_INT_1(
"pix not written to stream", fname, __func__, 1);
367pixWriteAutoFormat(
const char *filename,
373 return ERROR_INT(
"pix not defined", __func__, 1);
375 return ERROR_INT(
"filename not defined", __func__, 1);
377 if (pixGetAutoFormat(pix, &format))
378 return ERROR_INT(
"auto format not returned", __func__, 1);
379 return pixWrite(filename, pix, format);
392pixWriteStream(FILE *fp,
397 return ERROR_INT(
"stream not defined", __func__, 1);
399 return ERROR_INT(
"pix not defined", __func__, 1);
401 if (format == IFF_DEFAULT)
402 format = pixChooseOutputFormat(pix);
406 changeFormatForMissingLib(&format);
411 pixWriteStreamBmp(fp, pix);
415 return pixWriteStreamJpeg(fp, pix, var_JPEG_QUALITY, 0);
418 return pixWriteStreamPng(fp, pix, 0.0);
421 case IFF_TIFF_PACKBITS:
428 return pixWriteStreamTiff(fp, pix, format);
431 return pixWriteStreamPnm(fp, pix);
434 return pixWriteStreamPS(fp, pix, NULL, 0, DefaultScaling);
437 return pixWriteStreamGif(fp, pix);
440 return pixWriteStreamJp2k(fp, pix, 34, 4,
L_JP2_CODEC, 0, 0);
443 return pixWriteStreamWebP(fp, pix, 80, 0);
446 return pixWriteStreamPdf(fp, pix, 0, NULL);
449 return pixWriteStreamSpix(fp, pix);
452 return ERROR_INT(
"unknown format", __func__, 1);
476pixWriteImpliedFormat(
const char *filename,
484 return ERROR_INT(
"filename not defined", __func__, 1);
486 return ERROR_INT(
"pix not defined", __func__, 1);
489 format = getImpliedFileFormat(filename);
490 if (format == IFF_UNKNOWN) {
492 }
else if (format == IFF_TIFF) {
493 if (pixGetDepth(pix) == 1)
494 format = IFF_TIFF_G4;
497 format = IFF_TIFF_LZW;
499 format = IFF_TIFF_ZIP;
503 if (format == IFF_JFIF_JPEG) {
504 quality = L_MIN(quality, 100);
505 quality = L_MAX(quality, 0);
506 if (progressive != 0 && progressive != 1) {
508 L_WARNING(
"invalid progressive; setting to baseline\n", __func__);
512 pixWriteJpeg (filename, pix, quality, progressive);
514 pixWrite(filename, pix, format);
539pixChooseOutputFormat(
PIX *pix)
544 return ERROR_INT(
"pix not defined", __func__, 0);
546 d = pixGetDepth(pix);
547 format = pixGetInputFormat(pix);
548 if (format == IFF_UNKNOWN) {
550 format = IFF_TIFF_G4;
572getImpliedFileFormat(
const char *filename)
575l_int32 format = IFF_UNKNOWN;
578 return ERROR_INT(
"extension not defined", __func__, IFF_UNKNOWN);
580 if (splitPathAtExtension (filename, NULL, &extension))
583 format = getFormatFromExtension(extension);
584 LEPT_FREE(extension);
604getFormatFromExtension(
const char *extension)
607l_int32 format = IFF_UNKNOWN;
610 return ERROR_INT(
"extension not defined", __func__, IFF_UNKNOWN);
612 numext =
sizeof(extension_map) /
sizeof(extension_map[0]);
613 for (i = 0; i < numext; i++) {
614 if (!strcmp(extension, extension_map[i].extension)) {
615 format = extension_map[i].format;
642pixGetAutoFormat(
PIX *pix,
649 return ERROR_INT(
"&format not defined", __func__, 0);
650 *pformat = IFF_UNKNOWN;
652 return ERROR_INT(
"pix not defined", __func__, 0);
654 d = pixGetDepth(pix);
655 cmap = pixGetColormap(pix);
656 if (d == 1 && !cmap) {
657 *pformat = IFF_TIFF_G4;
658 }
else if ((d == 8 && !cmap) || d == 24 || d == 32) {
659 *pformat = IFF_JFIF_JPEG;
681getFormatExtension(l_int32 format)
683 if (format < 0 || format >= NumImageFileFormatExtensions)
684 return (
const char *)ERROR_PTR(
"invalid format", __func__, NULL);
686 return ImageFileFormatExtensions[format];
714pixWriteMem(l_uint8 **pdata,
722 return ERROR_INT(
"&data not defined", __func__, 1 );
724 return ERROR_INT(
"&size not defined", __func__, 1 );
726 return ERROR_INT(
"&pix not defined", __func__, 1 );
728 if (format == IFF_DEFAULT)
729 format = pixChooseOutputFormat(pix);
733 changeFormatForMissingLib(&format);
738 ret = pixWriteMemBmp(pdata, psize, pix);
742 ret = pixWriteMemJpeg(pdata, psize, pix, var_JPEG_QUALITY, 0);
746 ret = pixWriteMemPng(pdata, psize, pix, 0.0);
750 case IFF_TIFF_PACKBITS:
757 ret = pixWriteMemTiff(pdata, psize, pix, format);
761 ret = pixWriteMemPnm(pdata, psize, pix);
765 ret = pixWriteMemPS(pdata, psize, pix, NULL, 0, DefaultScaling);
769 ret = pixWriteMemGif(pdata, psize, pix);
773 ret = pixWriteMemJp2k(pdata, psize, pix, 34, 0, 0, 0);
777 ret = pixWriteMemWebP(pdata, psize, pix, 80, 0);
781 ret = pixWriteMemPdf(pdata, psize, pix, 0, NULL);
785 ret = pixWriteMemSpix(pdata, psize, pix);
789 return ERROR_INT(
"unknown format", __func__, 1);
816l_fileDisplay(
const char *fname,
824 L_INFO(
"displaying files is disabled; "
825 "use setLeptDebugOK(1) to enable\n", __func__);
831 return ERROR_INT(
"invalid scale factor", __func__, 1);
832 if ((pixs = pixRead(fname)) == NULL)
833 return ERROR_INT(
"pixs not read", __func__, 1);
836 pixd = pixClone(pixs);
838 if (scale < 1.0 && pixGetDepth(pixs) == 1)
839 pixd = pixScaleToGray(pixs, scale);
841 pixd = pixScale(pixs, scale, scale);
843 pixDisplay(pixd, x, y);
898 return pixDisplayWithTitle(pixs, x, y, NULL, 1);
919pixDisplayWithTitle(
PIX *pixs,
927static l_atomic index = 0;
928l_int32 w, h, d, spp, maxheight, opaque, threeviews;
929l_float32 ratw, rath, ratmin;
930PIX *pix0, *pix1, *pix2;
936char fullpath[_MAX_PATH];
940 L_INFO(
"displaying images is disabled;\n "
941 "use setLeptDebugOK(1) to enable\n", __func__);
946 return ERROR_INT(
"iOS 11 does not support system()", __func__, 1);
952 return ERROR_INT(
"pixs not defined", __func__, 1);
959 return ERROR_INT(
"invalid unix program chosen for display",
964 return ERROR_INT(
"invalid windows program chosen for display",
971 if ((cmap = pixGetColormap(pixs)) != NULL)
972 pixcmapIsOpaque(cmap, &opaque);
973 spp = pixGetSpp(pixs);
974 threeviews = (spp == 4 || !opaque) ? TRUE : FALSE;
980 pix0 = pixClone(pixs);
983 pixGetDimensions(pix0, &w, &h, &d);
984 maxheight = (threeviews) ? MaxDisplayHeight / 3 : MaxDisplayHeight;
985 if (w <= MaxDisplayWidth && h <= maxheight) {
989 pix1 = pixClone(pix0);
991 ratw = (l_float32)MaxDisplayWidth / (l_float32)w;
992 rath = (l_float32)maxheight / (l_float32)h;
993 ratmin = L_MIN(ratw, rath);
994 if (ratmin < 0.125 && d == 1)
995 pix1 = pixScaleToGray8(pix0);
996 else if (ratmin < 0.25 && d == 1)
997 pix1 = pixScaleToGray4(pix0);
998 else if (ratmin < 0.33 && d == 1)
999 pix1 = pixScaleToGray3(pix0);
1000 else if (ratmin < 0.5 && d == 1)
1001 pix1 = pixScaleToGray2(pix0);
1003 pix1 = pixScale(pix0, ratmin, ratmin);
1007 return ERROR_INT(
"pix1 not made", __func__, 1);
1011 pix2 = pixDisplayLayersRGBA(pix1, 0xffffff00, 0);
1013 pix2 = pixClone(pix1);
1016 lept_rmdir(
"lept/disp");
1017 lept_mkdir(
"lept/disp");
1021 if (pixGetDepth(pix2) < 8 || pixGetColormap(pix2) ||
1022 (w < MaxSizeForPng && h < MaxSizeForPng)) {
1023 snprintf(buffer, Bufsize,
"/tmp/lept/disp/write.%03d.png", index);
1024 pixWrite(buffer, pix2, IFF_PNG);
1026 snprintf(buffer, Bufsize,
"/tmp/lept/disp/write.%03d.jpg", index);
1027 pixWrite(buffer, pix2, IFF_JFIF_JPEG);
1029 tempname = genPathname(buffer, NULL);
1036 pixGetDimensions(pix2, &wt, &ht, NULL);
1037 snprintf(buffer, Bufsize,
1038 "xzgv --geometry %dx%d+%d+%d %s &", wt + 10, ht + 10,
1042 snprintf(buffer, Bufsize,
1043 "xli -dispgamma 1.0 -quiet -geometry +%d+%d -title \"%s\" %s &",
1044 x, y, title, tempname);
1046 snprintf(buffer, Bufsize,
1047 "xli -dispgamma 1.0 -quiet -geometry +%d+%d %s &",
1052 snprintf(buffer, Bufsize,
1053 "xv -quit -geometry +%d+%d -name \"%s\" %s &",
1054 x, y, title, tempname);
1056 snprintf(buffer, Bufsize,
1057 "xv -quit -geometry +%d+%d %s &", x, y, tempname);
1060 snprintf(buffer, Bufsize,
"open %s &", tempname);
1062 callSystemDebug(buffer);
1067 pathname = genPathname(tempname, NULL);
1068 _fullpath(fullpath, pathname,
sizeof(fullpath));
1071 snprintf(buffer, Bufsize,
1072 "i_view32.exe \"%s\" /pos=(%d,%d) /title=\"%s\"",
1073 fullpath, x, y, title);
1075 snprintf(buffer, Bufsize,
"i_view32.exe \"%s\" /pos=(%d,%d)",
1079 snprintf(buffer, Bufsize,
"explorer.exe /open,\"%s\"", fullpath);
1081 callSystemDebug(buffer);
1082 LEPT_FREE(pathname);
1088 LEPT_FREE(tempname);
1110pixMakeColorSquare(l_uint32 color,
1117l_int32 w, rval, gval, bval;
1121 w = (size <= 0) ? 100 : size;
1122 if (addlabel && w < 100) {
1123 L_WARNING(
"size too small for label; omitting label\n", __func__);
1127 if ((pix1 = pixCreate(w, w, 32)) == NULL)
1128 return (
PIX *)ERROR_PTR(
"pix1 not madel", __func__, NULL);
1129 pixSetAllArbitrary(pix1, color);
1136 L_ERROR(
"invalid location: adding below\n", __func__);
1139 bmf = bmfCreate(NULL, 4);
1140 extractRGBValues(color, &rval, &gval, &bval);
1141 snprintf(buf,
sizeof(buf),
"%d,%d,%d", rval, gval, bval);
1142 pix2 = pixAddSingleTextblock(pix1, bmf, buf, textcolor, location, NULL);
1150l_chooseDisplayProg(l_int32 selection)
1157 var_DISPLAY_PROG = selection;
1159 L_ERROR(
"invalid display program\n",
"l_chooseDisplayProg");
1181changeFormatForMissingLib(l_int32 *pformat)
1183#if !defined(HAVE_LIBJPEG)
1184 if (*pformat == IFF_JFIF_JPEG) {
1185 L_WARNING(
"jpeg library missing; output bmp format\n", __func__);
1189#if !defined(HAVE_LIBPNG)
1190 if (*pformat == IFF_PNG) {
1191 L_WARNING(
"png library missing; output bmp format\n", __func__);
1195#if !defined(HAVE_LIBTIFF)
1196 if (L_FORMAT_IS_TIFF(*pformat)) {
1197 L_WARNING(
"tiff library missing; output bmp format\n", __func__);
1220pixDisplayWrite(
PIX *pixs,
1223 lept_stderr(
"\n########################################################\n"
1224 " pixDisplayWrite() was last used in tesseract 3.04,"
1225 " in Feb 2016. As of 1.80, it is a non-functional stub\n"
1226 "########################################################"