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 PROCNAME(
"l_jpeqSetQuality");
194 prevq = var_JPEG_QUALITY;
195 newq = (new_quality == 0) ? 75 : new_quality;
196 if (newq < 1 || newq > 100)
197 L_ERROR(
"invalid jpeg quality; unchanged\n", procName);
199 var_JPEG_QUALITY = newq;
207 LEPT_DLL l_int32 LeptDebugOK = 0;
223 setLeptDebugOK(l_int32 allow)
225 if (allow != 0) allow = 1;
248 pixaWriteFiles(
const char *rootname,
252 char bigbuf[Bufsize];
253 l_int32 i, n, pixformat;
256 PROCNAME(
"pixaWriteFiles");
259 return ERROR_INT(
"rootname not defined", procName, 1);
261 return ERROR_INT(
"pixa not defined", procName, 1);
262 if (format < 0 || format == IFF_UNKNOWN ||
263 format >= NumImageFileFormatExtensions)
264 return ERROR_INT(
"invalid format", procName, 1);
267 for (i = 0; i < n; i++) {
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);
301 pixWriteDebug(
const char *fname,
305 PROCNAME(
"pixWriteDebug");
308 return pixWrite(fname, pix, format);
310 L_INFO(
"write to named temp file %s is disabled\n", procName, fname);
338 pixWrite(
const char *fname,
345 PROCNAME(
"pixWrite");
348 return ERROR_INT(
"pix not defined", procName, 1);
350 return ERROR_INT(
"fname not defined", procName, 1);
353 return ERROR_INT(
"stream not opened", procName, 1);
355 ret = pixWriteStream(fp, pix, format);
358 return ERROR_INT(
"pix not written to stream", procName, 1);
371 pixWriteAutoFormat(
const char *filename,
376 PROCNAME(
"pixWriteAutoFormat");
379 return ERROR_INT(
"pix not defined", procName, 1);
381 return ERROR_INT(
"filename not defined", procName, 1);
383 if (pixGetAutoFormat(pix, &format))
384 return ERROR_INT(
"auto format not returned", procName, 1);
385 return pixWrite(filename, pix, format);
398 pixWriteStream(FILE *fp,
402 PROCNAME(
"pixWriteStream");
405 return ERROR_INT(
"stream not defined", procName, 1);
407 return ERROR_INT(
"pix not defined", procName, 1);
409 if (format == IFF_DEFAULT)
410 format = pixChooseOutputFormat(pix);
414 changeFormatForMissingLib(&format);
429 case IFF_TIFF_PACKBITS:
445 return pixWriteStreamGif(fp, pix);
448 return pixWriteStreamJp2k(fp, pix, 34, 4,
L_JP2_CODEC, 0, 0);
451 return pixWriteStreamWebP(fp, pix, 80, 0);
460 return ERROR_INT(
"unknown format", procName, 1);
484 pixWriteImpliedFormat(
const char *filename,
491 PROCNAME(
"pixWriteImpliedFormat");
494 return ERROR_INT(
"filename not defined", procName, 1);
496 return ERROR_INT(
"pix not defined", procName, 1);
499 format = getImpliedFileFormat(filename);
500 if (format == IFF_UNKNOWN) {
502 }
else if (format == IFF_TIFF) {
503 if (pixGetDepth(pix) == 1)
504 format = IFF_TIFF_G4;
507 format = IFF_TIFF_LZW;
509 format = IFF_TIFF_ZIP;
513 if (format == IFF_JFIF_JPEG) {
514 quality = L_MIN(quality, 100);
515 quality = L_MAX(quality, 0);
516 if (progressive != 0 && progressive != 1) {
518 L_WARNING(
"invalid progressive; setting to baseline\n", procName);
524 pixWrite(filename, pix, format);
549 pixChooseOutputFormat(
PIX *pix)
553 PROCNAME(
"pixChooseOutputFormat");
556 return ERROR_INT(
"pix not defined", procName, 0);
558 d = pixGetDepth(pix);
559 format = pixGetInputFormat(pix);
560 if (format == IFF_UNKNOWN) {
562 format = IFF_TIFF_G4;
584 getImpliedFileFormat(
const char *filename)
588 l_int32 format = IFF_UNKNOWN;
593 numext =
sizeof(extension_map) /
sizeof(extension_map[0]);
594 for (i = 0; i < numext; i++) {
595 if (!strcmp(extension, extension_map[i].extension)) {
596 format = extension_map[i].format;
601 LEPT_FREE(extension);
625 pixGetAutoFormat(
PIX *pix,
631 PROCNAME(
"pixGetAutoFormat");
634 return ERROR_INT(
"&format not defined", procName, 0);
635 *pformat = IFF_UNKNOWN;
637 return ERROR_INT(
"pix not defined", procName, 0);
639 d = pixGetDepth(pix);
640 cmap = pixGetColormap(pix);
641 if (d == 1 && !cmap) {
642 *pformat = IFF_TIFF_G4;
643 }
else if ((d == 8 && !cmap) || d == 24 || d == 32) {
644 *pformat = IFF_JFIF_JPEG;
666 getFormatExtension(l_int32 format)
668 PROCNAME(
"getFormatExtension");
670 if (format < 0 || format >= NumImageFileFormatExtensions)
671 return (
const char *)ERROR_PTR(
"invalid format", procName, NULL);
673 return ImageFileFormatExtensions[format];
701 pixWriteMem(l_uint8 **pdata,
708 PROCNAME(
"pixWriteMem");
711 return ERROR_INT(
"&data not defined", procName, 1 );
713 return ERROR_INT(
"&size not defined", procName, 1 );
715 return ERROR_INT(
"&pix not defined", procName, 1 );
717 if (format == IFF_DEFAULT)
718 format = pixChooseOutputFormat(pix);
722 changeFormatForMissingLib(&format);
739 case IFF_TIFF_PACKBITS:
754 ret =
pixWriteMemPS(pdata, psize, pix, NULL, 0, DefaultScaling);
758 ret = pixWriteMemGif(pdata, psize, pix);
762 ret = pixWriteMemJp2k(pdata, psize, pix, 34, 0, 0, 0);
766 ret = pixWriteMemWebP(pdata, psize, pix, 80, 0);
778 return ERROR_INT(
"unknown format", procName, 1);
805 l_fileDisplay(
const char *fname,
812 PROCNAME(
"l_fileDisplay");
815 L_INFO(
"displaying files is disabled; " 816 "use setLeptDebugOK(1) to enable\n", procName);
822 return ERROR_INT(
"invalid scale factor", procName, 1);
823 if ((pixs =
pixRead(fname)) == NULL)
824 return ERROR_INT(
"pixs not read", procName, 1);
829 if (scale < 1.0 && pixGetDepth(pixs) == 1)
832 pixd =
pixScale(pixs, scale, scale);
834 pixDisplay(pixd, x, y);
881 pixDisplay(
PIX *pixs,
885 return pixDisplayWithTitle(pixs, x, y, NULL, 1);
905 pixDisplayWithTitle(
PIX *pixs,
912 char buffer[Bufsize];
913 static l_int32 index = 0;
914 l_int32 w, h, d, spp, maxheight, opaque, threeviews;
915 l_float32 ratw, rath, ratmin;
916 PIX *pix0, *pix1, *pix2;
922 char fullpath[_MAX_PATH];
925 PROCNAME(
"pixDisplayWithTitle");
928 L_INFO(
"displaying images is disabled;\n " 929 "use setLeptDebugOK(1) to enable\n", procName);
934 return ERROR_INT(
"iOS 11 does not support system()", procName, 1);
937 if (dispflag != 1)
return 0;
939 return ERROR_INT(
"pixs not defined", procName, 1);
945 return ERROR_INT(
"no program chosen for display", procName, 1);
951 if ((cmap = pixGetColormap(pixs)) != NULL)
953 spp = pixGetSpp(pixs);
954 threeviews = (spp == 4 || !opaque) ? TRUE : FALSE;
964 maxheight = (threeviews) ? MaxDisplayHeight / 3 : MaxDisplayHeight;
965 if (w <= MaxDisplayWidth && h <= maxheight) {
971 ratw = (l_float32)MaxDisplayWidth / (l_float32)w;
972 rath = (l_float32)maxheight / (l_float32)h;
973 ratmin = L_MIN(ratw, rath);
974 if (ratmin < 0.125 && d == 1)
976 else if (ratmin < 0.25 && d == 1)
978 else if (ratmin < 0.33 && d == 1)
980 else if (ratmin < 0.5 && d == 1)
983 pix1 =
pixScale(pix0, ratmin, ratmin);
987 return ERROR_INT(
"pix1 not made", procName, 1);
1001 if (pixGetDepth(pix2) < 8 || pixGetColormap(pix2) ||
1002 (w < MaxSizeForPng && h < MaxSizeForPng)) {
1003 snprintf(buffer, Bufsize,
"/tmp/lept/disp/write.%03d.png", index);
1004 pixWrite(buffer, pix2, IFF_PNG);
1006 snprintf(buffer, Bufsize,
"/tmp/lept/disp/write.%03d.jpg", index);
1007 pixWrite(buffer, pix2, IFF_JFIF_JPEG);
1017 snprintf(buffer, Bufsize,
1018 "xzgv --geometry %dx%d+%d+%d %s &", wt + 10, ht + 10,
1022 snprintf(buffer, Bufsize,
1023 "xli -dispgamma 1.0 -quiet -geometry +%d+%d -title \"%s\" %s &",
1024 x, y, title, tempname);
1026 snprintf(buffer, Bufsize,
1027 "xli -dispgamma 1.0 -quiet -geometry +%d+%d %s &",
1032 snprintf(buffer, Bufsize,
1033 "xv -quit -geometry +%d+%d -name \"%s\" %s &",
1034 x, y, title, tempname);
1036 snprintf(buffer, Bufsize,
1037 "xv -quit -geometry +%d+%d %s &", x, y, tempname);
1040 snprintf(buffer, Bufsize,
"open %s &", tempname);
1048 _fullpath(fullpath, pathname,
sizeof(fullpath));
1050 snprintf(buffer, Bufsize,
1051 "i_view32.exe \"%s\" /pos=(%d,%d) /title=\"%s\"",
1052 fullpath, x, y, title);
1054 snprintf(buffer, Bufsize,
"i_view32.exe \"%s\" /pos=(%d,%d)",
1058 LEPT_FREE(pathname);
1064 LEPT_FREE(tempname);
1086 pixMakeColorSquare(l_uint32 color,
1093 l_int32 w, rval, gval, bval;
1097 PROCNAME(
"pixMakeColorSquare");
1099 w = (size <= 0) ? 100 : size;
1100 if (addlabel && w < 100) {
1101 L_WARNING(
"size too small for label; omitting label\n", procName);
1105 if ((pix1 =
pixCreate(w, w, 32)) == NULL)
1106 return (
PIX *)ERROR_PTR(
"pix1 not madel", procName, NULL);
1114 L_ERROR(
"invalid location: adding below\n", procName);
1119 snprintf(buf,
sizeof(buf),
"%d,%d,%d", rval, gval, bval);
1128 l_chooseDisplayProg(l_int32 selection)
1135 var_DISPLAY_PROG = selection;
1137 L_ERROR(
"invalid display program\n",
"l_chooseDisplayProg");
1159 changeFormatForMissingLib(l_int32 *pformat)
1161 PROCNAME(
"changeFormatForMissingLib");
1163 #if !defined(HAVE_LIBJPEG) 1164 if (*pformat == IFF_JFIF_JPEG) {
1165 L_WARNING(
"jpeg library missing; output bmp format\n", procName);
1169 #if !defined(HAVE_LIBPNG) 1170 if (*pformat == IFF_PNG) {
1171 L_WARNING(
"png library missing; output bmp format\n", procName);
1175 #if !defined(HAVE_LIBTIFF) 1176 if (L_FORMAT_IS_TIFF(*pformat)) {
1177 L_WARNING(
"tiff library missing; output bmp format\n", procName);
1200 pixDisplayWrite(
PIX *pixs,
1203 lept_stderr(
"\n########################################################\n" 1204 " pixDisplayWrite() was last used in tesseract 3.04," 1205 " in Feb 2016. As of 1.80, it is a non-functional stub\n" 1206 "########################################################" void bmfDestroy(L_BMF **pbmf)
bmfDestroy()
PIX * pixRemoveColormap(PIX *pixs, l_int32 type)
pixRemoveColormap()
l_ok pixWriteStreamBmp(FILE *fp, PIX *pix)
pixWriteStreamBmp()
l_ok pixWriteMemPS(l_uint8 **pdata, size_t *psize, PIX *pix, BOX *box, l_int32 res, l_float32 scale)
pixWriteMemPS()
l_ok pixWriteMemTiff(l_uint8 **pdata, size_t *psize, PIX *pix, l_int32 comptype)
pixWriteMemTiff()
l_int32 lept_mkdir(const char *subdir)
lept_mkdir()
PIX * pixScaleToGray(PIX *pixs, l_float32 scalefactor)
pixScaleToGray()
l_ok splitPathAtExtension(const char *pathname, char **pbasename, char **pextension)
splitPathAtExtension()
l_ok pixWriteMemBmp(l_uint8 **pfdata, size_t *pfsize, PIX *pixs)
pixWriteMemBmp()
char * genPathname(const char *dir, const char *fname)
genPathname()
l_ok pixWriteMemJpeg(l_uint8 **pdata, size_t *psize, PIX *pix, l_int32 quality, l_int32 progressive)
pixWriteMemJpeg()
void lept_stderr(const char *fmt,...)
lept_stderr()
PIX * pixCreate(l_int32 width, l_int32 height, l_int32 depth)
pixCreate()
l_ok pixWriteStreamTiff(FILE *fp, PIX *pix, l_int32 comptype)
pixWriteStreamTiff()
l_ok pixWriteMemPdf(l_uint8 **pdata, size_t *pnbytes, PIX *pix, l_int32 res, const char *title)
pixWriteMemPdf()
l_ok pixWriteStreamPng(FILE *fp, PIX *pix, l_float32 gamma)
pixWriteStreamPng()
l_ok pixWriteStreamSpix(FILE *fp, PIX *pix)
pixWriteStreamSpix()
PIX * pixScaleToGray3(PIX *pixs)
pixScaleToGray3()
PIX * pixScaleToGray8(PIX *pixs)
pixScaleToGray8()
l_ok pixSetAllArbitrary(PIX *pix, l_uint32 val)
pixSetAllArbitrary()
l_ok pixWriteStreamJpeg(FILE *fp, PIX *pixs, l_int32 quality, l_int32 progressive)
pixWriteStreamJpeg()
l_ok pixWriteStreamPdf(FILE *fp, PIX *pix, l_int32 res, const char *title)
pixWriteStreamPdf()
l_ok pixWriteMemPng(l_uint8 **pfiledata, size_t *pfilesize, PIX *pix, l_float32 gamma)
pixWriteMemPng()
l_ok pixWriteJpeg(const char *filename, PIX *pix, l_int32 quality, l_int32 progressive)
pixWriteJpeg()
PIX * pixClone(PIX *pixs)
pixClone()
l_ok pixcmapIsOpaque(PIXCMAP *cmap, l_int32 *popaque)
pixcmapIsOpaque()
l_ok pixWriteStreamPnm(FILE *fp, PIX *pix)
pixWriteStreamPnm()
void pixDestroy(PIX **ppix)
pixDestroy()
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
PIX * pixRead(const char *filename)
pixRead()
l_ok pixWriteStreamPS(FILE *fp, PIX *pix, BOX *box, l_int32 res, l_float32 scale)
pixWriteStreamPS()
l_ok pixWriteMemSpix(l_uint8 **pdata, size_t *psize, PIX *pix)
pixWriteMemSpix()
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()
void callSystemDebug(const char *cmd)
callSystemDebug()
PIX * pixConvert16To8(PIX *pixs, l_int32 type)
pixConvert16To8()
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()
void extractRGBValues(l_uint32 pixel, l_int32 *prval, l_int32 *pgval, l_int32 *pbval)
extractRGBValues()
l_int32 pixaGetCount(PIXA *pixa)
pixaGetCount()
l_ok pixWriteMemPnm(l_uint8 **pdata, size_t *psize, PIX *pix)
pixWriteMemPnm()
PIX * pixScaleToGray2(PIX *pixs)
pixScaleToGray2()
L_BMF * bmfCreate(const char *dir, l_int32 fontsize)
bmfCreate()