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");
286 /*if ( maxslice != sliceno) {
287 fprintf(stdout,"[ERROR] Slices are not sequentially numbered !! Please rename them accordingly\n");
291 for (s = 0; s < sliceno; s++) {
292 int pos = maxslice == sliceno ? s : pgxslicepos[s];
293 f = fopen(pgxfiles[pos], "rb");
295 fprintf(stdout, "[ERROR] Failed to open %s for reading !\n", pgxfiles[s]);
298 fprintf(stdout, "[INFO] Loading %s \n", pgxfiles[pos]);
300 fseek(f, 0, SEEK_SET);
301 fscanf(f, "PG%31[ \t]%c%c%31[ \t+-]%d%31[ \t]%d%31[ \t]%d", temp, &endian1,
302 &endian2, signtmp, &prec, temp, &w, temp, &h);
306 while (signtmp[i] != '\0') {
307 if (signtmp[i] == '-') {
314 if (endian1 == 'M' && endian2 == 'L') {
315 cmptparm.bigendian = 1;
316 } else if (endian2 == 'M' && endian1 == 'L') {
317 cmptparm.bigendian = 0;
319 fprintf(stdout, "[ERROR] Bad pgx header, please check input file\n");
326 /* initialize volume component */
328 cmptparm.x0 = parameters->volume_offset_x0;
329 cmptparm.y0 = parameters->volume_offset_y0;
330 cmptparm.z0 = parameters->volume_offset_z0;
331 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 :
332 cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
333 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 :
334 cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
335 cmptparm.l = !cmptparm.z0 ? (sliceno - 1) * parameters->subsampling_dz + 1 :
336 cmptparm.z0 + (sliceno - 1) * parameters->subsampling_dz + 1;
343 cmptparm.prec = prec;
345 cmptparm.dcoffset = parameters->dcoffset;
346 cmptparm.dx = parameters->subsampling_dx;
347 cmptparm.dy = parameters->subsampling_dy;
348 cmptparm.dz = parameters->subsampling_dz;
350 /* create the volume */
351 volume = opj_volume_create(numcomps, &cmptparm, color_space);
356 /* set volume offset and reference grid */
357 volume->x0 = cmptparm.x0;
358 volume->y0 = cmptparm.y0;
359 volume->z0 = cmptparm.z0;
360 volume->x1 = cmptparm.w;
361 volume->y1 = cmptparm.h;
362 volume->z1 = cmptparm.l;
364 /* set volume data :only one component, that is a volume*/
365 comp = &volume->comps[0];
371 for (i = 0; i < w * h; i++) {
373 if (comp->prec <= 8) {
377 v = (char) readuchar(f);
379 } else if (comp->prec <= 16) {
381 v = readushort(f, cmptparm.bigendian);
383 v = (short) readushort(f, cmptparm.bigendian);
387 v = readuint(f, cmptparm.bigendian);
389 v = (int) readuint(f, cmptparm.bigendian);
395 comp->data[i + offset] = v;
399 } /* for s --> sliceno*/
400 comp->bpp = int_floorlog2(maxvalue) + 1;
402 /*dump_volume(stdout, volume);*/
407 int volumetopgx(opj_volume_t * volume, char *outfile)
409 int w, wr, wrr, h, hr, hrr, l, lr, lrr;
410 int i, j, compno, offset, sliceno;
413 for (compno = 0; compno < volume->numcomps; compno++) {
414 opj_volume_comp_t *comp = &volume->comps[compno];
421 while (*tmp != '.') {
425 for (sliceno = 0; sliceno < volume->z1 - volume->z0; sliceno++) {
427 if (volume->numcomps > 1) {
428 sprintf(name, "%s%d-%d.pgx", outfile, sliceno + 1, compno);
429 } else if ((volume->z1 - volume->z0) > 1) {
430 sprintf(name, "%s%d.pgx", outfile, sliceno + 1);
432 sprintf(name, "%s.pgx", outfile);
435 fdest = fopen(name, "wb");
437 fprintf(stdout, "[ERROR] Failed to open %s for writing \n", name);
441 fprintf(stdout, "[INFO] Writing in %s (%s)\n", name,
442 volume->comps[0].bigendian ? "Bigendian" : "Little-endian");
444 w = int_ceildiv(volume->x1 - volume->x0, volume->comps[compno].dx);
445 wr = volume->comps[compno].w;
446 wrr = int_ceildivpow2(volume->comps[compno].w, volume->comps[compno].factor[0]);
448 h = int_ceildiv(volume->y1 - volume->y0, volume->comps[compno].dy);
449 hr = volume->comps[compno].h;
450 hrr = int_ceildivpow2(volume->comps[compno].h, volume->comps[compno].factor[1]);
452 l = int_ceildiv(volume->z1 - volume->z0, volume->comps[compno].dz);
453 lr = volume->comps[compno].l;
454 lrr = int_ceildivpow2(volume->comps[compno].l, volume->comps[compno].factor[2]);
456 fprintf(fdest, "PG %c%c %c%d %d %d\n", comp->bigendian ? 'M' : 'L',
457 comp->bigendian ? 'L' : 'M', comp->sgnd ? '-' : '+', comp->prec, wr, hr);
458 if (comp->prec <= 8) {
460 } else if (comp->prec <= 16) {
466 offset = (sliceno / lrr * l) + (sliceno % lrr);
467 offset = wrr * hrr * offset;
468 /*fprintf(stdout,"%d %d %d %d\n",offset,wrr*hrr,wrr,w);*/
469 for (i = 0; i < wrr * hrr; i++) {
470 int v = volume->comps[0].data[(i / wrr * w) + (i % wrr) + offset];
471 if (volume->comps[0].bigendian) {
472 for (j = nbytes - 1; j >= 0; j--) {
473 char byte = (char)((v >> (j * 8)) & 0xff);
474 fwrite(&byte, 1, 1, fdest);
477 for (j = 0; j <= nbytes - 1; j++) {
478 char byte = (char)((v >> (j * 8)) & 0xff);
479 fwrite(&byte, 1, 1, fdest);
491 /* -->> -->> -->> -->>
495 <<-- <<-- <<-- <<-- */
497 opj_volume_t* bintovolume(char *filename, char *fileimg,
498 opj_cparameters_t *parameters)
500 int subsampling_dx = parameters->subsampling_dx;
501 int subsampling_dy = parameters->subsampling_dy;
502 int subsampling_dz = parameters->subsampling_dz;
504 int i, compno, w, h, l, numcomps = 1;
513 OPJ_COLOR_SPACE color_space;
514 opj_volume_cmptparm_t cmptparm; /* maximum of 1 component */
515 opj_volume_t * volume = NULL;
516 opj_volume_comp_t *comp = NULL;
519 color_space = CLRSPC_GRAY;
521 fimg = fopen(fileimg, "r");
523 fprintf(stdout, "[ERROR] Failed to open %s for reading !!\n", fileimg);
527 fseek(fimg, 0, SEEK_SET);
528 while (!feof(fimg)) {
529 fgets(line, 100, fimg);
530 /*fprintf(stdout,"%s %d \n",line,feof(fimg));*/
531 if (strncmp(line, "Bpp", 3) == 0) {
532 sscanf(line, "%*s%*[ \t]%d", &prec);
533 } else if (strncmp(line, "Color", 5) == 0) {
534 sscanf(line, "%*s%*[ \t]%d", &color_space);
535 } else if (strncmp(line, "Dim", 3) == 0) {
536 sscanf(line, "%*s%*[ \t]%d%*[ \t]%d%*[ \t]%d", &w, &h, &l);
539 /*fscanf(fimg, "Bpp%[ \t]%d%[ \t\n]",temp,&prec,temp);*/
540 /*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);*/
541 /*fscanf(fimg, "Resolution(mm)%[ \t]%d%[ \t]%d%[ \t]%d%[ \n\t]",temp,&subsampling_dx,temp,&subsampling_dy,temp,&subsampling_dz,temp);*/
544 fprintf(stdout, "[INFO] %d \t %d %d %d \t %3.2f %2.2f %2.2f \t %d \n",
545 color_space, w, h, l, subsampling_dx, subsampling_dy, subsampling_dz, prec);
549 /* initialize volume components */
550 memset(&cmptparm, 0, sizeof(opj_volume_cmptparm_t));
552 cmptparm.prec = prec;
555 cmptparm.bigendian = bigendian;
556 cmptparm.dcoffset = parameters->dcoffset;
557 cmptparm.dx = subsampling_dx;
558 cmptparm.dy = subsampling_dy;
559 cmptparm.dz = subsampling_dz;
564 /* create the volume */
565 volume = opj_volume_create(numcomps, &cmptparm, color_space);
567 fprintf(stdout, "[ERROR] Unable to create volume");
572 /* set volume offset and reference grid */
573 volume->x0 = parameters->volume_offset_x0;
574 volume->y0 = parameters->volume_offset_y0;
575 volume->z0 = parameters->volume_offset_z0;
576 volume->x1 = parameters->volume_offset_x0 + (w - 1) * subsampling_dx + 1;
577 volume->y1 = parameters->volume_offset_y0 + (h - 1) * subsampling_dy + 1;
578 volume->z1 = parameters->volume_offset_z0 + (l - 1) * subsampling_dz + 1;
580 /* set volume data */
581 f = fopen(filename, "rb");
583 fprintf(stdout, "[ERROR] Failed to open %s for reading !!\n", filename);
589 for (compno = 0; compno < volume->numcomps; compno++) {
591 /* set volume data */
592 comp = &volume->comps[compno];
594 /*if (comp->prec <= 8) {
596 unsigned char *data = (unsigned char *) malloc(whl * sizeof(unsigned char));
597 fread(data, 1, whl, f);
598 for (i = 0; i < whl; i++) {
599 comp->data[i] = data[i];
600 if (comp->data[i] > max)
605 char *data = (char *) malloc(whl);
606 fread(data, 1, whl, f);
607 for (i = 0; i < whl; i++) {
608 comp->data[i] = data[i];
609 if (comp->data[i] > max)
614 } else if (comp->prec <= 16) {
616 unsigned short *data = (unsigned short *) malloc(whl * sizeof(unsigned short));
617 int leido = fread(data, 2, whl, f);
619 free(data); fclose(f);
623 for (i = 0; i < whl; i++) {
624 if (bigendian) //(c1 << 8) + c2;
625 comp->data[i] = data[i];
626 else{ //(c2 << 8) + c1;
627 comp->data[i] = ShortSwap(data[i]);
629 if (comp->data[i] > max)
634 short *data = (short *) malloc(whl);
635 int leido = fread(data, 2, whl, f);
637 free(data); fclose(f);
640 for (i = 0; i < whl; i++) {
641 if (bigendian){ //(c1 << 8) + c2;
642 comp->data[i] = data[i];
643 }else{ //(c2 << 8) + c1;
644 comp->data[i] = (short) ShortSwap((unsigned short) data[i]);
646 if (comp->data[i] > max)
653 unsigned int *data = (unsigned int *) malloc(whl * sizeof(unsigned int));
654 int leido = fread(data, 4, whl, f);
656 free(data); fclose(f);
658 } for (i = 0; i < whl; i++) {
660 comp->data[i] = LongSwap(data[i]);
662 comp->data[i] = data[i];
663 if (comp->data[i] > max)
668 int leido = fread(comp->data, 4, whl, f);
673 for (i = 0; i < whl; i++) {
675 comp->data[i] = (int) LongSwap((unsigned int) comp->data[i]);
676 if (comp->data[i] > max)
682 for (i = 0; i < whl; i++) {
684 if (comp->prec <= 8) {
688 v = (char) readuchar(f);
690 } else if (comp->prec <= 16) {
692 v = readushort(f, bigendian);
694 v = (short) readushort(f, bigendian);
698 v = readuint(f, bigendian);
700 v = (int) readuint(f, bigendian);
708 comp->bpp = int_floorlog2(max) + 1;
714 int volumetobin(opj_volume_t * volume, char *outfile)
716 int w, wr, wrr, h, hr, hrr, l, lr, lrr, max;
717 int i, j, compno, nbytes;
720 FILE *fimgdest = NULL;
724 for (compno = 0; compno < 1; compno++) { /*Only one component*/
726 fdest = fopen(outfile, "wb");
728 fprintf(stdout, "[ERROR] Failed to open %s for writing\n", outfile);
731 fprintf(stdout, "[INFO] Writing outfile %s (%s) \n", outfile,
732 volume->comps[0].bigendian ? "Bigendian" : "Little-endian");
734 w = int_ceildiv(volume->x1 - volume->x0, volume->comps[compno].dx);
735 wr = volume->comps[compno].w;
736 wrr = int_ceildivpow2(volume->comps[compno].w, volume->comps[compno].factor[0]);
738 h = int_ceildiv(volume->y1 - volume->y0, volume->comps[compno].dy);
739 hr = volume->comps[compno].h;
740 hrr = int_ceildivpow2(volume->comps[compno].h, volume->comps[compno].factor[1]);
742 l = int_ceildiv(volume->z1 - volume->z0, volume->comps[compno].dz);
743 lr = volume->comps[compno].l;
744 lrr = int_ceildivpow2(volume->comps[compno].l, volume->comps[compno].factor[2]);
746 max = (volume->comps[compno].prec <= 8) ? 255 : (1 <<
747 volume->comps[compno].prec) - 1;
749 volume->comps[compno].x0 = int_ceildivpow2(volume->comps[compno].x0 -
750 int_ceildiv(volume->x0, volume->comps[compno].dx),
751 volume->comps[compno].factor[0]);
752 volume->comps[compno].y0 = int_ceildivpow2(volume->comps[compno].y0 -
753 int_ceildiv(volume->y0, volume->comps[compno].dy),
754 volume->comps[compno].factor[1]);
755 volume->comps[compno].z0 = int_ceildivpow2(volume->comps[compno].z0 -
756 int_ceildiv(volume->z0, volume->comps[compno].dz),
757 volume->comps[compno].factor[2]);
759 if (volume->comps[0].prec <= 8) {
761 } else if (volume->comps[0].prec <= 16) {
767 /*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]);*/
769 for (sliceno = 0; sliceno < lrr; sliceno++) {
770 offset = (sliceno / lrr * l) + (sliceno % lrr);
771 offset = wrr * hrr * offset;
772 for (i = 0; i < wrr * hrr; i++) {
773 int v = volume->comps[0].data[(i / wrr * w) + (i % wrr) + offset];
774 if (volume->comps[0].bigendian) {
775 for (j = nbytes - 1; j >= 0; j--) {
776 char byte = (char)((v >> (j * 8)) & 0xff);
777 fwrite(&byte, 1, 1, fdest);
780 for (j = 0; j <= nbytes - 1; j++) {
781 char byte = (char)((v >> (j * 8)) & 0xff);
782 fwrite(&byte, 1, 1, fdest);
792 snprintf(name, sizeof(name), "%s.img", outfile);
793 fimgdest = fopen(name, "w");
795 fprintf(stdout, "[ERROR] Failed to open %s for writing\n", name);
799 "Bpp\t%d\nColor Map\t2\nDimensions\t%d\t%d\t%d\nResolution(mm)\t%d\t%d\t%d\t\n",
800 volume->comps[0].prec, wrr, hrr, lrr, volume->comps[0].dx, volume->comps[0].dy,
801 volume->comps[0].dz);
806 /* -->> -->> -->> -->>
810 <<-- <<-- <<-- <<-- */
811 opj_volume_t* imgtovolume(char *fileimg, opj_cparameters_t *parameters)
813 int subsampling_dx = parameters->subsampling_dx;
814 int subsampling_dy = parameters->subsampling_dy;
815 int subsampling_dz = parameters->subsampling_dz;
817 int i, compno, w, h, l, numcomps = 1;
818 int prec, max = 0, min = 0;
820 char filename[100], tmpdirpath[100], dirpath[100], *tmp;
821 char line[100], datatype[100];
826 OPJ_COLOR_SPACE color_space;
827 opj_volume_cmptparm_t cmptparm; /* maximum of 1 component */
828 opj_volume_t * volume = NULL;
829 opj_volume_comp_t *comp = NULL;
832 color_space = CLRSPC_GRAY;
834 fimg = fopen(fileimg, "r");
836 fprintf(stderr, "[ERROR] Failed to open %s for reading !!\n", fileimg);
840 /*Fetch only the path */
841 strcpy(tmpdirpath, fileimg);
842 if ((tmp = strrchr(tmpdirpath, '/')) != NULL) {
845 strcpy(dirpath, tmpdirpath);
847 strcpy(dirpath, "./");
850 fseek(fimg, 0, SEEK_SET);
851 while (!feof(fimg)) {
852 fgets(line, 100, fimg);
853 /*fprintf(stdout,"%s %d \n",line,feof(fimg));*/
854 if (strncmp(line, "Image", 5) == 0) {
855 sscanf(line, "%*s%*[ \t]%s", datatype);
856 } else if (strncmp(line, "File", 4) == 0) {
857 sscanf(line, "%*s %*s%*[ \t]%s", filename);
858 strcat(dirpath, filename);
859 strcpy(filename, dirpath);
860 } else if (strncmp(line, "Min", 3) == 0) {
861 sscanf(line, "%*s %*s%*[ \t]%d%*[ \t]%d", &min, &max);
862 prec = int_floorlog2(max - min + 1);
863 } else if (strncmp(line, "Bpp", 3) == 0) {
864 sscanf(line, "%*s%*[ \t]%d", &prec);
865 } else if (strncmp(line, "Color", 5) == 0) {
866 sscanf(line, "%*s %*s%*[ \t]%d", &color_space);
867 } else if (strncmp(line, "Dim", 3) == 0) {
868 sscanf(line, "%*s%*[ \t]%d%*[ \t]%d%*[ \t]%d", &w, &h, &l);
869 } else if (strncmp(line, "Res", 3) == 0) {
870 sscanf(line, "%*s%*[ \t]%f%*[ \t]%f%*[ \t]%f", &dx, &dy, &dz);
875 fprintf(stdout, "[INFO] %s %d \t %d %d %d \t %f %f %f \t %d %d %d \n", filename,
876 color_space, w, h, l, dx, dy, dz, max, min, prec);
881 if (!prec || !w || !h || !l) {
883 "[ERROR] Unable to read IMG file correctly. Found some null values.");
887 /* initialize volume components */
888 memset(&cmptparm, 0, sizeof(opj_volume_cmptparm_t));
890 cmptparm.prec = prec;
893 cmptparm.bigendian = bigendian;
894 cmptparm.dcoffset = parameters->dcoffset;
895 cmptparm.dx = subsampling_dx;
896 cmptparm.dy = subsampling_dy;
897 cmptparm.dz = subsampling_dz;
902 /* create the volume */
903 volume = opj_volume_create(numcomps, &cmptparm, color_space);
905 fprintf(stdout, "[ERROR] Unable to create volume");
909 /* set volume offset and reference grid */
910 volume->x0 = parameters->volume_offset_x0;
911 volume->y0 = parameters->volume_offset_y0;
912 volume->z0 = parameters->volume_offset_z0;
913 volume->x1 = parameters->volume_offset_x0 + (w - 1) * subsampling_dx + 1;
914 volume->y1 = parameters->volume_offset_y0 + (h - 1) * subsampling_dy + 1;
915 volume->z1 = parameters->volume_offset_z0 + (l - 1) * subsampling_dz + 1;
918 /* set volume data */
919 f = fopen(filename, "rb");
921 fprintf(stderr, "[ERROR] Failed to open %s for reading !!\n", filename);
927 for (compno = 0; compno < volume->numcomps; compno++) {
929 /* set volume data */
930 comp = &volume->comps[compno];
932 /*if (comp->prec <= 8) {
934 unsigned char *data = (unsigned char *) malloc(whl * sizeof(unsigned char));
935 fread(data, 1, whl, f);
936 for (i = 0; i < whl; i++) {
937 comp->data[i] = data[i];
938 if (comp->data[i] > max)
943 char *data = (char *) malloc(whl);
944 fread(data, 1, whl, f);
945 for (i = 0; i < whl; i++) {
946 comp->data[i] = data[i];
947 if (comp->data[i] > max)
952 } else if (comp->prec <= 16) {
954 unsigned short *data = (unsigned short *) malloc(whl * sizeof(unsigned short));
955 int leido = fread(data, 2, whl, f);
957 free(data); fclose(f);
961 for (i = 0; i < whl; i++) {
962 if (bigendian) //(c1 << 8) + c2;
963 comp->data[i] = data[i];
964 else{ //(c2 << 8) + c1;
965 comp->data[i] = ShortSwap(data[i]);
967 if (comp->data[i] > max)
972 short *data = (short *) malloc(whl);
973 int leido = fread(data, 2, whl, f);
975 free(data); fclose(f);
978 for (i = 0; i < whl; i++) {
979 if (bigendian){ //(c1 << 8) + c2;
980 comp->data[i] = data[i];
981 }else{ //(c2 << 8) + c1;
982 comp->data[i] = (short) ShortSwap((unsigned short) data[i]);
984 if (comp->data[i] > max)
991 unsigned int *data = (unsigned int *) malloc(whl * sizeof(unsigned int));
992 int leido = fread(data, 4, whl, f);
994 free(data); fclose(f);
996 } for (i = 0; i < whl; i++) {
998 comp->data[i] = LongSwap(data[i]);
1000 comp->data[i] = data[i];
1001 if (comp->data[i] > max)
1002 max = comp->data[i];
1006 int leido = fread(comp->data, 4, whl, f);
1011 for (i = 0; i < whl; i++) {
1013 comp->data[i] = (int) LongSwap((unsigned int) comp->data[i]);
1014 if (comp->data[i] > max)
1015 max = comp->data[i];
1020 for (i = 0; i < whl; i++) {
1022 if (comp->prec <= 8) {
1026 v = (char) readuchar(f);
1028 } else if (comp->prec <= 16) {
1030 v = readushort(f, bigendian);
1032 v = (short) readushort(f, bigendian);
1036 v = readuint(f, bigendian);
1038 v = (int) readuint(f, bigendian);
1046 comp->bpp = int_floorlog2(max) + 1;