xrif
A fast lossless compression system
xrif_test_difference_first_whitenoise.c
Go to the documentation of this file.
1 /** \file xrif_test_difference_first_whitenoise.c
2  * \brief Test the first differencing method with white noise.
3  *
4  * \author Jared R. Males (jaredmales@gmail.com)
5  *
6  * \ingroup xrif_test_files
7  */
8 
9 /* This file is part of the xrif library.
10 
11 Copyright (c) 2019, 2020, 2021 The Arizona Board of Regents on behalf of The
12 University of Arizona
13 
14 All rights reserved.
15 
16 Developed by: The Arizona Board of Regents on behalf of the
17 University of Arizona.
18 
19 Redistribution and use for noncommercial purposes in source and
20 binary forms, with or without modification, are permitted provided
21 that the following conditions are met:
22 
23 1. The software is used solely for noncommercial purposes.
24 
25 2. Redistributions of source code must retain the above copyright
26 notice, terms and conditions specified herein and the disclaimer
27 specified in Section 4 below.
28 
29 3. Redistributions in binary form must reproduce the above
30 copyright notice, this list of conditions and the following
31 disclaimer in the documentation and/or other materials provided
32 with the distribution.
33 
34 4. Neither the name of the Arizona Board of Regents, the University
35 of Arizona nor the names of other contributors may be used to
36 endorse or promote products derived from this software without
37 specific prior written permission.
38 
39 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
40 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
41 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
42 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
43 COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
44 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
45 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
46 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 OF THE POSSIBILITY OF SUCH DAMAGE.
51 
52 Arizona Required Clauses:
53 
54 1.1. Arbitration. The parties agree that if a dispute arises
55 between them concerning this Agreement, the parties may be required
56 to submit the matter to arbitration pursuant to Arizona law.
57 
58 1.2. Applicable Law and Venue. This Agreement shall be interpreted
59 pursuant to the laws of the State of Arizona. Any arbitration or
60 litigation between the Parties shall be conducted in Pima County,
61 ARIZONA, and LICENSEE hereby submits to venue and jurisdiction in
62 Pima County, ARIZONA.
63 
64 1.3. Non-Discrimination. The Parties agree to be bound by state and
65 federal laws and regulations governing equal opportunity and non-
66 discrimination and immigration.
67 
68 1.4. Appropriation of Funds. The Parties recognize that performance
69 by ARIZONA may depend upon appropriation of funds by the State
70 Legislature of ARIZONA. If the Legislature fails to appropriate the
71 necessary funds, or if ARIZONA’S appropriation is reduced during
72 the fiscal year, ARIZONA may cancel this Agreement without further
73 duty or obligation. ARIZONA will notify LICENSEE as soon as
74 reasonably possible after it knows of the loss of funds.
75 
76 1.5. Conflict of Interest. This Agreement is subject to the
77 provisions of A.R.S. 38-511 and other conflict of interest
78 regulations. Within three years of the EFFECTIVE DATE, ARIZONA may
79 cancel this Agreement if any person significantly involved in
80 initiating, negotiating, drafting, securing, or creating this
81 Agreement for or on behalf of ARIZONA becomes an employee or
82 consultant in any capacity of LICENSEE with respect to the subject
83 matter of this Agreement.
84 
85 */
86 
87 #include <check.h>
88 #include <stdlib.h>
89 #include <stdio.h>
90 #include <time.h>
91 #include <limits.h>
92 
93 #include "../src/xrif.h"
94 
95 #include "randutils.h"
96 
97 #ifndef XRIF_TEST_TRIALS
98  #define XRIF_TEST_TRIALS (2)
99 #endif
100 
101 int test_trials;
102 
103 /************************************************************/
104 /* Fuzz testing differencing with the first method
105 /************************************************************/
106 
107 
108 int ws[] = {2,4,8,21, 33, 47, 64}; //widths of images
109 int hs[] = {2,4,8,21, 33, 47, 64}; //heights of images
110 int ps[] = {1,2,4,5,27,63,64}; //planes of the cube
111 
112 
113 /** Verify first differencing for int16_t
114  * Verify that xrif difference/un-difference cycle using the first image works with white noise for int16_t.
115  * \anchor diff_first_int16_white
116  */
117 START_TEST (diff_first_int16_white)
118 {
119  fprintf(stderr, "Testing first differencing for signed 16-bit white noise.\n");
120 
121  xrif_t hand = NULL;
122 
123  xrif_error_t rv = xrif_new(&hand);
124 
125  ck_assert( hand != NULL);
126  ck_assert( rv == XRIF_NOERROR );
127 
128  for(int q=0; q < test_trials; ++q)
129  {
130  for(int w =0; w < sizeof(ws)/sizeof(ws[0]); ++w)
131  {
132  for(int h=0; h < sizeof(hs)/sizeof(hs[0]); ++h)
133  {
134  for(int p=0; p< sizeof(ps)/sizeof(ps[0]); ++p)
135  {
136 
137  rv = xrif_set_size(hand, ws[w], hs[h], 1, ps[p], XRIF_TYPECODE_INT16);
138  ck_assert( rv == XRIF_NOERROR );
139 
140  rv = xrif_allocate_raw(hand);
141  ck_assert( rv == XRIF_NOERROR );
142 
143  rv = xrif_allocate_reordered(hand);
144  ck_assert( rv == XRIF_NOERROR );
145 
146  int16_t * buffer = (int16_t *) hand->raw_buffer;
147  rv = fill_int16_white( buffer, hand->width*hand->height*hand->frames, q);
148  ck_assert( rv == 0 );
149 
150  int16_t * compbuff = (int16_t *) malloc( hand->width*hand->height*hand->frames*sizeof(int16_t));
151  memcpy(compbuff, buffer, hand->width*hand->height*hand->frames*sizeof(int16_t));
152 
153  xrif_difference_first(hand);
155 
156  int neq = 0;
157  for( size_t i = 0 ; i < hand->width*hand->height*hand->frames ; ++i )
158  {
159  if(buffer[i] != compbuff[i])
160  {
161  ++neq;
162  }
163  }
164  ck_assert( neq == 0 );
165 
166  free(compbuff);
167  xrif_reset(hand);
168  }//p
169  }//h
170  }//w
171  }//q
172 
173  rv = xrif_delete(hand);
174  ck_assert( rv == XRIF_NOERROR );
175 }
176 END_TEST;
177 
178 /** Verify first differencing for uint16_t
179  * Verify that xrif difference/un-difference cycle using the first image works with white noise for uint16_t.
180  * \anchor diff_first_uint16_white
181  */
182 START_TEST (diff_first_uint16_white)
183 {
184  fprintf(stderr, "Testing first differencing for unsigned 16-bit white noise.\n");
185 
186  xrif_t hand = NULL;
187 
188  xrif_error_t rv = xrif_new(&hand);
189 
190  ck_assert( hand != NULL);
191  ck_assert( rv == XRIF_NOERROR );
192 
193  for(int q=0; q < test_trials; ++q)
194  {
195  for(int w =0; w < sizeof(ws)/sizeof(ws[0]); ++w)
196  {
197  for(int h=0; h < sizeof(hs)/sizeof(hs[0]); ++h)
198  {
199  for(int p=0; p< sizeof(ps)/sizeof(ps[0]); ++p)
200  {
201 
202  rv = xrif_set_size(hand, ws[w], hs[h], 1, ps[p], XRIF_TYPECODE_UINT16);
203  ck_assert( rv == XRIF_NOERROR );
204 
205  rv = xrif_allocate_raw(hand);
206  ck_assert( rv == XRIF_NOERROR );
207 
208  rv = xrif_allocate_reordered(hand);
209  ck_assert( rv == XRIF_NOERROR );
210 
211  uint16_t * buffer = (uint16_t *) hand->raw_buffer;
212  rv = fill_uint16_white( buffer, hand->width*hand->height*hand->frames,q);
213  ck_assert( rv == 0 );
214 
215  uint16_t * compbuff = (uint16_t *) malloc( hand->width*hand->height*hand->frames*sizeof(uint16_t));
216  memcpy(compbuff, buffer, hand->width*hand->height*hand->frames*sizeof(uint16_t));
217 
218  xrif_difference_first(hand);
220 
221  int neq = 0;
222  for( size_t i = 0 ; i < hand->width*hand->height*hand->frames ; ++i )
223  {
224  if(buffer[i] != compbuff[i])
225  {
226  ++neq;
227  }
228  }
229  ck_assert( neq == 0 );
230 
231  free(compbuff);
232  xrif_reset(hand);
233  }//p
234  }//h
235  }//w
236  }//q
237 
238  rv = xrif_delete(hand);
239  ck_assert( rv == XRIF_NOERROR );
240 }
241 END_TEST;
242 
243 /** Verify first differencing for int32_t
244  * Verify that xrif difference/un-difference cycle using the first image works with white noise for int32_t.
245  * \anchor diff_first_int32_white
246  */
247 START_TEST (diff_first_int32_white)
248 {
249  fprintf(stderr, "Testing first differencing for signed 32-bit white noise.\n");
250 
251  xrif_t hand = NULL;
252 
253  xrif_error_t rv = xrif_new(&hand);
254 
255  ck_assert( hand != NULL);
256  ck_assert( rv == XRIF_NOERROR );
257 
258  for(int q=0; q < test_trials; ++q)
259  {
260  for(int w =0; w < sizeof(ws)/sizeof(ws[0]); ++w)
261  {
262  for(int h=0; h < sizeof(hs)/sizeof(hs[0]); ++h)
263  {
264  for(int p=0; p< sizeof(ps)/sizeof(ps[0]); ++p)
265  {
266 
267  rv = xrif_set_size(hand, ws[w], hs[h], 1, ps[p], XRIF_TYPECODE_INT32);
268  ck_assert( rv == XRIF_NOERROR );
269 
270  rv = xrif_allocate_raw(hand);
271  ck_assert( rv == XRIF_NOERROR );
272 
273  rv = xrif_allocate_reordered(hand);
274  ck_assert( rv == XRIF_NOERROR );
275 
276  int32_t * buffer = (int32_t *) hand->raw_buffer;
277  rv = fill_int32_white( buffer, hand->width*hand->height*hand->frames,q);
278  ck_assert( rv == 0 );
279 
280  int32_t * compbuff = (int32_t *) malloc( hand->width*hand->height*hand->frames*sizeof(int32_t));
281  memcpy(compbuff, buffer, hand->width*hand->height*hand->frames*sizeof(int32_t));
282 
283  xrif_difference_first(hand);
285 
286  int neq = 0;
287  for( size_t i = 0 ; i < hand->width*hand->height*hand->frames ; ++i )
288  {
289  if(buffer[i] != compbuff[i])
290  {
291  ++neq;
292  }
293  }
294  ck_assert( neq == 0 );
295 
296  free(compbuff);
297  xrif_reset(hand);
298  }//p
299  }//h
300  }//w
301  }//q
302 
303  rv = xrif_delete(hand);
304  ck_assert( rv == XRIF_NOERROR );
305 }
306 END_TEST;
307 
308 /** Verify first differencing for uint32_t
309  * Verify that xrif difference/un-difference cycle using the first image works with white noise for uint32_t.
310  * \anchor diff_first_uint32_white
311  */
312 START_TEST (diff_first_uint32_white)
313 {
314  fprintf(stderr, "Testing first differencing for unsigned 32-bit white noise.\n");
315 
316  xrif_t hand = NULL;
317 
318  xrif_error_t rv = xrif_new(&hand);
319 
320  ck_assert( hand != NULL);
321  ck_assert( rv == XRIF_NOERROR );
322 
323  for(int q=0; q < test_trials; ++q)
324  {
325  for(int w =0; w < sizeof(ws)/sizeof(ws[0]); ++w)
326  {
327  for(int h=0; h < sizeof(hs)/sizeof(hs[0]); ++h)
328  {
329  for(int p=0; p< sizeof(ps)/sizeof(ps[0]); ++p)
330  {
331 
332  rv = xrif_set_size(hand, ws[w], hs[h], 1, ps[p], XRIF_TYPECODE_UINT32);
333  ck_assert( rv == XRIF_NOERROR );
334 
335  rv = xrif_allocate_raw(hand);
336  ck_assert( rv == XRIF_NOERROR );
337 
338  rv = xrif_allocate_reordered(hand);
339  ck_assert( rv == XRIF_NOERROR );
340 
341  uint32_t * buffer = (uint32_t *) hand->raw_buffer;
342  rv = fill_uint32_white( buffer, hand->width*hand->height*hand->frames,q);
343  ck_assert( rv == 0 );
344 
345  uint32_t * compbuff = (uint32_t *) malloc( hand->width*hand->height*hand->frames*sizeof(uint32_t));
346  memcpy(compbuff, buffer, hand->width*hand->height*hand->frames*sizeof(uint32_t));
347 
348  xrif_difference_first(hand);
350 
351  int neq = 0;
352  for( size_t i = 0 ; i < hand->width*hand->height*hand->frames ; ++i )
353  {
354  if(buffer[i] != compbuff[i])
355  {
356  ++neq;
357  }
358  }
359  ck_assert( neq == 0 );
360 
361  free(compbuff);
362  xrif_reset(hand);
363  }//p
364  }//h
365  }//w
366  }//q
367 
368  rv = xrif_delete(hand);
369  ck_assert( rv == XRIF_NOERROR );
370 }
371 END_TEST;
372 
373 /** Verify first differencing for int64_t
374  * Verify that xrif difference/un-difference cycle using the first image works with white noise for int64_t.
375  * \anchor diff_first_int64_white
376  */
377 START_TEST (diff_first_int64_white)
378 {
379  fprintf(stderr, "Testing first differencing for signed 64-bit white noise.\n");
380 
381  xrif_t hand = NULL;
382 
383  xrif_error_t rv = xrif_new(&hand);
384 
385  ck_assert( hand != NULL);
386  ck_assert( rv == XRIF_NOERROR );
387 
388  for(int q=0; q < test_trials; ++q)
389  {
390  for(int w =0; w < sizeof(ws)/sizeof(ws[0]); ++w)
391  {
392  for(int h=0; h < sizeof(hs)/sizeof(hs[0]); ++h)
393  {
394  for(int p=0; p< sizeof(ps)/sizeof(ps[0]); ++p)
395  {
396 
397  rv = xrif_set_size(hand, ws[w], hs[h], 1, ps[p], XRIF_TYPECODE_INT64);
398  ck_assert( rv == XRIF_NOERROR );
399 
400  rv = xrif_allocate_raw(hand);
401  ck_assert( rv == XRIF_NOERROR );
402 
403  rv = xrif_allocate_reordered(hand);
404  ck_assert( rv == XRIF_NOERROR );
405 
406  int64_t * buffer = (int64_t *) hand->raw_buffer;
407  rv = fill_int64_white( buffer, hand->width*hand->height*hand->frames,q);
408  ck_assert( rv == 0 );
409 
410  int64_t * compbuff = (int64_t *) malloc( hand->width*hand->height*hand->frames*sizeof(int64_t));
411  memcpy(compbuff, buffer, hand->width*hand->height*hand->frames*sizeof(int64_t));
412 
413  xrif_difference_first(hand);
415 
416  int neq = 0;
417  for( size_t i = 0 ; i < hand->width*hand->height*hand->frames ; ++i )
418  {
419  if(buffer[i] != compbuff[i])
420  {
421  ++neq;
422  }
423  }
424  ck_assert( neq == 0 );
425 
426  free(compbuff);
427  xrif_reset(hand);
428  }//p
429  }//h
430  }//w
431  }//q
432 
433  rv = xrif_delete(hand);
434  ck_assert( rv == XRIF_NOERROR );
435 }
436 END_TEST;
437 
438 /** Verify first differencing for uint64_t
439  * Verify that xrif difference/un-difference cycle using the first image works with white noise for uint64_t.
440  * \anchor diff_first_uint64_white
441  */
442 START_TEST (diff_first_uint64_white)
443 {
444  fprintf(stderr, "Testing first differencing for unsigned 64-bit white noise.\n");
445 
446  xrif_t hand = NULL;
447 
448  xrif_error_t rv = xrif_new(&hand);
449 
450  ck_assert( hand != NULL);
451  ck_assert( rv == XRIF_NOERROR );
452 
453  for(int q=0; q < test_trials; ++q)
454  {
455  for(int w =0; w < sizeof(ws)/sizeof(ws[0]); ++w)
456  {
457  for(int h=0; h < sizeof(hs)/sizeof(hs[0]); ++h)
458  {
459  for(int p=0; p< sizeof(ps)/sizeof(ps[0]); ++p)
460  {
461 
462  rv = xrif_set_size(hand, ws[w], hs[h], 1, ps[p], XRIF_TYPECODE_UINT64);
463  ck_assert( rv == XRIF_NOERROR );
464 
465  rv = xrif_allocate_raw(hand);
466  ck_assert( rv == XRIF_NOERROR );
467 
468  rv = xrif_allocate_reordered(hand);
469  ck_assert( rv == XRIF_NOERROR );
470 
471  uint64_t * buffer = (uint64_t *) hand->raw_buffer;
472  rv = fill_uint64_white( buffer, hand->width*hand->height*hand->frames,q);
473  ck_assert( rv == 0 );
474 
475  uint64_t * compbuff = (uint64_t *) malloc( hand->width*hand->height*hand->frames*sizeof(uint64_t));
476  memcpy(compbuff, buffer, hand->width*hand->height*hand->frames*sizeof(uint64_t));
477 
478  xrif_difference_first(hand);
480 
481  int neq = 0;
482  for( size_t i = 0 ; i < hand->width*hand->height*hand->frames ; ++i )
483  {
484  if(buffer[i] != compbuff[i])
485  {
486  ++neq;
487  }
488  }
489  ck_assert( neq == 0 );
490 
491  free(compbuff);
492  xrif_reset(hand);
493  }//p
494  }//h
495  }//w
496  }//q
497 
498  rv = xrif_delete(hand);
499  ck_assert( rv == XRIF_NOERROR );
500 }
501 END_TEST;
502 
503 Suite * whitenoise_suite(void)
504 {
505  Suite *s;
506  TCase *tc_core16, *tc_core32, *tc_core64;
507 
508  s = suite_create("White Noise - Difference First");
509 
510  /* 16-bit Core test case */
511  tc_core16 = tcase_create("16 bit white noise");
512 
513  tcase_set_timeout(tc_core16, 1e9);
514 
515  tcase_add_test(tc_core16, diff_first_int16_white);
516  tcase_add_test(tc_core16, diff_first_uint16_white);
517 
518  suite_add_tcase(s, tc_core16);
519 
520  /* 32-bit Core test case */
521  tc_core32 = tcase_create("32 bit white noise");
522 
523  tcase_set_timeout(tc_core32, 1e9);
524 
525  tcase_add_test(tc_core32, diff_first_int32_white);
526  tcase_add_test(tc_core32, diff_first_uint32_white);
527 
528  suite_add_tcase(s, tc_core32);
529 
530  /* 64-bit Core test case */
531  tc_core64 = tcase_create("64 bit white noise");
532 
533  tcase_set_timeout(tc_core64, 1e9);
534 
535  tcase_add_test(tc_core64, diff_first_int64_white);
536  tcase_add_test(tc_core64, diff_first_uint64_white);
537 
538  suite_add_tcase(s, tc_core64);
539 
540  return s;
541 }
542 
543 int main( int argc,
544  char ** argv
545  )
546 {
547 
548  extern int test_trials;
549 
550  test_trials = XRIF_TEST_TRIALS;
551 
552  if(argc == 2)
553  {
554  test_trials = atoi(argv[1]);
555  }
556 
557  fprintf(stderr, "running %d trials per format\n", test_trials);
558 
559  int number_failed;
560  Suite *s;
561  SRunner *sr;
562 
563  // Intialize the random number sequence
564  srand((unsigned) time(NULL));
565 
566  s = whitenoise_suite();
567  sr = srunner_create(s);
568 
569  srunner_run_all(sr, CK_NORMAL);
570  number_failed = srunner_ntests_failed(sr);
571  srunner_free(sr);
572 
573  return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
574 
575 }
576 
XRIF_TYPECODE_INT32
#define XRIF_TYPECODE_INT32
32-bit signed integer
Definition: xrif.h:221
xrif_handle::width
xrif_dimension_t width
The width of a single image, in pixels.
Definition: xrif.h:308
xrif_undifference_first
xrif_error_t xrif_undifference_first(xrif_t handle)
Undifference the images using the first image as a reference.
Definition: xrif_difference_first.c:386
xrif_allocate_reordered
xrif_error_t xrif_allocate_reordered(xrif_t handle)
Allocate the reordered buffer based on the already set stream dimensions.
Definition: xrif.c:673
XRIF_TYPECODE_INT16
#define XRIF_TYPECODE_INT16
16-bit signed integer
Definition: xrif.h:217
xrif_allocate_raw
xrif_error_t xrif_allocate_raw(xrif_t handle)
Allocate the raw buffer based on the already set stream dimensions.
Definition: xrif.c:584
XRIF_TYPECODE_UINT64
#define XRIF_TYPECODE_UINT64
64-bit unsigned integer
Definition: xrif.h:223
xrif_handle
The xrif library configuration structure, organizing various parameters used by the functions.
Definition: xrif.h:306
xrif_handle::frames
xrif_dimension_t frames
The number of frames in the stream.
Definition: xrif.h:311
xrif_new
xrif_error_t xrif_new(xrif_t *handle_ptr)
Allocate a handle and initialize it.
Definition: xrif.c:107
xrif_handle::height
xrif_dimension_t height
The height of a single image, in pixels.
Definition: xrif.h:309
XRIF_TYPECODE_UINT32
#define XRIF_TYPECODE_UINT32
32-bit unsigned integer
Definition: xrif.h:219
START_TEST
START_TEST(diff_first_int16_white)
Definition: xrif_test_difference_first_whitenoise.c:117
xrif_error_t
int xrif_error_t
The error reporting type.
Definition: xrif.h:157
xrif_set_size
xrif_error_t xrif_set_size(xrif_t handle, xrif_dimension_t w, xrif_dimension_t h, xrif_dimension_t d, xrif_dimension_t f, xrif_typecode_t c)
Set the basic parameters of an xrif handle.
Definition: xrif.c:128
xrif_handle::raw_buffer
char * raw_buffer
The raw buffer pointer, contains the image data, and if compress_on_raw == true the compressed data.
Definition: xrif.h:337
xrif_reset
xrif_error_t xrif_reset(xrif_t handle)
Reset a handle, restoring it to the initialized state. De-allocates owned pointers and re-initializes...
Definition: xrif.c:243
xrif_delete
xrif_error_t xrif_delete(xrif_t handle)
Deallocate a handle, including any memory that it owns.
Definition: xrif.c:277
xrif_difference_first
xrif_error_t xrif_difference_first(xrif_t handle)
Difference the images using the first image as a reference.
Definition: xrif_difference_first.c:216
XRIF_TYPECODE_INT64
#define XRIF_TYPECODE_INT64
64-bit signed integer
Definition: xrif.h:225
XRIF_NOERROR
#define XRIF_NOERROR
Return code for success.
Definition: xrif.h:160
XRIF_TYPECODE_UINT16
#define XRIF_TYPECODE_UINT16
16-bit unsigned integer
Definition: xrif.h:215