RGBtoGRAY.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  #include "../CImage.h" 
23   
24  //Convertion d'une image couleur en une image en niveau ed gris 
25  bool CImage::ConvertRGB2Gray(CImage *ImgDest) 
26  { 
27    if(ImgType==GRAY)return 0; 
28    if(hBmp==0){ 
29      MessageBox(NULL,"ConvertRGB2Gray : L'image source est vide", 
30        NULL,MB_OK|MB_ICONWARNING); 
31      return 0; 
32    } 
33   
34    if(ImgDest!=0 && ImgDest!=this) 
35      ImgDest->Copy(this); 
36   
37    if(ImgDest==0) 
38      ImgDest=this; 
39   
40    ImgDest->ImgType=GRAY; 
41    //Pour tous les pixels, la nouvelle valeur est la moyenne des trois composantes RGB 
42    for(int i=0;i<Width*Height*4;i+=4) 
43    { 
44      ImgDest->ucBits[i]=(ucBits[i]+ucBits[i+1]+ucBits[i+2])/3;; 
45      ImgDest->ucBits[i+1]=ImgDest->ucBits[i]; 
46      ImgDest->ucBits[i+2]=ImgDest->ucBits[i]; 
47    } 
48   
49    SetBitmapBits (ImgDest->hBmp,(Width*Height)*4,ImgDest->ucBits); 
50    return true; 
51  } 

lien1 lien2 lien3