src/jbclass.c
static l_int32
findSimilarSizedTemplatesNext(JBFINDCTX  *state)
{
l_int32  desiredh, desiredw, size, templ;
PIX     *pixt;

    while(1) {  /* Continue the walk over step 'i' */
        if (state->i >= 25) {  /* all done */
            return -1;
        }

        desiredw = state->w + two_by_two_walk[2 * state->i];
        desiredh = state->h + two_by_two_walk[2 * state->i + 1];
        if (desiredh < 1 || desiredw < 1) {  /* invalid size */
            state->i++;
            continue;
        }

        if (!state->numa) {
                /* We have yet to start walking the array for the step 'i' */
            state->numa = numaHashGetNuma(state->classer->nahash,
                                          desiredh * desiredw);
            if (!state->numa) {  /* nothing there */
                state->i++;
                continue;
            }

            state->n = 0;  /* OK, we got a numa. */
        }

            /* Continue working on this numa */
        size = numaGetCount(state->numa);
        for ( ; state->n < size; ) {
            templ = (l_int32)(state->numa->array[state->n++] + 0.5);
            pixt = pixaGetPix(state->classer->pixat, templ, L_CLONE);
            if (pixGetWidth(pixt) - 2 * JB_ADDED_PIXELS == desiredw &&
                pixGetHeight(pixt) - 2 * JB_ADDED_PIXELS == desiredh) {
                pixDestroy(&pixt);
                return templ;
            }
            pixDestroy(&pixt);
        }

            /* Exhausted the numa; take another step and try again */
        state->i++;
        numaDestroy(&state->numa);
        continue;
    }
}
/*!
 *  findSimilarSizedTemplatesNext()
 *
 *      Input:  state (from findSimilarSizedTemplatesInit)
 *      Return: Next template number, or -1 when finished
 *
 *  We have a hash table mapping template area to a list of template
 *  numbers with that area.  We wish to find similar sized templates,
 *  so we first look for templates with the same width and height, and
 *  then with width + 1, etc.  This walk is guided by the
 *  two_by_two_walk array, above.
 *
 *  We don't want to have to collect the whole list of templates first because
 *  (we hope) to find it quickly.  So we keep the context for this walk in an
 *  explictit state structure and this function acts like a generator.
 */
findSimilarSizedTemplatesNext()
Input:  state (from findSimilarSizedTemplatesInit)
Return: Next template number, or -1 when finished

We have a hash table mapping template area to a list of template
numbers with that area.  We wish to find similar sized templates,
so we first look for templates with the same width and height, and
then with width + 1, etc.  This walk is guided by the
two_by_two_walk array, above.

We don't want to have to collect the whole list of templates first because
(we hope) to find it quickly.  So we keep the context for this walk in an
explictit state structure and this function acts like a generator.
findSimilarSizedTemplatesNext () Input : state ( from findSimilarSizedTemplatesInit ) Return : Next template number or - 1 when finished We have a hash table mapping template area to a list of template numbers with that area . We wish to find similar sized templates so we first look for templates with the same width and height and then with width + 1 etc . This walk is guided by the two_by_two_walk array above . We don ' t want to have to collect the whole list of templates first because ( we hope ) to find it quickly . So we keep the context for this walk in an explictit state structure and this function acts like a generator .
static l_int32
findSimilarSizedTemplatesNext(JBFINDCTX  *state)
{
l_int32  desiredh, desiredw, size, templ;
PIX     *pixt;
    while(1) {  
        if (state->i >= 25) {  
            return -1;
        }
        desiredw = state->w + two_by_two_walk[2 * state->i];
        desiredh = state->h + two_by_two_walk[2 * state->i + 1];
        if (desiredh < 1 || desiredw < 1) {  
            state->i++;
            continue;
        }
        if (!state->numa) {
            state->numa = numaHashGetNuma(state->classer->nahash,
                                          desiredh * desiredw);
            if (!state->numa) {  
                state->i++;
                continue;
            }
            state->n = 0;  
        }
        size = numaGetCount(state->numa);
        for ( ; state->n < size; ) {
            templ = (l_int32)(state->numa->array[state->n++] + 0.5);
            pixt = pixaGetPix(state->classer->pixat, templ, L_CLONE);
            if (pixGetWidth(pixt) - 2 * JB_ADDED_PIXELS == desiredw &&
                pixGetHeight(pixt) - 2 * JB_ADDED_PIXELS == desiredh) {
                pixDestroy(&pixt);
                return templ;
            }
            pixDestroy(&pixt);
        }
        state->i++;
        numaDestroy(&state->numa);
        continue;
    }
}
static l_int32 findSimilarSizedTemplatesNext ( JBFINDCTX * state ) { l_int32 desiredh , desiredw , size , templ ; PIX * pixt ; while ( 1 ) { if ( state -> i >= 25 ) { return -1 ; } desiredw = state -> w + two_by_two_walk [ 2 * state -> i ] ; desiredh = state -> h + two_by_two_walk [ 2 * state -> i + 1 ] ; if ( desiredh < 1 || desiredw < 1 ) { state -> i ++ ; continue ; } if ( ! state -> numa ) { state -> numa = numaHashGetNuma ( state -> classer -> nahash , desiredh * desiredw ) ; if ( ! state -> numa ) { state -> i ++ ; continue ; } state -> n = 0 ; } size = numaGetCount ( state -> numa ) ; for ( ; state -> n < size ; ) { templ = ( l_int32 ) ( state -> numa -> array [ state -> n ++ ] + 0.5 ) ; pixt = pixaGetPix ( state -> classer -> pixat , templ , L_CLONE ) ; if ( pixGetWidth ( pixt ) - 2 * JB_ADDED_PIXELS == desiredw && pixGetHeight ( pixt ) - 2 * JB_ADDED_PIXELS == desiredh ) { pixDestroy ( & pixt ) ; return templ ; } pixDestroy ( & pixt ) ; } state -> i ++ ; numaDestroy ( & state -> numa ) ; continue ; } }
findSimilarSizedTemplatesNext()
      Input:  state (from findSimilarSizedTemplatesInit)
      Return: Next template number, or -1 when finished
findSimilarSizedTemplatesNext () Input : state ( from findSimilarSizedTemplatesInit ) Return : Next template number or - 1 when finished
/* Continue the walk over step 'i' */ /* all done */ /* invalid size */ /* We have yet to start walking the array for the step 'i' */ /* nothing there */ /* OK, we got a numa. */ /* Continue working on this numa */ /* Exhausted the numa; take another step and try again */