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
  27. pal_clean
  28. pal_restore
  29. handle_clean_overlay

   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 //----------------------------------------------------------------------------
 268 
 269 #ifdef CAM_CLEAN_OVERLAY
 270 /*
 271  * Making the Canon overlay invisible under selected conditions
 272  * Meant to be used on DIGIC 6 models that allow HDMI output in rec mode
 273  * On m3 and m10, same palette (0) is used in rec mode and during recording
 274  * Issues:
 275  * - regardless of setting, modes with color submode icon (such as hybrid auto) continue to display that (blue) icon
 276  * - when exposure controls are used, drop shadow of invisible osd elements may appear on screen
 277  */
 278 extern char **palette_buffer_ptr[];
 279 extern char active_palette_buffer;
 280 extern char palette_control;
 281 static char *palbackup = NULL;      // backup for the original palette
 282 static int palette_is_clean = 0;
 283 static long pending_screenerase = 0;
 284 const unsigned int handled_palette = 0; // model dependent constant, 0 for m3 and m10
 285 void pal_clean() {
 286     if (palette_is_clean) return;
 287     if (active_palette_buffer == handled_palette) {
 288         if (palbackup == NULL) {
 289             palbackup = malloc(256*4); // 256 entries, 4 bytes each
 290             if (palbackup) {
 291                 memcpy(palbackup, *palette_buffer_ptr[handled_palette]+4, 256*4);
 292             }
 293         }
 294         if (palbackup) {
 295             memset(*palette_buffer_ptr[handled_palette]+4, 0, 256*4);
 296             pending_screenerase = get_tick_count() + 50; // arbitrary 50msec delay
 297             palette_is_clean = 1;
 298         }
 299     }
 300 }
 301 void pal_restore() {
 302     if (!palette_is_clean || (palbackup == NULL)) return;
 303     memcpy(*palette_buffer_ptr[handled_palette]+4, palbackup, 256*4);
 304     pending_screenerase = get_tick_count() + 10; // arbitrary 10msec delay
 305     palette_is_clean = 0;
 306 }
 307 void handle_clean_overlay() {
 308     if ((conf.clean_overlay == 1) && camera_info.state.mode_rec) { // clean during rec mode
 309         pal_clean();
 310     }
 311     else if ((conf.clean_overlay == 2) && is_video_recording()) { // clean during video recording only
 312         pal_clean();
 313     }
 314     else {
 315         pal_restore();
 316     }
 317     if (pending_screenerase && (get_tick_count()>=pending_screenerase)) {
 318         pending_screenerase = 0;
 319 
 320 #if 1
 321         // following seems to be effective for removing/redrawing Canon overlay
 322         palette_control |= 3; // magic constant, valid for m10 and m3
 323         vid_bitmap_refresh();
 324 #else
 325         // following is for the case when palette_control + vid_bitmap_refresh is not effective
 326         extern void _displayblankscreen();
 327         extern void _undisplayblankscreen();
 328         _displayblankscreen();
 329         _undisplayblankscreen();
 330 #endif
 331     }
 332 }
 333 #endif // CAM_CLEAN_OVERLAY
 334 
 335 
 336 //----------------------------------------------------------------------------

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