src/sel1.c
static SEL *
selCreateFromSArray(SARRAY  *sa,
		    l_int32  first,
		    l_int32  last)
{
char     ch;
char    *name, *line;
l_int32  n, len, i, w, h, y, x;
SEL     *sel;

    PROCNAME("selCreateFromSArray");

    if (!sa)
        return (SEL *)ERROR_PTR("sa not defined", procName, NULL);
    n = sarrayGetCount(sa);
    if (first < 0 || first >= n || last <= first || last >= n)
        return (SEL *)ERROR_PTR("invalid range", procName, NULL);
    
    name = sarrayGetString(sa, first, L_NOCOPY);
    h = last - first;
    line = sarrayGetString(sa, first + 1, L_NOCOPY);
    len = strlen(line);
    if (line[0] != '"' || line[len - 1] != '"')
        return (SEL *)ERROR_PTR("invalid format", procName, NULL);
    w = len - 2;
    if ((sel = selCreate(h, w, name)) == NULL)
        return (SEL *)ERROR_PTR("sel not made", procName, NULL);
    for (i = first + 1; i <= last; i++) {
        line = sarrayGetString(sa, i, L_NOCOPY);
        y = i - first - 1;
        for (x = 0; x < w; ++x) {
            ch = line[x + 1];  /* skip the leading double-quote */
            switch (ch)
            {
                case 'X':
                    selSetOrigin(sel, y, x);
                case 'x':
                    selSetElement(sel, y, x, SEL_HIT);
                    break;

                case 'O':
                    selSetOrigin(sel, y, x);
                case 'o':
                    selSetElement(sel, y, x, SEL_MISS);
                    break;

                case 'C':
                    selSetOrigin(sel, y, x);
                case ' ':
                    selSetElement(sel, y, x, SEL_DONT_CARE);
                    break;
                default:
                    selDestroy(&sel);
                    return (SEL *)ERROR_PTR("unknown char", procName, NULL);
            }
        }
    }

    return sel;
}
/*!
 *  selCreateFromSArray()
 *
 *      Input:  sa
 *              first (line of sarray where Sel begins)
 *              last (line of sarray where Sel ends)
 *      Return: sela, or null on error
 *
 *  Notes:
 *      (1) The Sel contains the following lines:
 *          - The first line is the selname
 *          - The remaining lines contain the Sel data.  They must
 *            be formatted similarly to the string format in
 *            selCreateFromString(), with each line beginning and
 *            ending with a double-quote, and showing the 2D layout.
 *          - 'last' gives the last line in the Sel data.
 *      (2) See selCreateFromString() for a description of the string
 *          format for the Sel data.  As an example, here are the lines
 *          of is a valid file for a single Sel.  In the file, all lines 
 *          are left-justified:
 *                    # diagonal sel
 *                    sel_5diag
 *                    "x    "
 *                    " x   "
 *                    "  X  "
 *                    "   x "
 *                    "    x"
 */
selCreateFromSArray()
Input:  sa
first (line of sarray where Sel begins)
last (line of sarray where Sel ends)
Return: sela, or null on error

(1) The Sel contains the following lines:
- The first line is the selname
- The remaining lines contain the Sel data.  They must
be formatted similarly to the string format in
selCreateFromString(), with each line beginning and
ending with a double-quote, and showing the 2D layout.
- 'last' gives the last line in the Sel data.
(2) See selCreateFromString() for a description of the string
format for the Sel data.  As an example, here are the lines
of is a valid file for a single Sel.
selCreateFromSArray () Input : sa first ( line of sarray where Sel begins ) last ( line of sarray where Sel ends ) Return : sela or null on error ( 1 ) The Sel contains the following lines : - The first line is the selname - The remaining lines contain the Sel data . They must be formatted similarly to the string format in selCreateFromString () with each line beginning and ending with a double - quote and showing the 2D layout . - ' last ' gives the last line in the Sel data . ( 2 ) See selCreateFromString () for a description of the string format for the Sel data . As an example here are the lines of is a valid file for a single Sel .
static SEL *
selCreateFromSArray(SARRAY  *sa,
		    l_int32  first,
		    l_int32  last)
{
char     ch;
char    *name, *line;
l_int32  n, len, i, w, h, y, x;
SEL     *sel;
    PROCNAME("selCreateFromSArray");
    if (!sa)
        return (SEL *)ERROR_PTR("sa not defined", procName, NULL);
    n = sarrayGetCount(sa);
    if (first < 0 || first >= n || last <= first || last >= n)
        return (SEL *)ERROR_PTR("invalid range", procName, NULL);
    name = sarrayGetString(sa, first, L_NOCOPY);
    h = last - first;
    line = sarrayGetString(sa, first + 1, L_NOCOPY);
    len = strlen(line);
    if (line[0] != '"' || line[len - 1] != '"')
        return (SEL *)ERROR_PTR("invalid format", procName, NULL);
    w = len - 2;
    if ((sel = selCreate(h, w, name)) == NULL)
        return (SEL *)ERROR_PTR("sel not made", procName, NULL);
    for (i = first + 1; i <= last; i++) {
        line = sarrayGetString(sa, i, L_NOCOPY);
        y = i - first - 1;
        for (x = 0; x < w; ++x) {
            ch = line[x + 1];  
            switch (ch)
            {
                case 'X':
                    selSetOrigin(sel, y, x);
                case 'x':
                    selSetElement(sel, y, x, SEL_HIT);
                    break;
                case 'O':
                    selSetOrigin(sel, y, x);
                case 'o':
                    selSetElement(sel, y, x, SEL_MISS);
                    break;
                case 'C':
                    selSetOrigin(sel, y, x);
                case ' ':
                    selSetElement(sel, y, x, SEL_DONT_CARE);
                    break;
                default:
                    selDestroy(&sel);
                    return (SEL *)ERROR_PTR("unknown char", procName, NULL);
            }
        }
    }
    return sel;
}
static SEL * selCreateFromSArray ( SARRAY * sa , l_int32 first , l_int32 last ) { char ch ; char * name , * line ; l_int32 n , len , i , w , h , y , x ; SEL * sel ; PROCNAME ( " " ) ; if ( ! sa ) return ( SEL * ) ERROR_PTR ( " " , procName , NULL ) ; n = sarrayGetCount ( sa ) ; if ( first < 0 || first >= n || last <= first || last >= n ) return ( SEL * ) ERROR_PTR ( " " , procName , NULL ) ; name = sarrayGetString ( sa , first , L_NOCOPY ) ; h = last - first ; line = sarrayGetString ( sa , first + 1 , L_NOCOPY ) ; len = strlen ( line ) ; if ( line [ 0 ] != ' ' || line [ len - 1 ] != ' ' ) return ( SEL * ) ERROR_PTR ( " " , procName , NULL ) ; w = len - 2 ; if ( ( sel = selCreate ( h , w , name ) ) == NULL ) return ( SEL * ) ERROR_PTR ( " " , procName , NULL ) ; for ( i = first + 1 ; i <= last ; i ++ ) { line = sarrayGetString ( sa , i , L_NOCOPY ) ; y = i - first - 1 ; for ( x = 0 ; x < w ; ++ x ) { ch = line [ x + 1 ] ; switch ( ch ) { case ' ' : selSetOrigin ( sel , y , x ) ; case ' ' : selSetElement ( sel , y , x , SEL_HIT ) ; break ; case ' ' : selSetOrigin ( sel , y , x ) ; case ' ' : selSetElement ( sel , y , x , SEL_MISS ) ; break ; case ' ' : selSetOrigin ( sel , y , x ) ; case ' ' : selSetElement ( sel , y , x , SEL_DONT_CARE ) ; break ; default : selDestroy ( & sel ) ; return ( SEL * ) ERROR_PTR ( " " , procName , NULL ) ; } } } return sel ; }
selCreateFromSArray()
      Input:  sa
              first (line of sarray where Sel begins)
              last (line of sarray where Sel ends)
      Return: sela, or null on error
selCreateFromSArray () Input : sa first ( line of sarray where Sel begins ) last ( line of sarray where Sel ends ) Return : sela or null on error
/* skip the leading double-quote */