Laplace v8
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 |
|
28 |
//Worker Threads |
29 |
//Chacun des threads a une partie de l'image a traiter et se termine lorsqu'il a fini sa zone |
30 |
DWORD WINAPI LAPLACEII(LPVOID lParam) |
31 |
{ |
32 |
|
33 |
|
34 |
MULTITHREAD *MT=(MULTITHREAD*)lParam; |
35 |
MT->Error=0; |
36 |
if(MT->ImgDest==0 || MT->ImgDest->GetHBmp()==0){ |
37 |
MT->Error=1; |
38 |
return 0; |
39 |
} |
40 |
int i,j,Pos; |
41 |
double Val; |
42 |
Dimension Dim=MT->ImgDest->GetDimension(); |
43 |
|
44 |
if(MT->Param1!=RGBi) |
45 |
MT->ImgDest->SetType(GRAY); |
46 |
|
47 |
for(i=MT->X0 ; i<MT->X1 ; i++) |
48 |
for(j=MT->Y0 ; j<MT->Y1 ; j++) |
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-1+(j-1)*Dim.cx)*4]+MT->ucBits[(i+(j-1)*Dim.cx)*4]+MT->ucBits[(i+1+(j-1)*Dim.cx)*4] |
63 |
+MT->ucBits[(i-1+j*Dim.cx)*4] -8*MT->ucBits[(i+j*Dim.cx)*4] +MT->ucBits[(i+1+j*Dim.cx)*4] |
64 |
+MT->ucBits[(i-1+(j+1)*Dim.cx)*4]+MT->ucBits[(i+(j-1)*Dim.cx)*4]+MT->ucBits[(i+(j+1)*Dim.cx)*4]; |
65 |
MT->ImgDest->ucBits[Pos]=(int)Limit(Val); |
66 |
|
67 |
Val= MT->ucBits[(i-1+(j-1)*Dim.cx)*4+1]+MT->ucBits[(i+(j-1)*Dim.cx)*4+1]+MT->ucBits[(i+1+(j-1)*Dim.cx)*4+1] |
68 |
+MT->ucBits[(i-1+j*Dim.cx)*4+1] -8*MT->ucBits[(i+j*Dim.cx)*4+1] +MT->ucBits[(i+1+j*Dim.cx)*4+1] |
69 |
+MT->ucBits[(i-1+(j+1)*Dim.cx)*4+1]+MT->ucBits[(i+(j-1)*Dim.cx)*4+1]+MT->ucBits[(i+(j+1)*Dim.cx)*4+1]; |
70 |
MT->ImgDest->ucBits[Pos+1]=(int)Limit(Val); |
71 |
|
72 |
Val= MT->ucBits[(i-1+(j-1)*Dim.cx)*4+2]+MT->ucBits[(i+(j-1)*Dim.cx)*4+2]+MT->ucBits[(i+1+(j-1)*Dim.cx)*4+2] |
73 |
+MT->ucBits[(i-1+j*Dim.cx)*4+2] -8*MT->ucBits[(i+j*Dim.cx)*4+2] +MT->ucBits[(i+1+j*Dim.cx)*4+2] |
74 |
+MT->ucBits[(i-1+(j+1)*Dim.cx)*4+2]+MT->ucBits[(i+(j-1)*Dim.cx)*4+2]+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[9]; |
80 |
|
81 |
D[0]=(MT->ucBits[(i-1+(j-1)*Dim.cx)*4]+MT->ucBits[(i-1+(j-1)*Dim.cx)*4+1]+MT->ucBits[(i-1+(j-1)*Dim.cx)*4+2])/3; |
82 |
D[1]=(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; |
83 |
D[2]=(MT->ucBits[(i+1+(j-1)*Dim.cx)*4]+MT->ucBits[(i+1+(j-1)*Dim.cx)*4+1]+MT->ucBits[(i+1+(j-1)*Dim.cx)*4+2])/3; |
84 |
|
85 |
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; |
86 |
D[4]=(MT->ucBits[(i+j*Dim.cx)*4]+MT->ucBits[(i+j*Dim.cx)*4+1]+MT->ucBits[(i+j*Dim.cx)*4+2])/3; |
87 |
D[5]=(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; |
88 |
|
89 |
D[6]=(MT->ucBits[(i-1+(j+1)*Dim.cx)*4]+MT->ucBits[(i-1+(j+1)*Dim.cx)*4+1]+MT->ucBits[(i-1+(j+1)*Dim.cx)*4+2])/3; |
90 |
D[7]=(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; |
91 |
D[8]=(MT->ucBits[(i+1+(j+1)*Dim.cx)*4]+MT->ucBits[(i+1+(j+1)*Dim.cx)*4+1]+MT->ucBits[(i+1+(j+1)*Dim.cx)*4+2])/3; |
92 |
|
93 |
|
94 |
MT->ImgDest->ucBits[Pos]= (int)Limit(-8*D[4]+D[1]+D[2]+D[3]+D[5]+D[6]+D[7]+D[8]+D[0]); |
95 |
MT->ImgDest->ucBits[Pos+1]=MT->ImgDest->ucBits[Pos]; |
96 |
MT->ImgDest->ucBits[Pos+2]=MT->ImgDest->ucBits[Pos]; |
97 |
|
98 |
break; |
99 |
case RED : |
100 |
Val= MT->ucBits[(i-1+(j-1)*Dim.cx)*4+2]+MT->ucBits[(i+(j-1)*Dim.cx)*4+2]+MT->ucBits[(i+1+(j-1)*Dim.cx)*4+2] |
101 |
+MT->ucBits[(i-1+j*Dim.cx)*4+2] -8*MT->ucBits[(i+j*Dim.cx)*4+2] +MT->ucBits[(i+1+j*Dim.cx)*4+2] |
102 |
+MT->ucBits[(i-1+(j+1)*Dim.cx)*4+2]+MT->ucBits[(i+(j-1)*Dim.cx)*4+2]+MT->ucBits[(i+(j+1)*Dim.cx)*4+2]; |
103 |
MT->ImgDest->ucBits[Pos]=(int)Limit(Val); |
104 |
MT->ImgDest->ucBits[Pos+1]=MT->ImgDest->ucBits[Pos]; |
105 |
MT->ImgDest->ucBits[Pos+2]=MT->ImgDest->ucBits[Pos]; |
106 |
break; |
107 |
|
108 |
|
109 |
case GREEN: |
110 |
Val= MT->ucBits[(i-1+(j-1)*Dim.cx)*4+1]+MT->ucBits[(i+(j-1)*Dim.cx)*4+1]+MT->ucBits[(i+1+(j-1)*Dim.cx)*4+1] |
111 |
+MT->ucBits[(i-1+j*Dim.cx)*4+1] -8*MT->ucBits[(i+j*Dim.cx)*4+1] +MT->ucBits[(i+1+j*Dim.cx)*4+1] |
112 |
+MT->ucBits[(i-1+(j+1)*Dim.cx)*4+1]+MT->ucBits[(i+(j-1)*Dim.cx)*4+1]+MT->ucBits[(i+(j+1)*Dim.cx)*4+1]; |
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 |
case BLUE : |
119 |
Val= MT->ucBits[(i-1+(j-1)*Dim.cx)*4]+MT->ucBits[(i+(j-1)*Dim.cx)*4]+MT->ucBits[(i+1+(j-1)*Dim.cx)*4] |
120 |
+MT->ucBits[(i-1+j*Dim.cx)*4] -8*MT->ucBits[(i+j*Dim.cx)*4] +MT->ucBits[(i+1+j*Dim.cx)*4] |
121 |
+MT->ucBits[(i-1+(j+1)*Dim.cx)*4]+MT->ucBits[(i+(j-1)*Dim.cx)*4]+MT->ucBits[(i+(j+1)*Dim.cx)*4]; |
122 |
MT->ImgDest->ucBits[Pos]=(int)Limit(Val); |
123 |
MT->ImgDest->ucBits[Pos+1]=MT->ImgDest->ucBits[Pos]; |
124 |
MT->ImgDest->ucBits[Pos+2]=MT->ImgDest->ucBits[Pos]; |
125 |
break; |
126 |
} |
127 |
} |
128 |
|
129 |
|
130 |
return 1; |
131 |
} |
132 |
|
133 |
bool CImage::LaplaceII(int Type,CImage *ImgDest) |
134 |
{ |
135 |
if(hBmp==0){ |
136 |
MessageBox(NULL,"LaplaceII: L'image source est vide", |
137 |
NULL,MB_OK|MB_ICONWARNING); |
138 |
return 0; |
139 |
} |
140 |
if(ImgDest==0) |
141 |
ImgDest=this; |
142 |
|
143 |
CImage ImgTmp; |
144 |
int i; |
145 |
|
146 |
HANDLE hThread[NB_THREAD]; |
147 |
MULTITHREAD MT[NB_THREAD]; |
148 |
|
149 |
ImgTmp.CopyProp(this); |
150 |
|
151 |
InitThread(MT,Width,Height); |
152 |
|
153 |
|
154 |
for(i=0;i<NB_THREAD;i++){ |
155 |
MT[i].Param1=Type; |
156 |
MT[i].ImgDest=&ImgTmp; |
157 |
MT[i].ucBits=ucBits; |
158 |
} |
159 |
|
160 |
GetBitmapBits (hBmp,(Width*Height)*4,ucBits); |
161 |
|
162 |
for(i=0;i<NB_THREAD;i++) |
163 |
{ |
164 |
hThread[i]=CreateThread(NULL,0,LAPLACEII,&MT[i],0,0); |
165 |
if (hThread[i] == NULL) |
166 |
{ |
167 |
MessageBox(NULL,"LaplaceII : Erreur lors de la creation des threads", |
168 |
NULL,MB_OK|MB_ICONWARNING); |
169 |
return 0; |
170 |
} |
171 |
} |
172 |
|
173 |
WaitForMultipleObjects(NB_THREAD, hThread, TRUE, INFINITE); |
174 |
|
175 |
//Verifie si il y a eu des erreurs dans les threads |
176 |
for(i=0;i<NB_THREAD;i++) |
177 |
{ |
178 |
CloseHandle(hThread[i]); |
179 |
if(MT[i].Error==1){ |
180 |
MessageBox(NULL,"LaplaceII : Erreur lors de l'execution des threads", |
181 |
NULL,MB_OK|MB_ICONWARNING); |
182 |
return 0; |
183 |
|
184 |
} |
185 |
} |
186 |
|
187 |
|
188 |
|
189 |
ImgDest->Copy(&ImgTmp); |
190 |
|
191 |
SetBitmapBits (ImgDest->hBmp,(Width*Height)*4,ImgDest->ucBits); |
192 |
return 1; |
193 |
} |
194 |
/* |
195 |
bool CImage::LaplaceII(int Type,CImage *ImgDest) |
196 |
{ |
197 |
if(hBmp==0){ |
198 |
MessageBox(NULL,"LaplaceII : L'image source est vide", |
199 |
NULL,MB_OK|MB_ICONWARNING); |
200 |
return 0; |
201 |
} |
202 |
|
203 |
if(ImgDest!=0 && ImgDest!=this) |
204 |
ImgDest->Copy(this); |
205 |
if(ImgDest==0) |
206 |
ImgDest=this; |
207 |
|
208 |
int i,j; |
209 |
double *Lap=new double[Width*Height]; |
210 |
|
211 |
|
212 |
double *Lap_Red,*Lap_Green; |
213 |
if(Type==RGBi) |
214 |
{ |
215 |
Lap_Red=new double[Width*Height]; |
216 |
Lap_Green=new double[Width*Height]; |
217 |
} |
218 |
|
219 |
//recupération des pixels |
220 |
GetBitmapBits(hBmp,Width*Height*4,ucBits); |
221 |
|
222 |
for(i=1;i<Width-1;i++) |
223 |
for(j=1;j<Height-1;j++) |
224 |
{ |
225 |
switch(Type) |
226 |
{ |
227 |
case RGBi: |
228 |
Lap_Red[(i+j*Width)]=GetPixel(i-1,j-1,RED) +GetPixel(i,j-1,RED) +GetPixel(i+1,j-1,RED) |
229 |
+GetPixel(i-1,j,RED) -8*GetPixel(i,j,RED) +GetPixel(i+1,j,RED) |
230 |
+GetPixel(i-1,j+1,RED)+GetPixel(i,j+1,RED) +GetPixel(i+1,j+1,RED); |
231 |
|
232 |
|
233 |
Lap_Green[(i+j*Width)]=GetPixel(i-1,j-1,GREEN) +GetPixel(i,j-1,GREEN) +GetPixel(i+1,j-1,GREEN) |
234 |
+GetPixel(i-1,j,GREEN) -8*GetPixel(i,j,GREEN) +GetPixel(i+1,j,GREEN) |
235 |
+GetPixel(i-1,j+1,GREEN)+GetPixel(i,j+1,GREEN) +GetPixel(i+1,j+1,GREEN); |
236 |
|
237 |
Lap[(i+j*Width)]=GetPixel(i-1,j-1,BLUE) +GetPixel(i,j-1,BLUE) +GetPixel(i+1,j-1,BLUE) |
238 |
+GetPixel(i-1,j,BLUE) -8*GetPixel(i,j,BLUE) +GetPixel(i+1,j,BLUE) |
239 |
+GetPixel(i-1,j+1,BLUE)+GetPixel(i,j+1,BLUE) +GetPixel(i+1,j+1,BLUE); |
240 |
|
241 |
break; |
242 |
default: |
243 |
Lap[(i+j*Width)]=GetPixel(i-1,j-1,Type) +GetPixel(i,j-1,Type) +GetPixel(i+1,j-1,Type) |
244 |
+GetPixel(i-1,j,Type) -8*GetPixel(i,j,Type) +GetPixel(i+1,j,Type) |
245 |
+GetPixel(i-1,j+1,Type)+GetPixel(i,j+1,Type) +GetPixel(i+1,j+1,Type); |
246 |
break; |
247 |
} |
248 |
} |
249 |
|
250 |
switch(Type) |
251 |
{ |
252 |
case RGBi: |
253 |
ImgDest->ImgType=RGBi; |
254 |
for(i=0,j=0;i<Width*Height*4;i+=4,j++) |
255 |
{ |
256 |
ImgDest->ucBits[i]=LimitABS((int)Lap[j]); |
257 |
ImgDest->ucBits[i+1]=LimitABS((int)Lap_Green[j]); |
258 |
ImgDest->ucBits[i+2]=LimitABS((int)Lap_Red[j]); |
259 |
|
260 |
} |
261 |
delete []Lap_Red; |
262 |
delete []Lap_Green; |
263 |
|
264 |
break; |
265 |
|
266 |
default: |
267 |
ImgDest->ImgType=GRAY; |
268 |
for(i=0,j=0;i<Width*Height*4;i+=4,j++) |
269 |
{ |
270 |
ImgDest->ucBits[i]=LimitABS((int)Lap[j]); |
271 |
ImgDest->ucBits[i+1]=LimitABS((int)Lap[j]); |
272 |
ImgDest->ucBits[i+2]=LimitABS((int)Lap[j]); |
273 |
|
274 |
} |
275 |
break; |
276 |
|
277 |
|
278 |
} |
279 |
|
280 |
delete []Lap; |
281 |
|
282 |
|
283 |
SetBitmapBits (ImgDest->hBmp,(Width*Height)*4,ImgDest->ucBits); |
284 |
|
285 |
return 1; |
286 |
} |
287 |
|
288 |
|
289 |
|
290 |
*/ |
lien1 lien2 lien3