Laplace v4

1  /***********************************************************************************  
2      ImAnalyse : software in image processing and image analysis 
3     
4      Copyright (C) 10 juillet 2008  <Vincent MORARD> 
5    Version: 2.1 
6      Contact: vincent<POINT>morard<AROBAS>cpe<POINT>fr 
7    Website: http://ImAnalyse.free.fr 
8    Website: http://pistol.petesampras.free.fr 
9   
10      This program is free software: you can redistribute it and/or modify 
11      it under the terms of the GNU General Public License as published by 
12      the Free Software Foundation, either version 3 of the License, or 
13      (at your option) any later version. 
14   
15      This program is distributed in the hope that it will be useful, 
16      but WITHOUT ANY WARRANTY; without even the implied warranty of 
17      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
18      GNU General Public License for more details. 
19   
20      You should have received a copy of the GNU General Public License 
21      along with this program.  If not, see <http://www.gnu.org/licenses/ 
22  **********************************************************************************/ 
23  #include "../CImage.h" 
24   
25   
26   
27  //Worker Threads 
28  //Chacun des threads a une partie de l'image a traiter et se termine lorsqu'il a fini sa zone 
29  DWORD WINAPI LAPLACE_I(LPVOID lParam) 
30  { 
31   
32   
33    MULTITHREAD *MT=(MULTITHREAD*)lParam; 
34    MT->Error=0; 
35    if(MT->ImgDest==0 || MT->ImgDest->GetHBmp()==0){ 
36      MT->Error=1; 
37      return 0; 
38    } 
39    int i,j,Pos; 
40    double Val; 
41    Dimension Dim=MT->ImgDest->GetDimension(); 
42   
43    if(MT->Param1!=RGBi) 
44      MT->ImgDest->SetType(GRAY); 
45   
46    for(i=MT->X0 ; i<MT->X1 ; i++) 
47      for(j=MT->Y0 ; j<MT->Y1 ; j++) 
48      { 
49   
50        Pos=( i+ j * Dim.cx)*4; 
51   
52        if(i==0 || i==Dim.cx-1 ||j==0 || j==Dim.cy-1) 
53        { 
54          MT->ImgDest->ucBits[Pos]=0; 
55          MT->ImgDest->ucBits[Pos+1]=0; 
56          MT->ImgDest->ucBits[Pos+2]=0; 
57          continue; 
58        } 
59        switch(MT->Param1) 
60        { 
61        case RGBi : 
62            Val=                  MT->ucBits[(i+(j-1)*Dim.cx)*4] 
63                +MT->ucBits[(i-1+j*Dim.cx)*4] -4*MT->ucBits[(i+j*Dim.cx)*4]    +MT->ucBits[(i+1+j*Dim.cx)*4] 
64                               +  MT->ucBits[(i+(j+1)*Dim.cx)*4]; 
65            MT->ImgDest->ucBits[Pos]=(int)Limit(Val); 
66   
67            Val=                MT->ucBits[(i+(j-1)*Dim.cx)*4+1] 
68                +MT->ucBits[(i-1+j*Dim.cx)*4+1] -4*MT->ucBits[(i+j*Dim.cx)*4+1]    +MT->ucBits[(i+1+j*Dim.cx)*4+1] 
69                               +  MT->ucBits[(i+(j+1)*Dim.cx)*4+1]; 
70            MT->ImgDest->ucBits[Pos+1]=(int)Limit(Val); 
71   
72            Val=                MT->ucBits[(i+(j-1)*Dim.cx)*4+2] 
73                +MT->ucBits[(i-1+j*Dim.cx)*4+2] -4*MT->ucBits[(i+j*Dim.cx)*4+2]    +MT->ucBits[(i+1+j*Dim.cx)*4+2] 
74                                +  MT->ucBits[(i+(j+1)*Dim.cx)*4+2]; 
75            MT->ImgDest->ucBits[Pos+2]=(int)Limit(Val); 
76            break; 
77   
78          case GRAY : 
79            double D[5]; 
80            D[0]=(MT->ucBits[(i+(j-1)*Dim.cx)*4]+MT->ucBits[(i+(j-1)*Dim.cx)*4+1]+MT->ucBits[(i+(j-1)*Dim.cx)*4+2])/3; 
81            D[1]=(MT->ucBits[(i-1+j*Dim.cx)*4]  +MT->ucBits[(i-1+j*Dim.cx)*4+1]  +MT->ucBits[(i-1+j*Dim.cx)*4+2])  /3; 
82            D[2]=(MT->ucBits[(i+j*Dim.cx)*4]    +MT->ucBits[(i+j*Dim.cx)*4+1]    +MT->ucBits[(i+j*Dim.cx)*4+2])    /3; 
83            D[3]=(MT->ucBits[(i+1+j*Dim.cx)*4]  +MT->ucBits[(i+1+j*Dim.cx)*4+1]  +MT->ucBits[(i+1+j*Dim.cx)*4+2])  /3; 
84            D[4]=(MT->ucBits[(i+(j+1)*Dim.cx)*4]+MT->ucBits[(i+(j+1)*Dim.cx)*4+1]+MT->ucBits[(i+(j+1)*Dim.cx)*4+2])/3; 
85            MT->ImgDest->ucBits[Pos]=  (int)Limit((-4*D[2]+D[0]+D[1]+D[3]+D[4])); 
86            MT->ImgDest->ucBits[Pos+1]=MT->ImgDest->ucBits[Pos]; 
87            MT->ImgDest->ucBits[Pos+2]=MT->ImgDest->ucBits[Pos]; 
88   
89            break; 
90          case RED  : 
91            Val=                  MT->ucBits[(i+(j-1)*Dim.cx)*4+2] 
92                +MT->ucBits[(i-1+j*Dim.cx)*4+2] -4*MT->ucBits[(i+j*Dim.cx)*4+2]    +MT->ucBits[(i+1+j*Dim.cx)*4+2] 
93                                 +  MT->ucBits[(i+(j+1)*Dim.cx)*4+2]; 
94              MT->ImgDest->ucBits[Pos]=(int)Limit(Val); 
95            MT->ImgDest->ucBits[Pos+1]=MT->ImgDest->ucBits[Pos]; 
96            MT->ImgDest->ucBits[Pos+2]=MT->ImgDest->ucBits[Pos]; 
97            break; 
98   
99   
100          case GREEN: 
101            Val=                  MT->ucBits[(i+(j-1)*Dim.cx)*4+1] 
102                +MT->ucBits[(i-1+j*Dim.cx)*4+1] -4*MT->ucBits[(i+j*Dim.cx)*4+1]    +MT->ucBits[(i+1+j*Dim.cx)*4+1] 
103                                 +  MT->ucBits[(i+(j+1)*Dim.cx)*4+1]; 
104            MT->ImgDest->ucBits[Pos]=(int)Limit(Val); 
105            MT->ImgDest->ucBits[Pos+1]=MT->ImgDest->ucBits[Pos]; 
106            MT->ImgDest->ucBits[Pos+2]=MT->ImgDest->ucBits[Pos]; 
107            break; 
108   
109          case BLUE : 
110            Val=                    MT->ucBits[(i+(j-1)*Dim.cx)*4] 
111                +MT->ucBits[(i-1+j*Dim.cx)*4] -4*MT->ucBits[(i+j*Dim.cx)*4]   +MT->ucBits[(i+1+j*Dim.cx)*4] 
112                                 +  MT->ucBits[(i+(j+1)*Dim.cx)*4]; 
113              MT->ImgDest->ucBits[Pos]=(int)Limit(Val); 
114            MT->ImgDest->ucBits[Pos+1]=MT->ImgDest->ucBits[Pos]; 
115            MT->ImgDest->ucBits[Pos+2]=MT->ImgDest->ucBits[Pos]; 
116            break; 
117          } 
118      } 
119   
120   
121    return 1; 
122  } 
123   
124   
125  bool CImage::LaplaceI(int Type,CImage *ImgDest) 
126  { 
127    if(hBmp==0){ 
128      MessageBox(NULL,"LaplaceI : L'image source est vide", 
129            NULL,MB_OK|MB_ICONWARNING); 
130      return 0; 
131    } 
132    if(ImgDest==0) 
133      ImgDest=this; 
134   
135    CImage ImgTmp; 
136    int i; 
137   
138      HANDLE  hThread[NB_THREAD]; 
139    MULTITHREAD MT[NB_THREAD]; 
140   
141    ImgTmp.CopyProp(this); 
142   
143    InitThread(MT,Width,Height); 
144   
145   
146    for(i=0;i<NB_THREAD;i++){ 
147      MT[i].Param1=Type; 
148      MT[i].ImgDest=&ImgTmp; 
149      MT[i].ucBits=ucBits; 
150    } 
151   
152    GetBitmapBits (hBmp,(Width*Height)*4,ucBits); 
153   
154    for(i=0;i<NB_THREAD;i++) 
155    { 
156      hThread[i]=CreateThread(NULL,0,LAPLACE_I,&MT[i],0,0); 
157      if (hThread[i] == NULL) 
158      { 
159        MessageBox(NULL,"LaplaceI : Erreur lors de la creation des threads", 
160            NULL,MB_OK|MB_ICONWARNING); 
161        return 0; 
162      } 
163    } 
164   
165    WaitForMultipleObjects(NB_THREAD, hThread, TRUE, INFINITE); 
166   
167    //Verifie si il y a eu des erreurs dans les threads 
168    for(i=0;i<NB_THREAD;i++) 
169    { 
170      CloseHandle(hThread[i]); 
171      if(MT[i].Error==1){ 
172        MessageBox(NULL,"LaplaceI : Erreur lors de l'execution des threads", 
173            NULL,MB_OK|MB_ICONWARNING); 
174        return 0; 
175      } 
176    } 
177   
178   
179   
180    ImgDest->Copy(&ImgTmp); 
181   
182    SetBitmapBits (ImgDest->hBmp,(Width*Height)*4,ImgDest->ucBits); 
183    return 1; 
184  } 
185   
186  /* 
187  bool CImage::LaplaceI(int Type,CImage *ImgDest) 
188  { 
189    if(hBmp==0){ 
190      MessageBox(NULL,"LaplaceI : L'image source est vide", 
191            NULL,MB_OK|MB_ICONWARNING); 
192      return 0; 
193    } 
194     
195    if(ImgDest!=0 && ImgDest!=this) 
196      ImgDest->Copy(this); 
197    if(ImgDest==0) 
198      ImgDest=this; 
199     
200    int i,j; 
201    double *Lap=new double[Width*Height]; 
202     
203     
204    double *Lap_Red,*Lap_Green; 
205    if(Type==RGBi) 
206    { 
207      Lap_Red=new double[Width*Height]; 
208      Lap_Green=new double[Width*Height]; 
209    } 
210   
211    //recupération des pixels 
212    GetBitmapBits(hBmp,Width*Height*4,ucBits); 
213     
214    for(i=1;i<Width-1;i++) 
215      for(j=1;j<Height-1;j++) 
216      { 
217        switch(Type) 
218        { 
219        case RGBi: 
220          Lap_Red[(i+j*Width)]=          GetPixel(i,j-1,RED) 
221                      +GetPixel(i-1,j,RED)  -4*GetPixel(i,j,RED)  +GetPixel(i+1,j,RED) 
222                             +GetPixel(i,j+1,RED);   
223         
224           
225          Lap_Green[(i+j*Width)]=          GetPixel(i,j-1,GREEN) 
226                      +GetPixel(i-1,j,GREEN)  -4*GetPixel(i,j,GREEN)  +GetPixel(i+1,j,GREEN) 
227                             +GetPixel(i,j+1,GREEN);   
228         
229          Lap[(i+j*Width)]=             GetPixel(i,j-1,BLUE) 
230                      +GetPixel(i-1,j,BLUE)  -4*GetPixel(i,j,BLUE)  +GetPixel(i+1,j,BLUE) 
231                             +GetPixel(i,j+1,BLUE); 
232           
233        break; 
234        default: 
235          Lap[(i+j*Width)]=             GetPixel(i,j-1,Type) 
236                      +GetPixel(i-1,j,Type)  -4*GetPixel(i,j,Type)  +GetPixel(i+1,j,Type) 
237                             +GetPixel(i,j+1,Type); 
238          break; 
239        } 
240      } 
241   
242    switch(Type) 
243    { 
244    case RGBi: 
245      ImgDest->ImgType=RGBi; 
246      for(i=0,j=0;i<Width*Height*4;i+=4,j++) 
247      {   
248        ImgDest->ucBits[i]=LimitABS((int)Lap[j]); 
249        ImgDest->ucBits[i+1]=LimitABS((int)Lap_Green[j]); 
250        ImgDest->ucBits[i+2]=LimitABS((int)Lap_Red[j]); 
251      } 
252      delete []Lap_Red; 
253      delete []Lap_Green; 
254   
255      break; 
256     
257    default: 
258      ImgDest->ImgType=GRAY; 
259      for(i=0,j=0;i<Width*Height*4;i+=4,j++) 
260      { 
261        ImgDest->ucBits[i]=LimitABS((int)Lap[j]); 
262        ImgDest->ucBits[i+1]=LimitABS((int)Lap[j]); 
263        ImgDest->ucBits[i+2]=LimitABS((int)Lap[j]); 
264      } 
265      break; 
266   
267   
268    } 
269     
270    delete []Lap; 
271     
272     
273    SetBitmapBits (ImgDest->hBmp,(Width*Height)*4,ImgDest->ucBits); 
274     
275    return 1; 
276  } 
277   
278   
279  */ 

lien1 lien2 lien3