src/morphseq.c
l_int32
morphSequenceVerify(SARRAY  *sa)
{
char    *rawop, *op;
l_int32  nops, i, j, nred, fact, valid, w, h, netred, border;
l_int32  level[4];
l_int32  intlogbase2[5] = {1, 2, 3, 0, 4};  /* of arg/4 */

    PROCNAME("morphSequenceVerify");

    if (!sa)
        return ERROR_INT("sa not defined", procName, FALSE);

    nops = sarrayGetCount(sa);
    valid = TRUE;
    netred = 0;
    border = 0;
    for (i = 0; i < nops; i++) {
        rawop = sarrayGetString(sa, i, 0);
        op = stringRemoveChars(rawop, " \n\t");
        switch (op[0])
        {
        case 'd':
        case 'D':
        case 'e':
        case 'E':
        case 'o':
        case 'O':
        case 'c':
        case 'C':
            if (sscanf(&op[1], "%d.%d", &w, &h) != 2) {
                fprintf(stderr, "*** op: %s invalid\n", op);
                valid = FALSE;
                break;
            }
            if (w <= 0 || h <= 0) {
                fprintf(stderr,
                        "*** op: %s; w = %d, h = %d; must both be > 0\n",
                        op, w, h);
                valid = FALSE;
                break;
            }
/*            fprintf(stderr, "op = %s; w = %d, h = %d\n", op, w, h); */
            break;
        case 'r':
        case 'R':
            nred = strlen(op) - 1;
            netred += nred;
            if (nred < 1 || nred > 4) {
                fprintf(stderr,
                        "*** op = %s; num reduct = %d; must be in {1,2,3,4}\n",
                        op, nred);
                valid = FALSE;
                break;
            }
            for (j = 0; j < nred; j++) {
                level[j] = op[j + 1] - '0';
                if (level[j] < 1 || level[j] > 4) {
                    fprintf(stderr, "*** op = %s; level[%d] = %d is invalid\n",
                            op, j, level[j]);
                    valid = FALSE;
                    break;
                }
            }
            if (!valid)
                break;
/*            fprintf(stderr, "op = %s", op); */
            for (j = 0; j < nred; j++) {
                level[j] = op[j + 1] - '0';
/*                fprintf(stderr, ", level[%d] = %d", j, level[j]); */
            }
/*            fprintf(stderr, "\n"); */
            break;
        case 'x':
        case 'X':
            if (sscanf(&op[1], "%d", &fact) != 1) {
                fprintf(stderr, "*** op: %s; fact invalid\n", op);
                valid = FALSE;
                break;
            }
            if (fact != 2 && fact != 4 && fact != 8 && fact != 16) {
                fprintf(stderr, "*** op = %s; invalid fact = %d\n", op, fact);
                valid = FALSE;
                break;
            }
            netred -= intlogbase2[fact / 4];
/*            fprintf(stderr, "op = %s; fact = %d\n", op, fact); */
            break;
        case 'b':
        case 'B':
            if (sscanf(&op[1], "%d", &fact) != 1) {
                fprintf(stderr, "*** op: %s; fact invalid\n", op);
                valid = FALSE;
                break;
            }
            if (i > 0) {
                fprintf(stderr, "*** op = %s; must be first op\n", op);
                valid = FALSE;
                break;
            }
            if (fact < 1) {
                fprintf(stderr, "*** op = %s; invalid fact = %d\n", op, fact);
                valid = FALSE;
                break;
            }
            border = fact;
/*            fprintf(stderr, "op = %s; fact = %d\n", op, fact); */
            break;
        default:
            fprintf(stderr, "*** nonexistent op = %s\n", op);
            valid = FALSE;
        }
        FREE(op);
    }

    if (border != 0 && netred != 0) {
        fprintf(stderr,
                "*** op = %s; border added but net reduction not 0\n", op);
        valid = FALSE;
    }
    return valid;
}
/*!
 *  morphSequenceVerify()
 *
 *      Input:  sarray (of operation sequence)
 *      Return: TRUE if valid; FALSE otherwise or on error
 *
 *  Notes:
 *      (1) This does verification of valid binary morphological
 *          operation sequences.
 *      (2) See pixMorphSequence() for notes on valid operations
 *          in the sequence.
 */
morphSequenceVerify()
Input:  sarray (of operation sequence)
Return: TRUE if valid; FALSE otherwise or on error

(1) This does verification of valid binary morphological
operation sequences.
(2) See pixMorphSequence() for notes on valid operations
in the sequence.
morphSequenceVerify () Input : sarray ( of operation sequence ) Return : TRUE if valid ; FALSE otherwise or on error ( 1 ) This does verification of valid binary morphological operation sequences . ( 2 ) See pixMorphSequence () for notes on valid operations in the sequence .
l_int32
morphSequenceVerify(SARRAY  *sa)
{
char    *rawop, *op;
l_int32  nops, i, j, nred, fact, valid, w, h, netred, border;
l_int32  level[4];
l_int32  intlogbase2[5] = {1, 2, 3, 0, 4};  
    PROCNAME("morphSequenceVerify");
    if (!sa)
        return ERROR_INT("sa not defined", procName, FALSE);
    nops = sarrayGetCount(sa);
    valid = TRUE;
    netred = 0;
    border = 0;
    for (i = 0; i < nops; i++) {
        rawop = sarrayGetString(sa, i, 0);
        op = stringRemoveChars(rawop, " \n\t");
        switch (op[0])
        {
        case 'd':
        case 'D':
        case 'e':
        case 'E':
        case 'o':
        case 'O':
        case 'c':
        case 'C':
            if (sscanf(&op[1], "%d.%d", &w, &h) != 2) {
                fprintf(stderr, "*** op: %s invalid\n", op);
                valid = FALSE;
                break;
            }
            if (w <= 0 || h <= 0) {
                fprintf(stderr,
                        "*** op: %s; w = %d, h = %d; must both be > 0\n",
                        op, w, h);
                valid = FALSE;
                break;
            }
            break;
        case 'r':
        case 'R':
            nred = strlen(op) - 1;
            netred += nred;
            if (nred < 1 || nred > 4) {
                fprintf(stderr,
                        "*** op = %s; num reduct = %d; must be in {1,2,3,4}\n",
                        op, nred);
                valid = FALSE;
                break;
            }
            for (j = 0; j < nred; j++) {
                level[j] = op[j + 1] - '0';
                if (level[j] < 1 || level[j] > 4) {
                    fprintf(stderr, "*** op = %s; level[%d] = %d is invalid\n",
                            op, j, level[j]);
                    valid = FALSE;
                    break;
                }
            }
            if (!valid)
                break;
            for (j = 0; j < nred; j++) {
                level[j] = op[j + 1] - '0';
            }
            break;
        case 'x':
        case 'X':
            if (sscanf(&op[1], "%d", &fact) != 1) {
                fprintf(stderr, "*** op: %s; fact invalid\n", op);
                valid = FALSE;
                break;
            }
            if (fact != 2 && fact != 4 && fact != 8 && fact != 16) {
                fprintf(stderr, "*** op = %s; invalid fact = %d\n", op, fact);
                valid = FALSE;
                break;
            }
            netred -= intlogbase2[fact / 4];
            break;
        case 'b':
        case 'B':
            if (sscanf(&op[1], "%d", &fact) != 1) {
                fprintf(stderr, "*** op: %s; fact invalid\n", op);
                valid = FALSE;
                break;
            }
            if (i > 0) {
                fprintf(stderr, "*** op = %s; must be first op\n", op);
                valid = FALSE;
                break;
            }
            if (fact < 1) {
                fprintf(stderr, "*** op = %s; invalid fact = %d\n", op, fact);
                valid = FALSE;
                break;
            }
            border = fact;
            break;
        default:
            fprintf(stderr, "*** nonexistent op = %s\n", op);
            valid = FALSE;
        }
        FREE(op);
    }
    if (border != 0 && netred != 0) {
        fprintf(stderr,
                "*** op = %s; border added but net reduction not 0\n", op);
        valid = FALSE;
    }
    return valid;
}
l_int32 morphSequenceVerify ( SARRAY * sa ) { char * rawop , * op ; l_int32 nops , i , j , nred , fact , valid , w , h , netred , border ; l_int32 level [ 4 ] ; l_int32 intlogbase2 [ 5 ] = { 1 , 2 , 3 , 0 , 4 } ; PROCNAME ( " " ) ; if ( ! sa ) return ERROR_INT ( " " , procName , FALSE ) ; nops = sarrayGetCount ( sa ) ; valid = TRUE ; netred = 0 ; border = 0 ; for ( i = 0 ; i < nops ; i ++ ) { rawop = sarrayGetString ( sa , i , 0 ) ; op = stringRemoveChars ( rawop , " \n \t " ) ; switch ( op [ 0 ] ) { case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : if ( sscanf ( & op [ 1 ] , " " , & w , & h ) != 2 ) { fprintf ( stderr , " \n " , op ) ; valid = FALSE ; break ; } if ( w <= 0 || h <= 0 ) { fprintf ( stderr , " \n " , op , w , h ) ; valid = FALSE ; break ; } break ; case ' ' : case ' ' : nred = strlen ( op ) - 1 ; netred += nred ; if ( nred < 1 || nred > 4 ) { fprintf ( stderr , " \n " , op , nred ) ; valid = FALSE ; break ; } for ( j = 0 ; j < nred ; j ++ ) { level [ j ] = op [ j + 1 ] - ' ' ; if ( level [ j ] < 1 || level [ j ] > 4 ) { fprintf ( stderr , " \n " , op , j , level [ j ] ) ; valid = FALSE ; break ; } } if ( ! valid ) break ; for ( j = 0 ; j < nred ; j ++ ) { level [ j ] = op [ j + 1 ] - ' ' ; } break ; case ' ' : case ' ' : if ( sscanf ( & op [ 1 ] , " " , & fact ) != 1 ) { fprintf ( stderr , " \n " , op ) ; valid = FALSE ; break ; } if ( fact != 2 && fact != 4 && fact != 8 && fact != 16 ) { fprintf ( stderr , " \n " , op , fact ) ; valid = FALSE ; break ; } netred -= intlogbase2 [ fact / 4 ] ; break ; case ' ' : case ' ' : if ( sscanf ( & op [ 1 ] , " " , & fact ) != 1 ) { fprintf ( stderr , " \n " , op ) ; valid = FALSE ; break ; } if ( i > 0 ) { fprintf ( stderr , " \n " , op ) ; valid = FALSE ; break ; } if ( fact < 1 ) { fprintf ( stderr , " \n " , op , fact ) ; valid = FALSE ; break ; } border = fact ; break ; default : fprintf ( stderr , " \n " , op ) ; valid = FALSE ; } FREE ( op ) ; } if ( border != 0 && netred != 0 ) { fprintf ( stderr , " \n " , op ) ; valid = FALSE ; } return valid ; }
morphSequenceVerify()
      Input:  sarray (of operation sequence)
      Return: TRUE if valid; FALSE otherwise or on error
morphSequenceVerify () Input : sarray ( of operation sequence ) Return : TRUE if valid ; FALSE otherwise or on error
/* of arg/4 */ /*            fprintf(stderr, "op = %s; w = %d, h = %d\n", op, w, h); */ /*            fprintf(stderr, "op = %s", op); */ /*                fprintf(stderr, ", level[%d] = %d", j, level[j]); */ /*            fprintf(stderr, "\n"); */ /*            fprintf(stderr, "op = %s; fact = %d\n", op, fact); */ /*            fprintf(stderr, "op = %s; fact = %d\n", op, fact); */