|
| 1 | +#include <iostream> |
| 2 | +#include <cmath> |
| 3 | + |
| 4 | +using std::cout; |
| 5 | +using std::endl; |
| 6 | + |
| 7 | +inline double bilinear_filter(double x) |
| 8 | +{ |
| 9 | + if (x < 0.0) |
| 10 | + x = -x; |
| 11 | + if (x < 1.0) |
| 12 | + return 1.0 - x; |
| 13 | + return 0.0; |
| 14 | +} |
| 15 | + |
| 16 | +int precompute_coeffs(int inSize, int outSize, int** boundsp, double** kkp) |
| 17 | +{ |
| 18 | + double filterscale = (double)inSize / outSize; |
| 19 | + double scale = filterscale; |
| 20 | + if (filterscale < 1.0) |
| 21 | + filterscale = 1.0; |
| 22 | + double support = 1.0 * filterscale; |
| 23 | + int ksize = (int)ceil(support) * 2 + 1; |
| 24 | + |
| 25 | + double* kk = (double*)malloc(outSize * ksize * sizeof(double)); |
| 26 | + int* bounds = (int*)malloc(outSize * 2* sizeof(int)); |
| 27 | + double center = 0, ww = 0, ss = 0; |
| 28 | + int xmin = 0, xmax = 0; |
| 29 | + |
| 30 | + for (int i = 0; i < outSize; ++i) |
| 31 | + { |
| 32 | + center = (i + 0.5) * scale; |
| 33 | + ww = 0.0; |
| 34 | + ss = 1.0 / filterscale; |
| 35 | + |
| 36 | + xmin = (int) (center - support + 0.5); |
| 37 | + if (xmin < 0) |
| 38 | + xmin = 0; |
| 39 | + xmax = (int) (center + support + 0.5); |
| 40 | + if (xmax > inSize) |
| 41 | + xmax = inSize; |
| 42 | + xmax -= xmin; |
| 43 | + double* k = &kk[i * ksize]; |
| 44 | + for (int j = 0; j < xmax; ++j) |
| 45 | + { |
| 46 | + double w = bilinear_filter((j + xmin - center + 0.5) * ss); |
| 47 | + k[j] = w; |
| 48 | + ww += w; |
| 49 | + } |
| 50 | + for (int j = 0; j < xmax; ++j) |
| 51 | + { |
| 52 | + if (ww != 0.0) |
| 53 | + k[j] /= ww; |
| 54 | + } |
| 55 | + for (int j = xmax; j < ksize; ++j) |
| 56 | + k[j] = 0; |
| 57 | + bounds[i*2] = xmin; |
| 58 | + bounds[i*2 + 1] = xmax; |
| 59 | + } |
| 60 | + *boundsp = bounds; |
| 61 | + *kkp = kk; |
| 62 | + |
| 63 | + return ksize; |
| 64 | +} |
| 65 | + |
| 66 | +void resize(const unsigned char* srcImg, unsigned char* dstImg, int srcSize, int dstSize) |
| 67 | +{ |
| 68 | + int srcWidth = srcSize; |
| 69 | + int srcHeight = srcSize; |
| 70 | + int dstWidth = dstSize; |
| 71 | + int dstHeight = dstSize; |
| 72 | + |
| 73 | + int* bounds_horiz = nullptr, *bounds_vert = nullptr; |
| 74 | + double* kk_horiz = nullptr, *kk_vert = nullptr; |
| 75 | + |
| 76 | + int ksize_horiz = precompute_coeffs(srcWidth, dstWidth, &bounds_horiz, &kk_horiz); |
| 77 | + int ksize_vert = precompute_coeffs(srcHeight, dstHeight, &bounds_vert, &kk_vert); |
| 78 | + |
| 79 | + cout << ksize_horiz << " " << ksize_vert << endl; |
| 80 | + int ybox_first = bounds_vert[0]; |
| 81 | + int ybox_last = bounds_vert[dstHeight*2 - 2] + bounds_vert[dstHeight*2 - 1]; |
| 82 | + |
| 83 | + cout << ybox_first << " " << ybox_last << endl; |
| 84 | + |
| 85 | + for (int i = 0; i < dstHeight; ++i) |
| 86 | + { |
| 87 | + bounds_vert[i*2] -= ybox_first; |
| 88 | + } |
| 89 | + |
| 90 | + free(bounds_horiz); |
| 91 | + free(bounds_vert); |
| 92 | + free(kk_horiz); |
| 93 | + free(kk_vert); |
| 94 | +} |
| 95 | + |
| 96 | +int main() |
| 97 | +{ |
| 98 | + unsigned char* imgData = new unsigned char[200*200]; |
| 99 | + unsigned char* resizeImg = new unsigned char[64]; |
| 100 | + resize(imgData, resizeImg, 200, 64); |
| 101 | + |
| 102 | + delete[] imgData; |
| 103 | + delete[] resizeImg; |
| 104 | + |
| 105 | + int k = (64*3+3) & -4; |
| 106 | + cout << k << endl; |
| 107 | + return 0; |
| 108 | +} |
0 commit comments