opj_j2k_update_image_data(): avoid zero-ing the buffer if not needed
authorEven Rouault <even.rouault@spatialys.com>
Fri, 1 Sep 2017 14:30:52 +0000 (16:30 +0200)
committerEven Rouault <even.rouault@spatialys.com>
Fri, 1 Sep 2017 14:30:52 +0000 (16:30 +0200)
src/lib/openjp2/j2k.c

index e548fefc776568dcb242e37962f0c4a15556b98d..8fb56e37d4dc8aa5504b9e34cdd2f36e0409af5e 100644 (file)
@@ -8926,6 +8926,7 @@ static OPJ_BOOL opj_j2k_update_image_data(opj_tcd_t * p_tcd,
         OPJ_INT32 res_x0, res_x1, res_y0, res_y1;
         OPJ_UINT32 src_data_stride;
         const OPJ_INT32* p_src_data;
+        OPJ_BOOL check_if_must_memset = OPJ_FALSE;
 
         /* Allocate output component buffer if necessary */
         if (!l_img_comp_dest->data) {
@@ -8942,8 +8943,8 @@ static OPJ_BOOL opj_j2k_update_image_data(opj_tcd_t * p_tcd,
             if (! l_img_comp_dest->data) {
                 return OPJ_FALSE;
             }
-            /* Do we really need this memset ? */
-            memset(l_img_comp_dest->data, 0, l_width * l_height * sizeof(OPJ_INT32));
+
+            check_if_must_memset = OPJ_TRUE;
         }
 
         /* Copy info from decoded comp image to output image */
@@ -9061,6 +9062,12 @@ static OPJ_BOOL opj_j2k_update_image_data(opj_tcd_t * p_tcd,
         }
         /*-----*/
 
+        if (check_if_must_memset && (l_img_comp_dest->w != l_width_dest ||
+                                     l_img_comp_dest->h != l_height_dest)) {
+            memset(l_img_comp_dest->data, 0,
+                   (OPJ_SIZE_T)l_img_comp_dest->w * l_img_comp_dest->h * sizeof(OPJ_INT32));
+        }
+
         /* Compute the input buffer offset */
         l_start_offset_src = (OPJ_SIZE_T)l_offset_x0_src + (OPJ_SIZE_T)l_offset_y0_src
                              * (OPJ_SIZE_T)src_data_stride;