lsave_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 /* ***************************** lsave_ima.c ******************************/
00020 /*                                                                        */
00021 /* ************************************************************************/
00022 
00058 #include "image.h"
00059 #include "proto2D.h"
00060 #include "rasterfile.h"
00061 #include "permut.h"
00062 #include <stdlib.h>
00063 #include <fcntl.h>
00064 #include <sys/types.h>
00065 #include <sys/stat.h>
00066 #include <string.h>
00067 
00068 #define PERMS   0644
00069 
00070 /* ********************   LECTURE DE PARAMETRES   ***************************/
00083 param *save_ima_lect(save_ima_t *des, param *ptp, char *debq){
00084   char nom[300], question[500];
00085   int l;
00086 
00087   sprintf(question, "%s nom complet du fichier", debq); 
00088   lec_param(question, ptp);
00089   strcpy(nom, ptp->rep);
00090   ptp = ptp->next;
00091 
00092   sprintf(question, "%s legende pour fichier .origima (non cree si vide)", debq); 
00093   lec_param(question, ptp);
00094   strcpy(des->legende, ptp->rep);
00095   ptp = ptp->next;
00096 
00097   l = strlen(nom);
00098   do
00099       l--;
00100   while(nom[l] != '.'  && l>0);
00101   if( l>0 ){
00102       strncpy(des->nom, nom, l);
00103       des->nom[l] = '\0';
00104       strcpy(des->ext, nom+l+1);
00105   }
00106   else{
00107       printf("\n>> ERREUR save_ima_lect : nom image incomplet (extension?)\n");
00108       exit(1);
00109   }
00110 
00111   return(ptp);
00112 }
00113 
00114 /* ********* INITIALISATION  (8 bit) *******/
00128 /************************************/
00129 /* type u1 */
00130 /************************************/
00131 int save_imau1_init(save_ima_t *des, imau1 im){ 
00132   FILE *fp;
00133   char nom_ima[200], nom_dim[200], nom_lut[200];
00134   int j;
00135   struct rasterfile entete;
00136   unsigned char colormap[768], col_impair;
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       /* ouverture du .ima */
00142       if( (des->dfi=creat(nom_ima, PERMS)) == -1 ){
00143           printf("\n>> ERREUR save_imau1_init: creation fichier %s impossible\n",nom_ima);
00144           exit(1);
00145       }
00146       /* ouverture du .dim */
00147       if( (des->pfd=fopen(nom_dim,"w")) == NULL ){
00148           printf("\n>> ERREUR save_imau1_init: creation fichier %s impossible\n",nom_dim);
00149           exit(1);
00150       }
00151       /* ecriture du .3lt */
00152       if( im.lutr!=NULL && im.lutv!=NULL && im.lutb!=NULL) {
00153           sprintf(nom_lut, "%s.%s", des->nom, "3lt"); 
00154           printf("\n>> save_imau1_init: sauvegarde palette dans %s\n", nom_lut);
00155           if( (fp=fopen(nom_lut,"w")) == NULL ){ 
00156               printf("\n>> ERREUR save_imau1_init: creation fichier %s impossible\n",nom_lut); 
00157               exit(1); 
00158           } 
00159           fprintf(fp,"%3d ",im.lutr[0]);
00160           for(j = 1; j < 256; j++) {
00161               if((j % 20) == 0 ) 
00162                   fprintf(fp,"\n");
00163               fprintf(fp,"%3d ", im.lutr[j]);
00164           } 
00165           fprintf(fp,"\n"); 
00166 
00167           fprintf(fp,"%3d ",im.lutv[0]);
00168           for(j = 1; j < 256; j++) {
00169               if((j % 20) == 0 ) 
00170                   fprintf(fp,"\n");
00171               fprintf(fp,"%3d ", im.lutv[j]);
00172           } 
00173           fprintf(fp,"\n"); 
00174 
00175           fprintf(fp,"%3d ",im.lutb[0]);
00176           for(j = 1; j < 256; j++) {
00177               if((j % 20) == 0 ) 
00178                   fprintf(fp,"\n");
00179               fprintf(fp,"%3d ", im.lutb[j]); 
00180           }
00181           fprintf(fp,"\n"); 
00182           fclose(fp);
00183       }
00184   }
00185   else if(strcmp(des->ext, "ras")== 0){
00186       sprintf(nom_ima, "%s.ras", des->nom);
00187       /* ouverture du .ras */
00188       if( (des->pfd=fopen(nom_ima,"w")) == NULL ){
00189           printf("\n>> ERREUR save_imau1_init: creation fichier %s impossible\n",nom_dim);
00190           exit(1);
00191       }
00192       /*creation de l'entete*/
00193       col_impair=im.nc%2;
00194       entete.ras_magic=RAS_MAGIC;
00195       entete.ras_width=im.nc;
00196       entete.ras_height=im.nr;
00197       entete.ras_depth=8;       
00198       entete.ras_length=im.nr*(im.nc+col_impair);
00199       entete.ras_type=1;
00200       entete.ras_maptype=1;
00201       entete.ras_maplength=768;
00202 
00203       /* ecriture de l'entete*/
00204       fwrite( (char *)&entete, sizeof(struct rasterfile), 1, des->pfd);
00205       /*ecriture de la palette*/        
00206       if( im.lutr!=NULL && im.lutv!=NULL && im.lutb!=NULL) {
00207           fwrite( im.lutr, sizeof(unsigned char), 256, des->pfd); 
00208           fwrite( im.lutv, sizeof(unsigned char), 256, des->pfd); 
00209           fwrite( im.lutb, sizeof(unsigned char), 256, des->pfd); 
00210       } 
00211       else {
00212           for (j=0;j<768;j++) colormap[j]=j;    
00213           fwrite( colormap, sizeof(unsigned char), 768, des->pfd); 
00214       }
00215   } 
00216   else{
00217       printf("\n>> ERREUR save_imau1_init: extension fichier %s inconnues\n", des->ext);
00218       exit(1);  
00219   }
00220   des->nb_lig_ecr = 0;
00221   return(0);  
00222 }
00223 
00224 /* ********* INITIALISATION (autre que 8 bit) *******/
00233 /************************************/
00234 /* type autre que u1 */
00235 /************************************/
00236 int save_ima_init(save_ima_t *des){ 
00237   char nom_ima[200], nom_dim[200];
00238 
00239   sprintf(nom_ima, "%s.%s", des->nom, des->ext);
00240   sprintf(nom_dim, "%s.dim", des->nom);
00241 
00242   if( strcmp(des->ext, "imw")== 0 ||
00243       strcmp(des->ext, "iml")== 0 ||
00244       strcmp(des->ext, "imf")== 0 ||
00245       strcmp(des->ext, "cxs")== 0 ||
00246       strcmp(des->ext, "cxf")== 0 ){
00247       /* ouverture du .imw|... */
00248       if( (des->dfi=creat(nom_ima, PERMS)) == -1 ){
00249           printf("\n>> ERREUR save_ima_init: creation fichier %s impossible\n",nom_ima);
00250           exit(1);
00251       }
00252       /* ouverture du .dim */
00253       if( (des->pfd=fopen(nom_dim,"w")) == NULL ){
00254           printf("\n>> ERREUR save_ima_init: creation fichier %s impossible\n",nom_dim);
00255           exit(1);
00256       }
00257   }
00258   else{
00259       printf("\n>> ERREUR save_ima_init: extension fichier %s inconnue\n", des->ext);
00260       exit(1);  
00261   }
00262   des->nb_lig_ecr = 0;
00263   return(0);  
00264 }
00265 
00266 
00267 /* ********************************  CALCUL  **********************************/
00278 /************************************/
00279 /* image 8 bit */
00280 /************************************/
00281 int save_imau1_calc(save_ima_t *des, imau1 im){   /* .ima | .dim  */
00282   int j, nb_oct, col_impair;
00283   pixu1 tmp;
00284       
00285   nb_oct = sizeof(pixu1)*im.nc;
00286   if(strcmp(des->ext, "ima") == 0){
00287       for(j = 0; j < im.nr; j++)                                
00288       if( write(des->dfi, im.p[j], nb_oct) != nb_oct ){ 
00289           printf("\n>> ERREUR save_imau1_calc : ecriture du fichier %s.%s\n",
00290                  des->nom, des->ext); 
00291           exit(1); 
00292       }
00293   }
00294   else if(strcmp(des->ext, "ras") == 0 ){
00295       tmp = 0;
00296       col_impair = im.nc%2;
00297       for(j = 0; j < im.nr; j++)                                
00298           fwrite(im.p[j], sizeof(pixu1), nb_oct, des->pfd); 
00299       if(col_impair)
00300           fwrite(&tmp, sizeof(pixu1), 1, des->pfd);
00301       }  
00302   else{
00303       printf("\n>> ERREUR save_imau1_calc : extension fichier %s inconnue\n", des->ext);
00304       exit(1);  
00305   }
00306   if(des->nb_lig_ecr == 0)
00307       des->nb_col_ecr = im.nc;
00308   des->nb_lig_ecr += im.nr;
00309   return(0);
00310 }
00311 
00312 /************************************/
00313 /* image 16 bit */
00314 /************************************/
00315 int save_imau2_calc(save_ima_t *des, imau2 im){   /* .imw       */
00316   int j, nb_oct;
00317       
00318   nb_oct = sizeof(pixu2)*im.nc;
00319 
00320 #ifdef INVERSE_DATA
00321   printf("Swap des octets       des donnees pour ecriture sur fichier\n");
00322   for( j=0; j<im.nr; j++)
00323     permuteShortTab( (short *)im.p[j], im.nc);
00324 #endif
00325 
00326 
00327   for(j = 0; j < im.nr; j++)                            
00328       if( write(des->dfi, (char *)im.p[j], nb_oct) != nb_oct){ 
00329           printf("\n>> ERREUR save_imau2_calc : ecriture du fichier %s.%s\n",
00330                  des->nom, des->ext); 
00331           exit(1); 
00332       }
00333   if(des->nb_lig_ecr == 0)
00334       des->nb_col_ecr = im.nc;
00335   des->nb_lig_ecr += im.nr;
00336 
00337 #ifdef INVERSE_DATA
00338   //printf("Swap des octets     des donnees pour ecriture sur fichier\n");
00339         for( j=0; j<im.nr; j++)
00340                 permuteShortTab( (short *)im.p[j], im.nc);
00341 #endif
00342 
00343   return(0);
00344 }
00345 
00346 /************************************/
00347 /* image 32 bit */
00348 /************************************/
00349 int save_imau4_calc(save_ima_t *des, imau4 im){   /* .iml       */
00350   int j, nb_oct;
00351       
00352   nb_oct = sizeof(pixu4)*im.nc;
00353 
00354 #ifdef INVERSE_DATA
00355   printf("Swap des octets des donnees pour ecriture sur fichier\n");
00356   for( j=0; j<im.nr; j++)
00357     permuteIntTab( (unsigned int *)im.p[j], im.nc);
00358 #endif
00359 
00360 
00361   for(j = 0; j < im.nr; j++)                            
00362       if( write(des->dfi, (char *)im.p[j], nb_oct) != nb_oct){ 
00363           printf("\n>> ERREUR save_imau4_calc : ecriture du fichier %s.%s\n",
00364                  des->nom, des->ext); 
00365           exit(1); 
00366       }
00367   if(des->nb_lig_ecr == 0)
00368       des->nb_col_ecr = im.nc;
00369   des->nb_lig_ecr += im.nr;
00370 
00371 #ifdef INVERSE_DATA
00372   //printf("Swap des octets     des donnees pour ecriture sur fichier\n");
00373         for( j=0; j<im.nr; j++)
00374                 permuteIntTab( (unsigned int *)im.p[j], im.nc);
00375 #endif
00376 
00377   return(0);
00378 }
00379 
00380 /************************************/
00381 /* image float */
00382 /************************************/
00383 int save_imafl_calc(save_ima_t *des, imafl im){   /* .imf       */
00384   int j, nb_oct;
00385       
00386   nb_oct = sizeof(pixfl)*im.nc;
00387 #ifdef INVERSE_DATA
00388   printf("Swap des octets       des donnees pour ecriture sur fichier\n");
00389   for( j=0; j<im.nr; j++)
00390     permuteFloatTab( im.p[j], im.nc);
00391 #endif
00392 
00393   for(j = 0; j < im.nr; j++)                            
00394       if( write(des->dfi, (char *)im.p[j], nb_oct) != nb_oct){ 
00395           printf("\n>> ERREUR save_imafl_calc : ecriture du fichier %s.%s\n",
00396                  des->nom, des->ext); 
00397           exit(1); 
00398       }
00399   if(des->nb_lig_ecr == 0)
00400       des->nb_col_ecr = im.nc;
00401   des->nb_lig_ecr += im.nr;
00402 
00403 #ifdef INVERSE_DATA
00404         //printf("re Swap des octets des donnees float pour traitement ultérieur\n");
00405         for( j=0; j<im.nr; j++)
00406                 permuteFloatTab( im.p[j], im.nc);
00407 #endif
00408 
00409   return(0);
00410 }
00411 
00412 /************************************/
00413 /* image complex short */
00414 /************************************/
00415 int save_imacx4_calc(save_ima_t *des, imacx4 im){ /* .cxs       */
00416   int j, nb_oct;
00417       
00418   nb_oct = sizeof(pixcx4)*im.nc;
00419 #ifdef INVERSE_DATA
00420         printf("Swap des octets des donnees pour ecriture sur fichier\n");
00421         for( j=0; j<im.nr; j++)
00422                 permuteShortTab( (short *)&im.p[j][0].re, 2*im.nc);
00423 #endif
00424   for(j = 0; j < im.nr; j++)                            
00425       if( write(des->dfi, (char *)im.p[j], nb_oct) != nb_oct){ 
00426           printf("\n>> ERREUR save_imacx4_calc : ecriture du fichier %s.%s\n",
00427                  des->nom, des->ext); 
00428           exit(1); 
00429       }
00430   if(des->nb_lig_ecr == 0)
00431       des->nb_col_ecr = im.nc;
00432   des->nb_lig_ecr += im.nr;
00433 
00434 #ifdef INVERSE_DATA
00435   //printf("Swap des octets     des donnees pour ecriture sur fichier\n");
00436         for( j=0; j<im.nr; j++)
00437                 permuteShortTab( (short *)&im.p[j][0].re, 2*im.nc);
00438 #endif
00439   return(0);
00440 }
00441 
00442 /************************************/
00443 /* image complex float */
00444 /************************************/
00445 int save_imacx8_calc(save_ima_t *des, imacx8 im){ /* .cxf       */
00446   int j, nb_oct;
00447       
00448   nb_oct = sizeof(pixcx8)*im.nc;
00449 
00450 #ifdef INVERSE_DATA
00451         printf("Swap des octets des donnees pour ecriture sur fichier\n");
00452         for( j=0; j<im.nr; j++)
00453                 permuteFloatTab( (float *)im.p[j], 2*im.nc);
00454 #endif
00455 
00456   for(j = 0; j < im.nr; j++)                            
00457       if( write(des->dfi, (char *)im.p[j], nb_oct) != nb_oct){ 
00458           printf("\n>> ERREUR save_imacx8_calc : ecriture du fichier %s.%s\n",
00459                  des->nom, des->ext); 
00460           exit(1); 
00461       }
00462   if(des->nb_lig_ecr == 0)
00463       des->nb_col_ecr = im.nc;
00464   des->nb_lig_ecr += im.nr;
00465 
00466 #ifdef INVERSE_DATA
00467     //printf("Swap des octets   des donnees pour ecriture sur fichier\n");
00468         for( j=0; j<im.nr; j++)
00469                 permuteFloatTab( (float *)im.p[j], 2*im.nc);
00470 #endif
00471   return(0);
00472 }
00473 
00474 
00475 /* *********************** FERMETURE : ecriture .dim, .origima  *******************/
00487 int save_ima_ferm(select_ima_t sel, save_ima_t des){
00488   FILE *fp;
00489   char nom[200];
00490   int tmp, col_impair;
00491 
00492   if( strcmp(des.ext, "ima")== 0 ||
00493       strcmp(des.ext, "imw")== 0 ||
00494       strcmp(des.ext, "iml")== 0 ||
00495       strcmp(des.ext, "imf")== 0 ||
00496       strcmp(des.ext, "cxs")== 0 ||
00497       strcmp(des.ext, "cxf")== 0 ){
00498       /* ferme donnees */ 
00499       close(des.dfi);
00500       printf(">> save_ima_ferm : ecriture %s.%s terminee\n", des.nom, des.ext);
00501       /* ecrit et ferme .dim */
00502       fprintf(des.pfd, "%d %d", des.nb_col_ecr, des.nb_lig_ecr);
00503       fclose(des.pfd);
00504   }
00505   /* cas RASTER */   
00506   else if( strcmp(des.ext, "ras") == 0){
00507       col_impair = des.nb_col_ecr%2;
00508       tmp = des.nb_lig_ecr*(des.nb_col_ecr+col_impair);
00509       fseek(des.pfd, 2*sizeof(int), SEEK_SET);
00510       fwrite(&des.nb_lig_ecr, sizeof(int), 1, des.pfd);
00511       printf("nb ligne ecrite:%d",des.nb_lig_ecr);
00512       fseek(des.pfd, 4*sizeof(int), SEEK_SET);
00513       fwrite(&tmp, sizeof(int), 1, des.pfd);
00514       }
00515   else{
00516       printf("\n>> ERREUR save_ima_ferm: extension fichier %s inconnue\n", des.ext);
00517       exit(1);  
00518   }
00519 
00520   /* .origima */
00521   if( des.legende[0] != '\0' ){
00522       sprintf(nom, "%s.origima", des.nom);
00523       if( (fp=fopen(nom,"w")) == NULL ){
00524           printf("\n>> ERREUR save_ima_ferm: creation fichier %s impossible\n",nom);
00525           exit(1);
00526       }  
00527       fprintf(fp,"Fichier origine : %s.%s\n", sel.nom, sel.ext);
00528       /*      fprintf(fp,"\tdecimation ligne 1  colonne 1\n");  */
00529       fprintf(fp,"\tpoint haut gauche : np = %d, nl = %d\n", sel.col0, sel.lig0);
00530       fprintf(fp,"\tpoint bas droite  : np = %d, nl = %d\n", 
00531               sel.col0+sel.nb_col-1, sel.lig0+des.nb_lig_ecr-1);
00532       fprintf(fp,"Legende : %s\n", des.legende);
00533       fclose(fp);
00534   }
00535   return(0);
00536 }
00537 
00538 

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