sparse_array.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #include "opj_includes.h"
  32. #ifndef OPJ_SPARSE_ARRAY_H
  33. #define OPJ_SPARSE_ARRAY_H
  34. /**
  35. @file sparse_array.h
  36. @brief Sparse array management
  37. The functions in this file manage sparse arrays. Sparse arrays are arrays with
  38. potential big dimensions, but with very few samples actually set. Such sparse
  39. arrays require allocating a low amount of memory, by just allocating memory
  40. for blocks of the array that are set. The minimum memory allocation unit is a
  41. a block. There is a trade-off to pick up an appropriate dimension for blocks.
  42. If it is too big, and pixels set are far from each other, too much memory will
  43. be used. If blocks are too small, the book-keeping costs of blocks will raise.
  44. */
  45. /** @defgroup SPARSE_ARRAY SPARSE ARRAYS - Sparse arrays */
  46. /*@{*/
  47. /** Opaque type for sparse arrays that contain int32 values */
  48. typedef struct opj_sparse_array_int32 opj_sparse_array_int32_t;
  49. /** Creates a new sparse array.
  50. * @param width total width of the array.
  51. * @param height total height of the array
  52. * @param block_width width of a block.
  53. * @param block_height height of a block.
  54. * @return a new sparse array instance, or NULL in case of failure.
  55. */
  56. opj_sparse_array_int32_t* opj_sparse_array_int32_create(OPJ_UINT32 width,
  57. OPJ_UINT32 height,
  58. OPJ_UINT32 block_width,
  59. OPJ_UINT32 block_height);
  60. /** Frees a sparse array.
  61. * @param sa sparse array instance.
  62. */
  63. void opj_sparse_array_int32_free(opj_sparse_array_int32_t* sa);
  64. /** Returns whether region bounds are valid (non empty and within array bounds)
  65. * @param sa sparse array instance.
  66. * @param x0 left x coordinate of the region.
  67. * @param y0 top x coordinate of the region.
  68. * @param x1 right x coordinate (not included) of the region. Must be greater than x0.
  69. * @param y1 bottom y coordinate (not included) of the region. Must be greater than y0.
  70. * @return OPJ_TRUE or OPJ_FALSE.
  71. */
  72. OPJ_BOOL opj_sparse_array_is_region_valid(const opj_sparse_array_int32_t* sa,
  73. OPJ_UINT32 x0,
  74. OPJ_UINT32 y0,
  75. OPJ_UINT32 x1,
  76. OPJ_UINT32 y1);
  77. /** Read the content of a rectangular region of the sparse array into a
  78. * user buffer.
  79. *
  80. * Regions not written with opj_sparse_array_int32_write() are read as 0.
  81. *
  82. * @param sa sparse array instance.
  83. * @param x0 left x coordinate of the region to read in the sparse array.
  84. * @param y0 top x coordinate of the region to read in the sparse array.
  85. * @param x1 right x coordinate (not included) of the region to read in the sparse array. Must be greater than x0.
  86. * @param y1 bottom y coordinate (not included) of the region to read in the sparse array. Must be greater than y0.
  87. * @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.
  88. * @param dest_col_stride spacing (in elements, not in bytes) in x dimension between consecutive elements of the user buffer.
  89. * @param dest_line_stride spacing (in elements, not in bytes) in y dimension between consecutive elements of the user buffer.
  90. * @param forgiving if set to TRUE and the region is invalid, OPJ_TRUE will still be returned.
  91. * @return OPJ_TRUE in case of success.
  92. */
  93. OPJ_BOOL opj_sparse_array_int32_read(const opj_sparse_array_int32_t* sa,
  94. OPJ_UINT32 x0,
  95. OPJ_UINT32 y0,
  96. OPJ_UINT32 x1,
  97. OPJ_UINT32 y1,
  98. OPJ_INT32* dest,
  99. OPJ_UINT32 dest_col_stride,
  100. OPJ_UINT32 dest_line_stride,
  101. OPJ_BOOL forgiving);
  102. /** Write the content of a rectangular region into the sparse array from a
  103. * user buffer.
  104. *
  105. * Blocks intersecting the region are allocated, if not already done.
  106. *
  107. * @param sa sparse array instance.
  108. * @param x0 left x coordinate of the region to write into the sparse array.
  109. * @param y0 top x coordinate of the region to write into the sparse array.
  110. * @param x1 right x coordinate (not included) of the region to write into the sparse array. Must be greater than x0.
  111. * @param y1 bottom y coordinate (not included) of the region to write into the sparse array. Must be greater than y0.
  112. * @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.
  113. * @param src_col_stride spacing (in elements, not in bytes) in x dimension between consecutive elements of the user buffer.
  114. * @param src_line_stride spacing (in elements, not in bytes) in y dimension between consecutive elements of the user buffer.
  115. * @param forgiving if set to TRUE and the region is invalid, OPJ_TRUE will still be returned.
  116. * @return OPJ_TRUE in case of success.
  117. */
  118. OPJ_BOOL opj_sparse_array_int32_write(opj_sparse_array_int32_t* sa,
  119. OPJ_UINT32 x0,
  120. OPJ_UINT32 y0,
  121. OPJ_UINT32 x1,
  122. OPJ_UINT32 y1,
  123. const OPJ_INT32* src,
  124. OPJ_UINT32 src_col_stride,
  125. OPJ_UINT32 src_line_stride,
  126. OPJ_BOOL forgiving);
  127. /*@}*/
  128. #endif /* OPJ_SPARSE_ARRAY_H */