315l_int32 format, ret, valid;
321 return (
PIX *)ERROR_PTR(
"stream not defined", __func__, NULL);
328 if ((pix = pixReadStreamBmp(fp)) == NULL )
329 return (
PIX *)ERROR_PTR(
"bmp: no pix returned", __func__, NULL);
333 if ((pix = pixReadStreamJpeg(fp, 0, 1, NULL, hint)) == NULL)
334 return (
PIX *)ERROR_PTR(
"jpeg: no pix returned", __func__, NULL);
335 ret = fgetJpegComment(fp, &comment);
337 pixSetText(pix, (
char *)comment);
342 if ((pix = pixReadStreamPng(fp)) == NULL)
343 return (
PIX *)ERROR_PTR(
"png: no pix returned", __func__, NULL);
347 case IFF_TIFF_PACKBITS:
354 if ((pix = pixReadStreamTiff(fp, 0)) == NULL)
355 return (
PIX *)ERROR_PTR(
"tiff: no pix returned", __func__, NULL);
359 if ((pix = pixReadStreamPnm(fp)) == NULL)
360 return (
PIX *)ERROR_PTR(
"pnm: no pix returned", __func__, NULL);
364 if ((pix = pixReadStreamGif(fp)) == NULL)
365 return (
PIX *)ERROR_PTR(
"gif: no pix returned", __func__, NULL);
369 if ((pix = pixReadStreamJp2k(fp, 1, NULL, 0, 0)) == NULL)
370 return (
PIX *)ERROR_PTR(
"jp2: no pix returned", __func__, NULL);
374 if ((pix = pixReadStreamWebP(fp)) == NULL)
375 return (
PIX *)ERROR_PTR(
"webp: no pix returned", __func__, NULL);
379 L_ERROR(
"PostScript reading is not supported\n", __func__);
383 L_ERROR(
"Pdf reading is not supported\n", __func__);
387 if ((pix = pixReadStreamSpix(fp)) == NULL)
388 return (
PIX *)ERROR_PTR(
"spix: no pix returned", __func__, NULL);
392 return (
PIX *)ERROR_PTR(
"Unknown format: no pix returned",
398 pixSetInputFormat(pix, format);
399 if ((cmap = pixGetColormap(pix))) {
400 pixcmapIsValid(cmap, pix, &valid);
403 return (
PIX *)ERROR_PTR(
"invalid colormap", __func__, NULL);
442l_int32 format, ret, w, h, d, bps, spp, iscmap;
451 if (piscmap) *piscmap = 0;
452 if (pformat) *pformat = 0;
455 return ERROR_INT(
"filename not defined", __func__, 1);
457 if ((fp = fopenReadStream(filename)) == NULL)
458 return ERROR_INT(
"image file not found", __func__, 1);
465 if ((pix =
pixRead(filename)) == NULL)
466 return ERROR_INT(
"bmp: pix not read", __func__, 1);
467 pixGetDimensions(pix, &w, &h, &d);
468 if (pixGetColormap(pix))
471 bps = (d == 32) ? 8 : d;
472 spp = (d == 32) ? 3 : 1;
476 ret = readHeaderJpeg(filename, &w, &h, &spp, NULL, NULL);
479 return ERROR_INT(
"jpeg: no header info returned", __func__, 1);
483 ret = readHeaderPng(filename, &w, &h, &bps, &spp, &iscmap);
485 return ERROR_INT(
"png: no header info returned", __func__, 1);
489 case IFF_TIFF_PACKBITS:
497 ret = readHeaderTiff(filename, 0, &w, &h, &bps, &spp, NULL, &iscmap,
500 return ERROR_INT(
"tiff: no header info returned", __func__, 1);
504 ret = readHeaderPnm(filename, &w, &h, &d, &type, &bps, &spp);
506 return ERROR_INT(
"pnm: no header info returned", __func__, 1);
510 if ((pix =
pixRead(filename)) == NULL)
511 return ERROR_INT(
"gif: pix not read", __func__, 1);
512 pixGetDimensions(pix, &w, &h, &d);
520 ret = readHeaderJp2k(filename, &w, &h, &bps, &spp, NULL);
524 if (readHeaderWebP(filename, &w, &h, &spp))
525 return ERROR_INT(
"webp: no header info returned", __func__, 1);
530 if (pformat) *pformat = format;
531 return ERROR_INT(
"PostScript reading is not supported\n", __func__, 1);
534 if (pformat) *pformat = format;
535 return ERROR_INT(
"Pdf reading is not supported\n", __func__, 1);
538 ret = readHeaderSpix(filename, &w, &h, &bps, &spp, &iscmap);
540 return ERROR_INT(
"spix: no header info returned", __func__, 1);
544 L_ERROR(
"unknown format in file %s\n", __func__, filename);
551 if (pbps) *pbps = bps;
552 if (pspp) *pspp = spp;
553 if (piscmap) *piscmap = iscmap;
554 if (pformat) *pformat = format;
825l_int32 format, valid;
830 return (
PIX *)ERROR_PTR(
"data not defined", __func__, NULL);
832 return (
PIX *)ERROR_PTR(
"size < 12", __func__, NULL);
839 if ((pix = pixReadMemBmp(data, size)) == NULL )
840 return (
PIX *)ERROR_PTR(
"bmp: no pix returned", __func__, NULL);
844 if ((pix = pixReadMemJpeg(data, size, 0, 1, NULL, 0)) == NULL)
845 return (
PIX *)ERROR_PTR(
"jpeg: no pix returned", __func__, NULL);
849 if ((pix = pixReadMemPng(data, size)) == NULL)
850 return (
PIX *)ERROR_PTR(
"png: no pix returned", __func__, NULL);
854 case IFF_TIFF_PACKBITS:
861 if ((pix = pixReadMemTiff(data, size, 0)) == NULL)
862 return (
PIX *)ERROR_PTR(
"tiff: no pix returned", __func__, NULL);
866 if ((pix = pixReadMemPnm(data, size)) == NULL)
867 return (
PIX *)ERROR_PTR(
"pnm: no pix returned", __func__, NULL);
871 if ((pix = pixReadMemGif(data, size)) == NULL)
872 return (
PIX *)ERROR_PTR(
"gif: no pix returned", __func__, NULL);
876 if ((pix = pixReadMemJp2k(data, size, 1, NULL, 0, 0)) == NULL)
877 return (
PIX *)ERROR_PTR(
"jp2k: no pix returned", __func__, NULL);
881 if ((pix = pixReadMemWebP(data, size)) == NULL)
882 return (
PIX *)ERROR_PTR(
"webp: no pix returned", __func__, NULL);
886 L_ERROR(
"PostScript reading is not supported\n", __func__);
890 L_ERROR(
"Pdf reading is not supported\n", __func__);
894 if ((pix = pixReadMemSpix(data, size)) == NULL)
895 return (
PIX *)ERROR_PTR(
"spix: no pix returned", __func__, NULL);
899 return (
PIX *)ERROR_PTR(
"Unknown format: no pix returned",
908 if (format == IFF_TIFF && pixGetDepth(pix) == 1)
909 format = IFF_TIFF_G4;
910 pixSetInputFormat(pix, format);
911 if ((cmap = pixGetColormap(pix))) {
912 pixcmapIsValid(cmap, pix, &valid);
915 return (
PIX *)ERROR_PTR(
"invalid colormap", __func__, NULL);
918 pixSetPadBits(pix, 0);
959l_int32 format, ret, w, h, d, bps, spp, iscmap;
967 if (piscmap) *piscmap = 0;
968 if (pformat) *pformat = 0;
971 return ERROR_INT(
"data not defined", __func__, 1);
973 return ERROR_INT(
"size < 12", __func__, 1);
980 if ((pix = pixReadMemBmp(data, size)) == NULL)
981 return ERROR_INT(
"bmp: pix not read", __func__, 1);
982 pixGetDimensions(pix, &w, &h, &d);
984 bps = (d == 32) ? 8 : d;
985 spp = (d == 32) ? 3 : 1;
989 ret = readHeaderMemJpeg(data, size, &w, &h, &spp, NULL, NULL);
992 return ERROR_INT(
"jpeg: no header info returned", __func__, 1);
996 ret = readHeaderMemPng(data, size, &w, &h, &bps, &spp, &iscmap);
998 return ERROR_INT(
"png: no header info returned", __func__, 1);
1002 case IFF_TIFF_PACKBITS:
1010 ret = readHeaderMemTiff(data, size, 0, &w, &h, &bps, &spp,
1011 NULL, &iscmap, &format);
1013 return ERROR_INT(
"tiff: no header info returned", __func__, 1);
1017 ret = readHeaderMemPnm(data, size, &w, &h, &d, &type, &bps, &spp);
1019 return ERROR_INT(
"pnm: no header info returned", __func__, 1);
1023 if ((pix = pixReadMemGif(data, size)) == NULL)
1024 return ERROR_INT(
"gif: pix not read", __func__, 1);
1025 pixGetDimensions(pix, &w, &h, &d);
1033 ret = readHeaderMemJp2k(data, size, &w, &h, &bps, &spp, NULL);
1038 ret = readHeaderMemWebP(data, size, &w, &h, &spp);
1042 if (pformat) *pformat = format;
1043 return ERROR_INT(
"PostScript reading is not supported\n", __func__, 1);
1046 if (pformat) *pformat = format;
1047 return ERROR_INT(
"Pdf reading is not supported\n", __func__, 1);
1050 ret = sreadHeaderSpix((l_uint32 *)data, size, &w, &h, &bps,
1053 return ERROR_INT(
"pnm: no header info returned", __func__, 1);
1057 return ERROR_INT(
"unknown format; no data returned", __func__, 1);
1063 if (pbps) *pbps = bps;
1064 if (pspp) *pspp = spp;
1065 if (piscmap) *piscmap = iscmap;
1066 if (pformat) *pformat = format;
1100l_int32 w, h, d, wpl, count, npages, color;
1101l_int32 format, bps, spp, iscmap, xres, yres, transparency;
1107 return ERROR_INT(
"filename not defined", __func__, 1);
1109 return ERROR_INT(
"stream not defined", __func__, 1);
1112 if (
pixReadHeader(filename, &format, &w, &h, &bps, &spp, &iscmap)) {
1113 L_ERROR(
"failure to read header of %s\n", __func__, filename);
1116 fprintf(fpout,
"===============================================\n"
1117 "Reading the header:\n");
1118 fprintf(fpout,
" input image format type: %s\n",
1119 ImageFileFormatExtensions[format]);
1120 fprintf(fpout,
" w = %d, h = %d, bps = %d, spp = %d, iscmap = %d\n",
1121 w, h, bps, spp, iscmap);
1124 if (format == IFF_JP2) {
1125 fpin = lept_fopen(filename,
"rb");
1126 fgetJp2kResolution(fpin, &xres, &yres);
1128 fprintf(fpout,
" xres = %d, yres = %d\n", xres, yres);
1129 }
else if (format == IFF_PNG) {
1130 fpin = lept_fopen(filename,
"rb");
1131 fgetPngResolution(fpin, &xres, &yres);
1133 fprintf(fpout,
" xres = %d, yres = %d\n", xres, yres);
1135 fpin = lept_fopen(filename,
"rb");
1136 fgetPngColormapInfo(fpin, &cmap, &transparency);
1139 fprintf(fpout,
" colormap has transparency\n");
1141 fprintf(fpout,
" colormap does not have transparency\n");
1142 pixcmapWriteStream(fpout, cmap);
1143 pixcmapDestroy(&cmap);
1145 }
else if (format == IFF_JFIF_JPEG) {
1146 fpin = lept_fopen(filename,
"rb");
1147 fgetJpegResolution(fpin, &xres, &yres);
1149 fprintf(fpout,
" xres = %d, yres = %d\n", xres, yres);
1157 fprintf(fpout,
"===============================================\n"
1158 "Reading the full image:\n");
1161 if (format == IFF_PNG && bps == 16)
1162 l_pngSetReadStrip16To8(0);
1164 if ((pix =
pixRead(filename)) == NULL) {
1165 L_ERROR(
"failure to read full image of %s\n", __func__, filename);
1169 format = pixGetInputFormat(pix);
1170 pixGetDimensions(pix, &w, &h, &d);
1171 wpl = pixGetWpl(pix);
1172 spp = pixGetSpp(pix);
1173 fprintf(fpout,
" input image format type: %s\n",
1174 ImageFileFormatExtensions[format]);
1175 fprintf(fpout,
" w = %d, h = %d, d = %d, spp = %d, wpl = %d\n",
1177 fprintf(fpout,
" xres = %d, yres = %d\n",
1178 pixGetXRes(pix), pixGetYRes(pix));
1180 text = pixGetText(pix);
1182 fprintf(fpout,
" text: %s\n", text);
1184 cmap = pixGetColormap(pix);
1186 pixcmapHasColor(cmap, &color);
1188 fprintf(fpout,
" colormap exists and has color values:");
1190 fprintf(fpout,
" colormap exists and has only gray values:");
1191 pixcmapWriteStream(fpout, pixGetColormap(pix));
1194 fprintf(fpout,
" colormap does not exist\n");
1196 if (format == IFF_TIFF || format == IFF_TIFF_G4 ||
1197 format == IFF_TIFF_G3 || format == IFF_TIFF_PACKBITS) {
1198 fprintf(fpout,
" Tiff header information:\n");
1199 fpin = lept_fopen(filename,
"rb");
1200 tiffGetCount(fpin, &npages);
1203 fprintf(fpout,
" One page in file\n");
1205 fprintf(fpout,
" %d pages in file\n", npages);
1206 fprintTiffInfo(fpout, filename);
1210 pixCountPixels(pix, &count, NULL);
1211 pixGetDimensions(pix, &w, &h, NULL);
1212 fprintf(fpout,
" 1 bpp: foreground pixel fraction ON/Total = %g\n",
1213 (l_float32)count / (l_float32)(w * h));
1215 fprintf(fpout,
"===============================================\n");
1221 if (pixGetSpp(pix) == 4) {
1222 pixt = pixDisplayLayersRGBA(pix, 0xffffff00, 600.0);
1223 pixDisplay(pixt, 100, 100);
1227 if (format == IFF_PNG && bps == 16)
1228 l_pngSetReadStrip16To8(1);
1263l_int32 w, h, d, depth, equal, problems;
1266PIX *pixs, *pixc, *pix1, *pix2;
1270 return ERROR_INT(
"filename not defined", __func__, 1);
1273 if ((pix1 =
pixRead(filename)) == NULL)
1274 return ERROR_INT(
"pix1 not made", __func__, 1);
1275 pixGetDimensions(pix1, &w, &h, NULL);
1276 if (w > 250 && h > 250) {
1277 box = boxCreate(w / 2 - 125, h / 2 - 125, 250, 250);
1278 pixs = pixClipRectangle(pix1, box, NULL);
1281 pixs = pixClone(pix1);
1285 lept_mkdir(
"lept/format");
1292 pixc = pixClone(pixs);
1296 if (pixGetSpp(pixc) == 4)
1298 cmap = pixGetColormap(pixc);
1299 d = pixGetDepth(pixc);
1310 if (d == 1 || d == 8) {
1311 L_INFO(
"write/read bmp\n", __func__);
1312 pixWrite(FILE_BMP, pixc, IFF_BMP);
1317 pix2 = pixClone(pix1);
1318 pixEqual(pixc, pix2, &equal);
1320 L_INFO(
" **** bad bmp image: d = %d ****\n", __func__, d);
1327 if (d == 2 || d == 4 || d == 32) {
1328 L_INFO(
"write/read bmp\n", __func__);
1329 pixWrite(FILE_BMP, pixc, IFF_BMP);
1331 pixEqual(pixc, pix1, &equal);
1333 L_INFO(
" **** bad bmp image: d = %d ****\n", __func__, d);
1344 L_INFO(
"write/read png\n", __func__);
1345 pixWrite(FILE_PNG, pixc, IFF_PNG);
1347 pixEqual(pixc, pix1, &equal);
1349 L_INFO(
" **** bad png image: d = %d ****\n", __func__, d);
1368 L_INFO(
"write/read uncompressed tiff\n", __func__);
1369 pixWrite(FILE_TIFF, pixc, IFF_TIFF);
1371 pixEqual(pixc, pix1, &equal);
1373 L_INFO(
" **** bad tiff uncompressed image: d = %d ****\n",
1380 L_INFO(
"write/read lzw compressed tiff\n", __func__);
1381 pixWrite(FILE_LZW, pixc, IFF_TIFF_LZW);
1383 pixEqual(pixc, pix1, &equal);
1385 L_INFO(
" **** bad tiff lzw compressed image: d = %d ****\n",
1392 L_INFO(
"write/read zip compressed tiff\n", __func__);
1393 pixWrite(FILE_ZIP, pixc, IFF_TIFF_ZIP);
1395 pixEqual(pixc, pix1, &equal);
1397 L_INFO(
" **** bad tiff zip compressed image: d = %d ****\n",
1404 if (d == 8 || d == 32) {
1406 L_INFO(
"write/read jpeg compressed tiff\n", __func__);
1407 if (d == 8 && pixGetColormap(pixc)) {
1409 pixWrite(FILE_TIFF_JPEG, pixc1, IFF_TIFF_JPEG);
1410 if ((pix1 =
pixRead(FILE_TIFF_JPEG)) == NULL) {
1411 L_INFO(
" did not read FILE_TIFF_JPEG\n", __func__);
1416 pixWrite(FILE_TIFF_JPEG, pixc, IFF_TIFF_JPEG);
1417 pix1 =
pixRead(FILE_TIFF_JPEG);
1419 pixCompareGray(pix1, pixc, L_COMPARE_ABS_DIFF, 0, NULL, &diff,
1422 pixCompareRGB(pix1, pixc, L_COMPARE_ABS_DIFF, 0, NULL, &diff,
1426 L_INFO(
" **** bad tiff jpeg compressed image: "
1427 "d = %d, diff = %5.2f ****\n", __func__, d, diff);
1436 L_INFO(
"write/read g4 compressed tiff\n", __func__);
1437 pixWrite(FILE_G4, pixc, IFF_TIFF_G4);
1439 pixEqual(pixc, pix1, &equal);
1441 L_INFO(
" **** bad tiff g4 image ****\n", __func__);
1446 L_INFO(
"write/read g3 compressed tiff\n", __func__);
1447 pixWrite(FILE_G3, pixc, IFF_TIFF_G3);
1449 pixEqual(pixc, pix1, &equal);
1451 L_INFO(
" **** bad tiff g3 image ****\n", __func__);
1456 L_INFO(
"write/read rle compressed tiff\n", __func__);
1457 pixWrite(FILE_RLE, pixc, IFF_TIFF_RLE);
1459 pixEqual(pixc, pix1, &equal);
1461 L_INFO(
" **** bad tiff rle image: d = %d ****\n", __func__, d);
1466 L_INFO(
"write/read packbits compressed tiff\n", __func__);
1467 pixWrite(FILE_PB, pixc, IFF_TIFF_PACKBITS);
1469 pixEqual(pixc, pix1, &equal);
1471 L_INFO(
" **** bad tiff packbits image: d = %d ****\n",
1485 L_INFO(
"write/read pnm\n", __func__);
1486 pixWrite(FILE_PNM, pixc, IFF_PNM);
1491 pix2 = pixClone(pixc);
1492 pixEqual(pix1, pix2, &equal);
1494 L_INFO(
" **** bad pnm image: d = %d ****\n", __func__, d);
1503 if (d != 8 || !cmap)
1504 pix1 = pixConvertTo8(pixc, 1);
1506 pix1 = pixClone(pixc);
1507 L_INFO(
"write/read gif\n", __func__);
1508 pixWrite(FILE_GIF, pix1, IFF_GIF);
1510 pixEqual(pix1, pix2, &equal);
1512 L_INFO(
" **** bad gif image: d = %d ****\n", __func__, d);
1523 pix1 = pixConvertTo32(pixc);
1525 pix1 = pixConvertTo8(pixc, 0);
1526 depth = pixGetDepth(pix1);
1527 L_INFO(
"write/read jpeg\n", __func__);
1528 pixWrite(FILE_JPG, pix1, IFF_JFIF_JPEG);
1531 pixCompareGray(pix1, pix2, L_COMPARE_ABS_DIFF, 0, NULL, &diff,
1534 pixCompareRGB(pix1, pix2, L_COMPARE_ABS_DIFF, 0, NULL, &diff,
1538 L_INFO(
" **** bad jpeg image: d = %d, diff = %5.2f ****\n",
1539 __func__, depth, diff);
1549 if (cmap || d <= 16)
1550 pix1 = pixConvertTo32(pixc);
1552 pix1 = pixClone(pixc);
1553 depth = pixGetDepth(pix1);
1554 L_INFO(
"write/read webp\n", __func__);
1555 pixWrite(FILE_WEBP, pix1, IFF_WEBP);
1557 pixCompareRGB(pix1, pix2, L_COMPARE_ABS_DIFF, 0, NULL, &diff, NULL, NULL);
1559 L_INFO(
" **** bad webp image: d = %d, diff = %5.2f ****\n",
1560 __func__, depth, diff);
1571 pix1 = pixConvertTo32(pixc);
1573 pix1 = pixConvertTo8(pixc, 0);
1574 depth = pixGetDepth(pix1);
1575 L_INFO(
"write/read jp2k\n", __func__);
1576 pixWrite(FILE_JP2K, pix1, IFF_JP2);
1579 pixCompareGray(pix1, pix2, L_COMPARE_ABS_DIFF, 0, NULL, &diff,
1582 pixCompareRGB(pix1, pix2, L_COMPARE_ABS_DIFF, 0, NULL, &diff,
1585 lept_stderr(
"diff = %7.3f\n", diff);
1587 L_INFO(
" **** bad jp2k image: d = %d, diff = %5.2f ****\n",
1588 __func__, depth, diff);
1595 if (problems == FALSE)
1596 L_INFO(
"All formats read and written OK!\n", __func__);