Sub-tile decoding: only allocate tile component buffer of the needed dimension
[openjpeg.git] / src / lib / openjp2 / sparse_array.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) 2017, IntoPix SA <contact@intopix.com>
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
35 struct opj_sparse_array_int32 {
36     OPJ_UINT32 width;
37     OPJ_UINT32 height;
38     OPJ_UINT32 block_width;
39     OPJ_UINT32 block_height;
40     OPJ_UINT32 block_count_hor;
41     OPJ_UINT32 block_count_ver;
42     OPJ_INT32** data_blocks;
43 };
44
45 opj_sparse_array_int32_t* opj_sparse_array_int32_create(OPJ_UINT32 width,
46         OPJ_UINT32 height,
47         OPJ_UINT32 block_width,
48         OPJ_UINT32 block_height)
49 {
50     opj_sparse_array_int32_t* sa;
51
52     if (width == 0 || height == 0 || block_width == 0 || block_height == 0) {
53         return NULL;
54     }
55     if (block_width > ((OPJ_UINT32)~0U) / block_height / sizeof(OPJ_INT32)) {
56         return NULL;
57     }
58
59     sa = opj_calloc(1, sizeof(opj_sparse_array_int32_t));
60     sa->width = width;
61     sa->height = height;
62     sa->block_width = block_width;
63     sa->block_height = block_height;
64     sa->block_count_hor = opj_uint_ceildiv(width, block_width);
65     sa->block_count_ver = opj_uint_ceildiv(height, block_height);
66     if (sa->block_count_hor > ((OPJ_UINT32)~0U) / sa->block_count_ver) {
67         opj_free(sa);
68         return NULL;
69     }
70     sa->data_blocks = opj_calloc(sizeof(OPJ_INT32*),
71                                  sa->block_count_hor * sa->block_count_ver);
72     if (sa->data_blocks == NULL) {
73         opj_free(sa);
74         return NULL;
75     }
76
77     return sa;
78 }
79
80 void opj_sparse_array_int32_free(opj_sparse_array_int32_t* sa)
81 {
82     if (sa) {
83         OPJ_UINT32 i;
84         for (i = 0; i < sa->block_count_hor * sa->block_count_ver; i++) {
85             if (sa->data_blocks[i]) {
86                 opj_free(sa->data_blocks[i]);
87             }
88         }
89         opj_free(sa->data_blocks);
90         opj_free(sa);
91     }
92 }
93
94 OPJ_BOOL opj_sparse_array_is_region_valid(opj_sparse_array_int32_t* sa,
95         OPJ_UINT32 x0,
96         OPJ_UINT32 y0,
97         OPJ_UINT32 x1,
98         OPJ_UINT32 y1)
99 {
100     return !(x0 >= sa->width || x1 <= x0 || x1 > sa->width ||
101              y0 >= sa->height || y1 <= y0 || y1 > sa->height);
102 }
103
104 static OPJ_BOOL opj_sparse_array_int32_read_or_write(
105     opj_sparse_array_int32_t* sa,
106     OPJ_UINT32 x0,
107     OPJ_UINT32 y0,
108     OPJ_UINT32 x1,
109     OPJ_UINT32 y1,
110     OPJ_INT32* buf,
111     OPJ_UINT32 buf_col_stride,
112     OPJ_UINT32 buf_line_stride,
113     OPJ_BOOL forgiving,
114     OPJ_BOOL is_read_op)
115 {
116     OPJ_UINT32 y, block_y;
117     OPJ_UINT32 y_incr = 0;
118     if (!opj_sparse_array_is_region_valid(sa, x0, y0, x1, y1)) {
119         return forgiving;
120     }
121
122     block_y = y0 / sa->block_height;
123     for (y = y0; y < y1; block_y ++, y += y_incr) {
124         OPJ_UINT32 x, block_x;
125         OPJ_UINT32 x_incr = 0;
126         OPJ_UINT32 block_y_offset;
127         y_incr = (y == y0) ? sa->block_height - (y0 % sa->block_height) :
128                  sa->block_height;
129         block_y_offset = sa->block_height - y_incr;
130         y_incr = opj_uint_min(y_incr, y1 - y);
131         block_x = x0 / sa->block_width;
132         for (x = x0; x < x1; block_x ++, x += x_incr) {
133             OPJ_UINT32 j;
134             OPJ_UINT32 block_x_offset;
135             OPJ_INT32* src_block;
136             x_incr = (x == x0) ? sa->block_width - (x0 % sa->block_width) : sa->block_width;
137             block_x_offset = sa->block_width - x_incr;
138             x_incr = opj_uint_min(x_incr, x1 - x);
139             src_block = sa->data_blocks[block_y * sa->block_count_hor + block_x];
140             if (is_read_op) {
141                 if (src_block == NULL) {
142                     for (j = 0; j < y_incr; j++) {
143                         if (buf_col_stride == 1) {
144                             memset(buf + (y - y0 + j) * buf_line_stride + (x - x0) * buf_col_stride,
145                                    0,
146                                    sizeof(OPJ_INT32) * x_incr);
147                         } else {
148                             OPJ_UINT32 k;
149                             for (k = 0; k < x_incr; k++) {
150                                 *(buf + (y - y0 + j) * buf_line_stride + (x - x0 + k) * buf_col_stride) = 0;
151                             }
152                         }
153                     }
154                 } else {
155                     for (j = 0; j < y_incr; j++) {
156                         if (buf_col_stride == 1) {
157                             memcpy(buf + (y - y0 + j) * buf_line_stride + (x - x0) * buf_col_stride,
158                                    src_block + (block_y_offset + j) * sa->block_width + block_x_offset,
159                                    sizeof(OPJ_INT32) * x_incr);
160                         } else {
161                             OPJ_UINT32 k;
162                             for (k = 0; k < x_incr; k++) {
163                                 *(buf + (y - y0 + j) * buf_line_stride + (x - x0 + k) * buf_col_stride) =
164                                     *(src_block + (block_y_offset + j) * sa->block_width + block_x_offset + k);
165                             }
166                         }
167                     }
168                 }
169             } else {
170                 if (src_block == NULL) {
171                     src_block = opj_calloc(1,
172                                            sa->block_width * sa->block_height * sizeof(OPJ_INT32));
173                     if (src_block == NULL) {
174                         return OPJ_FALSE;
175                     }
176                     sa->data_blocks[block_y * sa->block_count_hor + block_x] = src_block;
177                 }
178
179                 for (j = 0; j < y_incr; j++) {
180                     if (buf_col_stride == 1) {
181                         memcpy(src_block + (block_y_offset + j) * sa->block_width + block_x_offset,
182                                buf + (y - y0 + j) * buf_line_stride + (x - x0) * buf_col_stride,
183                                sizeof(OPJ_INT32) * x_incr);
184                     } else {
185                         OPJ_UINT32 k;
186                         for (k = 0; k < x_incr; k++) {
187                             *(src_block + (block_y_offset + j) * sa->block_width + block_x_offset + k) =
188                                 *(buf + (y - y0 + j) * buf_line_stride + (x - x0 + k) * buf_col_stride);
189                         }
190                     }
191                 }
192             }
193         }
194     }
195
196     return OPJ_TRUE;
197 }
198
199 OPJ_BOOL opj_sparse_array_int32_read(opj_sparse_array_int32_t* sa,
200                                      OPJ_UINT32 x0,
201                                      OPJ_UINT32 y0,
202                                      OPJ_UINT32 x1,
203                                      OPJ_UINT32 y1,
204                                      OPJ_INT32* dest,
205                                      OPJ_UINT32 dest_col_stride,
206                                      OPJ_UINT32 dest_line_stride,
207                                      OPJ_BOOL forgiving)
208 {
209     return opj_sparse_array_int32_read_or_write(sa, x0, y0, x1, y1,
210             dest,
211             dest_col_stride,
212             dest_line_stride,
213             forgiving,
214             OPJ_TRUE);
215 }
216
217 OPJ_BOOL opj_sparse_array_int32_write(opj_sparse_array_int32_t* sa,
218                                       OPJ_UINT32 x0,
219                                       OPJ_UINT32 y0,
220                                       OPJ_UINT32 x1,
221                                       OPJ_UINT32 y1,
222                                       const OPJ_INT32* src,
223                                       OPJ_UINT32 src_col_stride,
224                                       OPJ_UINT32 src_line_stride,
225                                       OPJ_BOOL forgiving)
226 {
227     return opj_sparse_array_int32_read_or_write(sa, x0, y0, x1, y1,
228             (OPJ_INT32*)src,
229             src_col_stride,
230             src_line_stride,
231             forgiving,
232             OPJ_FALSE);
233 }