43 #include <config_auto.h>
46 #include "allheaders.h"
49 static const l_int32 MAX_ALLOWED_ITERATIONS = 20;
52 static const l_float32 DIST_EXPAND_FACT = 1.3;
58 static const l_int32 LEVEL_IN_OCTCUBE = 4;
62 l_int32 maxdist, l_int32 maxcolors,
144 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
145 if (pixGetDepth(pixs) != 32)
146 return (
PIX *)ERROR_PTR(
"must be rgb color", __func__, NULL);
151 return (
PIX *)ERROR_PTR(
"pixd not made", __func__, NULL);
154 pixWriteDebug(
"/tmp/lept/segment/colorseg1.png", pixd, IFF_PNG);
158 countarray = (l_int32 *)LEPT_CALLOC(256,
sizeof(l_int32));
161 pixWriteDebug(
"/tmp/lept/segment/colorseg2.png", pixd, IFF_PNG);
165 LEPT_FREE(countarray);
167 pixWriteDebug(
"/tmp/lept/segment/colorseg3.png", pixd, IFF_PNG);
204 l_int32 w, h, newmaxdist, ret, niters, ncolors, success;
209 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
210 if (pixGetDepth(pixs) != 32)
211 return (
PIX *)ERROR_PTR(
"must be rgb color", __func__, NULL);
215 return (
PIX *)ERROR_PTR(
"pixd not made", __func__, NULL);
218 pixCopyResolution(pixd, pixs);
220 newmaxdist = maxdist;
225 maxcolors, debugflag);
230 L_INFO(
"Success with %d colors after %d iters\n", __func__,
234 if (niters == MAX_ALLOWED_ITERATIONS) {
235 L_WARNING(
"too many iters; newmaxdist = %d\n",
236 __func__, newmaxdist);
240 newmaxdist = (l_int32)(DIST_EXPAND_FACT * (l_float32)newmaxdist);
245 return (
PIX *)ERROR_PTR(
"failure in phase 1", __func__, NULL);
274 l_int32 rmap[256], gmap[256], bmap[256];
275 l_int32 w, h, wpls, wpld, i, j, k, found, ret, index, ncolors;
276 l_int32 rval, gval, bval, dist2, maxdist2;
277 l_int32 countarray[256];
278 l_int32 rsum[256], gsum[256], bsum[256];
280 l_uint32 *datas, *datad, *lines, *lined;
284 return ERROR_INT(
"pixs not defined", __func__, 1);
286 return ERROR_INT(
"pixd not defined", __func__, 1);
288 w = pixGetWidth(pixs);
289 h = pixGetHeight(pixs);
290 maxdist2 = maxdist * maxdist;
291 cmap = pixGetColormap(pixd);
293 for (k = 0; k < 256; k++) {
294 rsum[k] = gsum[k] = bsum[k] = 0;
295 rmap[k] = gmap[k] = bmap[k] = 0;
300 wpls = pixGetWpl(pixs);
301 wpld = pixGetWpl(pixd);
303 for (i = 0; i < h; i++) {
304 lines = datas + i * wpls;
305 lined = datad + i * wpld;
306 for (j = 0; j < w; j++) {
313 for (k = 0; k < ncolors; k++) {
314 dist2 = (rval - rmap[k]) * (rval - rmap[k]) +
315 (gval - gmap[k]) * (gval - gmap[k]) +
316 (bval - bmap[k]) * (bval - bmap[k]);
317 if (dist2 <= maxdist2) {
332 if (ret == 0 && index < maxcolors) {
333 countarray[index] = 1;
343 L_INFO(
"maxcolors exceeded for maxdist = %d\n",
353 for (k = 0; k < ncolors; k++) {
354 rval = rsum[k] / countarray[k];
355 gval = gsum[k] / countarray[k];
356 bval = bsum[k] / countarray[k];
413 l_int32 w, h, wpls, wpld, wplm, i, j, success;
414 l_int32 rval, gval, bval, index;
417 l_uint32 *rtab, *gtab, *btab;
419 l_uint32 *datas, *datad, *datam, *lines, *lined, *linem;
423 return ERROR_INT(
"pixd not defined", __func__, 1);
424 if ((cmap = pixGetColormap(pixd)) == NULL)
425 return ERROR_INT(
"cmap not found", __func__, 1);
427 return ERROR_INT(
"pixs not defined", __func__, 1);
428 if (pixGetDepth(pixs) != 32)
429 return ERROR_INT(
"pixs not 32 bpp", __func__, 1);
430 if (level < 1 || level > 6)
431 return ERROR_INT(
"level not in [1 ... 6]", __func__, 1);
437 if (!rtab || !gtab || !btab || !cmaptab) {
438 L_ERROR(
"failure to make a table\n", __func__);
446 wpls = pixGetWpl(pixs);
447 wpld = pixGetWpl(pixd);
450 wplm = pixGetWpl(pixm);
452 for (i = 0; i < h; i++) {
453 lines = datas + i * wpls;
454 lined = datad + i * wpld;
456 linem = datam + i * wplm;
457 for (j = 0; j < w; j++) {
470 index = cmaptab[octindex];
482 return (success) ? 0 : 1;
509 l_int32 i, ncolors, val;
516 return ERROR_INT(
"pixs not defined", __func__, 1);
517 if (pixGetDepth(pixs) != 8)
518 return ERROR_INT(
"pixs not 8 bpp", __func__, 1);
519 if ((cmap = pixGetColormap(pixs)) == NULL)
520 return ERROR_INT(
"cmap not found", __func__, 1);
522 return ERROR_INT(
"countarray not defined", __func__, 1);
529 for (i = 0; i < ncolors; i++)
534 return ERROR_INT(
"nasi not made", __func__, 1);
540 for (i = 0; i < ncolors; i++) {
544 pixXor(pixt2, pixt2, pixt1);
579 l_int32 i, ncolors, index, tempindex;
587 return ERROR_INT(
"pixd not defined", __func__, 1);
588 if (pixGetDepth(pixd) != 8)
589 return ERROR_INT(
"pixd not 8 bpp", __func__, 1);
590 if ((cmap = pixGetColormap(pixd)) == NULL)
591 return ERROR_INT(
"cmap not found", __func__, 1);
593 return ERROR_INT(
"pixs not defined", __func__, 1);
595 if (finalcolors >= ncolors)
607 return ERROR_INT(
"nasi not made", __func__, 1);
611 tab = (l_int32 *)LEPT_CALLOC(256,
sizeof(l_int32));
612 for (i = finalcolors; i < ncolors; i++) {
#define GET_DATA_BYTE(pdata, n)
#define SET_DATA_BYTE(pdata, n, val)
#define GET_DATA_BIT(pdata, n)
l_int32 pixcmapGetCount(const PIXCMAP *cmap)
pixcmapGetCount()
PIXCMAP * pixcmapCreate(l_int32 depth)
pixcmapCreate()
l_ok pixcmapAddNewColor(PIXCMAP *cmap, l_int32 rval, l_int32 gval, l_int32 bval, l_int32 *pindex)
pixcmapAddNewColor()
l_ok pixcmapGetColor32(PIXCMAP *cmap, l_int32 index, l_uint32 *pval32)
pixcmapGetColor32()
l_ok pixcmapResetColor(PIXCMAP *cmap, l_int32 index, l_int32 rval, l_int32 gval, l_int32 bval)
pixcmapResetColor()
l_ok pixcmapClear(PIXCMAP *cmap)
pixcmapClear()
l_ok makeRGBToIndexTables(l_int32 cqlevels, l_uint32 **prtab, l_uint32 **pgtab, l_uint32 **pbtab)
makeRGBToIndexTables()
l_int32 * pixcmapToOctcubeLUT(PIXCMAP *cmap, l_int32 level, l_int32 metric)
pixcmapToOctcubeLUT()
l_ok pixRemoveUnusedColors(PIX *pixs)
pixRemoveUnusedColors()
void getOctcubeIndexFromRGB(l_int32 rval, l_int32 gval, l_int32 bval, l_uint32 *rtab, l_uint32 *gtab, l_uint32 *btab, l_uint32 *pindex)
getOctcubeIndexFromRGB()
l_ok pixColorSegmentRemoveColors(PIX *pixd, PIX *pixs, l_int32 finalcolors)
pixColorSegmentRemoveColors()
PIX * pixColorSegmentCluster(PIX *pixs, l_int32 maxdist, l_int32 maxcolors, l_int32 debugflag)
pixColorSegmentCluster()
PIX * pixColorSegment(PIX *pixs, l_int32 maxdist, l_int32 maxcolors, l_int32 selsize, l_int32 finalcolors, l_int32 debugflag)
pixColorSegment()
static l_int32 pixColorSegmentTryCluster(PIX *pixd, PIX *pixs, l_int32 maxdist, l_int32 maxcolors, l_int32 debugflag)
pixColorSegmentTryCluster()
l_ok pixAssignToNearestColor(PIX *pixd, PIX *pixs, PIX *pixm, l_int32 level, l_int32 *countarray)
pixAssignToNearestColor()
l_ok pixColorSegmentClean(PIX *pixs, l_int32 selsize, l_int32 *countarray)
pixColorSegmentClean()
PIX * pixGenerateMaskByValue(PIX *pixs, l_int32 val, l_int32 usecmap)
pixGenerateMaskByValue()
PIX * pixCloseSafeCompBrick(PIX *pixd, PIX *pixs, l_int32 hsize, l_int32 vsize)
pixCloseSafeCompBrick()
l_ok numaAddNumber(NUMA *na, l_float32 val)
numaAddNumber()
NUMA * numaCreate(l_int32 n)
numaCreate()
void numaDestroy(NUMA **pna)
numaDestroy()
l_ok numaGetIValue(NUMA *na, l_int32 index, l_int32 *pival)
numaGetIValue()
NUMA * numaGetSortIndex(NUMA *na, l_int32 sortorder)
numaGetSortIndex()
l_uint32 * pixGetData(PIX *pix)
pixGetData()
l_ok pixSetColormap(PIX *pix, PIXCMAP *colormap)
pixSetColormap()
void pixDestroy(PIX **ppix)
pixDestroy()
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
PIX * pixCreate(l_int32 width, l_int32 height, l_int32 depth)
pixCreate()
l_ok pixSetMasked(PIX *pixd, PIX *pixm, l_uint32 val)
pixSetMasked()
PIX * pixXor(PIX *pixd, PIX *pixs1, PIX *pixs2)
pixXor()
PIX * pixMakeMaskFromLUT(PIX *pixs, l_int32 *tab)
pixMakeMaskFromLUT()
NUMA * pixGetCmapHistogram(PIX *pixs, l_int32 factor)
pixGetCmapHistogram()
l_int32 lept_mkdir(const char *subdir)
lept_mkdir()