Intensité.cpp

1  /***********************************************************************************  
2      ImAnalyse : software in image processing and image analysis 
3     
4      Copyright (C) 27 avril 2008  <Vincent MORARD> 
5    Version: 2.0 
6      Contact: vincent<POINT>morard<AROBAS>cpe<POINT>fr 
7    Website: http://pistol.petesampras.free.fr 
8   
9      This program is free software: you can redistribute it and/or modify 
10      it under the terms of the GNU General Public License as published by 
11      the Free Software Foundation, either version 3 of the License, or 
12      (at your option) any later version. 
13   
14      This program is distributed in the hope that it will be useful, 
15      but WITHOUT ANY WARRANTY; without even the implied warranty of 
16      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
17      GNU General Public License for more details. 
18   
19      You should have received a copy of the GNU General Public License 
20      along with this program.  If not, see <http://www.gnu.org/licenses/ 
21  **********************************************************************************/ 
22   
23  #include "../CImage.h" 
24   
25   
26   
27  //************************************************************************************* 
28  //ContrateAuto  : Etire l'histogramme des couleurs au maximum (entre 0 et 255) 
29  // 
30  //INPUT 
31  //--Type   : RGBi,RED,GREEN,GRAY ou BLUE :canal a traiter. 
32  //-ImgDest :Image ou sera stoker le traitement. Si 0 sera stoker dans l'image source  
33  //************************************************************************************** 
34  bool CImage::ContrasteAuto(int Type,CImage *ImgDest) 
35  { 
36    int MinRED,MaxRED; 
37    int MinGREEN,MaxGREEN; 
38    int MinBLUE,MaxBLUE; 
39    if(hBmp==0){ 
40      MessageBox(NULL,"ContrasteAutomatique : L'image source est vide", 
41            NULL,MB_OK|MB_ICONWARNING); 
42      return 0; 
43    } 
44   
45    if(ImgDest!=0 && ImgDest!=this) 
46      ImgDest->Copy(this); 
47   
48    if(ImgDest==0) 
49      ImgDest=this; 
50   
51    //recupération des pixels 
52    GetBitmapBits(hBmp,Width*Height*4,ucBits); 
53   
54    //Valeur min et max de l'image 
55    ImgDest->ChercherMinMax(RED,&MinRED,&MaxRED); 
56    ImgDest->ChercherMinMax(GREEN,&MinGREEN,&MaxGREEN); 
57    ImgDest->ChercherMinMax(BLUE,&MinBLUE,&MaxBLUE); 
58   
59    if(MaxRED==0 && MaxGREEN==0 && MaxBLUE==0){ 
60      MessageBox(NULL,"ContrasteAutomatique: Division par 0",NULL,MB_OK|MB_ICONWARNING); 
61      return 0; 
62    } 
63   
64    for(int i=0;i<Width*Height*4;i+=4) 
65    { 
66      switch(Type) 
67      { 
68      case RED  :if(MaxRED)ImgDest->ucBits[i+2]=(ImgDest->ucBits[i+2]*255)/(MaxRED-MinRED)-(255*MinRED)/(MaxRED-MinRED);break; 
69      case GREEN:if(MaxGREEN)ImgDest->ucBits[i+1]=(ImgDest->ucBits[i+1]*255)/(MaxGREEN-MinGREEN)-(255*MinGREEN)/(MaxGREEN-MinGREEN);break; 
70      case BLUE :if(MaxBLUE)ImgDest->ucBits[i]  =(ImgDest->ucBits[i]*255)/(MaxBLUE-MinBLUE)-(255*MinBLUE)/(MaxBLUE-MinBLUE);  break; 
71      case RGBi :if(MaxRED)ImgDest->ucBits[i+2]=(ImgDest->ucBits[i+2]*255)/(MaxRED-MinRED)-(255*MinRED)/(MaxRED-MinRED); 
72             if(MaxGREEN)ImgDest->ucBits[i+1]=(ImgDest->ucBits[i+1]*255)/(MaxGREEN-MinGREEN)-(255*MinGREEN)/(MaxGREEN-MinGREEN);; 
73             if(MaxBLUE)ImgDest->ucBits[i]  =(ImgDest->ucBits[i]*255)/(MaxBLUE-MinBLUE)-(255*MinBLUE)/(MaxBLUE-MinBLUE);  break; 
74   
75      case GRAY :if(MaxRED){ImgDest->ucBits[i+2]=(ImgDest->ucBits[i+2]*255)/(MaxRED-MinRED)-(255*MinRED)/(MaxRED-MinRED); 
76            ImgDest->ucBits[i+1]=ImgDest->ucBits[i+2]; 
77            ImgDest->ucBits[i]=ImgDest->ucBits[i+2];}ImgDest->ImgType=GRAY;break; 
78      } 
79    } 
80   
81    SetBitmapBits (ImgDest->hBmp,(Width*Height)*4,ImgDest->ucBits); 
82    return true; 
83  } 
84   
85   
86  //************************************************************************************* 
87  //EgalisationContraste  : Egalise l'histogramme des couleurs  
88  // 
89  //INPUT 
90  //--Type   : RGBi,RED,GREEN,GRAY ou BLUE :canal a traiter. 
91  //-ImgDest :Image ou sera stoker le traitement. Si 0 sera stoker dans l'image source  
92  //************************************************************************************** 
93  bool CImage::EgalisationContraste(int Type,CImage *ImgDest) 
94  { 
95    int *HistRED=0,*HistGREEN=0,*HistBLUE=0,*HistGRAY=0,k; 
96    int CumulRED[256],CumulGREEN[256],CumulBLUE[256],CumulGRAY[256]; 
97   
98    if(hBmp==0){ 
99      MessageBox(NULL,"Egalisation du contraste : L'image source est vide", 
100            NULL,MB_OK|MB_ICONWARNING); 
101      return 0; 
102    } 
103   
104    if(ImgDest!=0 && ImgDest!=this) 
105      ImgDest->Copy(this); 
106   
107    if(ImgDest==0) 
108      ImgDest=this; 
109   
110    //recupération des pixels 
111    GetBitmapBits(hBmp,Width*Height*4,ucBits); 
112   
113   
114    switch(Type) 
115    { 
116    case BLUE :HistBLUE  =Histogramme(BLUE) ; CumulBLUE[0]=HistBLUE[0]  ;break; 
117    case GREEN:HistGREEN =Histogramme(GREEN); CumulGREEN[0]=HistGREEN[0];break; 
118    case RED  :HistRED   =Histogramme(RED)  ; CumulRED[0]=HistRED[0]    ;break; 
119    case GRAY :HistGRAY  =Histogramme(GRAY) ; CumulGRAY[0]=HistGRAY[0]  ;break; 
120    case RGBi :HistBLUE  =Histogramme(BLUE) ; CumulBLUE[0]=HistBLUE[0]  ; 
121             HistGREEN =Histogramme(GREEN); CumulGREEN[0]=HistGREEN[0]; 
122             HistRED   =Histogramme(RED)  ; CumulRED[0]=HistRED[0]  ;break; 
123    } 
124   
125    //Histogramme cumulé 
126    for(k=1;k<=255;k++) 
127    { 
128      if(HistRED)CumulRED[k]=CumulRED[k-1]+HistRED[k]; 
129      if(HistGREEN)CumulGREEN[k]=CumulGREEN[k-1]+HistGREEN[k]; 
130      if(HistBLUE)CumulBLUE[k]=CumulBLUE[k-1]+HistBLUE[k]; 
131      if(HistGRAY)CumulGRAY[k]=CumulGRAY[k-1]+HistGRAY[k]; 
132    } 
133    ImgDest->ImgType=RGBi; 
134    for(int i=0;i<Width*Height*4;i+=4) 
135    { 
136      switch(Type) 
137      { 
138      case RED  : ImgDest->ucBits[i+2]=CumulRED[ucBits[i+2]]*255/(Width*Height)  ;break; 
139      case GREEN: ImgDest->ucBits[i+1]=CumulGREEN[ucBits[i+1]]*255/(Width*Height);break; 
140      case BLUE : ImgDest->ucBits[i]=CumulBLUE[ucBits[i]]*255/(Width*Height)     ;break; 
141      case RGBi : ImgDest->ucBits[i+2]=CumulRED[ucBits[i+2]]*255/(Width*Height)  ; 
142            ImgDest->ucBits[i+1]=CumulGREEN[ucBits[i+1]]*255/(Width*Height); 
143            ImgDest->ucBits[i]=CumulBLUE[ucBits[i]]*255/(Width*Height)     ;break; 
144   
145      case GRAY : ImgDest->ucBits[i+2]=CumulGRAY[ucBits[i+2]]*255/(Width*Height); 
146            ImgDest->ucBits[i+1]=ImgDest->ucBits[i+2]; 
147            ImgDest->ucBits[i]=ImgDest->ucBits[i+2]; 
148            ImgDest->ImgType=GRAY;break; 
149      } 
150    } 
151   
152    SetBitmapBits (ImgDest->hBmp,(Width*Height)*4,ImgDest->ucBits); 
153   
154   
155    return true; 
156   
157   
158  } 
159   
160  bool CImage::ModifEclairement(int Luminance,CImage *ImgDest) 
161  { 
162   
163      if(hBmp==0){ 
164      MessageBox(NULL,"ModifEclairement : L'image source est vide", 
165            NULL,MB_OK|MB_ICONWARNING); 
166      return 0; 
167    } 
168   
169    if(ImgDest!=0 && ImgDest!=this) 
170      ImgDest->Copy(this); 
171   
172    if(ImgDest==0) 
173      ImgDest=this; 
174    //recupération des pixels 
175    GetBitmapBits(hBmp,(Width*Height)*4,ucBits); 
176   
177    for(int i=0;i<Width*Height*4;i+=4) 
178    { 
179      ImgDest->ucBits[i]=Limit((int)(ucBits[i]+Luminance)); 
180      ImgDest->ucBits[i+1]=Limit((int)(ucBits[i+1]+Luminance)); 
181      ImgDest->ucBits[i+2]=Limit((int)(ucBits[i+2]+Luminance)); 
182    } 
183    SetBitmapBits (ImgDest->hBmp,(Width*Height)*4,ImgDest->ucBits); 
184   
185    return 1; 
186   
187  } 
188   
189  //Convertion en HSL, remplacement de la luminance, et retour dans le domaine RGB 
190  bool CImage::ModifEclairementHSL(int Luminance,CImage *ImgDest) 
191  { 
192   
193      if(hBmp==0){ 
194      MessageBox(NULL,"ModifEclairementHSL : L'image source est vide", 
195            NULL,MB_OK|MB_ICONWARNING); 
196      return 0; 
197    } 
198   
199    if(ImgDest!=0 && ImgDest!=this) 
200      ImgDest->Copy(this); 
201   
202    if(ImgDest==0) 
203      ImgDest=this; 
204    //recupération des pixels 
205    GetBitmapBits(hBmp,(Width*Height)*4,ucBits); 
206    double H,S,L,R,G,B; 
207   
208    for(int i=0;i<Width*Height*4;i+=4) 
209    { 
210   
211      if(i==(539+36*Width)*4) 
212        i+=0; 
213        RGB_to_HSL2(ucBits[i+2],ucBits[i+1],ucBits[i],&H,&S,&L); 
214      L+=Luminance; 
215      if(L>240)L=240; 
216      else if(L<0)L=0; 
217   
218      HSL_to_RGB2(H,S,L,&R,&G,&B); 
219      ImgDest->ucBits[i]=Limit((int)B); 
220      ImgDest->ucBits[i+1]=Limit((int)G); 
221      ImgDest->ucBits[i+2]=Limit((int)R); 
222    } 
223    SetBitmapBits (ImgDest->hBmp,(Width*Height)*4,ImgDest->ucBits); 
224   
225    return 1; 
226   
227  } 

lien1 lien2 lien3