xrif
A fast lossless compression system
xrif_difference_previous.c
Go to the documentation of this file.
1 /** \file xrif_difference_previous.c
2  * \brief Implementation of xrif previous 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_previous_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 = (handle->frames - 2 - n) * npix*handle->depth;
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_previous_sint16
131 
132 xrif_error_t xrif_difference_previous_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 = (handle->frames - 2 - n) * npix*handle->depth;
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_previous_sint32
172 
173 xrif_error_t xrif_difference_previous_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 = (handle->frames - 2 - n) * npix*handle->depth;
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_previous_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_previous", "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_previous", "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_previous", "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_previous_sint16(handle);
239  }
240  else if(handle->type_code == XRIF_TYPECODE_INT32 || handle->type_code == XRIF_TYPECODE_UINT32)
241  {
242  return xrif_difference_previous_sint32(handle);
243  }
244  else if(handle->type_code == XRIF_TYPECODE_INT64 || handle->type_code == XRIF_TYPECODE_UINT64)
245  {
246  return xrif_difference_previous_sint64(handle);
247  }
248  else
249  {
250  XRIF_ERROR_PRINT("xrif_difference_previous", "previous differencing not implemented for type");
251  return XRIF_ERROR_NOTIMPL;
252  }
253 } //xrif_difference_previous
254 
255 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
256 // undifferencing
257 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
258 
259 xrif_error_t xrif_undifference_previous_sint16( xrif_t handle )
260 {
261  size_t npix = handle->width*handle->height;
262 
263  int16_t * rb = (int16_t *) handle->raw_buffer;
264 
265  for(int n=1; n < handle->frames; ++n)
266  {
267  size_t n_stride0 = (n-1) * npix*handle->depth;
268  size_t n_stride = n * npix*handle->depth;
269 
270  for(int kk=0; kk<handle->depth; ++kk)
271  {
272  size_t kk_stride = kk*npix;
273 
274  int16_t * rb0 = &rb[n_stride0 + kk_stride];
275  int16_t * rb1 = &rb[n_stride + kk_stride];
276 
277  #ifndef XRIF_NO_OMP
278  #pragma omp parallel if (handle->omp_parallel > 0)
279  {
280  #endif
281 
282  #ifndef XRIF_NO_OMP
283  #pragma omp for
284  #endif
285 
286  for(int qq=0; qq< handle->width*handle->height; ++qq)
287  {
288  rb1[qq] = rb1[qq] + rb0[qq];
289  }
290 
291  #ifndef XRIF_NO_OMP
292  }
293  #endif
294  }
295  }
296 
297  return XRIF_NOERROR;
298 }//xrif_undifference_previous_sint16
299 
300 xrif_error_t xrif_undifference_previous_sint32( xrif_t handle )
301 {
302  size_t npix = handle->width*handle->height;
303 
304  int32_t * rb = (int32_t *) handle->raw_buffer;
305 
306  for(int n=1; n < handle->frames; ++n)
307  {
308  size_t n_stride0 = (n-1) * npix*handle->depth;
309  size_t n_stride = n * npix*handle->depth;
310 
311  for(int kk=0; kk<handle->depth; ++kk)
312  {
313  size_t kk_stride = kk*npix;
314 
315  int32_t * rb0 = &rb[n_stride0 + kk_stride];
316  int32_t * rb1 = &rb[n_stride + kk_stride];
317 
318  #ifndef XRIF_NO_OMP
319  #pragma omp parallel if (handle->omp_parallel > 0)
320  {
321  #endif
322 
323  #ifndef XRIF_NO_OMP
324  #pragma omp for
325  #endif
326 
327  for(int qq=0; qq< handle->width*handle->height; ++qq)
328  {
329  rb1[qq] = rb1[qq] + rb0[qq];
330  }
331 
332  #ifndef XRIF_NO_OMP
333  }
334  #endif
335  }
336  }
337 
338  return XRIF_NOERROR;
339 } //xrif_undifference_previous_sint32
340 
341 xrif_error_t xrif_undifference_previous_sint64( xrif_t handle )
342 {
343  size_t npix = handle->width*handle->height;
344 
345  int64_t * rb = (int64_t *) handle->raw_buffer;
346 
347  for(int n=1; n < handle->frames; ++n)
348  {
349  size_t n_stride0 = (n-1) * npix*handle->depth;
350  size_t n_stride = n * npix*handle->depth;
351 
352  for(int kk=0; kk<handle->depth; ++kk)
353  {
354  size_t kk_stride = kk*npix;
355 
356  int64_t * rb0 = &rb[n_stride0 + kk_stride];
357  int64_t * rb1 = &rb[n_stride + kk_stride];
358 
359  #ifndef XRIF_NO_OMP
360  #pragma omp parallel if (handle->omp_parallel > 0)
361  {
362  #endif
363 
364  #ifndef XRIF_NO_OMP
365  #pragma omp for
366  #endif
367 
368  for(int qq=0; qq< npix; ++qq)
369  {
370  rb1[qq] = rb1[qq] + rb0[qq];
371  }
372 
373  #ifndef XRIF_NO_OMP
374  }
375  #endif
376  }
377  }
378 
379  return XRIF_NOERROR;
380 }//xrif_undifference_previous_sint64
381 
382 
383 //Dispatch undifferencing w.r.t. previous according to type
385 {
386  if( handle == NULL)
387  {
388  XRIF_ERROR_PRINT("xrif_undifference_previous", "can not use a null pointer");
389  return XRIF_ERROR_NULLPTR;
390  }
391 
392  if( handle->raw_buffer == NULL || handle->width*handle->height*handle->depth*handle->frames == 0 || handle->type_code == 0)
393  {
394  XRIF_ERROR_PRINT("xrif_undifference_previous", "handle not set up");
395  return XRIF_ERROR_NOT_SETUP;
396  }
397 
398  if(handle->raw_buffer_size < handle->width*handle->height*handle->depth*handle->frames)
399  {
400  XRIF_ERROR_PRINT("xrif_undifference_previous", "raw buffer size not sufficient");
402  }
403 
404  if(handle->type_code == XRIF_TYPECODE_INT16 || handle->type_code == XRIF_TYPECODE_UINT16)
405  {
406  return xrif_undifference_previous_sint16(handle);
407  }
408  else if(handle->type_code == XRIF_TYPECODE_INT32 || handle->type_code == XRIF_TYPECODE_UINT32)
409  {
410  return xrif_undifference_previous_sint32(handle);
411  }
412  else if(handle->type_code == XRIF_TYPECODE_INT64 || handle->type_code == XRIF_TYPECODE_UINT64)
413  {
414  return xrif_undifference_previous_sint64(handle);
415  }
416  else
417  {
418  XRIF_ERROR_PRINT("xrif_difference_previous", "previous undifferencing not implemented for type");
419  return XRIF_ERROR_NULLPTR;
420  }
421 } //xrif_undifference_previous
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_difference_previous
xrif_error_t xrif_difference_previous(xrif_t handle)
Difference the images using the previous image as a reference.
Definition: xrif_difference_previous.c:216
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_undifference_previous
xrif_error_t xrif_undifference_previous(xrif_t handle)
Undifference the images using the previous image as a reference.
Definition: xrif_difference_previous.c:384
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.