--------------------------------------------------------------------* * Simple (pixelwise) thresholding to 2 bpp with optional colormap * *--------------------------------------------------------------------*/ ! * \brief pixThresholdTo2bpp() * * \param[in] pixs 8 bpp * \param[in] nlevels equally spaced; must be between 2 and 4 * \param[in] cmapflag 1 to build colormap; 0 other
| 1370 | * </pre> |
| 1371 | */ |
| 1372 | PIX * |
| 1373 | pixThresholdTo2bpp(PIX *pixs, |
| 1374 | l_int32 nlevels, |
| 1375 | l_int32 cmapflag) |
| 1376 | { |
| 1377 | l_int32 *qtab; |
| 1378 | l_int32 w, h, d, wplt, wpld; |
| 1379 | l_uint32 *datat, *datad; |
| 1380 | PIX *pixt, *pixd; |
| 1381 | PIXCMAP *cmap; |
| 1382 | |
| 1383 | PROCNAME("pixThresholdTo2bpp"); |
| 1384 | |
| 1385 | if (!pixs) |
| 1386 | return (PIX *)ERROR_PTR("pixs not defined", procName, NULL); |
| 1387 | pixGetDimensions(pixs, &w, &h, &d); |
| 1388 | if (d != 8) |
| 1389 | return (PIX *)ERROR_PTR("pixs not 8 bpp", procName, NULL); |
| 1390 | if (nlevels < 2 || nlevels > 4) |
| 1391 | return (PIX *)ERROR_PTR("nlevels not in {2, 3, 4}", procName, NULL); |
| 1392 | |
| 1393 | if ((pixd = pixCreate(w, h, 2)) == NULL) |
| 1394 | return (PIX *)ERROR_PTR("pixd not made", procName, NULL); |
| 1395 | pixCopyResolution(pixd, pixs); |
| 1396 | pixCopyInputFormat(pixd, pixs); |
| 1397 | datad = pixGetData(pixd); |
| 1398 | wpld = pixGetWpl(pixd); |
| 1399 | |
| 1400 | if (cmapflag) { /* hold out (4 - nlevels) cmap entries */ |
| 1401 | cmap = pixcmapCreateLinear(2, nlevels); |
| 1402 | pixSetColormap(pixd, cmap); |
| 1403 | } |
| 1404 | |
| 1405 | /* If there is a colormap in the src, remove it */ |
| 1406 | pixt = pixRemoveColormap(pixs, REMOVE_CMAP_TO_GRAYSCALE); |
| 1407 | datat = pixGetData(pixt); |
| 1408 | wplt = pixGetWpl(pixt); |
| 1409 | |
| 1410 | /* Make the appropriate table */ |
| 1411 | if (cmapflag) |
| 1412 | qtab = makeGrayQuantIndexTable(nlevels); |
| 1413 | else |
| 1414 | qtab = makeGrayQuantTargetTable(4, 2); |
| 1415 | |
| 1416 | thresholdTo2bppLow(datad, h, wpld, datat, wplt, qtab); |
| 1417 | |
| 1418 | LEPT_FREE(qtab); |
| 1419 | pixDestroy(&pixt); |
| 1420 | return pixd; |
| 1421 | } |
| 1422 | |
| 1423 | |
| 1424 | /*! |
no test coverage detected