Debian Codegraph @

src/differenceimageprovider.cpp

raw
1 /* dvbcut
2 Copyright (c) 2005 Sven Over <[email protected]>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 /* $Id$ */
20
21 #include <qcolor.h>
22
23 #include "differenceimageprovider.h"
24 #include "avframe.h"
25 #include "mpgfile.h"
26 #include "busyindicator.h"
27
28 differenceimageprovider::differenceimageprovider(mpgfile &mpg, int basepicture, busyindicator *bi,
29 bool unscaled, double factor, int cachesize)
30 : imageprovider(mpg,bi,unscaled,factor,cachesize), basepic(basepicture)
31 {
32 RTTI=unscaled?IMAGEPROVIDER_DIFFERENCE_UNSCALED:IMAGEPROVIDER_DIFFERENCE;
33 baseimg=imageprovider(mpg,bi?new busyindicator(*bi):0,true).getimage(basepic);
34 }
35
36
37 differenceimageprovider::~differenceimageprovider()
38 {}
39
40
41 static inline int square(int x)
42 {
43 return x*x;
44 }
45 static inline QRgb mixcolors(QRgb a, QRgb b, int num, int den)
46 {
47 return qRgb(
48 (qRed(b)*num+qRed(a)*(den-num))/den,
49 (qGreen(b)*num+qGreen(a)*(den-num))/den,
50 (qBlue(b)*num+qBlue(a)*(den-num))/den
51 );
52 }
53
54 void differenceimageprovider::decodepicture(int picture, bool decodeallgop)
55 {
56 std::list<avframe*> framelist;
57 int startpic=m.lastiframe(picture);
58 m.decodegop(startpic,decodeallgop?-1:(picture+1),framelist);
59
60 for (std::list<avframe*>::iterator it=framelist.begin();it!=framelist.end();++it) {
61 QImage im=(*it)->getqimage(false);
62 int displaywidth=(*it)->getdisplaywidth();
63 delete *it;
64
65 if (im.size()!=baseimg.size())
66 im=im.scaled(baseimg.size());
67
68 if (im.depth()==32 && baseimg.depth()==32)
69 for (int y=0;y<baseimg.height();++y) {
70 QRgb *imd=(QRgb*)im.scanLine(y);
71 QRgb *bimd=(QRgb*)baseimg.scanLine(y);
72
73 for (int x=im.width();x>0;--x) {
74 int dist=square(qRed(*imd)-qRed(*bimd))+square(qGreen(*imd)-qGreen(*bimd))+square(qBlue(*imd)-qBlue(*bimd));
75 if (dist>1000)
76 dist=1000;
77
78 *imd=mixcolors(*imd,((x/16+y/16)&1) ? qRgb(64,64,64):qRgb(192,192,192) ,1000-dist,1000);
79
80 ++imd;
81 ++bimd;
82 }
83 }
84
85
86 if ((RTTI!=IMAGEPROVIDER_DIFFERENCE_UNSCALED && displaywidth!=im.width())||(viewscalefactor!=1.0))
87 im=im.scaled(int(((RTTI!=IMAGEPROVIDER_DIFFERENCE_UNSCALED)?displaywidth:im.width())/viewscalefactor+0.5),
88 int(im.height()/viewscalefactor+0.5));
89 framecache.push_front(framecacheitem(startpic++,im));
90 }
91 }