nv2_common.c

Go to the documentation of this file.
00001 /*!@file NeovisionII/nv2_common.c */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00005 // by the University of Southern California (USC) and the iLab at USC.  //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Rob Peters <rjpeters at usc dot edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/NeovisionII/nv2_common.c $
00035 // $Id: nv2_common.c 9496 2008-03-19 04:15:58Z rjpeters $
00036 //
00037 
00038 #ifndef NEOVISIONII_NV2_COMMON_C_DEFINED
00039 #define NEOVISIONII_NV2_COMMON_C_DEFINED
00040 
00041 #include "NeovisionII/nv2_common.h"
00042 
00043 #include <errno.h>
00044 #include <stdio.h>
00045 #include <stdlib.h>
00046 #include <string.h> // for strerror()
00047 #include <unistd.h>
00048 
00049 uint32_t nv2_pixel_type_bytes_per_pixel(enum nv2_pixel_type typ)
00050 {
00051         switch (typ)
00052         {
00053         case NV2_PIXEL_TYPE_NONE: return 0;
00054         case NV2_PIXEL_TYPE_GRAY8: return 1;
00055         case NV2_PIXEL_TYPE_RGB24: return 3;
00056         default: break;
00057         }
00058 
00059         errno = 0;
00060         nv2_fatal("invalid pixel type value");
00061         /* can't happen */ return ((uint32_t) -1);
00062 }
00063 
00064 void nv2_image_patch_init_empty(struct nv2_image_patch* p)
00065 {
00066         p->protocol_version = NV2_PATCH_PROTOCOL_VERSION;
00067         p->width = 0;
00068         p->height = 0;
00069         p->id = 0;
00070         p->is_training_image = 0;
00071         p->type = NV2_PIXEL_TYPE_NONE;
00072         p->training_label[sizeof(p->training_label)-1] = '\0';
00073         p->remote_command[sizeof(p->remote_command)-1] = '\0';
00074         p->data = 0;
00075 }
00076 
00077 void nv2_image_patch_destroy(struct nv2_image_patch* p)
00078 {
00079         if (p->data)
00080                 free(p->data);
00081 
00082         nv2_image_patch_init_empty(p);
00083 }
00084 
00085 void nv2_image_patch_set_training_label(
00086         struct nv2_image_patch* p,
00087         const char* label)
00088 {
00089         strncpy(&p->training_label[0], label,
00090                 sizeof(p->training_label));
00091         p->training_label[sizeof(p->training_label)-1] = '\0';
00092 }
00093 
00094 void nv2_image_patch_set_remote_command(
00095         struct nv2_image_patch* p,
00096         const char* command)
00097 {
00098         strncpy(&p->remote_command[0], command,
00099                 sizeof(p->remote_command));
00100         p->remote_command[sizeof(p->remote_command)-1] = '\0';
00101 }
00102 
00103 void nv2_patch_label_init_empty(struct nv2_patch_label* l)
00104 {
00105         l->protocol_version = NV2_LABEL_PROTOCOL_VERSION;
00106         l->patch_id = 0;
00107         l->confidence = 0;
00108         l->source[0] = '\0';
00109         l->name[0] = '\0';
00110         l->extra_info[0] = '\0';
00111 }
00112 
00113 void nv2_fatal_impl(const char* file, int line, const char* function,
00114                     const char* what)
00115 {
00116         if (errno != 0)
00117                 fprintf(stderr, "%s:%d(%s): error: %s (%s)\n",
00118                         file, line, function, what, strerror(errno));
00119         else
00120                 fprintf(stderr, "%s:%d(%s): error: %s\n",
00121                         file, line, function, what);
00122         exit(-1);
00123 }
00124 
00125 void nv2_warn_impl(const char* file, int line, const char* function,
00126                    const char* what)
00127 {
00128         if (errno != 0)
00129                 fprintf(stderr, "%s:%d(%s): warning: %s (%s)\n",
00130                         file, line, function, what, strerror(errno));
00131         else
00132                 fprintf(stderr, "%s:%d(%s): warning: %s\n",
00133                         file, line, function, what);
00134 }
00135 
00136 size_t nv2_robust_write(int fd, const void* const data,
00137                         size_t const nbytes)
00138 {
00139         size_t offset = 0;
00140 
00141         while (offset < nbytes)
00142         {
00143                 errno = 0;
00144                 const ssize_t n =
00145                         write(fd, data + offset,
00146                               nbytes - offset);
00147 
00148                 if (errno == EINTR || errno == EAGAIN)
00149                 {
00150                         // just try the same write() call again
00151                         continue;
00152                 }
00153                 else if (n < 0)
00154                 {
00155                         // there was some error, so just return the
00156                         // current offset value; the caller can tell
00157                         // that an error occurred because offset will
00158                         // be less than the requested nbytes
00159                         break;
00160                 }
00161                 else
00162                 {
00163                         offset += n;
00164                 }
00165         }
00166 
00167         return offset;
00168 }
00169 
00170 size_t nv2_robust_read(int fd, void* buf, size_t nbytes,
00171                        int* nchunks_return)
00172 {
00173         int nchunks = 0;
00174 
00175         size_t offset = 0;
00176         while (offset < nbytes)
00177         {
00178                 errno = 0;
00179                 const ssize_t n =
00180                         read(fd, buf + offset,
00181                              nbytes - offset);
00182 
00183                 if (errno == EINTR || errno == EAGAIN)
00184                 {
00185                         // just try the same read() call again
00186                         continue;
00187                 }
00188                 else if (n < 0)
00189                 {
00190                         // there was some error, so just return the
00191                         // current offset value; the caller can tell
00192                         // that an error occurred because offset will
00193                         // be less than the requested nbytes
00194                         break;
00195                 }
00196                 else
00197                 {
00198                         offset += n;
00199                         ++nchunks;
00200                 }
00201         }
00202 
00203         if (nchunks_return != NULL)
00204                 *nchunks_return = nchunks;
00205 
00206         return offset;
00207 }
00208 
00209 // ######################################################################
00210 /* So things look consistent in everyone's emacs... */
00211 /* Local Variables: */
00212 /* indent-tabs-mode: nil */
00213 /* c-file-style: "linux" */
00214 /* End: */
00215 
00216 #endif // NEOVISIONII_NV2_COMMON_C_DEFINED
Generated on Sun May 8 08:41:02 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3