TCD: allow tile buffer to be greater than 4GB on 64 bit hosts (but number of pixels...
[openjpeg.git] / src / lib / openjp2 / test_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 #undef NDEBUG
33
34 #include "opj_includes.h"
35
36 int main()
37 {
38     OPJ_UINT32 i, j, w, h;
39     OPJ_INT32 buffer[ 99 * 101 ];
40     OPJ_BOOL ret;
41     opj_sparse_array_int32_t* sa;
42
43     sa = opj_sparse_array_int32_create(0, 1, 1, 1);
44     assert(sa == NULL);
45     opj_sparse_array_int32_free(sa);
46
47     sa = opj_sparse_array_int32_create(1, 0, 1, 1);
48     assert(sa == NULL);
49
50     sa = opj_sparse_array_int32_create(1, 1, 0, 1);
51     assert(sa == NULL);
52
53     sa = opj_sparse_array_int32_create(1, 1, 1, 0);
54     assert(sa == NULL);
55
56     sa = opj_sparse_array_int32_create(99, 101, ~0U, ~0U);
57     assert(sa == NULL);
58
59     sa = opj_sparse_array_int32_create(99, 101, 15, 17);
60     opj_sparse_array_int32_free(sa);
61
62     sa = opj_sparse_array_int32_create(99, 101, 15, 17);
63     ret = opj_sparse_array_int32_read(sa, 0, 0, 0, 1, buffer, 1, 1, OPJ_FALSE);
64     assert(!ret);
65     ret = opj_sparse_array_int32_read(sa, 0, 0, 1, 0, buffer, 1, 1, OPJ_FALSE);
66     assert(!ret);
67     ret = opj_sparse_array_int32_read(sa, 0, 0, 100, 1, buffer, 1, 1, OPJ_FALSE);
68     assert(!ret);
69     ret = opj_sparse_array_int32_read(sa, 0, 0, 1, 102, buffer, 1, 1, OPJ_FALSE);
70     assert(!ret);
71     ret = opj_sparse_array_int32_read(sa, 1, 0, 0, 1, buffer, 1, 1, OPJ_FALSE);
72     assert(!ret);
73     ret = opj_sparse_array_int32_read(sa, 0, 1, 1, 0, buffer, 1, 1, OPJ_FALSE);
74     assert(!ret);
75     ret = opj_sparse_array_int32_read(sa, 99, 101, 99, 101, buffer, 1, 1,
76                                       OPJ_FALSE);
77     assert(!ret);
78
79     buffer[0] = 1;
80     ret = opj_sparse_array_int32_read(sa, 0, 0, 1, 1, buffer, 1, 1, OPJ_FALSE);
81     assert(ret);
82     assert(buffer[0] == 0);
83
84     memset(buffer, 0xFF, sizeof(buffer));
85     ret = opj_sparse_array_int32_read(sa, 0, 0, 99, 101, buffer, 1, 99, OPJ_FALSE);
86     assert(ret);
87     for (i = 0; i < 99 * 101; i++) {
88         assert(buffer[i] == 0);
89     }
90
91     buffer[0] = 1;
92     ret = opj_sparse_array_int32_write(sa, 4, 5, 4 + 1, 5 + 1, buffer, 1, 1,
93                                        OPJ_FALSE);
94     assert(ret);
95     buffer[0] = 2;
96     ret = opj_sparse_array_int32_write(sa, 4, 5, 4 + 1, 5 + 1, buffer, 1, 1,
97                                        OPJ_FALSE);
98     assert(ret);
99
100     buffer[0] = 0;
101     buffer[1] = 0xFF;
102     ret = opj_sparse_array_int32_read(sa, 4, 5, 4 + 1, 5 + 1, buffer, 1, 1,
103                                       OPJ_FALSE);
104     assert(ret);
105     assert(buffer[0] == 2);
106     assert(buffer[1] == 0xFF);
107
108     w = 15 + 1;
109     h = 17 + 1;
110     memset(buffer, 0xFF, sizeof(buffer));
111     ret = opj_sparse_array_int32_read(sa, 2, 1, 2 + w, 1 + h, buffer, 1, w,
112                                       OPJ_FALSE);
113     assert(ret);
114     for (j = 0; j < h; j++) {
115         for (i = 0; i < w; i++) {
116             if (i == 4 - 2 && j == 5 - 1) {
117                 assert(buffer[ j * w + i ] == 2);
118             } else {
119                 assert(buffer[ j * w + i ] == 0);
120             }
121         }
122     }
123
124     opj_sparse_array_int32_free(sa);
125
126
127     sa = opj_sparse_array_int32_create(99, 101, 15, 17);
128     memset(buffer, 0xFF, sizeof(buffer));
129     ret = opj_sparse_array_int32_read(sa, 0, 0, 2, 1, buffer, 2, 4, OPJ_FALSE);
130     assert(ret);
131     assert(buffer[0] == 0);
132     assert(buffer[1] == -1);
133     assert(buffer[2] == 0);
134
135     buffer[0] = 1;
136     buffer[2] = 3;
137     ret = opj_sparse_array_int32_write(sa, 0, 0, 2, 1, buffer, 2, 4, OPJ_FALSE);
138     assert(ret);
139
140     memset(buffer, 0xFF, sizeof(buffer));
141     ret = opj_sparse_array_int32_read(sa, 0, 0, 2, 1, buffer, 2, 4, OPJ_FALSE);
142     assert(ret);
143     assert(buffer[0] == 1);
144     assert(buffer[1] == -1);
145     assert(buffer[2] == 3);
146
147     opj_sparse_array_int32_free(sa);
148
149     return 0;
150 }