89 #include <config_auto.h>
93 #include "allheaders.h"
98 #elif defined(__APPLE__)
105 static const l_int32 MaxDisplayWidth = 1000;
106 static const l_int32 MaxDisplayHeight = 800;
107 static const l_int32 MaxSizeForPng = 200;
110 static const l_float32 DefaultScaling = 1.0;
121 LEPT_DLL l_int32 NumImageFileFormatExtensions = 20;
122 LEPT_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 } };
169 static l_int32 var_JPEG_QUALITY = 75;
188 l_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;
205 LEPT_DLL l_int32 LeptDebugOK = 0;
221 setLeptDebugOK(l_int32 allow)
223 if (allow != 0) allow = 1;
246 pixaWriteFiles(
const char *rootname,
250 char bigbuf[Bufsize];
251 l_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);
263 for (i = 0; i < n; i++) {
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);
297 pixWriteDebug(
const char *fname,
302 return pixWrite(fname, pix, format);
304 L_INFO(
"write to named temp file %s is disabled\n", __func__, fname);
332 pixWrite(
const char *fname,
340 return ERROR_INT(
"pix not defined", __func__, 1);
342 return ERROR_INT(
"fname not defined", __func__, 1);
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);
363 pixWriteAutoFormat(
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);
388 pixWriteStream(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);
417 case IFF_TIFF_PACKBITS:
433 return pixWriteStreamGif(fp, pix);
436 return pixWriteStreamJp2k(fp, pix, 34, 4,
L_JP2_CODEC, 0, 0);
439 return pixWriteStreamWebP(fp, pix, 80, 0);
448 return ERROR_INT(
"unknown format", __func__, 1);
472 pixWriteImpliedFormat(
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__);
510 pixWrite(filename, pix, format);
535 pixChooseOutputFormat(
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;
568 getImpliedFileFormat(
const char *filename)
572 l_int32 format = IFF_UNKNOWN;
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);
609 pixGetAutoFormat(
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;
648 getFormatExtension(l_int32 format)
650 if (format < 0 || format >= NumImageFileFormatExtensions)
651 return (
const char *)ERROR_PTR(
"invalid format", __func__, NULL);
653 return ImageFileFormatExtensions[format];
681 pixWriteMem(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);
717 case IFF_TIFF_PACKBITS:
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);
756 return ERROR_INT(
"unknown format", __func__, 1);
783 l_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);
805 if (scale < 1.0 && pixGetDepth(pixs) == 1)
808 pixd =
pixScale(pixs, scale, scale);
810 pixDisplay(pixd, x, y);
857 pixDisplay(
PIX *pixs,
861 return pixDisplayWithTitle(pixs, x, y, NULL, 1);
881 pixDisplayWithTitle(
PIX *pixs,
888 char buffer[Bufsize];
889 static l_int32 index = 0;
890 l_int32 w, h, d, spp, maxheight, opaque, threeviews;
891 l_float32 ratw, rath, ratmin;
892 PIX *pix0, *pix1, *pix2;
898 char 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)
927 spp = pixGetSpp(pixs);
928 threeviews = (spp == 4 || !opaque) ? TRUE : FALSE;
938 maxheight = (threeviews) ? MaxDisplayHeight / 3 : MaxDisplayHeight;
939 if (w <= MaxDisplayWidth && h <= maxheight) {
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)
950 else if (ratmin < 0.25 && d == 1)
952 else if (ratmin < 0.33 && d == 1)
954 else if (ratmin < 0.5 && d == 1)
957 pix1 =
pixScale(pix0, ratmin, ratmin);
961 return ERROR_INT(
"pix1 not made", __func__, 1);
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);
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);
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)",
1032 LEPT_FREE(pathname);
1038 LEPT_FREE(tempname);
1060 pixMakeColorSquare(l_uint32 color,
1067 l_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);
1086 L_ERROR(
"invalid location: adding below\n", __func__);
1091 snprintf(buf,
sizeof(buf),
"%d,%d,%d", rval, gval, bval);
1100 l_chooseDisplayProg(l_int32 selection)
1107 var_DISPLAY_PROG = selection;
1109 L_ERROR(
"invalid display program\n",
"l_chooseDisplayProg");
1131 changeFormatForMissingLib(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__);
1170 pixDisplayWrite(
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 "########################################################"
void bmfDestroy(L_BMF **pbmf)
bmfDestroy()
L_BMF * bmfCreate(const char *dir, l_int32 fontsize)
bmfCreate()
l_ok pixWriteStreamBmp(FILE *fp, PIX *pix)
pixWriteStreamBmp()
l_ok pixWriteMemBmp(l_uint8 **pfdata, size_t *pfsize, PIX *pixs)
pixWriteMemBmp()
l_ok pixcmapIsOpaque(PIXCMAP *cmap, l_int32 *popaque)
pixcmapIsOpaque()
l_ok pixWriteMemJpeg(l_uint8 **pdata, size_t *psize, PIX *pix, l_int32 quality, l_int32 progressive)
pixWriteMemJpeg()
l_ok pixWriteStreamJpeg(FILE *fp, PIX *pixs, l_int32 quality, l_int32 progressive)
pixWriteStreamJpeg()
l_ok pixWriteJpeg(const char *filename, PIX *pix, l_int32 quality, l_int32 progressive)
pixWriteJpeg()
l_ok pixWriteMemPdf(l_uint8 **pdata, size_t *pnbytes, PIX *pix, l_int32 res, const char *title)
pixWriteMemPdf()
l_ok pixWriteStreamPdf(FILE *fp, PIX *pix, l_int32 res, const char *title)
pixWriteStreamPdf()
void pixDestroy(PIX **ppix)
pixDestroy()
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
PIX * pixCreate(l_int32 width, l_int32 height, l_int32 depth)
pixCreate()
PIX * pixClone(PIX *pixs)
pixClone()
PIX * pixDisplayLayersRGBA(PIX *pixs, l_uint32 val, l_int32 maxw)
pixDisplayLayersRGBA()
void extractRGBValues(l_uint32 pixel, l_int32 *prval, l_int32 *pgval, l_int32 *pbval)
extractRGBValues()
l_ok pixSetAllArbitrary(PIX *pix, l_uint32 val)
pixSetAllArbitrary()
l_int32 pixaGetCount(PIXA *pixa)
pixaGetCount()
PIX * pixaGetPix(PIXA *pixa, l_int32 index, l_int32 accesstype)
pixaGetPix()
PIX * pixRemoveColormap(PIX *pixs, l_int32 type)
pixRemoveColormap()
PIX * pixConvert16To8(PIX *pixs, l_int32 type)
pixConvert16To8()
l_ok pixWriteStreamPng(FILE *fp, PIX *pix, l_float32 gamma)
pixWriteStreamPng()
l_ok pixWriteMemPng(l_uint8 **pfiledata, size_t *pfilesize, PIX *pix, l_float32 gamma)
pixWriteMemPng()
l_ok pixWriteStreamPnm(FILE *fp, PIX *pix)
pixWriteStreamPnm()
l_ok pixWriteMemPnm(l_uint8 **pdata, size_t *psize, PIX *pix)
pixWriteMemPnm()
l_ok pixWriteStreamPS(FILE *fp, PIX *pix, BOX *box, l_int32 res, l_float32 scale)
pixWriteStreamPS()
l_ok pixWriteMemPS(l_uint8 **pdata, size_t *psize, PIX *pix, BOX *box, l_int32 res, l_float32 scale)
pixWriteMemPS()
PIX * pixRead(const char *filename)
pixRead()
PIX * pixScale(PIX *pixs, l_float32 scalex, l_float32 scaley)
pixScale()
PIX * pixScaleToGray8(PIX *pixs)
pixScaleToGray8()
PIX * pixScaleToGray3(PIX *pixs)
pixScaleToGray3()
PIX * pixScaleToGray2(PIX *pixs)
pixScaleToGray2()
PIX * pixScaleToGray(PIX *pixs, l_float32 scalefactor)
pixScaleToGray()
PIX * pixScaleToGray4(PIX *pixs)
pixScaleToGray4()
l_ok pixWriteMemSpix(l_uint8 **pdata, size_t *psize, PIX *pix)
pixWriteMemSpix()
l_ok pixWriteStreamSpix(FILE *fp, PIX *pix)
pixWriteStreamSpix()
PIX * pixAddSingleTextblock(PIX *pixs, L_BMF *bmf, const char *textstr, l_uint32 val, l_int32 location, l_int32 *poverflow)
pixAddSingleTextblock()
l_ok pixWriteStreamTiff(FILE *fp, PIX *pix, l_int32 comptype)
pixWriteStreamTiff()
l_ok pixWriteMemTiff(l_uint8 **pdata, size_t *psize, PIX *pix, l_int32 comptype)
pixWriteMemTiff()
void lept_stderr(const char *fmt,...)
lept_stderr()
l_int32 lept_rmdir(const char *subdir)
lept_rmdir()
l_ok splitPathAtExtension(const char *pathname, char **pbasename, char **pextension)
splitPathAtExtension()
void callSystemDebug(const char *cmd)
callSystemDebug()
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
char * genPathname(const char *dir, const char *fname)
genPathname()
l_int32 lept_mkdir(const char *subdir)
lept_mkdir()