25 #include <tjutils/tjcomplex.h>
26 #include <tjutils/tjtypes.h>
27 #include <tjutils/tjlog.h>
37 static const char* get_compName();
56 static unsigned int get_elements(
const STD_complex&) {
78 template<
typename Src,
typename Dst>
79 static void convert_array(
const Src* src, Dst* dst,
unsigned int srcsize,
unsigned int dstsize,
bool autoscale=
true) {
81 unsigned int srcstep=get_elements(*dst);
82 unsigned int dststep=get_elements(*src);
83 ODINLOG(odinlog,normalDebug) <<
"srcstep/dststep=" << srcstep <<
"/" << dststep << STD_endl;
85 bool doScale = (autoscale && std::numeric_limits<Dst>::is_integer);
86 ODINLOG(odinlog,normalDebug) <<
"doScale=" << doScale << STD_endl;
88 if(dststep*srcsize != srcstep*dstsize) {
89 ODINLOG(odinlog,warningLog) <<
"size mismatch: dststep(" << dststep <<
") * srcsize(" << srcsize <<
") != srcstep(" << srcstep <<
") * dstsize(" << dstsize <<
")" << STD_endl;
95 const double domain_minus=creal(std::numeric_limits<Dst>::min());
96 const double domain_plus =creal(std::numeric_limits<Dst>::max());
97 double minval=std::numeric_limits<double>::min();
98 double maxval=std::numeric_limits<double>::max();
99 if(srcsize>0) minval=maxval=creal(src[0]);
100 for(
unsigned int i=1; i<srcsize; i++) {
101 if(src[i]<minval) minval=creal(src[i]);
102 if(src[i]>maxval) maxval=creal(src[i]);
105 ODINLOG(odinlog,normalDebug) <<
"domain_minus/domain_plus=" << domain_minus <<
"/" << domain_plus << STD_endl;
106 ODINLOG(odinlog,normalDebug) <<
"minval/maxval=" << minval <<
"/" << maxval << STD_endl;
109 offset=0.5*( (domain_plus+domain_minus) -
secureDivision(maxval+minval, maxval-minval) * (domain_plus-domain_minus) );
110 ODINLOG(odinlog,normalDebug) <<
"scale/offset=" << scale <<
"/" << offset << STD_endl;
115 if(srcstep==dststep) {
116 unsigned int count=srcsize < dstsize?srcsize:dstsize;
117 for(
unsigned int i=0; i<count; i++) convert(src+i, dst+i,scale,offset);
119 unsigned int srcindex=0, dstindex=0;
120 while(srcindex<srcsize && dstindex<dstsize) {
121 convert(src+srcindex, dst+dstindex,scale,offset);
138 template<
typename T>
static T round(
double x) {
139 double tobecasted=x < 0 ? x - 0.5 : x + 0.5;
140 if(std::numeric_limits<T>::is_integer) {
141 if(tobecasted<std::numeric_limits<T>::min()) tobecasted=std::numeric_limits<T>::min();
142 if(tobecasted>std::numeric_limits<T>::max()) tobecasted=std::numeric_limits<T>::max();
144 return (T)(tobecasted);
150 static void convert(
const s8bit* src, STD_complex* dst,
float scale,
float offset) {
151 (*dst)=STD_complex(src[0]*scale+offset,src[1]*scale);
153 static void convert(
const u8bit* src, STD_complex* dst,
float scale,
float offset) {
154 (*dst)=STD_complex(src[0]*scale+offset,src[1]*scale);
156 static void convert(
const u16bit* src, STD_complex* dst,
float scale,
float offset) {
157 (*dst)=STD_complex(src[0]*scale+offset,src[1]*scale);
159 static void convert(
const s16bit* src, STD_complex* dst,
float scale,
float offset) {
160 (*dst)=STD_complex(src[0]*scale+offset,src[1]*scale);
162 static void convert(
const u32bit* src, STD_complex* dst,
float scale,
float offset) {
163 (*dst)=STD_complex(src[0]*scale+offset,src[1]*scale);
165 static void convert(
const s32bit* src, STD_complex* dst,
float scale,
float offset) {
166 (*dst)=STD_complex(src[0]*scale+offset,src[1]*scale);
168 static void convert(
const float *src, STD_complex* dst,
float scale,
float offset) {
169 dst[0]=STD_complex(src[0]*scale+offset,src[1]*scale);
171 static void convert(
const double *src, STD_complex* dst,
float scale,
float offset) {
172 dst[0]=STD_complex(src[0]*scale+offset,src[1]*scale);
176 static void convert(
const STD_complex* src, s8bit* dst,
float scale,
float offset) {
177 dst[0]=round<s8bit>(src->real()*scale+offset);
178 dst[1]=round<s8bit>(src->imag()*scale);
180 static void convert(
const STD_complex* src, u8bit* dst,
float scale,
float offset) {
181 dst[0]=round<u8bit>(src->real()*scale+offset);
182 dst[1]=round<u8bit>(src->imag()*scale);
184 static void convert(
const STD_complex* src, s16bit* dst,
float scale,
float offset) {
185 dst[0]=round<s16bit>(src->real()*scale+offset);
186 dst[1]=round<s16bit>(src->imag()*scale);
188 static void convert(
const STD_complex* src, u16bit* dst,
float scale,
float offset) {
189 dst[0]=round<u16bit>(src->real()*scale+offset);
190 dst[1]=round<u16bit>(src->imag()*scale);
192 static void convert(
const STD_complex* src, s32bit* dst,
float scale,
float offset) {
193 dst[0]=round<s32bit>(src->real()*scale+offset);
194 dst[1]=round<s32bit>(src->imag()*scale);
196 static void convert(
const STD_complex* src, u32bit* dst,
float scale,
float offset) {
197 dst[0]=round<u32bit>(src->real()*scale+offset);
198 dst[1]=round<u32bit>(src->imag()*scale);
200 static void convert(
const STD_complex* src,
float* dst,
float scale,
float offset) {
201 dst[0]=src->real()*scale+offset;
202 dst[1]=src->imag()*scale;
204 static void convert(
const STD_complex* src,
double* dst,
float scale,
float offset) {
205 dst[0]=src->real()*scale+offset;
206 dst[1]=src->imag()*scale;
210 static void convert(
const float* src, s8bit* dst,
float scale,
float offset) {
211 dst[0]=round<s8bit>(src[0]*scale+offset);
213 static void convert(
const float* src, u8bit* dst,
float scale,
float offset) {
214 dst[0]=round<u8bit>(src[0]*scale+offset);
216 static void convert(
const float* src, s16bit* dst,
float scale,
float offset) {
217 dst[0]=round<s16bit>(src[0]*scale+offset);
219 static void convert(
const float* src, u16bit* dst,
float scale,
float offset) {
220 dst[0]=round<u16bit>(src[0]*scale+offset);
222 static void convert(
const float* src, s32bit* dst,
float scale,
float offset) {
223 dst[0]=round<s32bit>(src[0]*scale+offset);
225 static void convert(
const float* src, u32bit* dst,
float scale,
float offset) {
226 dst[0]=round<u32bit>(src[0]*scale+offset);
230 static void convert(
const double* src, s8bit* dst,
float scale,
float offset) {
231 dst[0]=round<s8bit>(src[0]*scale+offset);
233 static void convert(
const double* src, u8bit* dst,
float scale,
float offset) {
234 dst[0]=round<u8bit>(src[0]*scale+offset);
236 static void convert(
const double* src, s16bit* dst,
float scale,
float offset) {
237 dst[0]=round<s16bit>(src[0]*scale+offset);
239 static void convert(
const double* src, u16bit* dst,
float scale,
float offset) {
240 dst[0]=round<u16bit>(src[0]*scale+offset);
242 static void convert(
const double* src, s32bit* dst,
float scale,
float offset) {
243 dst[0]=round<s32bit>(src[0]*scale+offset);
245 static void convert(
const double* src, u32bit* dst,
float scale,
float offset) {
246 dst[0]=round<u32bit>(src[0]*scale+offset);
252 template<
typename Src,
typename Dst>
253 static void convert(
const Src* src, Dst* dst,
float scale,
float offset) {
254 dst[0]=(Dst)(src[0]*scale+offset);
static unsigned int get_elements(const T &)
static void convert_array(const Src *src, Dst *dst, unsigned int srcsize, unsigned int dstsize, bool autoscale=true)
double secureDivision(double numerator, double denominator)