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