static void TestSetIterator(L_ASET *s, l_int32 print) { l_int32 count, npix, val; L_ASET_NODE *n; n = l_asetGetFirst(s); count = 0; fprintf(stderr, "\n --------------- Begin iter listing --------------\n"); while (n) { count++; if (print) #if 0 fprintf(stderr, "key = %x\n", n->key.utype); #else fprintf(stderr, "key = %llx\n", n->key.utype); #endif n = l_asetGetNext(n); } fprintf(stderr, "Count from iterator: %d\n", count); fprintf(stderr, " --------------- End iter listing --------------\n"); return; }
/*! * \brief pixGetSortedNeighborValues() * * \param[in] pixs 8, 16 or 32 bpp, with pixels labeled by c.c. * \param[in] x, y location of pixel * \param[in] conn 4 or 8 connected neighbors * \param[out] pneigh array of integers, to be filled with * the values of the neighbors, if any * \param[out] pnvals the number of unique neighbor values found * \return 0 if OK, 1 on error * * <pre> * Notes: * (1) The returned %neigh array is the unique set of neighboring * pixel values, of size nvals, sorted from smallest to largest. * The value 0, which represents background pixels that do * not belong to any set of connected components, is discarded. * (2) If there are no neighbors, this returns %neigh = NULL; otherwise, * the caller must free the array. * (3) For either 4 or 8 connectivity, the maximum number of unique * neighbor values is 4. * </pre> */ l_int32 pixGetSortedNeighborValues(PIX *pixs, l_int32 x, l_int32 y, l_int32 conn, l_int32 **pneigh, l_int32 *pnvals) { l_int32 i, npt, index; l_int32 neigh[4]; l_uint32 val; l_float32 fx, fy; L_ASET *aset; L_ASET_NODE *node; PTA *pta; RB_TYPE key; PROCNAME("pixGetSortedNeighborValues"); if (pneigh) *pneigh = NULL; if (pnvals) *pnvals = 0; if (!pneigh || !pnvals) return ERROR_INT("&neigh and &nvals not both defined", procName, 1); if (!pixs || pixGetDepth(pixs) < 8) return ERROR_INT("pixs not defined or depth < 8", procName, 1); /* Identify the locations of nearest neighbor pixels */ if ((pta = ptaGetNeighborPixLocs(pixs, x, y, conn)) == NULL) return ERROR_INT("pta of neighbors not made", procName, 1); /* Find the pixel values and insert into a set as keys */ aset = l_asetCreate(L_UINT_TYPE); npt = ptaGetCount(pta); for (i = 0; i < npt; i++) { ptaGetPt(pta, i, &fx, &fy); pixGetPixel(pixs, (l_int32)fx, (l_int32)fy, &val); key.utype = val; l_asetInsert(aset, key); } /* Extract the set keys and put them into the %neigh array. * Omit the value 0, which indicates the pixel doesn't * belong to one of the sets of connected components. */ node = l_asetGetFirst(aset); index = 0; while (node) { val = node->key.utype; if (val > 0) neigh[index++] = (l_int32)val; node = l_asetGetNext(node); } *pnvals = index; if (index > 0) { *pneigh = (l_int32 *)LEPT_CALLOC(index, sizeof(l_int32)); for (i = 0; i < index; i++) (*pneigh)[i] = neigh[i]; } ptaDestroy(&pta); l_asetDestroy(&aset); return 0; }