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