Homepage Demos Overview Downloads Tutorials Reference
Credits

cmv_threshold.h

Go to the documentation of this file.
00001 /*========================================================================
00002     cmv_threshold.h : Color threshold support for CMVision2
00003   ------------------------------------------------------------------------
00004     Copyright (C) 1999-2002  James R. Bruce
00005     School of Computer Science, Carnegie Mellon University
00006   ------------------------------------------------------------------------
00007     This software is distributed under the GNU General Public License,
00008     version 2.  If you do not have a copy of this licence, visit
00009     www.gnu.org, or write: Free Software Foundation, 59 Temple Place,
00010     Suite 330 Boston, MA 02111-1307 USA.  This program is distributed
00011     in the hope that it will be useful, but WITHOUT ANY WARRANTY,
00012     including MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00013     ========================================================================*/
00014 
00015 #ifndef __CMV_THRESHOLD_H__
00016 #define __CMV_THRESHOLD_H__
00017 
00018 #include <stdio.h>
00019 #include "cmv_types.h"
00020 
00021 namespace CMVision{
00022 
00023 template<int x,int y,int z>
00024 class DummyI3 {
00025 };
00026 
00027 template<class T,int x,int y,int z>
00028 class DummyT1I3 {
00029 };
00030 
00031 template <class cmap_t,class image,int bits_y,int bits_u,int bits_v>
00032 void ThresholdImage(cmap_t *cmap,image &img,cmap_t *tmap,DummyI3<bits_y,bits_u,bits_v> dummy=DummyI3<bits_y,bits_u,bits_v>())
00033 {
00034   // yuyv *buf,p;
00035   uyvy *buf,p;
00036   int i,m,size;
00037 
00038   int rshift_y,rshift_u,rshift_v;
00039   int lshift_y,lshift_u,lshift_v;
00040 
00041   rshift_y = 8 - bits_y;
00042   rshift_u = 8 - bits_u;
00043   rshift_v = 8 - bits_v;
00044 
00045   lshift_y = bits_u + bits_v;
00046   lshift_u = bits_v;
00047   lshift_v = 0;
00048 
00049   size = img.width * img.height;
00050   buf  = img.buf;
00051 
00052   for(i=0; i<size; i++){
00053     p = buf[i / 2];
00054     m = ((p.u >> rshift_u) << lshift_u) +
00055         ((p.v >> rshift_v) << lshift_v);
00056     cmap[i + 0] = tmap[m + ((p.y1 >> rshift_y) << lshift_y)];
00057     cmap[i + 1] = tmap[m + ((p.y1 >> rshift_y) << lshift_y)];
00058   }
00059 }
00060 
00061 template <class cmap_t,class image>
00062 void ThresholdImageRGB16(cmap_t *cmap,image &img,cmap_t *tmap)
00063 {
00064   unsigned short *buf;
00065   int i,size;
00066 
00067   size = img.width * img.height;
00068   buf  = (unsigned short*)img.buf;
00069 
00070   for(i=0; i<size; i++){
00071     cmap[i] = tmap[buf[i]];
00072   }
00073 }
00074 
00075 //void ThresholdImageYUVPlanar(cmap_t *cmap,image &img,cmap_t *tmap,DummyT1I3<element,bits_y,bits_u,bits_v> dummy=DummyT1I3<element,bits_y,bits_u,bits_v>())
00076 template <class cmap_t,class image,class element,int bits_y,int bits_u,int bits_v>
00077 void ThresholdImageYUVPlanar(cmap_t *cmap,image &img,cmap_t *tmap)
00078 {
00079   //int nonzero_cnt=0;
00080 
00081   // element *buf_y,*buf_u,*buf_v;
00082   int row,col;
00083   int width,height;
00084   int py,pu,pv;
00085   int tmap_idx;
00086 #ifdef CALC_AVG_IMG_COLOR
00087   ulong total_y;
00088   ulong total_u;
00089   ulong total_v;
00090 #endif
00091 
00092   int rshift_y,rshift_u,rshift_v;
00093   int lshift_y,lshift_u,lshift_v;
00094 
00095   element *row_y,*row_u,*row_v;
00096   cmap_t *row_cmap;
00097 
00098   rshift_y = 8 - bits_y;
00099   rshift_u = 8 - bits_u;
00100   rshift_v = 8 - bits_v;
00101 
00102   lshift_y = bits_u + bits_v;
00103   lshift_u = bits_v;
00104   lshift_v = 0;
00105 
00106   width  = img.width;
00107   height = img.height;
00108 #ifdef CALC_AVG_IMG_COLOR
00109   total_y = 0;
00110   total_u = 0;
00111   total_v = 0;
00112 #endif
00113 
00114   for(row=0; row<height; row++) {
00115     row_y = img.buf_y + row*img.row_stride;
00116     row_u = img.buf_u + row*img.row_stride;
00117     row_v = img.buf_v + row*img.row_stride;
00118     row_cmap = cmap + row*width;
00119 
00120     for(col=0; col<width; col++) {
00121       py = row_y[col] >> rshift_y;
00122       pu = row_u[col] >> rshift_u;
00123       pv = row_v[col] >> rshift_v;
00124       tmap_idx = 
00125         (py << lshift_y) +
00126         (pu << lshift_u) +
00127         (pv << lshift_v);
00128       row_cmap[col] = tmap[tmap_idx];
00129 #ifdef CALC_AVG_IMG_COLOR
00130       total_y += row_y[col];
00131       total_u += row_u[col];
00132       total_v += row_v[col];
00133 #endif
00134 
00135       /*
00136       if(row==height/2 && col==width/2) {
00137         printf("py=%u pu=%u pv=%u tmap_idx=%d tmap val=%u\n",py,pu,pv,tmap_idx,tmap[tmap_idx]);
00138       }
00139       */
00140     }
00141   }
00142 
00143 }
00144 
00145 //#define ENABLE_JOIN_NEARBY
00146 
00147 template <class rle_t,class color_class_state_t>
00148 void RmapToRgb(rgb *img,rle_t *map,int last_run,int width,int height,
00149     color_class_state_t *color,int num)
00150 {
00151   int i,x,y=0,next_x;
00152 
00153   i=0;
00154   next_x=0;
00155 #ifdef ENABLE_JOIN_NEARBY
00156   i=AdvanceToNextRun(i,map);
00157 #endif  
00158   while(i < last_run) {
00159     rle_t *currun;
00160     currun = &map[i];
00161     
00162     y=currun->y;
00163     if(y>=height) {
00164       return;
00165     }
00166 
00167     x=currun->x;
00168     if(x<next_x) {
00169       return;
00170     }
00171 
00172     if(x!=next_x) {
00173       for(x=next_x; x<currun->x; x++)
00174         img[y*width + x] = color[0].color;
00175     }
00176 
00177     next_x = currun->x+currun->width;
00178     for(x=currun->x; x<next_x; x++)
00179       img[y*width + x] = color[currun->color].color;
00180 
00181     if(next_x == width) {
00182       y++;
00183       next_x = 0;
00184     }
00185 
00186     i=i+1;//AdvanceToNextRun(i,map);
00187   }
00188   for(x=next_x; x<width; x++)
00189     img[y*width + x] = color[0].color;
00190 }
00191 
00192 template <class cmap_t>
00193 void RgbToIndex(cmap_t *map,rgb *img,int width,int height,
00194     rgb *colors,int num)
00195 {
00196   int i,j,size;
00197 
00198   size = width * height;
00199 
00200   j = 0;
00201   for(i=0; i<size; i++){
00202     if(img[i] != colors[j]){
00203       j = 0;
00204       while(j<num && img[i]!=colors[j]) j++;
00205       if(j==num)
00206         j = 0;
00207     }
00208     map[i] = j;
00209   }
00210 }
00211 
00212 template <class cmap_t,class color_class_state_t>
00213 void IndexToRgb(rgb *img,cmap_t *map,int width,int height,
00214     color_class_state_t *color,int num)
00215 {
00216   int i,size;
00217 
00218   size = width * height;
00219 
00220   for(i=0; i<size; i++){
00221     img[i] = color[map[i]].color;
00222   }
00223 }
00224 
00225 template <class cmap_t>
00226 void IndexToRgb(rgb *img,cmap_t *map,int width,int height,
00227     rgb *colors,int num)
00228 {
00229   int i,size;
00230 
00231   size = width * height;
00232 
00233   for(i=0; i<size; i++){
00234     img[i] = colors[map[i]];
00235   }
00236 }
00237 
00238 template <class data>
00239 data Get3D(data *arr,int num_i,int num_j,int num_k,int i,int j,int k)
00240 {
00241   int l;
00242   l = i*num_j*num_k + j*num_k + k;
00243   return(arr[l]);
00244 }
00245 
00246 template <class data>
00247 void Set3D(data *arr,int num_i,int num_j,int num_k,int i,int j,int k,data v)
00248 {
00249   int l;
00250   l = i*num_j*num_k + j*num_k + k;
00251   arr[l] = v;
00252 }
00253 
00254 template <class tmap_t>
00255 int RemapTMapColor(tmap_t *tmap,int num_y,int num_u,int num_v,int src_id,int dest_id)
00256 {
00257   int i,n,size;
00258 
00259   size = num_y * num_u * num_v;
00260   n = 0;
00261 
00262   for(i=0; i<size; i++){
00263     if(tmap[i] == src_id){
00264       tmap[i] = dest_id;
00265       n++;
00266     }
00267   }
00268 
00269   return(n);
00270 }
00271 
00272 template <class tmap_t>
00273 int CheckTMapColors(tmap_t *tmap,int num_y,int num_u,int num_v,int colors,int default_id)
00274 {
00275   int i,n,size;
00276 
00277   size = num_y * num_u * num_v;
00278   n = 0;
00279 
00280   for(i=0; i<size; i++){
00281     if(tmap[i] >= colors){
00282       tmap[i] = default_id;
00283       n++;
00284     }
00285   }
00286 
00287   return(n);
00288 }
00289 
00290 template <class tmap_t>
00291 bool LoadThresholdFile(tmap_t *tmap,int num_y,int num_u,int num_v,const char *filename)
00292 {
00293   FILE *in;
00294   char buf[256];
00295   int ny,nu,nv;
00296   int size,read;
00297 
00298   in = fopen(filename,"r");
00299   if(!in) return(false);
00300 
00301   // read magic
00302   if(!fgets(buf,256,in)) goto error;
00303   buf[4] = 0;
00304   if(strcmp(buf,"TMAP")) goto error;
00305 
00306   // read type (ignore for now)
00307   if(!fgets(buf,256,in)) goto error;
00308 
00309   // read size
00310   if(!fgets(buf,256,in)) goto error;
00311   ny = nu = nv = 0;
00312   sscanf(buf,"%d %d %d",&ny,&nu,&nv);
00313   if(num_y!=ny || num_u!=nu || num_v!=nv) goto error;
00314 
00315   size = num_y * num_u * num_v;
00316   read = fread(tmap,sizeof(tmap_t),size,in);
00317 
00318   fclose(in);
00319 
00320   return(read == size);
00321 error:
00322   if(in) fclose(in);
00323   return(false);
00324 }
00325 
00326 template <class tmap_t>
00327 bool SaveThresholdFile(tmap_t *tmap,int num_y,int num_u,int num_v,char *filename)
00328 {
00329   FILE *out;
00330   int size,wrote;
00331 
00332   out = fopen(filename,"w");
00333   if(!out) return(false);
00334 
00335   fprintf(out,"TMAP\nYUV%d\n%d %d %d\n",
00336           sizeof(tmap_t),num_y,num_u,num_v);
00337   size = num_y * num_u * num_v;
00338   wrote = fwrite(tmap,sizeof(tmap_t),size,out);
00339   fclose(out);
00340 
00341   return(wrote == size);
00342 }
00343 
00344 } // namespace
00345 
00346 #endif

Tekkotsu v2.2.2
Generated Tue Jan 4 15:43:13 2005 by Doxygen 1.4.0