88 #include "allheaders.h" 93 #elif defined(__APPLE__) 99 static const l_int32 L_BUFSIZE = 512;
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 = 20;
117 LEPT_DLL
const char *ImageFileFormatExtensions[] =
146 { {
".bmp", IFF_BMP },
147 {
".jpg", IFF_JFIF_JPEG },
148 {
".jpeg", IFF_JFIF_JPEG },
150 {
".tif", IFF_TIFF },
151 {
".tiff", IFF_TIFF },
156 {
".pdf", IFF_LPDF },
157 {
".webp", IFF_WEBP } };
164 static l_int32 var_JPEG_QUALITY = 75;
183 l_jpegSetQuality(l_int32 new_quality)
187 PROCNAME(
"l_jpeqSetQuality");
189 prevq = var_JPEG_QUALITY;
190 newq = (new_quality == 0) ? 75 : new_quality;
191 if (newq < 1 || newq > 100)
192 L_ERROR(
"invalid jpeg quality; unchanged\n", procName);
194 var_JPEG_QUALITY = newq;
202 l_int32 LeptDebugOK = 0;
218 setLeptDebugOK(l_int32 allow)
220 if (allow != 0) allow = 1;
243 pixaWriteFiles(
const char *rootname,
247 char bigbuf[L_BUFSIZE];
248 l_int32 i, n, pixformat;
251 PROCNAME(
"pixaWriteFiles");
254 return ERROR_INT(
"rootname not defined", procName, 1);
256 return ERROR_INT(
"pixa not defined", procName, 1);
257 if (format < 0 || format == IFF_UNKNOWN ||
258 format >= NumImageFileFormatExtensions)
259 return ERROR_INT(
"invalid format", procName, 1);
262 for (i = 0; i < n; i++) {
264 if (format == IFF_DEFAULT)
265 pixformat = pixChooseOutputFormat(pix);
268 snprintf(bigbuf, L_BUFSIZE,
"%s%03d.%s", rootname, i,
269 ImageFileFormatExtensions[pixformat]);
270 pixWrite(bigbuf, pix, pixformat);
296 pixWriteDebug(
const char *fname,
300 PROCNAME(
"pixWriteDebug");
303 return pixWrite(fname, pix, format);
305 L_INFO(
"write to named temp file %s is disabled\n", procName, fname);
333 pixWrite(
const char *fname,
340 PROCNAME(
"pixWrite");
343 return ERROR_INT(
"pix not defined", procName, 1);
345 return ERROR_INT(
"fname not defined", procName, 1);
348 return ERROR_INT(
"stream not opened", procName, 1);
350 ret = pixWriteStream(fp, pix, format);
353 return ERROR_INT(
"pix not written to stream", procName, 1);
366 pixWriteAutoFormat(
const char *filename,
371 PROCNAME(
"pixWriteAutoFormat");
374 return ERROR_INT(
"pix not defined", procName, 1);
376 return ERROR_INT(
"filename not defined", procName, 1);
378 if (pixGetAutoFormat(pix, &format))
379 return ERROR_INT(
"auto format not returned", procName, 1);
380 return pixWrite(filename, pix, format);
393 pixWriteStream(FILE *fp,
397 PROCNAME(
"pixWriteStream");
400 return ERROR_INT(
"stream not defined", procName, 1);
402 return ERROR_INT(
"pix not defined", procName, 1);
404 if (format == IFF_DEFAULT)
405 format = pixChooseOutputFormat(pix);
422 case IFF_TIFF_PACKBITS:
441 return pixWriteStreamGif(fp, pix);
445 return pixWriteStreamJp2k(fp, pix, 34, 4, 0, 0);
449 return pixWriteStreamWebP(fp, pix, 80, 0);
461 return ERROR_INT(
"unknown format", procName, 1);
486 pixWriteImpliedFormat(
const char *filename,
493 PROCNAME(
"pixWriteImpliedFormat");
496 return ERROR_INT(
"filename not defined", procName, 1);
498 return ERROR_INT(
"pix not defined", procName, 1);
501 format = getImpliedFileFormat(filename);
502 if (format == IFF_UNKNOWN) {
504 }
else if (format == IFF_TIFF) {
505 if (pixGetDepth(pix) == 1)
506 format = IFF_TIFF_G4;
509 format = IFF_TIFF_LZW;
511 format = IFF_TIFF_ZIP;
515 if (format == IFF_JFIF_JPEG) {
516 quality = L_MIN(quality, 100);
517 quality = L_MAX(quality, 0);
518 if (progressive != 0 && progressive != 1) {
520 L_WARNING(
"invalid progressive; setting to baseline\n", procName);
526 pixWrite(filename, pix, format);
551 pixChooseOutputFormat(
PIX *pix)
555 PROCNAME(
"pixChooseOutputFormat");
558 return ERROR_INT(
"pix not defined", procName, 0);
560 d = pixGetDepth(pix);
561 format = pixGetInputFormat(pix);
562 if (format == IFF_UNKNOWN) {
564 format = IFF_TIFF_G4;
586 getImpliedFileFormat(
const char *filename)
590 l_int32 format = IFF_UNKNOWN;
595 numext =
sizeof(extension_map) /
sizeof(extension_map[0]);
596 for (i = 0; i < numext; i++) {
597 if (!strcmp(extension, extension_map[i].extension)) {
598 format = extension_map[i].format;
603 LEPT_FREE(extension);
627 pixGetAutoFormat(
PIX *pix,
633 PROCNAME(
"pixGetAutoFormat");
636 return ERROR_INT(
"&format not defined", procName, 0);
637 *pformat = IFF_UNKNOWN;
639 return ERROR_INT(
"pix not defined", procName, 0);
641 d = pixGetDepth(pix);
642 cmap = pixGetColormap(pix);
643 if (d == 1 && !cmap) {
644 *pformat = IFF_TIFF_G4;
645 }
else if ((d == 8 && !cmap) || d == 24 || d == 32) {
646 *pformat = IFF_JFIF_JPEG;
668 getFormatExtension(l_int32 format)
670 PROCNAME(
"getFormatExtension");
672 if (format < 0 || format >= NumImageFileFormatExtensions)
673 return (
const char *)ERROR_PTR(
"invalid format", procName, NULL);
675 return ImageFileFormatExtensions[format];
703 pixWriteMem(l_uint8 **pdata,
710 PROCNAME(
"pixWriteMem");
713 return ERROR_INT(
"&data not defined", procName, 1 );
715 return ERROR_INT(
"&size not defined", procName, 1 );
717 return ERROR_INT(
"&pix not defined", procName, 1 );
719 if (format == IFF_DEFAULT)
720 format = pixChooseOutputFormat(pix);
737 case IFF_TIFF_PACKBITS:
751 ret =
pixWriteMemPS(pdata, psize, pix, NULL, 0, DEFAULT_SCALING);
755 ret = pixWriteMemGif(pdata, psize, pix);
759 ret = pixWriteMemJp2k(pdata, psize, pix, 34, 0, 0, 0);
763 ret = pixWriteMemWebP(pdata, psize, pix, 80, 0);
775 return ERROR_INT(
"unknown format", procName, 1);
803 l_fileDisplay(
const char *fname,
810 PROCNAME(
"l_fileDisplay");
813 L_INFO(
"displaying files is disabled; " 814 "use setLeptDebugOK(1) to enable\n", procName);
820 return ERROR_INT(
"invalid scale factor", procName, 1);
821 if ((pixs =
pixRead(fname)) == NULL)
822 return ERROR_INT(
"pixs not read", procName, 1);
827 if (scale < 1.0 && pixGetDepth(pixs) == 1)
830 pixd =
pixScale(pixs, scale, scale);
832 pixDisplay(pixd, x, y);
879 pixDisplay(
PIX *pixs,
883 return pixDisplayWithTitle(pixs, x, y, NULL, 1);
903 pixDisplayWithTitle(
PIX *pixs,
910 char buffer[L_BUFSIZE];
911 static l_int32 index = 0;
912 l_int32 w, h, d, spp, maxheight, opaque, threeviews;
913 l_float32 ratw, rath, ratmin;
914 PIX *pix0, *pix1, *pix2;
920 char fullpath[_MAX_PATH];
923 PROCNAME(
"pixDisplayWithTitle");
926 L_INFO(
"displaying images is disabled;\n " 927 "use setLeptDebugOK(1) to enable\n", procName);
932 return ERROR_INT(
"iOS 11 does not support system()", procName, 1);
935 if (dispflag != 1)
return 0;
937 return ERROR_INT(
"pixs not defined", procName, 1);
943 return ERROR_INT(
"no program chosen for display", procName, 1);
949 if ((cmap = pixGetColormap(pixs)) != NULL)
951 spp = pixGetSpp(pixs);
952 threeviews = (spp == 4 || !opaque) ? TRUE : FALSE;
962 maxheight = (threeviews) ? MAX_DISPLAY_HEIGHT / 3 : MAX_DISPLAY_HEIGHT;
963 if (w <= MAX_DISPLAY_WIDTH && h <= maxheight) {
969 ratw = (l_float32)MAX_DISPLAY_WIDTH / (l_float32)w;
970 rath = (l_float32)maxheight / (l_float32)h;
971 ratmin = L_MIN(ratw, rath);
972 if (ratmin < 0.125 && d == 1)
974 else if (ratmin < 0.25 && d == 1)
976 else if (ratmin < 0.33 && d == 1)
978 else if (ratmin < 0.5 && d == 1)
981 pix1 =
pixScale(pix0, ratmin, ratmin);
985 return ERROR_INT(
"pix1 not made", procName, 1);
999 if (pixGetDepth(pix2) < 8 || pixGetColormap(pix2) ||
1000 (w < MAX_SIZE_FOR_PNG && h < MAX_SIZE_FOR_PNG)) {
1001 snprintf(buffer, L_BUFSIZE,
"/tmp/lept/disp/write.%03d.png", index);
1002 pixWrite(buffer, pix2, IFF_PNG);
1004 snprintf(buffer, L_BUFSIZE,
"/tmp/lept/disp/write.%03d.jpg", index);
1005 pixWrite(buffer, pix2, IFF_JFIF_JPEG);
1015 snprintf(buffer, L_BUFSIZE,
1016 "xzgv --geometry %dx%d+%d+%d %s &", wt + 10, ht + 10,
1020 snprintf(buffer, L_BUFSIZE,
1021 "xli -dispgamma 1.0 -quiet -geometry +%d+%d -title \"%s\" %s &",
1022 x, y, title, tempname);
1024 snprintf(buffer, L_BUFSIZE,
1025 "xli -dispgamma 1.0 -quiet -geometry +%d+%d %s &",
1030 snprintf(buffer, L_BUFSIZE,
1031 "xv -quit -geometry +%d+%d -name \"%s\" %s &",
1032 x, y, title, tempname);
1034 snprintf(buffer, L_BUFSIZE,
1035 "xv -quit -geometry +%d+%d %s &", x, y, tempname);
1038 snprintf(buffer, L_BUFSIZE,
"open %s &", tempname);
1046 _fullpath(fullpath, pathname,
sizeof(fullpath));
1048 snprintf(buffer, L_BUFSIZE,
1049 "i_view32.exe \"%s\" /pos=(%d,%d) /title=\"%s\"",
1050 fullpath, x, y, title);
1052 snprintf(buffer, L_BUFSIZE,
"i_view32.exe \"%s\" /pos=(%d,%d)",
1056 LEPT_FREE(pathname);
1062 LEPT_FREE(tempname);
1079 pixSaveTiled(
PIX *pixs,
1081 l_float32 scalefactor,
1087 return pixSaveTiledOutline(pixs, pixa, scalefactor, newrow, space, 0, dp);
1130 pixSaveTiledOutline(
PIX *pixs,
1132 l_float32 scalefactor,
1138 l_int32 n, top, left, bx, by, bw, w, h, depth, bottom;
1140 PIX *pix1, *pix2, *pix3, *pix4;
1142 PROCNAME(
"pixSaveTiledOutline");
1144 if (scalefactor == 0.0)
return 0;
1147 return ERROR_INT(
"pixs not defined", procName, 1);
1149 return ERROR_INT(
"pixa not defined", procName, 1);
1154 if (dp != 8 && dp != 32) {
1155 L_WARNING(
"dp not 8 or 32 bpp; using 32\n", procName);
1162 depth = pixGetDepth(pix1);
1163 bottom = pixGetInputFormat(pix1);
1172 if (scalefactor == 1.0) {
1174 }
else if (scalefactor > 1.0) {
1175 pix2 =
pixScale(pix1, scalefactor, scalefactor);
1177 if (pixGetDepth(pix1) == 1)
1180 pix2 =
pixScale(pix1, scalefactor, scalefactor);
1200 }
else if (newrow == 1) {
1201 top = bottom + space;
1206 left = bx + bw + space;
1210 bottom = L_MAX(bottom, top + h);
1217 pixSetInputFormat(pix1, bottom);
1260 pixSaveTiledWithText(
PIX *pixs,
1267 const char *textstr,
1271 PIX *pix1, *pix2, *pix3, *pix4;
1273 PROCNAME(
"pixSaveTiledWithText");
1275 if (outwidth == 0)
return 0;
1278 return ERROR_INT(
"pixs not defined", procName, 1);
1280 return ERROR_INT(
"pixa not defined", procName, 1);
1292 pixSaveTiled(pix4, pixa, 1.0, newrow, space, 32);
1302 l_chooseDisplayProg(l_int32 selection)
1309 var_DISPLAY_PROG = selection;
1311 L_ERROR(
"invalid display program\n",
"l_chooseDisplayProg");
1349 pixDisplayWrite(
PIX *pixs,
1352 char buf[L_BUFSIZE];
1356 static l_int32 index = 0;
1358 PROCNAME(
"pixDisplayWrite");
1360 if (reduction == 0)
return 0;
1361 if (reduction < 0) {
1367 return ERROR_INT(
"pixs not defined", procName, 1);
1372 if (reduction == 1) {
1375 scale = 1. / (l_float32)reduction;
1376 if (pixGetDepth(pixs) == 1)
1379 pix1 =
pixScale(pixs, scale, scale);
1382 if (pixGetDepth(pix1) == 16) {
1384 snprintf(buf, L_BUFSIZE,
"file.%03d.png", index);
1385 fname =
pathJoin(
"/tmp/lept/display", buf);
1386 pixWrite(fname, pix2, IFF_PNG);
1388 }
else if (pixGetDepth(pix1) < 8 || pixGetColormap(pix1)) {
1389 snprintf(buf, L_BUFSIZE,
"file.%03d.png", index);
1390 fname =
pathJoin(
"/tmp/lept/display", buf);
1391 pixWrite(fname, pix1, IFF_PNG);
1393 snprintf(buf, L_BUFSIZE,
"file.%03d.jpg", index);
1394 fname =
pathJoin(
"/tmp/lept/display", buf);
1395 pixWrite(fname, pix1, IFF_JFIF_JPEG);
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()
PIX * pixConvertTo32(PIX *pixs)
pixConvertTo32()
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()
PIX * pixMaxDynamicRange(PIX *pixs, l_int32 type)
pixMaxDynamicRange()
l_ok pixWriteMemJpeg(l_uint8 **pdata, size_t *psize, PIX *pix, l_int32 quality, l_int32 progressive)
pixWriteMemJpeg()
PIX * pixConvertTo8(PIX *pixs, l_int32 cmapflag)
pixConvertTo8()
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()
PIX * pixRemoveColormapGeneral(PIX *pixs, l_int32 type, l_int32 ifnocmap)
pixRemoveColormapGeneral()
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 * pixAddBorder(PIX *pixs, l_int32 npix, l_uint32 val)
pixAddBorder()
PIX * pixScaleToGray8(PIX *pixs)
pixScaleToGray8()
l_ok pixaAddPix(PIXA *pixa, PIX *pix, l_int32 copyflag)
pixaAddPix()
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 pixaGetBoxGeometry(PIXA *pixa, l_int32 index, l_int32 *px, l_int32 *py, l_int32 *pw, l_int32 *ph)
pixaGetBoxGeometry()
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()
PIX * pixScaleToSize(PIX *pixs, l_int32 wd, l_int32 hd)
pixScaleToSize()
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()
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()
void callSystemDebug(const char *cmd)
callSystemDebug()
PIX * pixConvert16To8(PIX *pixs, l_int32 type)
pixConvert16To8()
l_int32 lept_rmdir(const char *subdir)
lept_rmdir()
l_ok pixaAddBox(PIXA *pixa, BOX *box, l_int32 copyflag)
pixaAddBox()
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()
l_ok pixWriteMemPnm(l_uint8 **pdata, size_t *psize, PIX *pix)
pixWriteMemPnm()
PIX * pixScaleToGray2(PIX *pixs)
pixScaleToGray2()