Homepage
Demos
Overview
Downloads
Dev. Resources
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     int rowidx=0;
00121     for(col=0; col<width; col++) {
00122       rowidx+=img.col_stride;
00123       py = row_y[rowidx] >> rshift_y;
00124       pu = row_u[rowidx] >> rshift_u;
00125       pv = row_v[rowidx] >> rshift_v;
00126       tmap_idx = 
00127         (py << lshift_y) +
00128         (pu << lshift_u) +
00129         (pv << lshift_v);
00130       row_cmap[col] = tmap[tmap_idx];
00131 #ifdef CALC_AVG_IMG_COLOR
00132       total_y += row_y[rowidx];
00133       total_u += row_u[rowidx];
00134       total_v += row_v[rowidx];
00135 #endif
00136 
00137       /*
00138       if(row==height/2 && col==width/2) {
00139         printf("py=%u pu=%u pv=%u tmap_idx=%d tmap val=%u\n",py,pu,pv,tmap_idx,tmap[tmap_idx]);
00140       }
00141       */
00142     }
00143   }
00144 
00145 }
00146 
00147 //#define ENABLE_JOIN_NEARBY
00148 
00149 template <class rle_t,class color_class_state_t>
00150 void RmapToRgb(rgb *img,rle_t *map,int last_run,int width,int height,
00151     color_class_state_t *color,int num)
00152 {
00153   int i,x,y=0,next_x;
00154 
00155   i=0;
00156   next_x=0;
00157 #ifdef ENABLE_JOIN_NEARBY
00158   i=AdvanceToNextRun(i,map);
00159 #endif  
00160   while(i < last_run) {
00161     rle_t *currun;
00162     currun = &map[i];
00163     
00164     y=currun->y;
00165     if(y>=height) {
00166       return;
00167     }
00168 
00169     x=currun->x;
00170     if(x<next_x) {
00171       return;
00172     }
00173 
00174     if(x!=next_x) {
00175       for(x=next_x; x<currun->x; x++)
00176         img[y*width + x] = color[0].color;
00177     }
00178 
00179     next_x = currun->x+currun->width;
00180     for(x=currun->x; x<next_x; x++)
00181       img[y*width + x] = color[currun->color].color;
00182 
00183     if(next_x == width) {
00184       y++;
00185       next_x = 0;
00186     }
00187 
00188     i=i+1;//AdvanceToNextRun(i,map);
00189   }
00190   for(x=next_x; x<width; x++)
00191     img[y*width + x] = color[0].color;
00192 }
00193 
00194 template <class cmap_t>
00195 void RgbToIndex(cmap_t *map,rgb *img,int width,int height,
00196     rgb *colors,int num)
00197 {
00198   int i,j,size;
00199 
00200   size = width * height;
00201 
00202   j = 0;
00203   for(i=0; i<size; i++){
00204     if(img[i] != colors[j]){
00205       j = 0;
00206       while(j<num && img[i]!=colors[j]) j++;
00207       if(j==num)
00208         j = 0;
00209     }
00210     map[i] = j;
00211   }
00212 }
00213 
00214 template <class cmap_t,class color_class_state_t>
00215 void IndexToRgb(rgb *img,cmap_t *map,int width,int height,
00216     color_class_state_t *color,int num)
00217 {
00218   int i,size;
00219 
00220   size = width * height;
00221 
00222   for(i=0; i<size; i++){
00223     img[i] = color[map[i]].color;
00224   }
00225 }
00226 
00227 template <class cmap_t>
00228 void IndexToRgb(rgb *img,cmap_t *map,int width,int height,
00229     rgb *colors,int num)
00230 {
00231   int i,size;
00232 
00233   size = width * height;
00234 
00235   for(i=0; i<size; i++){
00236     img[i] = colors[map[i]];
00237   }
00238 }
00239 
00240 template <class data>
00241 data Get3D(data *arr,int num_i,int num_j,int num_k,int i,int j,int k)
00242 {
00243   int l;
00244   l = i*num_j*num_k + j*num_k + k;
00245   return(arr[l]);
00246 }
00247 
00248 template <class data>
00249 void Set3D(data *arr,int num_i,int num_j,int num_k,int i,int j,int k,data v)
00250 {
00251   int l;
00252   l = i*num_j*num_k + j*num_k + k;
00253   arr[l] = v;
00254 }
00255 
00256 template <class tmap_t>
00257 int RemapTMapColor(tmap_t *tmap,int num_y,int num_u,int num_v,int src_id,int dest_id)
00258 {
00259   int i,n,size;
00260 
00261   size = num_y * num_u * num_v;
00262   n = 0;
00263 
00264   for(i=0; i<size; i++){
00265     if(tmap[i] == src_id){
00266       tmap[i] = dest_id;
00267       n++;
00268     }
00269   }
00270 
00271   return(n);
00272 }
00273 
00274 template <class tmap_t>
00275 int CheckTMapColors(tmap_t *tmap,int num_y,int num_u,int num_v,int colors,int default_id)
00276 {
00277   int i,n,size;
00278 
00279   size = num_y * num_u * num_v;
00280   n = 0;
00281 
00282   for(i=0; i<size; i++){
00283     if(tmap[i] >= colors){
00284       tmap[i] = default_id;
00285       n++;
00286     }
00287   }
00288 
00289   return(n);
00290 }
00291 
00292 template <class tmap_t>
00293 bool LoadThresholdFile(tmap_t *tmap,int num_y,int num_u,int num_v,const char *filename)
00294 {
00295   FILE *in;
00296   char buf[256];
00297   int ny,nu,nv;
00298   int size,read;
00299 
00300   in = fopen(filename,"r");
00301   if(!in) return(false);
00302 
00303   // read magic
00304   if(!fgets(buf,256,in)) goto error;
00305   buf[4] = 0;
00306   if(strcmp(buf,"TMAP")) goto error;
00307 
00308   // read type (ignore for now)
00309   if(!fgets(buf,256,in)) goto error;
00310 
00311   // read size
00312   if(!fgets(buf,256,in)) goto error;
00313   ny = nu = nv = 0;
00314   sscanf(buf,"%d %d %d",&ny,&nu,&nv);
00315   if(num_y!=ny || num_u!=nu || num_v!=nv) goto error;
00316 
00317   size = num_y * num_u * num_v;
00318   read = fread(tmap,sizeof(tmap_t),size,in);
00319 
00320   fclose(in);
00321 
00322   return(read == size);
00323 error:
00324   if(in) fclose(in);
00325   return(false);
00326 }
00327 
00328 template <class tmap_t>
00329 bool SaveThresholdFile(tmap_t *tmap,int num_y,int num_u,int num_v,char *filename)
00330 {
00331   FILE *out;
00332   int size,wrote;
00333 
00334   out = fopen(filename,"w");
00335   if(!out) return(false);
00336 
00337   fprintf(out,"TMAP\nYUV%d\n%d %d %d\n",
00338           sizeof(tmap_t),num_y,num_u,num_v);
00339   size = num_y * num_u * num_v;
00340   wrote = fwrite(tmap,sizeof(tmap_t),size,out);
00341   fclose(out);
00342 
00343   return(wrote == size);
00344 }
00345 
00346 } // namespace
00347 
00348 #endif

Tekkotsu v2.4.1
Generated Tue Aug 16 16:32:46 2005 by Doxygen 1.4.4