[trunk] Import patch from sumatrapdf team. This handle testcase 2.pdf.SIGFPE.706...
[openjpeg.git] / src / lib / openjp3d / volume.c
1 /*
2  * Copyright (c) 2005, Herve Drolon, FreeImage Team
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "opj_includes.h"
28 #include "volume.h"
29 #include "openjp3d.h"
30
31 opj_volume_t* OPJ_CALLCONV opj_volume_create(int numcmpts, opj_volume_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) {
32         int compno;
33         opj_volume_t *volume = NULL;
34
35         volume = (opj_volume_t*)opj_malloc(sizeof(opj_volume_t));
36         if(volume) {
37                 volume->color_space = clrspc;
38                 volume->numcomps = numcmpts;
39                 /* allocate memory for the per-component information */
40                 volume->comps = (opj_volume_comp_t*)opj_malloc(volume->numcomps * sizeof(opj_volume_comp_t));
41                 if(!volume->comps) {
42                         opj_volume_destroy(volume);
43                         return NULL;
44                 }
45                 /* create the individual volume components */
46                 for(compno = 0; compno < numcmpts; compno++) {
47                         opj_volume_comp_t *comp = &volume->comps[compno];
48                         comp->dx = cmptparms[compno].dx;
49                         comp->dy = cmptparms[compno].dy;
50                         comp->dz = cmptparms[compno].dz;
51                         comp->w = cmptparms[compno].w;
52                         comp->h = cmptparms[compno].h;
53                         comp->l = cmptparms[compno].l;
54                         comp->x0 = cmptparms[compno].x0;
55                         comp->y0 = cmptparms[compno].y0;
56                         comp->z0 = cmptparms[compno].z0;
57                         comp->prec = cmptparms[compno].prec;
58                         comp->bpp = cmptparms[compno].bpp;
59                         comp->sgnd = cmptparms[compno].sgnd;
60                         comp->bigendian = cmptparms[compno].bigendian;
61                         comp->dcoffset = cmptparms[compno].dcoffset;
62                         comp->data = (int*)opj_malloc(comp->w * comp->h * comp->l * sizeof(int));
63                         if(!comp->data) {
64                                 fprintf(stdout,"Unable to malloc comp->data (%d x %d x %d x bytes)",comp->w,comp->h,comp->l);
65                                 opj_volume_destroy(volume);
66                                 return NULL;
67                         }
68                         /*fprintf(stdout,"%d %d %d %d %d %d %d %d %d", comp->w,comp->h, comp->l, comp->dx, comp->dy, comp->dz, comp->prec, comp->bpp, comp->sgnd);*/
69                 }
70         }
71
72         return volume;
73 }
74
75 void OPJ_CALLCONV opj_volume_destroy(opj_volume_t *volume) {
76         int i;
77         if(volume) {
78                 if(volume->comps) {
79                         /* volume components */
80                         for(i = 0; i < volume->numcomps; i++) {
81                                 opj_volume_comp_t *volume_comp = &volume->comps[i];
82                                 if(volume_comp->data) {
83                                         opj_free(volume_comp->data);
84                                 }
85                         }
86                         opj_free(volume->comps);
87                 }
88                 opj_free(volume);
89         }
90 }
91