lwrite_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 /****************************** lwrite_ima.c *****************************/
00020 /*                                                                       */
00021 /*************************************************************************/
00022 
00055 #include "image.h"
00056 #include "proto2D.h"
00057 #include "rasterfile.h"
00058 #include "permut.h"
00059 #include <stdlib.h>
00060 #include <fcntl.h>
00061 #include <sys/types.h>
00062 #include <sys/stat.h>
00063 #include <string.h>
00064 
00065 
00066 #define PERMS   0644
00067 
00068 /* *************************  LECTURE  *******************************/
00080 param *write_ima_lect(write_ima_t *des, param *ptp, char *debq){
00081   char nom[300], question[500];
00082   int l;
00083 
00084   sprintf(question, "%s nom complet du fichier image a ecrire", debq); 
00085   lec_param(question, ptp);
00086   strcpy(nom, ptp->rep);
00087   ptp = ptp->next;
00088 
00089   l = strlen(nom);
00090   do
00091       l--;
00092   while(nom[l] != '.'  && l>0);
00093   if( l>0 ){
00094       strncpy(des->nom, nom, l);
00095       des->nom[l] = '\0';
00096       strcpy(des->ext, nom+l+1);
00097   }
00098   else{
00099       printf("\n>> ERREUR write_ima_lect : nom image incomplet (extension?)\n");
00100       exit(1);
00101   }
00102   return(ptp);
00103 }
00104 
00105 
00106 /* *************************  INITIALISATION  ***************************/
00114 int write_ima_init(write_ima_t *des){ 
00115   char nom_ima[200], nom_dim[200];
00116 
00117   sprintf(nom_ima, "%s.%s", des->nom, des->ext);
00118   sprintf(nom_dim, "%s.dim", des->nom);
00119 
00120   if(strcmp(des->ext, "ima")== 0 ||
00121      strcmp(des->ext, "imw")== 0 ||
00122      strcmp(des->ext, "iml")== 0 ||
00123      strcmp(des->ext, "imf")== 0 ||
00124      strcmp(des->ext, "cxs")== 0 ||
00125      strcmp(des->ext, "cxf")== 0 ){
00126       if( (des->pfd=fopen(nom_dim,"w")) == NULL ){
00127           printf("\n>> ERREUR write_ima_init: creation fichier %s impossible\n",nom_dim);
00128           exit(1);
00129       }
00130       if( (des->dfi=creat(nom_ima, PERMS)) == -1 ){
00131           printf("\n>> ERREUR write_ima_init: creation fichier %s impossible\n",nom_ima);
00132           exit(1);
00133       }  
00134   }
00135   else if(strcmp(des->ext, "ras")== 0){
00136       if( (des->pfd=fopen(nom_ima,"w")) == NULL ){
00137           printf("\n>> ERREUR write_ima_init: creation fichier %s impossible\n",nom_dim);
00138           exit(1);
00139       }
00140   }
00141   else{
00142     printf("\n Erreur : %s extension inconnue",des->ext);
00143     exit(1);
00144   }
00145   return(0);  
00146 }
00147 
00148 
00149 /* *************************  CALCUL (aucun) ***********************/
00150 
00151 
00152 /* *************************  FERMETURE  ***************************/
00162 int write_imau1_ferm(write_ima_t *des, imau1 im){   /* .ima , .ras */
00163   int j;
00164   char nom_ras[200], nom_lut[200];
00165   int l,c, tmp;
00166   struct rasterfile entete;
00167   unsigned char colormap[768], nb_col_impaire, tampon=0;
00168   FILE *fid;
00169     
00170   /* format .ima+.dim */
00171   if(strcmp(des->ext, "ima")== 0){
00172       /*ecriture des dimensions dans .dim*/
00173       fprintf(des->pfd,"%d %d", im.nc, im.nr);
00174       fclose(des->pfd);
00175 
00176       /*ecriture du fichier palette si necesaire*/      
00177       if( im.lutr!=NULL && im.lutv!=NULL && im.lutb!=NULL) {
00178           sprintf(nom_lut, "%s.%s", des->nom, "3lt"); 
00179           printf("write_ima_ferm: sauvegarde palette dans %s\n", nom_lut);
00180           if( (des->pfd=fopen(nom_lut,"w")) == NULL ){ 
00181               printf("\n>> ERREUR write_ima_ferm: creation fichier %s impossible\n",nom_lut); 
00182               exit(1); 
00183           } 
00184           fprintf(des->pfd,"%3d ",im.lutr[0]);
00185           for(j = 1; j < 256; j++) {
00186               if((j % 20) == 0 ) 
00187                   fprintf(des->pfd,"\n");
00188               fprintf(des->pfd,"%3d ", im.lutr[j]);
00189           } 
00190           fprintf(des->pfd,"\n"); 
00191 
00192           fprintf(des->pfd,"%3d ",im.lutv[0]);
00193           for(j = 1; j < 256; j++) {
00194               if((j % 20) == 0 ) 
00195                   fprintf(des->pfd,"\n");
00196               fprintf(des->pfd,"%3d ", im.lutv[j]);
00197           } 
00198           fprintf(des->pfd,"\n"); 
00199 
00200           fprintf(des->pfd,"%3d ",im.lutb[0]);
00201           for(j = 1; j < 256; j++) {
00202               if((j % 20) == 0 ) 
00203                   fprintf(des->pfd,"\n");
00204               fprintf(des->pfd,"%3d ", im.lutb[j]); 
00205           }
00206           fprintf(des->pfd,"\n"); 
00207       }
00208      
00209 
00210       /*ecriture image*/
00211       lseek(des->dfi, 0L, 0);
00212       for(j = 0; j < im.nr; j++)                                
00213           if( write(des->dfi, im.p[j], im.nc) != im.nc ){ 
00214               printf("\n>> ERREUR write_imau1_ferm : ecriture du fichier %s.%s\n",
00215                      des->nom, des->ext); 
00216               exit(1); 
00217           } 
00218       
00219   }
00220   /* format .ras */
00221   else  if(strcmp(des->ext, "ras")== 0){ 
00222         nb_col_impaire=im.nc%2;
00223         entete.ras_magic=RAS_MAGIC;
00224         entete.ras_width=im.nc;
00225         entete.ras_height=im.nr;
00226         entete.ras_depth=8;     
00227         entete.ras_length=im.nr*(im.nc+nb_col_impaire);
00228         entete.ras_type=1;
00229         /*cas ecriture avec palette*/
00230         if(im.lutr!=NULL && im.lutv!=NULL && im.lutb!=NULL){
00231           entete.ras_maptype=1;
00232           entete.ras_maplength=768;
00233         }
00234         /*cas ecriture sans palette*/
00235         else {
00236           entete.ras_maptype=0;
00237           entete.ras_maplength=0;
00238         }
00239          
00240         /* ecriture de l'entete*/
00241 #ifdef INVERSE_DATA
00242         printf("inversion entete raster comme float\n");
00243         permuteFloatTab((float *)&entete, 8);
00244 #endif
00245         fwrite( (char *)&entete,sizeof(struct rasterfile), 1, des->pfd);
00246 
00247         /*ecriture de la palette si elle existe*/       
00248         if( im.lutr!=NULL && im.lutv!=NULL && im.lutb!=NULL) {
00249             fwrite( im.lutr, sizeof(unsigned char), 256, des->pfd); 
00250             fwrite( im.lutv, sizeof(unsigned char), 256, des->pfd); 
00251             fwrite( im.lutb, sizeof(unsigned char), 256, des->pfd); 
00252         } 
00253 
00254         /*ecriture des valeurs de l'image*/
00255         for(l=0; l<im.nr; l++)
00256             {
00257             for (c=0;c<im.nc;c++)
00258                 fwrite( &im.p[l][c], sizeof(unsigned char), 1, des->pfd);
00259             if (nb_col_impaire)
00260                fwrite( &tampon, 1, 1, des->pfd);
00261             }
00262         fclose(des->pfd);
00263   }
00264   return(0);
00265 }
00266 
00267 
00268 
00269 /* image 16 bit */
00270 int write_imau2_ferm(write_ima_t *des, imau2 im){   /* .imw       */
00271   int j, nb_oct;
00272       
00273   nb_oct = 2*im.nc;
00274   fprintf(des->pfd,"%d %d", im.nc, im.nr);
00275   fclose(des->pfd);
00276   lseek(des->dfi, 0L, 0);
00277 
00278 #ifdef INVERSE_DATA
00279         printf("Swap des octets des donnees pour ecriture sur fichier\n");
00280         for( j=0; j<im.nr; j++)
00281                 permuteShortTab( (short *)im.p[j], im.nc);
00282 #endif
00283 
00284   for(j = 0; j < im.nr; j++)                            
00285       if( write(des->dfi, (char *)im.p[j], nb_oct) != nb_oct){ 
00286           printf("\n>> ERREUR write_imau2_ferm : ecriture du fichier %s.%s\n",
00287                  des->nom, des->ext); 
00288           exit(1); 
00289       }
00290 #ifdef INVERSE_DATA
00291   //printf("Swap des octets     des donnees pour ecriture sur fichier\n");
00292         for( j=0; j<im.nr; j++)
00293                 permuteShortTab( (short *)im.p[j], im.nc);
00294 #endif
00295   return(0);
00296 }
00297 
00298 
00299 
00300 
00301 /* image 32 bit */
00302  int write_imau4_ferm(write_ima_t *des, imau4 im)
00303 {   /* .imw       */
00304    int j, nb_oct;
00305 
00306    nb_oct = 4*im.nc;
00307    fprintf(des->pfd,"%d %d", im.nc, im.nr);
00308    fclose(des->pfd);
00309    lseek(des->dfi, 0L, 0);
00310  
00311 #ifdef INVERSE_DATA
00312         printf("Swap des octets des donnees pour ecriture sur fichier\n");
00313         for( j=0; j<im.nr; j++)
00314                 permuteIntTab( im.p[j], im.nc);
00315 #endif
00316  
00317    for(j = 0; j < im.nr; j++)                            
00318        if( write(des->dfi, (char *)im.p[j], nb_oct) != nb_oct){ 
00319           printf("\n>> ERREUR write_imau4_ferm : ecriture du fichier %s.%s\n",
00320                   des->nom, des->ext); 
00321            exit(1); 
00322        }
00323 #ifdef INVERSE_DATA
00324    //printf("Swap des octets     des donnees pour ecriture sur fichier\n");
00325      for( j=0; j<im.nr; j++)
00326                 permuteIntTab(im.p[j], im.nc);
00327 #endif
00328    return(0);
00329  }
00330 
00331 
00332 
00333 
00334 
00335 
00336 /* image float */
00337 int write_imafl_ferm(write_ima_t *des, imafl im){   /* .imf       */
00338   int j, nb_oct;
00339       
00340   nb_oct = 4*im.nc;
00341   fprintf(des->pfd,"%d %d", im.nc, im.nr);
00342   fclose(des->pfd);
00343   lseek(des->dfi, 0L, 0);
00344 
00345 
00346 #ifdef INVERSE_DATA
00347         printf("Swap des octets des donnees pour ecriture sur fichier\n");
00348         for( j=0; j<im.nr; j++)
00349                 permuteFloatTab( im.p[j], im.nc);
00350 #endif
00351   
00352   for(j = 0; j < im.nr; j++)                            
00353       if( write(des->dfi, (char *)im.p[j], nb_oct) != nb_oct){ 
00354           printf("\n>> ERREUR write_imafl_ferm : ecriture du fichier %s.%s\n",
00355                  des->nom, des->ext); 
00356           exit(1); 
00357       }
00358 
00359 
00360 #ifdef INVERSE_DATA
00361         //printf("re Swap des octets des donnees float pour traitement ult�ieur\n");
00362         for( j=0; j<im.nr; j++)
00363                 permuteFloatTab( im.p[j], im.nc);
00364 #endif
00365   
00366   return(0);
00367 }
00368 
00369 /* image complex short */
00370 int write_imacx4_ferm(write_ima_t *des, imacx4 im){ /* .cxs       */
00371   int j, nb_oct;
00372       
00373   nb_oct = 4*im.nc;
00374   fprintf(des->pfd,"%d %d", im.nc, im.nr);
00375   fclose(des->pfd);
00376   lseek(des->dfi, 0L, 0);
00377 
00378 #ifdef INVERSE_DATA
00379         printf("Swap des octets des donnees pour ecriture sur fichier\n");
00380         for( j=0; j<im.nr; j++)
00381                 permuteShortTab( (short *)&im.p[j][0].re, 2*im.nc);
00382 #endif
00383   for(j = 0; j < im.nr; j++)                            
00384       if( write(des->dfi, (char *)im.p[j], nb_oct) != nb_oct){ 
00385           printf("\n>> ERREUR write_imacx4_ferm : ecriture du fichier %s.%s\n",
00386                  des->nom, des->ext); 
00387           exit(1); 
00388       }
00389 #ifdef INVERSE_DATA
00390   //printf("Swap des octets     des donnees pour ecriture sur fichier\n");
00391         for( j=0; j<im.nr; j++)
00392                 permuteShortTab( (short *)&im.p[j][0].re, 2*im.nc);
00393 #endif
00394   return(0);
00395 }
00396 
00397 /* image complex float */
00398 int write_imacx8_ferm(write_ima_t *des, imacx8 im){ /* .cxf       */
00399   int j, nb_oct;
00400       
00401   nb_oct = 8*im.nc;
00402   fprintf(des->pfd,"%d %d", im.nc, im.nr);
00403   fclose(des->pfd);
00404   lseek(des->dfi, 0L, 0);
00405 #ifdef INVERSE_DATA
00406         printf("Swap des octets des donnees pour ecriture sur fichier\n");
00407         for( j=0; j<im.nr; j++)
00408                 permuteFloatTab( (float *)im.p[j], 2*im.nc);
00409 #endif
00410     for(j = 0; j < im.nr; j++)                          
00411       if( write(des->dfi, (char *)im.p[j], nb_oct) != nb_oct){ 
00412           printf("\n>> ERREUR write_imacx8_ferm : ecriture du fichier %s.%s\n",
00413                  des->nom, des->ext); 
00414           exit(1); 
00415       }
00416 #ifdef INVERSE_DATA
00417     //printf("Swap des octets   des donnees pour ecriture sur fichier\n");
00418         for( j=0; j<im.nr; j++)
00419                 permuteFloatTab( (float *)im.p[j], 2*im.nc);
00420 #endif
00421   return(0);
00422 }
00423 
00424 /* image rvb */
00425 int write_imarvb_ferm(write_ima_t *des, imarvb im){   /* .ras       */
00426     struct rasterfile entete;
00427     register int l, c;
00428     unsigned char nb_col_impaire, tampon=0;
00429 
00430     nb_col_impaire = im.nc%2;
00431     entete.ras_magic = RAS_MAGIC;
00432     entete.ras_width = im.nc;
00433     entete.ras_height = im.nr;
00434     entete.ras_depth = 24;
00435     /*entete.ras_length = im.nr*(im.nc+nb_col_impaire)*3;*/
00436     entete.ras_length = im.nr*(im.nc*3+nb_col_impaire);
00437     /*entete.ras_type = RT_FORMAT_RGB;*/
00438     entete.ras_type = RT_STANDARD;
00439     entete.ras_maptype = RMT_NONE;
00440     entete.ras_maplength = 0;
00441     /*
00442     printf("ETAPE ECRITURE\n");
00443     printf("entete.ras_width=%d\n",entete.ras_width);
00444     printf("entete.ras_height=%d\n",entete.ras_height);
00445     printf("entete.ras_depth=%d\n",entete.ras_depth);
00446     printf("entete.ras_length=%d\n",entete.ras_length);
00447     printf("entete.ras_type=%d\n",entete.ras_type);
00448     printf("entete.ras_maptype=%d\n",entete.ras_maptype);
00449     printf("entete.ras_maplength=%d\n",entete.ras_maplength);
00450     */
00451 
00452 #ifdef INVERSE_DATA
00453     printf("inversion entete raster image rvb\n");
00454     permuteFloatTab((float *)&entete, 8);
00455 #endif
00456                     
00457     /*Ecriture entete */
00458     fwrite(&entete,1,sizeof(struct rasterfile),des->pfd);
00459     /*printf("essai ecriture\n");*/
00460 
00461     /* Ecriture image en BVR*/
00462     for(l=0; l<im.nr; l++)
00463         {
00464             for(c=0; c<im.nc; c++){
00465                     fwrite( &im.b[l][c], 1, sizeof(pixu1), des->pfd);
00466                     fwrite( &im.v[l][c], 1, sizeof(pixu1), des->pfd);
00467                     fwrite( &im.r[l][c], 1, sizeof(pixu1), des->pfd);
00468             }
00469             if(nb_col_impaire) {
00470                 fwrite( &tampon, 1, sizeof(pixu1), des->pfd);
00471                 /*fwrite( &tampon, 1, sizeof(pixu1), des->pfd);
00472                 fwrite( &tampon, 1, sizeof(pixu1), des->pfd);*/
00473             }
00474         }
00475     fclose( des->pfd);
00476 }
00477 

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