Credit Andrew - improve in-tree documentation
[goodguy/cinelerra.git] / cinelerra / picture.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2011 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include <ctype.h>
23 #include "bchash.h"
24 #include "mwindow.h"
25 #include "picture.h"
26 #include <string.h>
27
28
29
30
31
32 PictureItem::PictureItem()
33 {
34         name[0] = 0;
35         min = 0;
36         max = 0;
37         default_ = 0;
38         step = 0;
39         type = 0;
40         value = 0;
41         device_id = 0;
42 }
43
44
45 PictureItem::~PictureItem()
46 {
47 }
48
49 void PictureItem::copy_from(PictureItem *src)
50 {
51         strcpy(this->name, src->name);
52         this->device_id = src->device_id;
53         this->min = src->min;
54         this->max = src->max;
55         this->default_ = src->default_;
56         this->step = src->step;
57         this->type = src->type;
58         this->value = src->value;
59 }
60
61 char* PictureItem::get_default_string(char *string)
62 {
63         sprintf(string, "VIDEO_%s_VALUE", name);
64         int len = strlen(string);
65         for(int i = 0; i < len; i++)
66                 string[i] = toupper(string[i]);
67         return string;
68 }
69
70
71
72
73
74
75
76
77 PictureConfig::PictureConfig()
78 {
79         char path[BCTEXTLEN];
80         MWindow::create_defaults_path(path, PICTURE_FILE);
81         defaults = new BC_Hash(path);
82
83
84         brightness = -1;
85         hue = -1;
86         color = -1;
87         contrast = -1;
88         whiteness = -1;
89
90         use_brightness = 0;
91         use_contrast = 0;
92         use_color = 0;
93         use_hue = 0;
94         use_whiteness = 0;
95 }
96
97 PictureConfig::~PictureConfig()
98 {
99         controls.remove_all_objects();
100         delete defaults;
101 }
102
103 void PictureConfig::copy_settings(PictureConfig *picture)
104 {
105         this->brightness = picture->brightness;
106         this->hue = picture->hue;
107         this->color = picture->color;
108         this->contrast = picture->contrast;
109         this->whiteness = picture->whiteness;
110
111
112         for(int i = 0; i < picture->controls.total; i++)
113         {
114                 PictureItem *src_item = picture->controls.values[i];
115                 PictureItem *dst_item = 0;
116                 for(int j = 0; j < controls.total; j++)
117                 {
118                         if(controls.values[j]->device_id == src_item->device_id)
119                         {
120                                 dst_item = controls.values[j];
121                                 break;
122                         }
123                 }
124                 if(!dst_item)
125                 {
126                         dst_item = new PictureItem;
127                         controls.append(dst_item);
128                 }
129                 dst_item->copy_from(src_item);
130         }
131 }
132
133 void PictureConfig::copy_usage(PictureConfig *picture)
134 {
135         this->use_brightness = picture->use_brightness;
136         this->use_contrast = picture->use_contrast;
137         this->use_color = picture->use_color;
138         this->use_hue = picture->use_hue;
139         this->use_whiteness = picture->use_whiteness;
140
141 // Add the controls if they don't exists but don't replace existing values.
142         for(int i = 0; i < picture->controls.total; i++)
143         {
144                 PictureItem *src = picture->controls.values[i];
145                 int got_it = 0;
146                 for(int j = 0; j < controls.total; j++)
147                 {
148                         if(controls.values[j]->device_id == src->device_id)
149                         {
150                                 got_it = 1;
151                                 break;
152                         }
153                 }
154                 if(!got_it)
155                 {
156                         PictureItem *dst = new PictureItem;
157                         controls.append(dst);
158                         dst->copy_from(src);
159                 }
160         }
161 }
162
163 void PictureConfig::load_defaults()
164 {
165         if(!defaults)
166         {
167                 printf("PictureConfig::load_defaults: no defaults pointer.\n");
168                 return;
169         }
170
171
172         defaults->load();
173         brightness = defaults->get("VIDEO_BRIGHTNESS", 0);
174         hue = defaults->get("VIDEO_HUE", 0);
175         color = defaults->get("VIDEO_COLOR", 0);
176         contrast = defaults->get("VIDEO_CONTRAST", 0);
177         whiteness = defaults->get("VIDEO_WHITENESS", 0);
178
179 // The device must be probed first to keep unsupported controls from getting
180 // displayed.
181         for(int i = 0; i < controls.total; i++)
182         {
183                 PictureItem *item = controls.values[i];
184                 char string[BCTEXTLEN];
185                 item->get_default_string(string);
186                 item->value = defaults->get(string, item->value);
187 //printf("PictureConfig::load_defaults %s %d %d\n", item->name, item->device_id, item->value);
188         }
189 }
190
191 void PictureConfig::save_defaults()
192 {
193         if(!defaults)
194         {
195                 printf("PictureConfig::save_defaults: no defaults pointer.\n");
196                 return;
197         }
198         defaults->update("VIDEO_BRIGHTNESS", brightness);
199         defaults->update("VIDEO_HUE", hue);
200         defaults->update("VIDEO_COLOR", color);
201         defaults->update("VIDEO_CONTRAST", contrast);
202         defaults->update("VIDEO_WHITENESS", whiteness);
203         for(int i = 0; i < controls.total; i++)
204         {
205                 PictureItem *item = controls.values[i];
206                 char string[BCTEXTLEN];
207                 item->get_default_string(string);
208                 defaults->update(string, item->value);
209 //printf("PictureConfig::save_defaults %s %d %d\n", string, item->device_id, item->value);
210         }
211         defaults->save();
212 }
213
214 void PictureConfig::dump()
215 {
216         printf("    VIDEO_BRIGHTNESS=%d\n", brightness);
217         printf("    VIDEO_HUE=%d\n", hue);
218         printf("    VIDEO_COLOR=%d\n", color);
219         printf("    VIDEO_CONTRAST=%d\n", contrast);
220         printf("    VIDEO_WHITENESS=%d\n", whiteness);
221         for(int i = 0; i < controls.total; i++)
222         {
223                 PictureItem *item = controls.values[i];
224                 char string[BCTEXTLEN];
225                 item->get_default_string(string);
226                 printf("    %s=%d\n", string, item->value);
227         }
228 }
229
230 PictureItem* PictureConfig::new_item(const char *name)
231 {
232         for(int i = 0; i < controls.total; i++)
233         {
234                 if(!strcmp(controls.values[i]->name, name)) return controls.values[i];
235         }
236         PictureItem *item = new PictureItem;
237         strcpy(item->name, name);
238         controls.append(item);
239         return item;
240 }
241
242 PictureItem* PictureConfig::get_item(const char *name, int id)
243 {
244         for(int i = 0; i < controls.total; i++)
245         {
246                 if(!strcmp(controls.values[i]->name, name) &&
247                         controls.values[i]->device_id == id) return controls.values[i];
248         }
249         return 0;
250 }
251
252 int PictureConfig::set_item(int device_id, int value)
253 {
254         for(int i = 0; i < controls.total; i++)
255         {
256                 if(controls.values[i]->device_id == device_id)
257                 {
258                         if(controls.values[i]->value != value)
259                         {
260                                 controls.values[i]->value = value;
261                                 return 1;
262                         }
263                         return 0;
264                 }
265         }
266
267         return 0;
268 }
269
270
271
272
273
274
275
276