4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
26 #include "mainsession.h"
28 #include "videowindow.h"
31 #define _(String) gettext(String)
32 #define gettext_noop(String) String
33 #define N_(String) gettext_noop (String)
35 Scale::Scale(MWindow *mwindow)
36 : BC_MenuItem(_("Resize..."))
38 this->mwindow = mwindow;
39 thread = new ScaleThread(mwindow);
47 int Scale::handle_event()
52 ScaleThread::ScaleThread(MWindow *mwindow)
55 this->mwindow = mwindow;
59 ScaleThread::~ScaleThread() {}
61 void ScaleThread::run()
63 if(already_running) return;
65 constrain_ratio = mwindow->defaults->get("SCALECONSTRAIN", 0);
66 scale_data = mwindow->defaults->get("SCALEDATA", 0);
67 auto_aspect = mwindow->defaults->get("AUTOASPECT", 0);
68 offsets[0] = offsets[1] = offsets[2] = offsets[3] = 0;
70 orig_dimension[0] = dimension[0] = mwindow->session->track_w;
71 orig_dimension[1] = dimension[1] = mwindow->session->track_h;
72 orig_dimension[2] = dimension[2] = mwindow->session->output_w;
73 orig_dimension[3] = dimension[3] = mwindow->session->output_h;
74 ratio[0] = ratio[1] = ratio[2] = ratio[3] = 1;
75 aspect_w = mwindow->session->aspect_w;
76 aspect_h = mwindow->session->aspect_h;
78 window = new ScaleWindow(this);
79 window->create_objects();
80 int result = window->run_window();
84 dummy_offsets[0] = dummy_offsets[1] = dummy_offsets[2] = dummy_offsets[3] = 0;
85 // Fake the offsets if data is scaled.
88 //mwindow->stop_playback(1);
89 // save the before undo
90 mwindow->undo->update_undo_edits(_("Resize"), 0);
91 mwindow->tracks->scale_video(dimension, scale_data ? dummy_offsets : offsets, scale_data);
92 mwindow->session->track_w = dimension[0];
93 mwindow->session->track_h = dimension[1];
94 mwindow->session->output_w = dimension[2];
95 mwindow->session->output_h = dimension[3];
96 mwindow->session->aspect_w = aspect_w;
97 mwindow->session->aspect_h = aspect_h;
98 mwindow->video_window->resize_window();
100 mwindow->undo->update_undo_edits();
101 mwindow->session->changes_made = 1;
102 mwindow->defaults->update("ASPECTW", aspect_w);
103 mwindow->defaults->update("ASPECTH", aspect_h);
104 mwindow->defaults->update("AUTOASPECT", auto_aspect);
108 mwindow->defaults->update("SCALECONSTRAIN", constrain_ratio);
109 mwindow->defaults->update("SCALEDATA", scale_data);
113 int ScaleThread::update_window(int offset_updated)
116 int i, result, modified_item, dimension_modified = 0, ratio_modified = 0;
118 for(i = 0, result = 0; i < 4 && !result; i++)
120 if(i == 2) pair_start = 2;
126 dimension_modified = 1;
139 if(dimension_modified)
140 ratio[modified_item] = (float)dimension[modified_item] / orig_dimension[modified_item];
142 if(ratio_modified && !constrain_ratio)
144 dimension[modified_item] = (int)(orig_dimension[modified_item] * ratio[modified_item]);
145 window->dimension[modified_item]->update((long)dimension[modified_item]);
148 for(i = pair_start; i < pair_start + 2 && constrain_ratio; i++)
150 if(dimension_modified ||
151 (i != modified_item && ratio_modified))
153 ratio[i] = ratio[modified_item];
154 window->ratio[i]->update(ratio[i]);
158 (i != modified_item && dimension_modified))
160 dimension[i] = (int)(orig_dimension[i] * ratio[modified_item]);
161 window->dimension[i]->update((long)dimension[i]);
166 // window->position1->draw();
167 // window->position2->draw();
168 //printf("%d\n", offsets[0]);
169 // if(!offset_updated)
171 // window->offsets[0]->update(offsets[0]);
172 // window->offsets[1]->update(offsets[1]);
173 // window->offsets[2]->update(offsets[2]);
174 // window->offsets[3]->update(offsets[3]);
177 update_aspect(window);
181 int ScaleThread::update_aspect(ScaleWindow *window)
186 mwindow->create_aspect_ratio(aspect_w, aspect_h, dimension[2], dimension[3]);
187 sprintf(string, "%.0f", aspect_w);
188 window->aspect_w->update(string);
189 sprintf(string, "%.0f", aspect_h);
190 window->aspect_h->update(string);
196 ScaleWindow::ScaleWindow(ScaleThread *thread)
197 : BC_Window(_(PROGRAM_NAME ": Scale"), xS(370), yS(260), 0, 0)
198 { this->thread = thread; }
200 ScaleWindow::~ScaleWindow()
204 void ScaleWindow::create_objects()
206 int xs5 = xS(5), xs10 = xS(10), xs20 = xS(20), xs30 = xS(30), xs200 = xS(200);
207 int ys5 = yS(5), ys10 = yS(10), ys20 = yS(20), ys30 = yS(30);
208 lock_window("ScaleWindow::create_objects");
209 int x = xs10, y = ys10;
210 int x0 = x, x1 = x + xS(70), x2 = x1 = xS(110), x3 = x = xS(70);
211 add_subwindow(new BC_Title(x, y, _("New camera size:")));
212 add_subwindow(new BC_Title(x + xs200, y, _("New projector size:")));
214 add_subwindow(new BC_Title(x0, y, _("Width:")));
215 add_subwindow(dimension[0] = new ScaleSizeText(x1, y, thread, &(thread->dimension[0])));
216 add_subwindow(new BC_Title(x2, y, _("Width:")));
217 add_subwindow(dimension[2] = new ScaleSizeText(x3, y, thread, &(thread->dimension[2])));
220 add_subwindow(new BC_Title(x0, y, _("Height:")));
221 add_subwindow(dimension[1] = new ScaleSizeText(x1, y, thread, &(thread->dimension[1])));
222 add_subwindow(new BC_Title(x2, y, _("Height:")));
223 add_subwindow(dimension[3] = new ScaleSizeText(x3, y, thread, &(thread->dimension[3])));
226 add_subwindow(new BC_Title(x0, y, _("W Ratio:")));
227 add_subwindow(ratio[0] = new ScaleRatioText(x1, y, thread, &(thread->ratio[0])));
228 add_subwindow(new BC_Title(x2, y, _("W Ratio:")));
229 add_subwindow(ratio[2] = new ScaleRatioText(x3, y, thread, &(thread->ratio[2])));
232 add_subwindow(new BC_Title(x0, y, _("H Ratio:")));
233 add_subwindow(ratio[1] = new ScaleRatioText(x1, y, thread, &(thread->ratio[1])));
234 add_subwindow(new BC_Title(x2, y, _("H Ratio:")));
235 add_subwindow(ratio[3] = new ScaleRatioText(x3, y, thread, &(thread->ratio[3])));
238 // add_subwindow(new BC_Title(x0, y, "X Offset:"));
239 // add_subwindow(offsets[0] = new ScaleOffsetText(x1, y, thread, &(thread->offsets[0])));
240 // add_subwindow(new BC_Title(x2, y, "X Offset:"));
241 // add_subwindow(offsets[2] = new ScaleOffsetText(x3, y, thread, &(thread->offsets[2])));
244 // add_subwindow(new BC_Title(x0, y, "Y Offset:"));
245 // add_subwindow(offsets[1] = new ScaleOffsetText(x1, y, thread, &(thread->offsets[1])));
246 // add_subwindow(new BC_Title(x2, y, "Y Offset:"));
247 // add_subwindow(offsets[3] = new ScaleOffsetText(x3, y, thread, &(thread->offsets[3])));
250 add_subwindow(new BC_Title(x, y, _("Aspect ratio:")));
253 sprintf(string, "%.0f", thread->aspect_w);
254 add_subwindow(aspect_w = new ScaleAspectW(x, y, thread, &(thread->aspect_w), string));
256 add_subwindow(new BC_Title(x, y, ":"));
258 sprintf(string, "%.0f", thread->aspect_h);
259 add_subwindow(aspect_h = new ScaleAspectH(x, y, thread, &(thread->aspect_h), string));
261 add_subwindow(new ScaleAspectAuto(x, y + 5, thread));
265 // add_subwindow(new BC_Title(x, y, _("Camera position:")));
267 // add_subwindow(new BC_Title(x, y, _("Projector position:")));
269 // ScalePosition *position;
272 // add_subwindow(position1 = new ScalePosition(x, y, thread, this,
273 // &(thread->orig_dimension[0]), &(thread->dimension[0]), &(thread->offsets[0])));
274 // position1->draw();
277 // add_subwindow(position2 = new ScalePosition(x, y, thread, this,
278 // &(thread->orig_dimension[2]), &(thread->dimension[2]), &(thread->offsets[2])));
279 // position2->draw();
283 add_subwindow(new ScaleConstrain(x, y, thread));
285 add_subwindow(new ScaleData(x, y, thread));
289 add_subwindow(new BC_OKButton(x, y));
291 add_subwindow(new BC_CancelButton(x, y));
295 ScaleSizeText::ScaleSizeText(int x, int y, ScaleThread *thread, int *output)
296 : BC_TextBox(x, y, xS(100), 1, *output)
298 this->thread = thread;
299 this->output = output;
301 ScaleSizeText::~ScaleSizeText() {}
302 int ScaleSizeText::handle_event()
304 *output = atol(get_text());
305 *output /= 2; *output *= 2;
306 if(*output <= 0) *output = 2;
307 if(*output > 10000) *output = 10000;
309 thread->update_window();
312 ScaleOffsetText::ScaleOffsetText(int x, int y, ScaleThread *thread, int *output)
313 : BC_TextBox(x, y, xs(100), 1, *output)
314 { this->thread = thread; this->output = output; }
315 ScaleOffsetText::~ScaleOffsetText() {}
316 int ScaleOffsetText::handle_event()
318 *output = atol(get_text());
319 //if(*output <= 0) *output = 0;
320 if(*output > 10000) *output = 10000;
321 if(*output < -10000) *output = -10000;
322 thread->update_window(1);
325 ScaleRatioText::ScaleRatioText(int x, int y, ScaleThread *thread, float *output)
326 : BC_TextBox(x, y, xS(100), 1, *output)
327 { this->thread = thread; this->output = output; }
328 ScaleRatioText::~ScaleRatioText() {}
329 int ScaleRatioText::handle_event()
331 *output = atof(get_text());
332 //if(*output <= 0) *output = 1;
333 if(*output > 10000) *output = 10000;
334 if(*output < -10000) *output = -10000;
336 thread->update_window();
342 ScaleConstrain::ScaleConstrain(int x, int y, ScaleThread *thread)
343 : BC_CheckBox(x, y, thread->constrain_ratio, _("Constrain ratio"))
344 { this->thread = thread; }
345 ScaleConstrain::~ScaleConstrain() {}
346 int ScaleConstrain::handle_event()
348 thread->constrain_ratio = get_value();
351 ScaleData::ScaleData(int x, int y, ScaleThread *thread)
352 : BC_CheckBox(x, y, thread->scale_data, _("Scale data"))
353 { this->thread = thread; }
354 ScaleData::~ScaleData() {}
355 int ScaleData::handle_event()
357 thread->scale_data = get_value();
358 thread->update_window();
362 ScaleAspectAuto::ScaleAspectAuto(int x, int y, ScaleThread *thread)
363 : BC_CheckBox(x, y, thread->auto_aspect, _("Auto"))
364 { this->thread = thread; }
366 ScaleAspectAuto::~ScaleAspectAuto()
370 int ScaleAspectAuto::handle_event()
372 thread->auto_aspect = get_value();
373 thread->update_aspect(thread->window);
379 ScaleAspectW::ScaleAspectW(int x, int y, ScaleThread *thread, float *output, char *string)
380 : BC_TextBox(x, y, xS(50), 1, string)
382 this->output = output;
383 this->thread = thread;
385 ScaleAspectW::~ScaleAspectW()
389 int ScaleAspectW::handle_event()
391 *output = atof(get_text());
395 ScaleAspectH::ScaleAspectH(int x, int y, ScaleThread *thread, float *output, char *string)
396 : BC_TextBox(x, y, xS(50), 1, string)
398 this->output = output;
399 this->thread = thread;
401 ScaleAspectH::~ScaleAspectH()
405 int ScaleAspectH::handle_event()
407 *output = atof(get_text());