| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#include "templating.h" |
| 8 |
|
| 9 |
#ifndef TYPE |
| 10 |
#error TYPE must be defined here. |
| 11 |
#else |
| 12 |
|
| 13 |
#ifndef REAL_TYPE |
| 14 |
#error REAL_TYPE must be defined here. |
| 15 |
#else |
| 16 |
|
| 17 |
#include "wt.h" |
| 18 |
|
| 19 |
#if defined _MSC_VER |
| 20 |
#define restrict __restrict |
| 21 |
#elif defined __GNUC__ |
| 22 |
#define restrict __restrict__ |
| 23 |
#endif |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
int CAT(TYPE, _downcoef_axis)(const TYPE * const restrict input, const ArrayInfo input_info, |
| 28 |
TYPE * const restrict output, const ArrayInfo output_info, |
| 29 |
const DiscreteWavelet * const restrict wavelet, const size_t axis, |
| 30 |
const Coefficient coef, const MODE dwt_mode, |
| 31 |
const size_t swt_level, |
| 32 |
const DiscreteTransformType transform){ |
| 33 |
size_t i; |
| 34 |
size_t num_loops = 1; |
| 35 |
TYPE * temp_input = NULL, * temp_output = NULL; |
| 36 |
|
| 37 |
|
| 38 |
int make_temp_input, make_temp_output; |
| 39 |
|
| 40 |
if (input_info.ndim != output_info.ndim) |
| 41 |
return 1; |
| 42 |
if (axis >= input_info.ndim) |
| 43 |
return 2; |
| 44 |
|
| 45 |
for (i = 0; i < input_info.ndim; ++i){ |
| 46 |
if (i == axis){ |
| 47 |
switch (transform) { |
| 48 |
case DWT_TRANSFORM: |
| 49 |
if (dwt_buffer_length(input_info.shape[i], wavelet->dec_len, |
| 50 |
dwt_mode) != output_info.shape[i]) |
| 51 |
return 3; |
| 52 |
break; |
| 53 |
case SWT_TRANSFORM: |
| 54 |
if (swt_buffer_length(input_info.shape[i]) |
| 55 |
!= output_info.shape[i]) |
| 56 |
return 4; |
| 57 |
break; |
| 58 |
} |
| 59 |
} else { |
| 60 |
if (input_info.shape[i] != output_info.shape[i]) |
| 61 |
return 5; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
make_temp_input = input_info.strides[axis] != sizeof(TYPE); |
| 66 |
make_temp_output = output_info.strides[axis] != sizeof(TYPE); |
| 67 |
if (make_temp_input) |
| 68 |
if ((temp_input = malloc(input_info.shape[axis] * sizeof(TYPE))) == NULL) |
| 69 |
goto cleanup; |
| 70 |
if (make_temp_output) |
| 71 |
if ((temp_output = malloc(output_info.shape[axis] * sizeof(TYPE))) == NULL) |
| 72 |
goto cleanup; |
| 73 |
|
| 74 |
for (i = 0; i < output_info.ndim; ++i){ |
| 75 |
if (i != axis) |
| 76 |
num_loops *= output_info.shape[i]; |
| 77 |
} |
| 78 |
|
| 79 |
for (i = 0; i < num_loops; ++i){ |
| 80 |
size_t j; |
| 81 |
size_t input_offset = 0, output_offset = 0; |
| 82 |
const TYPE * input_row; |
| 83 |
TYPE * output_row; |
| 84 |
|
| 85 |
|
| 86 |
{ |
| 87 |
size_t reduced_idx = i; |
| 88 |
for (j = 0; j < output_info.ndim; ++j){ |
| 89 |
size_t j_rev = output_info.ndim - 1 - j; |
| 90 |
if (j_rev != axis){ |
| 91 |
size_t axis_idx = reduced_idx % output_info.shape[j_rev]; |
| 92 |
reduced_idx /= output_info.shape[j_rev]; |
| 93 |
|
| 94 |
input_offset += (axis_idx * input_info.strides[j_rev]); |
| 95 |
output_offset += (axis_idx * output_info.strides[j_rev]); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
if (make_temp_input) |
| 102 |
for (j = 0; j < input_info.shape[axis]; ++j) |
| 103 |
|
| 104 |
temp_input[j] = *(TYPE *)(((char *) input) + input_offset |
| 105 |
+ j * input_info.strides[axis]); |
| 106 |
|
| 107 |
|
| 108 |
input_row = make_temp_input ? temp_input |
| 109 |
: (const TYPE *)((const char *) input + input_offset); |
| 110 |
output_row = make_temp_output ? temp_output |
| 111 |
: (TYPE *)((char *) output + output_offset); |
| 112 |
|
| 113 |
|
| 114 |
switch (transform) { |
| 115 |
case DWT_TRANSFORM: |
| 116 |
|
| 117 |
switch (coef){ |
| 118 |
case COEF_APPROX: |
| 119 |
CAT(TYPE, _dec_a)(input_row, input_info.shape[axis], |
| 120 |
wavelet, |
| 121 |
output_row, output_info.shape[axis], |
| 122 |
dwt_mode); |
| 123 |
break; |
| 124 |
case COEF_DETAIL: |
| 125 |
CAT(TYPE, _dec_d)(input_row, input_info.shape[axis], |
| 126 |
wavelet, |
| 127 |
output_row, output_info.shape[axis], |
| 128 |
dwt_mode); |
| 129 |
break; |
| 130 |
} |
| 131 |
break; |
| 132 |
|
| 133 |
case SWT_TRANSFORM: |
| 134 |
|
| 135 |
switch (coef){ |
| 136 |
case COEF_APPROX: |
| 137 |
CAT(TYPE, _swt_a)(input_row, input_info.shape[axis], |
| 138 |
wavelet, |
| 139 |
output_row, output_info.shape[axis], |
| 140 |
swt_level); |
| 141 |
break; |
| 142 |
case COEF_DETAIL: |
| 143 |
CAT(TYPE, _swt_d)(input_row, input_info.shape[axis], |
| 144 |
wavelet, |
| 145 |
output_row, output_info.shape[axis], |
| 146 |
swt_level); |
| 147 |
break; |
| 148 |
} |
| 149 |
break; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
if (make_temp_output) |
| 154 |
for (j = 0; j < output_info.shape[axis]; ++j) |
| 155 |
|
| 156 |
*(TYPE *)((char *) output + output_offset |
| 157 |
+ j * output_info.strides[axis]) = output_row[j]; |
| 158 |
} |
| 159 |
|
| 160 |
free(temp_input); |
| 161 |
free(temp_output); |
| 162 |
return 0; |
| 163 |
|
| 164 |
cleanup: |
| 165 |
free(temp_input); |
| 166 |
free(temp_output); |
| 167 |
return 6; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
int CAT(TYPE, _idwt_axis)(const TYPE * const restrict coefs_a, const ArrayInfo * const a_info, |
| 172 |
const TYPE * const restrict coefs_d, const ArrayInfo * const d_info, |
| 173 |
TYPE * const restrict output, const ArrayInfo output_info, |
| 174 |
const DiscreteWavelet * const restrict wavelet, |
| 175 |
const size_t axis, const MODE mode){ |
| 176 |
size_t i; |
| 177 |
size_t num_loops = 1; |
| 178 |
TYPE * temp_coefs_a = NULL, * temp_coefs_d = NULL, * temp_output = NULL; |
| 179 |
|
| 180 |
|
| 181 |
int make_temp_coefs_a, make_temp_coefs_d, make_temp_output; |
| 182 |
int have_a = ((coefs_a != NULL) && (a_info != NULL)); |
| 183 |
int have_d = ((coefs_d != NULL) && (d_info != NULL)); |
| 184 |
|
| 185 |
|
| 186 |
if (!have_a && !have_d) |
| 187 |
return 3; |
| 188 |
|
| 189 |
if ((have_a && (a_info->ndim != output_info.ndim)) || |
| 190 |
(have_d && (d_info->ndim != output_info.ndim))) |
| 191 |
return 1; |
| 192 |
if (axis >= output_info.ndim) |
| 193 |
return 1; |
| 194 |
|
| 195 |
for (i = 0; i < output_info.ndim; ++i){ |
| 196 |
if (i == axis){ |
| 197 |
size_t input_shape; |
| 198 |
if (have_a && have_d && |
| 199 |
(d_info->shape[i] != a_info->shape[i])) |
| 200 |
return 1; |
| 201 |
input_shape = have_a ? a_info->shape[i] : d_info->shape[i]; |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
if (idwt_buffer_length(input_shape, wavelet->rec_len, mode) |
| 207 |
!= output_info.shape[i]) |
| 208 |
return 1; |
| 209 |
} else { |
| 210 |
if ((have_a && (a_info->shape[i] != output_info.shape[i])) || |
| 211 |
(have_d && (d_info->shape[i] != output_info.shape[i]))) |
| 212 |
return 1; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
make_temp_coefs_a = have_a && a_info->strides[axis] != sizeof(TYPE); |
| 217 |
make_temp_coefs_d = have_d && d_info->strides[axis] != sizeof(TYPE); |
| 218 |
make_temp_output = output_info.strides[axis] != sizeof(TYPE); |
| 219 |
if (make_temp_coefs_a) |
| 220 |
if ((temp_coefs_a = malloc(a_info->shape[axis] * sizeof(TYPE))) == NULL) |
| 221 |
goto cleanup; |
| 222 |
if (make_temp_coefs_d) |
| 223 |
if ((temp_coefs_d = malloc(d_info->shape[axis] * sizeof(TYPE))) == NULL) |
| 224 |
goto cleanup; |
| 225 |
if (make_temp_output) |
| 226 |
if ((temp_output = malloc(output_info.shape[axis] * sizeof(TYPE))) == NULL) |
| 227 |
goto cleanup; |
| 228 |
|
| 229 |
for (i = 0; i < output_info.ndim; ++i){ |
| 230 |
if (i != axis) |
| 231 |
num_loops *= output_info.shape[i]; |
| 232 |
} |
| 233 |
|
| 234 |
for (i = 0; i < num_loops; ++i){ |
| 235 |
size_t j; |
| 236 |
size_t a_offset = 0, d_offset = 0, output_offset = 0; |
| 237 |
TYPE * output_row; |
| 238 |
|
| 239 |
|
| 240 |
{ |
| 241 |
size_t reduced_idx = i; |
| 242 |
for (j = 0; j < output_info.ndim; ++j){ |
| 243 |
size_t j_rev = output_info.ndim - 1 - j; |
| 244 |
if (j_rev != axis){ |
| 245 |
size_t axis_idx = reduced_idx % output_info.shape[j_rev]; |
| 246 |
reduced_idx /= output_info.shape[j_rev]; |
| 247 |
|
| 248 |
if (have_a) |
| 249 |
a_offset += (axis_idx * a_info->strides[j_rev]); |
| 250 |
if (have_d) |
| 251 |
d_offset += (axis_idx * d_info->strides[j_rev]); |
| 252 |
output_offset += (axis_idx * output_info.strides[j_rev]); |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
if (make_temp_coefs_a) |
| 259 |
for (j = 0; j < a_info->shape[axis]; ++j) |
| 260 |
|
| 261 |
temp_coefs_a[j] = *(TYPE *)((char *) coefs_a + a_offset |
| 262 |
+ j * a_info->strides[axis]); |
| 263 |
if (make_temp_coefs_d) |
| 264 |
for (j = 0; j < d_info->shape[axis]; ++j) |
| 265 |
|
| 266 |
temp_coefs_d[j] = *(TYPE *)((char *) coefs_d + d_offset |
| 267 |
+ j * d_info->strides[axis]); |
| 268 |
|
| 269 |
|
| 270 |
output_row = make_temp_output ? temp_output |
| 271 |
: (TYPE *)((char *) output + output_offset); |
| 272 |
|
| 273 |
|
| 274 |
memset(output_row, 0, output_info.shape[axis] * sizeof(TYPE)); |
| 275 |
|
| 276 |
if (have_a){ |
| 277 |
|
| 278 |
const TYPE * a_row = make_temp_coefs_a ? temp_coefs_a |
| 279 |
: (const TYPE *)((const char *) coefs_a + a_offset); |
| 280 |
CAT(TYPE, _upsampling_convolution_valid_sf) |
| 281 |
(a_row, a_info->shape[axis], |
| 282 |
wavelet->CAT(rec_lo_, REAL_TYPE), wavelet->rec_len, |
| 283 |
output_row, output_info.shape[axis], |
| 284 |
mode); |
| 285 |
} |
| 286 |
if (have_d){ |
| 287 |
|
| 288 |
const TYPE * d_row = make_temp_coefs_d ? temp_coefs_d |
| 289 |
: (const TYPE *)((const char *) coefs_d + d_offset); |
| 290 |
CAT(TYPE, _upsampling_convolution_valid_sf) |
| 291 |
(d_row, d_info->shape[axis], |
| 292 |
wavelet->CAT(rec_hi_, REAL_TYPE), wavelet->rec_len, |
| 293 |
output_row, output_info.shape[axis], |
| 294 |
mode); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
if (make_temp_output) |
| 299 |
for (j = 0; j < output_info.shape[axis]; ++j) |
| 300 |
|
| 301 |
*(TYPE *)((char *) output + output_offset |
| 302 |
+ j * output_info.strides[axis]) = output_row[j]; |
| 303 |
} |
| 304 |
|
| 305 |
free(temp_coefs_a); |
| 306 |
free(temp_coefs_d); |
| 307 |
free(temp_output); |
| 308 |
return 0; |
| 309 |
|
| 310 |
cleanup: |
| 311 |
free(temp_coefs_a); |
| 312 |
free(temp_coefs_d); |
| 313 |
free(temp_output); |
| 314 |
return 2; |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
int CAT(TYPE, _dec_a)(const TYPE * const restrict input, const size_t input_len, |
| 319 |
const DiscreteWavelet * const restrict wavelet, |
| 320 |
TYPE * const restrict output, const size_t output_len, |
| 321 |
const MODE mode){ |
| 322 |
|
| 323 |
|
| 324 |
if(output_len != dwt_buffer_length(input_len, wavelet->dec_len, mode)){ |
| 325 |
return -1; |
| 326 |
} |
| 327 |
|
| 328 |
return CAT(TYPE, _downsampling_convolution)(input, input_len, |
| 329 |
wavelet->CAT(dec_lo_, REAL_TYPE), |
| 330 |
wavelet->dec_len, output, |
| 331 |
2, mode); |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
int CAT(TYPE, _dec_d)(const TYPE * const restrict input, const size_t input_len, |
| 338 |
const DiscreteWavelet * const restrict wavelet, |
| 339 |
TYPE * const restrict output, const size_t output_len, |
| 340 |
const MODE mode){ |
| 341 |
|
| 342 |
|
| 343 |
if(output_len != dwt_buffer_length(input_len, wavelet->dec_len, mode)) |
| 344 |
return -1; |
| 345 |
|
| 346 |
return CAT(TYPE, _downsampling_convolution)(input, input_len, |
| 347 |
wavelet->CAT(dec_hi_, REAL_TYPE), |
| 348 |
wavelet->dec_len, output, |
| 349 |
2, mode); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
int CAT(TYPE, _rec_a)(const TYPE * const restrict coeffs_a, const size_t coeffs_len, |
| 356 |
const DiscreteWavelet * const restrict wavelet, |
| 357 |
TYPE * const restrict output, const size_t output_len){ |
| 358 |
|
| 359 |
|
| 360 |
if(output_len != reconstruction_buffer_length(coeffs_len, wavelet->rec_len)) |
| 361 |
return -1; |
| 362 |
|
| 363 |
return CAT(TYPE, _upsampling_convolution_full)(coeffs_a, coeffs_len, |
| 364 |
wavelet->CAT(rec_lo_, REAL_TYPE), |
| 365 |
wavelet->rec_len, output, |
| 366 |
output_len); |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
int CAT(TYPE, _rec_d)(const TYPE * const restrict coeffs_d, const size_t coeffs_len, |
| 373 |
const DiscreteWavelet * const restrict wavelet, |
| 374 |
TYPE * const restrict output, const size_t output_len){ |
| 375 |
|
| 376 |
|
| 377 |
if(output_len != reconstruction_buffer_length(coeffs_len, wavelet->rec_len)) |
| 378 |
return -1; |
| 379 |
|
| 380 |
return CAT(TYPE, _upsampling_convolution_full)(coeffs_d, coeffs_len, |
| 381 |
wavelet->CAT(rec_hi_, REAL_TYPE), |
| 382 |
wavelet->rec_len, output, |
| 383 |
output_len); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
int CAT(TYPE, _idwt)(const TYPE * const restrict coeffs_a, const size_t coeffs_a_len, |
| 394 |
const TYPE * const restrict coeffs_d, const size_t coeffs_d_len, |
| 395 |
TYPE * const restrict output, const size_t output_len, |
| 396 |
const DiscreteWavelet * const restrict wavelet, const MODE mode){ |
| 397 |
size_t input_len; |
| 398 |
if(coeffs_a != NULL && coeffs_d != NULL){ |
| 399 |
if(coeffs_a_len != coeffs_d_len) |
| 400 |
goto error; |
| 401 |
input_len = coeffs_a_len; |
| 402 |
} else if(coeffs_a != NULL){ |
| 403 |
input_len = coeffs_a_len; |
| 404 |
} else if (coeffs_d != NULL){ |
| 405 |
input_len = coeffs_d_len; |
| 406 |
} else { |
| 407 |
goto error; |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
if(output_len != idwt_buffer_length(input_len, wavelet->rec_len, mode)) |
| 412 |
goto error; |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
if(coeffs_a){ |
| 421 |
if(CAT(TYPE, _upsampling_convolution_valid_sf)(coeffs_a, input_len, |
| 422 |
wavelet->CAT(rec_lo_, REAL_TYPE), |
| 423 |
wavelet->rec_len, output, |
| 424 |
output_len, mode) < 0){ |
| 425 |
goto error; |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
if(coeffs_d){ |
| 433 |
if(CAT(TYPE, _upsampling_convolution_valid_sf)(coeffs_d, input_len, |
| 434 |
wavelet->CAT(rec_hi_, REAL_TYPE), |
| 435 |
wavelet->rec_len, output, |
| 436 |
output_len, mode) < 0){ |
| 437 |
goto error; |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
return 0; |
| 442 |
|
| 443 |
error: |
| 444 |
return -1; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
int CAT(TYPE, _swt_)(const TYPE * const restrict input, pywt_index_t input_len, |
| 449 |
const REAL_TYPE * const restrict filter, pywt_index_t filter_len, |
| 450 |
TYPE * const restrict output, size_t output_len, |
| 451 |
unsigned int level){ |
| 452 |
|
| 453 |
REAL_TYPE * e_filter; |
| 454 |
pywt_index_t i, e_filter_len, fstep; |
| 455 |
int ret; |
| 456 |
|
| 457 |
if(level < 1) |
| 458 |
return -1; |
| 459 |
|
| 460 |
if(level > swt_max_level(input_len)) |
| 461 |
return -2; |
| 462 |
|
| 463 |
if(output_len != swt_buffer_length(input_len)) |
| 464 |
return -1; |
| 465 |
|
| 466 |
|
| 467 |
if(level > 1){ |
| 468 |
|
| 469 |
e_filter_len = filter_len << (level-1); |
| 470 |
if ((e_filter = wtcalloc(e_filter_len, sizeof(TYPE))) == NULL) |
| 471 |
goto cleanup; |
| 472 |
if(e_filter == NULL) |
| 473 |
return -1; |
| 474 |
fstep = 1 << (level - 1); |
| 475 |
|
| 476 |
|
| 477 |
for(i = 0; i < filter_len; ++i){ |
| 478 |
e_filter[i << (level-1)] = filter[i]; |
| 479 |
} |
| 480 |
ret = CAT(TYPE, _downsampling_convolution_periodization)(input, input_len, e_filter, |
| 481 |
e_filter_len, output, 1, |
| 482 |
fstep); |
| 483 |
wtfree(e_filter); |
| 484 |
return ret; |
| 485 |
|
| 486 |
} else { |
| 487 |
return CAT(TYPE, _downsampling_convolution_periodization)(input, input_len, filter, |
| 488 |
filter_len, output, 1, |
| 489 |
1); |
| 490 |
} |
| 491 |
cleanup: |
| 492 |
wtfree(e_filter); |
| 493 |
return -3; |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
int CAT(TYPE, _swt_a)(const TYPE * const restrict input, pywt_index_t input_len, |
| 501 |
const DiscreteWavelet * const restrict wavelet, |
| 502 |
TYPE * const restrict output, pywt_index_t output_len, |
| 503 |
unsigned int level){ |
| 504 |
return CAT(TYPE, _swt_)(input, input_len, wavelet->CAT(dec_lo_, REAL_TYPE), |
| 505 |
wavelet->dec_len, output, output_len, level); |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
int CAT(TYPE, _swt_d)(const TYPE * const restrict input, pywt_index_t input_len, |
| 512 |
const DiscreteWavelet * const restrict wavelet, |
| 513 |
TYPE * const restrict output, pywt_index_t output_len, |
| 514 |
unsigned int level){ |
| 515 |
return CAT(TYPE, _swt_)(input, input_len, wavelet->CAT(dec_hi_, REAL_TYPE), |
| 516 |
wavelet->dec_len, output, output_len, level); |
| 517 |
} |
| 518 |
|
| 519 |
#endif |
| 520 |
#endif |
| 521 |
#undef restrict |