Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce memory decoding #968

Merged
merged 8 commits into from
Aug 7, 2017
122 changes: 61 additions & 61 deletions src/bin/common/color.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ static void sycc444_to_rgb(opj_image_t *img)
cb = img->comps[1].data;
cr = img->comps[2].data;

d0 = r = (int*)malloc(sizeof(int) * max);
d1 = g = (int*)malloc(sizeof(int) * max);
d2 = b = (int*)malloc(sizeof(int) * max);
d0 = r = (int*)opj_image_data_alloc(sizeof(int) * max);
d1 = g = (int*)opj_image_data_alloc(sizeof(int) * max);
d2 = b = (int*)opj_image_data_alloc(sizeof(int) * max);

if (r == NULL || g == NULL || b == NULL) {
goto fails;
Expand All @@ -139,19 +139,19 @@ static void sycc444_to_rgb(opj_image_t *img)
++g;
++b;
}
free(img->comps[0].data);
opj_image_data_free(img->comps[0].data);
img->comps[0].data = d0;
free(img->comps[1].data);
opj_image_data_free(img->comps[1].data);
img->comps[1].data = d1;
free(img->comps[2].data);
opj_image_data_free(img->comps[2].data);
img->comps[2].data = d2;
img->color_space = OPJ_CLRSPC_SRGB;
return;

fails:
free(r);
free(g);
free(b);
opj_image_data_free(r);
opj_image_data_free(g);
opj_image_data_free(b);
}/* sycc444_to_rgb() */

static void sycc422_to_rgb(opj_image_t *img)
Expand All @@ -174,9 +174,9 @@ static void sycc422_to_rgb(opj_image_t *img)
cb = img->comps[1].data;
cr = img->comps[2].data;

d0 = r = (int*)malloc(sizeof(int) * max);
d1 = g = (int*)malloc(sizeof(int) * max);
d2 = b = (int*)malloc(sizeof(int) * max);
d0 = r = (int*)opj_image_data_alloc(sizeof(int) * max);
d1 = g = (int*)opj_image_data_alloc(sizeof(int) * max);
d2 = b = (int*)opj_image_data_alloc(sizeof(int) * max);

if (r == NULL || g == NULL || b == NULL) {
goto fails;
Expand Down Expand Up @@ -222,11 +222,11 @@ static void sycc422_to_rgb(opj_image_t *img)
}
}

free(img->comps[0].data);
opj_image_data_free(img->comps[0].data);
img->comps[0].data = d0;
free(img->comps[1].data);
opj_image_data_free(img->comps[1].data);
img->comps[1].data = d1;
free(img->comps[2].data);
opj_image_data_free(img->comps[2].data);
img->comps[2].data = d2;

img->comps[1].w = img->comps[2].w = img->comps[0].w;
Expand All @@ -237,9 +237,9 @@ static void sycc422_to_rgb(opj_image_t *img)
return;

fails:
free(r);
free(g);
free(b);
opj_image_data_free(r);
opj_image_data_free(g);
opj_image_data_free(b);
}/* sycc422_to_rgb() */

static void sycc420_to_rgb(opj_image_t *img)
Expand All @@ -262,9 +262,9 @@ static void sycc420_to_rgb(opj_image_t *img)
cb = img->comps[1].data;
cr = img->comps[2].data;

d0 = r = (int*)malloc(sizeof(int) * max);
d1 = g = (int*)malloc(sizeof(int) * max);
d2 = b = (int*)malloc(sizeof(int) * max);
d0 = r = (int*)opj_image_data_alloc(sizeof(int) * max);
d1 = g = (int*)opj_image_data_alloc(sizeof(int) * max);
d2 = b = (int*)opj_image_data_alloc(sizeof(int) * max);

if (r == NULL || g == NULL || b == NULL) {
goto fails;
Expand Down Expand Up @@ -380,11 +380,11 @@ static void sycc420_to_rgb(opj_image_t *img)
}
}

free(img->comps[0].data);
opj_image_data_free(img->comps[0].data);
img->comps[0].data = d0;
free(img->comps[1].data);
opj_image_data_free(img->comps[1].data);
img->comps[1].data = d1;
free(img->comps[2].data);
opj_image_data_free(img->comps[2].data);
img->comps[2].data = d2;

img->comps[1].w = img->comps[2].w = img->comps[0].w;
Expand All @@ -395,9 +395,9 @@ static void sycc420_to_rgb(opj_image_t *img)
return;

fails:
free(r);
free(g);
free(b);
opj_image_data_free(r);
opj_image_data_free(g);
opj_image_data_free(b);
}/* sycc420_to_rgb() */

void color_sycc_to_rgb(opj_image_t *img)
Expand Down Expand Up @@ -574,8 +574,8 @@ void color_apply_icc_profile(opj_image_t *image)

max = max_w * max_h;
nr_samples = (size_t)(max * 3U * sizeof(unsigned char));
in = inbuf = (unsigned char*)malloc(nr_samples);
out = outbuf = (unsigned char*)malloc(nr_samples);
in = inbuf = (unsigned char*)opj_image_data_alloc(nr_samples);
out = outbuf = (unsigned char*)opj_image_data_alloc(nr_samples);

if (inbuf == NULL || outbuf == NULL) {
goto fails0;
Expand Down Expand Up @@ -605,15 +605,15 @@ void color_apply_icc_profile(opj_image_t *image)
ok = 1;

fails0:
free(inbuf);
free(outbuf);
opj_image_data_free(inbuf);
opj_image_data_free(outbuf);
} else { /* prec > 8 */
unsigned short *inbuf, *outbuf, *in, *out;

max = max_w * max_h;
nr_samples = (size_t)(max * 3U * sizeof(unsigned short));
in = inbuf = (unsigned short*)malloc(nr_samples);
out = outbuf = (unsigned short*)malloc(nr_samples);
in = inbuf = (unsigned short*)opj_image_data_alloc(nr_samples);
out = outbuf = (unsigned short*)opj_image_data_alloc(nr_samples);

if (inbuf == NULL || outbuf == NULL) {
goto fails1;
Expand Down Expand Up @@ -643,8 +643,8 @@ void color_apply_icc_profile(opj_image_t *image)
ok = 1;

fails1:
free(inbuf);
free(outbuf);
opj_image_data_free(inbuf);
opj_image_data_free(outbuf);
}
} else { /* image->numcomps <= 2 : GRAY, GRAYA */
if (prec <= 8) {
Expand All @@ -653,10 +653,10 @@ void color_apply_icc_profile(opj_image_t *image)

max = max_w * max_h;
nr_samples = (size_t)(max * 3 * sizeof(unsigned char));
in = inbuf = (unsigned char*)malloc(nr_samples);
out = outbuf = (unsigned char*)malloc(nr_samples);
g = (int*)calloc((size_t)max, sizeof(int));
b = (int*)calloc((size_t)max, sizeof(int));
in = inbuf = (unsigned char*)opj_image_data_alloc(nr_samples);
out = outbuf = (unsigned char*)opj_image_data_alloc(nr_samples);
g = (int*)opj_image_data_alloc((size_t)max * sizeof(int));
b = (int*)opj_image_data_alloc((size_t)max * sizeof(int));

if (inbuf == NULL || outbuf == NULL || g == NULL || b == NULL) {
goto fails2;
Expand Down Expand Up @@ -703,20 +703,20 @@ void color_apply_icc_profile(opj_image_t *image)
ok = 1;

fails2:
free(inbuf);
free(outbuf);
free(g);
free(b);
opj_image_data_free(inbuf);
opj_image_data_free(outbuf);
opj_image_data_free(g);
opj_image_data_free(b);
} else { /* prec > 8 */
unsigned short *in, *inbuf, *out, *outbuf;
opj_image_comp_t *new_comps;

max = max_w * max_h;
nr_samples = (size_t)(max * 3U * sizeof(unsigned short));
in = inbuf = (unsigned short*)malloc(nr_samples);
out = outbuf = (unsigned short*)malloc(nr_samples);
g = (int*)calloc((size_t)max, sizeof(int));
b = (int*)calloc((size_t)max, sizeof(int));
in = inbuf = (unsigned short*)opj_image_data_alloc(nr_samples);
out = outbuf = (unsigned short*)opj_image_data_alloc(nr_samples);
g = (int*)opj_image_data_alloc((size_t)max * sizeof(int));
b = (int*)opj_image_data_alloc((size_t)max * sizeof(int));

if (inbuf == NULL || outbuf == NULL || g == NULL || b == NULL) {
goto fails3;
Expand Down Expand Up @@ -763,10 +763,10 @@ void color_apply_icc_profile(opj_image_t *image)
ok = 1;

fails3:
free(inbuf);
free(outbuf);
free(g);
free(b);
opj_image_data_free(inbuf);
opj_image_data_free(outbuf);
opj_image_data_free(g);
opj_image_data_free(b);
}
}/* if(image->numcomps > 2) */

Expand Down Expand Up @@ -881,9 +881,9 @@ void color_cielab_to_rgb(opj_image_t *image)

max = image->comps[0].w * image->comps[0].h;

red = dst0 = (int*)malloc(max * sizeof(int));
green = dst1 = (int*)malloc(max * sizeof(int));
blue = dst2 = (int*)malloc(max * sizeof(int));
red = dst0 = (int*)opj_image_data_alloc(max * sizeof(int));
green = dst1 = (int*)opj_image_data_alloc(max * sizeof(int));
blue = dst2 = (int*)opj_image_data_alloc(max * sizeof(int));

if (red == NULL || green == NULL || blue == NULL) {
goto fails;
Expand Down Expand Up @@ -917,11 +917,11 @@ void color_cielab_to_rgb(opj_image_t *image)
cmsCloseProfile(in);
cmsCloseProfile(out);
#endif
free(src0);
opj_image_data_free(src0);
image->comps[0].data = dst0;
free(src1);
opj_image_data_free(src1);
image->comps[1].data = dst1;
free(src2);
opj_image_data_free(src2);
image->comps[2].data = dst2;

image->color_space = new_space;
Expand All @@ -938,13 +938,13 @@ void color_cielab_to_rgb(opj_image_t *image)
cmsCloseProfile(out);
#endif
if (red) {
free(red);
opj_image_data_free(red);
}
if (green) {
free(green);
opj_image_data_free(green);
}
if (blue) {
free(blue);
opj_image_data_free(blue);
}
return;
}
Expand Down Expand Up @@ -1004,7 +1004,7 @@ void color_cmyk_to_rgb(opj_image_t *image)
image->comps[2].data[i] = (int)(255.0F * Y * K); /* B */
}

free(image->comps[3].data);
opj_image_data_free(image->comps[3].data);
image->comps[3].data = NULL;
image->comps[0].prec = 8;
image->comps[1].prec = 8;
Expand Down
10 changes: 6 additions & 4 deletions src/lib/openjp2/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,21 @@ opj_image_t* OPJ_CALLCONV opj_image_create(OPJ_UINT32 numcmpts,
comp->prec = cmptparms[compno].prec;
comp->bpp = cmptparms[compno].bpp;
comp->sgnd = cmptparms[compno].sgnd;
if (comp->h != 0 && (OPJ_SIZE_T)comp->w > SIZE_MAX / comp->h) {
if (comp->h != 0 &&
(OPJ_SIZE_T)comp->w > SIZE_MAX / comp->h / sizeof(OPJ_INT32)) {
// TODO event manager
opj_image_destroy(image);
return NULL;
}
comp->data = (OPJ_INT32*) opj_calloc((OPJ_SIZE_T)comp->w * comp->h,
sizeof(OPJ_INT32));
comp->data = (OPJ_INT32*) opj_image_data_alloc(
(size_t)comp->w * comp->h * sizeof(OPJ_INT32));
if (!comp->data) {
/* TODO replace with event manager, breaks API */
/* fprintf(stderr,"Unable to allocate memory for image.\n"); */
opj_image_destroy(image);
return NULL;
}
memset(comp->data, 0, (size_t)comp->w * comp->h * sizeof(OPJ_INT32));
}
}

Expand All @@ -97,7 +99,7 @@ void OPJ_CALLCONV opj_image_destroy(opj_image_t *image)
for (compno = 0; compno < image->numcomps; compno++) {
opj_image_comp_t *image_comp = &(image->comps[compno]);
if (image_comp->data) {
opj_free(image_comp->data);
opj_image_data_free(image_comp->data);
}
}
opj_free(image->comps);
Expand Down
Loading