root/core/lib_thumb.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. opendir
  2. opendir_chdk
  3. readdir
  4. readdir
  5. closedir
  6. _ctype
  7. isdigit
  8. isspace
  9. isalpha
  10. isupper
  11. islower
  12. ispunct
  13. isxdigit
  14. iscntrl
  15. tolower
  16. toupper
  17. iscntrl
  18. isalnum
  19. strpbrk
  20. strstr
  21. memchr
  22. get_localtime
  23. GetJpgCount
  24. GetRawCount
  25. vid_get_viewport_image_offset
  26. vid_get_viewport_row_offset

   1 // routines compiled as thumb
   2 #include "platform.h"
   3 #include "conf.h"
   4 
   5 #if defined(CAM_DRYOS)
   6 #include "chdk-dir.c"
   7 #endif
   8 
   9 //---------------------------------------------------------------------
  10 
  11 // CHDK dir functions
  12 
  13 extern int   fw_closedir(void *d);
  14 extern void *fw_opendir(const char* name);
  15 
  16 DIR *opendir(const char* name)
  17 {
  18     return opendir_chdk(name,OPENDIR_FL_NONE);
  19 }
  20 
  21 DIR *opendir_chdk(const char* name, unsigned flags)
  22 {
  23     // Create CHDK DIR structure
  24     DIR *dir = malloc(sizeof(DIR));
  25     // If malloc failed return failure
  26     if (dir == 0) return NULL;
  27 
  28 #if defined(CAM_DRYOS)
  29     extern int get_fstype(void);
  30     // try built-in routine first, but only on FAT
  31     if ((get_fstype() < 4) && (flags & OPENDIR_FL_CHDK_LFN))
  32     {
  33         dir->fw_dir = 0;
  34         dir->cam_DIR = CHDKOpenDir(name);
  35     }
  36     else
  37     {
  38         dir->cam_DIR = NULL;
  39     }
  40     if (!dir->cam_DIR)
  41     {
  42         // try firmware routine if built-in failed
  43         dir->fw_dir = 1;
  44         dir->cam_DIR = fw_opendir(name);
  45     }
  46 #else
  47     dir->cam_DIR = fw_opendir(name);
  48 #endif
  49 
  50     // Init readdir return value
  51     dir->dir.d_name[0] = 0;
  52 
  53     // If failed clean up and return failure
  54     if (!dir->cam_DIR)
  55     {
  56         free(dir);
  57         return NULL;
  58     }
  59 
  60     return dir;
  61 }
  62 
  63 #ifndef CAM_DRYOS
  64 
  65 // Internal VxWorks dirent structure returned by readdir
  66 struct __dirent
  67 {
  68     char            d_name[100];
  69 };
  70 
  71 struct dirent* readdir(DIR *d)
  72 {
  73     if (d && d->cam_DIR)
  74     {
  75         // Get next entry from firmware function
  76         extern void *fw_readdir(void *d);
  77         struct __dirent *de = fw_readdir(d->cam_DIR);
  78         // Return next directory name if present, else return 0 (end of list)
  79         if (de)
  80         {
  81             strcpy(d->dir.d_name,de->d_name);
  82             return &d->dir;
  83         }
  84         else
  85         {
  86             d->dir.d_name[0] = 0;
  87         }
  88     }
  89     return NULL;
  90 }
  91 
  92 #else // dryos
  93 
  94 struct dirent * readdir(DIR *d)
  95 {
  96     if (d && d->cam_DIR)
  97     {
  98         if (d->fw_dir == 0)
  99         {
 100             CHDKReadDir(d->cam_DIR, d->dir.d_name);
 101         }
 102         else
 103         {
 104             extern int fw_readdir(void *d, void* dd); // DRYOS
 105             fw_readdir(d->cam_DIR, d->dir.d_name);
 106         }
 107         return d->dir.d_name[0]? &d->dir : NULL;
 108     }
 109     return NULL;
 110 }
 111 
 112 #endif // dryos dir functions
 113 
 114 int closedir(DIR *d)
 115 {
 116     int rv = -1;
 117     if (d && d->cam_DIR)
 118     {
 119 #if defined(CAM_DRYOS)
 120         if (d->fw_dir == 0)
 121         {
 122             rv = CHDKCloseDir(d->cam_DIR);
 123         }
 124         else
 125 #endif
 126         rv = fw_closedir(d->cam_DIR);
 127 
 128         // Mark closed (just in case)
 129         d->cam_DIR = 0;
 130         // Free allocated memory
 131         free(d);    
 132     }
 133     return rv;
 134 }
 135 
 136 //----------------------------------------------------------------------------
 137 // Char Wrappers (ARM stubs not required)
 138 
 139 #if CAM_DRYOS
 140 
 141 #define _U      0x01    /* upper */
 142 #define _L      0x02    /* lower */
 143 #define _D      0x04    /* digit */
 144 #define _C      0x20    /* cntrl */
 145 #define _P      0x10    /* punct */
 146 #define _S      0x40    /* white space (space/lf/tab) */
 147 #define _X      0x80    /* hex digit */
 148 #define _SP     0x08    /* hard space (0x20) */
 149 static int _ctype(int c,int t) {
 150     extern unsigned char ctypes[];  // Firmware ctypes table (in stubs_entry.S)
 151     return ctypes[c&0xFF] & t;
 152 }
 153 
 154 int isdigit(int c) { return _ctype(c,_D); }
 155 int isspace(int c) { return _ctype(c,_S); }
 156 int isalpha(int c) { return _ctype(c,(_U|_L)); }
 157 int isupper(int c) { return _ctype(c,_U); }
 158 int islower(int c) { return _ctype(c,_L); }
 159 int ispunct(int c) { return _ctype(c,_P); }
 160 int isxdigit(int c) { return _ctype(c,(_X|_D)); }
 161 int iscntrl(int c) { return _ctype(c,_C); }
 162 
 163 int tolower(int c) { return isupper(c) ? c | 0x20 : c; }
 164 int toupper(int c) { return islower(c) ? c & ~0x20 : c; }
 165 
 166 #else
 167 
 168 // don't want to require the whole ctype table on vxworks just for this one
 169 int iscntrl(int c) { return ((c >=0 && c <32) || c == 127); }
 170 
 171 #endif
 172 
 173 int isalnum(int c) { return (isdigit(c) || isalpha(c)); }
 174 
 175 //----------------------------------------------------------------------------
 176 
 177 #if CAM_DRYOS
 178 char *strpbrk(const char *s, const char *accept)
 179 {
 180     const char *sc1,*sc2;
 181 
 182     for (sc1 = s; *sc1 != '\0'; ++sc1)
 183     {
 184         for (sc2 = accept; *sc2 != '\0'; ++sc2)
 185         {
 186             if (*sc1 == *sc2)
 187                 return (char*)sc1;
 188         }
 189     }
 190     return 0;
 191 }
 192 #endif
 193 
 194 char *strstr(const char *s1, const char *s2)
 195 {
 196     const char *p = s1;
 197     const int len = strlen(s2);
 198 
 199     for (; (p = strchr(p, *s2)) != 0; p++)
 200     {
 201         if (strncmp(p, s2, len) == 0)
 202             return (char*)p;
 203     }
 204     return (0);
 205 }
 206 
 207 #if CAM_DRYOS
 208 void *memchr(const void *s, int c, int n)
 209 {
 210     while (n-- > 0)
 211     {
 212         if (*(char *)s == c)
 213             return (void *)s;
 214         s++;
 215     }
 216     return (void *)0;
 217 }
 218 #endif
 219 
 220 //----------------------------------------------------------------------------
 221 
 222 struct tm *get_localtime()
 223 {
 224     time_t t = time(NULL);
 225     return localtime(&t);
 226 }
 227 
 228 //----------------------------------------------------------------------------
 229 
 230 unsigned int GetJpgCount(void)
 231 {
 232     return strtol(camera_jpeg_count_str(),((void*)0),0);
 233 }
 234 
 235 unsigned int GetRawCount(void)
 236 {
 237     unsigned free_kb = GetFreeCardSpaceKb();
 238     unsigned raw_kb =  camera_sensor.raw_size/1024;
 239     unsigned jpgcount = GetJpgCount();
 240     unsigned avg_jpg_kb = (jpgcount>0)? free_kb/jpgcount : 0;
 241 
 242     // 0.25 raw margin
 243     unsigned margin_kb = raw_kb/4;
 244     if(free_kb <= raw_kb + margin_kb) {
 245         return 0;
 246     }
 247     free_kb -= margin_kb;
 248     return free_kb/(raw_kb+avg_jpg_kb);
 249 }
 250 
 251 //----------------------------------------------------------------------------
 252 
 253 // viewport image offset - used when image size != viewport size (zebra, histogram, motion detect & edge overlay)
 254 // returns the byte offset into the viewport buffer where the image pixels start (to skip any black borders)
 255 // see G12 port for sample implementation
 256 int vid_get_viewport_image_offset() {
 257     return (vid_get_viewport_yoffset() * vid_get_viewport_byte_width() * vid_get_viewport_yscale()) + (vid_get_viewport_xoffset() * 3);
 258 }
 259 
 260 // viewport image offset - used when image size != viewport size (zebra, histogram, motion detect & edge overlay)
 261 // returns the byte offset to skip at the end of a viewport buffer row to get to the next row.
 262 // see G12 port for sample implementation
 263 int vid_get_viewport_row_offset() {
 264     return (vid_get_viewport_byte_width() * vid_get_viewport_yscale()) - (vid_get_viewport_width() * 3);
 265 }
 266 
 267 //----------------------------------------------------------------------------

/* [<][>][^][v][top][bottom][index][help] */