|
| 1 | +/******************************************************* |
| 2 | + * Copyright (c) 2014, ArrayFire |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This file is distributed under 3-clause BSD license. |
| 6 | + * The complete license agreement can be obtained at: |
| 7 | + * http://arrayfire.com/licenses/BSD-3-Clause |
| 8 | + ********************************************************/ |
| 9 | + |
| 10 | +#include <Array.hpp> |
| 11 | +#include <unwrap.hpp> |
| 12 | +#include <stdexcept> |
| 13 | +#include <err_cpu.hpp> |
| 14 | + |
| 15 | +namespace cpu |
| 16 | +{ |
| 17 | + template<typename T> |
| 18 | + void unwrap_(T *outPtr, const T *inPtr, const af::dim4 &odims, const af::dim4 &idims, |
| 19 | + const af::dim4 &ostrides, const af::dim4 &istrides, |
| 20 | + const dim_t wx, const dim_t wy, const dim_t sx, const dim_t sy) |
| 21 | + { |
| 22 | + dim_t nx = (idims[0] - wx) / sx + 1; |
| 23 | + dim_t ny = (idims[1] - wy) / sy + 1; |
| 24 | + |
| 25 | + for(dim_t w = 0; w < odims[3]; w++) { |
| 26 | + for(dim_t z = 0; z < odims[2]; z++) { |
| 27 | + dim_t cOut = w * ostrides[3] + z * ostrides[2]; |
| 28 | + dim_t cIn = w * istrides[3] + z * istrides[2]; |
| 29 | + for(dim_t col = 0; col < odims[1]; col++) { |
| 30 | + // Calculate input window index |
| 31 | + dim_t winy = (col / ny); |
| 32 | + dim_t winx = (col % ny); |
| 33 | + |
| 34 | + dim_t startx = winx * sx; |
| 35 | + dim_t starty = winy * sy; |
| 36 | + |
| 37 | + T* optr = outPtr + cOut + col * ostrides[1]; |
| 38 | + const T* iptr = inPtr + cIn + starty * istrides[1] + startx; |
| 39 | + |
| 40 | + for(dim_t y = 0; y < wy; y++) { |
| 41 | + for(dim_t x = 0; x < wx; x++) { |
| 42 | + dim_t oloc = (y * wy + x) * ostrides[0]; |
| 43 | + dim_t iloc = (y * istrides[1] + x * istrides[0]); |
| 44 | + optr[oloc] = iptr[iloc]; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + template<typename T> |
| 53 | + Array<T> unwrap(const Array<T> &in, const dim_t wx, const dim_t wy, |
| 54 | + const dim_t sx, const dim_t sy) |
| 55 | + { |
| 56 | + af::dim4 idims = in.dims(); |
| 57 | + |
| 58 | + dim_t nx = (idims[0] - wx) / sx + 1; |
| 59 | + dim_t ny = (idims[1] - wy) / sy + 1; |
| 60 | + |
| 61 | + af::dim4 odims(wx * wy, nx * ny, idims[2], idims[3]); |
| 62 | + |
| 63 | + // Create output placeholder |
| 64 | + Array<T> outArray = createEmptyArray<T>(odims); |
| 65 | + |
| 66 | + // Get pointers to raw data |
| 67 | + const T *inPtr = in.get(); |
| 68 | + T *outPtr = outArray.get(); |
| 69 | + |
| 70 | + af::dim4 ostrides = outArray.strides(); |
| 71 | + af::dim4 istrides = in.strides(); |
| 72 | + |
| 73 | + unwrap_(outPtr, inPtr, odims, idims, ostrides, istrides, wx, wy, sx, sy); |
| 74 | + |
| 75 | + return outArray; |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | +#define INSTANTIATE(T) \ |
| 80 | + template Array<T> unwrap<T> (const Array<T> &in, const dim_t wx, const dim_t wy, \ |
| 81 | + const dim_t sx, const dim_t sy); |
| 82 | + |
| 83 | + |
| 84 | + INSTANTIATE(float) |
| 85 | + INSTANTIATE(double) |
| 86 | + INSTANTIATE(cfloat) |
| 87 | + INSTANTIATE(cdouble) |
| 88 | + INSTANTIATE(int) |
| 89 | + INSTANTIATE(uint) |
| 90 | + INSTANTIATE(intl) |
| 91 | + INSTANTIATE(uintl) |
| 92 | + INSTANTIATE(uchar) |
| 93 | + INSTANTIATE(char) |
| 94 | +} |
| 95 | + |
0 commit comments