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)
51 fprintf(fd, "volume {\n");
52 fprintf(fd, " x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d\n", vol->x0, vol->y0,
53 vol->z0, vol->x1, vol->y1, vol->z1);
54 fprintf(fd, " numcomps=%d\n", vol->numcomps);
55 for (compno = 0; compno < vol->numcomps; compno++) {
56 opj_volume_comp_t *comp = &vol->comps[compno];
57 fprintf(fd, " comp %d {\n", compno);
58 fprintf(fd, " dx=%d, dy=%d, dz=%d\n", comp->dx, comp->dy, comp->dz);
59 fprintf(fd, " prec=%d\n", comp->prec);
60 fprintf(fd, " sgnd=%d\n", comp->sgnd);
67 * Get logarithm of an integer and round downwards.
71 static int int_floorlog2(int a)
74 for (l = 0; a > 1; l++) {
81 * Divide an integer by a power of 2 and round upwards.
85 static int int_ceildivpow2(int a, int b)
87 return (a + (1 << b) - 1) >> b;
91 * Divide an integer and round upwards.
95 static int int_ceildiv(int a, int b)
97 return (a + b - 1) / b;
101 /* -->> -->> -->> -->>
105 <<-- <<-- <<-- <<-- */
108 unsigned char readuchar(FILE * f)
115 unsigned short readushort(FILE * f, int bigendian)
117 unsigned char c1, c2;
121 return (c1 << 8) + c2;
123 return (c2 << 8) + c1;
127 unsigned int readuint(FILE * f, int bigendian)
129 unsigned char c1, c2, c3, c4;
135 return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
137 return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
140 /*****************************************/
141 static unsigned short ShortSwap(unsigned short v)
143 unsigned char c1, c2;
145 c2 = (v >> 8) & 0xff;
146 return (c1 << 8) + c2;
149 static unsigned int LongSwap(unsigned int i)
151 unsigned char b1, b2, b3, b4;
154 b3 = (i >> 16) & 255;
155 b4 = (i >> 24) & 255;
156 return ((int)b1 << 24) + ((int)b2 << 16) + ((int)b3 << 8) + b4;
158 /*****************************************/
160 opj_volume_t* pgxtovolume(char *relpath, opj_cparameters_t *parameters)
165 unsigned long offset;
166 int i, s, numcomps, maxvalue, sliceno, slicepos, maxslice = 0;
168 OPJ_COLOR_SPACE color_space;
169 opj_volume_cmptparm_t cmptparm; /* maximum of 1 component */
170 opj_volume_t * volume = NULL;
172 char endian1, endian2, sign;
175 opj_volume_comp_t *comp = NULL;
178 struct dirent *direntp;
180 char *tmp = NULL, *tmp2 = NULL,
181 *point = NULL, *pgx = NULL;
182 char tmpdirpath[MAX_PATH];
183 char dirpath[MAX_PATH];
184 char pattern[MAX_PATH];
185 char pgxfiles[MAX_SLICES][MAX_PATH];
186 int pgxslicepos[MAX_SLICES];
190 color_space = CLRSPC_GRAY;
193 memset(pgxfiles, 0, MAX_SLICES * MAX_PATH * sizeof(char));
194 memset(&cmptparm, 0, sizeof(opj_volume_cmptparm_t));
196 /* Separaci�n del caso de un �nico slice frente al de muchos */
197 if ((tmp = strrchr(relpath, '-')) == NULL) {
198 /*fprintf(stdout,"[INFO] A volume of only one slice....\n");*/
201 strcpy(pgxfiles[0], relpath);
204 /*Fetch only the path */
205 strcpy(tmpdirpath, relpath);
206 if ((tmp = strrchr(tmpdirpath, '/')) != NULL) {
209 strcpy(dirpath, tmpdirpath);
211 strcpy(dirpath, "./");
214 /*Fetch the pattern of the volume slices*/
215 if ((tmp = strrchr(relpath, '/')) != NULL) {
220 if ((tmp2 = strrchr(tmp, '-')) != NULL) {
223 fprintf(stdout, "[ERROR] tmp2 ha dado null. no ha encontrado el * %s %s", tmp,
227 strcpy(pattern, tmp);
229 dirp = opendir(dirpath);
232 "[ERROR] Infile must be a .pgx file or a directory that contain pgx files");
236 /*Read all .pgx files of directory */
237 while ((direntp = readdir(dirp)) != NULL) {
238 /* Found a directory, but ignore . and .. */
239 if (strcmp(".", direntp->d_name) == 0 || strcmp("..", direntp->d_name) == 0) {
243 if (((pgx = strstr(direntp->d_name, pattern)) != NULL) &&
244 ((tmp2 = strstr(direntp->d_name, ".pgx")) != NULL)) {
246 strcpy(tmp, dirpath);
247 tmp = strcat(tmp, direntp->d_name);
249 /*Obtenemos el index de la secuencia de slices*/
250 if ((tmp2 = strpbrk(direntp->d_name, "0123456789")) == NULL) {
254 while (tmp2 != NULL) {
257 tmp2 = strpbrk(tmp2 + 1, "0123456789");
261 /*Comprobamos que no estamos leyendo algo raro como pattern.jp3d*/
262 if ((point = strpbrk(point, ".")) == NULL) {
265 /*Slicepos --> index de slice; Sliceno --> no de slices hasta el momento*/
266 slicepos = atoi(tmpno);
267 pgxslicepos[sliceno] = slicepos - 1;
269 if (slicepos > maxslice) {
273 /*Colocamos el slices en su posicion correspondiente*/
274 strcpy(pgxfiles[slicepos - 1], tmp);
278 }/* else if pattern*.pgx */
282 "[ERROR] No slices with this pattern founded !! Please check input volume name\n");
285 /*if ( maxslice != sliceno) {
286 fprintf(stdout,"[ERROR] Slices are not sequentially numbered !! Please rename them accordingly\n");
290 for (s = 0; s < sliceno; s++) {
291 int pos = maxslice == sliceno ? s : pgxslicepos[s];
292 f = fopen(pgxfiles[pos], "rb");
294 fprintf(stdout, "[ERROR] Failed to open %s for reading !\n", pgxfiles[s]);
297 fprintf(stdout, "[INFO] Loading %s \n", pgxfiles[pos]);
299 fseek(f, 0, SEEK_SET);
300 fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d", temp, &endian1, &endian2,
301 signtmp, &prec, temp, &w, temp, &h);
305 while (signtmp[i] != '\0') {
306 if (signtmp[i] == '-') {
313 if (endian1 == 'M' && endian2 == 'L') {
314 cmptparm.bigendian = 1;
315 } else if (endian2 == 'M' && endian1 == 'L') {
316 cmptparm.bigendian = 0;
318 fprintf(stdout, "[ERROR] Bad pgx header, please check input file\n");
323 /* initialize volume component */
325 cmptparm.x0 = parameters->volume_offset_x0;
326 cmptparm.y0 = parameters->volume_offset_y0;
327 cmptparm.z0 = parameters->volume_offset_z0;
328 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 :
329 cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
330 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 :
331 cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
332 cmptparm.l = !cmptparm.z0 ? (sliceno - 1) * parameters->subsampling_dz + 1 :
333 cmptparm.z0 + (sliceno - 1) * parameters->subsampling_dz + 1;
340 cmptparm.prec = prec;
342 cmptparm.dcoffset = parameters->dcoffset;
343 cmptparm.dx = parameters->subsampling_dx;
344 cmptparm.dy = parameters->subsampling_dy;
345 cmptparm.dz = parameters->subsampling_dz;
347 /* create the volume */
348 volume = opj_volume_create(numcomps, &cmptparm, color_space);
353 /* set volume offset and reference grid */
354 volume->x0 = cmptparm.x0;
355 volume->y0 = cmptparm.y0;
356 volume->z0 = cmptparm.z0;
357 volume->x1 = cmptparm.w;
358 volume->y1 = cmptparm.h;
359 volume->z1 = cmptparm.l;
361 /* set volume data :only one component, that is a volume*/
362 comp = &volume->comps[0];
368 for (i = 0; i < w * h; i++) {
370 if (comp->prec <= 8) {
374 v = (char) readuchar(f);
376 } else if (comp->prec <= 16) {
378 v = readushort(f, cmptparm.bigendian);
380 v = (short) readushort(f, cmptparm.bigendian);
384 v = readuint(f, cmptparm.bigendian);
386 v = (int) readuint(f, cmptparm.bigendian);
392 comp->data[i + offset] = v;
396 } /* for s --> sliceno*/
397 comp->bpp = int_floorlog2(maxvalue) + 1;
401 /*dump_volume(stdout, volume);*/
406 int volumetopgx(opj_volume_t * volume, char *outfile)
408 int w, wr, wrr, h, hr, hrr, l, lr, lrr;
409 int i, j, compno, offset, sliceno;
412 for (compno = 0; compno < volume->numcomps; compno++) {
413 opj_volume_comp_t *comp = &volume->comps[compno];
420 while (*tmp != '.') {
424 for (sliceno = 0; sliceno < volume->z1 - volume->z0; sliceno++) {
426 if (volume->numcomps > 1) {
427 sprintf(name, "%s%d-%d.pgx", outfile, sliceno + 1, compno);
428 } else if ((volume->z1 - volume->z0) > 1) {
429 sprintf(name, "%s%d.pgx", outfile, sliceno + 1);
431 sprintf(name, "%s.pgx", outfile);
434 fdest = fopen(name, "wb");
436 fprintf(stdout, "[ERROR] Failed to open %s for writing \n", name);
440 fprintf(stdout, "[INFO] Writing in %s (%s)\n", name,
441 volume->comps[0].bigendian ? "Bigendian" : "Little-endian");
443 w = int_ceildiv(volume->x1 - volume->x0, volume->comps[compno].dx);
444 wr = volume->comps[compno].w;
445 wrr = int_ceildivpow2(volume->comps[compno].w, volume->comps[compno].factor[0]);
447 h = int_ceildiv(volume->y1 - volume->y0, volume->comps[compno].dy);
448 hr = volume->comps[compno].h;
449 hrr = int_ceildivpow2(volume->comps[compno].h, volume->comps[compno].factor[1]);
451 l = int_ceildiv(volume->z1 - volume->z0, volume->comps[compno].dz);
452 lr = volume->comps[compno].l;
453 lrr = int_ceildivpow2(volume->comps[compno].l, volume->comps[compno].factor[2]);
455 fprintf(fdest, "PG %c%c %c%d %d %d\n", comp->bigendian ? 'M' : 'L',
456 comp->bigendian ? 'L' : 'M', comp->sgnd ? '-' : '+', comp->prec, wr, hr);
457 if (comp->prec <= 8) {
459 } else if (comp->prec <= 16) {
465 offset = (sliceno / lrr * l) + (sliceno % lrr);
466 offset = wrr * hrr * offset;
467 /*fprintf(stdout,"%d %d %d %d\n",offset,wrr*hrr,wrr,w);*/
468 for (i = 0; i < wrr * hrr; i++) {
469 int v = volume->comps[0].data[(i / wrr * w) + (i % wrr) + offset];
470 if (volume->comps[0].bigendian) {
471 for (j = nbytes - 1; j >= 0; j--) {
472 char byte = (char)((v >> (j * 8)) & 0xff);
473 fwrite(&byte, 1, 1, fdest);
476 for (j = 0; j <= nbytes - 1; j++) {
477 char byte = (char)((v >> (j * 8)) & 0xff);
478 fwrite(&byte, 1, 1, fdest);
490 /* -->> -->> -->> -->>
494 <<-- <<-- <<-- <<-- */
496 opj_volume_t* bintovolume(char *filename, char *fileimg,
497 opj_cparameters_t *parameters)
499 int subsampling_dx = parameters->subsampling_dx;
500 int subsampling_dy = parameters->subsampling_dy;
501 int subsampling_dz = parameters->subsampling_dz;
503 int i, compno, w, h, l, numcomps = 1;
512 OPJ_COLOR_SPACE color_space;
513 opj_volume_cmptparm_t cmptparm; /* maximum of 1 component */
514 opj_volume_t * volume = NULL;
515 opj_volume_comp_t *comp = NULL;
518 color_space = CLRSPC_GRAY;
520 fimg = fopen(fileimg, "r");
522 fprintf(stdout, "[ERROR] Failed to open %s for reading !!\n", fileimg);
526 fseek(fimg, 0, SEEK_SET);
527 while (!feof(fimg)) {
528 fgets(line, 100, fimg);
529 /*fprintf(stdout,"%s %d \n",line,feof(fimg));*/
530 if (strncmp(line, "Bpp", 3) == 0) {
531 sscanf(line, "%*s%*[ \t]%d", &prec);
532 } else if (strncmp(line, "Color", 5) == 0) {
533 sscanf(line, "%*s%*[ \t]%d", &color_space);
534 } else if (strncmp(line, "Dim", 3) == 0) {
535 sscanf(line, "%*s%*[ \t]%d%*[ \t]%d%*[ \t]%d", &w, &h, &l);
538 /*fscanf(fimg, "Bpp%[ \t]%d%[ \t\n]",temp,&prec,temp);*/
539 /*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);*/
540 /*fscanf(fimg, "Resolution(mm)%[ \t]%d%[ \t]%d%[ \t]%d%[ \n\t]",temp,&subsampling_dx,temp,&subsampling_dy,temp,&subsampling_dz,temp);*/
543 fprintf(stdout, "[INFO] %d \t %d %d %d \t %3.2f %2.2f %2.2f \t %d \n",
544 color_space, w, h, l, subsampling_dx, subsampling_dy, subsampling_dz, prec);
548 /* initialize volume components */
549 memset(&cmptparm, 0, sizeof(opj_volume_cmptparm_t));
551 cmptparm.prec = prec;
554 cmptparm.bigendian = bigendian;
555 cmptparm.dcoffset = parameters->dcoffset;
556 cmptparm.dx = subsampling_dx;
557 cmptparm.dy = subsampling_dy;
558 cmptparm.dz = subsampling_dz;
563 /* create the volume */
564 volume = opj_volume_create(numcomps, &cmptparm, color_space);
566 fprintf(stdout, "[ERROR] Unable to create volume");
571 /* set volume offset and reference grid */
572 volume->x0 = parameters->volume_offset_x0;
573 volume->y0 = parameters->volume_offset_y0;
574 volume->z0 = parameters->volume_offset_z0;
575 volume->x1 = parameters->volume_offset_x0 + (w - 1) * subsampling_dx + 1;
576 volume->y1 = parameters->volume_offset_y0 + (h - 1) * subsampling_dy + 1;
577 volume->z1 = parameters->volume_offset_z0 + (l - 1) * subsampling_dz + 1;
579 /* set volume data */
580 f = fopen(filename, "rb");
582 fprintf(stdout, "[ERROR] Failed to open %s for reading !!\n", filename);
587 for (compno = 0; compno < volume->numcomps; compno++) {
589 /* set volume data */
590 comp = &volume->comps[compno];
592 /*if (comp->prec <= 8) {
594 unsigned char *data = (unsigned char *) malloc(whl * sizeof(unsigned char));
595 fread(data, 1, whl, f);
596 for (i = 0; i < whl; i++) {
597 comp->data[i] = data[i];
598 if (comp->data[i] > max)
603 char *data = (char *) malloc(whl);
604 fread(data, 1, whl, f);
605 for (i = 0; i < whl; i++) {
606 comp->data[i] = data[i];
607 if (comp->data[i] > max)
612 } else if (comp->prec <= 16) {
614 unsigned short *data = (unsigned short *) malloc(whl * sizeof(unsigned short));
615 int leido = fread(data, 2, whl, f);
617 free(data); fclose(f);
621 for (i = 0; i < whl; i++) {
622 if (bigendian) //(c1 << 8) + c2;
623 comp->data[i] = data[i];
624 else{ //(c2 << 8) + c1;
625 comp->data[i] = ShortSwap(data[i]);
627 if (comp->data[i] > max)
632 short *data = (short *) malloc(whl);
633 int leido = fread(data, 2, whl, f);
635 free(data); fclose(f);
638 for (i = 0; i < whl; i++) {
639 if (bigendian){ //(c1 << 8) + c2;
640 comp->data[i] = data[i];
641 }else{ //(c2 << 8) + c1;
642 comp->data[i] = (short) ShortSwap((unsigned short) data[i]);
644 if (comp->data[i] > max)
651 unsigned int *data = (unsigned int *) malloc(whl * sizeof(unsigned int));
652 int leido = fread(data, 4, whl, f);
654 free(data); fclose(f);
656 } for (i = 0; i < whl; i++) {
658 comp->data[i] = LongSwap(data[i]);
660 comp->data[i] = data[i];
661 if (comp->data[i] > max)
666 int leido = fread(comp->data, 4, whl, f);
671 for (i = 0; i < whl; i++) {
673 comp->data[i] = (int) LongSwap((unsigned int) comp->data[i]);
674 if (comp->data[i] > max)
680 for (i = 0; i < whl; i++) {
682 if (comp->prec <= 8) {
686 v = (char) readuchar(f);
688 } else if (comp->prec <= 16) {
690 v = readushort(f, bigendian);
692 v = (short) readushort(f, bigendian);
696 v = readuint(f, bigendian);
698 v = (int) readuint(f, bigendian);
706 comp->bpp = int_floorlog2(max) + 1;
712 int volumetobin(opj_volume_t * volume, char *outfile)
714 int w, wr, wrr, h, hr, hrr, l, lr, lrr, max;
715 int i, j, compno, nbytes;
718 FILE *fimgdest = NULL;
722 for (compno = 0; compno < 1; compno++) { /*Only one component*/
724 fdest = fopen(outfile, "wb");
726 fprintf(stdout, "[ERROR] Failed to open %s for writing\n", outfile);
729 fprintf(stdout, "[INFO] Writing outfile %s (%s) \n", outfile,
730 volume->comps[0].bigendian ? "Bigendian" : "Little-endian");
732 w = int_ceildiv(volume->x1 - volume->x0, volume->comps[compno].dx);
733 wr = volume->comps[compno].w;
734 wrr = int_ceildivpow2(volume->comps[compno].w, volume->comps[compno].factor[0]);
736 h = int_ceildiv(volume->y1 - volume->y0, volume->comps[compno].dy);
737 hr = volume->comps[compno].h;
738 hrr = int_ceildivpow2(volume->comps[compno].h, volume->comps[compno].factor[1]);
740 l = int_ceildiv(volume->z1 - volume->z0, volume->comps[compno].dz);
741 lr = volume->comps[compno].l;
742 lrr = int_ceildivpow2(volume->comps[compno].l, volume->comps[compno].factor[2]);
744 max = (volume->comps[compno].prec <= 8) ? 255 : (1 <<
745 volume->comps[compno].prec) - 1;
747 volume->comps[compno].x0 = int_ceildivpow2(volume->comps[compno].x0 -
748 int_ceildiv(volume->x0, volume->comps[compno].dx),
749 volume->comps[compno].factor[0]);
750 volume->comps[compno].y0 = int_ceildivpow2(volume->comps[compno].y0 -
751 int_ceildiv(volume->y0, volume->comps[compno].dy),
752 volume->comps[compno].factor[1]);
753 volume->comps[compno].z0 = int_ceildivpow2(volume->comps[compno].z0 -
754 int_ceildiv(volume->z0, volume->comps[compno].dz),
755 volume->comps[compno].factor[2]);
757 if (volume->comps[0].prec <= 8) {
759 } else if (volume->comps[0].prec <= 16) {
765 /*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]);*/
767 for (sliceno = 0; sliceno < lrr; sliceno++) {
768 offset = (sliceno / lrr * l) + (sliceno % lrr);
769 offset = wrr * hrr * offset;
770 for (i = 0; i < wrr * hrr; i++) {
771 int v = volume->comps[0].data[(i / wrr * w) + (i % wrr) + offset];
772 if (volume->comps[0].bigendian) {
773 for (j = nbytes - 1; j >= 0; j--) {
774 char byte = (char)((v >> (j * 8)) & 0xff);
775 fwrite(&byte, 1, 1, fdest);
778 for (j = 0; j <= nbytes - 1; j++) {
779 char byte = (char)((v >> (j * 8)) & 0xff);
780 fwrite(&byte, 1, 1, fdest);
790 sprintf(name, "%s.img", outfile);
791 fimgdest = fopen(name, "w");
793 fprintf(stdout, "[ERROR] Failed to open %s for writing\n", name);
797 "Bpp\t%d\nColor Map\t2\nDimensions\t%d\t%d\t%d\nResolution(mm)\t%d\t%d\t%d\t\n",
798 volume->comps[0].prec, wrr, hrr, lrr, volume->comps[0].dx, volume->comps[0].dy,
799 volume->comps[0].dz);
804 /* -->> -->> -->> -->>
808 <<-- <<-- <<-- <<-- */
809 opj_volume_t* imgtovolume(char *fileimg, opj_cparameters_t *parameters)
811 int subsampling_dx = parameters->subsampling_dx;
812 int subsampling_dy = parameters->subsampling_dy;
813 int subsampling_dz = parameters->subsampling_dz;
815 int i, compno, w, h, l, numcomps = 1;
816 int prec, max = 0, min = 0;
818 char filename[100], tmpdirpath[100], dirpath[100], *tmp;
819 char line[100], datatype[100];
824 OPJ_COLOR_SPACE color_space;
825 opj_volume_cmptparm_t cmptparm; /* maximum of 1 component */
826 opj_volume_t * volume = NULL;
827 opj_volume_comp_t *comp = NULL;
830 color_space = CLRSPC_GRAY;
832 fimg = fopen(fileimg, "r");
834 fprintf(stderr, "[ERROR] Failed to open %s for reading !!\n", fileimg);
838 /*Fetch only the path */
839 strcpy(tmpdirpath, fileimg);
840 if ((tmp = strrchr(tmpdirpath, '/')) != NULL) {
843 strcpy(dirpath, tmpdirpath);
845 strcpy(dirpath, "./");
848 fseek(fimg, 0, SEEK_SET);
849 while (!feof(fimg)) {
850 fgets(line, 100, fimg);
851 /*fprintf(stdout,"%s %d \n",line,feof(fimg));*/
852 if (strncmp(line, "Image", 5) == 0) {
853 sscanf(line, "%*s%*[ \t]%s", datatype);
854 } else if (strncmp(line, "File", 4) == 0) {
855 sscanf(line, "%*s %*s%*[ \t]%s", filename);
856 strcat(dirpath, filename);
857 strcpy(filename, dirpath);
858 } else if (strncmp(line, "Min", 3) == 0) {
859 sscanf(line, "%*s %*s%*[ \t]%d%*[ \t]%d", &min, &max);
860 prec = int_floorlog2(max - min + 1);
861 } else if (strncmp(line, "Bpp", 3) == 0) {
862 sscanf(line, "%*s%*[ \t]%d", &prec);
863 } else if (strncmp(line, "Color", 5) == 0) {
864 sscanf(line, "%*s %*s%*[ \t]%d", &color_space);
865 } else if (strncmp(line, "Dim", 3) == 0) {
866 sscanf(line, "%*s%*[ \t]%d%*[ \t]%d%*[ \t]%d", &w, &h, &l);
867 } else if (strncmp(line, "Res", 3) == 0) {
868 sscanf(line, "%*s%*[ \t]%f%*[ \t]%f%*[ \t]%f", &dx, &dy, &dz);
873 fprintf(stdout, "[INFO] %s %d \t %d %d %d \t %f %f %f \t %d %d %d \n", filename,
874 color_space, w, h, l, dx, dy, dz, max, min, prec);
879 if (!prec || !w || !h || !l) {
881 "[ERROR] Unable to read IMG file correctly. Found some null values.");
885 /* initialize volume components */
886 memset(&cmptparm, 0, sizeof(opj_volume_cmptparm_t));
888 cmptparm.prec = prec;
891 cmptparm.bigendian = bigendian;
892 cmptparm.dcoffset = parameters->dcoffset;
893 cmptparm.dx = subsampling_dx;
894 cmptparm.dy = subsampling_dy;
895 cmptparm.dz = subsampling_dz;
900 /* create the volume */
901 volume = opj_volume_create(numcomps, &cmptparm, color_space);
903 fprintf(stdout, "[ERROR] Unable to create volume");
907 /* set volume offset and reference grid */
908 volume->x0 = parameters->volume_offset_x0;
909 volume->y0 = parameters->volume_offset_y0;
910 volume->z0 = parameters->volume_offset_z0;
911 volume->x1 = parameters->volume_offset_x0 + (w - 1) * subsampling_dx + 1;
912 volume->y1 = parameters->volume_offset_y0 + (h - 1) * subsampling_dy + 1;
913 volume->z1 = parameters->volume_offset_z0 + (l - 1) * subsampling_dz + 1;
916 /* set volume data */
917 f = fopen(filename, "rb");
919 fprintf(stderr, "[ERROR] Failed to open %s for reading !!\n", filename);
925 for (compno = 0; compno < volume->numcomps; compno++) {
927 /* set volume data */
928 comp = &volume->comps[compno];
930 /*if (comp->prec <= 8) {
932 unsigned char *data = (unsigned char *) malloc(whl * sizeof(unsigned char));
933 fread(data, 1, whl, f);
934 for (i = 0; i < whl; i++) {
935 comp->data[i] = data[i];
936 if (comp->data[i] > max)
941 char *data = (char *) malloc(whl);
942 fread(data, 1, whl, f);
943 for (i = 0; i < whl; i++) {
944 comp->data[i] = data[i];
945 if (comp->data[i] > max)
950 } else if (comp->prec <= 16) {
952 unsigned short *data = (unsigned short *) malloc(whl * sizeof(unsigned short));
953 int leido = fread(data, 2, whl, f);
955 free(data); fclose(f);
959 for (i = 0; i < whl; i++) {
960 if (bigendian) //(c1 << 8) + c2;
961 comp->data[i] = data[i];
962 else{ //(c2 << 8) + c1;
963 comp->data[i] = ShortSwap(data[i]);
965 if (comp->data[i] > max)
970 short *data = (short *) malloc(whl);
971 int leido = fread(data, 2, whl, f);
973 free(data); fclose(f);
976 for (i = 0; i < whl; i++) {
977 if (bigendian){ //(c1 << 8) + c2;
978 comp->data[i] = data[i];
979 }else{ //(c2 << 8) + c1;
980 comp->data[i] = (short) ShortSwap((unsigned short) data[i]);
982 if (comp->data[i] > max)
989 unsigned int *data = (unsigned int *) malloc(whl * sizeof(unsigned int));
990 int leido = fread(data, 4, whl, f);
992 free(data); fclose(f);
994 } for (i = 0; i < whl; i++) {
996 comp->data[i] = LongSwap(data[i]);
998 comp->data[i] = data[i];
999 if (comp->data[i] > max)
1000 max = comp->data[i];
1004 int leido = fread(comp->data, 4, whl, f);
1009 for (i = 0; i < whl; i++) {
1011 comp->data[i] = (int) LongSwap((unsigned int) comp->data[i]);
1012 if (comp->data[i] > max)
1013 max = comp->data[i];
1018 for (i = 0; i < whl; i++) {
1020 if (comp->prec <= 8) {
1024 v = (char) readuchar(f);
1026 } else if (comp->prec <= 16) {
1028 v = readushort(f, bigendian);
1030 v = (short) readushort(f, bigendian);
1034 v = readuint(f, bigendian);
1036 v = (int) readuint(f, bigendian);
1044 comp->bpp = int_floorlog2(max) + 1;