nv2_common.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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>
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 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
00151 continue;
00152 }
00153 else if (n < 0)
00154 {
00155
00156
00157
00158
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
00186 continue;
00187 }
00188 else if (n < 0)
00189 {
00190
00191
00192
00193
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
00211
00212
00213
00214
00215
00216 #endif // NEOVISIONII_NV2_COMMON_C_DEFINED