File: SparseImageCopy.c

package info (click to toggle)
paraview 5.1.2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 221,108 kB
  • ctags: 236,092
  • sloc: cpp: 2,416,026; ansic: 190,891; python: 99,856; xml: 81,001; tcl: 46,915; yacc: 5,039; java: 4,413; perl: 3,108; sh: 1,974; lex: 1,926; f90: 748; asm: 471; pascal: 228; makefile: 198; objc: 83; fortran: 31
file content (344 lines) | stat: -rw-r--r-- 11,510 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* -*- c -*- *****************************************************************
** Copyright (C) 2011 Sandia Corporation
** Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
** the U.S. Government retains certain rights in this software.
**
** This source code is released under the New BSD License.
**
** This test checks the behavior of various ways to copy blocks of pixels
** in sparse images.
*****************************************************************************/

#include "test_codes.h"
#include "test_util.h"

#include <IceTDevImage.h>

#include <stdlib.h>
#include <stdio.h>

/* Encode image position in color. */
#define ACTIVE_COLOR(x, y) \
    ((((x) & 0xFFFF) | 0x8000) | ((((y) & 0xFFFF) | 0x8000) << 16))

/* Fills the given image to have data in the lower triangle like this:
 *
 * +-------------+
 * |  \          |
 * |    \        |
 * |      \      |
 * |        \    |
 * |          \  |
 * +-------------+
 *
 * Where the lower half is filled with data and the upper half is background. */
static void LowerTriangleImage(IceTImage image)
{
    IceTUInt *data = icetImageGetColorui(image);
    IceTSizeType width = icetImageGetWidth(image);
    IceTSizeType height = icetImageGetHeight(image);
    IceTSizeType x, y;

    for (y = 0; y < height; y++) {
        for (x = 0; x < width; x++) {
            if (x < (height-y)) {
                data[0] = ACTIVE_COLOR(x, y);
            } else {
                data[0] = 0;
            }
            data++;
        }
    }
}

/* Fills the given image to have data in the upper triangle like this:
 *
 * +-------------+
 * |  \          |
 * |    \        |
 * |      \      |
 * |        \    |
 * |          \  |
 * +-------------+
 *
 * Where the upper half is filled with data and the upper half is background. */
static void UpperTriangleImage(IceTImage image)
{
    IceTUInt *data = icetImageGetColorui(image);
    IceTSizeType width = icetImageGetWidth(image);
    IceTSizeType height = icetImageGetHeight(image);
    IceTSizeType x, y;

    for (y = 0; y < height; y++) {
        for (x = 0; x < width; x++) {
            if ((height-y) < x) {
                data[0] = ACTIVE_COLOR(x, y);
            } else {
                data[0] = 0;
            }
            data++;
        }
    }
}

static int CompareSparseImages(const IceTSparseImage image0,
                               const IceTSparseImage image1)
{
    IceTSizeType width;
    IceTSizeType height;
    IceTVoid *image_buffer[2];
    IceTImage image[2];
    IceTUInt *color_buffer[2];
    IceTSizeType i;

    if (   icetSparseImageGetCompressedBufferSize(image0)
        != icetSparseImageGetCompressedBufferSize(image1) ) {
        printrank("Buffer sizes do not match: %d vs %d!\n",
                  icetSparseImageGetCompressedBufferSize(image0),
                  icetSparseImageGetCompressedBufferSize(image1));
        return TEST_FAILED;
    }

    width = icetSparseImageGetWidth(image1);
    height = icetSparseImageGetHeight(image1);

    image_buffer[0] = malloc(icetImageBufferSize(width, height));
    image[0] = icetImageAssignBuffer(image_buffer[0], width, height);
    icetDecompressImage(image0, image[0]);
    color_buffer[0] = icetImageGetColorui(image[0]);

    image_buffer[1] = malloc(icetImageBufferSize(width, height));
    image[1] = icetImageAssignBuffer(image_buffer[1], width, height);
    icetDecompressImage(image1, image[1]);
    color_buffer[1] = icetImageGetColorui(image[1]);

    for (i = 0; i < width*height; i++) {
        if (color_buffer[0][i] != color_buffer[1][i]) {
            printrank("Buffer mismatch at uint %d\n", i);
            printrank("0x%x vs 0x%x\n", color_buffer[0][i], color_buffer[1][i]);
            return TEST_FAILED;
        }
    }

    return TEST_PASSED;
}

static int TrySparseImageCopyPixels(const IceTImage image,
                                    IceTSizeType start,
                                    IceTSizeType end)
{
    IceTVoid *full_sparse_buffer;
    IceTSparseImage full_sparse;
    IceTVoid *compress_sub_buffer;
    IceTSparseImage compress_sub;
    IceTVoid *sparse_copy_buffer;
    IceTSparseImage sparse_copy;

    IceTSizeType width = icetImageGetWidth(image);
    IceTSizeType height = icetImageGetHeight(image);
    IceTSizeType sub_size = end - start;

    int result;

    printstat("Trying sparse image copy from %d to %d\n", start, end);

    full_sparse_buffer = malloc(icetSparseImageBufferSize(width, height));
    full_sparse = icetSparseImageAssignBuffer(full_sparse_buffer,width,height);

    compress_sub_buffer = malloc(icetSparseImageBufferSize(sub_size, 1));
    compress_sub = icetSparseImageAssignBuffer(compress_sub_buffer,
                                               sub_size, 1);

    sparse_copy_buffer = malloc(icetSparseImageBufferSize(sub_size, 1));
    sparse_copy = icetSparseImageAssignBuffer(sparse_copy_buffer,
                                              sub_size, 1);

    icetCompressSubImage(image, start, sub_size, compress_sub);

    icetCompressImage(image, full_sparse);
    icetSparseImageCopyPixels(full_sparse, start, sub_size, sparse_copy);

    result = CompareSparseImages(compress_sub, sparse_copy);

    free(full_sparse_buffer);
    free(compress_sub_buffer);
    free(sparse_copy_buffer);

    return result;
}

static int TestSparseImageCopyPixels(const IceTImage image)
{
#define NUM_OFFSETS 7
    IceTSizeType interesting_offset[NUM_OFFSETS];
    IceTSizeType width = icetImageGetWidth(image);
    IceTSizeType height = icetImageGetHeight(image);
    int start, end;

    if (height <= 20) {
        printstat("Need image height greater than 20.\n");
        return TEST_NOT_RUN;
    }

    interesting_offset[0] = 0;  /* First pixel. */
    interesting_offset[1] = 10*width; /* Some pixels up at left. */
    interesting_offset[2] = 10*width + width/2; /* A bit up in the middle. */
    interesting_offset[3] = width*(height/2) + height/2; /* Middle at triangle diagonal. */
    interesting_offset[4] = width*(height-10); /* Some pixels from top at left. */
    interesting_offset[5] = width*(height-10) + width/2; /* Some pixels from top in middle. */
    interesting_offset[6] = width*height; /* Last pixel. */

    for (start = 0; start < NUM_OFFSETS; start++) {
        for (end = start+1; end < NUM_OFFSETS; end++) {
            int result;
            result = TrySparseImageCopyPixels(image,
                                              interesting_offset[start],
                                              interesting_offset[end]);
            if (result != TEST_PASSED) return result;
        }
    }

    return TEST_PASSED;
#undef NUM_OFFSETS
}

static int TestSparseImageSplit(const IceTImage image)
{
#define NUM_PARTITIONS 7
    IceTVoid *full_sparse_buffer;
    IceTSparseImage full_sparse;
    IceTVoid *sparse_partition_buffer[NUM_PARTITIONS];
    IceTSparseImage sparse_partition[NUM_PARTITIONS];
    IceTSizeType offsets[NUM_PARTITIONS];
    IceTVoid *compare_sparse_buffer;
    IceTSparseImage compare_sparse;

    IceTSizeType width;
    IceTSizeType height;
    IceTSizeType num_partition_pixels;

    IceTInt partition;

    width = icetImageGetWidth(image);
    height = icetImageGetHeight(image);
    num_partition_pixels
        = icetSparseImageSplitPartitionNumPixels(width*height,
                                                 NUM_PARTITIONS,
                                                 NUM_PARTITIONS);

    full_sparse_buffer = malloc(icetSparseImageBufferSize(width, height));
    full_sparse = icetSparseImageAssignBuffer(full_sparse_buffer,width,height);

    for (partition = 0; partition < NUM_PARTITIONS; partition++) {
        sparse_partition_buffer[partition]
            = malloc(icetSparseImageBufferSize(num_partition_pixels, 1));
        sparse_partition[partition]
            = icetSparseImageAssignBuffer(sparse_partition_buffer[partition],
                                          num_partition_pixels, 1);
    }

    compare_sparse_buffer
        = malloc(icetSparseImageBufferSize(num_partition_pixels, 1));
    compare_sparse
        = icetSparseImageAssignBuffer(compare_sparse_buffer,
                                      num_partition_pixels, 1);

    icetCompressImage(image, full_sparse);

    printstat("Spliting image %d times\n", NUM_PARTITIONS);
    icetSparseImageSplit(full_sparse,
                         0,
                         NUM_PARTITIONS,
                         NUM_PARTITIONS,
                         sparse_partition,
                         offsets);
    for (partition = 0; partition < NUM_PARTITIONS; partition++) {
        IceTInt result;
        icetCompressSubImage(image,
                             offsets[partition],
                             icetSparseImageGetNumPixels(
                                                   sparse_partition[partition]),
                             compare_sparse);
        printstat("    Comparing partition %d\n", partition);
        result = CompareSparseImages(compare_sparse,
                                     sparse_partition[partition]);
        if (result != TEST_PASSED) return result;
    }

    printstat("Spliting image %d times with first partition in place.\n",
              NUM_PARTITIONS);
    sparse_partition[0] = full_sparse;
    icetSparseImageSplit(full_sparse,
                         0,
                         NUM_PARTITIONS,
                         NUM_PARTITIONS,
                         sparse_partition,
                         offsets);
    for (partition = 0; partition < NUM_PARTITIONS; partition++) {
        IceTInt result;
        icetCompressSubImage(image,
                             offsets[partition],
                             icetSparseImageGetNumPixels(
                                                   sparse_partition[partition]),
                             compare_sparse);
        printstat("    Comparing partition %d\n", partition);
        result = CompareSparseImages(compare_sparse,
                                     sparse_partition[partition]);
        if (result != TEST_PASSED) return result;
    }

    free(full_sparse_buffer);
    for (partition = 0; partition < NUM_PARTITIONS; partition++) {
        free(sparse_partition_buffer[partition]);
    }
    free(compare_sparse_buffer);

    return TEST_PASSED;
#undef NUM_PARTITIONS
}

static int SparseImageCopyRun()
{
    IceTVoid *imagebuffer;
    IceTImage image;

    icetSetColorFormat(ICET_IMAGE_COLOR_RGBA_UBYTE);
    icetSetDepthFormat(ICET_IMAGE_DEPTH_NONE);
    icetCompositeMode(ICET_COMPOSITE_MODE_BLEND);

    imagebuffer = malloc(icetImageBufferSize(SCREEN_WIDTH, SCREEN_HEIGHT));
    image = icetImageAssignBuffer(imagebuffer, SCREEN_WIDTH, SCREEN_HEIGHT);

    printstat("\n********* Creating lower triangle image\n");
    LowerTriangleImage(image);

    if (TestSparseImageCopyPixels(image) != TEST_PASSED) {
        return TEST_FAILED;
    }
    if (TestSparseImageSplit(image) != TEST_PASSED) {
        return TEST_FAILED;
    }

    printstat("\n********* Creating upper triangle image\n");
    UpperTriangleImage(image);

    if (TestSparseImageCopyPixels(image) != TEST_PASSED) {
        return TEST_FAILED;
    }
    if (TestSparseImageSplit(image) != TEST_PASSED) {
        return TEST_FAILED;
    }

    free(imagebuffer);

    return TEST_PASSED;
}

int SparseImageCopy(int argc, char *argv[])
{
    /* To remove warning */
    (void)argc;
    (void)argv;

    return run_test(SparseImageCopyRun);
}