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

ConvertTest.cpp

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002     Phission : 
00003         Realtime Vision Processing System
00004     
00005     Copyright (C) 2003 Philip D.S. Thoren (pthoren@cs.uml.edu)
00006     University of Massachusetts at Lowell,
00007     Laboratory for Artificial Intelligence and Robotics
00008     
00009     This file is part of Phission.
00010 
00011  ---------------------------------------------------------------------------*/
00012 #include <ConvertTest.h>
00013 #include <phission.h>
00014 
00015 #define CONVERT_TO_VAL phImageHSV24
00016 
00017 /* ------------------------------------------------------------------------ */
00018 int glbl_disable_displays = 0;
00019 int glbl_do_timing = 0;
00020 int glbl_use_images = 0;
00021 
00022 /* ------------------------------------------------------------------------ */
00023 void usage()
00024 {
00025     printf("\n\nUsage:\n");
00026     printf("\t--help            Display usage\n");
00027     printf("\t--nodisplay       Disable the allocation, opening or any use of a display.\n");
00028     printf("\t--test            Runs all convert testing once instead of looping\n");
00029     printf("\t--images <value>  0:don't use images | >=1:use images\n");
00030     printf("\n\n");
00031 
00032     exit(1);
00033 }
00034 
00035 
00036 /* ------------------------------------------------------------------------ */
00037 /* ------------------------------------------------------------------------ */
00038 int main(int argc, char *argv[] )
00039 {
00040     phFUNCTION("main")
00041 
00042     char            *capture_path   = NULL;
00043     int             channel         = 0;
00044     
00045     /* Display variables */
00046     uint32_t            i           = 0;
00047     uint32_t            nDisplays   = 0;
00048     int                 displaysOpen= 1;
00049     char                title[255];
00050     phDisplayInterface  **display   = NULL;
00051     phImage             input_image;
00052     phImage             temp_image;
00053 
00054     /* convert filter and associated variables */
00055     uint32_t        j                       = 0;
00056     int32_t         y                       = 0;
00057     int32_t         z                       = -1;
00058     uint32_t        processed_image_count   = 10;
00059     int             nConverts               = 2;
00060     convert_Filter  *convert_from           = new convert_Filter();
00061     convert_Filter  *convert_to             = new convert_Filter();
00062     int             saved_one               = 0;
00063 
00064     /* Used to read in test image for converting */
00065     uint32_t        k           = 0;
00066     const uint32_t  file_image_count = 2;
00067     phImage         *file_images    = new phImage[file_image_count];
00068     char            filename[255];
00069     
00070 #ifdef WIN32
00071     VFWSource       *capture = new VFWSource();
00072 #else
00073     V4LCapture      *capture = new V4LCapture();
00074 #endif
00075 
00076     phPipeline      *pipeline = new phPipeline();
00077         
00078     /* Utility class to ease the starting and stopping of displays, captures and
00079      * pipelines */
00080     int             done        = 0;
00081     int             first_time  = 1;
00082     phSystem        system;
00083    
00084     /* Remove the code below when using this code as an example.
00085      * 
00086      * This just checks whether "--test" has been specified with
00087      * a time value argument. It's for testing all the examples
00088      * without the need for human intervention. */
00089     int             test = 0;
00090 
00091     phArgTable      *arg_parser = new phArgTable();
00092 
00093     rc = arg_parser->add("--test",&test,phARG_BOOL);
00094     phCHECK_RC(rc,NULL,"arg_parser->add");
00095     
00096     rc = arg_parser->add("--images",&glbl_use_images,phARG_INT);
00097     phCHECK_RC(rc,NULL,"arg_parser->add");
00098     
00099     rc = arg_parser->add("--nodisplay",&glbl_disable_displays,phARG_BOOL);
00100     phCHECK_RC(rc,NULL,"arg_parser->add");
00101 
00102     rc = arg_parser->add("--help",(void *)&usage,phARG_FUNC);
00103     phCHECK_RC(rc,NULL,"arg_parser->add");
00104     
00105     rc = arg_parser->add("--path",&capture_path,phARG_CHAR);
00106     phCHECK_RC(rc,NULL,"arg_parser->add");
00107 
00108     rc = arg_parser->add("--channel",&channel,phARG_INT);
00109     phCHECK_RC(rc,NULL,"arg_parser->add");
00110     
00111     rc = arg_parser->add("--timing",&glbl_do_timing,phARG_BOOL);
00112     phCHECK_RC(rc,NULL,"arg_parser->add");
00113     
00114     rc = arg_parser->parse(argc,argv);
00115     phCHECK_RC(rc,NULL,"arg_parser->parse");
00116 
00117     nDisplays = ((glbl_use_images == 1) ? 1 : 1);
00118 
00119     if (glbl_disable_displays) nDisplays = 0;
00120     if (test) glbl_do_timing = 1;
00121     
00122     if (glbl_do_timing)
00123     {
00124         /* we need to do alot of conversions to gather an accurate average */
00125         processed_image_count *= 10;
00126     }
00127     
00128     if (glbl_use_images == 1)
00129     {
00130         /* load the images */
00131         for (i = 0; i < file_image_count; i++)
00132         {
00133             sprintf(filename,"../images/thisisatest_%u.ppm",i);
00134             rc = file_images[i].load(filename);
00135             phCHECK_RC(rc,NULL,"image loading failed");
00136         }
00137     
00138         /* listen to the input_image object when using preloaded
00139          * images */
00140         rc = pipeline->setLiveSourceInput(&input_image);
00141         phPRINT_RC(rc,NULL,"pipeline->setLiveSourceInput");
00142     }
00143     else
00144     {
00145         capture->set(320,240,capture_path);
00146         capture->setChannel(channel);
00147         
00148         rc = system.addCapture(capture);
00149         phPRINT_RC(rc,NULL,"system.addCapture(capture)");
00150    
00151         /* listen to the capture object when not using images */
00152         rc = pipeline->setLiveSourceInput(capture->getLiveSourceOutput());
00153         phPRINT_RC(rc,NULL,"pipeline->setLiveSourceInput");
00154     }
00155 
00156     /* Connect to the output from the pipeline so we can wait for images
00157      * to go through the pipeline synchronously */
00158     rc = temp_image.connect(pipeline->getLiveSourceOutput());
00159     phCHECK_RC(rc,NULL,"temp_image.connect");
00160     
00161     display = new phDisplayInterface *[nDisplays];
00162     phCHECK_NULLPTR(display,"new","new phDisplayInterface *[nDisplays];");
00163     
00164     for (i = 0; i < nDisplays; i++ )
00165     {
00166         sprintf(title,"ConvertTest[%u]-%s",i,
00167                 (i==0)?"converted" : (i==1) ? "capture" : "unknown");
00168 #if defined(PH_HAVE_X11)        
00169         display[i] = new X11Display(320,240,title);
00170 #elif defined(PH_HAVE_GDI)
00171         display[i] = new GDIDisplay(320,240,title);
00172 #endif
00173 
00174         rc = system.addDisplay(display[i]);
00175         phPRINT_RC(rc,NULL,"system.addDisplay(display[i:%d])",i);
00176     }
00177     
00178     /* <> Attach the displays to the live sources */
00179     /* Image -> (b)phPipeline -> (a)Processed Output Display */
00180     
00181     /* <> Attach the Pipeline output image to the display so the
00182      * display updates when a processing has been completed
00183      * for each loop */
00184     if (nDisplays > 0)
00185         display[0]->setLiveSourceInput(pipeline->getLiveSourceOutput());
00186     if (nDisplays > 1)
00187         display[1]->setLiveSourceInput(capture->getLiveSourceOutput());
00188 
00189     rc = pipeline->add(convert_from);
00190     phCHECK_RC(rc,NULL,"pipeline->add");
00191     
00192     if (nConverts > 1)
00193     {
00194         rc = pipeline->add(convert_to);
00195         phCHECK_RC(rc,NULL,"pipeline->add");
00196     }
00197 
00198     /* Since timing in the pipeline is disabled by default, enable it */
00199     if (glbl_do_timing)
00200     {
00201         rc = pipeline->enableTiming();
00202         phPRINT_RC(rc,NULL,"pipeline->enableTiming()");
00203     }
00204     
00205     rc = system.addPipeline(pipeline);
00206     phCHECK_RC(rc,NULL,"system.addPipeline");
00207     
00208     /* <> Keep going until all the windows are closed */
00209     displaysOpen = 1;
00210     while (displaysOpen)
00211     {
00212         saved_one = 0;
00213         
00214         /* Make sure the pipeline is stopped so we can change both
00215          * the _from _to formats */
00216         if ((pipeline->isPipelineActive()) && (!first_time))
00217         {
00218             rc = pipeline->stopPipeline();
00219             phPRINT_RC(rc,NULL,"pipeline->stopPipeline()");
00220         }
00221         
00222         /* increment through the _to formats */
00223         /* phImageFormatCount - 1 because InvalidFormat == format */
00224         if (nConverts > 1)
00225         {
00226             z++;
00227             if (z >= (phImageFormatCount - 1))
00228             {
00229                 z = 0;
00230                 y++;
00231             }
00232         }
00233         else if (first_time == 0)
00234         {
00235             y++;
00236         }
00237         
00238         /* Increment through the _from formats */
00239         if (y >= (phImageFormatCount - 1))
00240         {
00241             y = 0;
00242             if (test > 0) done = 1;
00243         }
00244 
00245         /* The image format is a bit flag so we can do this */
00246         rc = convert_from->set((1<<y));
00247         phPRINT_RC(rc,NULL,"convert_from->set");
00248         
00249         rc = convert_to->set((1<<z));
00250         phPRINT_RC(rc,NULL,"convert_to->set");
00251 
00252         if (nConverts > 1)
00253         {
00254             phPROGRESS("Testing: cap:%s:0x%08x -> %s:0x%08x -> %s:0x%08x\n",
00255                     phImageFormatToString(capture->getFormat()),
00256                     capture->getFormat(),
00257                     phImageFormatToString((1<<y)),
00258                     (1<<y),
00259                     phImageFormatToString((1<<z)),
00260                     (1<<z));                 
00261         }
00262         else
00263         {
00264             phPROGRESS("Testing: cap:%s:0x%08x -> %s:0x%08x\n",
00265                      phImageFormatToString(capture->getFormat()),
00266                      capture->getFormat(),
00267                      phImageFormatToString((1<<y)),
00268                      (1<<y));
00269         }
00270         
00271         if (glbl_do_timing)
00272         {
00273             /* Reset the average calculations */
00274             rc = pipeline->resetAverage();
00275             phPRINT_RC(rc,NULL,"pipeline->resetAverage()");
00276         }
00277         
00278         /* Make sure the pipeline is started so the conversions
00279          * can be tested */
00280         if ((pipeline->isPipelineActive() == 0) && (!first_time))
00281         {
00282             rc = pipeline->startPipeline();
00283             phPRINT_RC(rc,NULL,"pipeline->startPipeline()");
00284         }
00285         /* This is likely the first time through, so start the system
00286          * after configuring the convert filters; unless something
00287          * broke elsewhere, this should work */
00288         else
00289         {
00290             /* <> Startup the system */
00291             rc = system.startup();
00292             phPRINT_RC(rc,NULL,"system.startup()");
00293 
00294             first_time = 0;
00295         }
00296         
00297         /* wait for at least N images to be processed from the pipeline
00298          * before going to the next conversion */
00299         for (j = 0; (j < processed_image_count) && (displaysOpen); j++ )
00300         {
00301             if (glbl_use_images == 1)
00302             {
00303                 /* Copy images into the input_image variable which the
00304                  * pipeline is listening to */
00305                 rc = input_image.setImage(file_images[k]);
00306                 phPRINT_RC(rc,NULL,"setImage failed");
00307                 if (rc != 0) { displaysOpen = 0; continue; }
00308             
00309                 /* loops through the preloaded images */
00310                 k++;
00311                 if (k >= file_image_count)
00312                 {
00313                     k = 0;
00314                 }
00315             }
00316            
00317             /* wait for the output from the pipeline */
00318             rc = temp_image.update();
00319             phPRINT_RC(rc,NULL,"temp_image.update failed.");
00320 
00321             if (saved_one == 0)
00322             {
00323                 if (nConverts > 1)
00324                 {
00325                     sprintf(filename,"cap_%s_to_%s_to_%s.ppm",
00326                             phImageFormatToString(capture->getFormat()),
00327                             phImageFormatToString((1<<y)),
00328                             phImageFormatToString((1<<z)));
00329                 }
00330                 else
00331                 {
00332                     sprintf(filename,"cap_%s_to_%s.ppm",
00333                             phImageFormatToString(capture->getFormat()),
00334                             phImageFormatToString((1<<y)));
00335                 }
00336 
00337                 rc = temp_image.save(filename);
00338                 phPRINT_RC(rc,NULL,"temp_image.save");
00339 
00340                 saved_one = 1;
00341             }
00342             
00343             /* Check the displays to see if they are all closed */
00344             /* we need this extra check here to cancel this loop 
00345              * early */
00346             if (nDisplays > 0)
00347             {
00348                 displaysOpen = 0;
00349                 for (i = 0; (i < nDisplays) && (displaysOpen == 0); i++)
00350                 {
00351                     if (display[i]->isOpen() == 1)
00352                     {
00353                         displaysOpen = 1;
00354                     }
00355                 }
00356             }
00357             if ((test > 0) && done)
00358                 displaysOpen = 0;
00359             else
00360                 displaysOpen = 1;
00361         }     
00362 
00363         if (glbl_do_timing)
00364         {
00365             /* Report the average using the phTimeStamp report facility
00366              * to allow for portable/robust reporting */
00367             pipeline->averageRuntime().report("ConvertTest - Filtering Average");
00368             phPRINT_RC(rc,NULL,"pipeline->averageRuntime()");
00369         }
00370         
00371         /* Check the displays to see if they are all closed */
00372         if (nDisplays > 0)
00373         {
00374             displaysOpen = 0;
00375             for (i = 0; (i < nDisplays) && (displaysOpen == 0); i++)
00376             {
00377                 if (display[i]->isOpen() == 1)
00378                 {
00379                     displaysOpen = 1;
00380                 }
00381             }
00382         }
00383         /* Remove this if block when using this code as an example */
00384         /* Set the loop control value to end the loop when testing */
00385         if ((test > 0) && done)
00386             displaysOpen = 0;
00387         else
00388             displaysOpen = 1;
00389     }
00390 
00391 
00392 error:
00393     /* <> Shutdown the system */
00394     rc = system.shutdown();
00395     phPRINT_RC(rc,NULL,"system.shutdown()");
00396 
00397     /* just good coding; disconnect the connected image object */
00398     rc = temp_image.disconnect();
00399     phPRINT_RC(rc,NULL,"temp_image.disconnect() failed.");
00400 
00401     phDeleteArray(file_images);
00402     
00403     phDelete(pipeline);
00404     phDelete(convert_from);
00405     phDelete(convert_to);
00406     
00407     for (i = 0; (i < nDisplays) && (display != NULL); i++)
00408     {
00409         phDelete(display[i]);
00410     }
00411     
00412     phDeleteArray(display);
00413 
00414     phDelete(capture);
00415     
00416     phFree(capture_path);
00417     phDelete(arg_parser);
00418 
00419     return phSUCCESS;
00420 }




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:04 2007 for phission by  doxygen 1.4.4