xrif
A fast lossless compression system
xrif_difference_first.c
Go to the documentation of this file.
1 /** \file xrif_difference_first.c
2  * \brief Implementation of xrif first frame differencing
3  *
4  * \author Jared R. Males (jaredmales@gmail.com)
5  *
6  * \ingroup xrif_files
7  */
8 
9 /* This file is part of the xrif library.
10 
11 Copyright (c) 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 "xrif.h"
88 
89 
90 
91 xrif_error_t xrif_difference_first_sint16( xrif_t handle )
92 {
93  size_t npix = handle->width*handle->height;
94 
95  int16_t * rb = (int16_t *) handle->raw_buffer;
96 
97  for(int n=0; n < handle->frames-1; ++n)
98  {
99  size_t n_stride0 = 0;
100  size_t n_stride = (handle->frames - 1 - n) * npix*handle->depth;
101 
102  for(int kk=0; kk< handle->depth; ++kk)
103  {
104  size_t kk_stride = kk*npix;
105 
106  int16_t * rb0 = &rb[n_stride0 + kk_stride];
107  int16_t * rb1 = &rb[n_stride + kk_stride];
108 
109  #ifndef XRIF_NO_OMP
110  #pragma omp parallel if (handle->omp_parallel > 0)
111  {
112  #endif
113 
114  #ifndef XRIF_NO_OMP
115  #pragma omp for
116  #endif
117 
118  for(int qq=0; qq < npix; ++qq)
119  {
120  rb1[qq] = (rb1[qq] - rb0[qq]);
121  }
122 
123  #ifndef XRIF_NO_OMP
124  }
125  #endif
126  }
127  }
128 
129  return XRIF_NOERROR;
130 } //xrif_difference_first_sint16
131 
132 xrif_error_t xrif_difference_first_sint32( xrif_t handle )
133 {
134  size_t npix = handle->width*handle->height;
135 
136  int32_t * rb = (int32_t *) handle->raw_buffer;
137 
138  for(int n=0; n < handle->frames-1; ++n)
139  {
140  size_t n_stride0 = 0;
141  size_t n_stride = (handle->frames - 1 - n) * npix*handle->depth;
142 
143  for(int kk=0; kk< handle->depth; ++kk)
144  {
145  size_t kk_stride = kk*npix;
146 
147  int32_t * rb0 = &rb[n_stride0 + kk_stride];
148  int32_t * rb1 = &rb[n_stride + kk_stride];
149 
150  #ifndef XRIF_NO_OMP
151  #pragma omp parallel if (handle->omp_parallel > 0)
152  {
153  #endif
154 
155  #ifndef XRIF_NO_OMP
156  #pragma omp for
157  #endif
158 
159  for(int qq=0; qq < npix; ++qq)
160  {
161  rb1[qq] = (rb1[qq] - rb0[qq]);
162  }
163 
164  #ifndef XRIF_NO_OMP
165  }
166  #endif
167  }
168  }
169 
170  return XRIF_NOERROR;
171 } //xrif_difference_first_sint32
172 
173 xrif_error_t xrif_difference_first_sint64( xrif_t handle )
174 {
175  size_t npix = handle->width*handle->height;
176 
177  int64_t * rb = (int64_t *) handle->raw_buffer;
178 
179  for(int n=0; n < handle->frames-1; ++n)
180  {
181  size_t n_stride0 = 0;
182  size_t n_stride = (handle->frames - 1 - n) * npix*handle->depth;
183 
184  for(int kk=0; kk< handle->depth; ++kk)
185  {
186  size_t kk_stride = kk*npix;
187 
188  int64_t * rb0 = &rb[n_stride0 + kk_stride];
189  int64_t * rb1 = &rb[n_stride + kk_stride];
190 
191  #ifndef XRIF_NO_OMP
192  #pragma omp parallel if (handle->omp_parallel > 0)
193  {
194  #endif
195 
196  #ifndef XRIF_NO_OMP
197  #pragma omp for
198  #endif
199 
200  for(int qq=0; qq< npix;++qq)
201  {
202  rb1[qq] = (rb1[qq] - rb0[qq]);
203  }
204 
205  #ifndef XRIF_NO_OMP
206  }
207  #endif
208  }
209  }
210 
211  return XRIF_NOERROR;
212 } //xrif_difference_first_sint64
213 
214 
215 //Dispatch differencing w.r.t. previous according to type
217 {
218  if( handle == NULL)
219  {
220  XRIF_ERROR_PRINT("xrif_difference_first", "can not use a null pointer");
221  return XRIF_ERROR_NULLPTR;
222  }
223 
224  if( handle->raw_buffer == NULL || handle->width*handle->height*handle->depth*handle->frames == 0 || handle->type_code == 0)
225  {
226  XRIF_ERROR_PRINT("xrif_difference_first", "handle not set up");
227  return XRIF_ERROR_NOT_SETUP;
228  }
229 
230  if(handle->raw_buffer_size < handle->width*handle->height*handle->depth*handle->frames)
231  {
232  XRIF_ERROR_PRINT("xrif_difference_first", "raw buffer size not sufficient");
234  }
235 
236  if(handle->type_code == XRIF_TYPECODE_INT16 || handle->type_code == XRIF_TYPECODE_UINT16)
237  {
238  return xrif_difference_first_sint16(handle);
239  }
240  else if(handle->type_code == XRIF_TYPECODE_INT32 || handle->type_code == XRIF_TYPECODE_UINT32)
241  {
242  return xrif_difference_first_sint32(handle);
243  }
244  else if(handle->type_code == XRIF_TYPECODE_INT64 || handle->type_code == XRIF_TYPECODE_UINT64)
245  {
246  return xrif_difference_first_sint64(handle);
247  }
248  else
249  {
250  XRIF_ERROR_PRINT("xrif_difference_first", "previous differencing not implemented for type");
251  return XRIF_ERROR_NOTIMPL;
252  }
253 } //xrif_difference_first
254 
255 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
256 // undifferencing
257 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
258 
259 
260 
261 xrif_error_t xrif_undifference_first_sint16( xrif_t handle )
262 {
263  size_t npix = handle->width*handle->height;
264 
265  int16_t * rb = (int16_t *) handle->raw_buffer;
266 
267  for(int n=1; n < handle->frames; ++n)
268  {
269  size_t n_stride0 = 0;
270  size_t n_stride = n * npix*handle->depth;
271 
272  for(int kk=0; kk<handle->depth; ++kk)
273  {
274  size_t kk_stride = kk*npix;
275 
276  int16_t * rb0 = &rb[n_stride0 + kk_stride];
277  int16_t * rb1 = &rb[n_stride + kk_stride];
278 
279  #ifndef XRIF_NO_OMP
280  #pragma omp parallel if (handle->omp_parallel > 0)
281  {
282  #endif
283 
284  #ifndef XRIF_NO_OMP
285  #pragma omp for
286  #endif
287 
288  for(int qq=0; qq< handle->width*handle->height; ++qq)
289  {
290  rb1[qq] = rb1[qq] + rb0[qq];
291  }
292 
293  #ifndef XRIF_NO_OMP
294  }
295  #endif
296  }
297  }
298 
299  return XRIF_NOERROR;
300 }//xrif_undifference_first_sint16
301 
302 xrif_error_t xrif_undifference_first_sint32( xrif_t handle )
303 {
304  size_t npix = handle->width*handle->height;
305 
306  int32_t * rb = (int32_t *) handle->raw_buffer;
307 
308  for(int n=1; n < handle->frames; ++n)
309  {
310  size_t n_stride0 = 0;
311  size_t n_stride = n * npix*handle->depth;
312 
313  for(int kk=0; kk<handle->depth; ++kk)
314  {
315  size_t kk_stride = kk*npix;
316 
317  int32_t * rb0 = &rb[n_stride0 + kk_stride];
318  int32_t * rb1 = &rb[n_stride + kk_stride];
319 
320  #ifndef XRIF_NO_OMP
321  #pragma omp parallel if (handle->omp_parallel > 0)
322  {
323  #endif
324 
325  #ifndef XRIF_NO_OMP
326  #pragma omp for
327  #endif
328 
329  for(int qq=0; qq< handle->width*handle->height; ++qq)
330  {
331  rb1[qq] = rb1[qq] + rb0[qq];
332  }
333 
334  #ifndef XRIF_NO_OMP
335  }
336  #endif
337  }
338  }
339 
340  return XRIF_NOERROR;
341 } //xrif_undifference_first_sint32
342 
343 xrif_error_t xrif_undifference_first_sint64( xrif_t handle )
344 {
345  size_t npix = handle->width*handle->height;
346 
347  int64_t * rb = (int64_t *) handle->raw_buffer;
348 
349  for(int n=1; n < handle->frames; ++n)
350  {
351  size_t n_stride0 = 0;
352  size_t n_stride = n * npix*handle->depth;
353 
354  for(int kk=0; kk<handle->depth; ++kk)
355  {
356  size_t kk_stride = kk*npix;
357 
358  int64_t * rb0 = &rb[n_stride0 + kk_stride];
359  int64_t * rb1 = &rb[n_stride + kk_stride];
360 
361  #ifndef XRIF_NO_OMP
362  #pragma omp parallel if (handle->omp_parallel > 0)
363  {
364  #endif
365 
366  #ifndef XRIF_NO_OMP
367  #pragma omp for
368  #endif
369 
370  for(int qq=0; qq< npix; ++qq)
371  {
372  rb1[qq] = rb1[qq] + rb0[qq];
373  }
374 
375  #ifndef XRIF_NO_OMP
376  }
377  #endif
378  }
379  }
380 
381  return XRIF_NOERROR;
382 }//xrif_undifference_first_sint64
383 
384 
385 //Dispatch undifferencing w.r.t. previous according to type
387 {
388  if( handle == NULL)
389  {
390  XRIF_ERROR_PRINT("xrif_undifference_first", "can not use a null pointer");
391  return XRIF_ERROR_NULLPTR;
392  }
393 
394  if( handle->raw_buffer == NULL || handle->width*handle->height*handle->depth*handle->frames == 0 || handle->type_code == 0)
395  {
396  XRIF_ERROR_PRINT("xrif_undifference_first", "handle not set up");
397  return XRIF_ERROR_NOT_SETUP;
398  }
399 
400  if(handle->raw_buffer_size < handle->width*handle->height*handle->depth*handle->frames)
401  {
402  XRIF_ERROR_PRINT("xrif_undifference_first", "raw buffer size not sufficient");
404  }
405 
406  if(handle->type_code == XRIF_TYPECODE_INT16 || handle->type_code == XRIF_TYPECODE_UINT16)
407  {
408  return xrif_undifference_first_sint16(handle);
409  }
410  else if(handle->type_code == XRIF_TYPECODE_INT32 || handle->type_code == XRIF_TYPECODE_UINT32)
411  {
412  return xrif_undifference_first_sint32(handle);
413  }
414  else if(handle->type_code == XRIF_TYPECODE_INT64 || handle->type_code == XRIF_TYPECODE_UINT64)
415  {
416  return xrif_undifference_first_sint64(handle);
417  }
418  else
419  {
420  XRIF_ERROR_PRINT("xrif_difference_first", "previous undifferencing not implemented for type");
421  return XRIF_ERROR_NULLPTR;
422  }
423 } //xrif_undifference_first
XRIF_TYPECODE_INT32
#define XRIF_TYPECODE_INT32
32-bit signed integer
Definition: xrif.h:221
XRIF_ERROR_NULLPTR
#define XRIF_ERROR_NULLPTR
Return code indicating that a NULL pointer was passed.
Definition: xrif.h:163
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_TYPECODE_INT16
#define XRIF_TYPECODE_INT16
16-bit signed integer
Definition: xrif.h:217
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_ERROR_PRINT
#define XRIF_ERROR_PRINT(function, msg)
Standard error report.
Definition: xrif.h:196
XRIF_ERROR_NOT_SETUP
#define XRIF_ERROR_NOT_SETUP
Return code indicating that the handle was not setup.
Definition: xrif.h:166
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
xrif_error_t
int xrif_error_t
The error reporting type.
Definition: xrif.h:157
XRIF_ERROR_NOTIMPL
#define XRIF_ERROR_NOTIMPL
Return code indicating that the requested feature is not available.
Definition: xrif.h:181
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_handle::depth
xrif_dimension_t depth
The depth of a single image, in pixels.
Definition: xrif.h:310
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_handle::raw_buffer_size
size_t raw_buffer_size
Definition: xrif.h:338
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
XRIF_ERROR_INSUFFICIENT_SIZE
#define XRIF_ERROR_INSUFFICIENT_SIZE
Return code indicating that an insufficient size was given.
Definition: xrif.h:175
xrif_handle::type_code
xrif_typecode_t type_code
The code specifying the data type of the pixels.
Definition: xrif.h:313
xrif.h
The eXtreme-ao Reordered Image Format: Declarations.