Merge pull request #1244 from rouault/fix_pi_warnings
[openjpeg.git] / src / bin / jp3d / convert.c
1 /*
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.
6  *
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.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
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.
23  *
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.
35  */
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include "openjp3d.h"
40 #ifdef _WIN32
41 #include "windirent.h"
42 #else
43 #include <dirent.h>
44 #endif /* _WIN32 */
45
46
47
48 void dump_volume(FILE *fd, opj_volume_t * vol)
49 {
50     int compno;
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);
61         fprintf(fd, "  }\n");
62     }
63     fprintf(fd, "}\n");
64 }
65
66 /*
67  * Get logarithm of an integer and round downwards.
68  *
69  * log2(a)
70  */
71 static int int_floorlog2(int a)
72 {
73     int l;
74     for (l = 0; a > 1; l++) {
75         a >>= 1;
76     }
77     return l;
78 }
79
80 /*
81  * Divide an integer by a power of 2 and round upwards.
82  *
83  * a divided by 2^b
84  */
85 static int int_ceildivpow2(int a, int b)
86 {
87     return (a + (1 << b) - 1) >> b;
88 }
89
90 /*
91  * Divide an integer and round upwards.
92  *
93  * a divided by b
94  */
95 static int int_ceildiv(int a, int b)
96 {
97     return (a + b - 1) / b;
98 }
99
100
101 /* -->> -->> -->> -->>
102
103 PGX IMAGE FORMAT
104
105 <<-- <<-- <<-- <<-- */
106
107
108 unsigned char readuchar(FILE * f)
109 {
110     unsigned char c1;
111     fread(&c1, 1, 1, f);
112     return c1;
113 }
114
115 unsigned short readushort(FILE * f, int bigendian)
116 {
117     unsigned char c1, c2;
118     fread(&c1, 1, 1, f);
119     fread(&c2, 1, 1, f);
120     if (bigendian) {
121         return (c1 << 8) + c2;
122     } else {
123         return (c2 << 8) + c1;
124     }
125 }
126
127 unsigned int readuint(FILE * f, int bigendian)
128 {
129     unsigned char c1, c2, c3, c4;
130     fread(&c1, 1, 1, f);
131     fread(&c2, 1, 1, f);
132     fread(&c3, 1, 1, f);
133     fread(&c4, 1, 1, f);
134     if (bigendian) {
135         return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
136     } else {
137         return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
138     }
139 }
140 /*****************************************/
141 static unsigned short ShortSwap(unsigned short v)
142 {
143     unsigned char c1, c2;
144     c1 = v & 0xff;
145     c2 = (v >> 8) & 0xff;
146     return (c1 << 8) + c2;
147 }
148
149 static unsigned int LongSwap(unsigned int i)
150 {
151     unsigned char b1, b2, b3, b4;
152     b1 = i & 255;
153     b2 = (i >> 8) & 255;
154     b3 = (i >> 16) & 255;
155     b4 = (i >> 24) & 255;
156     return ((int)b1 << 24) + ((int)b2 << 16) + ((int)b3 << 8) + b4;
157 }
158 /*****************************************/
159
160 opj_volume_t* pgxtovolume(char *relpath, opj_cparameters_t *parameters)
161 {
162
163     FILE *f = NULL;
164     int w, h, prec;
165     unsigned long offset;
166     int i, s, numcomps, maxvalue, sliceno, slicepos, maxslice = 0;
167
168     OPJ_COLOR_SPACE color_space;
169     opj_volume_cmptparm_t cmptparm; /* maximum of 1 component */
170     opj_volume_t * volume = NULL;
171
172     char endian1, endian2, sign;
173     char signtmp[32];
174     char temp[32];
175     opj_volume_comp_t *comp = NULL;
176
177     DIR *dirp;
178     struct dirent *direntp;
179
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];
187     char tmpno[3];
188
189     numcomps = 1;
190     color_space = CLRSPC_GRAY;
191     sliceno = 0;
192     maxvalue = 0;
193     memset(pgxfiles, 0, MAX_SLICES * MAX_PATH * sizeof(char));
194     memset(&cmptparm, 0, sizeof(opj_volume_cmptparm_t));
195
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");*/
199         sliceno = 1;
200         maxslice = 1;
201         strcpy(pgxfiles[0], relpath);
202
203     } else {
204         /*Fetch only the path */
205         strcpy(tmpdirpath, relpath);
206         if ((tmp = strrchr(tmpdirpath, '/')) != NULL) {
207             tmp++;
208             *tmp = '\0';
209             strcpy(dirpath, tmpdirpath);
210         } else {
211             strcpy(dirpath, "./");
212         }
213
214         /*Fetch the pattern of the volume slices*/
215         if ((tmp = strrchr(relpath, '/')) != NULL) {
216             tmp++;
217         } else {
218             tmp = relpath;
219         }
220         if ((tmp2 = strrchr(tmp, '-')) != NULL) {
221             *tmp2 = '\0';
222         } else {
223             fprintf(stdout, "[ERROR] tmp2 ha dado null. no ha encontrado el * %s %s", tmp,
224                     relpath);
225             return NULL;
226         }
227         strcpy(pattern, tmp);
228
229         dirp = opendir(dirpath);
230         if (dirp == NULL) {
231             fprintf(stdout,
232                     "[ERROR] Infile must be a .pgx file or a directory that contain pgx files");
233             return NULL;
234         }
235
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) {
240                 continue;
241             }
242
243             if (((pgx = strstr(direntp->d_name, pattern)) != NULL) &&
244                     ((tmp2 = strstr(direntp->d_name, ".pgx")) != NULL)) {
245
246                 strcpy(tmp, dirpath);
247                 tmp = strcat(tmp, direntp->d_name);
248
249                 /*Obtenemos el index de la secuencia de slices*/
250                 if ((tmp2 = strpbrk(direntp->d_name, "0123456789")) == NULL) {
251                     continue;
252                 }
253                 i = 0;
254                 while (tmp2 != NULL) {
255                     tmpno[i++] = *tmp2;
256                     point = tmp2;
257                     tmp2 = strpbrk(tmp2 + 1, "0123456789");
258                 }
259                 tmpno[i] = '\0';
260
261                 /*Comprobamos que no estamos leyendo algo raro como pattern.jp3d*/
262                 if ((point = strpbrk(point, ".")) == NULL) {
263                     break;
264                 }
265                 /*Slicepos --> index de slice; Sliceno --> no de slices hasta el momento*/
266                 slicepos = atoi(tmpno);
267                 pgxslicepos[sliceno] = slicepos - 1;
268                 sliceno++;
269                 if (slicepos > maxslice) {
270                     maxslice = slicepos;
271                 }
272
273                 /*Colocamos el slices en su posicion correspondiente*/
274                 strcpy(pgxfiles[slicepos - 1], tmp);
275             }
276         }
277
278     }/* else if pattern*.pgx */
279
280     if (!sliceno) {
281         fprintf(stdout,
282                 "[ERROR] No slices with this pattern founded !! Please check input volume name\n");
283         closedir(dirp);
284         return NULL;
285     }
286     /*if ( maxslice != sliceno) {
287         fprintf(stdout,"[ERROR] Slices are not sequentially numbered !! Please rename them accordingly\n");
288         return NULL;
289     }*/
290
291     for (s = 0; s < sliceno; s++) {
292         int pos = maxslice == sliceno ? s : pgxslicepos[s];
293         f = fopen(pgxfiles[pos], "rb");
294         if (!f) {
295             fprintf(stdout, "[ERROR] Failed to open %s for reading !\n", pgxfiles[s]);
296             return NULL;
297         }
298         fprintf(stdout, "[INFO] Loading %s \n", pgxfiles[pos]);
299
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);
303
304         i = 0;
305         sign = '+';
306         while (signtmp[i] != '\0') {
307             if (signtmp[i] == '-') {
308                 sign = '-';
309             }
310             i++;
311         }
312
313         fgetc(f);
314         if (endian1 == 'M' && endian2 == 'L') {
315             cmptparm.bigendian = 1;
316         } else if (endian2 == 'M' && endian1 == 'L') {
317             cmptparm.bigendian = 0;
318         } else {
319             fprintf(stdout, "[ERROR] Bad pgx header, please check input file\n");
320             fclose(f);
321             closedir(dirp);
322             return NULL;
323         }
324
325         if (s == 0) {
326             /* initialize volume component */
327
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;
337
338             if (sign == '-') {
339                 cmptparm.sgnd = 1;
340             } else {
341                 cmptparm.sgnd = 0;
342             }
343             cmptparm.prec = prec;
344             cmptparm.bpp = prec;
345             cmptparm.dcoffset = parameters->dcoffset;
346             cmptparm.dx = parameters->subsampling_dx;
347             cmptparm.dy = parameters->subsampling_dy;
348             cmptparm.dz = parameters->subsampling_dz;
349
350             /* create the volume */
351             volume = opj_volume_create(numcomps, &cmptparm, color_space);
352             if (!volume) {
353                 fclose(f);
354                 return NULL;
355             }
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;
363
364             /* set volume data :only one component, that is a volume*/
365             comp = &volume->comps[0];
366
367         }/*if sliceno==1*/
368
369         offset = w * h * s;
370
371         for (i = 0; i < w * h; i++) {
372             int v;
373             if (comp->prec <= 8) {
374                 if (!comp->sgnd) {
375                     v = readuchar(f);
376                 } else {
377                     v = (char) readuchar(f);
378                 }
379             } else if (comp->prec <= 16) {
380                 if (!comp->sgnd) {
381                     v = readushort(f, cmptparm.bigendian);
382                 } else {
383                     v = (short) readushort(f, cmptparm.bigendian);
384                 }
385             } else {
386                 if (!comp->sgnd) {
387                     v = readuint(f, cmptparm.bigendian);
388                 } else {
389                     v = (int) readuint(f, cmptparm.bigendian);
390                 }
391             }
392             if (v > maxvalue) {
393                 maxvalue = v;
394             }
395             comp->data[i + offset] = v;
396
397         }
398         fclose(f);
399     } /* for s --> sliceno*/
400     comp->bpp = int_floorlog2(maxvalue) + 1;
401     closedir(dirp);
402     /*dump_volume(stdout, volume);*/
403     return volume;
404 }
405
406
407 int volumetopgx(opj_volume_t * volume, char *outfile)
408 {
409     int w, wr, wrr, h, hr, hrr, l, lr, lrr;
410     int i, j, compno, offset, sliceno;
411     FILE *fdest = NULL;
412
413     for (compno = 0; compno < volume->numcomps; compno++) {
414         opj_volume_comp_t *comp = &volume->comps[compno];
415         char name[256];
416         int nbytes = 0;
417         char *tmp = outfile;
418         while (*tmp) {
419             tmp++;
420         }
421         while (*tmp != '.') {
422             tmp--;
423         }
424         *tmp = '\0';
425         for (sliceno = 0; sliceno < volume->z1 - volume->z0; sliceno++) {
426
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);
431             } else {
432                 sprintf(name, "%s.pgx", outfile);
433             }
434
435             fdest = fopen(name, "wb");
436             if (!fdest) {
437                 fprintf(stdout, "[ERROR] Failed to open %s for writing \n", name);
438                 return 1;
439             }
440
441             fprintf(stdout, "[INFO] Writing in %s (%s)\n", name,
442                     volume->comps[0].bigendian ? "Bigendian" : "Little-endian");
443
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]);
447
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]);
451
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]);
455
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) {
459                 nbytes = 1;
460             } else if (comp->prec <= 16) {
461                 nbytes = 2;
462             } else {
463                 nbytes = 4;
464             }
465
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);
475                     }
476                 } else {
477                     for (j = 0; j <= nbytes - 1; j++) {
478                         char byte = (char)((v >> (j * 8)) & 0xff);
479                         fwrite(&byte, 1, 1, fdest);
480                     }
481                 }
482             }
483
484             fclose(fdest);
485         }/*for sliceno*/
486     }/*for compno*/
487
488     return 0;
489 }
490
491 /* -->> -->> -->> -->>
492
493 BIN IMAGE FORMAT
494
495 <<-- <<-- <<-- <<-- */
496
497 opj_volume_t* bintovolume(char *filename, char *fileimg,
498                           opj_cparameters_t *parameters)
499 {
500     int subsampling_dx =  parameters->subsampling_dx;
501     int subsampling_dy =  parameters->subsampling_dy;
502     int subsampling_dz =  parameters->subsampling_dz;
503
504     int i, compno, w, h, l, numcomps = 1;
505     int prec, max = 0;
506
507     /*  char temp[32];*/
508     char line[100];
509     int bigendian;
510
511     FILE *f = NULL;
512     FILE *fimg = NULL;
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;
517
518     bigendian = 0;
519     color_space = CLRSPC_GRAY;
520
521     fimg = fopen(fileimg, "r");
522     if (!fimg) {
523         fprintf(stdout, "[ERROR] Failed to open %s for reading !!\n", fileimg);
524         return 0;
525     }
526
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);
537         }
538     }
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);*/
542
543 #ifdef VERBOSE
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);
546 #endif
547     fclose(fimg);
548
549     /* initialize volume components */
550     memset(&cmptparm, 0, sizeof(opj_volume_cmptparm_t));
551
552     cmptparm.prec = prec;
553     cmptparm.bpp = prec;
554     cmptparm.sgnd = 0;
555     cmptparm.bigendian = bigendian;
556     cmptparm.dcoffset = parameters->dcoffset;
557     cmptparm.dx = subsampling_dx;
558     cmptparm.dy = subsampling_dy;
559     cmptparm.dz = subsampling_dz;
560     cmptparm.w = w;
561     cmptparm.h = h;
562     cmptparm.l = l;
563
564     /* create the volume */
565     volume = opj_volume_create(numcomps, &cmptparm, color_space);
566     if (!volume) {
567         fprintf(stdout, "[ERROR] Unable to create volume");
568         fclose(f);
569         return NULL;
570     }
571
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;
579
580     /* set volume data */
581     f = fopen(filename, "rb");
582     if (!f) {
583         fprintf(stdout, "[ERROR] Failed to open %s for reading !!\n", filename);
584         opj_free(volume);
585         return 0;
586     }
587
588     /* BINARY */
589     for (compno = 0; compno < volume->numcomps; compno++) {
590         int whl = w * h * l;
591         /* set volume data */
592         comp = &volume->comps[compno];
593
594         /*if (comp->prec <= 8) {
595             if (!comp->sgnd) {
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)
601                         max = comp->data[i];
602                 }
603                 free(data);
604             } else {
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)
610                         max = comp->data[i];
611                 }
612                 free(data);
613             }
614         } else if (comp->prec <= 16) {
615             if (!comp->sgnd) {
616                 unsigned short *data = (unsigned short *) malloc(whl * sizeof(unsigned short));
617                 int leido = fread(data, 2, whl, f);
618                 if (!leido) {
619                     free(data); fclose(f);
620                     return NULL;
621                 }
622
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]);
628                     }
629                     if (comp->data[i] > max)
630                         max = comp->data[i];
631                 }
632                 free(data);
633             } else {
634                 short *data = (short *) malloc(whl);
635                 int leido = fread(data, 2, whl, f);
636                 if (!leido) {
637                     free(data); fclose(f);
638                     return NULL;
639                 }
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]);
645                     }
646                     if (comp->data[i] > max)
647                         max = comp->data[i];
648                 }
649                 free(data);
650             }
651         } else {
652             if (!comp->sgnd) {
653                 unsigned int *data = (unsigned int *) malloc(whl * sizeof(unsigned int));
654                 int leido = fread(data, 4, whl, f);
655                 if (!leido) {
656                     free(data); fclose(f);
657                     return NULL;
658                 }               for (i = 0; i < whl; i++) {
659                     if (!bigendian)
660                         comp->data[i] = LongSwap(data[i]);
661                     else
662                         comp->data[i] = data[i];
663                     if (comp->data[i] > max)
664                         max = comp->data[i];
665                 }
666                 free(data);
667             } else {
668                 int leido = fread(comp->data, 4, whl, f);
669                 if (!leido) {
670                     fclose(f);
671                     return NULL;
672                 }
673                 for (i = 0; i < whl; i++) {
674                     if (!bigendian)
675                         comp->data[i] = (int) LongSwap((unsigned int) comp->data[i]);
676                     if (comp->data[i] > max)
677                         max = comp->data[i];
678                 }
679             }
680         }*/
681
682         for (i = 0; i < whl; i++) {
683             int v;
684             if (comp->prec <= 8) {
685                 if (!comp->sgnd) {
686                     v = readuchar(f);
687                 } else {
688                     v = (char) readuchar(f);
689                 }
690             } else if (comp->prec <= 16) {
691                 if (!comp->sgnd) {
692                     v = readushort(f, bigendian);
693                 } else {
694                     v = (short) readushort(f, bigendian);
695                 }
696             } else {
697                 if (!comp->sgnd) {
698                     v = readuint(f, bigendian);
699                 } else {
700                     v = (int) readuint(f, bigendian);
701                 }
702             }
703             if (v > max) {
704                 max = v;
705             }
706             comp->data[i] = v;
707         }
708         comp->bpp = int_floorlog2(max) + 1;
709     }
710     fclose(f);
711     return volume;
712 }
713
714 int volumetobin(opj_volume_t * volume, char *outfile)
715 {
716     int w, wr, wrr, h, hr, hrr, l, lr, lrr, max;
717     int i, j, compno, nbytes;
718     int offset, sliceno;
719     FILE *fdest = NULL;
720     FILE *fimgdest = NULL;
721     /*  char *imgtemp;*/
722     char name[256];
723
724     for (compno = 0; compno < 1; compno++) { /*Only one component*/
725
726         fdest = fopen(outfile, "wb");
727         if (!fdest) {
728             fprintf(stdout, "[ERROR] Failed to open %s for writing\n", outfile);
729             return 1;
730         }
731         fprintf(stdout, "[INFO] Writing outfile %s (%s) \n", outfile,
732                 volume->comps[0].bigendian ? "Bigendian" : "Little-endian");
733
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]);
737
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]);
741
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]);
745
746         max = (volume->comps[compno].prec <= 8) ? 255 : (1 <<
747                 volume->comps[compno].prec) - 1;
748
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]);
758
759         if (volume->comps[0].prec <= 8) {
760             nbytes = 1;
761         } else if (volume->comps[0].prec <= 16) {
762             nbytes = 2;
763         } else {
764             nbytes = 4;
765         }
766
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]);*/
768
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);
778                     }
779                 } else {
780                     for (j = 0; j <= nbytes - 1; j++) {
781                         char byte = (char)((v >> (j * 8)) & 0xff);
782                         fwrite(&byte, 1, 1, fdest);
783                     }
784                 }
785             }
786         }
787
788     }
789
790     fclose(fdest);
791
792     snprintf(name, sizeof(name), "%s.img", outfile);
793     fimgdest = fopen(name, "w");
794     if (!fimgdest) {
795         fprintf(stdout, "[ERROR] Failed to open %s for writing\n", name);
796         return 1;
797     }
798     fprintf(fimgdest,
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);
802
803     fclose(fimgdest);
804     return 0;
805 }
806 /* -->> -->> -->> -->>
807
808 IMG IMAGE FORMAT
809
810 <<-- <<-- <<-- <<-- */
811 opj_volume_t* imgtovolume(char *fileimg, opj_cparameters_t *parameters)
812 {
813     int subsampling_dx =  parameters->subsampling_dx;
814     int subsampling_dy =  parameters->subsampling_dy;
815     int subsampling_dz =  parameters->subsampling_dz;
816
817     int i, compno, w, h, l, numcomps = 1;
818     int prec, max = 0, min = 0;
819     float dx, dy, dz;
820     char filename[100], tmpdirpath[100], dirpath[100], *tmp;
821     char line[100], datatype[100];
822     int bigendian;
823
824     FILE *f = NULL;
825     FILE *fimg = NULL;
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;
830
831     bigendian = 0;
832     color_space = CLRSPC_GRAY;
833
834     fimg = fopen(fileimg, "r");
835     if (!fimg) {
836         fprintf(stderr, "[ERROR] Failed to open %s for reading !!\n", fileimg);
837         return 0;
838     }
839
840     /*Fetch only the path */
841     strcpy(tmpdirpath, fileimg);
842     if ((tmp = strrchr(tmpdirpath, '/')) != NULL) {
843         tmp++;
844         *tmp = '\0';
845         strcpy(dirpath, tmpdirpath);
846     } else {
847         strcpy(dirpath, "./");
848     }
849
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);
871         }
872
873     }
874 #ifdef VERBOSE
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);
877 #endif
878     fclose(fimg);
879
880     /* error control */
881     if (!prec || !w || !h || !l) {
882         fprintf(stderr,
883                 "[ERROR] Unable to read IMG file correctly. Found some null values.");
884         return NULL;
885     }
886
887     /* initialize volume components */
888     memset(&cmptparm, 0, sizeof(opj_volume_cmptparm_t));
889
890     cmptparm.prec = prec;
891     cmptparm.bpp = prec;
892     cmptparm.sgnd = 0;
893     cmptparm.bigendian = bigendian;
894     cmptparm.dcoffset = parameters->dcoffset;
895     cmptparm.dx = subsampling_dx;
896     cmptparm.dy = subsampling_dy;
897     cmptparm.dz = subsampling_dz;
898     cmptparm.w = w;
899     cmptparm.h = h;
900     cmptparm.l = l;
901
902     /* create the volume */
903     volume = opj_volume_create(numcomps, &cmptparm, color_space);
904     if (!volume) {
905         fprintf(stdout, "[ERROR] Unable to create volume");
906         return NULL;
907     }
908
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;
916
917     max = 0;
918     /* set volume data */
919     f = fopen(filename, "rb");
920     if (!f) {
921         fprintf(stderr, "[ERROR] Failed to open %s for reading !!\n", filename);
922         opj_free(volume);
923         return 0;
924     }
925
926     /* BINARY */
927     for (compno = 0; compno < volume->numcomps; compno++) {
928         int whl = w * h * l;
929         /* set volume data */
930         comp = &volume->comps[compno];
931
932         /*if (comp->prec <= 8) {
933             if (!comp->sgnd) {
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)
939                         max = comp->data[i];
940                 }
941                 free(data);
942             } else {
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)
948                         max = comp->data[i];
949                 }
950                 free(data);
951             }
952         } else if (comp->prec <= 16) {
953             if (!comp->sgnd) {
954                 unsigned short *data = (unsigned short *) malloc(whl * sizeof(unsigned short));
955                 int leido = fread(data, 2, whl, f);
956                 if (!leido) {
957                     free(data); fclose(f);
958                     return NULL;
959                 }
960
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]);
966                     }
967                     if (comp->data[i] > max)
968                         max = comp->data[i];
969                 }
970                 free(data);
971             } else {
972                 short *data = (short *) malloc(whl);
973                 int leido = fread(data, 2, whl, f);
974                 if (!leido) {
975                     free(data); fclose(f);
976                     return NULL;
977                 }
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]);
983                     }
984                     if (comp->data[i] > max)
985                         max = comp->data[i];
986                 }
987                 free(data);
988             }
989         } else {
990             if (!comp->sgnd) {
991                 unsigned int *data = (unsigned int *) malloc(whl * sizeof(unsigned int));
992                 int leido = fread(data, 4, whl, f);
993                 if (!leido) {
994                     free(data); fclose(f);
995                     return NULL;
996                 }               for (i = 0; i < whl; i++) {
997                     if (!bigendian)
998                         comp->data[i] = LongSwap(data[i]);
999                     else
1000                         comp->data[i] = data[i];
1001                     if (comp->data[i] > max)
1002                         max = comp->data[i];
1003                 }
1004                 free(data);
1005             } else {
1006                 int leido = fread(comp->data, 4, whl, f);
1007                 if (!leido) {
1008                     fclose(f);
1009                     return NULL;
1010                 }
1011                 for (i = 0; i < whl; i++) {
1012                     if (!bigendian)
1013                         comp->data[i] = (int) LongSwap((unsigned int) comp->data[i]);
1014                     if (comp->data[i] > max)
1015                         max = comp->data[i];
1016                 }
1017             }
1018         }*/
1019
1020         for (i = 0; i < whl; i++) {
1021             int v;
1022             if (comp->prec <= 8) {
1023                 if (!comp->sgnd) {
1024                     v = readuchar(f);
1025                 } else {
1026                     v = (char) readuchar(f);
1027                 }
1028             } else if (comp->prec <= 16) {
1029                 if (!comp->sgnd) {
1030                     v = readushort(f, bigendian);
1031                 } else {
1032                     v = (short) readushort(f, bigendian);
1033                 }
1034             } else {
1035                 if (!comp->sgnd) {
1036                     v = readuint(f, bigendian);
1037                 } else {
1038                     v = (int) readuint(f, bigendian);
1039                 }
1040             }
1041             if (v > max) {
1042                 max = v;
1043             }
1044             comp->data[i] = v;
1045         }
1046         comp->bpp = int_floorlog2(max) + 1;
1047     }
1048     fclose(f);
1049     return volume;
1050 }
1051