lread_ima.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 /* **************************** lread_ima.c ******************************/
00021 /*                                                                       */
00022 /* ***********************************************************************/
00023 
00057 #include "image.h"
00058 #include "proto2D.h"
00059 #include "permut.h"
00060 #include <stdlib.h>
00061 #include <fcntl.h>
00062 #include <sys/types.h>
00063 #include <sys/stat.h>
00064 #include "rasterfile.h"
00065 #include <string.h>
00066 
00067 
00068 #define PERMS   0644
00069 
00070 
00071 /* *************************  LECTURE  *******************************/
00084 param *read_ima_lect(read_ima_t *des, param *ptp, char *debq){
00085   char nom[300], question[500];
00086   int l;
00087 
00088   sprintf(question, "%s nom complet du fichier image a lire", debq); 
00089   lec_param(question, ptp);
00090   strcpy(nom, ptp->rep);
00091   ptp = ptp->next;
00092 
00093   l = strlen(nom);
00094   do
00095       l--;
00096   while(nom[l] != '.'  && l>0);
00097   if( l>0 ){
00098       strncpy(des->nom, nom, l);
00099       des->nom[l] = '\0';
00100       strcpy(des->ext, nom+l+1);
00101   }
00102   else{
00103       printf("\n>> ERREUR read_ima_lect : nom image incomplet (extension?)\n");
00104       exit(1);
00105   }
00106 
00107   return(ptp);
00108 }
00109 
00110 
00111 
00112 /* *************************  INITIALISATION  ***************************/
00121 int read_imau1_init(read_ima_t *des, imau1 *ima){ 
00122   FILE *fpdim, *fp3lt;
00123   int fima;
00124   int headras[8], tmpi, erreur, fausse_couleur;
00125   char nom_ima[200], nom_dim[200], nom_ras[200], nom_3lt[200];  
00126   register int tmp_int, i, j, nb_color;
00127   pixu1 tmppix;
00128   pixu1 cmap[3][256];
00129   pixu1 *ptpix;
00130 
00131   ima->lutr = NULL;
00132   ima->lutv = NULL;
00133   ima->lutb = NULL;
00134 
00135   /*  sprintf(ima->nom, "%s.%s", des->nom, des->ext);  mis a la fin */
00136   /* format .ima+.dim+[.3lt] */
00137   
00138   if(strcmp(des->ext, "ima")== 0){  
00139       sprintf(nom_ima, "%s.ima", des->nom); 
00140       sprintf(nom_dim, "%s.dim", des->nom); 
00141       sprintf(nom_3lt, "%s.3lt", des->nom); 
00142       /* dimension */
00143       if( fpdim=fopen(nom_dim,"r") ){
00144           fscanf(fpdim,"%d %d", &ima->nc, &ima->nr );
00145           fclose(fpdim);
00146       }
00147       else{
00148           printf("\n>> ERREUR read_imau1_init : fichier %s introuvable\n",
00149                  nom_dim);
00150           exit(1);
00151       }
00152       /* donnees */
00153       if( (fima=open(nom_ima, O_RDONLY, 0)) != -1 ){
00154           alloc_imau1(ima);  
00155           for(j = 0; j < ima->nr; j++)
00156               if( read(fima, ima->p[j], ima->nc) != ima->nc ){
00157                   printf("\n>> ERREUR read_imau1_init: lecture fichier %s\n",
00158                          nom_ima);
00159                   exit(1); 
00160               }
00161           close(fima);  
00162       }
00163       else{
00164           printf("\n>> ERREUR read_imau1_init : fichier %s introuvable\n", 
00165                  nom_ima);
00166           exit(1);
00167       }  
00168       printf("\n>> read_imau1_init : image lue %s, dimX=%d, dimY=%d\n", 
00169              nom_ima, ima->nc, ima->nr);
00170       /* look up table */
00171       if( fp3lt=fopen(nom_3lt,"r") ){ /* presence de colormap */
00172           if( (ima->lutr = (pixu1*)malloc(256)) == NULL ||
00173               (ima->lutv = (pixu1*)malloc(256)) == NULL ||
00174               (ima->lutb = (pixu1*)malloc(256)) == NULL ){
00175               printf ("\nERREUR  read_imau1_init: allocation impossible\n");  
00176               exit(1);      
00177           }
00178           erreur = 0;
00179           for(j = 0; j < 256; j++)
00180               if( fscanf(fp3lt, "%d", &tmpi) == 1 )
00181                   ima->lutr[j] = (pixu1)tmpi;
00182               else
00183                   erreur = 1;
00184           for(j = 0; j < 256; j++)
00185               if( fscanf(fp3lt, "%d", &tmpi) == 1 )
00186                   ima->lutv[j] = (pixu1)tmpi;
00187               else
00188                   erreur = 1;
00189           for(j = 0; j < 256; j++)
00190               if( fscanf(fp3lt, "%d", &tmpi) == 1 )
00191                   ima->lutb[j] = (pixu1)tmpi;
00192               else
00193                   erreur = 1;
00194           fclose(fp3lt);
00195           if( erreur ){  
00196               printf ("\nATT read_imau1_init: lecture %s incomplete, LUT ignoree\n", nom_3lt);  
00197               free(ima->lutr); ima->lutr = NULL;
00198               free(ima->lutv); ima->lutv = NULL;
00199               free(ima->lutb); ima->lutb = NULL;
00200           }
00201           else
00202               printf ("\nread_imau1_init: LUT %s chargee\n",nom_3lt);  
00203       }
00204   }
00205 
00206 
00207   /* format raster 8 bit*/
00208   else if(!strcmp(des->ext, "ras")){ 
00209     sprintf(nom_ras, "%s.%s", des->nom, des->ext); 
00210       if( (fima=open(nom_ras, O_RDONLY, 0)) == -1 ){
00211           printf("\n>> ERREUR read_imau1_init : fichier %s introuvable\n", nom_ras);
00212           exit(1);
00213       }
00214       if( read(fima, (char*)headras, 32) != 32 ){
00215           printf("\n>> ERREUR read_imau1_init : lecture fichier %s \n", nom_ras);  
00216           exit(1); 
00217       }
00218 
00219 #ifdef INVERSE_DATA
00220         printf("inversion entete raster (comme des float)\n");
00221         permuteFloatTab((float *)&headras, 8);
00222 #endif
00223 
00224       
00225 /*       printf("\nFichier RASTER : \n -> headraster[0] : %x\n", headras[0]);    */
00226       if( headras[0] != 0x59a66a95 ){
00227           printf("\n>> ERREUR read_imau1_init: magic number incorrect %x!=0x59a66a95\n", 
00228                   headras[0]);
00229           exit(1);
00230       }
00231 /*       for(j = 1; j < 8; j++) */
00232 /*         printf(" -> headraster[%d] : %d\n", j, headras[j]);       */
00233       ima->nc = headras[1];
00234       ima->nr = headras[2];
00235       alloc_imau1(ima);  
00236       printf("\n>> read_imau1_init : image %s, dimX=%d, dimY=%d\n",
00237              nom_ras, ima->nc, ima->nr);
00238       if(headras[3]!=8){
00239           printf("\n>> ERREUR read_imau1_init: nb bit par pixel %d != 8", headras[3]);
00240           exit(1);
00241       }
00242       nb_color = 0;
00243       if(headras[6]){  /* presence de colormap */
00244          nb_color = headras[7]/3;
00245          printf("\nATT read_imau1_init : detection d'une LUT de %d couleurs dans %s\n",
00246                 nb_color, nom_ras);
00247          for(i=0; i<3; i++){ 
00248             if( read(fima, cmap[i], nb_color) != nb_color ){
00249                printf("\n>> ERREUR read_imau1_init: lecture LUT fichier %s \n",
00250                       nom_ras);  
00251                exit(1); 
00252             }
00253             for(j=nb_color; j<256; j++) 
00254                 cmap[i][j] = j;
00255          }
00256          fausse_couleur = 0;
00257          for(j=0; j<nb_color; j++)
00258              if(cmap[0][j] != cmap[1][j] || cmap[1][j] != cmap[2][j]){
00259                   fausse_couleur = 1;
00260                   break;
00261              }
00262          if( fausse_couleur ){
00263              if((ima->lutr = (pixu1*)malloc(256)) == NULL ||
00264                 (ima->lutv = (pixu1*)malloc(256)) == NULL ||
00265                 (ima->lutb = (pixu1*)malloc(256)) == NULL ){
00266                  printf ("\nERREUR  read_imau1_init: allocation impossible\n");  
00267                  exit(1);      
00268              }
00269              for(j = 0; j < 256; j++){
00270                  ima->lutr[j] = cmap[0][j];
00271                  ima->lutv[j] = cmap[1][j];
00272                  ima->lutb[j] = cmap[2][j];
00273              }
00274          }
00275          else
00276              printf("\n>> ATT read_imau1_init: LUT de niveau de gris utilisee comme valeur des pixels\n");
00277       }
00278       /*lecture image*/
00279       for(j = 0; j < ima->nr; j++){
00280          if( read(fima, ima->p[j], ima->nc) != ima->nc ){
00281              printf("\n>> ERREUR read_imau1_init : lecture fichier %s \n", nom_ras); 
00282              exit(1); 
00283          }
00284          if(ima->nc % 2)  /* nombre de colonnes impaire => 0 a eliminer */
00285              read(fima, &tmppix, 1);
00286       }
00287       close(fima);
00288       /* mapping des pixel par le vrai niveau de gris si colormap de type R==V==B */
00289       if (headras[6] && (!fausse_couleur)){
00290         ptpix = ima->p[0];
00291         tmp_int = ima->nc * ima->nr;
00292         for(j=0; j< tmp_int; j++, ptpix++)
00293             *ptpix = cmap[0][*ptpix];       
00294       }     
00295   }
00296   else{
00297       printf("\n>> ERREUR read_imau1_init : extension %s inconnue (.ima/.ras)\n", 
00298               des->ext);
00299       exit(1);
00300   }
00301   sprintf(ima->nom, "%s.%s", des->nom, des->ext);
00302 
00303   return(0);  
00304 }
00305 
00306 
00307 
00308 /* image 16 bit */
00309 int read_imau2_init(read_ima_t *des, imau2 *im){   /* .imw  et IMW     */
00310   FILE *fpdim;
00311   int fima;
00312   char nom_ima[200], nom_dim[200];
00313   register int  j, nb_oct;
00314 
00315   sprintf(nom_ima, "%s.%s", des->nom, des->ext);
00316   /*  strcpy(im->nom, nom_ima); mis a la fin */
00317   sprintf(nom_dim, "%s.dim", des->nom);   
00318   if(fpdim=fopen(nom_dim,"r")){
00319       fscanf(fpdim,"%d %d", &im->nc, &im->nr);
00320       fclose(fpdim);
00321   }
00322   else{
00323       printf("\n>> ERREUR read_imau2_init : fichier %s introuvable\n", nom_dim);
00324       exit(1);
00325   } 
00326   if((fima=open(nom_ima, O_RDONLY, 0)) != -1) {
00327       alloc_imau2(im);
00328       nb_oct = 2*im->nc;
00329       for(j = 0; j < im->nr; j++)
00330          if( read(fima, (char *)im->p[j], nb_oct) != nb_oct ){
00331              printf("\n>> ERREUR read_imau2_init :lecture fichier %s \n", nom_ima);
00332              exit(1);
00333          }
00334       close(fima);
00335 #ifdef INVERSE_DATA
00336       if(!strcmp(des->ext, "imw")){
00337         printf("Swap des octets des donnees lors de la lecture\n");
00338         for( j=0; j<im->nr; j++)
00339         permuteShortTab( (short *)im->p[j], im->nc);
00340       }
00341       else if(!strcmp(des->ext, "IMW")){
00342           printf("Pas de swap des octets lors de la lecture car fichier .IMW (INTEL)\n");
00343       }
00344       else{
00345           printf("\n extension %s inconnue\n", des->ext);
00346           exit(0);
00347       } 
00348 #endif
00349 
00350   
00351   }
00352   else{
00353       printf("\n>> ERREUR read_imau2_init : Fichier %s introuvable\n", nom_ima);
00354       exit(1);
00355   }
00356   printf("\n>> read_imau2_init : image %s dimX=%d, dimY=%d\n", 
00357          nom_ima, im->nc, im->nr);
00358   strcpy(im->nom, nom_ima);
00359   return(0);
00360 }
00361 
00362 /***************************************************/
00363 /* image 32 bit */
00364 int read_imau4_init(read_ima_t *des, imau4 *im){   /* .iml  et IML     */
00365   FILE *fpdim;
00366   int fima;
00367   char nom_ima[200], nom_dim[200];
00368   register int  j, nb_oct;
00369 
00370   sprintf(nom_ima, "%s.%s", des->nom, des->ext);
00371   /*  strcpy(im->nom, nom_ima); mis a la fin */
00372   sprintf(nom_dim, "%s.dim", des->nom);   
00373   if(fpdim=fopen(nom_dim,"r")){
00374       fscanf(fpdim,"%d %d", &im->nc, &im->nr);
00375       fclose(fpdim);
00376   }
00377   else{
00378       printf("\n>> ERREUR read_imau4_init : fichier %s introuvable\n", nom_dim);
00379       exit(1);
00380   } 
00381   if((fima=open(nom_ima, O_RDONLY, 0)) != -1) {
00382       alloc_imau4(im);
00383       nb_oct = 4*im->nc;
00384       for(j = 0; j < im->nr; j++)
00385          if( read(fima, (char *)im->p[j], nb_oct) != nb_oct ){
00386              printf("\n>> ERREUR read_imau4_init :lecture fichier %s \n", nom_ima);
00387              exit(1);
00388          }
00389       close(fima);
00390 #ifdef INVERSE_DATA
00391       if(!strcmp(des->ext, "iml")){
00392         printf("Swap des octets des donnees lors de la lecture\n");
00393         for( j=0; j<im->nr; j++)
00394         permuteIntTab( (unsigned int *)im->p[j], im->nc);
00395       }
00396       else if(!strcmp(des->ext, "IML")){
00397           printf("Pas de swap des octets lors de la lecture car fichier .IML (INTEL)\n");
00398       }
00399       else{
00400           printf("\n extension %s inconnue\n", des->ext);
00401           exit(0);
00402       } 
00403 #endif
00404 
00405   
00406   }
00407   else{
00408       printf("\n>> ERREUR read_imau4_init : Fichier %s introuvable\n", nom_ima);
00409       exit(1);
00410   }
00411   printf("\n>> read_imau4_init : image %s dimX=%d, dimY=%d\n", 
00412          nom_ima, im->nc, im->nr);
00413   strcpy(im->nom, nom_ima);
00414   return(0);
00415 }
00416 
00417 /***************************************************/
00418 /* image float */
00419 int read_imafl_init(read_ima_t *des, imafl *im){   /* .imf et IMF    */
00420   FILE *fpdim;
00421   int fima;
00422   char nom_ima[200], nom_dim[200];
00423   register int  j, nb_oct;
00424 
00425   sprintf(nom_ima, "%s.%s", des->nom, des->ext);
00426   /* strcpy(im->nom, nom_ima);  plus loin */ 
00427   sprintf(nom_dim, "%s.dim", des->nom);
00428   if(fpdim=fopen(nom_dim,"r")){
00429       fscanf(fpdim,"%d %d", &im->nc, &im->nr);
00430       fclose(fpdim);
00431   }
00432   else{
00433       printf("\n>> ERREUR read_imafl_init : fichier %s introuvable\n", nom_dim);
00434       exit(1);
00435   } 
00436   if((fima=open(nom_ima, O_RDONLY, 0)) != -1) {
00437       alloc_imafl(im);
00438       nb_oct = 4*im->nc;
00439       for(j = 0; j < im->nr; j++)
00440          if( read(fima, (char *)im->p[j], nb_oct) != nb_oct ){
00441              printf("\n>> ERREUR read_imafl_init :lecture fichier %s \n", nom_ima);
00442              exit(1);
00443          }
00444       close(fima);  
00445   
00446 #ifdef INVERSE_DATA
00447 
00448         if(!strcmp(des->ext, "imf")){
00449           printf("Swap des octets des donnees lors de la lecture\n");
00450           for( j=0; j<im->nr; j++)
00451                 permuteFloatTab( im->p[j], im->nc);
00452         }
00453         else if(!strcmp(des->ext, "IMF")){
00454           printf("Pas de swap des octets lors de la lecture car fichier .IMF (INTEL)\n");
00455         }
00456         else{
00457           printf("\n extension %s inconnue\n", des->ext);
00458           exit(0);
00459         } 
00460 #endif
00461   
00462   }
00463   else{
00464       printf("\n>> ERREUR read_imafl_init : Fichier %s introuvable\n", nom_ima);
00465       exit(1);
00466   }
00467   printf("\n>> read_imafl_init : image %s dimX=%d, dimY=%d\n", 
00468          nom_ima, im->nc, im->nr);
00469   strcpy(im->nom, nom_ima);
00470   return(0);
00471 }
00472 
00473 /* image complexe short */
00474 int read_imacx4_init(read_ima_t *des, imacx4 *im){ /* .cxs  ou CXS     */
00475   FILE *fpdim;
00476   int fima;
00477   char nom_ima[200], nom_dim[200];
00478   register int  j, nb_oct;
00479 
00480   sprintf(nom_ima, "%s.%s", des->nom, des->ext);
00481   /* strcpy(im->nom, nom_ima);  plus loin */
00482   sprintf(nom_dim, "%s.dim", des->nom);
00483   if(fpdim=fopen(nom_dim,"r")){
00484       fscanf(fpdim,"%d %d", &im->nc, &im->nr);
00485       fclose(fpdim);
00486   }
00487   else{
00488       printf("\n>> ERREUR read_imacx4_init : fichier %s introuvable\n", nom_dim);
00489       exit(1);
00490   } 
00491   if((fima=open(nom_ima, O_RDONLY, 0)) != -1) {
00492       alloc_imacx4(im);
00493       nb_oct = 4*im->nc;
00494       for(j = 0; j < im->nr; j++)
00495          if( read(fima, (char *)im->p[j], nb_oct) != nb_oct ){
00496              printf("\n>> ERREUR read_imacx4_init :lecture fichier %s \n", nom_ima);
00497              exit(1);
00498          }
00499       close(fima); 
00500 
00501 #ifdef INVERSE_DATA
00502         if(!strcmp(des->ext, "cxs")){
00503           printf("Swap des octets des donnees lors de la lecture\n");
00504           for( j=0; j<im->nr; j++)
00505             permuteShortTab( (short *)&im->p[j][0].re, 2*im->nc);
00506         }
00507         else if(!strcmp(des->ext, "CXS")){
00508           printf("Pas de swap des octets lors de la lecture car fichier .CXS (INTEL)\n");
00509         }
00510         else{
00511           printf("\n extension %s inconnue\n", des->ext);
00512           exit(0);
00513         } 
00514 #endif
00515 
00516  
00517   }
00518   else{
00519       printf("\n>> ERREUR read_imacx4_init : Fichier %s introuvable\n", nom_ima);
00520       exit(1);
00521   }
00522   printf("\n>> read_imacx4_init : image %s dimX=%d, dimY=%d\n", 
00523          nom_ima, im->nc, im->nr);
00524   strcpy(im->nom, nom_ima);
00525   return(0);
00526 }
00527 
00528 /* image complexe float */
00529 int read_imacx8_init(read_ima_t *des, imacx8 *im){ /* .cxf et CXF    */
00530   FILE *fpdim;
00531   int fima;
00532   char nom_ima[200], nom_dim[200];
00533   register int  j, nb_oct;
00534 
00535   sprintf(nom_ima, "%s.%s", des->nom, des->ext);
00536   /*   strcpy(im->nom, nom_ima);  plus loin  */
00537   sprintf(nom_dim, "%s.dim", des->nom);
00538   if(fpdim=fopen(nom_dim,"r")){
00539       fscanf(fpdim,"%d %d", &im->nc, &im->nr);
00540       fclose(fpdim);
00541   }
00542   else{
00543       printf("\n>> ERREUR read_imacx8_init : fichier %s introuvable\n", nom_dim);
00544       exit(1);
00545   } 
00546   if((fima=open(nom_ima, O_RDONLY, 0)) != -1) {
00547       alloc_imacx8(im);
00548       nb_oct = 8*im->nc;
00549       for(j = 0; j < im->nr; j++)
00550          if( read(fima, (char *)im->p[j], nb_oct) != nb_oct ){
00551              printf("\n>> ERREUR read_imacx8_init :lecture fichier %s \n", nom_ima);
00552              exit(1);
00553          }
00554       close(fima);
00555 #ifdef INVERSE_DATA
00556 
00557         if(!strcmp(des->ext, "cxf")){
00558           printf("Swap des octets des donnees lors de la lecture\n");
00559           for( j=0; j<im->nr; j++)
00560             permuteFloatTab( &im->p[j][0].re, 2*im->nc);
00561         }
00562         else if(!strcmp(des->ext, "CXF")){
00563           printf("Pas de swap des octets lors de la lecture car fichier .CXF (INTEL)\n");
00564         }
00565         else{
00566           printf("\n extension %s inconnue\n", des->ext);
00567           exit(0);
00568         } 
00569 #endif 
00570   
00571   }
00572   else{
00573       printf("\n>> ERREUR read_imacx8_init : Fichier %s introuvable\n", nom_ima);
00574       exit(1);
00575   }
00576   printf("\n>> read_imacx8_init : image %s dimX=%d, dimY=%d\n", 
00577          nom_ima, im->nc, im->nr);
00578   strcpy(im->nom, nom_ima);
00579   return(0);
00580 }
00581 
00582 /******************************************/
00583 /* image couleur */
00584 int read_imarvb_init(read_ima_t *des, imarvb *im){ /* .ras       */
00585   FILE *fichier;
00586   char nom_ima[200];
00587   struct rasterfile entete;
00588   register int l, c;
00589   unsigned char nb_col_impaire, tampon;
00590 
00591   sprintf(nom_ima, "%s.%s", des->nom, des->ext);
00592   if((fichier=fopen(nom_ima,"r")) == NULL) {
00593       printf("ERREUR: impossible d'ouvrir le fichier %s\n", nom_ima);
00594       exit(1);
00595   }
00596 
00597   /*Lecture entete */
00598   fread(&entete, 1, sizeof(struct rasterfile), fichier);
00599 #ifdef INVERSE_DATA
00600     printf("inversion entete raster image rvb\n");
00601     permuteFloatTab((float *)&entete, 8);
00602 #endif
00603 
00604   if(entete.ras_depth != 24){
00605       printf("ERREUR: image pas au format 24 bits");
00606       exit(1);
00607   }
00608   if(entete.ras_maplength != 0){
00609       printf("cette image contient une palette, non suporte actuellement\n");
00610       exit(1);
00611   }
00612   im->nc = entete.ras_width;
00613   im->nr = entete.ras_height;
00614   nb_col_impaire = im->nc%2;
00615   
00616   alloc_imarvb(im);
00617   /*
00618   printf("ETAPE LECTURE\n");  
00619   printf("entete.ras_width=%d\n",entete.ras_width);
00620   printf("entete.ras_height=%d\n",entete.ras_height);
00621   printf("entete.ras_depth=%d\n",entete.ras_depth);
00622   printf("entete.ras_length=%d\n",entete.ras_length);
00623   printf("entete.ras_type=%d\n",entete.ras_type);
00624   printf("entete.ras_maptype=%d\n",entete.ras_maptype);
00625   printf("entete.ras_maplength=%d\n",entete.ras_maplength);
00626   */
00627   /* format RVB ou BVR */
00628   if(entete.ras_type == RT_FORMAT_RGB)
00629       {
00630           for(l=0; l<im->nr; l++) {
00631               for(c=0; c<im->nc; c++) {
00632                   fread( &im->r[l][c], 1, sizeof(pixu1), fichier);
00633                   fread( &im->v[l][c], 1, sizeof(pixu1), fichier);
00634                   fread( &im->b[l][c], 1, sizeof(pixu1), fichier);
00635               }
00636               if(nb_col_impaire) {
00637                   fread( &tampon, 1, sizeof(pixu1), fichier);
00638                   fread( &tampon, 1, sizeof(pixu1), fichier);
00639                   fread( &tampon, 1, sizeof(pixu1), fichier);
00640               }
00641           }
00642       }
00643   else
00644       {
00645           if(entete.ras_type == RT_STANDARD)
00646               {
00647                   for(l=0; l<im->nr; l++) {
00648                       for(c=0; c<im->nc; c++) {
00649                           fread( &im->b[l][c], 1, sizeof(pixu1), fichier);
00650                           fread( &im->v[l][c], 1, sizeof(pixu1), fichier);
00651                           fread( &im->r[l][c], 1, sizeof(pixu1), fichier);
00652                         }
00653                       if(nb_col_impaire) {
00654                           fread( &tampon, 1, sizeof(pixu1), fichier);
00655                       }
00656                   }
00657               }
00658       }
00659   fclose(fichier);
00660   strcpy(im->nom, nom_ima);
00661   return(0);
00662 }
00663 /* *************************  CALCUL (aucun) ***********************/
00664 
00665 
00666 
00667 
00668 
00669 
00670 
00671 
00672 /* ********************* Op�ateur d'ajout d'une colormap  ***********************/
00693 /* *************************  LECTURE  *******************************/
00704 param * add_colormap_lect(add_colormap_t *des, param * ptp, char * debq){
00705   char question[500];
00706 
00707   sprintf(question, "%s Nom du fichier contenant la colormap", debq);
00708   lec_param(question, ptp);
00709   des->nom = ptp->rep;
00710   ptp = ptp->next;
00711 
00712   return(ptp);
00713 }
00714 
00722 int add_colormap_init(add_colormap_t *des)
00723 {
00724     FILE *map;
00725     int tmp, erreur=0, j;
00726 
00727     /*ouverture fichier colormap*/
00728     if( (map=fopen(des->nom, "r"))!=NULL ){
00729 
00730         /*allocation des luts*/
00731         if( (des->lutr = (pixu1*)malloc(256)) == NULL ||
00732             (des->lutv = (pixu1*)malloc(256)) == NULL ||
00733             (des->lutb = (pixu1*)malloc(256)) == NULL ){
00734           printf ("add_colormap_init: ERREUR allocation impossible\n");  
00735           exit(1); 
00736         }
00737 
00738         /*lecture de la colormap*/
00739         for(j = 0; j < 256; j++)
00740           if( fscanf(map, "%d", &tmp) == 1 )
00741             des->lutr[j] = (pixu1)tmp;
00742           else
00743             erreur = 1;
00744         for(j = 0; j < 256; j++)
00745           if( fscanf(map, "%d", &tmp) == 1 )
00746             des->lutv[j] = (pixu1)tmp;
00747           else
00748             erreur = 1;
00749         for(j = 0; j < 256; j++)
00750           if( fscanf(map, "%d", &tmp) == 1 )
00751             des->lutb[j] = (pixu1)tmp;
00752           else
00753             erreur = 1;
00754 
00755         if( erreur ){  
00756           printf ("add_colormap_init: ATTENTION colormap %s incomplete, LUT ignoree\n", des->nom);  
00757           free(des->lutr); des->lutr = NULL;
00758           free(des->lutv); des->lutv = NULL;
00759           free(des->lutb); des->lutb = NULL;
00760         }    
00761         else
00762           printf ("add_colormap_init: LUT %s chargee\n", des->nom);
00763 
00764         fclose(map);
00765     }
00766     else{
00767       /* si fichier non ouvert, annule modif colormap */
00768       printf("add_colormap_init: ATTENTION, impossible d'ouvrir le fichier %s\n", des->nom);
00769       des->lutr = NULL;
00770       des->lutv = NULL;
00771       des->lutb = NULL;
00772     }
00773 
00774   
00775 }
00776 
00785 int add_colormap_calc(add_colormap_t *des, imau1 *ima){
00786 
00787     if( ima->lutr!=NULL && ima->lutv!=NULL && ima->lutb!=NULL ){
00788         free(ima->lutr);
00789         free(ima->lutv);
00790         free(ima->lutb);
00791     }           
00792     ima->lutr = des->lutr;
00793     ima->lutv = des->lutv;
00794     ima->lutb = des->lutb;
00795 }
00796 
00797 
00798 
00806 int alloc_lut_u1(imau1 *im)
00807 {
00808    if( im->lutr!=NULL && im->lutv!=NULL && im->lutb!=NULL ){
00809         free(im->lutr);
00810         free(im->lutv);
00811         free(im->lutb);
00812     }    
00813   
00814    /*allocation des luts*/
00815         if( (im->lutr = (pixu1*)malloc(256 * sizeof(pixu1))) == NULL ||
00816             (im->lutv = (pixu1*)malloc(256 * sizeof(pixu1))) == NULL ||
00817             (im->lutb = (pixu1*)malloc(256 * sizeof(pixu1))) == NULL )
00818           {
00819           printf ("alloc_lut_u1: lutr, lutv, lutb: ERREUR allocation impossible\n");  
00820           exit(1); 
00821           }
00822 }
00823 
00824 
00825 
00833 int free_lut_u1(imau1 *im)
00834 {
00835     if(im->lutb!=NULL){
00836         free(im->lutb); im->lutb = NULL;
00837     }
00838     if(im->lutv){
00839         free(im->lutv); im->lutv = NULL;
00840     }
00841     if(im->lutr){
00842         free(im->lutr); im->lutr = NULL;
00843     }
00844   
00845 }
00846 
00847 
00848 

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