1*3ac0a46fSAndroid Build Coastguard Worker /* 2*3ac0a46fSAndroid Build Coastguard Worker * The copyright in this software is being made available under the 2-clauses 3*3ac0a46fSAndroid Build Coastguard Worker * BSD License, included below. This software may be subject to other third 4*3ac0a46fSAndroid Build Coastguard Worker * party and contributor rights, including patent rights, and no such rights 5*3ac0a46fSAndroid Build Coastguard Worker * are granted under this license. 6*3ac0a46fSAndroid Build Coastguard Worker * 7*3ac0a46fSAndroid Build Coastguard Worker * Copyright (c) 2017, IntoPix SA <[email protected]> 8*3ac0a46fSAndroid Build Coastguard Worker * All rights reserved. 9*3ac0a46fSAndroid Build Coastguard Worker * 10*3ac0a46fSAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without 11*3ac0a46fSAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions 12*3ac0a46fSAndroid Build Coastguard Worker * are met: 13*3ac0a46fSAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright 14*3ac0a46fSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer. 15*3ac0a46fSAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright 16*3ac0a46fSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the 17*3ac0a46fSAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution. 18*3ac0a46fSAndroid Build Coastguard Worker * 19*3ac0a46fSAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' 20*3ac0a46fSAndroid Build Coastguard Worker * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21*3ac0a46fSAndroid Build Coastguard Worker * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22*3ac0a46fSAndroid Build Coastguard Worker * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 23*3ac0a46fSAndroid Build Coastguard Worker * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24*3ac0a46fSAndroid Build Coastguard Worker * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25*3ac0a46fSAndroid Build Coastguard Worker * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26*3ac0a46fSAndroid Build Coastguard Worker * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27*3ac0a46fSAndroid Build Coastguard Worker * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28*3ac0a46fSAndroid Build Coastguard Worker * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29*3ac0a46fSAndroid Build Coastguard Worker * POSSIBILITY OF SUCH DAMAGE. 30*3ac0a46fSAndroid Build Coastguard Worker */ 31*3ac0a46fSAndroid Build Coastguard Worker 32*3ac0a46fSAndroid Build Coastguard Worker #include "opj_includes.h" 33*3ac0a46fSAndroid Build Coastguard Worker 34*3ac0a46fSAndroid Build Coastguard Worker #ifndef OPJ_SPARSE_ARRAY_H 35*3ac0a46fSAndroid Build Coastguard Worker #define OPJ_SPARSE_ARRAY_H 36*3ac0a46fSAndroid Build Coastguard Worker /** 37*3ac0a46fSAndroid Build Coastguard Worker @file sparse_array.h 38*3ac0a46fSAndroid Build Coastguard Worker @brief Sparse array management 39*3ac0a46fSAndroid Build Coastguard Worker 40*3ac0a46fSAndroid Build Coastguard Worker The functions in this file manage sparse arrays. Sparse arrays are arrays with 41*3ac0a46fSAndroid Build Coastguard Worker potential big dimensions, but with very few samples actually set. Such sparse 42*3ac0a46fSAndroid Build Coastguard Worker arrays require allocating a low amount of memory, by just allocating memory 43*3ac0a46fSAndroid Build Coastguard Worker for blocks of the array that are set. The minimum memory allocation unit is a 44*3ac0a46fSAndroid Build Coastguard Worker a block. There is a trade-off to pick up an appropriate dimension for blocks. 45*3ac0a46fSAndroid Build Coastguard Worker If it is too big, and pixels set are far from each other, too much memory will 46*3ac0a46fSAndroid Build Coastguard Worker be used. If blocks are too small, the book-keeping costs of blocks will raise. 47*3ac0a46fSAndroid Build Coastguard Worker */ 48*3ac0a46fSAndroid Build Coastguard Worker 49*3ac0a46fSAndroid Build Coastguard Worker /** @defgroup SPARSE_ARRAY SPARSE ARRAYS - Sparse arrays */ 50*3ac0a46fSAndroid Build Coastguard Worker /*@{*/ 51*3ac0a46fSAndroid Build Coastguard Worker 52*3ac0a46fSAndroid Build Coastguard Worker /** Opaque type for sparse arrays that contain int32 values */ 53*3ac0a46fSAndroid Build Coastguard Worker typedef struct opj_sparse_array_int32 opj_sparse_array_int32_t; 54*3ac0a46fSAndroid Build Coastguard Worker 55*3ac0a46fSAndroid Build Coastguard Worker /** Creates a new sparse array. 56*3ac0a46fSAndroid Build Coastguard Worker * @param width total width of the array. 57*3ac0a46fSAndroid Build Coastguard Worker * @param height total height of the array 58*3ac0a46fSAndroid Build Coastguard Worker * @param block_width width of a block. 59*3ac0a46fSAndroid Build Coastguard Worker * @param block_height height of a block. 60*3ac0a46fSAndroid Build Coastguard Worker * @return a new sparse array instance, or NULL in case of failure. 61*3ac0a46fSAndroid Build Coastguard Worker */ 62*3ac0a46fSAndroid Build Coastguard Worker opj_sparse_array_int32_t* opj_sparse_array_int32_create(OPJ_UINT32 width, 63*3ac0a46fSAndroid Build Coastguard Worker OPJ_UINT32 height, 64*3ac0a46fSAndroid Build Coastguard Worker OPJ_UINT32 block_width, 65*3ac0a46fSAndroid Build Coastguard Worker OPJ_UINT32 block_height); 66*3ac0a46fSAndroid Build Coastguard Worker 67*3ac0a46fSAndroid Build Coastguard Worker /** Frees a sparse array. 68*3ac0a46fSAndroid Build Coastguard Worker * @param sa sparse array instance. 69*3ac0a46fSAndroid Build Coastguard Worker */ 70*3ac0a46fSAndroid Build Coastguard Worker void opj_sparse_array_int32_free(opj_sparse_array_int32_t* sa); 71*3ac0a46fSAndroid Build Coastguard Worker 72*3ac0a46fSAndroid Build Coastguard Worker /** Returns whether region bounds are valid (non empty and within array bounds) 73*3ac0a46fSAndroid Build Coastguard Worker * @param sa sparse array instance. 74*3ac0a46fSAndroid Build Coastguard Worker * @param x0 left x coordinate of the region. 75*3ac0a46fSAndroid Build Coastguard Worker * @param y0 top x coordinate of the region. 76*3ac0a46fSAndroid Build Coastguard Worker * @param x1 right x coordinate (not included) of the region. Must be greater than x0. 77*3ac0a46fSAndroid Build Coastguard Worker * @param y1 bottom y coordinate (not included) of the region. Must be greater than y0. 78*3ac0a46fSAndroid Build Coastguard Worker * @return OPJ_TRUE or OPJ_FALSE. 79*3ac0a46fSAndroid Build Coastguard Worker */ 80*3ac0a46fSAndroid Build Coastguard Worker OPJ_BOOL opj_sparse_array_is_region_valid(const opj_sparse_array_int32_t* sa, 81*3ac0a46fSAndroid Build Coastguard Worker OPJ_UINT32 x0, 82*3ac0a46fSAndroid Build Coastguard Worker OPJ_UINT32 y0, 83*3ac0a46fSAndroid Build Coastguard Worker OPJ_UINT32 x1, 84*3ac0a46fSAndroid Build Coastguard Worker OPJ_UINT32 y1); 85*3ac0a46fSAndroid Build Coastguard Worker 86*3ac0a46fSAndroid Build Coastguard Worker /** Read the content of a rectangular region of the sparse array into a 87*3ac0a46fSAndroid Build Coastguard Worker * user buffer. 88*3ac0a46fSAndroid Build Coastguard Worker * 89*3ac0a46fSAndroid Build Coastguard Worker * Regions not written with opj_sparse_array_int32_write() are read as 0. 90*3ac0a46fSAndroid Build Coastguard Worker * 91*3ac0a46fSAndroid Build Coastguard Worker * @param sa sparse array instance. 92*3ac0a46fSAndroid Build Coastguard Worker * @param x0 left x coordinate of the region to read in the sparse array. 93*3ac0a46fSAndroid Build Coastguard Worker * @param y0 top x coordinate of the region to read in the sparse array. 94*3ac0a46fSAndroid Build Coastguard Worker * @param x1 right x coordinate (not included) of the region to read in the sparse array. Must be greater than x0. 95*3ac0a46fSAndroid Build Coastguard Worker * @param y1 bottom y coordinate (not included) of the region to read in the sparse array. Must be greater than y0. 96*3ac0a46fSAndroid Build Coastguard Worker * @param dest user buffer to fill. Must be at least sizeof(int32) * ( (y1 - y0 - 1) * dest_line_stride + (x1 - x0 - 1) * dest_col_stride + 1) bytes large. 97*3ac0a46fSAndroid Build Coastguard Worker * @param dest_col_stride spacing (in elements, not in bytes) in x dimension between consecutive elements of the user buffer. 98*3ac0a46fSAndroid Build Coastguard Worker * @param dest_line_stride spacing (in elements, not in bytes) in y dimension between consecutive elements of the user buffer. 99*3ac0a46fSAndroid Build Coastguard Worker * @param forgiving if set to TRUE and the region is invalid, OPJ_TRUE will still be returned. 100*3ac0a46fSAndroid Build Coastguard Worker * @return OPJ_TRUE in case of success. 101*3ac0a46fSAndroid Build Coastguard Worker */ 102*3ac0a46fSAndroid Build Coastguard Worker OPJ_BOOL opj_sparse_array_int32_read(const opj_sparse_array_int32_t* sa, 103*3ac0a46fSAndroid Build Coastguard Worker OPJ_UINT32 x0, 104*3ac0a46fSAndroid Build Coastguard Worker OPJ_UINT32 y0, 105*3ac0a46fSAndroid Build Coastguard Worker OPJ_UINT32 x1, 106*3ac0a46fSAndroid Build Coastguard Worker OPJ_UINT32 y1, 107*3ac0a46fSAndroid Build Coastguard Worker OPJ_INT32* dest, 108*3ac0a46fSAndroid Build Coastguard Worker OPJ_UINT32 dest_col_stride, 109*3ac0a46fSAndroid Build Coastguard Worker OPJ_UINT32 dest_line_stride, 110*3ac0a46fSAndroid Build Coastguard Worker OPJ_BOOL forgiving); 111*3ac0a46fSAndroid Build Coastguard Worker 112*3ac0a46fSAndroid Build Coastguard Worker 113*3ac0a46fSAndroid Build Coastguard Worker /** Write the content of a rectangular region into the sparse array from a 114*3ac0a46fSAndroid Build Coastguard Worker * user buffer. 115*3ac0a46fSAndroid Build Coastguard Worker * 116*3ac0a46fSAndroid Build Coastguard Worker * Blocks intersecting the region are allocated, if not already done. 117*3ac0a46fSAndroid Build Coastguard Worker * 118*3ac0a46fSAndroid Build Coastguard Worker * @param sa sparse array instance. 119*3ac0a46fSAndroid Build Coastguard Worker * @param x0 left x coordinate of the region to write into the sparse array. 120*3ac0a46fSAndroid Build Coastguard Worker * @param y0 top x coordinate of the region to write into the sparse array. 121*3ac0a46fSAndroid Build Coastguard Worker * @param x1 right x coordinate (not included) of the region to write into the sparse array. Must be greater than x0. 122*3ac0a46fSAndroid Build Coastguard Worker * @param y1 bottom y coordinate (not included) of the region to write into the sparse array. Must be greater than y0. 123*3ac0a46fSAndroid Build Coastguard Worker * @param src user buffer to fill. Must be at least sizeof(int32) * ( (y1 - y0 - 1) * src_line_stride + (x1 - x0 - 1) * src_col_stride + 1) bytes large. 124*3ac0a46fSAndroid Build Coastguard Worker * @param src_col_stride spacing (in elements, not in bytes) in x dimension between consecutive elements of the user buffer. 125*3ac0a46fSAndroid Build Coastguard Worker * @param src_line_stride spacing (in elements, not in bytes) in y dimension between consecutive elements of the user buffer. 126*3ac0a46fSAndroid Build Coastguard Worker * @param forgiving if set to TRUE and the region is invalid, OPJ_TRUE will still be returned. 127*3ac0a46fSAndroid Build Coastguard Worker * @return OPJ_TRUE in case of success. 128*3ac0a46fSAndroid Build Coastguard Worker */ 129*3ac0a46fSAndroid Build Coastguard Worker OPJ_BOOL opj_sparse_array_int32_write(opj_sparse_array_int32_t* sa, 130*3ac0a46fSAndroid Build Coastguard Worker OPJ_UINT32 x0, 131*3ac0a46fSAndroid Build Coastguard Worker OPJ_UINT32 y0, 132*3ac0a46fSAndroid Build Coastguard Worker OPJ_UINT32 x1, 133*3ac0a46fSAndroid Build Coastguard Worker OPJ_UINT32 y1, 134*3ac0a46fSAndroid Build Coastguard Worker const OPJ_INT32* src, 135*3ac0a46fSAndroid Build Coastguard Worker OPJ_UINT32 src_col_stride, 136*3ac0a46fSAndroid Build Coastguard Worker OPJ_UINT32 src_line_stride, 137*3ac0a46fSAndroid Build Coastguard Worker OPJ_BOOL forgiving); 138*3ac0a46fSAndroid Build Coastguard Worker 139*3ac0a46fSAndroid Build Coastguard Worker /*@}*/ 140*3ac0a46fSAndroid Build Coastguard Worker 141*3ac0a46fSAndroid Build Coastguard Worker #endif /* OPJ_SPARSE_ARRAY_H */ 142