OpenJPH
Open-source implementation of JPEG2000 Part-15
ojph_colour.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) 2019, Aous Naman
6 // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia
7 // Copyright (c) 2019, 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_colour.cpp
34 // Author: Aous Naman
35 // Date: 28 August 2019
36 //***************************************************************************/
37 
38 #include <cmath>
39 
40 #include "ojph_defs.h"
41 #include "ojph_arch.h"
42 #include "ojph_colour.h"
43 #include "ojph_colour_local.h"
44 
45 namespace ojph {
46  namespace local {
47 
50  (const si32 *sp, si32 *dp, int shift, ui32 width)
52 
55  (const si32 *sp, float *dp, float mul, ui32 width)
57 
60  (const si32 *sp, float *dp, float mul, ui32 width)
62 
65  (const float *sp, si32 *dp, float mul, ui32 width)
67 
70  (const float *sp, si32 *dp, float mul, ui32 width)
72 
74  void (*rct_forward)
75  (const si32 *r, const si32 *g, const si32 *b,
76  si32 *y, si32 *cb, si32 *cr, ui32 repeat)
78 
80  void (*rct_backward)
81  (const si32 *y, const si32 *cb, const si32 *cr,
82  si32 *r, si32 *g, si32 *b, ui32 repeat)
84 
86  void (*ict_forward)
87  (const float *r, const float *g, const float *b,
88  float *y, float *cb, float *cr, ui32 repeat)
90 
92  void (*ict_backward)
93  (const float *y, const float *cb, const float *cr,
94  float *r, float *g, float *b, ui32 repeat)
96 
99 
102  {
104  return;
105 
115 
116 #ifndef OJPH_DISABLE_INTEL_SIMD
117  int level = cpu_ext_level();
118 
119  if (level >= 2)
120  {
127  }
128 
129  if (level >= 3)
130  {
136  }
137 
138  if (level >= 7)
139  {
146  }
147 
148  if (level >= 8)
149  {
153  }
154 #endif
155 
157  }
158 
159 
161  const float CT_CNST::ALPHA_RF = 0.299f;
162  const float CT_CNST::ALPHA_GF = 0.587f;
163  const float CT_CNST::ALPHA_BF = 0.114f;
164  const float CT_CNST::BETA_CbF = float(0.5/(1-double(CT_CNST::ALPHA_BF)));
165  const float CT_CNST::BETA_CrF = float(0.5/(1-double(CT_CNST::ALPHA_RF)));
166  const float CT_CNST::GAMMA_CB2G =
167  float(2.0*double(ALPHA_BF)*(1.0-double(ALPHA_BF))/double(ALPHA_GF));
168  const float CT_CNST::GAMMA_CR2G =
169  float(2.0*double(ALPHA_RF)*(1.0-double(ALPHA_RF))/double(ALPHA_GF));
170  const float CT_CNST::GAMMA_CB2B = float(2.0 * (1.0 - double(ALPHA_BF)));
171  const float CT_CNST::GAMMA_CR2R = float(2.0 * (1.0 - double(ALPHA_RF)));
172 
174  void gen_cnvrt_si32_to_si32_shftd(const si32 *sp, si32 *dp, int shift,
175  ui32 width)
176  {
177  for (ui32 i = width; i > 0; --i)
178  *dp++ = *sp++ + shift;
179  }
180 
182  void gen_cnvrt_si32_to_float_shftd(const si32 *sp, float *dp, float mul,
183  ui32 width)
184  {
185  for (ui32 i = width; i > 0; --i)
186  *dp++ = (float)*sp++ * mul - 0.5f;
187  }
188 
190  void gen_cnvrt_si32_to_float(const si32 *sp, float *dp, float mul,
191  ui32 width)
192  {
193  for (ui32 i = width; i > 0; --i)
194  *dp++ = (float)*sp++ * mul;
195  }
196 
198  void gen_cnvrt_float_to_si32_shftd(const float *sp, si32 *dp, float mul,
199  ui32 width)
200  {
201  for (ui32 i = width; i > 0; --i)
202  *dp++ = ojph_round((*sp++ + 0.5f) * mul);
203  }
204 
206  void gen_cnvrt_float_to_si32(const float *sp, si32 *dp, float mul,
207  ui32 width)
208  {
209  for (ui32 i = width; i > 0; --i)
210  *dp++ = ojph_round(*sp++ * mul);
211  }
212 
214  void gen_rct_forward(const si32 *r, const si32 *g, const si32 *b,
215  si32 *y, si32 *cb, si32 *cr, ui32 repeat)
216  {
217  for (ui32 i = repeat; i > 0; --i)
218  {
219  *y++ = (*r + (*g << 1) + *b) >> 2;
220  *cb++ = (*b++ - *g);
221  *cr++ = (*r++ - *g++);
222  }
223  }
224 
226  void gen_rct_backward(const si32 *y, const si32 *cb, const si32 *cr,
227  si32 *r, si32 *g, si32 *b, ui32 repeat)
228  {
229  for (ui32 i = repeat; i > 0; --i)
230  {
231  *g = *y++ - ((*cb + *cr)>>2);
232  *b++ = *cb++ + *g;
233  *r++ = *cr++ + *g++;
234  }
235  }
236 
238  void gen_ict_forward(const float *r, const float *g, const float *b,
239  float *y, float *cb, float *cr, ui32 repeat)
240  {
241  for (ui32 i = repeat; i > 0; --i)
242  {
243  *y = CT_CNST::ALPHA_RF * *r
244  + CT_CNST::ALPHA_GF * *g++
245  + CT_CNST::ALPHA_BF * *b;
246  *cb++ = CT_CNST::BETA_CbF * (*b++ - *y);
247  *cr++ = CT_CNST::BETA_CrF * (*r++ - *y++);
248  }
249  }
250 
252  void gen_ict_backward(const float *y, const float *cb, const float *cr,
253  float *r, float *g, float *b, ui32 repeat)
254  {
255  for (ui32 i = repeat; i > 0; --i)
256  {
257  *g++ = *y - CT_CNST::GAMMA_CR2G * *cr - CT_CNST::GAMMA_CB2G * *cb;
258  *r++ = *y + CT_CNST::GAMMA_CR2R * *cr++;
259  *b++ = *y++ + CT_CNST::GAMMA_CB2B * *cb++;
260  }
261  }
262 
263  }
264 }
void(* cnvrt_float_to_si32)(const float *sp, si32 *dp, float mul, ui32 width)
Definition: ojph_colour.cpp:70
void avx2_rct_forward(const si32 *r, const si32 *g, const si32 *b, si32 *y, si32 *cb, si32 *cr, ui32 repeat)
void avx_cnvrt_float_to_si32(const float *sp, si32 *dp, float mul, ui32 width)
void(* cnvrt_si32_to_float_shftd)(const si32 *sp, float *dp, float mul, ui32 width)
Definition: ojph_colour.cpp:55
void(* ict_forward)(const float *r, const float *g, const float *b, float *y, float *cb, float *cr, ui32 repeat)
Definition: ojph_colour.cpp:87
void avx_ict_forward(const float *r, const float *g, const float *b, float *y, float *cb, float *cr, ui32 repeat)
void(* cnvrt_si32_to_si32_shftd)(const si32 *sp, si32 *dp, int shift, ui32 width)
Definition: ojph_colour.cpp:50
void avx_cnvrt_si32_to_float_shftd(const si32 *sp, float *dp, float mul, ui32 width)
void(* ict_backward)(const float *y, const float *cb, const float *cr, float *r, float *g, float *b, ui32 repeat)
Definition: ojph_colour.cpp:93
void gen_rct_backward(const si32 *y, const si32 *cb, const si32 *cr, si32 *r, si32 *g, si32 *b, ui32 repeat)
void(* rct_backward)(const si32 *y, const si32 *cb, const si32 *cr, si32 *r, si32 *g, si32 *b, ui32 repeat)
Definition: ojph_colour.cpp:81
void gen_cnvrt_si32_to_float(const si32 *sp, float *dp, float mul, ui32 width)
void avx2_rct_backward(const si32 *y, const si32 *cb, const si32 *cr, si32 *r, si32 *g, si32 *b, ui32 repeat)
void init_colour_transform_functions()
void gen_ict_forward(const float *r, const float *g, const float *b, float *y, float *cb, float *cr, ui32 repeat)
void sse2_rct_forward(const si32 *r, const si32 *g, const si32 *b, si32 *y, si32 *cb, si32 *cr, ui32 repeat)
void sse_cnvrt_float_to_si32_shftd(const float *sp, si32 *dp, float mul, ui32 width)
void sse2_cnvrt_float_to_si32_shftd(const float *sp, si32 *dp, float mul, ui32 width)
void(* cnvrt_float_to_si32_shftd)(const float *sp, si32 *dp, float mul, ui32 width)
Definition: ojph_colour.cpp:65
void sse2_cnvrt_si32_to_si32_shftd(const si32 *sp, si32 *dp, int shift, ui32 width)
void gen_cnvrt_si32_to_si32_shftd(const si32 *sp, si32 *dp, int shift, ui32 width)
void sse_ict_forward(const float *r, const float *g, const float *b, float *y, float *cb, float *cr, ui32 repeat)
void avx_cnvrt_si32_to_float(const si32 *sp, float *dp, float mul, ui32 width)
static bool colour_transform_functions_initialized
Definition: ojph_colour.cpp:98
void sse_cnvrt_si32_to_float_shftd(const si32 *sp, float *dp, float mul, ui32 width)
void(* cnvrt_si32_to_float)(const si32 *sp, float *dp, float mul, ui32 width)
Definition: ojph_colour.cpp:60
void gen_cnvrt_float_to_si32(const float *sp, si32 *dp, float mul, ui32 width)
void gen_ict_backward(const float *y, const float *cb, const float *cr, float *r, float *g, float *b, ui32 repeat)
void sse2_rct_backward(const si32 *y, const si32 *cb, const si32 *cr, si32 *r, si32 *g, si32 *b, ui32 repeat)
void(* rct_forward)(const si32 *r, const si32 *g, const si32 *b, si32 *y, si32 *cb, si32 *cr, ui32 repeat)
Definition: ojph_colour.cpp:75
void gen_cnvrt_float_to_si32_shftd(const float *sp, si32 *dp, float mul, ui32 width)
void gen_rct_forward(const si32 *r, const si32 *g, const si32 *b, si32 *y, si32 *cb, si32 *cr, ui32 repeat)
void avx_cnvrt_float_to_si32_shftd(const float *sp, si32 *dp, float mul, ui32 width)
void avx_ict_backward(const float *y, const float *cb, const float *cr, float *r, float *g, float *b, ui32 repeat)
void sse2_cnvrt_float_to_si32(const float *sp, si32 *dp, float mul, ui32 width)
void sse_cnvrt_si32_to_float(const si32 *sp, float *dp, float mul, ui32 width)
void gen_cnvrt_si32_to_float_shftd(const si32 *sp, float *dp, float mul, ui32 width)
void avx2_cnvrt_si32_to_si32_shftd(const si32 *sp, si32 *dp, int shift, ui32 width)
void sse_ict_backward(const float *y, const float *cb, const float *cr, float *r, float *g, float *b, ui32 repeat)
void sse_cnvrt_float_to_si32(const float *sp, si32 *dp, float mul, ui32 width)
static si32 ojph_round(float val)
Definition: ojph_arch.h:131
int cpu_ext_level()
Definition: ojph_arch.cpp:170
int32_t si32
Definition: ojph_defs.h:55
uint32_t ui32
Definition: ojph_defs.h:54
static const float GAMMA_CR2R
static const float BETA_CbF
static const float GAMMA_CB2B
static const float ALPHA_RF
static const float GAMMA_CB2G
static const float GAMMA_CR2G
static const float ALPHA_BF
static const float BETA_CrF
static const float ALPHA_GF