xrif
A fast lossless compression system
xrif_difference_pixel.c
Go to the documentation of this file.
1 /** \file xrif_difference_pixel.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 xrif_error_t xrif_difference_pixel_sint8( xrif_t handle )
90 {
91  size_t npix = handle->width*handle->height;
92 
93  for(size_t n=0; n < handle->frames; ++n)
94  {
95  for(size_t kk=0; kk< handle->depth; ++kk)
96  {
97  int8_t * rboff = (int8_t*) handle->raw_buffer + n * npix * handle->depth + kk*npix;
98 
99  #ifndef XRIF_NO_OMP
100  #pragma omp parallel if (handle->omp_parallel > 0)
101  {
102  #endif
103 
104  #ifndef XRIF_NO_OMP
105  #pragma omp for
106  #endif
107 
108  for(size_t nn = 0; nn < npix-1; ++nn)
109  {
110  rboff[npix - nn - 1] -= rboff[npix - nn - 2];
111  }
112 
113  #ifndef XRIF_NO_OMP
114  }
115  #endif
116  }
117  }
118 
119  return XRIF_NOERROR;
120 
121 } //xrif_difference_pixel_sint8
122 
123 xrif_error_t xrif_difference_pixel_sint16( xrif_t handle )
124 {
125  size_t npix = handle->width*handle->height;
126 
127  for(size_t n=0; n < handle->frames; ++n)
128  {
129  for(size_t kk=0; kk< handle->depth; ++kk)
130  {
131  int16_t * rboff = (int16_t*) handle->raw_buffer + n * npix * handle->depth + kk*npix;
132 
133  #ifndef XRIF_NO_OMP
134  #pragma omp parallel if (handle->omp_parallel > 0)
135  {
136  #endif
137 
138  #ifndef XRIF_NO_OMP
139  #pragma omp for
140  #endif
141 
142  for(size_t nn = 0; nn < npix-1; ++nn)
143  {
144  rboff[npix - nn - 1] -= rboff[npix - nn - 2];
145  }
146 
147  #ifndef XRIF_NO_OMP
148  }
149  #endif
150  }
151  }
152 
153  return XRIF_NOERROR;
154 
155 } //xrif_difference_pixel_sint16
156 
157 xrif_error_t xrif_difference_pixel_sint32( xrif_t handle )
158 {
159  size_t npix = handle->width*handle->height;
160 
161  for(size_t n=0; n < handle->frames; ++n)
162  {
163  for(size_t kk=0; kk< handle->depth; ++kk)
164  {
165  int32_t * rboff = (int32_t*) handle->raw_buffer + n * npix * handle->depth + kk*npix;
166 
167  #ifndef XRIF_NO_OMP
168  #pragma omp parallel if (handle->omp_parallel > 0)
169  {
170  #endif
171 
172  #ifndef XRIF_NO_OMP
173  #pragma omp for
174  #endif
175 
176  for(size_t nn = 0; nn < npix-1; ++nn)
177  {
178  rboff[npix - nn - 1] -= rboff[npix - nn - 2];
179  }
180 
181  #ifndef XRIF_NO_OMP
182  }
183  #endif
184  }
185  }
186 
187  return XRIF_NOERROR;
188 
189 } //xrif_difference_pixel_sint32
190 
191 xrif_error_t xrif_difference_pixel_sint64( xrif_t handle )
192 {
193  size_t npix = handle->width*handle->height;
194 
195  for(size_t n=0; n < handle->frames; ++n)
196  {
197  for(size_t kk=0; kk< handle->depth; ++kk)
198  {
199  int64_t * rboff = (int64_t*) handle->raw_buffer + n * npix * handle->depth + kk*npix;
200 
201  #ifndef XRIF_NO_OMP
202  #pragma omp parallel if (handle->omp_parallel > 0)
203  {
204  #endif
205 
206  #ifndef XRIF_NO_OMP
207  #pragma omp for
208  #endif
209 
210  for(size_t nn = 0; nn < npix-1; ++nn)
211  {
212  rboff[npix - nn - 1] -= rboff[npix - nn - 2];
213  }
214 
215  #ifndef XRIF_NO_OMP
216  }
217  #endif
218  }
219  }
220 
221  return XRIF_NOERROR;
222 
223 } //xrif_difference_pixel_sint64
224 
225 
226 //Dispatch differencing w.r.t. previous according to type
228 {
229  if( handle == NULL)
230  {
231  XRIF_ERROR_PRINT("xrif_difference_pixel", "can not use a null pointer");
232  return XRIF_ERROR_NULLPTR;
233  }
234 
235  if( handle->raw_buffer == NULL || handle->width*handle->height*handle->depth*handle->frames == 0 || handle->type_code == 0)
236  {
237  XRIF_ERROR_PRINT("xrif_difference_pixel", "handle not set up");
238  return XRIF_ERROR_NOT_SETUP;
239  }
240 
241  if(handle->raw_buffer_size < handle->width*handle->height*handle->depth*handle->frames)
242  {
243  XRIF_ERROR_PRINT("xrif_difference_pixel", "raw buffer size not sufficient");
245  }
246 
247  if(handle->type_code == XRIF_TYPECODE_INT8 || handle->type_code == XRIF_TYPECODE_UINT8)
248  {
249  return xrif_difference_pixel_sint8(handle);
250  }
251  if(handle->type_code == XRIF_TYPECODE_INT16 || handle->type_code == XRIF_TYPECODE_UINT16)
252  {
253  return xrif_difference_pixel_sint16(handle);
254  }
255  else if(handle->type_code == XRIF_TYPECODE_INT32 || handle->type_code == XRIF_TYPECODE_UINT32)
256  {
257  return xrif_difference_pixel_sint32(handle);
258  }
259  else if(handle->type_code == XRIF_TYPECODE_INT64 || handle->type_code == XRIF_TYPECODE_UINT64)
260  {
261  return xrif_difference_pixel_sint64(handle);
262  }
263  else
264  {
265  XRIF_ERROR_PRINT("xrif_difference_pixel", "previous differencing not implemented for type");
266  return XRIF_ERROR_NOTIMPL;
267  }
268 } //xrif_difference_pixel
269 
270 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
271 // undifferencing
272 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
273 
274 
275 xrif_error_t xrif_undifference_pixel_sint8( xrif_t handle )
276 {
277  size_t npix = handle->width*handle->height;
278 
279  for(int n=0; n < handle->frames; ++n)
280  {
281  for(int kk=0; kk< handle->depth; ++kk)
282  {
283  int8_t * rboff = (int8_t*)handle->raw_buffer + n * npix * handle->depth + kk*npix;
284 
285  #ifndef XRIF_NO_OMP
286  #pragma omp parallel if (handle->omp_parallel > 0)
287  {
288  #endif
289 
290  #ifndef XRIF_NO_OMP
291  #pragma omp for
292  #endif
293 
294  for(int nn = 1; nn < npix; ++nn)
295  {
296  rboff[nn] += rboff[nn-1] ;
297  }
298 
299  #ifndef XRIF_NO_OMP
300  }
301  #endif
302  }
303  }
304 
305  return XRIF_NOERROR;
306 
307 }//xrif_undifference_pixel_sint8
308 
309 xrif_error_t xrif_undifference_pixel_sint16( xrif_t handle )
310 {
311  size_t npix = handle->width*handle->height;
312 
313  for(int n=0; n < handle->frames; ++n)
314  {
315  for(int kk=0; kk< handle->depth; ++kk)
316  {
317  int16_t * rboff = (int16_t*)handle->raw_buffer + n * npix * handle->depth + kk*npix;
318 
319  #ifndef XRIF_NO_OMP
320  #pragma omp parallel if (handle->omp_parallel > 0)
321  {
322  #endif
323 
324  #ifndef XRIF_NO_OMP
325  #pragma omp for
326  #endif
327 
328  for(int nn = 1; nn < npix; ++nn)
329  {
330  rboff[nn] += rboff[nn-1] ;
331  }
332 
333  #ifndef XRIF_NO_OMP
334  }
335  #endif
336  }
337  }
338 
339  return XRIF_NOERROR;
340 
341 }//xrif_undifference_pixel_sint16
342 
343 xrif_error_t xrif_undifference_pixel_sint32( xrif_t handle )
344 {
345  size_t npix = handle->width*handle->height;
346 
347  for(int n=0; n < handle->frames; ++n)
348  {
349  for(int kk=0; kk< handle->depth; ++kk)
350  {
351  int32_t * rboff = (int32_t*)handle->raw_buffer + n * npix * handle->depth + kk*npix;
352 
353  #ifndef XRIF_NO_OMP
354  #pragma omp parallel if (handle->omp_parallel > 0)
355  {
356  #endif
357 
358  #ifndef XRIF_NO_OMP
359  #pragma omp for
360  #endif
361 
362  for(int nn = 1; nn < npix; ++nn)
363  {
364  rboff[nn] += rboff[nn-1] ;
365  }
366 
367  #ifndef XRIF_NO_OMP
368  }
369  #endif
370  }
371  }
372 
373  return XRIF_NOERROR;
374 
375 } //xrif_undifference_pixel_sint32
376 
377 xrif_error_t xrif_undifference_pixel_sint64( xrif_t handle )
378 {
379  size_t npix = handle->width*handle->height;
380 
381  for(int n=0; n < handle->frames; ++n)
382  {
383  for(int kk=0; kk< handle->depth; ++kk)
384  {
385  int64_t * rboff = (int64_t*)handle->raw_buffer + n * npix * handle->depth + kk*npix;
386 
387  #ifndef XRIF_NO_OMP
388  #pragma omp parallel if (handle->omp_parallel > 0)
389  {
390  #endif
391 
392  #ifndef XRIF_NO_OMP
393  #pragma omp for
394  #endif
395 
396  for(int nn = 1; nn < npix; ++nn)
397  {
398  rboff[nn] += rboff[nn-1] ;
399  }
400 
401  #ifndef XRIF_NO_OMP
402  }
403  #endif
404  }
405  }
406 
407  return XRIF_NOERROR;
408 
409 }//xrif_undifference_pixel_sint64
410 
411 
412 //Dispatch undifferencing w.r.t. previous according to type
414 {
415  if( handle == NULL)
416  {
417  XRIF_ERROR_PRINT("xrif_undifference_pixel", "can not use a null pointer");
418  return XRIF_ERROR_NULLPTR;
419  }
420 
421  if( handle->raw_buffer == NULL || handle->width*handle->height*handle->depth*handle->frames == 0 || handle->type_code == 0)
422  {
423  XRIF_ERROR_PRINT("xrif_undifference_pixel", "handle not set up");
424  return XRIF_ERROR_NOT_SETUP;
425  }
426 
427  if(handle->raw_buffer_size < handle->width*handle->height*handle->depth*handle->frames)
428  {
429  XRIF_ERROR_PRINT("xrif_undifference_pixel", "raw buffer size not sufficient");
431  }
432 
433  if(handle->type_code == XRIF_TYPECODE_INT8 || handle->type_code == XRIF_TYPECODE_UINT8)
434  {
435  return xrif_undifference_pixel_sint8(handle);
436  }
437  if(handle->type_code == XRIF_TYPECODE_INT16 || handle->type_code == XRIF_TYPECODE_UINT16)
438  {
439  return xrif_undifference_pixel_sint16(handle);
440  }
441  else if(handle->type_code == XRIF_TYPECODE_INT32 || handle->type_code == XRIF_TYPECODE_UINT32)
442  {
443  return xrif_undifference_pixel_sint32(handle);
444  }
445  else if(handle->type_code == XRIF_TYPECODE_INT64 || handle->type_code == XRIF_TYPECODE_UINT64)
446  {
447  return xrif_undifference_pixel_sint64(handle);
448  }
449  else
450  {
451  XRIF_ERROR_PRINT("xrif_difference_pixel", "previous undifferencing not implemented for type");
452  return XRIF_ERROR_NULLPTR;
453  }
454 } //xrif_undifference_pixel
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_TYPECODE_INT16
#define XRIF_TYPECODE_INT16
16-bit signed integer
Definition: xrif.h:217
xrif_handle::omp_parallel
int omp_parallel
Definition: xrif.h:328
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_undifference_pixel
xrif_error_t xrif_undifference_pixel(xrif_t handle)
Undifference the images using the previous pixel as a reference.
Definition: xrif_difference_pixel.c:413
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_TYPECODE_UINT8
#define XRIF_TYPECODE_UINT8
8-bit unsigned integer
Definition: xrif.h:211
XRIF_TYPECODE_INT8
#define XRIF_TYPECODE_INT8
8-bit signed integer
Definition: xrif.h:213
XRIF_ERROR_NOTIMPL
#define XRIF_ERROR_NOTIMPL
Return code indicating that the requested feature is not available.
Definition: xrif.h:181
xrif_difference_pixel
xrif_error_t xrif_difference_pixel(xrif_t handle)
Difference the images using the previous pixel as a reference.
Definition: xrif_difference_pixel.c:227
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_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.