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