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 #include "gui_draw.h"
   5 
   6 #if defined(CAM_DRYOS)
   7 #include "chdk-dir.c"
   8 #endif
   9 
  10 //---------------------------------------------------------------------
  11 
  12 // CHDK dir functions
  13 
  14 extern int   fw_closedir(void *d);
  15 extern void *fw_opendir(const char* name);
  16 
  17 DIR *opendir(const char* name)
  18 {
  19     return opendir_chdk(name,OPENDIR_FL_NONE);
  20 }
  21 
  22 DIR *opendir_chdk(const char* name, unsigned flags)
  23 {
  24     // Create CHDK DIR structure
  25     DIR *dir = malloc(sizeof(DIR));
  26     // If malloc failed return failure
  27     if (dir == 0) return NULL;
  28 
  29 #if defined(CAM_DRYOS)
  30     extern int get_fstype(void);
  31     // try built-in routine first, but only on FAT
  32     if ((get_fstype() < 4) && (flags & OPENDIR_FL_CHDK_LFN))
  33     {
  34         dir->fw_dir = 0;
  35         dir->cam_DIR = CHDKOpenDir(name);
  36     }
  37     else
  38     {
  39         dir->cam_DIR = NULL;
  40     }
  41     if (!dir->cam_DIR)
  42     {
  43         // try firmware routine if built-in failed
  44         dir->fw_dir = 1;
  45         dir->cam_DIR = fw_opendir(name);
  46     }
  47 #else
  48     dir->cam_DIR = fw_opendir(name);
  49 #endif
  50 
  51     // Init readdir return value
  52     dir->dir.d_name[0] = 0;
  53 
  54     // If failed clean up and return failure
  55     if (!dir->cam_DIR)
  56     {
  57         free(dir);
  58         return NULL;
  59     }
  60 
  61     return dir;
  62 }
  63 
  64 #ifndef CAM_DRYOS
  65 
  66 // Internal VxWorks dirent structure returned by readdir
  67 struct __dirent
  68 {
  69     char            d_name[100];
  70 };
  71 
  72 struct dirent* readdir(DIR *d)
  73 {
  74     if (d && d->cam_DIR)
  75     {
  76         // Get next entry from firmware function
  77         extern void *fw_readdir(void *d);
  78         struct __dirent *de = fw_readdir(d->cam_DIR);
  79         // Return next directory name if present, else return 0 (end of list)
  80         if (de)
  81         {
  82             strcpy(d->dir.d_name,de->d_name);
  83             return &d->dir;
  84         }
  85         else
  86         {
  87             d->dir.d_name[0] = 0;
  88         }
  89     }
  90     return NULL;
  91 }
  92 
  93 #else // dryos
  94 
  95 struct dirent * readdir(DIR *d)
  96 {
  97     if (d && d->cam_DIR)
  98     {
  99         if (d->fw_dir == 0)
 100         {
 101             CHDKReadDir(d->cam_DIR, d->dir.d_name);
 102         }
 103         else
 104         {
 105             extern int fw_readdir(void *d, void* dd); // DRYOS
 106             fw_readdir(d->cam_DIR, d->dir.d_name);
 107         }
 108         return d->dir.d_name[0]? &d->dir : NULL;
 109     }
 110     return NULL;
 111 }
 112 
 113 #endif // dryos dir functions
 114 
 115 int closedir(DIR *d)
 116 {
 117     int rv = -1;
 118     if (d && d->cam_DIR)
 119     {
 120 #if defined(CAM_DRYOS)
 121         if (d->fw_dir == 0)
 122         {
 123             rv = CHDKCloseDir(d->cam_DIR);
 124         }
 125         else
 126 #endif
 127         rv = fw_closedir(d->cam_DIR);
 128 
 129         // Mark closed (just in case)
 130         d->cam_DIR = 0;
 131         // Free allocated memory
 132         free(d);    
 133     }
 134     return rv;
 135 }
 136 
 137 //----------------------------------------------------------------------------
 138 // Char Wrappers (ARM stubs not required)
 139 
 140 #if CAM_DRYOS
 141 
 142 #define _U      0x01    /* upper */
 143 #define _L      0x02    /* lower */
 144 #define _D      0x04    /* digit */
 145 #define _C      0x20    /* cntrl */
 146 #define _P      0x10    /* punct */
 147 #define _S      0x40    /* white space (space/lf/tab) */
 148 #define _X      0x80    /* hex digit */
 149 #define _SP     0x08    /* hard space (0x20) */
 150 static int _ctype(int c,int t) {
 151     extern unsigned char ctypes[];  // Firmware ctypes table (in stubs_entry.S)
 152     return ctypes[c&0xFF] & t;
 153 }
 154 
 155 int isdigit(int c) { return _ctype(c,_D); }
 156 int isspace(int c) { return _ctype(c,_S); }
 157 int isalpha(int c) { return _ctype(c,(_U|_L)); }
 158 int isupper(int c) { return _ctype(c,_U); }
 159 int islower(int c) { return _ctype(c,_L); }
 160 int ispunct(int c) { return _ctype(c,_P); }
 161 int isxdigit(int c) { return _ctype(c,(_X|_D)); }
 162 int iscntrl(int c) { return _ctype(c,_C); }
 163 
 164 int tolower(int c) { return isupper(c) ? c | 0x20 : c; }
 165 int toupper(int c) { return islower(c) ? c & ~0x20 : c; }
 166 
 167 #else
 168 
 169 // don't want to require the whole ctype table on vxworks just for this one
 170 int iscntrl(int c) { return ((c >=0 && c <32) || c == 127); }
 171 
 172 #endif
 173 
 174 int isalnum(int c) { return (isdigit(c) || isalpha(c)); }
 175 
 176 //----------------------------------------------------------------------------
 177 
 178 #if CAM_DRYOS
 179 char *strpbrk(const char *s, const char *accept)
 180 {
 181     const char *sc1,*sc2;
 182 
 183     for (sc1 = s; *sc1 != '\0'; ++sc1)
 184     {
 185         for (sc2 = accept; *sc2 != '\0'; ++sc2)
 186         {
 187             if (*sc1 == *sc2)
 188                 return (char*)sc1;
 189         }
 190     }
 191     return 0;
 192 }
 193 #endif
 194 
 195 char *strstr(const char *s1, const char *s2)
 196 {
 197     const char *p = s1;
 198     const int len = strlen(s2);
 199 
 200     for (; (p = strchr(p, *s2)) != 0; p++)
 201     {
 202         if (strncmp(p, s2, len) == 0)
 203             return (char*)p;
 204     }
 205     return (0);
 206 }
 207 
 208 #if CAM_DRYOS
 209 void *memchr(const void *s, int c, int n)
 210 {
 211     while (n-- > 0)
 212     {
 213         if (*(char *)s == c)
 214             return (void *)s;
 215         s++;
 216     }
 217     return (void *)0;
 218 }
 219 #endif
 220 
 221 //----------------------------------------------------------------------------
 222 
 223 struct tm *get_localtime()
 224 {
 225     time_t t = time(NULL);
 226     return localtime(&t);
 227 }
 228 
 229 //----------------------------------------------------------------------------
 230 
 231 unsigned int GetJpgCount(void)
 232 {
 233     return strtol(camera_jpeg_count_str(),((void*)0),0);
 234 }
 235 
 236 unsigned int GetRawCount(void)
 237 {
 238     unsigned free_kb = GetFreeCardSpaceKb();
 239     unsigned raw_kb =  camera_sensor.raw_size/1024;
 240     unsigned jpgcount = GetJpgCount();
 241     unsigned avg_jpg_kb = (jpgcount>0)? free_kb/jpgcount : 0;
 242 
 243     // 0.25 raw margin
 244     unsigned margin_kb = raw_kb/4;
 245     if(free_kb <= raw_kb + margin_kb) {
 246         return 0;
 247     }
 248     free_kb -= margin_kb;
 249     return free_kb/(raw_kb+avg_jpg_kb);
 250 }
 251 
 252 //----------------------------------------------------------------------------
 253 
 254 // viewport image offset - used when image size != viewport size (zebra, histogram, motion detect & edge overlay)
 255 // returns the byte offset into the viewport buffer where the image pixels start (to skip any black borders)
 256 // see G12 port for sample implementation
 257 int vid_get_viewport_image_offset() {
 258     return (vid_get_viewport_yoffset() * vid_get_viewport_byte_width() * vid_get_viewport_yscale()) + (vid_get_viewport_xoffset() * 3);
 259 }
 260 
 261 // viewport image offset - used when image size != viewport size (zebra, histogram, motion detect & edge overlay)
 262 // returns the byte offset to skip at the end of a viewport buffer row to get to the next row.
 263 // see G12 port for sample implementation
 264 int vid_get_viewport_row_offset() {
 265 #ifndef THUMB_FW
 266     return (vid_get_viewport_byte_width() * vid_get_viewport_yscale()) - (vid_get_viewport_width_proper() * 6 / 4);
 267 #else
 268     return (vid_get_viewport_byte_width() * vid_get_viewport_yscale()) - (vid_get_viewport_width_proper() * 4 / 2);
 269 #endif
 270 }
 271 
 272 //----------------------------------------------------------------------------
 273 
 274 #ifdef CAM_CLEAN_OVERLAY
 275 /*
 276  * Making the Canon overlay invisible under selected conditions
 277  * Meant to be used on DIGIC 6 models that allow HDMI output in rec mode
 278  * On m3 and m10, same palette (0) is used in rec mode and during recording
 279  * Issues:
 280  * - regardless of setting, modes with color submode icon (such as hybrid auto) continue to display that (blue) icon
 281  * - when exposure controls are used, drop shadow of invisible osd elements may appear on screen
 282  */
 283 extern char **palette_buffer_ptr[];
 284 extern char active_palette_buffer;
 285 extern char palette_control;
 286 static char *palbackup = NULL;      // backup for the original palette
 287 static int palette_is_clean = 0;
 288 static long pending_screenerase = 0;
 289 const unsigned int handled_palette = 0; // model dependent constant, 0 for m3 and m10
 290 void pal_clean() {
 291     if (palette_is_clean) return;
 292     if (active_palette_buffer == handled_palette) {
 293         if (palbackup == NULL) {
 294             palbackup = malloc(256*4); // 256 entries, 4 bytes each
 295             if (palbackup) {
 296                 memcpy(palbackup, *palette_buffer_ptr[handled_palette]+4, 256*4);
 297             }
 298         }
 299         if (palbackup) {
 300             memset(*palette_buffer_ptr[handled_palette]+4, 0, 256*4);
 301             pending_screenerase = get_tick_count() + 50; // arbitrary 50msec delay
 302             palette_is_clean = 1;
 303         }
 304     }
 305 }
 306 void pal_restore() {
 307     if (!palette_is_clean || (palbackup == NULL)) return;
 308     memcpy(*palette_buffer_ptr[handled_palette]+4, palbackup, 256*4);
 309     pending_screenerase = get_tick_count() + 10; // arbitrary 10msec delay
 310     palette_is_clean = 0;
 311 }
 312 void handle_clean_overlay() {
 313     if ((conf.clean_overlay == 1) && camera_info.state.mode_rec) { // clean during rec mode
 314         pal_clean();
 315     }
 316     else if ((conf.clean_overlay == 2) && is_video_recording()) { // clean during video recording only
 317         pal_clean();
 318     }
 319     else {
 320         pal_restore();
 321     }
 322     if (pending_screenerase && (get_tick_count()>=pending_screenerase) && !draw_is_suspended()) {
 323         pending_screenerase = 0;
 324 
 325 #if 1
 326         // following seems to be effective for removing/redrawing Canon overlay
 327         palette_control |= 3; // magic constant, valid for m10 and m3
 328         vid_bitmap_refresh();
 329 #else
 330         // following is for the case when palette_control + vid_bitmap_refresh is not effective
 331         extern void _displayblankscreen();
 332         extern void _undisplayblankscreen();
 333         _displayblankscreen();
 334         _undisplayblankscreen();
 335 #endif
 336     }
 337 }
 338 #endif // CAM_CLEAN_OVERLAY
 339 
 340 
 341 //----------------------------------------------------------------------------

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