Debian Codegraph @

samples/qtrecompress/filters.cpp

raw
1 #include "filters.h"
2 #include "filters.moc"
3 #include "rgn.h"
4 #include "recompressor.h"
5
6 #include "okdialog.h"
7
8 #include <qapplication.h>
9 #include <qslider.h>
10 #include <qdialog.h>
11 #include <qhbox.h>
12 #include <qmessagebox.h>
13 #include <qpushbutton.h>
14 #include <qlabel.h>
15 #include <qlayout.h>
16 #include <qgroupbox.h>
17 #include <qlineedit.h>
18 #include <qvalidator.h>
19 #include <qcheckbox.h>
20
21 #include <math.h>
22 #include <stdio.h>
23
24 #if QT_VERSION <= 220
25 #define LAYOUTCOL 1
26 #else
27 #define LAYOUTCOL 0
28 #endif
29
30 // simple fullresizable dialog which handles sliders
31 class MySliderDialog : public QavmOkDialog
32 {
33 public:
34 MySliderDialog(const char* title) :QavmOkDialog( 0, title, true ), m_iRow(0) {}
35 QSlider* addSlider( const char* param, const char* left = 0, const char* right = 0 );
36 protected:
37 int m_iRow;
38 };
39
40 QSlider* MySliderDialog::addSlider(const char* param, const char* left, const char* right)
41 {
42 QGroupBox* gb = new QGroupBox( this );
43 gb->setTitle( param );
44 gb->setColumnLayout( LAYOUTCOL, Qt::Vertical );
45 QGridLayout* qvl = new QGridLayout( gb->layout(), 1, 1 );
46
47 QSlider* s = new QSlider( gb );
48 qvl->addMultiCellWidget(s, 0, 0, 0, 4);
49 s->setTracking(false);
50 s->setOrientation(QSlider::Horizontal);
51 s->setTickmarks(QSlider::Below);
52
53 if (left)
54 {
55 QLabel *l1 = new QLabel( gb );
56 l1->setText( left );
57 qvl->addWidget( l1, 1, 1 );
58 }
59
60 if (right)
61 {
62 QLabel *l2 = new QLabel( gb );
63 l2->setText( right );
64 qvl->addWidget( l2, 1, 3 );
65 }
66
67 qvl->setColStretch( 0, 1 );
68 qvl->setColStretch( 2, 10 );
69 qvl->setColStretch( 4, 1 );
70
71 gridLayout()->addMultiCellWidget( gb, m_iRow, m_iRow, 0, 2 );
72 m_iRow++;
73
74 return s;
75 }
76
77
78 void Filter::redraw()
79 {
80 if (kernel)
81 kernel->redraw();
82 }
83
84
85
86 ////////////
87 // GAMMA
88 ////////////
89
90 QString GammaFilter::fullname()
91 {
92 QString s;
93 s.sprintf(tr("Adjust gamma by: %.2f"), _gamma);
94 return s;
95 }
96
97 avm::CImage* GammaFilter::process(avm::CImage* sim, int pos)
98 {
99 avm::CImage* im = 0;
100 if (sim->Format() == 0 && sim->Depth() == 24)
101 {
102 im = sim;
103 im->AddRef();
104 }
105 else
106 {
107 sim->GetFmt()->Print();
108 avm::BitmapInfo bi(sim->GetFmt());
109 bi.SetBits(24);
110 //bi.SetDirection(true);
111 im = new avm::CImage(sim, &bi);
112 }
113
114 for(int i=0; i<im->Height(); i++)
115 for(int j=0; j<im->Width(); j++)
116 {
117 unsigned char* c=im->At(j,i);
118 int z=c[0];
119 z=int(z*_gamma);
120 if(z>255)z=255;
121 c[0]=z;
122 z=c[1];
123 z=int(z*_gamma);
124 if(z>255)z=255;
125 c[1]=z;
126 z=c[2];
127 z=int(z*_gamma);
128 if(z>255)z=255;
129 c[2]=z;
130 }
131 return im;
132 }
133
134 void GammaFilter::about()
135 {
136 QMessageBox::about(0, tr("About Gamma adjustment Filter"),
137 tr("This is a sample filter"));
138 }
139
140 void GammaFilter::config()
141 {
142 MySliderDialog d( tr( "Gamma adjustment" ) );
143 QSlider* s = d.addSlider( tr( "Gamma:" ), "0", "5" );
144
145 float old_gamma = _gamma;
146 s->setRange(0, 50);
147 s->setValue(int(_gamma * 10));
148 connect( s, SIGNAL( valueChanged( int ) ), this, SLOT( valueChanged( int ) ) );
149
150 if (d.exec() == QDialog::Rejected)
151 _gamma = old_gamma;
152
153 redraw();
154 }
155 void GammaFilter::valueChanged(int new_val)
156 {
157 _gamma = (float)new_val / 10.f;
158 redraw();
159 }
160 QString GammaFilter::save()
161 {
162 QString b;
163 b.sprintf("%f", _gamma);
164 return b;
165 }
166
167 void GammaFilter::load(const QString& str)
168 {
169 sscanf(str.ascii(), "%f", &_gamma);
170 }
171
172 ///////////
173 // BLUR
174 ///////////
175
176 QString BlurFilter::fullname()
177 {
178 QString s;
179 s.sprintf(tr("Blur - radius: %d"), _range);
180 return s;
181 }
182
183 avm::CImage* BlurFilter::process(avm::CImage* sim, int pos)
184 {
185 avm::CImage* im;
186 if (sim->Format() == IMG_FMT_YUV)
187 {
188 im = sim;
189 im->AddRef();
190 }
191 else
192 im = new avm::CImage(sim, IMG_FMT_YUV);
193
194 im->Blur(_range);
195 return im;
196 }
197
198 void BlurFilter::about()
199 {
200 QMessageBox::about(0, tr("About Blur Filter"),
201 tr("This is a sample MMX filter"));
202 }
203
204 void BlurFilter::config()
205 {
206 MySliderDialog d(tr("Blur adjustment"));
207 QSlider* s = d.addSlider(tr("Radius:"), "1", "8");
208 int old_range=_range;
209 s->setRange(1, 8);
210 s->setValue(_range);
211 //s->setLineStep(1);
212 #if QT_VERSION > 220
213 s->setPageStep(1);
214 #endif
215 connect(s, SIGNAL(valueChanged(int)), this, SLOT(valueChanged(int)));
216
217 if(d.exec()==QDialog::Rejected)
218 _range=old_range;
219 redraw();
220 }
221 void BlurFilter::valueChanged(int new_val)
222 {
223 _range=new_val;
224 redraw();
225 }
226 QString BlurFilter::save()
227 {
228 QString b;
229 b.sprintf("%d", _range);
230 return b;
231 }
232
233 void BlurFilter::load(const QString& s)
234 {
235 sscanf(s.ascii(), "%d", &_range);
236 }
237
238
239 /////////////
240 // REFRACT
241 /////////////
242
243 QString RefractFilter::fullname()
244 {
245 QString s;
246 s.sprintf(tr("Compensate refraction - distance: %d, power: %.3f"),
247 _dist, _power);
248 return s;
249 }
250
251 void RefractFilter::about()
252 {
253 QMessageBox::about(0, tr("About Refraction Filter"),
254 tr("This is a sample filter"));
255 }
256
257 void RefractFilter::config()
258 {
259 MySliderDialog d(tr("Refraction compensation"));
260 QSlider* s = d.addSlider(tr("Distance:"));
261 int old_dist = _dist;
262 double old_power = _power;
263
264 s->setRange(1, 50);
265 s->setValue(_dist);
266 connect(s, SIGNAL(valueChanged(int)), this, SLOT(valueChanged1(int)));
267
268 s = d.addSlider(tr("Power:"));
269 s->setRange(1, 50);
270 s->setValue(int(_power * 100));
271 connect(s, SIGNAL(valueChanged(int)), this, SLOT(valueChanged2(int)));
272
273 if (d.exec() == QDialog::Rejected)
274 {
275 _dist = old_dist;
276 _power = old_power;
277 }
278 redraw();
279 }
280
281 void RefractFilter::valueChanged1(int new_val)
282 {
283 _dist = new_val;
284 redraw();
285 }
286 void RefractFilter::valueChanged2(int new_val)
287 {
288 _power = new_val / 100.f;
289 redraw();
290 }
291
292 avm::CImage* RefractFilter::process(avm::CImage* sim, int pos)
293 {
294 avm::CImage* im;
295 if (sim->Format() == IMG_FMT_YUV)
296 {
297 im = sim;
298 im->AddRef();
299 }
300 else
301 im = new avm::CImage(sim, IMG_FMT_YUV);
302
303 avm::CImage* refractor = new avm::CImage(im);
304
305 //refractor->Blur(2);
306 int xd=im->Width();
307 int yd=im->Height();
308
309 uint8_t* data = im->Data();
310 uint8_t* refdata = refractor->Data();
311 for (unsigned int i=xd*yd*3-3; i>=3*_dist+9+9*xd; i-=3)
312 {
313 unsigned char* p = data+i;
314 unsigned char* refp = refdata+i-3-3*xd;
315 int brightness;
316 if (((i-3*_dist)%(3*xd)<(unsigned int)(2.6*xd)) || ((i%(3*xd)>(unsigned int)xd)))
317 //brightness=(refp[-3*dist+0]+refp[-3*dist+1]+refp[-3*dist+2])/3;
318 //brightness=(.114*refp[-3*dist+0]+.586*refp[-3*dist+1]+.300*refp[-3*dist+2]);
319 //brightness=((col*)(refp-3*dist))->Y();
320 brightness=refp[-3*_dist];
321 else
322 brightness=0;
323 brightness=int(brightness*_power);
324 //int y=(*p+power*p[-3*dist])/(1+power);
325 /*if(y<0x10)y=0x10;
326 if(y>0xef)y=0xef;*/
327 /*p[0]=max(0, unsigned char((p[0]+brightness)/(1+power)));
328 p[1]=max(0, unsigned char((p[1]+brightness)/(1+power)));
329 p[2]=max(0, unsigned char((p[2]+brightness)/(1+power)));*/
330 p[0]=(unsigned char)((p[0]+brightness)/(1+_power));
331 //p[1]=unsigned char((p[1]+brightness)/(1+power));
332 //p[2]=unsigned char((p[2]+brightness)/(1+power));
333 //*p=y;
334 }
335 refractor->Release();
336 return im;
337 }
338 QString RefractFilter::save()
339 {
340 QString b;
341 b.sprintf("%d %f", _dist, _power);
342 return b;
343 }
344
345 void RefractFilter::load(const QString& s)
346 {
347 sscanf(s.ascii(), "%d %f", &_dist, &_power);
348 }
349
350 ////////////
351 // NOISE
352 ////////////
353
354 QString NoiseFilter::fullname()
355 {
356 QString s;
357 s.sprintf(tr( "Reduce noise - quality: %d, edge radius: %d" ),
358 _qual, _edge);
359 return s;
360 }
361
362 void NoiseFilter::about()
363 {
364 QMessageBox::about(0, tr("About Noise reduction Filter"),
365 tr("This is a sample filter"));
366 }
367
368 void NoiseFilter::config()
369 {
370 MySliderDialog d( tr( "Noise reduction" ) );
371 QSlider* s = d.addSlider( tr( "Quality:" ), "850", "1000" );
372 int old_qual=_qual;
373 s->setRange(850,1000);
374 s->setValue(_qual);
375 connect(s, SIGNAL(valueChanged(int)), this, SLOT(valueChanged1(int)));
376
377 s = d.addSlider( tr( "Radius:" ), "1", "6" );
378 int old_edge=_edge;
379 s->setRange(1,6);
380 s->setValue(_edge);
381 #if QT_VERSION > 220
382 s->setPageStep(1);
383 #endif
384 connect(s, SIGNAL(valueChanged(int)), this, SLOT(valueChanged2(int)));
385
386 s = d.addSlider( tr(""), "1", "6" ); // FIXME: name of this parameter ????
387 int old_zz=_zz;
388 s->setRange(1,6);
389 s->setValue(_zz);
390 #if QT_VERSION > 220
391 s->setPageStep(1);
392 #endif
393 connect(s, SIGNAL(valueChanged(int)), this, SLOT(valueChanged3(int)));
394
395 if (d.exec() == QDialog::Rejected)
396 {
397 _qual = old_qual;
398 _edge = old_edge;
399 _zz = old_zz;
400 }
401 redraw();
402 }
403
404
405 void NoiseFilter::valueChanged1(int new_val)
406 {
407 _qual=new_val;
408 redraw();
409 }
410
411 void NoiseFilter::valueChanged2(int new_val)
412 {
413 _edge=new_val;
414 redraw();
415 }
416
417 void NoiseFilter::valueChanged3(int new_val)
418 {
419 _zz=new_val;
420 redraw();
421 }
422
423 avm::CImage* NoiseFilter::process(avm::CImage* sim, int pos)
424 {
425 avm::CImage* im;
426 if (sim->Format() == IMG_FMT_YUV)
427 {
428 im = sim;
429 im->AddRef();
430 }
431 else
432 im = new avm::CImage(sim, IMG_FMT_YUV);
433
434 // Move(move);
435 double m_dQual=_qual/1000.;
436
437 avm::CImage* result=new avm::CImage(im);
438 // result->Enhance(256/m_dEnhQual);//128 ms
439 // result->Move(move);
440
441 CEdgeRgn* edges=new CEdgeRgn(im, int(256/m_dQual), _edge, false, _zz);
442
443 avm::CImage* blurred=new avm::CImage(result);
444 blurred->Blur(1);//53 ms
445
446 avm::CImage* blurred3=new avm::CImage(blurred);
447
448 blurred3->Blur(2,1);
449
450 CEdgeRgn* v2=new CEdgeRgn(edges);
451
452 v2->Normalize();//17 ms
453
454 v2->Blur(1);
455 CEdgeRgn* v3=new CEdgeRgn(v2);
456 v3->Blur(2,1);
457
458 /*
459 struct yuv* yptr=(struct yuv*)result->Data();
460 for(int i=0; i<im->Width(); i++)
461 for(int j=0; j<im->Height(); j++)
462 {
463 yptr[i+j*im->Width()].Cb=yptr[i+j*im->Width()].Cr=128;
464 // if((*v2.at(i,j))<0)
465 // yptr[i+j*im->Width()].Y=128;
466 // else
467 yptr[i+j*im->Width()].Y=*(v2.at(i,j));
468 }
469 */
470 const int _xd=im->Width();
471 const int _yd=im->Height();
472 //const int OFF1=-1-1*_xd;
473 const int OFF2=-2-2*_xd;
474 const int OFF4=-4-4*_xd;
475
476 for(int i=4; i<_xd-4; i++)
477 {
478 for(int j=i+_xd*4; j<i+_xd*(_yd-4); j+=_xd)
479 {
480 avm::yuv* src=(avm::yuv*)result->Data()+j;
481 avm::yuv* v;
482 if((*v3->Offset(j+OFF4))==0)
483 {
484 v=(avm::yuv*)blurred3->Data()+j+OFF4;
485 src->Y=v->Y;
486 }
487 else
488 if((*v2->Offset(j+OFF2))==0)
489 {
490 v=(avm::yuv*)blurred3->Data()+j+OFF4;
491 src->Y=v->Y;
492 }
493 // else
494 // if((*edges->Offset(j))>128)
495 // {
496 // v=(yuv*)result->Data()+j;
497 // src->Y=128;
498 // src->Cr=64;
499 // src->Cb=96;
500 // continue;
501 // }
502 // else
503 // {
504 // src->Y=128;
505 // src->Cr=96;
506 // src->Cb=64;
507 // continue;
508 // }
509 // src->Cr=v->Cr;
510 // src->Cb=v->Cb;
511 else
512 if((*v2->Offset(j+OFF2))==0)
513 {
514 v=(avm::yuv*)blurred->Data()+j+OFF2;
515 src->Y=v->Y;
516 }
517 // else
518 // if((*edges->Offset(j))>128)
519 // {
520 // v=(yuv*)result->Data()+j;
521 // src->Y=128;
522 // src->Cr=64;
523 // src->Cb=96;
524 // continue;
525 // }
526 // else
527 // {
528 // src->Y=128;
529 // src->Cr=96;
530 // src->Cb=64;
531 // continue;
532 // }
533 // src->Cr=v->Cr;
534 // src->Cb=v->Cb;
535 }
536 }
537 delete edges;
538 delete blurred;
539 delete blurred3;
540 delete v2;
541 delete v3;
542
543 return result;
544 }
545 QString NoiseFilter::save()
546 {
547 QString b;
548 b.sprintf("%d %d %d", _qual, _edge, _zz);
549 return b;
550 }
551
552 void NoiseFilter::load(const QString& s)
553 {
554 sscanf(s.ascii(), "%d %d %d", &_qual, &_edge, &_zz);
555 }
556
557
558 ///////////
559 // MOVE
560 //////////
561
562 QString MoveFilter::fullname()
563 {
564 QString s;
565 s.sprintf(tr("Move colors by %d"), _delta);
566 return s;
567 }
568
569 avm::CImage* MoveFilter::process(avm::CImage* im, int pos)
570 {
571 im->AddRef();
572 if (im->Depth() != 24)
573 return im;
574 if(_delta==0)
575 return im;
576 if(!im->GetFmt()->IsRGB())im->ToYUV();
577 avm::yuv* ptr=(avm::yuv*)im->Data();
578 int csize=im->Width()*im->Height();
579 if(_delta<0)
580 {
581 for(int i=0; i<csize+_delta; i++)
582 {
583 ptr[i].Cb=ptr[i-_delta].Cb;
584 ptr[i].Cr=ptr[i-_delta].Cr;
585 }
586 }
587 else
588 {
589 for(int i=csize-1; i>=_delta; i--)
590 {
591 ptr[i].Cb=ptr[i-_delta].Cb;
592 ptr[i].Cr=ptr[i-_delta].Cr;
593 }
594 }
595 return im;
596 }
597
598 void MoveFilter::about()
599 {
600 QMessageBox::about(0, tr( "About Color move Filter" ),
601 tr( "This is a sample filter" ));
602 }
603
604 void MoveFilter::config()
605 {
606 MySliderDialog d( tr( "Color move adjustment" ) );
607 QSlider* s = d.addSlider( tr( "Color move:" ), "-20", "20");
608 int old_delta=_delta;
609 s->setRange(-20, 20);
610 s->setValue(_delta);
611 connect(s, SIGNAL(valueChanged(int)), this, SLOT(valueChanged(int)));
612 if (d.exec() == QDialog::Rejected)
613 _delta = old_delta;
614 redraw();
615 }
616
617 void MoveFilter::valueChanged(int new_val)
618 {
619 _delta=new_val;
620 redraw();
621 }
622
623 QString MoveFilter::save()
624 {
625 QString b;
626 b.sprintf("%d", _delta);
627 return b;
628 }
629
630 void MoveFilter::load(const QString& s)
631 {
632 sscanf(s.ascii(), "%d", &_delta);
633 }
634 /*
635 avm::CImage* SwapFilter::process(avm::CImage* im, int pos)
636 {
637 // if(im->yuv())im->ToRGB();
638 if(!im->yuv())im->ToYUV();
639 int csize=im->Width()*im->Height();
640 yuv* ptr=(yuv*)im->Data();
641 for(int i=0; i<csize; i++)
642 {
643 int t=ptr[i].r;
644 ptr[i].r=ptr[i].g;
645 ptr[i].g=t;
646 // col z=col(ptr[i]);
647 // *(col*)(ptr+i)=z;
648 // yuv z(0,0,0);
649 // z.Cr=_c1*ptr[i].Cr+_c2*ptr[i].Cb;
650 // z.Cb=_c3*ptr[i].Cr+_c4*ptr[i].Cb;
651 int z=ptr[i].Y;
652 z+=ptr[i].Cr;
653 z+=ptr[i].Cb;
654 z/=3;
655 ptr[i].Y=z;
656 ptr[i].Cr=ptr[i].Cb=128;
657 // ptr[i]=z;
658
659 }
660 im->addref();
661 return im;
662 }
663
664 void SwapFilter::config()
665 {
666 QDialog d(0, "Noise reduction", true);
667 d.setFixedSize(300,200);
668 QSlider* slider1=new QSlider(&d, "");
669 QSlider* slider2=new QSlider(&d, "");
670 QSlider* slider3=new QSlider(&d, "");
671 QSlider* slider4=new QSlider(&d, "");
672 // slider1->setTracking(false);
673 // slider2->setTracking(false);
674 // slider3->setTracking(false);
675 // slider4->setTracking(false);
676 QLabel* label1=new QLabel(&d, "");
677 label1->setText("Adjust qual:");
678 label1->resize(70,15);
679 label1->move(10,15);
680 QLabel* label2=new QLabel(&d, "");
681 label2->setText("Adjust radius:");
682 label2->resize(70,15);
683 label2->move(10,50);
684 slider1->setOrientation(QSlider::Horizontal);
685 slider1->setTickmarks(QSlider::Below);
686 slider2->setOrientation(QSlider::Horizontal);
687 slider2->setTickmarks(QSlider::Below);
688 slider3->setOrientation(QSlider::Horizontal);
689 slider3->setTickmarks(QSlider::Below);
690 slider4->setOrientation(QSlider::Horizontal);
691 slider4->setTickmarks(QSlider::Below);
692
693 slider1->move(90,15);
694 slider1->resize(200,25);
695 slider1->setRange(-200,200);
696 slider1->setValue(int(_c1*100));
697 slider2->move(90,50);
698 slider2->resize(200,25);
699 slider2->setRange(-200,200);
700 slider2->setValue(int(_c2*100));
701 slider3->move(90,85);
702 slider3->resize(200,25);
703 slider3->setRange(-200,200);
704 slider3->setValue(int(_c3*100));
705 slider4->move(90,135);
706 slider4->resize(200,25);
707 slider4->setRange(-200,200);
708 slider4->setValue(int(_c4*100));
709 connect(slider1, SIGNAL(valueChanged(int)), this, SLOT(valueChanged1(int)));
710 connect(slider2, SIGNAL(valueChanged(int)), this, SLOT(valueChanged2(int)));
711 connect(slider3, SIGNAL(valueChanged(int)), this, SLOT(valueChanged3(int)));
712 connect(slider4, SIGNAL(valueChanged(int)), this, SLOT(valueChanged4(int)));
713 QPushButton* b_ok=new QPushButton("Ok", &d);
714 b_ok->resize(40,20);
715 b_ok->move(200,160);
716 QPushButton* b_cancel=new QPushButton("Cancel", &d);
717 b_cancel->resize(40,20);
718 b_cancel->move(250,160);
719 connect(b_ok, SIGNAL(clicked()), &d, SLOT(accept()));
720 connect(b_cancel, SIGNAL(clicked()), &d, SLOT(reject()));
721 d.exec();
722 kernel->redraw();
723 }
724 void SwapFilter::valueChanged1(int new_val)
725 {
726 _c1=double(new_val)/100.;
727 kernel->redraw();
728 }
729 void SwapFilter::valueChanged2(int new_val)
730 {
731 _c2=double(new_val)/100.;
732 kernel->redraw();
733 }
734 void SwapFilter::valueChanged3(int new_val)
735 {
736 _c3=double(new_val)/100.;
737 kernel->redraw();
738 }
739 void SwapFilter::valueChanged4(int new_val)
740 {
741 _c4=double(new_val)/100.;
742 kernel->redraw();
743 }
744 */
745
746 namespace {
747 typedef int myGLint;
748 typedef unsigned int myGLuint;
749 typedef unsigned short int myGLushort;
750 typedef char myGLbyte;
751 typedef unsigned char myGLubyte;
752
753 void halveImage(myGLint components, myGLuint width, myGLuint height,
754 const myGLubyte *datain, myGLubyte *dataout)
755 {
756 int i, j, k;
757 int newwidth, newheight;
758 int delta;
759 myGLubyte *s;
760 const myGLubyte *t;
761
762 newwidth = width / 2;
763 newheight = height / 2;
764 delta = width * components;
765 s = dataout;
766 t = datain;
767
768 /* Piece o' cake! */
769 for (i = 0; i < newheight; ++i) {
770 for (j = 0; j < newwidth; ++j) {
771 for (k = 0; k < components; ++k) {
772 s[0] = (t[0] + t[components] + t[delta] +
773 t[delta+components] + 2) / 4;
774 ++s; ++t;
775 }
776 t += components;
777 }
778 t += delta;
779 }
780 }
781
782 void scale_internal(myGLint components, myGLint widthin, myGLint heightin,
783 const myGLubyte *datain,
784 myGLint widthout, myGLint heightout,
785 myGLubyte *dataout)
786 {
787 float x, lowx, highx, convx, halfconvx;
788 float y, lowy, highy, convy, halfconvy;
789 float xpercent,ypercent;
790 float percent;
791 /* Max components in a format is 4, so... */
792 float totals[4];
793 float area;
794 int i,j,k,yint,xint,xindex,yindex;
795 int temp;
796
797 if (widthin == widthout*2 && heightin == heightout*2) {
798 halveImage(components, widthin, heightin, datain, dataout);
799 return;
800 }
801 convy = (float)heightin/(float)heightout;
802 convx = (float)widthin/(float)widthout;
803 halfconvx = convx/2;
804 halfconvy = convy/2;
805 for (i = 0; i < heightout; i++) {
806 y = convy * (i+0.5f);
807 if (heightin > heightout) {
808 highy = y + halfconvy;
809 lowy = y - halfconvy;
810 } else {
811 highy = y + 0.5f;
812 lowy = y - 0.5f;
813 }
814 for (j = 0; j < widthout; j++) {
815 x = convx * (j + 0.5f);
816 if (widthin > widthout) {
817 highx = x + halfconvx;
818 lowx = x - halfconvx;
819 } else {
820 highx = x + 0.5f;
821 lowx = x - 0.5f;
822 }
823
824 /*
825 ** Ok, now apply box filter to box that goes from (lowx, lowy)
826 ** to (highx, highy) on input data into this pixel on output
827 ** data.
828 */
829 totals[0] = totals[1] = totals[2] = totals[3] = 0.0;
830 area = 0.0;
831
832 y = lowy;
833 yint = (int)floor(y);
834 while (y < highy) {
835 yindex = (yint + heightin) % heightin;
836 if (highy < yint+1) {
837 ypercent = highy - y;
838 } else {
839 ypercent = yint+1 - y;
840 }
841
842 x = lowx;
843 xint = (int)floor(x);
844
845 while (x < highx) {
846 xindex = (xint + widthin) % widthin;
847 if (highx < xint+1) {
848 xpercent = highx - x;
849 } else {
850 xpercent = xint+1 - x;
851 }
852
853 percent = xpercent * ypercent;
854 area += percent;
855 temp = (xindex + (yindex * widthin)) * components;
856 for (k = 0; k < components; k++) {
857 totals[k] += datain[temp + k] * percent;
858 }
859
860 xint++;
861 x = xint;
862 }
863 yint++;
864 y = yint;
865 }
866
867 temp = (j + (i * widthout)) * components;
868 for (k = 0; k < components; k++) {
869 /* totals[] should be rounded in the case of enlarging an RGB
870 * ramp when the type is 332 or 4444
871 */
872 dataout[temp + k] = (short unsigned int)((totals[k]+0.5)/area);
873 }
874 }
875 }
876 }
877 };
878
879 QString ScaleFilter::fullname()
880 {
881 QString s;
882 s.sprintf(tr( "Scale to %.2f x %.2f" ), m_dWidth, m_dHeight);
883 return s;
884 }
885
886 void ScaleFilter::about()
887 {
888 QMessageBox::about( 0, tr( "About Scal Filter" ),
889 tr( "Universal scale filter" ) );
890 }
891
892 void ScaleFilter::config()
893 {
894 bool old_aspect = m_bAspect;
895 bool old_percent = m_bPercent;
896 double old_width = m_dWidth;
897 double old_height = m_dHeight;
898 QString s;
899
900 QavmOkDialog d(0, tr( "Scale filter" ), true );
901
902 QCheckBox* chb1 = new QCheckBox( tr( "Preserve aspect ratio" ), &d );
903 chb1->setChecked(m_bAspect);
904 d.gridLayout()->addWidget(chb1, 0, 0);
905
906 connect(chb1, SIGNAL(clicked()), this, SLOT(onAspectToggle()));
907
908 QCheckBox* chb2 = new QCheckBox( tr( "Use as percentage" ), &d );
909 chb2->setChecked(m_bPercent);
910 d.gridLayout()->addWidget(chb2, 1, 0);
911
912 connect(chb2, SIGNAL(clicked()), this, SLOT(onPercentToggle()));
913
914 QDoubleValidator qv( 0.0, 1000.0, 3, &d );
915 m_pLew = new QLineEdit( &d, "scale_width" );
916 m_pLew->setValidator( &qv );
917 m_pLew->setText( s.sprintf("%.2f", m_dWidth) );
918 d.gridLayout()->addMultiCellWidget( m_pLew, 2, 2, 0, 0 );
919
920 connect(m_pLew, SIGNAL(returnPressed()), this, SLOT(valueChanged()));
921
922 m_pLeh = new QLineEdit( &d, "scale_height" );
923 m_pLeh->setValidator( &qv );
924 m_pLeh->setText( s.sprintf("%.2f",m_dHeight) );
925 d.gridLayout()->addMultiCellWidget( m_pLeh, 3, 3, 0, 0 );
926
927 connect(m_pLeh, SIGNAL(returnPressed()), this, SLOT(valueChanged()));
928
929 if (d.exec() == QDialog::Rejected)
930 {
931 fprintf(stderr, "tb:config> QDialog::Rejected\n");
932 m_bAspect = old_aspect;
933 m_bPercent = old_percent;
934 m_dWidth = old_width;
935 m_dHeight = old_height;
936 }
937 else
938 {
939 valueChanged();
940 valueChanged();
941 }
942 redraw();
943 }
944
945 avm::CImage* ScaleFilter::process(avm::CImage* im, int pos)
946 {
947 if (im->Depth()!=24)
948 {
949 im->AddRef();
950 return im;
951 }
952
953 avm::BitmapInfo info = *(im->GetFmt());
954 adjust(info);
955 avm::CImage* result = new avm::CImage(&info);
956
957 scale_internal(3,im->Width(),im->Height(),(myGLubyte *)(im->Data()),
958 result->Width(),result->Height(),(myGLubyte *)(result->Data()));
959 return result;
960 }
961
962 void ScaleFilter::adjust(BITMAPINFOHEADER& bh)
963 {
964 double r = bh.biWidth / (double) bh.biHeight;
965 if (m_bPercent)
966 {
967 bh.biWidth = int(bh.biWidth * (m_dWidth / 100.0));
968 bh.biHeight = int(bh.biHeight * (m_dHeight / 100.0));
969 }
970 else
971 {
972 bh.biWidth = (int)m_dWidth;
973 bh.biHeight = (int)m_dHeight;
974 }
975
976 if (m_bAspect)
977 bh.biHeight = int(bh.biWidth / r);
978
979 bh.biSizeImage = bh.biWidth * bh.biHeight * (bh.biBitCount / 8);
980 }
981
982 void ScaleFilter::onAspectToggle()
983 {
984 m_bAspect = !m_bAspect;
985 redraw();
986 }
987
988 void ScaleFilter::onPercentToggle()
989 {
990 m_bPercent = !m_bPercent;
991 redraw();
992 }
993
994 void ScaleFilter::valueChanged()
995 {
996 m_dWidth = m_pLeh->text().toFloat();
997 m_dHeight = m_pLeh->text().toFloat();
998 redraw();
999 }
1000
1001 class FilterSelectionDialog : public QDialog
1002 {
1003 public:
1004 QGridLayout* m_pGl;
1005 FilterSelectionDialog(const char* title);
1006 int exec();
1007 };