1 /*
2  *  This file is part of the XForms library package.
3  *
4  *  XForms is free software; you can redistribute it and/or modify it
5  *  under the terms of the GNU Lesser General Public License as
6  *  published by the Free Software Foundation; either version 2.1, or
7  *  (at your option) any later version.
8  *
9  *  XForms is distributed in the hope that it will be useful, but
10  *  WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *  Lesser General Public License for more details.
13  *
14  *  You should have received a copy of the GNU Lesser General Public License
15  *  along with XForms. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 
19 /*
20  *  This file is part of the XForms library package.
21  *  Copyright (c) 1998-2002  T.C. Zhao
22  *  All rights reserved.
23  *
24  */
25 
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 
30 #include "include/forms.h"
31 #include "flimage.h"
32 #include "flimage_int.h"
33 
34 
35 /***************************************
36  ***************************************/
37 
38 void *
fl_get_matrix(int nrows,int ncols,unsigned int esize)39 fl_get_matrix( int          nrows,
40                int          ncols,
41                unsigned int esize )
42 {
43     char **mat;
44     int i;
45 
46     if ( ! ( mat = fl_malloc( ( nrows + 1 ) * sizeof *mat ) ) )
47         return NULL;
48 
49     mat[ 0 ] = ( void * ) FL_GET_MATRIX;
50 
51     if ( ! ( mat[ 1 ] = fl_calloc( nrows * ncols, esize ) ) )
52     {
53         fl_free( mat );
54         return NULL;
55     }
56 
57     for ( i = 2; i <= nrows; i++ )
58         mat[ i ] = mat[ i - 1 ] + ncols * esize;
59 
60     return mat + 1;
61 }
62 
63 
64 /***************************************
65  * Given a piece of memory, make a matrix out of it
66  ***************************************/
67 
68 void *
fl_make_matrix(int nrows,int ncols,unsigned int esize,void * mem)69 fl_make_matrix( int            nrows,
70                 int            ncols,
71                 unsigned int   esize,
72                 void         * mem )
73 {
74     char **mat = fl_malloc( ( nrows + 1 ) * sizeof *mat );
75     int i;
76 
77     if ( ! mat )
78         return NULL;
79 
80     mat[ 0 ] = ( char * ) FL_MAKE_MATRIX;
81 
82     for ( mat[ 1 ] = mem, i = 2; i <= nrows; i++ )
83         mat[ i ] = mat[ i - 1 ] + ncols * esize;
84 
85     return mat + 1;
86 }
87 
88 
89 /***************************************
90  ***************************************/
91 
92 void
fl_free_matrix(void * p)93 fl_free_matrix( void *p )
94 {
95     char **matrix = p;
96 
97     if ( ! p )
98         return;
99 
100     if ( matrix[ -1 ] && matrix[ 0 ] )
101     {
102         if ( matrix[ -1 ] == ( char * ) FL_GET_MATRIX )
103             fl_free( matrix[ 0 ] );
104         fl_free( matrix - 1 );
105     }
106 }
107 
108 
109 /*
110  * Local variables:
111  * tab-width: 4
112  * indent-tabs-mode: nil
113  * End:
114  */
115