conversion.c

00001 /*
00002 *    Copyright (c) 2007. The BATI team. All right reserved.
00003 *
00004 *    This file is part of BATI library.
00005 *
00006 *    BATI library is free software: you can redistribute it and/or modify
00007 *    it under the terms of the GNU General Public License as published by
00008 *    the Free Software Foundation, either version 3 of the License, or
00009 *    (at your option) any later version.
00010 *
00011 *    BATI library  is distributed in the hope that it will be useful,
00012 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 *    GNU General Public License for more details.
00015 *
00016 *    You should have received a copy of the GNU General Public License
00017 *    along with BATI library.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 #include <stdio.h>
00021 #include <math.h>
00022 #include <ctype.h>
00023 #include "image.h"
00024 
00025 
00038 void conv_TLS_RVB(imarvb *ima_tls, imarvb *ima_rvb)
00039 {
00040         unsigned char *ima_L,*ima_T,*ima_S;
00041         int Y,X;
00042         unsigned char *ima_R,*ima_V,*ima_B;
00043 
00044 
00045         unsigned char *R,*V,*B;
00046         unsigned char *L,*T,*S;
00047         double          L_f,T_f,S_f,R_f, V_f, B_f;      
00048         double          DEUX_PI;
00049         double          C1,C2,angle;
00050         double          Fact,F_1_3,F_2_3;
00051         register        i,j;
00052         
00053         ima_T = ima_tls->r[0];
00054         ima_L = ima_tls->v[0];
00055         ima_S = ima_tls->b[0];
00056         Y = ima_tls->nr;
00057         X = ima_tls->nc;
00058         ima_R = ima_rvb->r[0];
00059         ima_V = ima_rvb->v[0];
00060         ima_B = ima_rvb->b[0];
00061 
00062         R=ima_R;
00063         V=ima_V;
00064         B=ima_B;
00065         L=ima_L;
00066         T=ima_T;
00067         S=ima_S;
00068 
00069         DEUX_PI = 2*M_PI;
00070         F_1_3  = 1.0/3.0;
00071         F_2_3  = 2.0/3.0;
00072         Fact   = sqrt(3.0)/3.0;
00073 
00074         for(i=Y;i--;)
00075                 for(j=X;j--;)
00076                         {
00077                                                 /* Modif teinte */
00078                                 if( *T > 192)
00079                                     T_f = (double)((int)*T-255);
00080                                 else
00081                                         T_f = (double)*T;
00082                         
00083                                 L_f = (double)*L;
00084                                 S_f = (double)*S;
00085 
00086                                                 /* calcul de C1 et C2 */
00087                                 angle = T_f*DEUX_PI/255.0;
00088                                 C1 = S_f*cos(angle);
00089                                 C2 = S_f*sin(angle);
00090 
00091                                                 /* calcul de R,V,B      flotant*/
00092                                 R_f = (L_f + (F_2_3)*C1);
00093                                 V_f = (L_f - (F_1_3)*C1 + Fact*C2);
00094                                 B_f = (L_f - (F_1_3)*C1 - Fact*C2);
00095                                                 /* test si valeur non negative ou > 255 */
00096                                 if (R_f <0 )
00097                                         R_f = 0;
00098                                 if (R_f > 255)
00099                                         R_f = 255;
00100                                 if (V_f <0 )
00101                                         V_f = 0;
00102                                 if (V_f > 255)
00103                                         V_f = 255;
00104                                 if (B_f <0 )
00105                                         B_f = 0;
00106                                 if (B_f > 255)
00107                                         B_f = 255;
00108                                 /*
00109                                 *R = (unsigned char)irint(R_f);
00110                                 *V = (unsigned char)irint(V_f);
00111                                 *B = (unsigned char)irint(B_f);
00112                                 */
00113                                 *R = (unsigned char)rint(R_f);
00114                                 *V = (unsigned char)rint(V_f);
00115                                 *B = (unsigned char)rint(B_f);
00116 
00117                                                 /* Passage pixel suivant */
00118                                 R++,V++,B++;
00119                                 L++,T++,S++;
00120                         }
00121 }
00122 
00123 
00124 
00136 void conv_RVB_TLS(imarvb *ima_rvb, imarvb *ima_tls)
00137 {
00138         unsigned char *ima_R,*ima_V,*ima_B;
00139         int Y,X;
00140         unsigned char *ima_L,*ima_T,*ima_S;
00141 
00142         unsigned char *R,*V,*B;
00143         unsigned char *L,*T,*S;
00144         double          DEUX_PI;
00145         double          C1,C2,calc_inter,double_T;
00146         double          Fact_T,Fact_C2;
00147         register        i,j;
00148 
00149         ima_R = ima_rvb->r[0];
00150         ima_V = ima_rvb->v[0];
00151         ima_B = ima_rvb->b[0];
00152         Y = ima_rvb->nr;
00153         X = ima_rvb->nc;
00154         ima_T = ima_tls->r[0];
00155         ima_L = ima_tls->v[0];
00156         ima_S = ima_tls->b[0];
00157 
00158         R=ima_R;
00159         V=ima_V;
00160         B=ima_B;
00161         L=ima_L;
00162         T=ima_T;
00163         S=ima_S;
00164 
00165         DEUX_PI = 2*M_PI;
00166         Fact_C2 = sqrt(3.0)/2.0;
00167         Fact_T = (double)(255.0 / DEUX_PI);     /* Norm Couleur entre 0..255 */
00168 
00169         for(i=Y;i--;)
00170                 for(j=X;j--;)
00171                         {
00172                                                 /* calcul de la luminance */
00173                                 ;
00174                                 /* *L=(unsigned char)irint(((double)*R + (double)*V  + (double)*B)/3.0 ); */
00175                                 *L=(unsigned char)rint(((double)*R + (double)*V  + (double)*B)/3.0 );
00176                                                 /* calcul facteur C1 et C2 */
00177                                 C1 = (double)(*R) - ((double)(*V) + (double)(*B)) / 2.0;
00178                                 C2 = ((double)(*V) - (double)(*B)) * Fact_C2;
00179 
00180                                                 /* Calcul de la teinte */
00181                                 if (C1 == 0.0)
00182                                 {
00183                                    if (C2 > 0)
00184                                       *T = 64;
00185                                    else
00186                                       *T = 192;
00187                                 }
00188                                 else
00189                                 {
00190                                    calc_inter = (double)C2 / (double)C1;
00191                                    if (C1 > 0.0) 
00192                                       double_T = atan(calc_inter) * Fact_T;
00193                                    else
00194                                   double_T = (M_PI + atan(calc_inter)) * Fact_T;
00195 
00196                                         if (double_T >= 0)
00197                                         {
00198                                                 if (double_T - (int)double_T < 0.4999)
00199                                                 {
00200                                                         *T = (unsigned char)((int)double_T );
00201                                                 }
00202                                                 else
00203                                                 {
00204                                                         *T = (unsigned char)((int)double_T + 1);
00205                                                 }
00206                                         }
00207                                         else
00208                                         {
00209                                                 if (double_T - (int)double_T  > 0.5)
00210                                                 {
00211                                                         *T = (unsigned char)(255 + (int)double_T + 1);
00212                                                 }
00213                                                 else
00214                                                 {
00215                                                         *T = (unsigned char)(255 + (int)double_T);
00216                                                 }
00217                         
00218                                         }
00219  
00220                                 }
00221 
00222                                                 /* Calcul de la saturation */
00223                                 calc_inter = (double)(C1) * (double)C1  + (double)(C2) * (double)C2;
00224                                 /* *S = (unsigned char)irint(sqrt(calc_inter)); */
00225                                 *S = (unsigned char)rint(sqrt(calc_inter));
00226                                                 /* Passage pixel suivant */
00227                                 R++,V++,B++;
00228                                 L++,T++,S++;
00229                         }
00230 }
00231 
00232 

Generated on Tue Apr 22 13:31:04 2008 for ima2D by  doxygen 1.5.3