apps/autoexposure/autoexposure.c

Go to the documentation of this file.
00001 /*!***************************************************************************
00002 *! FILE NAME  : autoexposure.c
00003 *! DESCRIPTION: Daemon to adjust camera exposure and white balance
00004 *! Copyright (C) 2008 Elphel, Inc.
00005 *! -----------------------------------------------------------------------------**
00006 *!  This program is free software: you can redistribute it and/or modify
00007 *!  it under the terms of the GNU General Public License as published by
00008 *!  the Free Software Foundation, either version 3 of the License, or
00009 *!  (at your option) any later version.
00010 *!
00011 *!  This program is distributed in the hope that it will be useful,
00012 *!  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 *!  GNU General Public License for more details.
00015 *!
00016 *!  You should have received a copy of the GNU General Public License
00017 *!  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 *! -----------------------------------------------------------------------------**
00019 *!
00020 *!  $Log: autoexposure.c,v $
00021 *!  Revision 1.1.1.1  2008/11/27 20:04:01  elphel
00022 *!
00023 *!
00024 *!  Revision 1.16  2008/11/18 19:30:16  elphel
00025 *!  Added initialization of the "next" frame - otherwise it wait _very_ long if the camera frame number is reset
00026 *!
00027 *!  Revision 1.15  2008/11/15 23:08:24  elphel
00028 *!  8.0.alpha17 - split autoexposure source file
00029 *!
00030 *!  Revision 1.14  2008/11/15 07:05:38  elphel
00031 *!  implemented analog gain control (in addition to gammas) while while balancing
00032 *!
00033 *!  Revision 1.13  2008/11/15 03:10:13  elphel
00034 *!  Some parameters renamed, reassigned.
00035 *!
00036 *!  Revision 1.12  2008/11/14 07:13:32  elphel
00037 *!  Added gammaReverse() and gammaDirect() functions, TODO lists, bug fixes
00038 *!
00039 *!  Revision 1.11  2008/11/14 01:01:59  elphel
00040 *!  cleared debug output
00041 *!
00042 *!  Revision 1.10  2008/11/13 05:40:45  elphel
00043 *!  8.0.alpha16 - modified histogram storage, profiling
00044 *!
00045 *!  Revision 1.9  2008/11/08 05:52:21  elphel
00046 *!  debug feature
00047 *!
00048 *!  Revision 1.8  2008/11/04 17:40:34  elphel
00049 *!  comment typos
00050 *!
00051 *!  Revision 1.7  2008/11/02 00:32:35  elphel
00052 *!  added TODO
00053 *!
00054 *!  Revision 1.6  2008/10/31 18:26:32  elphel
00055 *!  Adding support for constants like SENSOR_REGS32 (defined constant plus 32 to simplify referencing sensor registers from PHP
00056 *!
00057 *!  Revision 1.5  2008/10/29 04:18:28  elphel
00058 *!  v.8.0.alpha10 made a separate structure for global parameters (not related to particular frames in a frame queue)
00059 *!
00060 *!  Revision 1.4  2008/10/28 07:05:12  elphel
00061 *!  implemented white balance, HDR mode (1/1 and 2/2)
00062 *!
00063 *!  Revision 1.3  2008/10/26 05:55:38  elphel
00064 *!  snapshot
00065 *!
00066 *!  Revision 1.2  2008/10/25 19:49:15  elphel
00067 *!  8.0.alpha8 - added autoexposure to the installation
00068 *!
00069 *!  Revision 1.1  2008/10/24 00:34:12  elphel
00070 *!  initial release
00071 *!
00072 */ 
00073 #include "autoexposure.h"
00074 
00075 int main (int argc, char *argv[]) {
00076   int daemon_bit=0; 
00077 //  int perc;
00078   int rslt, ae_rslt;
00079   int hdr_mode; 
00080   unsigned long vexp , old_vexp, old_that_vexpos;
00081   int exp_ahead; 
00082   unsigned long next_frame;
00083   const char usage[]=   "Usage:\n%s [-b <daemon_bit_number> [-d <debug_bits>]]\n\n"
00084                         "Start autoexposure daemon, bind it to bit <daemon_bit_number> (0..31) in P_DAEMON_EN (ELPHEL_DAEMON_EN in PHP)\n"
00085                         "Optional debug_bits (hex number) enable different groups of debug messages (1 group per bit) to stderr\n\n";
00086 
00087   if (argc < 2) {
00088      daemon_bit=DAEMON_BIT_AUTOEXPOSURE;
00089   } else if ((argc < 3) || (strcasecmp(argv[1], "-b"))) {
00090      printf (usage,argv[0]);
00091      return 0;
00092   }
00093   if ((argc >=5) && (strcasecmp(argv[3], "-d")==0)) {
00094     autoexposure_debug=strtol(argv[4], NULL, 16);
00095   } else autoexposure_debug=1;
00096 
00097   daemon_bit=strtol(argv[2], NULL, 10);
00098   if ((daemon_bit<0) || (daemon_bit>31)) {printf ("Invalid bit number %d (should be 0..31)\n", daemon_bit); exit (1);}
00099   fprintf(stderr,"autoexposure started, daemon_bit=0x%x, debug=0x%x\n",daemon_bit,autoexposure_debug);
00100 //  MDF1(fprintf(stderr,"\n"));
00101   if (initFilesMmap()<0) exit (1); 
00102   if (autoexposure_debug <0) { 
00103     GLOBALPARS(G_DEBUG)=0;
00104     exit (0);
00105   }
00106   MDF0(fprintf(stderr,"autoexposure started, daemon_bit=0x%x, debug=0x%x\n",daemon_bit,autoexposure_debug));
00108   while (1) { 
00109     if (initParams(daemon_bit)<0) exit (1); 
00110 
00111 
00112   while (1) {
00113 
00114   MDF3(fprintf(stderr,"G_AE_INTEGERR=0x%08lx\n",GLOBALPARS(G_AE_INTEGERR))); 
00115 
00116       this_frame=GLOBALPARS(G_THIS_FRAME); 
00117       lseek(fd_histogram_cache, LSEEK_DAEMON_HIST_Y+daemon_bit, SEEK_END);   
00118       if (GLOBALPARS(G_THIS_FRAME) != this_frame) {
00121        if (initParams(daemon_bit)<0) exit (1); 
00122       }
00124       if (GLOBALPARS(G_HIST_DIM_01)==0xffffffff)  {
00125         rslt=recalibrateDim();
00126         MDF1(fprintf(stderr,"G_HIST_DIM_01: 0x%08lx, G_HIST_DIM_23: 0x%08lx, recalibrateDim()->%d\n",GLOBALPARS(G_HIST_DIM_01),GLOBALPARS(G_HIST_DIM_23),rslt));
00127         this_frame=GLOBALPARS(G_THIS_FRAME);
00128       }
00130       if ((hdr_mode= get_imageParamsThis(P_HDR_DUR))) {
00131         skipHDR(hdr_mode,this_frame);
00132       }
00133       exp_ahead=get_imageParamsThis(P_EXP_AHEAD);
00134       if (!exp_ahead) exp_ahead = 3;
00135    old_vexp=framePars[(this_frame+exp_ahead)  & PARS_FRAMES_MASK].pars[P_VEXPOS];
00136    old_that_vexpos= get_imageParamsThatValid(P_VEXPOS, this_frame-1);
00137   MDF3(fprintf(stderr,"G_AE_INTEGERR=0x%08lx\n",GLOBALPARS(G_AE_INTEGERR))); 
00138       if (((ae_rslt=aexpCorr(COLOR_Y_NUMBER, this_frame, this_frame+exp_ahead )))<0) break; 
00139   MDF3(fprintf(stderr,"G_AE_INTEGERR=0x%08lx\n",GLOBALPARS(G_AE_INTEGERR))); 
00140 
00141 //GLOBALPARS(G_AE_INTEGERR)
00142       vexp= framePars[(this_frame+exp_ahead)  & PARS_FRAMES_MASK].pars[P_VEXPOS];
00143       next_frame=GLOBALPARS(G_NEXT_AE_FRAME);
00144       if (ae_rslt>0) MDF1(fprintf(stderr,"aexpCorr(0x%x, 0x%lx, 0%lx) -> %d, VEXPOS will be 0x%lx (0x%lx , 0x%lx), next_frame=0x%lx\n", COLOR_Y_NUMBER,this_frame,this_frame+exp_ahead,ae_rslt,vexp,old_vexp, old_that_vexpos,next_frame));
00146       if (((rslt=whiteBalanceCorr(this_frame, this_frame+exp_ahead, ae_rslt )))<0) break;  
00147 
00148       if (rslt>0) MDF1(fprintf(stderr,"whiteBalanceCorr(0x%lx, 0%lx) -> %d\n",this_frame,this_frame+exp_ahead,rslt));
00149 
00151       if (((rslt=exposureHDR(this_frame, this_frame+exp_ahead )))<0) break;  
00152       if (rslt>0) MDF1(fprintf(stderr,"exposureHDR(0x%lx, 0%lx) -> %d\n",this_frame,this_frame+exp_ahead,rslt));
00153       if (rslt>0) next_frame=this_frame+4;
00154   MDF3(fprintf(stderr,"G_AE_INTEGERR=0x%08lx\n",GLOBALPARS(G_AE_INTEGERR))); 
00155 
00156 
00157       waitRequstPrevHist(next_frame);
00158 
00159 
00160 
00163 /*
00164         perc=getPercentile(next_frame-1,COLOR_Y_NUMBER, framePars[next_frame & PARS_FRAMES_MASK].pars[P_AEXP_FRACPIX], 1 << COLOR_Y_NUMBER);
00165         MDF6(fprintf(stderr,"FRAME: 0x%lx, COLOR: %d, FRACTION: 0x%04lx RESULT:0x%04x, NOW: 0x%lx\n",next_frame-1,COLOR_Y_NUMBER,framePars[next_frame & PARS_FRAMES_MASK].pars[P_AEXP_FRACPIX],perc,GLOBALPARS(G_THIS_FRAME)));
00166 */
00167     }
00168     ELP_FERR(fprintf (stderr,"Restarting autoexposure due to errors, skipping a frame\n"));
00169     lseek(fd_fparmsall, GLOBALPARS(G_THIS_FRAME) + 1+LSEEK_FRAME_WAIT_ABS, SEEK_END);
00170 
00171   }
00172   return 0;
00173 }
00174 

Generated on Fri Nov 28 00:06:21 2008 for elphel by  doxygen 1.5.1