Merge pull request #2887 from ilya-lavrenov:ipp_morph_fix
[platform/upstream/opencv.git] / modules / objdetect / src / resizeimg.cpp
1 #include "precomp.hpp"
2 #include "opencv2/imgproc/imgproc_c.h"
3 #include "_lsvm_resizeimg.h"
4 #include <stdio.h>
5 #include <assert.h>
6 #include <math.h>
7
8 IplImage* resize_opencv(IplImage* img, float scale)
9 {
10     IplImage* imgTmp;
11
12     int W, H, tW, tH;
13
14     W = img->width;
15     H = img->height;
16
17     tW = (int)(((float)W) * scale + 0.5);
18     tH = (int)(((float)H) * scale + 0.5);
19
20     imgTmp = cvCreateImage(cvSize(tW , tH), img->depth, img->nChannels);
21     cvResize(img, imgTmp, CV_INTER_AREA);
22
23     return imgTmp;
24 }
25
26 //
27 ///*
28 // * Fast image subsampling.
29 // * This is used to construct the feature pyramid.
30 // */
31 //
32 //// struct used for caching interpolation values
33 //typedef struct  {
34 //  int si, di;
35 //  float alpha;
36 //}alphainfo;
37 //
38 //// copy src into dst using pre-computed interpolation values
39 //void alphacopy(float *src, float *dst, alphainfo *ofs, int n) {
40 //    int i;
41 //    for(i = 0; i < n; i++){
42 //        dst[ofs[i].di] += ofs[i].alpha * src[ofs[i].si];
43 //    }
44 //}
45 //
46 //int round(float val){
47 //    return (int)(val + 0.5);
48 //}
49 //void bzero(float * arr, int cnt){
50 //    int i;
51 //    for(i = 0; i < cnt; i++){
52 //        arr[i] = 0.0f;
53 //    }
54 //}
55 //// resize along each column
56 //// result is transposed, so we can apply it twice for a complete resize
57 //void resize1dtran(float *src, int sheight, float *dst, int dheight,
58 //                int width, int chan) {
59 //  alphainfo *ofs;
60 //  float scale = (float)dheight/(float)sheight;
61 //  float invscale = (float)sheight/(float)dheight;
62 //
63 //  // we cache the interpolation values since they can be
64 //  // shared among different columns
65 //  int len = (int)ceilf(dheight*invscale) + 2*dheight;
66 //  int k = 0;
67 //  int dy;
68 //  float fsy1;
69 //  float fsy2;
70 //  int sy1;
71 //  int sy2;
72 //  int sy;
73 //  int c, x;
74 //  float *s, *d;
75 //
76 //  ofs = (alphainfo *) malloc (sizeof(alphainfo) * len);
77 //  for (dy = 0; dy < dheight; dy++) {
78 //    fsy1 = dy * invscale;
79 //    fsy2 = fsy1 + invscale;
80 //    sy1 = (int)ceilf(fsy1);
81 //    sy2 = (int)floorf(fsy2);
82 //
83 //    if (sy1 - fsy1 > 1e-3) {
84 //      assert(k < len);
85 //      assert(sy1 - 1 >= 0);
86 //      ofs[k].di = dy*width;
87 //      ofs[k].si = sy1-1;
88 //      ofs[k++].alpha = (sy1 - fsy1) * scale;
89 //    }
90 //
91 //    for (sy = sy1; sy < sy2; sy++) {
92 //      assert(k < len);
93 //      assert(sy < sheight);
94 //      ofs[k].di = dy*width;
95 //      ofs[k].si = sy;
96 //      ofs[k++].alpha = scale;
97 //    }
98 //
99 //    if (fsy2 - sy2 > 1e-3) {
100 //      assert(k < len);
101 //      assert(sy2 < sheight);
102 //      ofs[k].di = dy*width;
103 //      ofs[k].si = sy2;
104 //      ofs[k++].alpha = (fsy2 - sy2) * scale;
105 //    }
106 //  }
107 //
108 //  // resize each column of each color channel
109 //  bzero(dst, chan*width*dheight);
110 //  for (c = 0; c < chan; c++) {
111 //    for (x = 0; x < width; x++) {
112 //      s = src + c*width*sheight + x*sheight;
113 //      d = dst + c*width*dheight + x;
114 //      alphacopy(s, d, ofs, k);
115 //    }
116 //  }
117 //  free(ofs);
118 //}
119 //
120 //IplImage * resize_article_dp(IplImage * img, float scale, const int k){
121 //    IplImage * imgTmp;
122 //    float W, H;
123 //    unsigned  char   *dataSrc;
124 //    float * dataf;
125 //    float *src, *dst, *tmp;
126 //    int i, j, kk, channels;
127 //      int index;
128 //    int widthStep;
129 //    int tW, tH;
130 //
131 //    W = (float)img->width;
132 //    H = (float)img->height;
133 //    channels  = img->nChannels;
134 //      widthStep = img->widthStep;
135 //
136 //    tW = (int)(((float)W) * scale + 0.5f);
137 //    tH = (int)(((float)H) * scale + 0.5f);
138 //
139 //    src = (float *)malloc(sizeof(float) * (int)(W * H * 3));
140 //
141 //    dataSrc = (unsigned char*)(img->imageData);
142 //      index = 0;
143 //      for (kk = 0; kk < channels; kk++)
144 //      {
145 //              for (i = 0; i < W; i++)
146 //              {
147 //                      for (j = 0; j < H; j++)
148 //                      {
149 //                              src[index++] = (float)dataSrc[j * widthStep + i * channels + kk];
150 //                      }
151 //              }
152 //      }
153 //
154 //    imgTmp = cvCreateImage(cvSize(tW , tH), IPL_DEPTH_32F, channels);
155 //
156 //    dst = (float *)malloc(sizeof(float) * (int)(tH * tW) * channels);
157 //    tmp = (float *)malloc(sizeof(float) * (int)(tH *  W) * channels);
158 //
159 //    resize1dtran(src, (int)H, tmp, (int)tH, (int)W , 3);
160 //
161 //    resize1dtran(tmp, (int)W, dst, (int)tW, (int)tH, 3);
162 //
163 //      index = 0;
164 //      //dataf = (float*)imgTmp->imageData;
165 //      for (kk = 0; kk < channels; kk++)
166 //      {
167 //              for (i = 0; i < tW; i++)
168 //              {
169 //                      for (j = 0; j < tH; j++)
170 //                      {
171 //                dataf = (float*)(imgTmp->imageData + j * imgTmp->widthStep);
172 //                              dataf[ i * channels + kk] = dst[index++];
173 //                      }
174 //              }
175 //      }
176 //
177 //    free(src);
178 //    free(dst);
179 //    free(tmp);
180 //    return imgTmp;
181 //}
182 //
183 //IplImage * resize_article_dp1(IplImage * img, float scale, const int k){
184 //    IplImage * imgTmp;
185 //    float W, H;
186 //    float * dataf;
187 //    float *src, *dst, *tmp;
188 //    int i, j, kk, channels;
189 //      int index;
190 //      int widthStep;
191 //    int tW, tH;
192 //
193 //    W = (float)img->width;
194 //    H = (float)img->height;
195 //    channels  = img->nChannels;
196 //      widthStep = img->widthStep;
197 //
198 //    tW = (int)(((float)W) * scale + 0.5f);
199 //    tH = (int)(((float)H) * scale + 0.5f);
200 //
201 //    src = (float *)malloc(sizeof(float) * (int)(W * H) * 3);
202 //
203 //      index = 0;
204 //      for (kk = 0; kk < channels; kk++)
205 //      {
206 //              for (i = 0; i < W; i++)
207 //              {
208 //                      for (j = 0; j < H; j++)
209 //                      {
210 //                              src[index++] = (float)(*( (float *)(img->imageData + j * widthStep) + i * channels + kk));
211 //                      }
212 //              }
213 //      }
214 //
215 //    imgTmp = cvCreateImage(cvSize(tW , tH), IPL_DEPTH_32F, channels);
216 //
217 //    dst = (float *)malloc(sizeof(float) * (int)(tH * tW) * channels);
218 //    tmp = (float *)malloc(sizeof(float) * (int)(tH *  W) * channels);
219 //
220 //    resize1dtran(src, (int)H, tmp, (int)tH, (int)W , 3);
221 //
222 //    resize1dtran(tmp, (int)W, dst, (int)tW, (int)tH, 3);
223 //
224 //      index = 0;
225 //      for (kk = 0; kk < channels; kk++)
226 //      {
227 //              for (i = 0; i < tW; i++)
228 //              {
229 //                      for (j = 0; j < tH; j++)
230 //                      {
231 //                dataf = (float *)(imgTmp->imageData + j * imgTmp->widthStep);
232 //                              dataf[ i * channels + kk] = dst[index++];
233 //                      }
234 //              }
235 //      }
236 //
237 //    free(src);
238 //    free(dst);
239 //    free(tmp);
240 //    return imgTmp;
241 //}
242 //