1 /*
2     Gri - A language for scientific graphics programming
3     Copyright (C) 2008 Daniel Kelley
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; version 3 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 //#define DEBUG_STORAGE 1		// Debug
21 
22 #include        <stdio.h>
23 #include	"gr.hh"
24 #include	"extern.hh"
25 #include	"image_ex.hh"
26 #include	"private.hh"
27 extern char     _grTempString[];
28 
29 /*
30  * Allocate storage.
31  */
32 #define CHARLEN	50
33 bool
create_arrays()34 create_arrays()
35 {
36 	GET_STORAGE(_dstack, double, _num_dstackMAX);
37 	GET_STORAGE(_errorMsg, char, LineLength);
38 	GET_STORAGE(_cmdLine, char, LineLength);
39 	GET_STORAGE(_cmdLineCOPY, char, LineLength);
40 	GET_STORAGE(_imageHist, double, 256);
41 	return true;
42 }
43 
44 bool
allocate_image_storage(int nx,int ny)45 allocate_image_storage(int nx, int ny)
46 {
47 #ifdef DEBUG_STORAGE
48 	printf("%s:%d ENTERING allocate_image_storage(%d, %d)\n",__FILE__,__LINE__,nx,ny);
49 #endif
50 	if (nx < 0 || ny < 0)
51 		return false;
52 	_image.ras_magic = RAS_MAGIC;
53 	_image.ras_width = nx;
54 	_image.ras_height = ny;
55 	_image.ras_depth = 8;
56 	_image.ras_length = _image.ras_width * _image.ras_height;
57 	_image.ras_type = RT_STANDARD;
58 	_image.ras_maptype = RMT_NONE;
59 	_image.ras_maplength = 0;
60 	if (_image.storage_exists)
61 		free(_image.image);
62 	GET_STORAGE(_image.image, unsigned char, _image.ras_length);
63 	if (!_image.image) OUT_OF_MEMORY;
64 	_image.storage_exists = true;
65 	_imageHist_exists = false;
66 #ifdef DEBUG_STORAGE
67 	printf("%s:%d allocate_image_storage() got storage for image %d wide and %d tall\n",__FILE__,__LINE__,_image.ras_width,_image.ras_height);
68 #endif
69 	return true;
70 }
71 
72 bool
allocate_imageMask_storage(int nx,int ny)73 allocate_imageMask_storage(int nx, int ny)
74 {
75 #ifdef DEBUG_STORAGE
76 	printf("%s:%d ENTERING allocate_imageMask_storage(%d, %d)\n",__FILE__,__LINE__,nx,ny);
77 #endif
78 	if (nx < 0 || ny < 0)
79 		return false;
80 	_imageMask.ras_magic = RAS_MAGIC;
81 	_imageMask.ras_width = nx;
82 	_imageMask.ras_height = ny;
83 	_imageMask.ras_depth = 8;
84 	_imageMask.ras_length = _imageMask.ras_width * _imageMask.ras_height;
85 	_imageMask.ras_type = RT_STANDARD;
86 	_imageMask.ras_maptype = RMT_NONE;
87 	_imageMask.ras_maplength = 0;
88 	if (_imageMask.storage_exists) {
89 #ifdef DEBUG_STORAGE
90 		printf("%s:%d allocate_imageMask_storage() freeing up storage\n",__FILE__,__LINE__);
91 #endif
92 		free(_imageMask.image);
93 	}
94 	GET_STORAGE(_imageMask.image, unsigned char, _imageMask.ras_length);
95 	if (!_imageMask.image) OUT_OF_MEMORY;
96 	_imageMask.storage_exists = true;
97 	for (unsigned int i = 0; i < _imageMask.ras_length; i++)
98 		*(_imageMask.image + i) = 0;
99 	_imageHist_exists = false;
100 #ifdef DEBUG_STORAGE
101 	printf("%s:%d allocate_imageMask_storage() got storage for imageMask %d wide and %d tall\n",__FILE__,__LINE__,_imageMask.ras_width,_imageMask.ras_height);
102 #endif
103 	return true;
104 }
105 
106 bool
allocate_grid_storage(int nx,int ny)107 allocate_grid_storage(int nx, int ny)
108 {
109 	if (nx < 0 || ny < 0)
110 		return false;
111 	_num_xmatrix_data = nx;
112 	_num_ymatrix_data = ny;
113 	_f_xy.set_size(_num_xmatrix_data, _num_ymatrix_data);
114 	_f_xy.set_value(0.0);
115 	_legit_xy.set_size(_num_xmatrix_data, _num_ymatrix_data);
116 	_legit_xy.set_value(true);
117 	_grid_exists = true;
118 	return true;
119 }
120 
121 bool
allocate_xmatrix_storage(int cols)122 allocate_xmatrix_storage(int cols)
123 {
124 	if (_xgrid_exists == true) {
125 #if defined(DEBUG_STORAGE)
126 		printf("allocate_xmatrix_storage(%d) deleting storage\n", cols);
127 #endif
128 		delete [] _xmatrix;
129 	}
130 	_num_xmatrix_data = cols;
131 	_xmatrix = new double [_num_xmatrix_data];
132 	if (!_xmatrix) OUT_OF_MEMORY;
133 #if defined(DEBUG_STORAGE)
134 	printf("allocate_xmatrix_storage(%d) allocating\n", cols);
135 #endif
136 	_xgrid_exists = true;
137 	return true;
138 }
139 
140 bool
allocate_ymatrix_storage(int rows)141 allocate_ymatrix_storage(int rows)
142 {
143 	if (_ygrid_exists == true) {
144 #if defined(DEBUG_STORAGE)
145 		printf("allocate_ymatrix_storage(%d) deleting\n", rows);
146 #endif
147 		delete [] _ymatrix;
148 	}
149 	_num_ymatrix_data = rows;
150 	_ymatrix = new double [_num_ymatrix_data];
151 	if (!_ymatrix) OUT_OF_MEMORY;
152 #if defined(DEBUG_STORAGE)
153 	printf("allocate_ymatrix_storage(%d) allocating\n", rows);
154 #endif
155 	_ygrid_exists = true;
156 	return true;
157 }
158