-
- char *tmp = NULL, *tmp2 = NULL,
- *point = NULL, *pgx = NULL;
- char tmpdirpath[MAX_PATH];
- char dirpath[MAX_PATH];
- char pattern[MAX_PATH];
- char pgxfiles[MAX_SLICES][MAX_PATH];
- int pgxslicepos[MAX_SLICES];
- char tmpno[3];
-
- numcomps = 1;
- color_space = CLRSPC_GRAY;
- sliceno = 0;
- maxvalue = 0;
- memset(pgxfiles, 0, MAX_SLICES * MAX_PATH * sizeof(char));
- memset(&cmptparm, 0, sizeof(opj_volume_cmptparm_t));
-
- /* Separaci�n del caso de un �nico slice frente al de muchos */
- if ((tmp = strrchr(relpath,'-')) == NULL){
- /*fprintf(stdout,"[INFO] A volume of only one slice....\n");*/
- sliceno = 1;
- maxslice = 1;
- strcpy(pgxfiles[0],relpath);
-
- } else {
- /*Fetch only the path */
- strcpy(tmpdirpath,relpath);
- if ((tmp = strrchr(tmpdirpath,'/')) != NULL){
- tmp++; *tmp='\0';
- strcpy(dirpath,tmpdirpath);
- } else {
- strcpy(dirpath,"./");
- }
-
- /*Fetch the pattern of the volume slices*/
- if ((tmp = strrchr (relpath,'/')) != NULL)
- tmp++;
- else
- tmp = relpath;
- if ((tmp2 = strrchr(tmp,'-')) != NULL)
- *tmp2='\0';
- else{
- fprintf(stdout, "[ERROR] tmp2 ha dado null. no ha encontrado el * %s %s",tmp,relpath);
- return NULL;
- }
- strcpy(pattern,tmp);
-
- dirp = opendir( dirpath );
- if (dirp == NULL){
- fprintf(stdout, "[ERROR] Infile must be a .pgx file or a directory that contain pgx files");
- return NULL;
- }
-
- /*Read all .pgx files of directory */
- while ( (direntp = readdir( dirp )) != NULL )
- {
- /* Found a directory, but ignore . and .. */
- if(strcmp(".",direntp->d_name) == 0 || strcmp("..",direntp->d_name) == 0)
- continue;
-
- if( ((pgx = strstr(direntp->d_name,pattern)) != NULL) && ((tmp2 = strstr(direntp->d_name,".pgx")) != NULL) ){
-
- strcpy(tmp,dirpath);
- tmp = strcat(tmp,direntp->d_name);
-
- /*Obtenemos el index de la secuencia de slices*/
- if ((tmp2 = strpbrk (direntp->d_name, "0123456789")) == NULL)
- continue;
- i = 0;
- while (tmp2 != NULL) {
- tmpno[i++] = *tmp2;
- point = tmp2;
- tmp2 = strpbrk (tmp2+1,"0123456789");
- }tmpno[i]='\0';
-
- /*Comprobamos que no estamos leyendo algo raro como pattern.jp3d*/
- if ((point = strpbrk (point,".")) == NULL){
- break;
- }
- /*Slicepos --> index de slice; Sliceno --> no de slices hasta el momento*/
- slicepos = atoi(tmpno);
- pgxslicepos[sliceno] = slicepos - 1;
- sliceno++;
- if (slicepos>maxslice)
- maxslice = slicepos;
-
- /*Colocamos el slices en su posicion correspondiente*/
- strcpy(pgxfiles[slicepos-1],tmp);
- }
- }
-
- }/* else if pattern*.pgx */
-
- if (!sliceno) {
- fprintf(stdout,"[ERROR] No slices with this pattern founded !! Please check input volume name\n");
- return NULL;
- }
- /*if ( maxslice != sliceno) {
- fprintf(stdout,"[ERROR] Slices are not sequentially numbered !! Please rename them accordingly\n");
- return NULL;
- }*/
-
- for (s=0;s<sliceno;s++)
- {
- int pos = maxslice == sliceno ? s: pgxslicepos[s];
- f = fopen(pgxfiles[pos], "rb");
- if (!f) {
- fprintf(stdout, "[ERROR] Failed to open %s for reading !\n", pgxfiles[s]);
- return NULL;
- }
- fprintf(stdout, "[INFO] Loading %s \n",pgxfiles[pos]);
-
- fseek(f, 0, SEEK_SET);
- fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h);
-
- i=0;
- sign='+';
- while (signtmp[i]!='\0') {
- if (signtmp[i]=='-') sign='-';
- i++;
- }
-
- fgetc(f);
- if (endian1=='M' && endian2=='L') {
- cmptparm.bigendian = 1;
- } else if (endian2=='M' && endian1=='L') {
- cmptparm.bigendian = 0;
- } else {
- fprintf(stdout, "[ERROR] Bad pgx header, please check input file\n");
- return NULL;
- }
-
- if (s==0){
- /* initialize volume component */
-
- cmptparm.x0 = parameters->volume_offset_x0;
- cmptparm.y0 = parameters->volume_offset_y0;
- cmptparm.z0 = parameters->volume_offset_z0;
- cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
- cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
- cmptparm.l = !cmptparm.z0 ? (sliceno - 1) * parameters->subsampling_dz + 1 : cmptparm.z0 + (sliceno - 1) * parameters->subsampling_dz + 1;
-
- if (sign == '-') {
- cmptparm.sgnd = 1;
- } else {
- cmptparm.sgnd = 0;
- }
- cmptparm.prec = prec;
- cmptparm.bpp = prec;
- cmptparm.dcoffset = parameters->dcoffset;
- cmptparm.dx = parameters->subsampling_dx;
- cmptparm.dy = parameters->subsampling_dy;
- cmptparm.dz = parameters->subsampling_dz;
-
- /* create the volume */
- volume = opj_volume_create(numcomps, &cmptparm, color_space);
- if(!volume) {
- fclose(f);
- return NULL;
- }
- /* set volume offset and reference grid */
- volume->x0 = cmptparm.x0;
- volume->y0 = cmptparm.y0;
- volume->z0 = cmptparm.z0;
- volume->x1 = cmptparm.w;
- volume->y1 = cmptparm.h;
- volume->z1 = cmptparm.l;
-
- /* set volume data :only one component, that is a volume*/
- comp = &volume->comps[0];
-
- }/*if sliceno==1*/
-
- offset = w * h * s;
-
- for (i = 0; i < w * h; i++) {
- int v;
- if (comp->prec <= 8) {
- if (!comp->sgnd) {
- v = readuchar(f);
- } else {
- v = (char) readuchar(f);
- }
- } else if (comp->prec <= 16) {
- if (!comp->sgnd) {
- v = readushort(f, cmptparm.bigendian);
- } else {
- v = (short) readushort(f, cmptparm.bigendian);
- }
- } else {
- if (!comp->sgnd) {
- v = readuint(f, cmptparm.bigendian);
- } else {
- v = (int) readuint(f, cmptparm.bigendian);
- }
- }
- if (v > maxvalue)
- maxvalue = v;
- comp->data[i + offset] = v;
-
- }
- fclose(f);
- } /* for s --> sliceno*/
- comp->bpp = int_floorlog2(maxvalue) + 1;
- if (sliceno != 1)
- closedir( dirp );
- /*dump_volume(stdout, volume);*/
- return volume;
+
+ char *tmp = NULL, *tmp2 = NULL,
+ *point = NULL, *pgx = NULL;
+ char tmpdirpath[MAX_PATH];
+ char dirpath[MAX_PATH];
+ char pattern[MAX_PATH];
+ char pgxfiles[MAX_SLICES][MAX_PATH];
+ int pgxslicepos[MAX_SLICES];
+ char tmpno[3];
+
+ numcomps = 1;
+ color_space = CLRSPC_GRAY;
+ sliceno = 0;
+ maxvalue = 0;
+ memset(pgxfiles, 0, MAX_SLICES * MAX_PATH * sizeof(char));
+ memset(&cmptparm, 0, sizeof(opj_volume_cmptparm_t));
+
+ /* Separación del caso de un único slice frente al de muchos */
+ if ((tmp = strrchr(relpath, '-')) == NULL) {
+ /*fprintf(stdout,"[INFO] A volume of only one slice....\n");*/
+ sliceno = 1;
+ maxslice = 1;
+ strcpy(pgxfiles[0], relpath);
+
+ } else {
+ /*Fetch only the path */
+ strcpy(tmpdirpath, relpath);
+ if ((tmp = strrchr(tmpdirpath, '/')) != NULL) {
+ tmp++;
+ *tmp = '\0';
+ strcpy(dirpath, tmpdirpath);
+ } else {
+ strcpy(dirpath, "./");
+ }
+
+ /*Fetch the pattern of the volume slices*/
+ if ((tmp = strrchr(relpath, '/')) != NULL) {
+ tmp++;
+ } else {
+ tmp = relpath;
+ }
+ if ((tmp2 = strrchr(tmp, '-')) != NULL) {
+ *tmp2 = '\0';
+ } else {
+ fprintf(stdout, "[ERROR] tmp2 ha dado null. no ha encontrado el * %s %s", tmp,
+ relpath);
+ return NULL;
+ }
+ strcpy(pattern, tmp);
+
+ dirp = opendir(dirpath);
+ if (dirp == NULL) {
+ fprintf(stdout,
+ "[ERROR] Infile must be a .pgx file or a directory that contain pgx files");
+ return NULL;
+ }
+
+ /*Read all .pgx files of directory */
+ while ((direntp = readdir(dirp)) != NULL) {
+ /* Found a directory, but ignore . and .. */
+ if (strcmp(".", direntp->d_name) == 0 || strcmp("..", direntp->d_name) == 0) {
+ continue;
+ }
+
+ if (((pgx = strstr(direntp->d_name, pattern)) != NULL) &&
+ ((tmp2 = strstr(direntp->d_name, ".pgx")) != NULL)) {
+
+ strcpy(tmp, dirpath);
+ tmp = strcat(tmp, direntp->d_name);
+
+ /*Obtenemos el index de la secuencia de slices*/
+ if ((tmp2 = strpbrk(direntp->d_name, "0123456789")) == NULL) {
+ continue;
+ }
+ i = 0;
+ while (tmp2 != NULL) {
+ tmpno[i++] = *tmp2;
+ point = tmp2;
+ tmp2 = strpbrk(tmp2 + 1, "0123456789");
+ }
+ tmpno[i] = '\0';
+
+ /*Comprobamos que no estamos leyendo algo raro como pattern.jp3d*/
+ if ((point = strpbrk(point, ".")) == NULL) {
+ break;
+ }
+ /*Slicepos --> index de slice; Sliceno --> no de slices hasta el momento*/
+ slicepos = atoi(tmpno);
+ pgxslicepos[sliceno] = slicepos - 1;
+ sliceno++;
+ if (slicepos > maxslice) {
+ maxslice = slicepos;
+ }
+
+ /*Colocamos el slices en su posicion correspondiente*/
+ strcpy(pgxfiles[slicepos - 1], tmp);
+ }
+ }
+
+ }/* else if pattern*.pgx */
+
+ if (!sliceno) {
+ fprintf(stdout,
+ "[ERROR] No slices with this pattern founded !! Please check input volume name\n");
+ return NULL;
+ }
+ /*if ( maxslice != sliceno) {
+ fprintf(stdout,"[ERROR] Slices are not sequentially numbered !! Please rename them accordingly\n");
+ return NULL;
+ }*/
+
+ for (s = 0; s < sliceno; s++) {
+ int pos = maxslice == sliceno ? s : pgxslicepos[s];
+ f = fopen(pgxfiles[pos], "rb");
+ if (!f) {
+ fprintf(stdout, "[ERROR] Failed to open %s for reading !\n", pgxfiles[s]);
+ return NULL;
+ }
+ fprintf(stdout, "[INFO] Loading %s \n", pgxfiles[pos]);
+
+ fseek(f, 0, SEEK_SET);
+ fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d", temp, &endian1, &endian2,
+ signtmp, &prec, temp, &w, temp, &h);
+
+ i = 0;
+ sign = '+';
+ while (signtmp[i] != '\0') {
+ if (signtmp[i] == '-') {
+ sign = '-';
+ }
+ i++;
+ }
+
+ fgetc(f);
+ if (endian1 == 'M' && endian2 == 'L') {
+ cmptparm.bigendian = 1;
+ } else if (endian2 == 'M' && endian1 == 'L') {
+ cmptparm.bigendian = 0;
+ } else {
+ fprintf(stdout, "[ERROR] Bad pgx header, please check input file\n");
+ fclose(f);
+ return NULL;
+ }
+
+ if (s == 0) {
+ /* initialize volume component */
+
+ cmptparm.x0 = parameters->volume_offset_x0;
+ cmptparm.y0 = parameters->volume_offset_y0;
+ cmptparm.z0 = parameters->volume_offset_z0;
+ cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 :
+ cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
+ cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 :
+ cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
+ cmptparm.l = !cmptparm.z0 ? (sliceno - 1) * parameters->subsampling_dz + 1 :
+ cmptparm.z0 + (sliceno - 1) * parameters->subsampling_dz + 1;
+
+ if (sign == '-') {
+ cmptparm.sgnd = 1;
+ } else {
+ cmptparm.sgnd = 0;
+ }
+ cmptparm.prec = prec;
+ cmptparm.bpp = prec;
+ cmptparm.dcoffset = parameters->dcoffset;
+ cmptparm.dx = parameters->subsampling_dx;
+ cmptparm.dy = parameters->subsampling_dy;
+ cmptparm.dz = parameters->subsampling_dz;
+
+ /* create the volume */
+ volume = opj_volume_create(numcomps, &cmptparm, color_space);
+ if (!volume) {
+ fclose(f);
+ return NULL;
+ }
+ /* set volume offset and reference grid */
+ volume->x0 = cmptparm.x0;
+ volume->y0 = cmptparm.y0;
+ volume->z0 = cmptparm.z0;
+ volume->x1 = cmptparm.w;
+ volume->y1 = cmptparm.h;
+ volume->z1 = cmptparm.l;
+
+ /* set volume data :only one component, that is a volume*/
+ comp = &volume->comps[0];
+
+ }/*if sliceno==1*/
+
+ offset = w * h * s;
+
+ for (i = 0; i < w * h; i++) {
+ int v;
+ if (comp->prec <= 8) {
+ if (!comp->sgnd) {
+ v = readuchar(f);
+ } else {
+ v = (char) readuchar(f);
+ }
+ } else if (comp->prec <= 16) {
+ if (!comp->sgnd) {
+ v = readushort(f, cmptparm.bigendian);
+ } else {
+ v = (short) readushort(f, cmptparm.bigendian);
+ }
+ } else {
+ if (!comp->sgnd) {
+ v = readuint(f, cmptparm.bigendian);
+ } else {
+ v = (int) readuint(f, cmptparm.bigendian);
+ }
+ }
+ if (v > maxvalue) {
+ maxvalue = v;
+ }
+ comp->data[i + offset] = v;
+
+ }
+ fclose(f);
+ } /* for s --> sliceno*/
+ comp->bpp = int_floorlog2(maxvalue) + 1;
+ if (sliceno != 1) {
+ closedir(dirp);
+ }
+ /*dump_volume(stdout, volume);*/
+ return volume;