• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /* $Id: db_utilities.cpp,v 1.4 2011/06/17 14:03:31 mbansal Exp $ */
18 
19 #include "db_utilities.h"
20 #include <string.h>
21 #include <stdio.h>
22 
db_SetupImageReferences_f(float * im,int w,int h)23 float** db_SetupImageReferences_f(float *im,int w,int h)
24 {
25     int i;
26     float **img;
27     assert(im);
28     img=new float* [h];
29     for(i=0;i<h;i++)
30     {
31         img[i]=im+w*i;
32     }
33     return(img);
34 }
35 
db_SetupImageReferences_u(unsigned char * im,int w,int h)36 unsigned char** db_SetupImageReferences_u(unsigned char *im,int w,int h)
37 {
38     int i;
39     unsigned char **img;
40 
41     assert(im);
42 
43     img=new unsigned char* [h];
44     for(i=0;i<h;i++)
45     {
46         img[i]=im+w*i;
47     }
48     return(img);
49 }
db_AllocImage_f(int w,int h,int over_allocation)50 float** db_AllocImage_f(int w,int h,int over_allocation)
51 {
52     float **img,*im;
53 
54     im=new float [w*h+over_allocation];
55     img=db_SetupImageReferences_f(im,w,h);
56 
57     return(img);
58 }
59 
db_AllocImage_u(int w,int h,int over_allocation)60 unsigned char** db_AllocImage_u(int w,int h,int over_allocation)
61 {
62     unsigned char **img,*im;
63 
64     im=new unsigned char [w*h+over_allocation];
65     img=db_SetupImageReferences_u(im,w,h);
66 
67     return(img);
68 }
69 
db_FreeImage_f(float ** img,int h)70 void db_FreeImage_f(float **img,int h)
71 {
72     delete [] (img[0]);
73     delete [] img;
74 }
75 
db_FreeImage_u(unsigned char ** img,int h)76 void db_FreeImage_u(unsigned char **img,int h)
77 {
78     delete [] (img[0]);
79     delete [] img;
80 }
81 
82 // ----------------------------------------------------------------------------------------------------------- ;
83 //
84 // copy image (source to destination)
85 // ---> must be a 2D image array with the same image size
86 // ---> the size of the input and output images must be same
87 //
88 // ------------------------------------------------------------------------------------------------------------ ;
db_CopyImage_u(unsigned char ** d,const unsigned char * const * s,int w,int h,int over_allocation)89 void db_CopyImage_u(unsigned char **d,const unsigned char * const *s, int w, int h, int over_allocation)
90 {
91     int i;
92 
93     for (i=0;i<h;i++)
94     {
95         memcpy(d[i],s[i],w*sizeof(unsigned char));
96     }
97 
98     memcpy(&d[h],&d[h],over_allocation);
99 
100 }
101 
db_WarpImageLutFast_u(const unsigned char * const * src,unsigned char ** dst,int w,int h,const float * const * lut_x,const float * const * lut_y)102 inline void db_WarpImageLutFast_u(const unsigned char * const * src, unsigned char ** dst, int w, int h,
103                                   const float * const * lut_x, const float * const * lut_y)
104 {
105     assert(src && dst);
106     int xd=0, yd=0;
107 
108     for ( int i = 0; i < w; ++i )
109         for ( int j = 0; j < h; ++j )
110         {
111             //xd = static_cast<unsigned int>(lut_x[j][i]);
112             //yd = static_cast<unsigned int>(lut_y[j][i]);
113             xd = (unsigned int)(lut_x[j][i]);
114             yd = (unsigned int)(lut_y[j][i]);
115             if ( xd >= w || yd >= h ||
116                  xd < 0 || yd < 0)
117                 dst[j][i] = 0;
118             else
119                 dst[j][i] = src[yd][xd];
120         }
121 }
122 
db_WarpImageLutBilinear_u(const unsigned char * const * src,unsigned char ** dst,int w,int h,const float * const * lut_x,const float * const * lut_y)123 inline void db_WarpImageLutBilinear_u(const unsigned char * const * src, unsigned char ** dst, int w, int h,
124                                       const float * const * lut_x,const float * const* lut_y)
125 {
126     assert(src && dst);
127     double xd=0.0, yd=0.0;
128 
129     for ( int i = 0; i < w; ++i )
130         for ( int j = 0; j < h; ++j )
131         {
132             xd = static_cast<double>(lut_x[j][i]);
133             yd = static_cast<double>(lut_y[j][i]);
134             if ( xd > w   || yd > h ||
135                  xd < 0.0 || yd < 0.0)
136                 dst[j][i] = 0;
137             else
138                 dst[j][i] = db_BilinearInterpolation(yd, xd, src);
139         }
140 }
141 
142 
db_WarpImageLut_u(const unsigned char * const * src,unsigned char ** dst,int w,int h,const float * const * lut_x,const float * const * lut_y,int type)143 void db_WarpImageLut_u(const unsigned char * const * src, unsigned char ** dst, int w, int h,
144                        const float * const * lut_x,const float * const * lut_y, int type)
145 {
146     switch (type)
147     {
148     case DB_WARP_FAST:
149         db_WarpImageLutFast_u(src,dst,w,h,lut_x,lut_y);
150         break;
151     case DB_WARP_BILINEAR:
152         db_WarpImageLutBilinear_u(src,dst,w,h,lut_x,lut_y);
153         break;
154     default:
155         break;
156     }
157 }
158 
159 
db_PrintDoubleVector(double * a,long size)160 void db_PrintDoubleVector(double *a,long size)
161 {
162     printf("[ ");
163     for(long i=0;i<size;i++) printf("%lf ",a[i]);
164     printf("]");
165 }
166 
db_PrintDoubleMatrix(double * a,long rows,long cols)167 void db_PrintDoubleMatrix(double *a,long rows,long cols)
168 {
169     printf("[\n");
170     for(long i=0;i<rows;i++)
171     {
172         for(long j=0;j<cols;j++) printf("%lf ",a[i*cols+j]);
173         printf("\n");
174     }
175     printf("]");
176 }
177