2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2001-2003, David Janssens
8 * Copyright (c) 2002-2003, Yannick Verschueren
9 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
10 * Copyright (c) 2005, Herve Drolon, FreeImage Team
11 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
12 * Copyright (c) 2006, M�nica D�ez Garc�a, Image Processing Laboratory, University of Valladolid, Spain
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
41 #include "windirent.h"
48 void dump_volume(FILE *fd, opj_volume_t * vol) {
50 fprintf(fd, "volume {\n");
51 fprintf(fd, " x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d\n", vol->x0, vol->y0, vol->z0,vol->x1, vol->y1, vol->z1);
52 fprintf(fd, " numcomps=%d\n", vol->numcomps);
53 for (compno = 0; compno < vol->numcomps; compno++) {
54 opj_volume_comp_t *comp = &vol->comps[compno];
55 fprintf(fd, " comp %d {\n", compno);
56 fprintf(fd, " dx=%d, dy=%d, dz=%d\n", comp->dx, comp->dy, comp->dz);
57 fprintf(fd, " prec=%d\n", comp->prec);
58 fprintf(fd, " sgnd=%d\n", comp->sgnd);
65 * Get logarithm of an integer and round downwards.
69 static int int_floorlog2(int a) {
71 for (l = 0; a > 1; l++) {
78 * Divide an integer by a power of 2 and round upwards.
82 static int int_ceildivpow2(int a, int b) {
83 return (a + (1 << b) - 1) >> b;
87 * Divide an integer and round upwards.
91 static int int_ceildiv(int a, int b) {
92 return (a + b - 1) / b;
96 /* -->> -->> -->> -->>
100 <<-- <<-- <<-- <<-- */
103 unsigned char readuchar(FILE * f)
110 unsigned short readushort(FILE * f, int bigendian)
112 unsigned char c1, c2;
116 return (c1 << 8) + c2;
118 return (c2 << 8) + c1;
121 unsigned int readuint(FILE * f, int bigendian)
123 unsigned char c1, c2, c3, c4;
129 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
131 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
133 /*****************************************/
134 static unsigned short ShortSwap(unsigned short v)
136 unsigned char c1, c2;
138 c2 = (v >> 8) & 0xff;
139 return (c1 << 8) + c2;
142 static unsigned int LongSwap (unsigned int i)
144 unsigned char b1, b2, b3, b4;
146 b2 = ( i >> 8 ) & 255;
147 b3 = ( i>>16 ) & 255;
148 b4 = ( i>>24 ) & 255;
149 return ((int)b1 << 24) + ((int)b2 << 16) + ((int)b3 << 8) + b4;
151 /*****************************************/
153 opj_volume_t* pgxtovolume(char *relpath, opj_cparameters_t *parameters) {
157 unsigned long offset;
158 int i, s, numcomps, maxvalue, sliceno, slicepos, maxslice = 0;
160 OPJ_COLOR_SPACE color_space;
161 opj_volume_cmptparm_t cmptparm; /* maximum of 1 component */
162 opj_volume_t * volume = NULL;
164 char endian1,endian2,sign;
167 opj_volume_comp_t *comp = NULL;
170 struct dirent *direntp;
172 char *tmp = NULL, *tmp2 = NULL,
173 *point = NULL, *pgx = NULL;
174 char tmpdirpath[MAX_PATH];
175 char dirpath[MAX_PATH];
176 char pattern[MAX_PATH];
177 char pgxfiles[MAX_SLICES][MAX_PATH];
178 int pgxslicepos[MAX_SLICES];
182 color_space = CLRSPC_GRAY;
185 memset(pgxfiles, 0, MAX_SLICES * MAX_PATH * sizeof(char));
186 memset(&cmptparm, 0, sizeof(opj_volume_cmptparm_t));
188 /* Separaci�n del caso de un �nico slice frente al de muchos */
189 if ((tmp = strrchr(relpath,'-')) == NULL){
190 /*fprintf(stdout,"[INFO] A volume of only one slice....\n");*/
193 strcpy(pgxfiles[0],relpath);
196 /*Fetch only the path */
197 strcpy(tmpdirpath,relpath);
198 if ((tmp = strrchr(tmpdirpath,'/')) != NULL){
200 strcpy(dirpath,tmpdirpath);
202 strcpy(dirpath,"./");
205 /*Fetch the pattern of the volume slices*/
206 if ((tmp = strrchr (relpath,'/')) != NULL)
210 if ((tmp2 = strrchr(tmp,'-')) != NULL)
213 fprintf(stdout, "[ERROR] tmp2 ha dado null. no ha encontrado el * %s %s",tmp,relpath);
218 dirp = opendir( dirpath );
220 fprintf(stdout, "[ERROR] Infile must be a .pgx file or a directory that contain pgx files");
224 /*Read all .pgx files of directory */
225 while ( (direntp = readdir( dirp )) != NULL )
227 /* Found a directory, but ignore . and .. */
228 if(strcmp(".",direntp->d_name) == 0 || strcmp("..",direntp->d_name) == 0)
231 if( ((pgx = strstr(direntp->d_name,pattern)) != NULL) && ((tmp2 = strstr(direntp->d_name,".pgx")) != NULL) ){
234 tmp = strcat(tmp,direntp->d_name);
236 /*Obtenemos el index de la secuencia de slices*/
237 if ((tmp2 = strpbrk (direntp->d_name, "0123456789")) == NULL)
240 while (tmp2 != NULL) {
243 tmp2 = strpbrk (tmp2+1,"0123456789");
246 /*Comprobamos que no estamos leyendo algo raro como pattern.jp3d*/
247 if ((point = strpbrk (point,".")) == NULL){
250 /*Slicepos --> index de slice; Sliceno --> no de slices hasta el momento*/
251 slicepos = atoi(tmpno);
252 pgxslicepos[sliceno] = slicepos - 1;
254 if (slicepos>maxslice)
257 /*Colocamos el slices en su posicion correspondiente*/
258 strcpy(pgxfiles[slicepos-1],tmp);
262 }/* else if pattern*.pgx */
265 fprintf(stdout,"[ERROR] No slices with this pattern founded !! Please check input volume name\n");
268 /*if ( maxslice != sliceno) {
269 fprintf(stdout,"[ERROR] Slices are not sequentially numbered !! Please rename them accordingly\n");
273 for (s=0;s<sliceno;s++)
275 int pos = maxslice == sliceno ? s: pgxslicepos[s];
276 f = fopen(pgxfiles[pos], "rb");
278 fprintf(stdout, "[ERROR] Failed to open %s for reading !\n", pgxfiles[s]);
281 fprintf(stdout, "[INFO] Loading %s \n",pgxfiles[pos]);
283 fseek(f, 0, SEEK_SET);
284 fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h);
288 while (signtmp[i]!='\0') {
289 if (signtmp[i]=='-') sign='-';
294 if (endian1=='M' && endian2=='L') {
295 cmptparm.bigendian = 1;
296 } else if (endian2=='M' && endian1=='L') {
297 cmptparm.bigendian = 0;
299 fprintf(stdout, "[ERROR] Bad pgx header, please check input file\n");
304 /* initialize volume component */
306 cmptparm.x0 = parameters->volume_offset_x0;
307 cmptparm.y0 = parameters->volume_offset_y0;
308 cmptparm.z0 = parameters->volume_offset_z0;
309 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
310 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
311 cmptparm.l = !cmptparm.z0 ? (sliceno - 1) * parameters->subsampling_dz + 1 : cmptparm.z0 + (sliceno - 1) * parameters->subsampling_dz + 1;
318 cmptparm.prec = prec;
320 cmptparm.dcoffset = parameters->dcoffset;
321 cmptparm.dx = parameters->subsampling_dx;
322 cmptparm.dy = parameters->subsampling_dy;
323 cmptparm.dz = parameters->subsampling_dz;
325 /* create the volume */
326 volume = opj_volume_create(numcomps, &cmptparm, color_space);
331 /* set volume offset and reference grid */
332 volume->x0 = cmptparm.x0;
333 volume->y0 = cmptparm.y0;
334 volume->z0 = cmptparm.z0;
335 volume->x1 = cmptparm.w;
336 volume->y1 = cmptparm.h;
337 volume->z1 = cmptparm.l;
339 /* set volume data :only one component, that is a volume*/
340 comp = &volume->comps[0];
346 for (i = 0; i < w * h; i++) {
348 if (comp->prec <= 8) {
352 v = (char) readuchar(f);
354 } else if (comp->prec <= 16) {
356 v = readushort(f, cmptparm.bigendian);
358 v = (short) readushort(f, cmptparm.bigendian);
362 v = readuint(f, cmptparm.bigendian);
364 v = (int) readuint(f, cmptparm.bigendian);
369 comp->data[i + offset] = v;
373 } /* for s --> sliceno*/
374 comp->bpp = int_floorlog2(maxvalue) + 1;
377 /*dump_volume(stdout, volume);*/
382 int volumetopgx(opj_volume_t * volume, char *outfile) {
383 int w, wr, wrr, h, hr, hrr, l, lr, lrr;
384 int i, j, compno, offset, sliceno;
387 for (compno = 0; compno < volume->numcomps; compno++) {
388 opj_volume_comp_t *comp = &volume->comps[compno];
399 for(sliceno = 0; sliceno < volume->z1 - volume->z0; sliceno++) {
401 if (volume->numcomps > 1) {
402 sprintf(name, "%s%d-%d.pgx", outfile, sliceno+1, compno);
403 } else if ((volume->z1 - volume->z0) > 1) {
404 sprintf(name, "%s%d.pgx", outfile, sliceno+1);
406 sprintf(name, "%s.pgx", outfile);
409 fdest = fopen(name, "wb");
411 fprintf(stdout, "[ERROR] Failed to open %s for writing \n", name);
415 fprintf(stdout,"[INFO] Writing in %s (%s)\n",name,volume->comps[0].bigendian ? "Bigendian" : "Little-endian");
417 w = int_ceildiv(volume->x1 - volume->x0, volume->comps[compno].dx);
418 wr = volume->comps[compno].w;
419 wrr = int_ceildivpow2(volume->comps[compno].w, volume->comps[compno].factor[0]);
421 h = int_ceildiv(volume->y1 - volume->y0, volume->comps[compno].dy);
422 hr = volume->comps[compno].h;
423 hrr = int_ceildivpow2(volume->comps[compno].h, volume->comps[compno].factor[1]);
425 l = int_ceildiv(volume->z1 - volume->z0, volume->comps[compno].dz);
426 lr = volume->comps[compno].l;
427 lrr = int_ceildivpow2(volume->comps[compno].l, volume->comps[compno].factor[2]);
429 fprintf(fdest, "PG %c%c %c%d %d %d\n", comp->bigendian ? 'M':'L', comp->bigendian ? 'L':'M',comp->sgnd ? '-' : '+', comp->prec, wr, hr);
430 if (comp->prec <= 8) {
432 } else if (comp->prec <= 16) {
438 offset = (sliceno / lrr * l) + (sliceno % lrr);
439 offset = wrr * hrr * offset;
440 /*fprintf(stdout,"%d %d %d %d\n",offset,wrr*hrr,wrr,w);*/
441 for (i = 0; i < wrr * hrr; i++) {
442 int v = volume->comps[0].data[(i / wrr * w) + (i % wrr) + offset];
443 if (volume->comps[0].bigendian) {
444 for (j = nbytes - 1; j >= 0; j--) {
445 char byte = (char) ((v >> (j * 8)) & 0xff);
446 fwrite(&byte, 1, 1, fdest);
449 for (j = 0; j <= nbytes - 1; j++) {
450 char byte = (char) ((v >> (j * 8)) & 0xff);
451 fwrite(&byte, 1, 1, fdest);
463 /* -->> -->> -->> -->>
467 <<-- <<-- <<-- <<-- */
469 opj_volume_t* bintovolume(char *filename, char *fileimg, opj_cparameters_t *parameters) {
470 int subsampling_dx = parameters->subsampling_dx;
471 int subsampling_dy = parameters->subsampling_dy;
472 int subsampling_dz = parameters->subsampling_dz;
474 int i, compno, w, h, l, numcomps = 1;
483 OPJ_COLOR_SPACE color_space;
484 opj_volume_cmptparm_t cmptparm; /* maximum of 1 component */
485 opj_volume_t * volume = NULL;
486 opj_volume_comp_t *comp = NULL;
489 color_space = CLRSPC_GRAY;
491 fimg = fopen(fileimg,"r");
493 fprintf(stdout, "[ERROR] Failed to open %s for reading !!\n", fileimg);
497 fseek(fimg, 0, SEEK_SET);
498 while (!feof(fimg)) {
499 fgets(line,100,fimg);
500 /*fprintf(stdout,"%s %d \n",line,feof(fimg));*/
501 if (strncmp(line,"Bpp",3) == 0){
502 sscanf(line,"%*s%*[ \t]%d",&prec);
503 } else if (strncmp(line,"Color",5) == 0){
504 sscanf(line, "%*s%*[ \t]%d",&color_space);
505 } else if (strncmp(line,"Dim",3) == 0){
506 sscanf(line, "%*s%*[ \t]%d%*[ \t]%d%*[ \t]%d",&w,&h,&l);
509 /*fscanf(fimg, "Bpp%[ \t]%d%[ \t\n]",temp,&prec,temp);*/
510 /*fscanf(fimg, "Color Map%[ \t]%d%[ \n\t]Dimensions%[ \t]%d%[ \t]%d%[ \t]%d%[ \n\t]",temp,&color_space,temp,temp,&w,temp,&h,temp,&l,temp);*/
511 /*fscanf(fimg, "Resolution(mm)%[ \t]%d%[ \t]%d%[ \t]%d%[ \n\t]",temp,&subsampling_dx,temp,&subsampling_dy,temp,&subsampling_dz,temp);*/
514 fprintf(stdout, "[INFO] %d \t %d %d %d \t %3.2f %2.2f %2.2f \t %d \n",color_space,w,h,l,subsampling_dx,subsampling_dy,subsampling_dz,prec);
518 /* initialize volume components */
519 memset(&cmptparm, 0, sizeof(opj_volume_cmptparm_t));
521 cmptparm.prec = prec;
524 cmptparm.bigendian = bigendian;
525 cmptparm.dcoffset = parameters->dcoffset;
526 cmptparm.dx = subsampling_dx;
527 cmptparm.dy = subsampling_dy;
528 cmptparm.dz = subsampling_dz;
533 /* create the volume */
534 volume = opj_volume_create(numcomps, &cmptparm, color_space);
536 fprintf(stdout,"[ERROR] Unable to create volume");
541 /* set volume offset and reference grid */
542 volume->x0 = parameters->volume_offset_x0;
543 volume->y0 = parameters->volume_offset_y0;
544 volume->z0 = parameters->volume_offset_z0;
545 volume->x1 = parameters->volume_offset_x0 + (w - 1) * subsampling_dx + 1;
546 volume->y1 = parameters->volume_offset_y0 + (h - 1) * subsampling_dy + 1;
547 volume->z1 = parameters->volume_offset_z0 + (l - 1) * subsampling_dz + 1;
549 /* set volume data */
550 f = fopen(filename, "rb");
552 fprintf(stdout, "[ERROR] Failed to open %s for reading !!\n", filename);
557 for (compno = 0; compno < volume->numcomps; compno++) {
559 /* set volume data */
560 comp = &volume->comps[compno];
562 /*if (comp->prec <= 8) {
564 unsigned char *data = (unsigned char *) malloc(whl * sizeof(unsigned char));
565 fread(data, 1, whl, f);
566 for (i = 0; i < whl; i++) {
567 comp->data[i] = data[i];
568 if (comp->data[i] > max)
573 char *data = (char *) malloc(whl);
574 fread(data, 1, whl, f);
575 for (i = 0; i < whl; i++) {
576 comp->data[i] = data[i];
577 if (comp->data[i] > max)
582 } else if (comp->prec <= 16) {
584 unsigned short *data = (unsigned short *) malloc(whl * sizeof(unsigned short));
585 int leido = fread(data, 2, whl, f);
587 free(data); fclose(f);
591 for (i = 0; i < whl; i++) {
592 if (bigendian) //(c1 << 8) + c2;
593 comp->data[i] = data[i];
594 else{ //(c2 << 8) + c1;
595 comp->data[i] = ShortSwap(data[i]);
597 if (comp->data[i] > max)
602 short *data = (short *) malloc(whl);
603 int leido = fread(data, 2, whl, f);
605 free(data); fclose(f);
608 for (i = 0; i < whl; i++) {
609 if (bigendian){ //(c1 << 8) + c2;
610 comp->data[i] = data[i];
611 }else{ //(c2 << 8) + c1;
612 comp->data[i] = (short) ShortSwap((unsigned short) data[i]);
614 if (comp->data[i] > max)
621 unsigned int *data = (unsigned int *) malloc(whl * sizeof(unsigned int));
622 int leido = fread(data, 4, whl, f);
624 free(data); fclose(f);
626 } for (i = 0; i < whl; i++) {
628 comp->data[i] = LongSwap(data[i]);
630 comp->data[i] = data[i];
631 if (comp->data[i] > max)
636 int leido = fread(comp->data, 4, whl, f);
641 for (i = 0; i < whl; i++) {
643 comp->data[i] = (int) LongSwap((unsigned int) comp->data[i]);
644 if (comp->data[i] > max)
650 for (i = 0; i < whl; i++) {
652 if (comp->prec <= 8) {
656 v = (char) readuchar(f);
658 } else if (comp->prec <= 16) {
660 v = readushort(f, bigendian);
662 v = (short) readushort(f, bigendian);
666 v = readuint(f, bigendian);
668 v = (int) readuint(f, bigendian);
675 comp->bpp = int_floorlog2(max) + 1;
681 int volumetobin(opj_volume_t * volume, char *outfile) {
682 int w, wr, wrr, h, hr, hrr, l, lr, lrr, max;
683 int i,j, compno, nbytes;
686 FILE *fimgdest = NULL;
690 for (compno = 0; compno < 1; compno++) { /*Only one component*/
692 fdest = fopen(outfile, "wb");
694 fprintf(stdout, "[ERROR] Failed to open %s for writing\n", outfile);
697 fprintf(stdout,"[INFO] Writing outfile %s (%s) \n",outfile, volume->comps[0].bigendian ? "Bigendian" : "Little-endian");
699 w = int_ceildiv(volume->x1 - volume->x0, volume->comps[compno].dx);
700 wr = volume->comps[compno].w;
701 wrr = int_ceildivpow2(volume->comps[compno].w, volume->comps[compno].factor[0]);
703 h = int_ceildiv(volume->y1 - volume->y0, volume->comps[compno].dy);
704 hr = volume->comps[compno].h;
705 hrr = int_ceildivpow2(volume->comps[compno].h, volume->comps[compno].factor[1]);
707 l = int_ceildiv(volume->z1 - volume->z0, volume->comps[compno].dz);
708 lr = volume->comps[compno].l;
709 lrr = int_ceildivpow2(volume->comps[compno].l, volume->comps[compno].factor[2]);
711 max = (volume->comps[compno].prec <= 8) ? 255 : (1 << volume->comps[compno].prec) - 1;
713 volume->comps[compno].x0 = int_ceildivpow2(volume->comps[compno].x0 - int_ceildiv(volume->x0, volume->comps[compno].dx), volume->comps[compno].factor[0]);
714 volume->comps[compno].y0 = int_ceildivpow2(volume->comps[compno].y0 - int_ceildiv(volume->y0, volume->comps[compno].dy), volume->comps[compno].factor[1]);
715 volume->comps[compno].z0 = int_ceildivpow2(volume->comps[compno].z0 - int_ceildiv(volume->z0, volume->comps[compno].dz), volume->comps[compno].factor[2]);
717 if (volume->comps[0].prec <= 8) {
719 } else if (volume->comps[0].prec <= 16) {
725 /*fprintf(stdout,"w %d wr %d wrr %d h %d hr %d hrr %d l %d lr %d lrr %d max %d nbytes %d\n Factor %d %d %d",w,wr,wrr,h,hr,hrr,l,lr,lrr,max,nbytes,volume->comps[compno].factor[0],volume->comps[compno].factor[1],volume->comps[compno].factor[2]);*/
727 for(sliceno = 0; sliceno < lrr; sliceno++) {
728 offset = (sliceno / lrr * l) + (sliceno % lrr);
729 offset = wrr * hrr * offset;
730 for (i = 0; i < wrr * hrr; i++) {
731 int v = volume->comps[0].data[(i / wrr * w) + (i % wrr) + offset];
732 if (volume->comps[0].bigendian) {
733 for (j = nbytes - 1; j >= 0; j--) {
734 char byte = (char) ((v >> (j * 8)) & 0xff);
735 fwrite(&byte, 1, 1, fdest);
738 for (j = 0; j <= nbytes - 1; j++) {
739 char byte = (char) ((v >> (j * 8)) & 0xff);
740 fwrite(&byte, 1, 1, fdest);
750 sprintf(name,"%s.img",outfile);
751 fimgdest = fopen(name, "w");
753 fprintf(stdout, "[ERROR] Failed to open %s for writing\n", name);
756 fprintf(fimgdest, "Bpp\t%d\nColor Map\t2\nDimensions\t%d\t%d\t%d\nResolution(mm)\t%d\t%d\t%d\t\n",
757 volume->comps[0].prec,wrr,hrr,lrr,volume->comps[0].dx,volume->comps[0].dy,volume->comps[0].dz);
762 /* -->> -->> -->> -->>
766 <<-- <<-- <<-- <<-- */
767 opj_volume_t* imgtovolume(char *fileimg, opj_cparameters_t *parameters) {
768 int subsampling_dx = parameters->subsampling_dx;
769 int subsampling_dy = parameters->subsampling_dy;
770 int subsampling_dz = parameters->subsampling_dz;
772 int i, compno, w, h, l, numcomps = 1;
773 int prec, max = 0, min = 0;
775 char filename[100], tmpdirpath[100], dirpath[100], *tmp;
776 char line[100], datatype[100];
781 OPJ_COLOR_SPACE color_space;
782 opj_volume_cmptparm_t cmptparm; /* maximum of 1 component */
783 opj_volume_t * volume = NULL;
784 opj_volume_comp_t *comp = NULL;
787 color_space = CLRSPC_GRAY;
789 fimg = fopen(fileimg,"r");
791 fprintf(stderr, "[ERROR] Failed to open %s for reading !!\n", fileimg);
795 /*Fetch only the path */
796 strcpy(tmpdirpath,fileimg);
797 if ((tmp = strrchr(tmpdirpath,'/')) != NULL){
799 strcpy(dirpath,tmpdirpath);
801 strcpy(dirpath,"./");
804 fseek(fimg, 0, SEEK_SET);
805 while (!feof(fimg)) {
806 fgets(line,100,fimg);
807 /*fprintf(stdout,"%s %d \n",line,feof(fimg));*/
808 if (strncmp(line,"Image",5) == 0){
809 sscanf(line,"%*s%*[ \t]%s",datatype);
810 } else if (strncmp(line,"File",4) == 0){
811 sscanf(line,"%*s %*s%*[ \t]%s",filename);
812 strcat(dirpath, filename);
813 strcpy(filename,dirpath);
814 } else if (strncmp(line,"Min",3) == 0){
815 sscanf(line,"%*s %*s%*[ \t]%d%*[ \t]%d",&min,&max);
816 prec = int_floorlog2(max - min + 1);
817 } else if (strncmp(line,"Bpp",3) == 0){
818 sscanf(line,"%*s%*[ \t]%d",&prec);
819 } else if (strncmp(line,"Color",5) == 0){
820 sscanf(line, "%*s %*s%*[ \t]%d",&color_space);
821 } else if (strncmp(line,"Dim",3) == 0){
822 sscanf(line, "%*s%*[ \t]%d%*[ \t]%d%*[ \t]%d",&w,&h,&l);
823 } else if (strncmp(line,"Res",3) == 0){
824 sscanf(line,"%*s%*[ \t]%f%*[ \t]%f%*[ \t]%f",&dx,&dy,&dz);
829 fprintf(stdout, "[INFO] %s %d \t %d %d %d \t %f %f %f \t %d %d %d \n",filename,color_space,w,h,l,dx,dy,dz,max,min,prec);
834 if ( !prec || !w || !h || !l ){
835 fprintf(stderr,"[ERROR] Unable to read IMG file correctly. Found some null values.");
839 /* initialize volume components */
840 memset(&cmptparm, 0, sizeof(opj_volume_cmptparm_t));
842 cmptparm.prec = prec;
845 cmptparm.bigendian = bigendian;
846 cmptparm.dcoffset = parameters->dcoffset;
847 cmptparm.dx = subsampling_dx;
848 cmptparm.dy = subsampling_dy;
849 cmptparm.dz = subsampling_dz;
854 /* create the volume */
855 volume = opj_volume_create(numcomps, &cmptparm, color_space);
857 fprintf(stdout,"[ERROR] Unable to create volume");
861 /* set volume offset and reference grid */
862 volume->x0 = parameters->volume_offset_x0;
863 volume->y0 = parameters->volume_offset_y0;
864 volume->z0 = parameters->volume_offset_z0;
865 volume->x1 = parameters->volume_offset_x0 + (w - 1) * subsampling_dx + 1;
866 volume->y1 = parameters->volume_offset_y0 + (h - 1) * subsampling_dy + 1;
867 volume->z1 = parameters->volume_offset_z0 + (l - 1) * subsampling_dz + 1;
870 /* set volume data */
871 f = fopen(filename, "rb");
873 fprintf(stderr, "[ERROR] Failed to open %s for reading !!\n", filename);
879 for (compno = 0; compno < volume->numcomps; compno++) {
881 /* set volume data */
882 comp = &volume->comps[compno];
884 /*if (comp->prec <= 8) {
886 unsigned char *data = (unsigned char *) malloc(whl * sizeof(unsigned char));
887 fread(data, 1, whl, f);
888 for (i = 0; i < whl; i++) {
889 comp->data[i] = data[i];
890 if (comp->data[i] > max)
895 char *data = (char *) malloc(whl);
896 fread(data, 1, whl, f);
897 for (i = 0; i < whl; i++) {
898 comp->data[i] = data[i];
899 if (comp->data[i] > max)
904 } else if (comp->prec <= 16) {
906 unsigned short *data = (unsigned short *) malloc(whl * sizeof(unsigned short));
907 int leido = fread(data, 2, whl, f);
909 free(data); fclose(f);
913 for (i = 0; i < whl; i++) {
914 if (bigendian) //(c1 << 8) + c2;
915 comp->data[i] = data[i];
916 else{ //(c2 << 8) + c1;
917 comp->data[i] = ShortSwap(data[i]);
919 if (comp->data[i] > max)
924 short *data = (short *) malloc(whl);
925 int leido = fread(data, 2, whl, f);
927 free(data); fclose(f);
930 for (i = 0; i < whl; i++) {
931 if (bigendian){ //(c1 << 8) + c2;
932 comp->data[i] = data[i];
933 }else{ //(c2 << 8) + c1;
934 comp->data[i] = (short) ShortSwap((unsigned short) data[i]);
936 if (comp->data[i] > max)
943 unsigned int *data = (unsigned int *) malloc(whl * sizeof(unsigned int));
944 int leido = fread(data, 4, whl, f);
946 free(data); fclose(f);
948 } for (i = 0; i < whl; i++) {
950 comp->data[i] = LongSwap(data[i]);
952 comp->data[i] = data[i];
953 if (comp->data[i] > max)
958 int leido = fread(comp->data, 4, whl, f);
963 for (i = 0; i < whl; i++) {
965 comp->data[i] = (int) LongSwap((unsigned int) comp->data[i]);
966 if (comp->data[i] > max)
972 for (i = 0; i < whl; i++) {
974 if (comp->prec <= 8) {
978 v = (char) readuchar(f);
980 } else if (comp->prec <= 16) {
982 v = readushort(f, bigendian);
984 v = (short) readushort(f, bigendian);
988 v = readuint(f, bigendian);
990 v = (int) readuint(f, bigendian);
997 comp->bpp = int_floorlog2(max) + 1;