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

image_collection.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 <phission.h>
00013 
00014 #include <image_collection.h>
00015 #include <phStdint.h> /* if stdint.h doesn't come till after phission.h,
00016                        then __STDC_LIMIT_MACROS won't be defined and thus 
00017                        UINT32_MAX won't be defined */
00018 #include <errno.h>
00019 
00020 #if USE_FILETYPE
00021 #else
00022     #include <sys/file.h>
00023     #include <sys/types.h>
00024     #include <sys/stat.h>
00025     #include <fcntl.h>
00026     #include <unistd.h>
00027 #endif
00028 
00029 
00030 #ifdef __cplusplus
00031 extern "C" {
00032 #endif
00033 
00034 /* ------------------------------------------------------------------------ */
00035 typedef struct image_data_set_t
00036 {
00037     /* loaded during reading */
00038     char       *filedir;
00039     char       *fileprefix;
00040     char       *filetype;
00041     uint32_t    nfiles; /* indexed from 0 */
00042     int32_t     hue_adjust;
00043     int32_t     sat_adjust;
00044     int32_t     val_adjust;
00045     uint32_t    x1;
00046     uint32_t    y1;
00047     uint32_t    x2;
00048     uint32_t    y2;
00049     
00050     char      **tags;
00051     uint32_t    tags_size;
00052     uint32_t    ntags;
00053 
00054     /* set up after all the image data sets have been read */
00055     uint32_t   *tag_ids;
00056     
00057     uint32_t    width;
00058     uint32_t    height;
00059     float       ratio;
00060 };
00061 
00062 /* ------------------------------------------------------------------------ */
00063 typedef struct image_data_collection_t
00064 {
00065     uint32_t                    nsets;
00066     struct image_data_set_t    *images;
00067     uint32_t                    collection_size;
00068     
00069     int     width;
00070     int     height;
00071     char   *outdir;
00072 
00073     char      **tags;
00074     uint32_t    tags_size;
00075     uint32_t    ntags;
00076 };
00077 
00078 /* ------------------------------------------------------------------------ */
00079 static int check_and_add_tags( char ***tags, uint32_t *ntags, uint32_t *arrsize,
00080                                char **newtags, uint32_t n_newtags )
00081 {
00082     phFUNCTION("check_and_add_tags")
00083     uint32_t    x           = 0;
00084     uint32_t    y           = 0;
00085     uint32_t    i           = 0;
00086     int         tag_found   = 0;
00087     uint32_t    index       = 0;
00088     char        *tmp_tag    = NULL;
00089 
00090     //phPRINT("%p %p %p %p %d\n",tags,ntags,arrsize,newtags,n_newtags);
00091     
00092     for (x = 0; x < n_newtags; x++ )
00093     {
00094         tag_found = 0;
00095 
00096         /* search for the tag in the data collection list */
00097         for (y = 0; (y < *ntags) && (tag_found == 0); y++ )
00098         {
00099             if (strcasecmp((*tags)[y],newtags[x]) == 0)
00100             {
00101                 tag_found = 1;
00102             }
00103         }
00104         if ((strcasecmp(newtags[x],"unknown") != 0) &&
00105             (tag_found == 0))
00106         {
00107             tmp_tag = (char *)phCalloc(strlen(newtags[x])+5,sizeof(char));
00108             phCHECK_PTR(tmp_tag,"phCalloc","phCalloc failed");
00109 
00110             sprintf(tmp_tag,"%s",newtags[x]);
00111 
00112             (*ntags)++;
00113 
00114             /* allocate a new location by resizing it with phDALLOC_RESIZE */
00115             phDALLOC_RESIZE((*tags),(*arrsize),(*ntags),char *);
00116             (*tags)[(*ntags)-1] = NULL;
00117 
00118             /* find the right place in the list (alphabetically) for the
00119              * new tag */
00120             for (index = 0; 
00121                  /* The last array location is new, so we don't need to go
00122                   * beyond that location */
00123                  (index < (*ntags-1)) && 
00124                  /* The last index will be null, this is a double check */
00125                  ((*tags)[index] != NULL) && 
00126                  /* while the string is alphabetically greater than the
00127                   * current string indexed */
00128                  (strcasecmp(tmp_tag,(*tags)[index]) > 0 );
00129                  /* next location */
00130                  index++ );
00131             
00132             if (index != ((*ntags)-1)) 
00133             {
00134                 /*
00135                 // Here's an interesting lesson. If you do this, it won't
00136                 // work because it will copy the same byte from location to
00137                 // location because the memory segments overlap.
00138                 phMemcpy(&((*tags)[index+1]),
00139                          &((*tags)[index]),
00140                          sizeof(char *) * ((*ntags)-(index+1)));
00141                 */
00142                 // However, if you use memmove, it will work because it
00143                 // checks if the memory segments are overlapped
00144                 phMemmove(&((*tags)[index+1]),
00145                           &((*tags)[index]),
00146                           sizeof(char *) * ((*ntags)-(index+1)));
00147             }
00148             /* keep the new string in the array */
00149             (*tags)[index] = tmp_tag;
00150         }
00151     }
00152     return phSUCCESS;
00153 error:
00154     return phFAIL;
00155 }
00156 
00157 /* ------------------------------------------------------------------------ */
00158 static int create_tag_index_array( char        **alltags, 
00159                                    uint32_t    n_alltags,
00160                                    char        **tags,
00161                                    uint32_t    ntags,
00162                                    uint32_t    **index_array )
00163 {
00164     phFUNCTION("create_tag_index_array")
00165     uint32_t i      = 0; 
00166     uint32_t j      = 0; 
00167     uint32_t done   = 0;
00168 
00169     (*index_array) = (uint32_t *)phCalloc(ntags,sizeof(uint32_t));
00170     phCHECK_PTR((*index_array),"phCalloc", "phCalloc failed.");
00171 
00172     for ( i = 0; i < ntags; i++ )
00173     {
00174         done = 0;
00175         for ( j = 0; (j < n_alltags) && (done == 0); j++ )
00176         {
00177             if (strcasecmp(alltags[j],tags[i]) == 0)
00178             {
00179                 (*index_array)[i] = j;
00180                 done = 1;
00181             }
00182         }
00183     }
00184 
00185     return phSUCCESS;
00186 error:
00187     return phFAIL;
00188 }
00189 
00190 /* ------------------------------------------------------------------------ */
00191 int idc_new( struct image_data_collection_t **data )
00192 {
00193     phFUNCTION("idc_new")
00194 
00195     /* Check the arguments */
00196     phCHECK_PTR(data,NULL,"data == NULL");
00197     if (*data != NULL) return phFAIL;
00198 
00199     /* allocate the database */
00200     *data = (struct image_data_collection_t *)
00201                 phCalloc(1,sizeof(struct image_data_collection_t));
00202     phCHECK_PTR(*data,"phCalloc","*data == NULL");
00203 
00204     (*data)->nsets      = 0;
00205     (*data)->images     = NULL;
00206     (*data)->width      = 0;
00207     (*data)->height     = 0;
00208     (*data)->outdir     = NULL;
00209 
00210     (*data)->tags       = NULL;
00211     (*data)->tags_size  = 0;
00212     (*data)->ntags      = 0;
00213 
00214     return phSUCCESS;
00215 error:
00216     return phFAIL;
00217 }
00218 
00219 /* ------------------------------------------------------------------------ */
00220 static struct image_data_set_t *idc_new_data_set( 
00221                                     struct image_data_collection_t   *data,
00222                                     const char                       *str )
00223 {
00224     phFUNCTION("idc_new_data_set")
00225 
00226     int     nfields         = 0;
00227 
00228     char    *filedir        = NULL;
00229     char    *fileprefix     = NULL;
00230     char    *filetype       = NULL;
00231     uint32_t nfiles         = 0;
00232     uint32_t x1, y1, x2, y2 = 0;
00233     int32_t hue, sat, val   = 0;
00234     char    *h              = NULL;
00235     char    *s              = NULL;
00236     char    *v              = NULL;
00237     char    *x1_b           = NULL;
00238     char    *y1_b           = NULL;
00239     char    *x2_b           = NULL;
00240     char    *y2_b           = NULL;
00241     
00242     char    *tag_str        = NULL;
00243 
00244     uint32_t    done    = 0;
00245     uint32_t    i       = 0;
00246     uint32_t    count   = 0;
00247     char       *tmp_tag    = NULL;
00248 
00249     struct image_data_set_t *new_image_data_set = NULL;
00250 
00251     filedir  = (char *)phCalloc(255,sizeof(char));
00252     phCHECK_PTR(filedir,"phCalloc","phCalloc failed.");
00253 
00254     fileprefix  = (char *)phCalloc(255,sizeof(char));
00255     phCHECK_PTR(fileprefix,"phCalloc","phCalloc failed.");
00256 
00257     filetype  = (char *)phCalloc(255,sizeof(char));
00258     phCHECK_PTR(filetype,"phCalloc","phCalloc failed.");
00259 
00260     h = (char *)phCalloc(255,sizeof(char));
00261     phCHECK_PTR(h,"phCalloc","phCalloc failed.");
00262 
00263     s = (char *)phCalloc(255,sizeof(char));
00264     phCHECK_PTR(s,"phCalloc","phCalloc failed.");
00265 
00266     v = (char *)phCalloc(255,sizeof(char));
00267     phCHECK_PTR(v,"phCalloc","phCalloc failed.");
00268 
00269     x1_b = (char *)phCalloc(255,sizeof(char));
00270     phCHECK_PTR(x1_b,"phCalloc","phCalloc failed.");
00271 
00272     y1_b = (char *)phCalloc(255,sizeof(char));
00273     phCHECK_PTR(y1_b,"phCalloc","phCalloc failed.");
00274 
00275     x2_b = (char *)phCalloc(255,sizeof(char));
00276     phCHECK_PTR(x2_b,"phCalloc","phCalloc failed.");
00277 
00278     y2_b = (char *)phCalloc(255,sizeof(char));
00279     phCHECK_PTR(y2_b,"phCalloc","phCalloc failed.");
00280 
00281     tag_str = (char *)phCalloc(255,sizeof(char));
00282     phCHECK_PTR(tag_str,"phCalloc","phCalloc failed.");
00283 
00284     nfields = sscanf(   str,
00285                         "%s %s %s %u %s %s %s %s %s %s %s %s",
00286                         filetype,
00287                         filedir,
00288                         fileprefix,
00289                         &nfiles,
00290                         h,s,v,
00291                         x1_b, 
00292                         y1_b,
00293                         x2_b,
00294                         y2_b,
00295                         tag_str);
00296     if (nfields == 12)
00297     {
00298         data->nsets++;
00299         phDALLOC_RESIZE(data->images,
00300                         data->collection_size,
00301                         data->nsets,
00302                         struct image_data_set_t );
00303 
00304         new_image_data_set = &(data->images[data->nsets - 1]);
00305         new_image_data_set->filetype    = NULL;
00306         new_image_data_set->filedir     = NULL;
00307         new_image_data_set->fileprefix  = NULL;
00308         new_image_data_set->nfiles      = 0;
00309         new_image_data_set->hue_adjust  = 0;
00310         new_image_data_set->sat_adjust  = 0;
00311         new_image_data_set->val_adjust  = 0;
00312         new_image_data_set->x1          = 0;
00313         new_image_data_set->y1          = 0;
00314         new_image_data_set->x2          = 0;
00315         new_image_data_set->y2          = 0;
00316         new_image_data_set->ntags       = 0;
00317         new_image_data_set->tags        = NULL;
00318         new_image_data_set->tags_size   = 0;
00319 
00320         /* file type */
00321         new_image_data_set->filetype = 
00322             (char *)phCalloc(strlen(filetype)+5, sizeof(char));
00323         phCHECK_PTR(new_image_data_set->filetype,"phCalloc","phCalloc failed.");
00324 
00325         sprintf( new_image_data_set->filetype, "%s", filetype);
00326 
00327         /* file dir */
00328         new_image_data_set->filedir = 
00329             (char *)phCalloc(strlen(filedir)+5,sizeof(char));
00330         phCHECK_PTR(new_image_data_set->filedir,"phCalloc","phCalloc failed.");
00331 
00332         sprintf( new_image_data_set->filedir, "%s", filedir);
00333 
00334         /* file prefix */
00335         new_image_data_set->fileprefix = 
00336             (char *)phCalloc(strlen(fileprefix)+5,sizeof(char));
00337         phCHECK_PTR(new_image_data_set->fileprefix,"phCalloc","phCalloc failed.");
00338 
00339         sprintf( new_image_data_set->fileprefix, "%s", fileprefix);
00340 
00341         hue = (int32_t)atoi(h);
00342         sat = (int32_t)atoi(s);
00343         val = (int32_t)atoi(v);
00344         
00345         /* Set up the x and y coordinates of the object in the original
00346          * images */
00347         if (strcasecmp(x1_b,"min") == 0)
00348         {
00349             x1 = 0;
00350         }
00351         else
00352         {
00353             x1 = (uint32_t)atoi(x1_b);
00354         }
00355         if (strcasecmp(y1_b,"min") == 0)
00356         {
00357             y1 = 0;
00358         }
00359         else
00360         {
00361             y1 = (uint32_t)atoi(y1_b);
00362         }
00363          
00364         if (strcasecmp(x2_b,"max") == 0)
00365         {
00366             x2 = UINT32_MAX;
00367         }
00368         else
00369         {
00370             x2 = (uint32_t)atoi(x2_b);
00371         }
00372         
00373         if (strcasecmp(y2_b,"max") == 0)
00374         {
00375             y2 = UINT32_MAX;
00376         }
00377         else
00378         {
00379             y2 = (uint32_t)atoi(y2_b);
00380         }
00381 
00382         new_image_data_set->nfiles  = nfiles;
00383         new_image_data_set->hue_adjust = hue;
00384         new_image_data_set->sat_adjust = sat;
00385         new_image_data_set->val_adjust = val;
00386         new_image_data_set->x1      = x1;
00387         new_image_data_set->y1      = y1;
00388         new_image_data_set->x2      = x2;
00389         new_image_data_set->y2      = y2;
00390         new_image_data_set->width   = x2 - x1;
00391         new_image_data_set->height  = y2 - y1;
00392         new_image_data_set->ratio   = 
00393                 (float)new_image_data_set->width / (float)new_image_data_set->height;
00394 
00395         /* Process the tag string */
00396         i = 0;
00397         count = 0;
00398         while (!done)
00399         {
00400             if (((tag_str[i] == ',') || (tag_str[i] == '\0')) && (count > 0))
00401             {
00402                 /* allocate a new string for the tag */
00403                 tmp_tag = (char *)phCalloc(count+1,sizeof(char));
00404                 phCHECK_PTR(tmp_tag,"phCalloc","phCalloc failed.");
00405                 
00406                 /* copy the string from the tag str */
00407                 /*
00408                 phPRINT("%d %d %s:%c:\n",
00409                         i,
00410                         count,
00411                         &(tag_str[i-count]),
00412                         tag_str[i]);
00413                  */
00414                 snprintf(tmp_tag,count+1,"%s",&(tag_str[i-count]));
00415 
00416                 /* increment the total tags for this set */
00417                 new_image_data_set->ntags++;
00418                 
00419                 /* Resize the pointer of arrays */
00420                 phDALLOC_RESIZE(new_image_data_set->tags,
00421                                 new_image_data_set->tags_size,
00422                                 new_image_data_set->ntags,
00423                                 char *);
00424 
00425                 /* Add the new string to the array */
00426                 new_image_data_set->tags[new_image_data_set->ntags-1] = tmp_tag;
00427                 
00428                 if (tag_str[i] == '\0') done = 1;
00429 
00430                 count = 0;
00431                 i++;
00432             }
00433             else
00434             {
00435                 count++;
00436                 i++;
00437             }
00438         }
00439     }
00440     else
00441     {
00442         phPROGRESS("nfields:%d < 12\n",nfields);
00443     }
00444 
00445     phFree(fileprefix);
00446     phFree(filedir);
00447     phFree(filetype);
00448     phFree(h);
00449     phFree(s);
00450     phFree(v);
00451     phFree(x1_b);
00452     phFree(y1_b);
00453     phFree(x2_b);
00454     phFree(y2_b);
00455     phFree(tag_str);
00456 
00457     return new_image_data_set;
00458 
00459 error:
00460     phFree(fileprefix);
00461     phFree(filedir);
00462     phFree(filetype);
00463     phFree(h);
00464     phFree(s);
00465     phFree(v);
00466     phFree(x1_b);
00467     phFree(y1_b);
00468     phFree(x2_b);
00469     phFree(y2_b);
00470     phFree(tag_str);
00471 
00472     return NULL;
00473 }
00474 
00475 /* ------------------------------------------------------------------------ */
00476 int free_data_set_allocs( struct image_data_set_t *data_set )
00477 {
00478     uint32_t i = 0;
00479 
00480     phFree( data_set->filedir );
00481     phFree( data_set->fileprefix );
00482     phFree( data_set->filetype );
00483 
00484     for (i = 0; i < data_set->ntags; i++ )
00485     {
00486         phFree(data_set->tags[i]);
00487     }
00488     phFree(data_set->tags);
00489     phFree(data_set->tag_ids);
00490    
00491     return phSUCCESS;
00492 }
00493 
00494 
00495 /* ------------------------------------------------------------------------ */
00496 int idc_readfile( const char *filename, 
00497                   struct image_data_collection_t *data )
00498 {
00499     phFUNCTION("idc_readfile")
00500 
00501     intmax_t    frc             = 0;
00502     FILE        *fp             = NULL;
00503     char        *buffer         = NULL;
00504     int         file_finished   = 0;
00505     uint32_t    i               = 0;
00506     int         nfields         = 0;
00507     char        *outdir         = NULL;
00508     
00509     char        *type           = NULL;
00510     int         width, height   = 0;
00511 
00512     struct image_data_set_t *setptr = NULL;
00513 
00514     /* Check the arguments */
00515     phCHECK_PTR(filename,NULL,"filename == NULL");
00516     phCHECK_PTR(data,NULL,"data == NULL");
00517 
00518     /* open the file */
00519     fp = fopen(filename,"r");
00520     phCHECK_PTR(fp,"fopen","fopen(%s,\"r\")",filename);
00521 
00522     buffer      = (char *)phCalloc(255,sizeof(char));
00523     phCHECK_PTR(buffer,"phCalloc","phCalloc failed.");
00524 
00525     type        = (char *)phCalloc(255,sizeof(char));
00526     phCHECK_PTR(type,"phCalloc","phCalloc failed.");
00527     
00528     outdir  = (char *)phCalloc(255,sizeof(char));
00529     phCHECK_PTR(outdir,"phCalloc","phCalloc failed.");
00530 
00531     /* lock the file */
00532     rc = flock(fileno(fp),LOCK_SH);
00533     phPRINT_RC(rc,"flock","flock(%d,LOCK_SH)",fileno(fp));
00534 
00535     while (!file_finished)
00536     {
00537         buffer[0] = '\0';
00538         frc = (intmax_t)fgets(buffer,254,fp);
00539         //phPROGRESS("(%d)buf-%s\n",errno,buffer);
00540         if (frc != EOF)
00541         {
00542             while ((buffer[i] != '\0') && (buffer[i] != '#')) i++;
00543             if (buffer[i] == '#') buffer[i] = '\0';
00544 
00545             /* Make sure we actually read the type from the buffer */
00546             nfields = sscanf(buffer,"%s ",type);
00547             //phPROGRESS("%s\n",type);
00548 
00549             if (nfields > 0)
00550             {
00551                 /* an image set with the location of the object in the
00552                  * image covered by the box (x1,y1)-(x2,y2) */
00553                 if (strcasecmp(type,":end") == 0)
00554                 {
00555                     file_finished = 1;
00556                 }
00557                 else if (strcasecmp(type,"set:") == 0)
00558                 {
00559                     setptr = idc_new_data_set( data, &(buffer[5]) );
00560                     phPRINT_PTR( setptr, NULL, "idc_new_data_set" );
00561 
00562                     if (setptr != NULL)
00563                     {
00564                         rc = check_and_add_tags(&data->tags,
00565                                                 &data->ntags,
00566                                                 &data->tags_size,
00567                                                 setptr->tags,
00568                                                 setptr->ntags );
00569                         phPRINT_RC(rc,NULL,"check_and_add_tags");
00570                     }
00571                 }
00572                 else if (strcasecmp(type,"outdir:") == 0)
00573                 {
00574                     nfields = sscanf( &(buffer[7]),"%s", outdir);
00575                     if (nfields == 1)
00576                     {
00577                         data->outdir = (char *)phCalloc(strlen(outdir)+5,
00578                                                                      sizeof(char));
00579                         phCHECK_PTR(data->outdir,"phCalloc","phCalloc failed.");
00580 
00581                         sprintf(data->outdir,"%s",outdir);
00582                     }
00583                 }
00584                 else if (strcasecmp(type,"size:") == 0)
00585                 {
00586                     nfields = sscanf( &(buffer[6]),"%d %d",
00587                                       &width,
00588                                       &height);
00589                     if (nfields == 2)
00590                     {
00591                         data->width = width;
00592                         data->height = height;
00593                     }
00594                 }
00595             }
00596         }
00597         else
00598         {
00599             file_finished = 1;
00600         }
00601     }
00602     
00603     /* unlock the file */
00604     rc = flock(fileno(fp),LOCK_UN);
00605     phPRINT_RC(rc,"flock","flock(%d,LOCK_UN)",fileno(fp));
00606 
00607     fclose(fp);
00608     fp = NULL;
00609 
00610     /* map the tags to the index locations of the total tags */
00611     for (i = 0; i < data->nsets; i++ )
00612     {
00613         rc = create_tag_index_array( data->tags, data->ntags, 
00614                                      data->images[i].tags,
00615                                      data->images[i].ntags,
00616                                      &(data->images[i].tag_ids));
00617         phPRINT_RC(rc,NULL,"create_tag_index_array");
00618     }
00619     
00620     
00621     phFree(buffer);
00622     phFree(type);
00623     phFree(outdir);
00624 
00625     return phSUCCESS;
00626 error:
00627     if (fp != NULL)
00628     {
00629         fclose(fp);
00630     }
00631 
00632     phFree(buffer);
00633     phFree(type);
00634     phFree(outdir);
00635     
00636     return phFAIL;
00637 }
00638 
00639 /* ------------------------------------------------------------------------ */
00640 int idc_print( struct image_data_collection_t *collection )
00641 {
00642     phFUNCTION("idc_print");
00643     
00644     uint32_t                i       = 0;
00645     uint32_t                j       = 0;
00646     struct image_data_set_t *img    = NULL;
00647 
00648     phPRINT("set count:\t%d\n",
00649             collection->nsets );
00650     phPRINT("output size - w:%d h:%d\n",
00651             collection->width,
00652             collection->height );
00653     phPRINT("outdir:\t\t%s\nnsets:\t\t%d\n", 
00654             collection->outdir,
00655             collection->nsets );
00656     phPRINT( "tags: ");
00657     for ( j = 0; j < collection->ntags; j++ )
00658     {
00659         phPRINT( "%s%s", collection->tags[j], (j == collection->ntags-1) ? "" : ", ");
00660     }
00661     phPRINT("\n");
00662     
00663     for ( i = 0; i < collection->nsets; i++ )
00664     {
00665         img = &collection->images[i];
00666         phPRINT("fileset - \n\tdir:\t%s\n\tname:\t%s\n\ttype:\t%s\n\tnfiles:\t%d\nadjustment( h:%d, s:%d, v:%d )\n\tlocation:( %d, %d, %d, %d )\n",
00667                 img->filedir,
00668                 img->fileprefix,
00669                 img->filetype,
00670                 img->nfiles,
00671                 img->hue_adjust,
00672                 img->sat_adjust,
00673                 img->val_adjust,
00674                 img->x1,
00675                 img->y1,
00676                 img->x2,
00677                 img->y2 );
00678 
00679         phPRINT( "\ttags: ");
00680         for ( j = 0; j < img->ntags; j++ )
00681         {
00682             phPRINT( "%s%s", img->tags[j], (j == img->ntags-1) ? "" : ", ");
00683         }
00684         phPRINT("\n");
00685     }
00686     
00687     phPRINT("\n");
00688     
00689     return phSUCCESS;
00690 error:
00691     return phFAIL;
00692 }
00693 
00694 /* ------------------------------------------------------------------------ */
00695 int idc_crop_resize( struct image_data_collection_t *collection )
00696 {
00697     phFUNCTION("idc_crop_resize");
00698 
00699     phImage                 image;
00700     struct image_data_set_t *img    = NULL;
00701     
00702     uint32_t    j   = 0;
00703     uint32_t    i   = 0;
00704     
00705     char        to_filename[255];
00706     char        from_filename[255];
00707 
00708     uint32_t    to_width    = 0;
00709     uint32_t    to_height   = 0;
00710 
00711     /*
00712     phPRINT("image count: %d\n", collection->nsets );
00713     phPRINT("output size - w:%d h:%d\n", collection->width, collection->height );
00714      */
00715     for ( i = 0; i < collection->nsets; i++ )
00716     {
00717         img = &collection->images[i];
00718 
00719         phPRINT("Processing: %s",img->fileprefix);
00720         
00721         for (j = 0; j < img->nfiles; j++ )
00722         {
00723             phPRINT(".");
00724             /*
00725             img->filetype
00726             img->filedir
00727             img->fileprefix;
00728             img->nfiles;
00729             img->x1;
00730             img->y1;
00731             img->x2;
00732             img->y2;
00733             */
00734             sprintf(from_filename,
00735                     "%s/%s_%04u.%s",
00736                     img->filedir,
00737                     img->fileprefix,
00738                     j,
00739                     img->filetype);
00740             
00741             sprintf(to_filename,
00742                     "%s/%s_%04u.%s",
00743                     collection->outdir,
00744                     img->fileprefix,
00745                     j,
00746                     img->filetype);
00747             
00748             to_width  = collection->width;
00749             to_height = collection->height;
00750             
00751             rc = image.load(from_filename);
00752             
00753             if (rc == phSUCCESS)
00754             {
00755                 if (img->x2 == INT_MAX)
00756                 {
00757                     img->x2 = image.getWidth();
00758                 }
00759                 if (img->y2 == INT_MAX)
00760                 {
00761                     img->y2 = image.getHeight();
00762                 }
00763 
00764                 rc = image.crop(img->x1,img->y1,img->x2,img->y2);
00765                 phCHECK_RC(rc,NULL,"image.crop()");
00766 
00767                 rc = image.resize(to_width,to_height);
00768                 phCHECK_RC(rc,NULL,"image.resize()");
00769 
00770                 rc = image.save(to_filename);
00771                 phCHECK_RC(rc,NULL,"image.save(to_filename)");
00772             }
00773             else
00774             {
00775                 phPROGRESS("%s failed to load\n",from_filename);
00776             }
00777         }
00778         phPRINT("\n");
00779     }
00780     
00781     phPRINT("\n");
00782 
00783     return phSUCCESS;
00784 error:
00785 
00786     return phFAIL;
00787 }
00788 
00789 /* ------------------------------------------------------------------------ */
00790 int idc_free( struct image_data_collection_t **collection )
00791 {
00792     phFUNCTION("idc_free")
00793     uint32_t i = 0;
00794         
00795     phCHECK_PTR(collection,NULL,"collection == NULL");
00796     phCHECK_PTR(*collection,NULL,"collection == NULL");
00797 
00798     if (((*collection)->images) != NULL)
00799     {
00800         for (i = 0; i < (*collection)->nsets; i++ )
00801         {
00802             rc = free_data_set_allocs( &((*collection)->images[i]) );
00803             phPRINT_RC(rc,NULL,"free_data_set_allocs");
00804         }
00805     }    
00806 
00807     for (i = 0; i < (*collection)->ntags; i++ )
00808     {
00809         phFree((*collection)->tags[i]);
00810     }
00811     
00812     phFree((*collection)->tags);
00813     phFree((*collection)->outdir );
00814     phFree((*collection)->images );
00815     phFree((*collection));
00816 
00817     return phSUCCESS;
00818     
00819 error:
00820     return phFAIL;
00821 }
00822 
00823 /* ------------------------------------------------------------------------ */
00824 const char *idc_get_dir( struct image_data_collection_t *data )
00825 {
00826     return data == NULL ? NULL : data->outdir;
00827 }
00828 
00829 /* ------------------------------------------------------------------------ */
00830 uint32_t idc_get_width( struct image_data_collection_t *data )
00831 {
00832     return data == NULL ? 0 : data->width;
00833 }
00834 
00835 /* ------------------------------------------------------------------------ */
00836 uint32_t idc_get_height( struct image_data_collection_t *data )
00837 {
00838     return data == NULL ? 0 : data->height;
00839 }
00840 
00841 /* ------------------------------------------------------------------------ */
00842 uint32_t idc_get_nsets( struct image_data_collection_t *data )
00843 {
00844     return data == NULL ? 0 : data->nsets;
00845 }
00846 
00847 /* ------------------------------------------------------------------------ */
00848 struct image_data_set_t *idc_get_set( struct image_data_collection_t *data,
00849                                       uint32_t index )
00850 {
00851     if ((data == NULL) ||
00852         (data->nsets <= index) ||
00853         (index < 0)) return NULL;
00854 
00855     return &(data->images[index]);
00856 }
00857 
00858 /* ------------------------------------------------------------------------ */
00859 uint32_t idc_get_ntags( struct image_data_collection_t *data )
00860 {
00861     return data == NULL ? 0 : data->ntags;
00862 }
00863 
00864 /* ------------------------------------------------------------------------ */
00865 char **idc_get_tags( struct image_data_collection_t *data )
00866 {
00867     return data == NULL ? 0 : data->tags;
00868 }
00869 
00870 /* ------------------------------------------------------------------------ */
00871 const char *ids_get_filedir     ( struct image_data_set_t *data_set )
00872 {
00873     return (data_set != NULL) ? data_set->filedir : NULL;
00874 }
00875 /* ------------------------------------------------------------------------ */
00876 const char *ids_get_fileprefix  ( struct image_data_set_t *data_set )
00877 {
00878     return (data_set != NULL) ? data_set->fileprefix : NULL;
00879 }
00880 /* ------------------------------------------------------------------------ */
00881 const char *ids_get_filetype    ( struct image_data_set_t *data_set )
00882 {
00883     return (data_set != NULL) ? data_set->filetype : NULL;
00884 }
00885 /* ------------------------------------------------------------------------ */
00886 int   ids_get_filename( struct image_data_set_t *data_set,
00887                         uint32_t                fileid,
00888                         char                  **pfilename )
00889 {
00890     phFUNCTION("ids_get_filename")
00891     char *filename = NULL;
00892     
00893     if (data_set == NULL) return phFAIL;
00894     if (pfilename == NULL) return phFAIL;
00895     
00896     if (*pfilename == NULL)
00897     {
00898         filename = *pfilename = (char *)phCalloc(255,sizeof(char));
00899         phCHECK_PTR(filename,"phCalloc","phCalloc failed.");
00900     }
00901     else
00902     {
00903         filename = *pfilename;
00904     }
00905     if (fileid > data_set->nfiles) fileid = data_set->nfiles - 1;
00906     
00907     sprintf(filename,
00908             "%s/%s_%04u.%s",
00909             data_set->filedir,
00910             data_set->fileprefix,
00911             fileid,
00912             data_set->filetype);
00913 
00914     return phSUCCESS;
00915 error:
00916     return phFAIL;
00917 }
00918 
00919 /* ------------------------------------------------------------------------ */
00920 uint32_t    ids_get_nfiles      ( struct image_data_set_t *data_set )
00921 {
00922     return (data_set != NULL) ? data_set->nfiles : NULL;
00923 }
00924 /* ------------------------------------------------------------------------ */
00925 uint32_t    ids_get_width       ( struct image_data_set_t *data_set )
00926 {
00927     return (data_set != NULL) ? data_set->width : NULL;
00928 }
00929 /* ------------------------------------------------------------------------ */
00930 uint32_t    ids_get_height      ( struct image_data_set_t *data_set )
00931 {
00932     return (data_set != NULL) ? data_set->height : NULL;
00933 }
00934 /* ------------------------------------------------------------------------ */
00935 float       ids_get_ratio       ( struct image_data_set_t *data_set )
00936 {
00937     return (data_set != NULL) ? data_set->ratio : NULL;
00938 }
00939 /* ------------------------------------------------------------------------ */
00940 uint32_t    ids_get_ntags       ( struct image_data_set_t *data_set )
00941 {
00942     return (data_set != NULL) ? data_set->ntags : NULL;
00943 }
00944 /* ------------------------------------------------------------------------ */
00945 char      **ids_get_tags        ( struct image_data_set_t *data_set )
00946 {
00947     return (data_set != NULL) ? data_set->tags : NULL;
00948 }
00949 
00950 /* ------------------------------------------------------------------------ */
00951 uint32_t   *ids_get_tag_ids     ( struct image_data_set_t *data_set )
00952 {
00953     return (data_set != NULL) ? data_set->tag_ids : NULL;
00954 }
00955 
00956 /* ------------------------------------------------------------------------ */
00957 int         ids_get_crop_coords ( struct image_data_set_t *data_set,
00958                                   uint32_t *x1,
00959                                   uint32_t *y1, 
00960                                   uint32_t *x2,
00961                                   uint32_t *y2 )
00962 {
00963     if (data_set == NULL) return phFAIL;
00964     if (x1 != NULL) *x1 = data_set->x1;
00965     if (y1 != NULL) *y1 = data_set->y1;
00966     if (x2 != NULL) *x2 = data_set->x2;
00967     if (y2 != NULL) *y2 = data_set->y2;
00968     return phSUCCESS;
00969 }
00970 
00971 /* ------------------------------------------------------------------------ */
00972 int         ids_get_hsv_adjust ( struct image_data_set_t *data_set,
00973                                  int32_t *h,
00974                                  int32_t *s, 
00975                                  int32_t *v )
00976 {
00977     if (data_set == NULL) return phFAIL;
00978     if (h != NULL) *h = data_set->hue_adjust;
00979     if (s != NULL) *s = data_set->sat_adjust;
00980     if (v != NULL) *v = data_set->val_adjust;
00981     return phSUCCESS;
00982 }
00983 
00984 #ifdef __cplusplus
00985 }
00986 #endif




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