CompressionSize.c 13.2 KB
Newer Older
Andy Cedilnik's avatar
Andy Cedilnik committed
1 2
/* -*- c -*- *****************************************************************
** Copyright (C) 2003 Sandia Corporation
3 4 5 6
** 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.
Andy Cedilnik's avatar
Andy Cedilnik committed
7 8 9 10 11 12
**
** This test checks to make sure the size of compressed images never
** exceeds the advertised maximum buffer size.
*****************************************************************************/

#include "test_codes.h"
13
#include "test_util.h"
Andy Cedilnik's avatar
Andy Cedilnik committed
14

15 16
#include <IceTDevImage.h>
#include <IceTDevState.h>
Andy Cedilnik's avatar
Andy Cedilnik committed
17 18 19

#include <stdlib.h>
#include <stdio.h>
20
#include <time.h>
Andy Cedilnik's avatar
Andy Cedilnik committed
21

22 23 24 25 26 27
static IceTDouble IdentityMatrix[16] = {
    1.0, 0.0, 0.0, 0.0,
    0.0, 1.0, 0.0, 0.0,
    0.0, 0.0, 1.0, 0.0,
    0.0, 0.0, 0.0, 1.0
};
28
static IceTFloat Black[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
Andy Cedilnik's avatar
Andy Cedilnik committed
29

30 31 32 33 34 35 36
static void InitPathologicalImage(IceTImage image)
{
  /* Create a worst case possible for image with respect to compression.
     Every other pixel is active so the run lengths are all 1. */
    IceTEnum format;
    IceTSizeType num_pixels;

37
    num_pixels = icetImageGetNumPixels(image);
38 39 40

    format = icetImageGetColorFormat(image);
    if (format == ICET_IMAGE_COLOR_RGBA_UBYTE) {
41
        IceTUByte *buffer = icetImageGetColorub(image);
42 43 44 45 46 47 48 49
        IceTSizeType i;
        for (i = 0; i < num_pixels; i++) {
            buffer[4*i + 0] = 255*(IceTUByte)(i%2);
            buffer[4*i + 1] = 255*(IceTUByte)(i%2);
            buffer[4*i + 2] = 255*(IceTUByte)(i%2);
            buffer[4*i + 3] = 255*(IceTUByte)(i%2);
        }
    } else if (format == ICET_IMAGE_COLOR_RGBA_FLOAT) {
50
        IceTFloat *buffer = icetImageGetColorf(image);
51 52 53 54 55 56 57 58
        IceTSizeType i;
        for (i = 0; i < num_pixels; i++) {
            buffer[4*i + 0] = (IceTFloat)(i%2);
            buffer[4*i + 1] = (IceTFloat)(i%2);
            buffer[4*i + 2] = (IceTFloat)(i%2);
            buffer[4*i + 3] = (IceTFloat)(i%2);
        }
    } else if (format != ICET_IMAGE_COLOR_NONE) {
59
        printrank("*** Unknown color format? ***\n");
60 61 62 63
    }

    format = icetImageGetDepthFormat(image);
    if (format == ICET_IMAGE_DEPTH_FLOAT) {
64
        IceTFloat *buffer = icetImageGetDepthf(image);
65 66 67 68 69
        IceTSizeType i;
        for (i = 0; i < num_pixels; i++) {
            buffer[i] = (IceTFloat)(i%2);
        }
    } else if (format != ICET_IMAGE_DEPTH_NONE) {
70
        printrank("*** Unknown depth format? ***\n");
71 72 73 74 75 76 77 78 79 80 81
    }
}

static void InitActiveImage(IceTImage image)
{
  /* Create a worst case possible for image with respect to compression.
     All the pixels are active, so no data can be removed. */
    IceTEnum format;
    IceTSizeType num_pixels;
    int seed;

82
    seed = (int)time(NULL);
83 84
    srand(seed);

85
    num_pixels = icetImageGetNumPixels(image);
86 87 88

    format = icetImageGetColorFormat(image);
    if (format == ICET_IMAGE_COLOR_RGBA_UBYTE) {
89
        IceTUByte *buffer = icetImageGetColorub(image);
90 91 92 93 94 95 96 97
        IceTSizeType i;
        for (i = 0; i < num_pixels; i++) {
            buffer[4*i + 0] = (IceTUByte)(rand()%255 + 1);
            buffer[4*i + 1] = (IceTUByte)(rand()%255 + 1);
            buffer[4*i + 2] = (IceTUByte)(rand()%255 + 1);
            buffer[4*i + 3] = (IceTUByte)(rand()%255 + 1);
        }
    } else if (format == ICET_IMAGE_COLOR_RGBA_FLOAT) {
98
        IceTFloat *buffer = icetImageGetColorf(image);
99 100 101 102 103 104 105 106
        IceTSizeType i;
        for (i = 0; i < num_pixels; i++) {
            buffer[4*i + 0] = ((IceTFloat)(rand()%255 + 1))/255;
            buffer[4*i + 1] = ((IceTFloat)(rand()%255 + 1))/255;
            buffer[4*i + 2] = ((IceTFloat)(rand()%255 + 1))/255;
            buffer[4*i + 3] = ((IceTFloat)(rand()%255 + 1))/255;
        }
    } else if (format != ICET_IMAGE_COLOR_NONE) {
107
        printrank("*** Unknown color format? ***\n");
108 109 110 111
    }

    format = icetImageGetDepthFormat(image);
    if (format == ICET_IMAGE_DEPTH_FLOAT) {
112
        IceTFloat *buffer = icetImageGetDepthf(image);
113 114 115 116 117
        IceTSizeType i;
        for (i = 0; i < num_pixels; i++) {
            buffer[i] = ((IceTFloat)(rand()%255))/255;
        }
    } else if (format != ICET_IMAGE_DEPTH_NONE) {
118
        printrank("*** Unknown depth format? ***\n");
119 120 121
    }
}

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
static void drawCallback(const IceTDouble *projection_matrix,
                         const IceTDouble *modelview_matrix,
                         const IceTFloat *background_color,
                         const IceTInt *readback_viewport,
                         IceTImage result)
{
  /* Don't care about this information. */
    (void)projection_matrix;
    (void)modelview_matrix;
    (void)background_color;
    (void)readback_viewport;

    InitActiveImage(result);
}

137 138
static int DoCompressionTest(IceTEnum color_format, IceTEnum depth_format,
                             IceTEnum composite_mode)
Andy Cedilnik's avatar
Andy Cedilnik committed
139
{
140
    IceTInt viewport[4];
141
    IceTSizeType pixels;
142 143 144 145 146
    IceTImage image;
    IceTVoid *imagebuffer;
    IceTSizeType imagesize;
    IceTSparseImage compressedimage;
    IceTVoid *compressedbuffer;
147 148
    IceTSparseImage interlacedimage;
    IceTVoid *interlacedbuffer;
149 150 151 152 153
    IceTSizeType compressedsize;
    IceTSizeType color_pixel_size;
    IceTSizeType depth_pixel_size;
    IceTSizeType pixel_size;
    IceTSizeType size;
Andy Cedilnik's avatar
Andy Cedilnik committed
154 155 156 157
    int result;

    result = TEST_PASSED;

158 159 160
    printstat("Using color format of 0x%x\n", (int)color_format);
    printstat("Using depth format of 0x%x\n", (int)depth_format);
    printstat("Using composite mode of 0x%x\n", (int)composite_mode);
161

162 163
    icetSetColorFormat(color_format);
    icetSetDepthFormat(depth_format);
164
    icetCompositeMode(composite_mode);
Andy Cedilnik's avatar
Andy Cedilnik committed
165

166
    pixels = SCREEN_WIDTH*SCREEN_HEIGHT;
Andy Cedilnik's avatar
Andy Cedilnik committed
167

168 169
    printstat("Allocating memory for %dx%d pixel image.\n",
              (int)SCREEN_WIDTH, (int)SCREEN_HEIGHT);
170
    imagesize = icetImageBufferSize(SCREEN_WIDTH, SCREEN_HEIGHT);
171
    imagebuffer = malloc(imagesize);
172
    image = icetImageAssignBuffer(imagebuffer, SCREEN_WIDTH, SCREEN_HEIGHT);
173

174
    compressedsize = icetSparseImageBufferSize(SCREEN_WIDTH, SCREEN_HEIGHT);
175
    compressedbuffer = malloc(compressedsize);
176
    compressedimage = icetSparseImageAssignBuffer(compressedbuffer,
177 178
                                                  SCREEN_WIDTH,
                                                  SCREEN_HEIGHT);
179

180 181 182 183 184
    interlacedbuffer = malloc(compressedsize);
    interlacedimage = icetSparseImageAssignBuffer(interlacedbuffer,
                                                  SCREEN_WIDTH,
                                                  SCREEN_HEIGHT);

185 186 187 188 189
  /* Get the number of bytes per pixel.  This is used in checking the
     size of compressed images. */
    icetImageGetColorVoid(image, &color_pixel_size);
    icetImageGetDepthVoid(image, &depth_pixel_size);
    pixel_size = color_pixel_size + depth_pixel_size;
190 191
    printstat("Pixel size: color=%d, depth=%d, total=%d\n",
              (int)color_pixel_size, (int)depth_pixel_size, (int)pixel_size);
192

193 194
    printstat("\nCreating worst possible image"
              " (with respect to compression).\n");
195 196
    InitPathologicalImage(image);

197
    printstat("Compressing image.\n");
198
    icetCompressImage(image, compressedimage);
199
    size = icetSparseImageGetCompressedBufferSize(compressedimage);
200 201 202
    printstat("Expected size: %d.  Actual size: %d\n",
              (int)(pixel_size*(pixels/2) + 2*sizeof(IceTUShort)*(pixels/2)),
              (int)size);
203 204
    if (   (size > compressedsize)
        || (size < pixel_size*(pixels/2)) ) {
205
        printrank("*** Size differs from expected size!\n");
Ken Martin's avatar
Ken Martin committed
206
        result = TEST_FAILED;
Andy Cedilnik's avatar
Andy Cedilnik committed
207 208
    }

209
    printstat("Interlacing image.\n");
210 211 212 213 214
    icetSparseImageInterlace(compressedimage,
                             97,
                             ICET_SI_STRATEGY_BUFFER_0,
                             interlacedimage);
    size = icetSparseImageGetCompressedBufferSize(compressedimage);
215 216 217
    printstat("Expected size: %d.  Actual size: %d\n",
              (int)(pixel_size*(pixels/2) + 2*sizeof(IceTUShort)*(pixels/2)),
              (int)size);
218 219
    if (   (size > compressedsize)
        || (size < pixel_size*(pixels/2)) ) {
220
        printrank("*** Size differs from expected size!\n");
221 222 223
        result = TEST_FAILED;
    }

224
    printstat("\nCreating a different worst possible image.\n");
225
    InitActiveImage(image);
226
    printstat("Compressing image.\n");
227
    icetCompressImage(image, compressedimage);
228
    size = icetSparseImageGetCompressedBufferSize(compressedimage);
229
    printstat("Expected size: %d.  Actual size: %d\n",
230
           (int)compressedsize, (int)size);
231
    if ((size > compressedsize) || (size < pixel_size*pixels)) {
232
        printrank("*** Size differs from expected size!\n");
Ken Martin's avatar
Ken Martin committed
233
        result = TEST_FAILED;
Andy Cedilnik's avatar
Andy Cedilnik committed
234 235
    }

236
    printstat("Interlacing image.\n");
237 238 239 240 241
    icetSparseImageInterlace(compressedimage,
                             97,
                             ICET_SI_STRATEGY_BUFFER_0,
                             interlacedimage);
    size = icetSparseImageGetCompressedBufferSize(compressedimage);
242
    printstat("Expected size: %d.  Actual size: %d\n",
243 244 245 246
           (int)(pixel_size*(pixels/2) + 2*sizeof(IceTUShort)*(pixels/2)),
           (int)size);
    if (   (size > compressedsize)
        || (size < pixel_size*(pixels/2)) ) {
247
        printrank("*** Size differs from expected size!\n");
248 249 250
        result = TEST_FAILED;
    }

251
    printstat("\nCompressing zero size image.\n");
252
    icetImageSetDimensions(image, 0, 0);
253
    icetCompressImage(image, compressedimage);
254
    size = icetSparseImageGetCompressedBufferSize(compressedimage);
255
    printstat("Expected size: %d.  Actual size: %d\n",
256 257
           (int)icetSparseImageBufferSize(0, 0), (int)size);
    if (size > icetSparseImageBufferSize(0, 0)) {
258
        printrank("*** Size differs from expected size!\n");
Ken Martin's avatar
Ken Martin committed
259
        result = TEST_FAILED;
Andy Cedilnik's avatar
Andy Cedilnik committed
260 261 262 263 264 265 266 267
    }

  /* This test can be a little volatile.  The icetGetCompressedTileImage
   * expects certain things to be set correctly by the icetDrawFrame
   * function.  Since we want to call icetGetCompressedTileImage directly,
   * we try to set up these parameters by hand.  It is possible for this
   * test to incorrectly fail if the two functions are mutually changed and
   * this scaffolding is not updated correctly. */
268
    printstat("\nSetup for actual render.\n");
Andy Cedilnik's avatar
Andy Cedilnik committed
269
    icetResetTiles();
270
    icetAddTile(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
271
    icetDrawCallback(drawCallback);
Andy Cedilnik's avatar
Andy Cedilnik committed
272
  /* Do a perfunctory draw to set other state variables. */
273
    icetDrawFrame(IdentityMatrix, IdentityMatrix, Black);
274
    viewport[0] = viewport[1] = 0;
275
    viewport[2] = (IceTInt)SCREEN_WIDTH;  viewport[3] = (IceTInt)SCREEN_HEIGHT;
Andy Cedilnik's avatar
Andy Cedilnik committed
276
    icetStateSetIntegerv(ICET_CONTAINED_VIEWPORT, 4, viewport);
277
    printstat("Now render and get compressed image.\n");
278
    icetGetCompressedTileImage(0, compressedimage);
279
    size = icetSparseImageGetCompressedBufferSize(compressedimage);
280 281
    printstat("Expected size: %d.  Actual size: %d\n",
              (int)compressedsize, (int)size);
282
    if ((size > compressedsize) || (size < pixel_size*pixels)) {
283
        printrank("*** Size differs from expected size!\n");
Ken Martin's avatar
Ken Martin committed
284
        result = TEST_FAILED;
Andy Cedilnik's avatar
Andy Cedilnik committed
285 286
    }

287
    printstat("Cleaning up.\n");
Andy Cedilnik's avatar
Andy Cedilnik committed
288 289
    free(imagebuffer);
    free(compressedbuffer);
290
    free(interlacedbuffer);
Andy Cedilnik's avatar
Andy Cedilnik committed
291 292 293
    return result;
}

294
static int CompressionSizeRun()
Andy Cedilnik's avatar
Andy Cedilnik committed
295 296 297 298 299
{
    int result;

    icetStrategy(ICET_STRATEGY_REDUCE);

300
    printstat("Compress depth only.\n");
301 302 303
    result = DoCompressionTest(ICET_IMAGE_COLOR_NONE, ICET_IMAGE_DEPTH_FLOAT,
                               ICET_COMPOSITE_MODE_Z_BUFFER);

304
    printstat("\n\nCompress 8-bit color only.\n");
305 306 307 308 309 310 311 312 313 314
    if (result == TEST_PASSED) {
        result = DoCompressionTest(ICET_IMAGE_COLOR_RGBA_UBYTE,
                                   ICET_IMAGE_DEPTH_NONE,
                                   ICET_COMPOSITE_MODE_BLEND);
    } else {
        DoCompressionTest(ICET_IMAGE_COLOR_RGBA_UBYTE,
                          ICET_IMAGE_DEPTH_NONE,
                          ICET_COMPOSITE_MODE_BLEND);
    }

315
    printstat("\n\nCompress 32-bit color only.\n");
Andy Cedilnik's avatar
Andy Cedilnik committed
316
    if (result == TEST_PASSED) {
317 318 319
        result = DoCompressionTest(ICET_IMAGE_COLOR_RGBA_FLOAT,
                                   ICET_IMAGE_DEPTH_NONE,
                                   ICET_COMPOSITE_MODE_BLEND);
Andy Cedilnik's avatar
Andy Cedilnik committed
320
    } else {
321 322 323
        DoCompressionTest(ICET_IMAGE_COLOR_RGBA_FLOAT,
                          ICET_IMAGE_DEPTH_NONE,
                          ICET_COMPOSITE_MODE_BLEND);
Andy Cedilnik's avatar
Andy Cedilnik committed
324
    }
325

326
    printstat("\n\nCompress depth and 8-bit color.\n");
327 328 329
    if (result == TEST_PASSED) {
        result = DoCompressionTest(ICET_IMAGE_COLOR_RGBA_UBYTE,
                                   ICET_IMAGE_DEPTH_FLOAT,
330
                                   ICET_COMPOSITE_MODE_Z_BUFFER);
331 332 333
    } else {
        DoCompressionTest(ICET_IMAGE_COLOR_RGBA_UBYTE,
                          ICET_IMAGE_DEPTH_FLOAT,
334
                          ICET_COMPOSITE_MODE_Z_BUFFER);
335 336
    }

337
    printstat("\n\nCompress depth and 32-bit color.\n");
Andy Cedilnik's avatar
Andy Cedilnik committed
338
    if (result == TEST_PASSED) {
339 340
        result = DoCompressionTest(ICET_IMAGE_COLOR_RGBA_FLOAT,
                                   ICET_IMAGE_DEPTH_FLOAT,
341
                                   ICET_COMPOSITE_MODE_Z_BUFFER);
Andy Cedilnik's avatar
Andy Cedilnik committed
342
    } else {
343 344
        DoCompressionTest(ICET_IMAGE_COLOR_RGBA_FLOAT,
                          ICET_IMAGE_DEPTH_FLOAT,
345
                          ICET_COMPOSITE_MODE_Z_BUFFER);
Andy Cedilnik's avatar
Andy Cedilnik committed
346 347 348 349
    }

    return result;
}
350 351 352 353 354 355 356

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

357
    return run_test(CompressionSizeRun);
358
}