OpenJPH
Open-source implementation of JPEG2000 Part-15
ojph_codestream_gen.cpp
Go to the documentation of this file.
1//***************************************************************************/
2// This software is released under the 2-Clause BSD license, included
3// below.
4//
5// Copyright (c) 2022, Aous Naman
6// Copyright (c) 2022, Kakadu Software Pty Ltd, Australia
7// Copyright (c) 2022, The University of New South Wales, Australia
8//
9// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions are
11// met:
12//
13// 1. Redistributions of source code must retain the above copyright
14// notice, this list of conditions and the following disclaimer.
15//
16// 2. Redistributions in binary form must reproduce the above copyright
17// notice, this list of conditions and the following disclaimer in the
18// documentation and/or other materials provided with the distribution.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31//***************************************************************************/
32// This file is part of the OpenJPH software implementation.
33// File: ojph_codestream_gen.cpp
34// Author: Aous Naman
35// Date: 15 May 2022
36//***************************************************************************/
37
38#include "ojph_defs.h"
39#include "ojph_arch.h"
40
41namespace ojph {
42 namespace local {
43
45 void gen_mem_clear(void* addr, size_t count)
46 {
47 ui32* p = (ui32*)addr;
48 for (size_t i = 0; i < count; i += 4, p += 1)
49 *p = 0;
50 }
51
53 ui32 gen_find_max_val(ui32* addr) { return addr[0]; }
54
56 void gen_rev_tx_to_cb(const void *sp, ui32 *dp, ui32 K_max,
57 float delta_inv, ui32 count,
58 ui32* max_val)
59 {
60 ojph_unused(delta_inv);
61 ui32 shift = 31 - K_max;
62 // convert to sign and magnitude and keep max_val
63 ui32 tmax = *max_val;
64 si32 *p = (si32*)sp;
65 for (ui32 i = count; i > 0; --i)
66 {
67 si32 v = *p++;
68 ui32 sign = v >= 0 ? 0 : 0x80000000;
69 ui32 val = (ui32)(v >= 0 ? v : -v);
70 val <<= shift;
71 *dp++ = sign | val;
72 tmax |= val; // it is more efficient to use or than max
73 }
74 *max_val = tmax;
75 }
76
78 void gen_irv_tx_to_cb(const void *sp, ui32 *dp, ui32 K_max,
79 float delta_inv, ui32 count,
80 ui32* max_val)
81 {
82 ojph_unused(K_max);
83 //quantize and convert to sign and magnitude and keep max_val
84 ui32 tmax = *max_val;
85 float *p = (float*)sp;
86 for (ui32 i = count; i > 0; --i)
87 {
88 float v = *p++;
89 si32 t = ojph_trunc(v * delta_inv);
90 ui32 sign = t >= 0 ? 0 : 0x80000000;
91 ui32 val = (ui32)(t >= 0 ? t : -t);
92 *dp++ = sign | val;
93 tmax |= val; // it is more efficient to use or than max
94 }
95 *max_val = tmax;
96 }
97
99 void gen_rev_tx_from_cb(const ui32 *sp, void *dp, ui32 K_max,
100 float delta, ui32 count)
101 {
102 ojph_unused(delta);
103 ui32 shift = 31 - K_max;
104 //convert to sign and magnitude
105 si32 *p = (si32*)dp;
106 for (ui32 i = count; i > 0; --i)
107 {
108 ui32 v = *sp++;
109 si32 val = (v & 0x7FFFFFFF) >> shift;
110 *p++ = (v & 0x80000000) ? -val : val;
111 }
112 }
113
115 void gen_irv_tx_from_cb(const ui32 *sp, void *dp, ui32 K_max,
116 float delta, ui32 count)
117 {
118 ojph_unused(K_max);
119 //convert to sign and magnitude
120 float *p = (float*)dp;
121 for (ui32 i = count; i > 0; --i)
122 {
123 ui32 v = *sp++;
124 float val = (float)(v & 0x7FFFFFFF) * delta;
125 *p++ = (v & 0x80000000) ? -val : val;
126 }
127 }
128
129 }
130}
void gen_rev_tx_to_cb(const void *sp, ui32 *dp, ui32 K_max, float delta_inv, ui32 count, ui32 *max_val)
void gen_rev_tx_from_cb(const ui32 *sp, void *dp, ui32 K_max, float delta, ui32 count)
void gen_irv_tx_to_cb(const void *sp, ui32 *dp, ui32 K_max, float delta_inv, ui32 count, ui32 *max_val)
void gen_irv_tx_from_cb(const ui32 *sp, void *dp, ui32 K_max, float delta, ui32 count)
void gen_mem_clear(void *addr, size_t count)
ui32 gen_find_max_val(ui32 *address)
static si32 ojph_trunc(float val)
Definition: ojph_arch.h:183
int32_t si32
Definition: ojph_defs.h:55
uint32_t ui32
Definition: ojph_defs.h:54
#define ojph_unused(x)
Definition: ojph_defs.h:78