Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

superRGB_Filter.cpp

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002     Phission :
00003         Realtime Vision Processing System
00004 
00005     Copyright (C) 2003-2006 Philip D.S. Thoren (pthoren@cs.uml.edu)
00006     University of Massachusetts at Lowell,
00007     Laboratory for Artificial Intelligence and Robotics
00008 
00009  ---------------------------------------------------------------------------
00010     <Add other copyrights here> 
00011  ---------------------------------------------------------------------------
00012 
00013     This file is part of Phission.
00014 
00015     Phission is free software; you can redistribute it and/or modify
00016     it under the terms of the GNU Lesser General Public License as published by
00017     the Free Software Foundation; either version 2 of the License, or
00018     (at your option) any later version.
00019 
00020     Phission is distributed in the hope that it will be useful,
00021     but WITHOUT ANY WARRANTY; without even the implied warranty of
00022     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023     GNU Lesser General Public License for more details.
00024 
00025     You should have received a copy of the GNU Lesser General Public License
00026     along with Phission; if not, write to the Free Software
00027     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028 
00029  ---------------------------------------------------------------------------*/
00030 #ifdef HAVE_CONFIG_H
00031     #include <phissionconfig.h>
00032 #endif
00033 
00034 #include <phStandard.h>
00035 
00036 #include <superRGB_Filter.h>
00037 
00038 #include <phError.h>
00039 #include <phMemory.h>
00040 #include <phPrint.h>
00041 
00042 /* ------------------------------------------------------------------------ */
00043 superRGB_Filter::superRGB_Filter(int32_t inChannel,int32_t outChannel) :
00044     phFilter("superRGB_Filter")
00045 {
00046     this->m_inChannel   = 0;
00047     this->m_outChannel  = 0;
00048     this->m_mult        = NULL;
00049     this->m_mult_size   = 0;
00050     this->m_ci          = NULL;
00051     this->m_ci_size     = 0;
00052 
00053     this->m_format = (phImageRGB24 | phImageRGBA32 |
00054                       phImageBGR24 | phImageABGR32);
00055     this->set(inChannel,outChannel);
00056 }
00057 
00058 /* ------------------------------------------------------------------------ */
00059 superRGB_Filter::~superRGB_Filter()
00060 {
00061     phFree(this->m_ci);
00062     phFree(this->m_mult);
00063     this->m_ci_size = 0;
00064     this->m_mult_size = 0;
00065 }
00066 
00067 /* ------------------------------------------------------------------------ */
00068 phFilter *superRGB_Filter::cloneFilter()
00069 {
00070     return (phFilter *)new superRGB_Filter( this->m_inChannel, this->m_outChannel );
00071 }
00072 
00073 /* ------------------------------------------------------------------------ */
00074 int superRGB_Filter::set( int32_t inChannel,
00075                           int32_t outChannel )
00076 {
00077     phFUNCTION("superRGB_Filter::set")
00078     int locked = 0;
00079     
00080     phTHIS_LOOSE_LOCK(locked);
00081     
00082     this->m_lighten = 1;
00083     if (this->m_inChannel >= 0)
00084         this->m_inChannel = inChannel;
00085     if (this->m_outChannel >= 0)
00086         this->m_outChannel = outChannel;
00087 
00088     phTHIS_LOOSE_UNLOCK(locked);
00089     
00090     return phSUCCESS;
00091 }
00092 
00093 /* ------------------------------------------------------------------------ */
00094 int superRGB_Filter::filter() 
00095 {
00096     phFUNCTION("superRGB_Filter::filter")
00097     
00098 #if 0
00099     uint32_t h = 0;
00100     uint32_t w = 0;
00101     uint32_t d = 0;
00102     uint32_t row = 0;
00103     uint32_t pixel = 0;
00104 #endif
00105     uint32_t m1 = 0;
00106     uint32_t m2 = 0;
00107    
00108     int32_t order = ((format & (phImageRGB24 | phImageRGBA32)) ? 0 : 1);
00109 #if 0
00110     int32_t ri = (depth == 4) ? (order ? 3 : 0) : (order ? 2 : 0);
00111     int32_t gi = (depth == 4) ? (order ? 2 : 1) : (order ? 1 : 1);
00112     int32_t bi = (depth == 4) ? (order ? 1 : 2) : (order ? 0 : 2);
00113     int32_t ai = (depth == 4) ? (order ? 0 : 3) : (order ? 0 : 0);
00114 #endif
00115 
00116     uint32_t    i       = 0;
00117     uint32_t    j       = 0;
00118     float       tempf   = 0.0F;
00119     int32_t     tempi   = 0;
00120     uint32_t    limit   = width * height;
00121     uint8_t    *imgptr  = (uint8_t *)Image;
00122     
00123     /* This will allow testing of possible required multipliers for
00124      * certain colors, like green, to get a better match... it might work,
00125      * you never know! */
00126     /* Perhaps the weights used by a luminance conversion could be used
00127      * here in some manner: r-0.33 g-0.59 b-0.11 */
00128     const float multipliers_o0[] = {1.0,1.0,1.0,1.0}; 
00129     const float multipliers_o1[] = {1.0,1.0,1.0,1.0};
00130     const uint32_t multiplier_count = 4;
00131         
00132     m1 = (this->m_outChannel + 1 ) % depth;
00133     m2 = (this->m_outChannel + 2 ) % depth;
00134     
00135     phDALLOC_RESIZE(this->m_ci,
00136                     this->m_ci_size,
00137                     depth,
00138                     uint32_t);
00139     phDALLOC_RESIZE(this->m_mult,
00140                     this->m_mult_size,
00141                     depth,
00142                     float);
00143     
00144     /* Set up the channels, index:0 is the value to be subtracted from
00145      * and index:>0 are all added up and subtracted from index:0 */
00146     this->m_ci[0] = this->m_inChannel;
00147     for (j = 1; j < depth; j++)
00148     {
00149         this->m_ci[j] = (this->m_inChannel + j) % depth;
00150     }
00151     /* Use multipliers because some color values are usually stronger than 
00152      * others */
00153     for (j = 0; j < depth; j++)
00154     {
00155         if (j < multiplier_count)
00156         {
00157             if (order == 0)
00158                 this->m_mult[j] = multipliers_o0[(this->m_inChannel + j) % depth];
00159             else if (order == 1)
00160                 this->m_mult[j] = multipliers_o1[(this->m_inChannel + j) % depth];
00161         }
00162         else
00163             this->m_mult[j] = 1.0F;
00164     }
00165    
00166     for ( i = 0; i < limit; i++, imgptr += depth) 
00167     {
00168         tempf = 0;
00169         for (j = 1; j < depth; j++ )
00170         {
00171             tempf += imgptr[this->m_ci[j]] * this->m_mult[j];
00172         }
00173         //tempf = imgptr[ri] - (imgptr[bi] + imgptr[gi]);
00174         tempf = (imgptr[this->m_inChannel] * this->m_mult[this->m_inChannel]) - 
00175                tempf;
00176         
00177         if (tempf<0)
00178         {
00179             imgptr[this->m_outChannel] = 0;
00180         }
00181         else
00182         {
00183             tempi = (int32_t)tempf;
00184             if (this->m_lighten)
00185             {
00186                 tempi += 60;
00187                 if( tempi > 255) tempi = 255;
00188                 imgptr[this->m_outChannel] = tempi;
00189             }
00190             else
00191             {
00192                 imgptr[this->m_outChannel] = tempi;
00193             }
00194         }
00195         imgptr[m1] = 0;
00196         imgptr[m2] = 0;
00197     }
00198     /* This was the original code. It's confusing because there are no
00199      * comments and alot of hardcoded values which could be anything.
00200      * Although, one could make very educated guessed, look above for
00201      * a more understandable version of the code */
00202 #if 0
00203   m1=(channel+1)%3;
00204   m2=(channel+2)%3;
00205 
00206   p=map;
00207 
00208   for (i=0; i<h; i++) {
00209     for (j=0; j<w; j++) {
00210       temp = *(p+2) - (*(p+1) + *p);
00211 
00212       if (temp<0)
00213         *(p+channel) = 0;
00214 
00215       else{
00216         if(lighten)
00217           {
00218             if(temp+60 > 255)
00219               temp = 195;
00220             *(p+channel) = temp+60;
00221           }
00222         else
00223           *(p+channel) = temp;
00224       }
00225       *(p+m1) = 0;
00226       *(p+m2) = 0;
00227       p += 3;
00228     }
00229   }
00230 
00231 #endif
00232     return phSUCCESS;
00233 error:
00234     return phFAIL;
00235 }  
00236 
00237 




Copyright (C) 2002 - 2007 Philip D.S. Thoren ( pthoren@users.sourceforge.net )
University Of Massachusetts at Lowell
Robotics Lab
SourceForge.net Logo

Generated on Sat Jun 16 02:44:11 2007 for phission by  doxygen 1.4.4