----------------------------------------------------------------------* * Simple (pixelwise) thresholding on 8 bpp with optional colormap * *----------------------------------------------------------------------*/ ! * \brief pixThresholdOn8bpp() * * \param[in] pixs 8 bpp, can have colormap * \param[in] nlevels equally spaced; must be between 2 and 256 * \param[in] cmapflag 1
| 1629 | * </pre> |
| 1630 | */ |
| 1631 | PIX * |
| 1632 | pixThresholdOn8bpp(PIX *pixs, |
| 1633 | l_int32 nlevels, |
| 1634 | l_int32 cmapflag) |
| 1635 | { |
| 1636 | l_int32 *qtab; /* quantization table */ |
| 1637 | l_int32 i, j, w, h, wpld, val, newval; |
| 1638 | l_uint32 *datad, *lined; |
| 1639 | PIX *pixd; |
| 1640 | PIXCMAP *cmap; |
| 1641 | |
| 1642 | PROCNAME("pixThresholdOn8bpp"); |
| 1643 | |
| 1644 | if (!pixs) |
| 1645 | return (PIX *)ERROR_PTR("pixs not defined", procName, NULL); |
| 1646 | if (pixGetDepth(pixs) != 8) |
| 1647 | return (PIX *)ERROR_PTR("pixs not 8 bpp", procName, NULL); |
| 1648 | if (nlevels < 2 || nlevels > 256) |
| 1649 | return (PIX *)ERROR_PTR("nlevels not in [2,...,256]", procName, NULL); |
| 1650 | |
| 1651 | /* Get a new pixd; if there is a colormap in the src, remove it */ |
| 1652 | if (pixGetColormap(pixs)) |
| 1653 | pixd = pixRemoveColormap(pixs, REMOVE_CMAP_TO_GRAYSCALE); |
| 1654 | else |
| 1655 | pixd = pixCopy(NULL, pixs); |
| 1656 | |
| 1657 | if (cmapflag) { /* hold out (256 - nlevels) cmap entries */ |
| 1658 | cmap = pixcmapCreateLinear(8, nlevels); |
| 1659 | pixSetColormap(pixd, cmap); |
| 1660 | } |
| 1661 | |
| 1662 | if (cmapflag) |
| 1663 | qtab = makeGrayQuantIndexTable(nlevels); |
| 1664 | else |
| 1665 | qtab = makeGrayQuantTargetTable(nlevels, 8); |
| 1666 | |
| 1667 | pixGetDimensions(pixd, &w, &h, NULL); |
| 1668 | pixCopyResolution(pixd, pixs); |
| 1669 | pixCopyInputFormat(pixd, pixs); |
| 1670 | datad = pixGetData(pixd); |
| 1671 | wpld = pixGetWpl(pixd); |
| 1672 | for (i = 0; i < h; i++) { |
| 1673 | lined = datad + i * wpld; |
| 1674 | for (j = 0; j < w; j++) { |
| 1675 | val = GET_DATA_BYTE(lined, j); |
| 1676 | newval = qtab[val]; |
| 1677 | SET_DATA_BYTE(lined, j, newval); |
| 1678 | } |
| 1679 | } |
| 1680 | |
| 1681 | LEPT_FREE(qtab); |
| 1682 | return pixd; |
| 1683 | } |
| 1684 | |
| 1685 | |
| 1686 | /*----------------------------------------------------------------------* |
no test coverage detected