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");
324 /* initialize volume component */
326 cmptparm.x0 = parameters->volume_offset_x0;
327 cmptparm.y0 = parameters->volume_offset_y0;
328 cmptparm.z0 = parameters->volume_offset_z0;
329 cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 :
330 cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
331 cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 :
332 cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
333 cmptparm.l = !cmptparm.z0 ? (sliceno - 1) * parameters->subsampling_dz + 1 :
334 cmptparm.z0 + (sliceno - 1) * parameters->subsampling_dz + 1;
341 cmptparm.prec = prec;
343 cmptparm.dcoffset = parameters->dcoffset;
344 cmptparm.dx = parameters->subsampling_dx;
345 cmptparm.dy = parameters->subsampling_dy;
346 cmptparm.dz = parameters->subsampling_dz;
348 /* create the volume */
349 volume = opj_volume_create(numcomps, &cmptparm, color_space);
354 /* set volume offset and reference grid */
355 volume->x0 = cmptparm.x0;
356 volume->y0 = cmptparm.y0;
357 volume->z0 = cmptparm.z0;
358 volume->x1 = cmptparm.w;
359 volume->y1 = cmptparm.h;
360 volume->z1 = cmptparm.l;
362 /* set volume data :only one component, that is a volume*/
363 comp = &volume->comps[0];
369 for (i = 0; i < w * h; i++) {
371 if (comp->prec <= 8) {
375 v = (char) readuchar(f);
377 } else if (comp->prec <= 16) {
379 v = readushort(f, cmptparm.bigendian);
381 v = (short) readushort(f, cmptparm.bigendian);
385 v = readuint(f, cmptparm.bigendian);
387 v = (int) readuint(f, cmptparm.bigendian);
393 comp->data[i + offset] = v;
397 } /* for s --> sliceno*/
398 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);
588 for (compno = 0; compno < volume->numcomps; compno++) {
590 /* set volume data */
591 comp = &volume->comps[compno];
593 /*if (comp->prec <= 8) {
595 unsigned char *data = (unsigned char *) malloc(whl * sizeof(unsigned char));
596 fread(data, 1, whl, f);
597 for (i = 0; i < whl; i++) {
598 comp->data[i] = data[i];
599 if (comp->data[i] > max)
604 char *data = (char *) malloc(whl);
605 fread(data, 1, whl, f);
606 for (i = 0; i < whl; i++) {
607 comp->data[i] = data[i];
608 if (comp->data[i] > max)
613 } else if (comp->prec <= 16) {
615 unsigned short *data = (unsigned short *) malloc(whl * sizeof(unsigned short));
616 int leido = fread(data, 2, whl, f);
618 free(data); fclose(f);
622 for (i = 0; i < whl; i++) {
623 if (bigendian) //(c1 << 8) + c2;
624 comp->data[i] = data[i];
625 else{ //(c2 << 8) + c1;
626 comp->data[i] = ShortSwap(data[i]);
628 if (comp->data[i] > max)
633 short *data = (short *) malloc(whl);
634 int leido = fread(data, 2, whl, f);
636 free(data); fclose(f);
639 for (i = 0; i < whl; i++) {
640 if (bigendian){ //(c1 << 8) + c2;
641 comp->data[i] = data[i];
642 }else{ //(c2 << 8) + c1;
643 comp->data[i] = (short) ShortSwap((unsigned short) data[i]);
645 if (comp->data[i] > max)
652 unsigned int *data = (unsigned int *) malloc(whl * sizeof(unsigned int));
653 int leido = fread(data, 4, whl, f);
655 free(data); fclose(f);
657 } for (i = 0; i < whl; i++) {
659 comp->data[i] = LongSwap(data[i]);
661 comp->data[i] = data[i];
662 if (comp->data[i] > max)
667 int leido = fread(comp->data, 4, whl, f);
672 for (i = 0; i < whl; i++) {
674 comp->data[i] = (int) LongSwap((unsigned int) comp->data[i]);
675 if (comp->data[i] > max)
681 for (i = 0; i < whl; i++) {
683 if (comp->prec <= 8) {
687 v = (char) readuchar(f);
689 } else if (comp->prec <= 16) {
691 v = readushort(f, bigendian);
693 v = (short) readushort(f, bigendian);
697 v = readuint(f, bigendian);
699 v = (int) readuint(f, bigendian);
707 comp->bpp = int_floorlog2(max) + 1;
713 int volumetobin(opj_volume_t * volume, char *outfile)
715 int w, wr, wrr, h, hr, hrr, l, lr, lrr, max;
716 int i, j, compno, nbytes;
719 FILE *fimgdest = NULL;
723 for (compno = 0; compno < 1; compno++) { /*Only one component*/
725 fdest = fopen(outfile, "wb");
727 fprintf(stdout, "[ERROR] Failed to open %s for writing\n", outfile);
730 fprintf(stdout, "[INFO] Writing outfile %s (%s) \n", outfile,
731 volume->comps[0].bigendian ? "Bigendian" : "Little-endian");
733 w = int_ceildiv(volume->x1 - volume->x0, volume->comps[compno].dx);
734 wr = volume->comps[compno].w;
735 wrr = int_ceildivpow2(volume->comps[compno].w, volume->comps[compno].factor[0]);
737 h = int_ceildiv(volume->y1 - volume->y0, volume->comps[compno].dy);
738 hr = volume->comps[compno].h;
739 hrr = int_ceildivpow2(volume->comps[compno].h, volume->comps[compno].factor[1]);
741 l = int_ceildiv(volume->z1 - volume->z0, volume->comps[compno].dz);
742 lr = volume->comps[compno].l;
743 lrr = int_ceildivpow2(volume->comps[compno].l, volume->comps[compno].factor[2]);
745 max = (volume->comps[compno].prec <= 8) ? 255 : (1 <<
746 volume->comps[compno].prec) - 1;
748 volume->comps[compno].x0 = int_ceildivpow2(volume->comps[compno].x0 -
749 int_ceildiv(volume->x0, volume->comps[compno].dx),
750 volume->comps[compno].factor[0]);
751 volume->comps[compno].y0 = int_ceildivpow2(volume->comps[compno].y0 -
752 int_ceildiv(volume->y0, volume->comps[compno].dy),
753 volume->comps[compno].factor[1]);
754 volume->comps[compno].z0 = int_ceildivpow2(volume->comps[compno].z0 -
755 int_ceildiv(volume->z0, volume->comps[compno].dz),
756 volume->comps[compno].factor[2]);
758 if (volume->comps[0].prec <= 8) {
760 } else if (volume->comps[0].prec <= 16) {
766 /*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]);*/
768 for (sliceno = 0; sliceno < lrr; sliceno++) {
769 offset = (sliceno / lrr * l) + (sliceno % lrr);
770 offset = wrr * hrr * offset;
771 for (i = 0; i < wrr * hrr; i++) {
772 int v = volume->comps[0].data[(i / wrr * w) + (i % wrr) + offset];
773 if (volume->comps[0].bigendian) {
774 for (j = nbytes - 1; j >= 0; j--) {
775 char byte = (char)((v >> (j * 8)) & 0xff);
776 fwrite(&byte, 1, 1, fdest);
779 for (j = 0; j <= nbytes - 1; j++) {
780 char byte = (char)((v >> (j * 8)) & 0xff);
781 fwrite(&byte, 1, 1, fdest);
791 snprintf(name, sizeof(name), "%s.img", outfile);
792 fimgdest = fopen(name, "w");
794 fprintf(stdout, "[ERROR] Failed to open %s for writing\n", name);
798 "Bpp\t%d\nColor Map\t2\nDimensions\t%d\t%d\t%d\nResolution(mm)\t%d\t%d\t%d\t\n",
799 volume->comps[0].prec, wrr, hrr, lrr, volume->comps[0].dx, volume->comps[0].dy,
800 volume->comps[0].dz);
805 /* -->> -->> -->> -->>
809 <<-- <<-- <<-- <<-- */
810 opj_volume_t* imgtovolume(char *fileimg, opj_cparameters_t *parameters)
812 int subsampling_dx = parameters->subsampling_dx;
813 int subsampling_dy = parameters->subsampling_dy;
814 int subsampling_dz = parameters->subsampling_dz;
816 int i, compno, w, h, l, numcomps = 1;
817 int prec, max = 0, min = 0;
819 char filename[100], tmpdirpath[100], dirpath[100], *tmp;
820 char line[100], datatype[100];
825 OPJ_COLOR_SPACE color_space;
826 opj_volume_cmptparm_t cmptparm; /* maximum of 1 component */
827 opj_volume_t * volume = NULL;
828 opj_volume_comp_t *comp = NULL;
831 color_space = CLRSPC_GRAY;
833 fimg = fopen(fileimg, "r");
835 fprintf(stderr, "[ERROR] Failed to open %s for reading !!\n", fileimg);
839 /*Fetch only the path */
840 strcpy(tmpdirpath, fileimg);
841 if ((tmp = strrchr(tmpdirpath, '/')) != NULL) {
844 strcpy(dirpath, tmpdirpath);
846 strcpy(dirpath, "./");
849 fseek(fimg, 0, SEEK_SET);
850 while (!feof(fimg)) {
851 fgets(line, 100, fimg);
852 /*fprintf(stdout,"%s %d \n",line,feof(fimg));*/
853 if (strncmp(line, "Image", 5) == 0) {
854 sscanf(line, "%*s%*[ \t]%s", datatype);
855 } else if (strncmp(line, "File", 4) == 0) {
856 sscanf(line, "%*s %*s%*[ \t]%s", filename);
857 strcat(dirpath, filename);
858 strcpy(filename, dirpath);
859 } else if (strncmp(line, "Min", 3) == 0) {
860 sscanf(line, "%*s %*s%*[ \t]%d%*[ \t]%d", &min, &max);
861 prec = int_floorlog2(max - min + 1);
862 } else if (strncmp(line, "Bpp", 3) == 0) {
863 sscanf(line, "%*s%*[ \t]%d", &prec);
864 } else if (strncmp(line, "Color", 5) == 0) {
865 sscanf(line, "%*s %*s%*[ \t]%d", &color_space);
866 } else if (strncmp(line, "Dim", 3) == 0) {
867 sscanf(line, "%*s%*[ \t]%d%*[ \t]%d%*[ \t]%d", &w, &h, &l);
868 } else if (strncmp(line, "Res", 3) == 0) {
869 sscanf(line, "%*s%*[ \t]%f%*[ \t]%f%*[ \t]%f", &dx, &dy, &dz);
874 fprintf(stdout, "[INFO] %s %d \t %d %d %d \t %f %f %f \t %d %d %d \n", filename,
875 color_space, w, h, l, dx, dy, dz, max, min, prec);
880 if (!prec || !w || !h || !l) {
882 "[ERROR] Unable to read IMG file correctly. Found some null values.");
886 /* initialize volume components */
887 memset(&cmptparm, 0, sizeof(opj_volume_cmptparm_t));
889 cmptparm.prec = prec;
892 cmptparm.bigendian = bigendian;
893 cmptparm.dcoffset = parameters->dcoffset;
894 cmptparm.dx = subsampling_dx;
895 cmptparm.dy = subsampling_dy;
896 cmptparm.dz = subsampling_dz;
901 /* create the volume */
902 volume = opj_volume_create(numcomps, &cmptparm, color_space);
904 fprintf(stdout, "[ERROR] Unable to create volume");
908 /* set volume offset and reference grid */
909 volume->x0 = parameters->volume_offset_x0;
910 volume->y0 = parameters->volume_offset_y0;
911 volume->z0 = parameters->volume_offset_z0;
912 volume->x1 = parameters->volume_offset_x0 + (w - 1) * subsampling_dx + 1;
913 volume->y1 = parameters->volume_offset_y0 + (h - 1) * subsampling_dy + 1;
914 volume->z1 = parameters->volume_offset_z0 + (l - 1) * subsampling_dz + 1;
917 /* set volume data */
918 f = fopen(filename, "rb");
920 fprintf(stderr, "[ERROR] Failed to open %s for reading !!\n", filename);
926 for (compno = 0; compno < volume->numcomps; compno++) {
928 /* set volume data */
929 comp = &volume->comps[compno];
931 /*if (comp->prec <= 8) {
933 unsigned char *data = (unsigned char *) malloc(whl * sizeof(unsigned char));
934 fread(data, 1, whl, f);
935 for (i = 0; i < whl; i++) {
936 comp->data[i] = data[i];
937 if (comp->data[i] > max)
942 char *data = (char *) malloc(whl);
943 fread(data, 1, whl, f);
944 for (i = 0; i < whl; i++) {
945 comp->data[i] = data[i];
946 if (comp->data[i] > max)
951 } else if (comp->prec <= 16) {
953 unsigned short *data = (unsigned short *) malloc(whl * sizeof(unsigned short));
954 int leido = fread(data, 2, whl, f);
956 free(data); fclose(f);
960 for (i = 0; i < whl; i++) {
961 if (bigendian) //(c1 << 8) + c2;
962 comp->data[i] = data[i];
963 else{ //(c2 << 8) + c1;
964 comp->data[i] = ShortSwap(data[i]);
966 if (comp->data[i] > max)
971 short *data = (short *) malloc(whl);
972 int leido = fread(data, 2, whl, f);
974 free(data); fclose(f);
977 for (i = 0; i < whl; i++) {
978 if (bigendian){ //(c1 << 8) + c2;
979 comp->data[i] = data[i];
980 }else{ //(c2 << 8) + c1;
981 comp->data[i] = (short) ShortSwap((unsigned short) data[i]);
983 if (comp->data[i] > max)
990 unsigned int *data = (unsigned int *) malloc(whl * sizeof(unsigned int));
991 int leido = fread(data, 4, whl, f);
993 free(data); fclose(f);
995 } for (i = 0; i < whl; i++) {
997 comp->data[i] = LongSwap(data[i]);
999 comp->data[i] = data[i];
1000 if (comp->data[i] > max)
1001 max = comp->data[i];
1005 int leido = fread(comp->data, 4, whl, f);
1010 for (i = 0; i < whl; i++) {
1012 comp->data[i] = (int) LongSwap((unsigned int) comp->data[i]);
1013 if (comp->data[i] > max)
1014 max = comp->data[i];
1019 for (i = 0; i < whl; i++) {
1021 if (comp->prec <= 8) {
1025 v = (char) readuchar(f);
1027 } else if (comp->prec <= 16) {
1029 v = readushort(f, bigendian);
1031 v = (short) readushort(f, bigendian);
1035 v = readuint(f, bigendian);
1037 v = (int) readuint(f, bigendian);
1045 comp->bpp = int_floorlog2(max) + 1;