89#include <config_auto.h>
93#include "allheaders.h"
98#elif defined(__APPLE__)
105static const l_int32 MaxDisplayWidth = 1000;
106static const l_int32 MaxDisplayHeight = 800;
107static const l_int32 MaxSizeForPng = 200;
110static const l_float32 DefaultScaling = 1.0;
121LEPT_DLL l_int32 NumImageFileFormatExtensions = 20;
122LEPT_DLL
const char *ImageFileFormatExtensions[] =
151 { {
".bmp", IFF_BMP },
152 {
".jpg", IFF_JFIF_JPEG },
153 {
".jpeg", IFF_JFIF_JPEG },
155 {
".tif", IFF_TIFF },
156 {
".tiff", IFF_TIFF },
161 {
".pdf", IFF_LPDF },
162 {
".webp", IFF_WEBP } };
169static l_int32 var_JPEG_QUALITY = 75;
188l_jpegSetQuality(l_int32 new_quality)
192 prevq = var_JPEG_QUALITY;
193 newq = (new_quality == 0) ? 75 : new_quality;
194 if (newq < 1 || newq > 100)
195 L_ERROR(
"invalid jpeg quality; unchanged\n", __func__);
197 var_JPEG_QUALITY = newq;
205LEPT_DLL l_int32 LeptDebugOK = 0;
221setLeptDebugOK(l_int32 allow)
223 if (allow != 0) allow = 1;
246pixaWriteFiles(
const char *rootname,
251l_int32 i, n, pixformat;
255 return ERROR_INT(
"rootname not defined", __func__, 1);
257 return ERROR_INT(
"pixa not defined", __func__, 1);
258 if (format < 0 || format == IFF_UNKNOWN ||
259 format >= NumImageFileFormatExtensions)
260 return ERROR_INT(
"invalid format", __func__, 1);
262 n = pixaGetCount(pixa);
263 for (i = 0; i < n; i++) {
264 pix = pixaGetPix(pixa, i,
L_CLONE);
265 if (format == IFF_DEFAULT)
266 pixformat = pixChooseOutputFormat(pix);
269 snprintf(bigbuf, Bufsize,
"%s%03d.%s", rootname, i,
270 ImageFileFormatExtensions[pixformat]);
271 pixWrite(bigbuf, pix, pixformat);
297pixWriteDebug(
const char *fname,
302 return pixWrite(fname, pix, format);
304 L_INFO(
"write to named temp file %s is disabled\n", __func__, fname);
332pixWrite(
const char *fname,
340 return ERROR_INT(
"pix not defined", __func__, 1);
342 return ERROR_INT(
"fname not defined", __func__, 1);
344 if ((fp = fopenWriteStream(fname,
"wb+")) == NULL)
345 return ERROR_INT(
"stream not opened", __func__, 1);
347 ret = pixWriteStream(fp, pix, format);
350 return ERROR_INT(
"pix not written to stream", __func__, 1);
363pixWriteAutoFormat(
const char *filename,
369 return ERROR_INT(
"pix not defined", __func__, 1);
371 return ERROR_INT(
"filename not defined", __func__, 1);
373 if (pixGetAutoFormat(pix, &format))
374 return ERROR_INT(
"auto format not returned", __func__, 1);
375 return pixWrite(filename, pix, format);
388pixWriteStream(FILE *fp,
393 return ERROR_INT(
"stream not defined", __func__, 1);
395 return ERROR_INT(
"pix not defined", __func__, 1);
397 if (format == IFF_DEFAULT)
398 format = pixChooseOutputFormat(pix);
402 changeFormatForMissingLib(&format);
407 pixWriteStreamBmp(fp, pix);
411 return pixWriteStreamJpeg(fp, pix, var_JPEG_QUALITY, 0);
414 return pixWriteStreamPng(fp, pix, 0.0);
417 case IFF_TIFF_PACKBITS:
424 return pixWriteStreamTiff(fp, pix, format);
427 return pixWriteStreamPnm(fp, pix);
430 return pixWriteStreamPS(fp, pix, NULL, 0, DefaultScaling);
433 return pixWriteStreamGif(fp, pix);
436 return pixWriteStreamJp2k(fp, pix, 34, 4,
L_JP2_CODEC, 0, 0);
439 return pixWriteStreamWebP(fp, pix, 80, 0);
442 return pixWriteStreamPdf(fp, pix, 0, NULL);
445 return pixWriteStreamSpix(fp, pix);
448 return ERROR_INT(
"unknown format", __func__, 1);
472pixWriteImpliedFormat(
const char *filename,
480 return ERROR_INT(
"filename not defined", __func__, 1);
482 return ERROR_INT(
"pix not defined", __func__, 1);
485 format = getImpliedFileFormat(filename);
486 if (format == IFF_UNKNOWN) {
488 }
else if (format == IFF_TIFF) {
489 if (pixGetDepth(pix) == 1)
490 format = IFF_TIFF_G4;
493 format = IFF_TIFF_LZW;
495 format = IFF_TIFF_ZIP;
499 if (format == IFF_JFIF_JPEG) {
500 quality = L_MIN(quality, 100);
501 quality = L_MAX(quality, 0);
502 if (progressive != 0 && progressive != 1) {
504 L_WARNING(
"invalid progressive; setting to baseline\n", __func__);
508 pixWriteJpeg (filename, pix, quality, progressive);
510 pixWrite(filename, pix, format);
535pixChooseOutputFormat(
PIX *pix)
540 return ERROR_INT(
"pix not defined", __func__, 0);
542 d = pixGetDepth(pix);
543 format = pixGetInputFormat(pix);
544 if (format == IFF_UNKNOWN) {
546 format = IFF_TIFF_G4;
568getImpliedFileFormat(
const char *filename)
572l_int32 format = IFF_UNKNOWN;
574 if (splitPathAtExtension (filename, NULL, &extension))
577 numext =
sizeof(extension_map) /
sizeof(extension_map[0]);
578 for (i = 0; i < numext; i++) {
579 if (!strcmp(extension, extension_map[i].extension)) {
580 format = extension_map[i].format;
585 LEPT_FREE(extension);
609pixGetAutoFormat(
PIX *pix,
616 return ERROR_INT(
"&format not defined", __func__, 0);
617 *pformat = IFF_UNKNOWN;
619 return ERROR_INT(
"pix not defined", __func__, 0);
621 d = pixGetDepth(pix);
622 cmap = pixGetColormap(pix);
623 if (d == 1 && !cmap) {
624 *pformat = IFF_TIFF_G4;
625 }
else if ((d == 8 && !cmap) || d == 24 || d == 32) {
626 *pformat = IFF_JFIF_JPEG;
648getFormatExtension(l_int32 format)
650 if (format < 0 || format >= NumImageFileFormatExtensions)
651 return (
const char *)ERROR_PTR(
"invalid format", __func__, NULL);
653 return ImageFileFormatExtensions[format];
681pixWriteMem(l_uint8 **pdata,
689 return ERROR_INT(
"&data not defined", __func__, 1 );
691 return ERROR_INT(
"&size not defined", __func__, 1 );
693 return ERROR_INT(
"&pix not defined", __func__, 1 );
695 if (format == IFF_DEFAULT)
696 format = pixChooseOutputFormat(pix);
700 changeFormatForMissingLib(&format);
705 ret = pixWriteMemBmp(pdata, psize, pix);
709 ret = pixWriteMemJpeg(pdata, psize, pix, var_JPEG_QUALITY, 0);
713 ret = pixWriteMemPng(pdata, psize, pix, 0.0);
717 case IFF_TIFF_PACKBITS:
724 ret = pixWriteMemTiff(pdata, psize, pix, format);
728 ret = pixWriteMemPnm(pdata, psize, pix);
732 ret = pixWriteMemPS(pdata, psize, pix, NULL, 0, DefaultScaling);
736 ret = pixWriteMemGif(pdata, psize, pix);
740 ret = pixWriteMemJp2k(pdata, psize, pix, 34, 0, 0, 0);
744 ret = pixWriteMemWebP(pdata, psize, pix, 80, 0);
748 ret = pixWriteMemPdf(pdata, psize, pix, 0, NULL);
752 ret = pixWriteMemSpix(pdata, psize, pix);
756 return ERROR_INT(
"unknown format", __func__, 1);
783l_fileDisplay(
const char *fname,
791 L_INFO(
"displaying files is disabled; "
792 "use setLeptDebugOK(1) to enable\n", __func__);
798 return ERROR_INT(
"invalid scale factor", __func__, 1);
799 if ((pixs = pixRead(fname)) == NULL)
800 return ERROR_INT(
"pixs not read", __func__, 1);
803 pixd = pixClone(pixs);
805 if (scale < 1.0 && pixGetDepth(pixs) == 1)
806 pixd = pixScaleToGray(pixs, scale);
808 pixd = pixScale(pixs, scale, scale);
810 pixDisplay(pixd, x, y);
861 return pixDisplayWithTitle(pixs, x, y, NULL, 1);
881pixDisplayWithTitle(
PIX *pixs,
889static l_int32 index = 0;
890l_int32 w, h, d, spp, maxheight, opaque, threeviews;
891l_float32 ratw, rath, ratmin;
892PIX *pix0, *pix1, *pix2;
898char fullpath[_MAX_PATH];
902 L_INFO(
"displaying images is disabled;\n "
903 "use setLeptDebugOK(1) to enable\n", __func__);
908 return ERROR_INT(
"iOS 11 does not support system()", __func__, 1);
911 if (dispflag != 1)
return 0;
913 return ERROR_INT(
"pixs not defined", __func__, 1);
919 return ERROR_INT(
"no program chosen for display", __func__, 1);
925 if ((cmap = pixGetColormap(pixs)) != NULL)
926 pixcmapIsOpaque(cmap, &opaque);
927 spp = pixGetSpp(pixs);
928 threeviews = (spp == 4 || !opaque) ? TRUE : FALSE;
934 pix0 = pixClone(pixs);
937 pixGetDimensions(pix0, &w, &h, &d);
938 maxheight = (threeviews) ? MaxDisplayHeight / 3 : MaxDisplayHeight;
939 if (w <= MaxDisplayWidth && h <= maxheight) {
943 pix1 = pixClone(pix0);
945 ratw = (l_float32)MaxDisplayWidth / (l_float32)w;
946 rath = (l_float32)maxheight / (l_float32)h;
947 ratmin = L_MIN(ratw, rath);
948 if (ratmin < 0.125 && d == 1)
949 pix1 = pixScaleToGray8(pix0);
950 else if (ratmin < 0.25 && d == 1)
951 pix1 = pixScaleToGray4(pix0);
952 else if (ratmin < 0.33 && d == 1)
953 pix1 = pixScaleToGray3(pix0);
954 else if (ratmin < 0.5 && d == 1)
955 pix1 = pixScaleToGray2(pix0);
957 pix1 = pixScale(pix0, ratmin, ratmin);
961 return ERROR_INT(
"pix1 not made", __func__, 1);
965 pix2 = pixDisplayLayersRGBA(pix1, 0xffffff00, 0);
967 pix2 = pixClone(pix1);
970 lept_rmdir(
"lept/disp");
971 lept_mkdir(
"lept/disp");
975 if (pixGetDepth(pix2) < 8 || pixGetColormap(pix2) ||
976 (w < MaxSizeForPng && h < MaxSizeForPng)) {
977 snprintf(buffer, Bufsize,
"/tmp/lept/disp/write.%03d.png", index);
978 pixWrite(buffer, pix2, IFF_PNG);
980 snprintf(buffer, Bufsize,
"/tmp/lept/disp/write.%03d.jpg", index);
981 pixWrite(buffer, pix2, IFF_JFIF_JPEG);
983 tempname = genPathname(buffer, NULL);
990 pixGetDimensions(pix2, &wt, &ht, NULL);
991 snprintf(buffer, Bufsize,
992 "xzgv --geometry %dx%d+%d+%d %s &", wt + 10, ht + 10,
996 snprintf(buffer, Bufsize,
997 "xli -dispgamma 1.0 -quiet -geometry +%d+%d -title \"%s\" %s &",
998 x, y, title, tempname);
1000 snprintf(buffer, Bufsize,
1001 "xli -dispgamma 1.0 -quiet -geometry +%d+%d %s &",
1006 snprintf(buffer, Bufsize,
1007 "xv -quit -geometry +%d+%d -name \"%s\" %s &",
1008 x, y, title, tempname);
1010 snprintf(buffer, Bufsize,
1011 "xv -quit -geometry +%d+%d %s &", x, y, tempname);
1014 snprintf(buffer, Bufsize,
"open %s &", tempname);
1016 callSystemDebug(buffer);
1021 pathname = genPathname(tempname, NULL);
1022 _fullpath(fullpath, pathname,
sizeof(fullpath));
1024 snprintf(buffer, Bufsize,
1025 "i_view32.exe \"%s\" /pos=(%d,%d) /title=\"%s\"",
1026 fullpath, x, y, title);
1028 snprintf(buffer, Bufsize,
"i_view32.exe \"%s\" /pos=(%d,%d)",
1031 callSystemDebug(buffer);
1032 LEPT_FREE(pathname);
1038 LEPT_FREE(tempname);
1060pixMakeColorSquare(l_uint32 color,
1067l_int32 w, rval, gval, bval;
1071 w = (size <= 0) ? 100 : size;
1072 if (addlabel && w < 100) {
1073 L_WARNING(
"size too small for label; omitting label\n", __func__);
1077 if ((pix1 = pixCreate(w, w, 32)) == NULL)
1078 return (
PIX *)ERROR_PTR(
"pix1 not madel", __func__, NULL);
1079 pixSetAllArbitrary(pix1, color);
1086 L_ERROR(
"invalid location: adding below\n", __func__);
1089 bmf = bmfCreate(NULL, 4);
1090 extractRGBValues(color, &rval, &gval, &bval);
1091 snprintf(buf,
sizeof(buf),
"%d,%d,%d", rval, gval, bval);
1092 pix2 = pixAddSingleTextblock(pix1, bmf, buf, textcolor, location, NULL);
1100l_chooseDisplayProg(l_int32 selection)
1107 var_DISPLAY_PROG = selection;
1109 L_ERROR(
"invalid display program\n",
"l_chooseDisplayProg");
1131changeFormatForMissingLib(l_int32 *pformat)
1133#if !defined(HAVE_LIBJPEG)
1134 if (*pformat == IFF_JFIF_JPEG) {
1135 L_WARNING(
"jpeg library missing; output bmp format\n", __func__);
1139#if !defined(HAVE_LIBPNG)
1140 if (*pformat == IFF_PNG) {
1141 L_WARNING(
"png library missing; output bmp format\n", __func__);
1145#if !defined(HAVE_LIBTIFF)
1146 if (L_FORMAT_IS_TIFF(*pformat)) {
1147 L_WARNING(
"tiff library missing; output bmp format\n", __func__);
1170pixDisplayWrite(
PIX *pixs,
1173 lept_stderr(
"\n########################################################\n"
1174 " pixDisplayWrite() was last used in tesseract 3.04,"
1175 " in Feb 2016. As of 1.80, it is a non-functional stub\n"
1176 "########################################################"
struct PixColormap PIXCMAP