1 /***************************************************************************
2                           DIA_flyCrop.cpp  -  description
3                              -------------------
4 
5         Common part of the crop dialog
6 
7     copyright            : (C) 2002/2007 by mean
8     email                : fixounet@free.fr
9  ***************************************************************************/
10 
11 /***************************************************************************
12  *                                                                         *
13  *   This program is free software; you can redistribute it and/or modify  *
14  *   it under the terms of the GNU General Public License as published by  *
15  *   the Free Software Foundation; either version 2 of the License, or     *
16  *   (at your option) any later version.                                   *
17  *                                                                         *
18  ***************************************************************************/
19 #include "DIA_flyDialogQt4.h"
20 #include "ADM_default.h"
21 #include "ADM_image.h"
22 #include "DIA_flyBlackenBorders.h"
23 #include "ui_blackenBorders.h"
24 
25 #if 0
26     #define aprintf printf
27 #else
28     #define aprintf(...) {}
29 #endif
30 
31 /**
32  */
flyBlacken(QDialog * parent,uint32_t width,uint32_t height,ADM_coreVideoFilter * in,ADM_QCanvas * canvas,ADM_QSlider * slider)33 flyBlacken::flyBlacken (QDialog *parent,uint32_t width,uint32_t height,ADM_coreVideoFilter *in,
34                                     ADM_QCanvas *canvas, ADM_QSlider *slider)
35                 : ADM_flyDialogRgb(parent,width, height,in,canvas, slider,RESIZE_LAST)
36   {
37     rubber=new ADM_rubberControl(this,canvas);
38     _ox=0;
39     _oy=0;
40     _ow=width;
41     _oh=height;
42   }
43 /**
44  *
45  */
~flyBlacken()46 flyBlacken::~flyBlacken()
47 {
48     delete rubber;
49     rubber=NULL;
50 }
51 
52 /**
53     \fn process
54 	\brief
55 */
processRgb(uint8_t * imageIn,uint8_t * imageOut)56 uint8_t    flyBlacken::processRgb(uint8_t *imageIn, uint8_t *imageOut)
57 {
58         uint32_t x,y;
59         uint8_t  *in;
60         uint32_t w=_w,h=_h;
61 
62 
63         memcpy(imageOut,imageIn,_w*_h*4);
64         in=imageOut;
65         for(y=0;y<top;y++)
66         {
67                 for(x=0;x<w;x++)
68                 {
69                         *in++=0;
70 
71 
72                         *in++=0xff;
73 
74                         *in++=0;
75                         *in++=0xff;
76                 }
77         }
78         // bottom
79         in=imageOut+(w*4)*(h-bottom);
80         for(y=0;y<bottom;y++)
81         {
82                 for(x=0;x<w;x++)
83                 {
84                         *in++=0;
85 
86 
87                         *in++=0xff;
88                         *in++=0;
89                         *in++=0xff;
90                 }
91         }
92         // left
93         in=imageOut;
94         uint32_t stride=4*w-4;
95         for(y=0;y<h;y++)
96         {
97                 for(x=0;x<left;x++)
98                 {
99                         *(in+4*x)=0;
100 
101 
102                         *(in+4*x+1)=0xff;
103                         *(in+4*x+2)=0;
104                         *(in+4*x+3)=0xff;
105                 }
106                 for(x=0;x<right;x++)
107                 {
108                         *(in-4*x+stride-4)=0;
109 
110 
111                         *(in-4*x+stride-3)=0xff;
112                         *(in-4*x+stride-2)=0;
113                         *(in-4*x+stride-1)=0xff;
114 
115                 }
116                 in+=4*w;
117 
118         }
119         return true;
120 
121 }
122 
123 /**
124  *
125  * @param imageIn
126  * @param imageOut
127  * @return
128  */
bound(int val,int other,int maxx)129 static int bound(int val, int other, int maxx)
130 {
131    int r=(int)maxx-(int)(val+other);
132    if(r<0)
133         r=0;
134    return r;
135 }
136 /**
137  * \fn bandResized
138  * @param x
139  * @param y
140  * @param w
141  * @param h
142  * @return
143  */
bandResized(int x,int y,int w,int h)144 bool    flyBlacken::bandResized(int x,int y,int w, int h)
145 {
146 
147     aprintf("Rubber resized: x=%d, y=%d, w=%d, h=%d\n",x,y,w,h);
148     aprintf("Debug: old values: x=%d, y=%d, w=%d, h=%d\n",_ox,_oy,_ow,_oh);
149 
150     double halfzoom=_zoom/2-0.01;
151     // try to recalculate values only if these values were actually modified by moving the handles
152     bool leftHandleMoved=false;
153     bool rightHandleMoved=false;
154     if((x+w)==(_ox+_ow) && (y+h)==(_oy+_oh))
155         leftHandleMoved=true;
156     if(x==_ox && y==_oy)
157         rightHandleMoved=true;
158 
159     _ox=x;
160     _oy=y;
161     _ow=w;
162     _oh=h;
163 
164     bool ignore=false;
165     if(leftHandleMoved && rightHandleMoved) // bogus event
166         ignore=true;
167 
168     int normX, normY, normW, normH;
169     normX=(int)(((double)x+halfzoom)/_zoom);
170     normY=(int)(((double)y+halfzoom)/_zoom);
171     normW=(int)(((double)w+halfzoom)/_zoom);
172     normH=(int)(((double)h+halfzoom)/_zoom);
173 
174     // resize the rubberband back into bounds once the user tries to drag handles out of sight
175     bool resizeRubber=false;
176     if(normX<0 || normY<0 || normX+normW>_w || normY+normH>_h)
177     {
178         resizeRubber=true;
179         aprintf("rubberband out of bounds, will be resized back\n");
180     }
181 
182     if(ignore)
183     {
184         upload(false,resizeRubber);
185         return false;
186     }
187 
188     if(rightHandleMoved)
189     {
190         right=bound(normX,normW,_w)&0xfffe;
191         bottom=bound(normY,normH,_h)&0xfffe;
192     }
193 
194     if(normX<0) normX=0;
195     if(normY<0) normY=0;
196 
197     if(leftHandleMoved)
198     {
199         top=normY&0xfffe;
200         left=normX&0xfffe;
201     }
202 
203     upload(false,resizeRubber);
204     sameImage();
205 
206     return true;
207 }
208 /**
209  * \fn blockChanges
210  * @param block
211  * @return
212  */
213 #define APPLY_TO_ALL(x) {w->spinBoxLeft->x;w->spinBoxRight->x;w->spinBoxTop->x;w->spinBoxBottom->x;rubber->x;}
blockChanges(bool block)214 bool flyBlacken::blockChanges(bool block)
215 {
216     Ui_blackenDialog *w=(Ui_blackenDialog *)_cookie;
217     APPLY_TO_ALL(blockSignals(block));
218     return true;
219 }
220 //EOF
221 
222