00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <sys/types.h>
00021 #include <sys/stat.h>
00022 #include <unistd.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include "volume.h"
00026
00027
00028
00029 int lecture_entete_lvf(char * nomfich, ima3Du1 * pt_image)
00030 {
00031 FILE * f;
00032 unsigned int val, nb_element_lu;
00033 unsigned int tab_val[5];
00034 struct stat info;
00035
00036
00037
00038
00039
00040
00041 f = fopen(nomfich,"r");
00042 if( f==NULL ){
00043 printf("Dans %s, ligne %d, ", __FILE__,__LINE__);
00044 fflush(stdout);
00045 perror("lect_volume_u1");
00046 exit(-1);
00047 }
00048
00049
00050 if( stat( nomfich, &info) ){
00051 perror("stat");
00052 fclose(f);
00053 return(-1);
00054 }
00055
00056
00057 nb_element_lu = fread( &val, sizeof(int), 1, f);
00058 if( nb_element_lu != 1){
00059 printf("lecture impossible du nombre magique\n");
00060 fclose(f);
00061 return(-1);
00062 }
00063
00064
00065 if( val != LVF_MAGIC_NUMBER){
00066 if( val == LVF_MAGIC_NUMBER_INV)
00067 printf("Attention image codee dans l'ordre inverse de la lecture\n");
00068 printf("Nombre magique incorrecte\n");
00069 fclose(f);
00070 return(-1);
00071 }
00072
00073
00074 nb_element_lu = fread( tab_val, sizeof(int), 5, f);
00075 if( nb_element_lu != 5 ){
00076 printf("probleme lecture paramètres dimensionnels de l'image\n");
00077 fclose(f);
00078 return(-1);
00079 }
00080
00081
00082 if( tab_val[4]!=8 ){
00083 printf("Image non codee sur 8bits\n");
00084 fclose(f);
00085 return(-1);
00086 }
00087
00088
00089 if( info.st_size != tab_val[0] + tab_val[1]*tab_val[2]*tab_val[3]){
00090 printf("fichier de %d octets alors que %d octets attendus!\n",
00091 info.st_size,
00092 tab_val[0] + tab_val[1]*tab_val[2]*tab_val[3]);
00093 fclose(f);
00094 return(-1);
00095 }
00096
00097
00098
00099 pt_image->lgtete = tab_val[0];
00100 pt_image->dimx = tab_val[1];
00101 pt_image->dimy = tab_val[2];
00102 pt_image->dimz = tab_val[3];
00103 pt_image->dept = tab_val[4];
00104 pt_image->f = f;
00105
00106
00107 fseek( f, tab_val[0], SEEK_SET);
00108
00109 }
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132 int lect_volume_u1(char * nomfich, ima3Du1 * pt_image){
00133
00134 FILE * f;
00135 unsigned int val, nb_element_lu;
00136 unsigned int tab_val[5];
00137 register unsigned int x,y,z;
00138 struct stat info;
00139
00140
00141
00142
00143
00144
00145 f = fopen(nomfich,"r");
00146 if( f==NULL ){
00147 printf("Dans %s, ligne %d, ", __FILE__,__LINE__);
00148 fflush(stdout);
00149 perror("lect_volume_u1");
00150 exit(-1);
00151 }
00152
00153
00154 if( stat( nomfich, &info) ){
00155 perror("stat");
00156 fclose(f);
00157 return(-1);
00158 }
00159
00160
00161 nb_element_lu = fread( &val, sizeof(int), 1, f);
00162 if( nb_element_lu != 1){
00163 printf("lecture impossible du nombre magique\n");
00164 fclose(f);
00165 return(-1);
00166 }
00167
00168
00169 if( val != LVF_MAGIC_NUMBER){
00170 if( val == LVF_MAGIC_NUMBER_INV)
00171 printf("Attention image codee dans l'ordre inverse de la lecture\n");
00172 printf("Nombre magique incorrecte\n");
00173 fclose(f);
00174 return(-1);
00175 }
00176
00177
00178 nb_element_lu = fread( tab_val, sizeof(int), 5, f);
00179 if( nb_element_lu != 5 ){
00180 printf("probleme lecture paramètres dimensionnels de l'image\n");
00181 fclose(f);
00182 return(-1);
00183 }
00184
00185
00186 if( tab_val[4]!=8 ){
00187 printf("Image non codee sur 8bits\n");
00188 fclose(f);
00189 return(-1);
00190 }
00191
00192
00193 if( info.st_size != tab_val[0] + tab_val[1]*tab_val[2]*tab_val[3]){
00194 printf("fichier de %d octets alors que %d octets attendus!\n",
00195 info.st_size,
00196 tab_val[0] + tab_val[1]*tab_val[2]*tab_val[3]);
00197 fclose(f);
00198 return(-1);
00199 }
00200
00201
00202
00203 pt_image->lgtete = tab_val[0];
00204 pt_image->dimx = tab_val[1];
00205 pt_image->dimy = tab_val[2];
00206 pt_image->dimz = tab_val[3];
00207 pt_image->dept = tab_val[4];
00208
00209
00210 pt_image->data = (unsigned char ***)malloc( tab_val[3] * sizeof(unsigned char **));
00211 if( pt_image->data == NULL){
00212 perror("Probleme d'allocation");
00213 fclose(f);
00214 return(-1);
00215 }
00216 for(z=0; z<tab_val[3]; z++){
00217 pt_image->data[z] = (unsigned char **)malloc(pt_image->dimy * sizeof(unsigned char *));
00218 if( pt_image->data[z] == NULL){
00219 perror("Probleme d'allocation");
00220 fclose(f);
00221 return(-1);
00222 }
00223 for(y=0; y<pt_image->dimy; y++){
00224 pt_image->data[z][y] = (unsigned char *)malloc(pt_image->dimx * sizeof(unsigned char));
00225 if( pt_image->data[z][y] == NULL){
00226 perror("Probleme d'allocation");
00227 fclose(f);
00228 return(-1);
00229 }
00230 }
00231 }
00232
00233
00234 fseek( f, tab_val[0], SEEK_SET);
00235 for(z=0; z<tab_val[3]; z++)
00236 for(y=0; y<tab_val[2]; y++)
00237 for(x=0; x<tab_val[1]; x++){
00238 fread( &(pt_image->data[z][y][x]), sizeof(unsigned char), 1, f);
00239
00240 }
00241
00242 fclose(f);
00243 return(0);
00244 }
00245
00246
00247
00248
00249
00250
00251
00252 int lect_volume_u1_sequence_first(char * nomfich, ima3Du1 * pt_image, int sw){
00253 int z,y;
00254
00255 lecture_entete_lvf( nomfich, pt_image);
00256
00257
00258 if( sw%2==0){
00259 printf("Sequence_width must be even: %d used instead of %d\n", sw+1, sw);
00260 sw=sw+1;
00261 }
00262
00263
00264 pt_image->sequence_width = sw;
00265 allouer_ima3Du1_sequence(pt_image);
00266
00267
00268 for(z=0; z<sw-1; z++)
00269 for(y=0; y<pt_image->dimy; y++)
00270 fread( pt_image->data[z][y], sizeof(unsigned char), pt_image->dimx, pt_image->f);
00271
00272
00273 pt_image->sequence_number = sw/2;
00274
00275 }
00276
00277
00278
00279
00280
00281
00282
00283
00284 int lect_volume_u1_sequence_next(ima3Du1 * pt_image){
00285 unsigned char **tampon;
00286 int i;
00287 int z,y;
00288
00289
00290
00291 if( pt_image->sequence_width/2+pt_image->sequence_number >= pt_image->dimz ){
00292 printf("Lecture sequence suivante impossible: fin du bloc\n");
00293 return( -1);
00294 }
00295
00296
00297 tampon = pt_image->data[0];
00298 for( z=0; z<pt_image->sequence_width-1; z++)
00299 pt_image->data[z] = pt_image->data[z+1];
00300 pt_image->data[ pt_image->sequence_width-1 ] = tampon;
00301
00302
00303 for(y=0; y<pt_image->dimy; y++)
00304 fread( pt_image->data[pt_image->sequence_width-1][y], sizeof(unsigned char), pt_image->dimx, pt_image->f);
00305
00306
00307
00308 pt_image->sequence_number++;
00309
00310
00311
00312
00313
00314 }
00315
00316
00317
00318
00319
00320
00321
00322 int lect_volume_u1_sequence_previous(ima3Du1 * pt_image){
00323 }
00324
00325
00326
00327
00328
00329
00330 int lect_volume_u1_sequence_number(ima3Du1 * pt_image){
00331 }
00332
00333
00334
00335
00336
00337
00338
00339 int ecriture_entete_lvf( char * nomfich, ima3Du1 * pt_image){
00340
00341 FILE * f;
00342 unsigned int val, nb_element_ecrit;
00343 register unsigned int x,y,z;
00344
00345
00346
00347 f = fopen(nomfich,"w");
00348 if( f==NULL ){
00349 printf("Dans %s, ligne %d, ", __FILE__,__LINE__);
00350 fflush(stdout);
00351 perror("write_volume_u1");
00352 return(-1);
00353 }
00354
00355
00356 val = LVF_MAGIC_NUMBER;
00357 nb_element_ecrit = fwrite( &val, sizeof(int), 1, f);
00358 if( nb_element_ecrit != 1){
00359 printf("Ecriture impossible du nombre magique\n");
00360 fclose(f);
00361 return(-1);
00362 }
00363
00364
00365
00366 val = 6*sizeof(int);
00367 fwrite( &pt_image->lgtete, sizeof(int), 1, f);
00368 fwrite( &pt_image->dimx, sizeof(int), 1, f);
00369 fwrite( &pt_image->dimy, sizeof(int), 1, f);
00370 fwrite( &pt_image->dimz, sizeof(int), 1, f);
00371 fwrite( &pt_image->dept, sizeof(int), 1, f);
00372
00373
00374 pt_image->f = f;
00375
00376 }
00377
00378
00379
00380
00381
00382 int write_volume_u1(char * nomfich, ima3Du1 * pt_image){
00383
00384 FILE * f;
00385 unsigned int val, nb_element_ecrit;
00386 register unsigned int x,y,z;
00387
00388
00389
00390 f = fopen(nomfich,"w");
00391 if( f==NULL ){
00392 printf("Dans %s, ligne %d, ", __FILE__,__LINE__);
00393 fflush(stdout);
00394 perror("write_volume_u1");
00395 return(-1);
00396 }
00397
00398
00399 val = LVF_MAGIC_NUMBER;
00400 nb_element_ecrit = fwrite( &val, sizeof(int), 1, f);
00401 if( nb_element_ecrit != 1){
00402 printf("Ecriture impossible du nombre magique\n");
00403 fclose(f);
00404 return(-1);
00405 }
00406
00407
00408 if( pt_image->lgtete!=24){
00409 printf("ATTENTION ecriture d'image impossible: entete=%d\n", pt_image->lgtete);
00410 fclose(f);
00411 return 1;
00412 }
00413
00414
00415
00416 if( pt_image->dept!=8){
00417 printf("ATTENTION ecriture d'image impossible: profondeur=%d\n", pt_image->dept);
00418 fclose(f);
00419 return 1;
00420 }
00421
00422
00423
00424 fwrite( &pt_image->lgtete, sizeof(int), 1, f);
00425 fwrite( &pt_image->dimx, sizeof(int), 1, f);
00426 fwrite( &pt_image->dimy, sizeof(int), 1, f);
00427 fwrite( &pt_image->dimz, sizeof(int), 1, f);
00428 fwrite( &pt_image->dept, sizeof(int), 1, f);
00429
00430
00431 for(z=0; z< pt_image->dimz; z++)
00432 for(y=0; y<pt_image->dimy; y++)
00433 fwrite( &pt_image->data[z][y][0], sizeof(unsigned char), pt_image->dimx, f);
00434
00435 fclose(f);
00436 }
00437
00438
00439
00440
00441 int write_part_of_volume_u1(char * nomfich, ima3Du1 * pt_image,
00442 unsigned int x1, unsigned int y1, unsigned int z1,
00443 unsigned int x2, unsigned int y2, unsigned int z2){
00444
00445
00446 FILE * f;
00447 unsigned int val, nb_element_ecrit;
00448 register unsigned int x,y,z;
00449 unsigned dimx,dimy,dimz;
00450
00451
00452
00453 f = fopen(nomfich,"w");
00454 if( f==NULL ){
00455 printf("Dans %s, ligne %d, ", __FILE__,__LINE__);
00456 fflush(stdout);
00457 perror("write_volume_u1");
00458 return(-1);
00459 }
00460
00461
00462 val = LVF_MAGIC_NUMBER;
00463 nb_element_ecrit = fwrite( &val, sizeof(int), 1, f);
00464 if( nb_element_ecrit != 1){
00465 printf("Ecriture impossible du nombre magique\n");
00466 fclose(f);
00467 return(-1);
00468 }
00469
00470 dimx = x2-x1+1;
00471 dimy = y2-y1+1;
00472 dimz = z2-z1+1;
00473
00474
00475
00476 val = 6*sizeof(int);
00477 fwrite( &val, sizeof(int), 1, f);
00478 fwrite( &dimx, sizeof(int), 1, f);
00479 fwrite( &dimy, sizeof(int), 1, f);
00480 fwrite( &dimz, sizeof(int), 1, f);
00481 fwrite( &pt_image->dept, sizeof(int), 1, f);
00482
00483
00484 for(z=z1; z<=z2; z++)
00485 for(y=y1; y<=y2; y++)
00486 fwrite( &pt_image->data[z][y][x1], sizeof(unsigned char), dimx, f);
00487
00488 }
00489
00490
00491
00492
00493
00494 int copie_entete_ima3du1(ima3Du1 * ptImaSrc, ima3Du1 * ptImaDst){
00495
00496 ptImaDst->dimx = ptImaSrc->dimx;
00497 ptImaDst->dimy = ptImaSrc->dimy;
00498 ptImaDst->dimz = ptImaSrc->dimz;
00499 ptImaDst->lgtete = ptImaSrc->lgtete;
00500 ptImaDst->dept = ptImaSrc->dept;
00501 ptImaDst->sequence_number = ptImaSrc->sequence_number;
00502 ptImaDst->sequence_width = ptImaSrc->sequence_width;
00503
00504 }
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514 int write_volume_u1_sequence_preparation( char * nomfich, ima3Du1 * pt_image, int nb_section_avant){
00515 int x,y,z;
00516 unsigned char val=0;
00517
00518 ecriture_entete_lvf( nomfich, pt_image);
00519
00520
00521 for( z=0; z<nb_section_avant; z++)
00522 for( y=0; y<pt_image->dimy; y++)
00523 for( x=0; x<pt_image->dimx; x++)
00524 fwrite( &val, sizeof(unsigned char), 1, pt_image->f);
00525
00526 }
00527
00528
00529
00530
00531
00532
00533
00534
00535 int write_volume_u1_sequence_next(ima3Du1 * pt_image){
00536 int y;
00537
00538
00539 for(y=0; y<pt_image->dimy; y++)
00540 fwrite( pt_image->data[pt_image->sequence_number][y], sizeof(unsigned char), pt_image->dimx, pt_image->f);
00541
00542
00543 }
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553 int write_volume_u1_sequence_fermeture(ima3Du1 * pt_image, int nb_section_apres){
00554 int x,y,z;
00555 unsigned char val=0;
00556
00557
00558 for( z=0; z<nb_section_apres; z++)
00559 for( y=0; y<pt_image->dimy; y++)
00560 for( x=0; x<pt_image->dimx; x++)
00561 fwrite( &val, sizeof(unsigned char), 1, pt_image->f);
00562
00563
00564
00565 fclose( pt_image->f);
00566
00567 }