Changeset 1053 for trunk


Ignore:
Timestamp:
01/22/11 01:15:10 (2 years ago)
Author:
pixeldoc2000
Message:

core/raw.c cleanup

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/core/raw.c

    r1049 r1053  
    3131static char dir[32]; 
    3232static int develop_raw=0; 
    33 //------------------------------------------------------------------- 
    34 void raw_prepare_develop(const char* filename){ 
    35  if (filename) { 
    36   develop_raw=1;  
    37   strcpy(fn,filename); 
    38  } 
    39  else { 
    40   develop_raw=0; 
    41  } 
     33 
     34//------------------------------------------------------------------- 
     35void raw_prepare_develop(const char* filename) { 
     36    if (filename) { 
     37        develop_raw=1; 
     38        strcpy(fn,filename); 
     39    } else { 
     40        develop_raw=0; 
     41    } 
    4242} 
    4343 
     
    4646//------------------------------------------------------------------- 
    4747 
    48 char* get_raw_image_addr(void){ 
    49  if (!conf.raw_cache) return hook_raw_image_addr(); 
    50  else return (char*) ((int)hook_raw_image_addr()&~CAM_UNCACHED_BIT); 
    51 } 
    52  
    53 char* get_alt_raw_image_addr(void){    // return inactive buffer for cameras with multiple RAW buffers (otherwise return active buffer) 
    54  if (!conf.raw_cache) return hook_alt_raw_image_addr(); 
    55  else return (char*) ((int)hook_alt_raw_image_addr()&~CAM_UNCACHED_BIT); 
     48char* get_raw_image_addr(void) { 
     49    if (!conf.raw_cache) return hook_raw_image_addr(); 
     50    else return (char*) ((int)hook_raw_image_addr()&~CAM_UNCACHED_BIT); 
     51} 
     52 
     53char* get_alt_raw_image_addr(void) {    // return inactive buffer for cameras with multiple RAW buffers (otherwise return active buffer) 
     54    if (!conf.raw_cache) return hook_alt_raw_image_addr(); 
     55    else return (char*) ((int)hook_alt_raw_image_addr()&~CAM_UNCACHED_BIT); 
    5656} 
    5757 
     
    6363 
    6464int init_badpixel_bin_flag; // contants above to count/create file, > 0 num bad pixel 
    65 int raw_init_badpixel_bin(){ 
    66   int count; 
    67   unsigned short c[2]; 
    68   FILE*f; 
    69   if(init_badpixel_bin_flag == INIT_BADPIXEL_FILE) { 
    70     f=fopen(PATH_BAD_TMP_BIN,"w+b"); 
    71   } else if (init_badpixel_bin_flag == INIT_BADPIXEL_COUNT){ 
    72     f=NULL; 
    73   } else { 
    74     return 0; 
    75   } 
    76   count = 0; 
    77   for (c[1]=CAM_ACTIVE_AREA_Y1; c[1]<CAM_ACTIVE_AREA_Y2; c[1]++) { 
    78     for (c[0]=CAM_ACTIVE_AREA_X1; c[0]<CAM_ACTIVE_AREA_X2; c[0]++) { 
    79       if (get_raw_pixel(c[0],c[1])==0) { 
    80         if(f) { 
    81           fwrite(c, 1, 4, f); 
    82         } 
    83         count++; 
    84       } 
    85     } 
    86   } 
    87   if(f) { 
    88     fclose(f); 
    89   } 
    90   init_badpixel_bin_flag = count; 
    91   state_shooting_progress = SHOOTING_PROGRESS_PROCESSING; 
    92   return 1; 
     65int raw_init_badpixel_bin() { 
     66    int count; 
     67    unsigned short c[2]; 
     68    FILE*f; 
     69    if(init_badpixel_bin_flag == INIT_BADPIXEL_FILE) { 
     70        f=fopen(PATH_BAD_TMP_BIN,"w+b"); 
     71    } else if (init_badpixel_bin_flag == INIT_BADPIXEL_COUNT) { 
     72        f=NULL; 
     73    } else { 
     74        return 0; 
     75    } 
     76    count = 0; 
     77    for (c[1]=CAM_ACTIVE_AREA_Y1; c[1]<CAM_ACTIVE_AREA_Y2; c[1]++) { 
     78        for (c[0]=CAM_ACTIVE_AREA_X1; c[0]<CAM_ACTIVE_AREA_X2; c[0]++) { 
     79            if (get_raw_pixel(c[0],c[1])==0) { 
     80                if(f) { 
     81                    fwrite(c, 1, 4, f); 
     82                } 
     83                count++; 
     84            } 
     85        } 
     86    } 
     87    if(f) { 
     88        fclose(f); 
     89    } 
     90    init_badpixel_bin_flag = count; 
     91    state_shooting_progress = SHOOTING_PROGRESS_PROCESSING; 
     92    return 1; 
    9393} 
    9494 
     
    9797static unsigned char gamma[256]; 
    9898 
    99 void fill_gamma_buf(void){ 
    100  int i; 
    101  if (gamma[255]) return; 
     99void fill_gamma_buf(void) { 
     100    int i; 
     101    if (gamma[255]) return; 
    102102#if defined(CAMERA_sx30) || defined(CAMERA_g12) 
    103  for (i=0; i<12; i++) gamma[i]=255*pow(i/255.0, 0.5); 
    104  for (i=12; i<64; i++) gamma[i]=255*pow(i/255.0, 0.4); 
    105  for (i=64; i<=255; i++) gamma[i]=255*pow(i/255.0, 0.25); 
     103    for (i=0; i<12; i++) gamma[i]=255*pow(i/255.0, 0.5); 
     104    for (i=12; i<64; i++) gamma[i]=255*pow(i/255.0, 0.4); 
     105    for (i=64; i<=255; i++) gamma[i]=255*pow(i/255.0, 0.25); 
    106106#else 
    107  for (i=0; i<=255; i++) gamma[i]=255*pow(i/255.0, 0.5); 
    108 #endif 
    109 } 
    110  
    111  
    112 void create_thumbnail(char* buf){ 
    113  unsigned int i, j, x, y; 
    114  unsigned char r, g, b; 
    115  for (i=0; i<DNG_TH_HEIGHT; i++) 
    116    for (j=0; j<DNG_TH_WIDTH; j++) { 
    117     x=CAM_ACTIVE_AREA_X1+((CAM_ACTIVE_AREA_X2-CAM_ACTIVE_AREA_X1)*j)/DNG_TH_WIDTH; 
    118     y=CAM_ACTIVE_AREA_Y1+((CAM_ACTIVE_AREA_Y2-CAM_ACTIVE_AREA_Y1)*i)/DNG_TH_HEIGHT; 
     107    for (i=0; i<=255; i++) gamma[i]=255*pow(i/255.0, 0.5); 
     108#endif 
     109} 
     110 
     111void create_thumbnail(char* buf) { 
     112    unsigned int i, j, x, y; 
     113    unsigned char r, g, b; 
     114    for (i=0; i<DNG_TH_HEIGHT; i++) 
     115        for (j=0; j<DNG_TH_WIDTH; j++) { 
     116            x=CAM_ACTIVE_AREA_X1+((CAM_ACTIVE_AREA_X2-CAM_ACTIVE_AREA_X1)*j)/DNG_TH_WIDTH; 
     117            y=CAM_ACTIVE_AREA_Y1+((CAM_ACTIVE_AREA_Y2-CAM_ACTIVE_AREA_Y1)*i)/DNG_TH_HEIGHT; 
    119118#if cam_CFAPattern==0x02010100    // Red  Green  Green  Blue 
    120     r=gamma[get_raw_pixel((x/2)*2,(y/2)*2)>>(CAM_SENSOR_BITS_PER_PIXEL-8)]; // red pixel 
    121     g=gamma[6*(get_raw_pixel((x/2)*2+1,(y/2)*2)>>(CAM_SENSOR_BITS_PER_PIXEL-8))/10]; // green pixel 
    122     b=gamma[get_raw_pixel((x/2)*2+1,(y/2)*2+1)>>(CAM_SENSOR_BITS_PER_PIXEL-8)]; //blue pixel 
     119            r=gamma[get_raw_pixel((x/2)*2,(y/2)*2)>>(CAM_SENSOR_BITS_PER_PIXEL-8)]; // red pixel 
     120            g=gamma[6*(get_raw_pixel((x/2)*2+1,(y/2)*2)>>(CAM_SENSOR_BITS_PER_PIXEL-8))/10]; // green pixel 
     121            b=gamma[get_raw_pixel((x/2)*2+1,(y/2)*2+1)>>(CAM_SENSOR_BITS_PER_PIXEL-8)]; //blue pixel 
    123122#elif cam_CFAPattern==0x01000201 // Green  Blue  Red  Green 
    124     r=gamma[get_raw_pixel((x/2)*2,(y/2)*2+1)>>(CAM_SENSOR_BITS_PER_PIXEL-8)]; // red pixel 
    125     g=gamma[6*(get_raw_pixel((x/2)*2,(y/2)*2)>>(CAM_SENSOR_BITS_PER_PIXEL-8))/10]; // green pixel 
    126     b=gamma[get_raw_pixel((x/2)*2+1,(y/2)*2)>>(CAM_SENSOR_BITS_PER_PIXEL-8)]; //blue pixel 
     123            r=gamma[get_raw_pixel((x/2)*2,(y/2)*2+1)>>(CAM_SENSOR_BITS_PER_PIXEL-8)]; // red pixel 
     124            g=gamma[6*(get_raw_pixel((x/2)*2,(y/2)*2)>>(CAM_SENSOR_BITS_PER_PIXEL-8))/10]; // green pixel 
     125            b=gamma[get_raw_pixel((x/2)*2+1,(y/2)*2)>>(CAM_SENSOR_BITS_PER_PIXEL-8)]; //blue pixel 
    127126#else  
    128  #error please define new pattern here 
    129 #endif 
    130     *buf++=r; *buf++=g; *buf++=b;  
    131    } 
     127    #error please define new pattern here 
     128#endif 
     129            *buf++=r; *buf++=g; *buf++=b; 
     130        } 
    132131} 
    133132#else // no DNG_SUPPORT 
    134 static inline int __attribute__((always_inline)) raw_init_badpixel_bin(void) {return 0;} 
     133    static inline int __attribute__((always_inline)) raw_init_badpixel_bin(void) {return 0;} 
    135134#endif 
    136135//------------------------------------------------------------------- 
    137136 
    138137int raw_savefile() { 
    139     int ret = 0;   
     138    int ret = 0; 
    140139    int fd, m=(mode_get()&MODE_SHOOTING_MASK); 
    141140    static struct utimbuf t; 
    142141    static int br_counter;  
    143142#if DNG_SUPPORT 
    144      struct t_data_for_exif* exif_data = NULL;   
    145      char *thumbnail_buf = NULL; 
    146      if (conf.dng_raw) exif_data=capture_data_for_exif(); 
     143    struct t_data_for_exif* exif_data = NULL; 
     144    char *thumbnail_buf = NULL; 
     145    if (conf.dng_raw) exif_data=capture_data_for_exif(); 
    147146#endif     
    148147    if (state_kbd_script_run && shot_histogram_isenabled()) build_shot_histogram(); 
     
    154153    // count/save badpixels if requested 
    155154    if(raw_init_badpixel_bin()) { 
    156       return 0; 
     155        return 0; 
    157156    } 
    158157 
    159158    if (develop_raw) { 
    160      started(); 
    161      fd = open(fn, O_RDONLY, 0777); 
    162      if (fd>=0) { 
    163       read(fd, get_raw_image_addr(), hook_raw_size()); 
    164       close(fd); 
    165       } 
     159        started(); 
     160        fd = open(fn, O_RDONLY, 0777); 
     161        if (fd>=0) { 
     162            read(fd, get_raw_image_addr(), hook_raw_size()); 
     163            close(fd); 
     164        } 
    166165#ifdef OPT_CURVES 
    167      if (conf.curve_enable) curve_apply(); 
    168 #endif 
    169      finished(); 
    170      develop_raw=0; 
    171      return 0; 
     166        if (conf.curve_enable) curve_apply(); 
     167#endif 
     168        finished(); 
     169        develop_raw=0; 
     170        return 0; 
    172171    } 
    173172 
     
    185184        br_counter=0; 
    186185 
    187  
    188186    // got here second time in a row. Skip second RAW saving. 
    189187    if (conf.raw_save_first_only && state_shooting_progress == SHOOTING_PROGRESS_PROCESSING) { 
    190188        return 0; 
    191189    } 
    192      
     190 
    193191    state_shooting_progress = SHOOTING_PROGRESS_PROCESSING; 
    194192 
     
    233231                    img_prefixes[conf.raw_prefix], 
    234232                    get_target_file_num(), 
    235                     conf.dng_raw&&conf.raw_dng_ext ? ".DNG" : img_exts[conf.raw_ext]);  
     233                    conf.dng_raw&&conf.raw_dng_ext ? ".DNG" : img_exts[conf.raw_ext]); 
    236234        } 
    237235        fd = open(fn, O_WRONLY|O_CREAT, 0777); 
     
    240238#if DNG_SUPPORT 
    241239            if (conf.dng_raw) { 
    242              fill_gamma_buf(); 
    243              create_dng_header(exif_data); 
    244              thumbnail_buf = malloc(DNG_TH_WIDTH*DNG_TH_HEIGHT*3); 
    245               if (get_dng_header() && thumbnail_buf) { 
    246                patch_bad_pixels_b(); 
    247                create_thumbnail(thumbnail_buf); 
    248                write(fd, get_dng_header(), get_dng_header_size()); 
    249                write(fd, thumbnail_buf, DNG_TH_WIDTH*DNG_TH_HEIGHT*3); 
    250                reverse_bytes_order2(rawadr, altrawadr, hook_raw_size()); 
    251              } 
     240                fill_gamma_buf(); 
     241                create_dng_header(exif_data); 
     242                thumbnail_buf = malloc(DNG_TH_WIDTH*DNG_TH_HEIGHT*3); 
     243                if (get_dng_header() && thumbnail_buf) { 
     244                    patch_bad_pixels_b(); 
     245                    create_thumbnail(thumbnail_buf); 
     246                    write(fd, get_dng_header(), get_dng_header_size()); 
     247                    write(fd, thumbnail_buf, DNG_TH_WIDTH*DNG_TH_HEIGHT*3); 
     248                    reverse_bytes_order2(rawadr, altrawadr, hook_raw_size()); 
     249                } 
    252250            } 
    253251#endif 
     
    255253                // Write alternate (inactive) buffer that we reversed the bytes into above (if only one buffer then it will be the active buffer instead) 
    256254                write(fd, (char*)(((unsigned long)altrawadr)|CAM_UNCACHED_BIT), hook_raw_size()); 
    257             } 
    258             else 
    259             { 
     255            } else { 
    260256                // Write active RAW buffer 
    261257                write(fd, (char*)(((unsigned long)rawadr)|CAM_UNCACHED_BIT), hook_raw_size()); 
     
    265261#if DNG_SUPPORT 
    266262            if (conf.dng_raw) { 
    267              if (get_dng_header() && thumbnail_buf) { 
    268                  if (rawadr == altrawadr)    // If only one RAW buffer then we have to swap the bytes back 
    269                     reverse_bytes_order2(rawadr, altrawadr, hook_raw_size()); 
    270           //   unpatch_bad_pixels_b(); 
    271               } 
    272              if (get_dng_header()) free_dng_header(); 
    273              if (thumbnail_buf) free(thumbnail_buf); 
     263                if (get_dng_header() && thumbnail_buf) { 
     264                    if (rawadr == altrawadr)    // If only one RAW buffer then we have to swap the bytes back 
     265                        reverse_bytes_order2(rawadr, altrawadr, hook_raw_size()); 
     266                    //unpatch_bad_pixels_b(); 
     267                } 
     268                if (get_dng_header()) free_dng_header(); 
     269                if (thumbnail_buf) free(thumbnail_buf); 
    274270            } 
    275271#endif 
     
    298294//------------------------------------------------------------------- 
    299295 
    300 void set_raw_pixel(unsigned int x, unsigned int y, unsigned short value){ 
     296void set_raw_pixel(unsigned int x, unsigned int y, unsigned short value) { 
    301297#if CAM_SENSOR_BITS_PER_PIXEL==10 
    302  unsigned char* addr=(unsigned char*)get_raw_image_addr()+y*RAW_ROWLEN+(x/8)*CAM_SENSOR_BITS_PER_PIXEL; 
    303  switch (x%8) { 
    304   case 0: addr[0]=(addr[0]&0x3F)|(value<<6); addr[1]=value>>2;                  break; 
    305   case 1: addr[0]=(addr[0]&0xC0)|(value>>4); addr[3]=(addr[3]&0x0F)|(value<<4); break; 
    306   case 2: addr[2]=(addr[2]&0x03)|(value<<2); addr[3]=(addr[3]&0xF0)|(value>>6); break; 
    307   case 3: addr[2]=(addr[2]&0xFC)|(value>>8); addr[5]=value;                     break; 
    308   case 4: addr[4]=value>>2;                  addr[7]=(addr[7]&0x3F)|(value<<6); break; 
    309   case 5: addr[6]=(addr[6]&0x0F)|(value<<4); addr[7]=(addr[7]&0xC0)|(value>>4); break; 
    310   case 6: addr[6]=(addr[6]&0xF0)|(value>>6); addr[9]=(addr[9]&0x03)|(value<<2); break; 
    311   case 7: addr[8]=value;                     addr[9]=(addr[9]&0xFC)|(value>>8); break; 
    312  } 
     298    unsigned char* addr=(unsigned char*)get_raw_image_addr()+y*RAW_ROWLEN+(x/8)*CAM_SENSOR_BITS_PER_PIXEL; 
     299    switch (x%8) { 
     300        case 0: addr[0]=(addr[0]&0x3F)|(value<<6); addr[1]=value>>2;                  break; 
     301        case 1: addr[0]=(addr[0]&0xC0)|(value>>4); addr[3]=(addr[3]&0x0F)|(value<<4); break; 
     302        case 2: addr[2]=(addr[2]&0x03)|(value<<2); addr[3]=(addr[3]&0xF0)|(value>>6); break; 
     303        case 3: addr[2]=(addr[2]&0xFC)|(value>>8); addr[5]=value;                     break; 
     304        case 4: addr[4]=value>>2;                  addr[7]=(addr[7]&0x3F)|(value<<6); break; 
     305        case 5: addr[6]=(addr[6]&0x0F)|(value<<4); addr[7]=(addr[7]&0xC0)|(value>>4); break; 
     306        case 6: addr[6]=(addr[6]&0xF0)|(value>>6); addr[9]=(addr[9]&0x03)|(value<<2); break; 
     307        case 7: addr[8]=value;                     addr[9]=(addr[9]&0xFC)|(value>>8); break; 
     308    } 
    313309#elif CAM_SENSOR_BITS_PER_PIXEL==12 
    314  unsigned char* addr=(unsigned char*)get_raw_image_addr()+y*RAW_ROWLEN+(x/4)*6; 
    315  switch (x%4) { 
    316   case 0: addr[0] = (addr[0]&0x0F) | (unsigned char)(value << 4);  addr[1] = (unsigned char)(value >> 4);  break; 
    317   case 1: addr[0] = (addr[0]&0xF0) | (unsigned char)(value >> 8);  addr[3] = (unsigned char)value;         break; 
    318   case 2: addr[2] = (unsigned char)(value >> 4);  addr[5] = (addr[5]&0x0F) | (unsigned char)(value << 4);  break; 
    319   case 3: addr[4] = (unsigned char)value; addr[5] = (addr[5]&0xF0) | (unsigned char)(value >> 8);  break; 
    320  } 
     310    unsigned char* addr=(unsigned char*)get_raw_image_addr()+y*RAW_ROWLEN+(x/4)*6; 
     311    switch (x%4) { 
     312        case 0: addr[0] = (addr[0]&0x0F) | (unsigned char)(value << 4);  addr[1] = (unsigned char)(value >> 4);  break; 
     313        case 1: addr[0] = (addr[0]&0xF0) | (unsigned char)(value >> 8);  addr[3] = (unsigned char)value;         break; 
     314        case 2: addr[2] = (unsigned char)(value >> 4);  addr[5] = (addr[5]&0x0F) | (unsigned char)(value << 4);  break; 
     315        case 3: addr[4] = (unsigned char)value; addr[5] = (addr[5]&0xF0) | (unsigned char)(value >> 8);  break; 
     316    } 
    321317#else  
    322  #error define set_raw_pixel for sensor bit depth 
    323 #endif 
    324 } 
    325  
    326 //------------------------------------------------------------------- 
    327 unsigned short get_raw_pixel(unsigned int x,unsigned  int y){ 
     318    #error define set_raw_pixel for sensor bit depth 
     319#endif 
     320} 
     321 
     322//------------------------------------------------------------------- 
     323unsigned short get_raw_pixel(unsigned int x,unsigned  int y) { 
    328324#if CAM_SENSOR_BITS_PER_PIXEL==10 
    329  unsigned char* addr=(unsigned char*)get_raw_image_addr()+y*RAW_ROWLEN+(x/8)*CAM_SENSOR_BITS_PER_PIXEL; 
    330  switch (x%8) { 
    331   case 0: return ((0x3fc&(((unsigned short)addr[1])<<2)) | (addr[0] >> 6)); 
    332   case 1: return ((0x3f0&(((unsigned short)addr[0])<<4)) | (addr[3] >> 4)); 
    333   case 2: return ((0x3c0&(((unsigned short)addr[3])<<6)) | (addr[2] >> 2)); 
    334   case 3: return ((0x300&(((unsigned short)addr[2])<<8)) | (addr[5]));  
    335   case 4: return ((0x3fc&(((unsigned short)addr[4])<<2)) | (addr[7] >> 6));  
    336   case 5: return ((0x3f0&(((unsigned short)addr[7])<<4)) | (addr[6] >> 4));  
    337   case 6: return ((0x3c0&(((unsigned short)addr[6])<<6)) | (addr[9] >> 2));  
    338   case 7: return ((0x300&(((unsigned short)addr[9])<<8)) | (addr[8]));  
    339  } 
     325    unsigned char* addr=(unsigned char*)get_raw_image_addr()+y*RAW_ROWLEN+(x/8)*CAM_SENSOR_BITS_PER_PIXEL; 
     326    switch (x%8) { 
     327        case 0: return ((0x3fc&(((unsigned short)addr[1])<<2)) | (addr[0] >> 6)); 
     328        case 1: return ((0x3f0&(((unsigned short)addr[0])<<4)) | (addr[3] >> 4)); 
     329        case 2: return ((0x3c0&(((unsigned short)addr[3])<<6)) | (addr[2] >> 2)); 
     330        case 3: return ((0x300&(((unsigned short)addr[2])<<8)) | (addr[5])); 
     331        case 4: return ((0x3fc&(((unsigned short)addr[4])<<2)) | (addr[7] >> 6)); 
     332        case 5: return ((0x3f0&(((unsigned short)addr[7])<<4)) | (addr[6] >> 4)); 
     333        case 6: return ((0x3c0&(((unsigned short)addr[6])<<6)) | (addr[9] >> 2)); 
     334        case 7: return ((0x300&(((unsigned short)addr[9])<<8)) | (addr[8])); 
     335    } 
    340336#elif CAM_SENSOR_BITS_PER_PIXEL==12 
    341  unsigned char* addr=(unsigned char*)get_raw_image_addr()+y*RAW_ROWLEN+(x/4)*6; 
    342  switch (x%4) { 
    343   case 0: return ((unsigned short)(addr[1]) << 4) | (addr[0] >> 4); 
    344   case 1: return ((unsigned short)(addr[0] & 0x0F) << 8) | (addr[3]); 
    345   case 2: return ((unsigned short)(addr[2]) << 4) | (addr[5] >> 4); 
    346   case 3: return ((unsigned short)(addr[5] & 0x0F) << 8) | (addr[4]); 
    347  } 
     337    unsigned char* addr=(unsigned char*)get_raw_image_addr()+y*RAW_ROWLEN+(x/4)*6; 
     338    switch (x%4) { 
     339        case 0: return ((unsigned short)(addr[1]) << 4) | (addr[0] >> 4); 
     340        case 1: return ((unsigned short)(addr[0] & 0x0F) << 8) | (addr[3]); 
     341        case 2: return ((unsigned short)(addr[2]) << 4) | (addr[5] >> 4); 
     342        case 3: return ((unsigned short)(addr[5] & 0x0F) << 8) | (addr[4]); 
     343    } 
    348344#else  
    349  #error define get_raw_pixel for sensor bit depth 
    350 #endif 
    351  return 0; 
    352 } 
    353  
    354 //------------------------------------------------------------------- 
    355 void patch_bad_pixel(unsigned int x,unsigned  int y){ 
    356  int sum=0; 
    357  int nzero=0; 
    358  int i,j; 
    359  int val; 
    360  if ((x>=2) && (x<CAM_RAW_ROWPIX-2) && (y>=2) && (y<CAM_RAW_ROWS-2)) { 
    361   if ((conf.bad_pixel_removal==1) || conf.dng_raw) {  // interpolation or DNG saving  
    362    for (i=-2; i<=2; i+=2) 
    363     for (j=-2; j<=2; j+=2)  
    364      if ((i!=0) && (j!=0)) { 
    365       val=get_raw_pixel(x+i, y+j); 
    366       if (val) {sum+=val; nzero++;} 
    367      } 
    368    if (nzero) set_raw_pixel(x,y,sum/nzero); 
    369   } 
    370   else if (conf.bad_pixel_removal==2)  // or this makes RAW converter (internal/external)  
    371    set_raw_pixel(x,y,0);    
    372  } 
     345    #error define get_raw_pixel for sensor bit depth 
     346#endif 
     347    return 0; 
     348} 
     349 
     350//------------------------------------------------------------------- 
     351void patch_bad_pixel(unsigned int x,unsigned  int y) { 
     352    int sum=0; 
     353    int nzero=0; 
     354    int i,j; 
     355    int val; 
     356    if ((x>=2) && (x<CAM_RAW_ROWPIX-2) && (y>=2) && (y<CAM_RAW_ROWS-2)) { 
     357        if ((conf.bad_pixel_removal==1) || conf.dng_raw) {  // interpolation or DNG saving 
     358            for (i=-2; i<=2; i+=2) 
     359                for (j=-2; j<=2; j+=2) 
     360                    if ((i!=0) && (j!=0)) { 
     361                        val=get_raw_pixel(x+i, y+j); 
     362                        if (val) {sum+=val; nzero++;} 
     363                    } 
     364                if (nzero) set_raw_pixel(x,y,sum/nzero); 
     365        } else if (conf.bad_pixel_removal==2)  // or this makes RAW converter (internal/external) 
     366            set_raw_pixel(x,y,0); 
     367    } 
    373368} 
    374369 
    375370struct point{ 
    376  int x; 
    377  int y; 
    378  struct point *next; 
     371    int x; 
     372    int y; 
     373    struct point *next; 
    379374} *pixel_list=NULL; 
    380375 
    381 void patch_bad_pixels(void){ 
    382  struct point *pixel=pixel_list; 
    383  while (pixel){ 
    384   patch_bad_pixel((*pixel).x,(*pixel).y); 
    385   pixel=(*pixel).next; 
    386  } 
    387 } 
    388  
    389 void make_pixel_list(char * ptr){ 
    390  int x,y; 
    391  struct point *pixel; 
    392  char *endptr; 
    393  while(*ptr){ 
    394   while (*ptr==' ' || *ptr=='\t') ++ptr; // whitespaces 
    395    x=strtol(ptr, &endptr, 0); 
    396    if (endptr != ptr) {  
    397     ptr = endptr; 
    398     if (*ptr++==',') { 
    399      while (*ptr==' ' || *ptr=='\t') ++ptr; // whitespaces 
    400      if (*ptr!='\n' && *ptr!='\r'){ 
    401       y=strtol(ptr, &endptr, 0); 
    402       if (endptr != ptr) {  
    403        ptr = endptr; 
    404        pixel=malloc(sizeof(struct point)); 
    405        if (pixel) { 
    406         (*pixel).x=x; 
    407         (*pixel).y=y; 
    408         (*pixel).next=pixel_list; 
    409         pixel_list=pixel; 
    410        } 
    411       } 
    412      } 
    413     } 
    414    } 
    415    while (*ptr && *ptr!='\n') ++ptr; // unless end of line 
    416    if (*ptr) ++ptr; 
    417  } 
     376void patch_bad_pixels(void) { 
     377    struct point *pixel=pixel_list; 
     378    while (pixel) { 
     379        patch_bad_pixel((*pixel).x,(*pixel).y); 
     380        pixel=(*pixel).next; 
     381    } 
     382} 
     383 
     384void make_pixel_list(char * ptr) { 
     385    int x,y; 
     386    struct point *pixel; 
     387    char *endptr; 
     388    while(*ptr) { 
     389        while (*ptr==' ' || *ptr=='\t') ++ptr;    // whitespaces 
     390        x=strtol(ptr, &endptr, 0); 
     391        if (endptr != ptr) { 
     392            ptr = endptr; 
     393            if (*ptr++==',') { 
     394                while (*ptr==' ' || *ptr=='\t') ++ptr;    // whitespaces 
     395                    if (*ptr!='\n' && *ptr!='\r') { 
     396                        y=strtol(ptr, &endptr, 0); 
     397                        if (endptr != ptr) { 
     398                            ptr = endptr; 
     399                            pixel=malloc(sizeof(struct point)); 
     400                            if (pixel) { 
     401                                (*pixel).x=x; 
     402                                (*pixel).y=y; 
     403                                (*pixel).next=pixel_list; 
     404                                pixel_list=pixel; 
     405                            } 
     406                        } 
     407                    } 
     408                } 
     409        } 
     410        while (*ptr && *ptr!='\n') ++ptr;    // unless end of line 
     411        if (*ptr) ++ptr; 
     412    } 
    418413} 
    419414 
    420415#define PIXELS_BUF_SIZE 8192 
    421 void load_bad_pixels_list(const char* filename){ 
     416void load_bad_pixels_list(const char* filename) { 
    422417    char *buf; 
    423418    int fd; 
     
    448443int binary_count=0; 
    449444 
    450 void load_bad_pixels_list_b(char* filename){ 
    451  struct stat st; 
    452  long filesize; 
    453  void* ptr; 
    454  FILE *fd; 
    455  binary_count=0; 
    456  if (stat(filename,&st)!=0) return; 
    457  filesize=st.st_size; 
    458  if ((filesize==0) || (filesize%(2*sizeof(short))!=0)) return; 
    459  ptr=malloc(filesize); 
    460  if (!ptr) return; 
    461  fd=fopen(filename, "rb"); 
    462  if (fd) { 
    463   fread(ptr,1, filesize,fd); 
    464   fclose(fd); 
    465   binary_list=ptr; 
    466   binary_count=filesize/(2*sizeof(short)); 
    467  } 
    468  else free(ptr); 
    469 } 
    470  
    471 void unload_bad_pixels_list_b(void){ 
    472  if (binary_list) free(binary_list); 
    473  binary_list=NULL; 
    474  binary_count=0; 
    475 } 
    476  
    477 void patch_bad_pixels_b(void){ 
    478  int i; 
    479  short* ptr=binary_list; 
    480  for (i=0; i<binary_count; i++, ptr+=2) if (get_raw_pixel(ptr[0], ptr[1])==0) patch_bad_pixel(ptr[0], ptr[1]); 
     445void load_bad_pixels_list_b(char* filename) { 
     446    struct stat st; 
     447    long filesize; 
     448    void* ptr; 
     449    FILE *fd; 
     450    binary_count=0; 
     451    if (stat(filename,&st)!=0) return; 
     452    filesize=st.st_size; 
     453    if ((filesize==0) || (filesize%(2*sizeof(short))!=0)) return; 
     454    ptr=malloc(filesize); 
     455    if (!ptr) return; 
     456    fd=fopen(filename, "rb"); 
     457    if (fd) { 
     458        fread(ptr,1, filesize,fd); 
     459        fclose(fd); 
     460        binary_list=ptr; 
     461        binary_count=filesize/(2*sizeof(short)); 
     462    } 
     463    else free(ptr); 
     464} 
     465 
     466void unload_bad_pixels_list_b(void) { 
     467    if (binary_list) free(binary_list); 
     468    binary_list=NULL; 
     469    binary_count=0; 
     470} 
     471 
     472void patch_bad_pixels_b(void) { 
     473    int i; 
     474    short* ptr=binary_list; 
     475    for (i=0; i<binary_count; i++, ptr+=2) if (get_raw_pixel(ptr[0], ptr[1])==0) patch_bad_pixel(ptr[0], ptr[1]); 
    481476} 
    482477/* 
    483 void unpatch_bad_pixels_b(void){ 
    484  int i; 
    485  short* ptr=binary_list; 
    486  for (i=0; i<binary_count; i++, ptr+=2) set_raw_pixel(ptr[0], ptr[1], 0); 
     478void unpatch_bad_pixels_b(void) { 
     479    int i; 
     480    short* ptr=binary_list; 
     481    for (i=0; i<binary_count; i++, ptr+=2) set_raw_pixel(ptr[0], ptr[1], 0); 
    487482} 
    488483*/ 
    489 int badpixel_list_loaded_b(void){ 
    490  return binary_count; 
     484int badpixel_list_loaded_b(void) { 
     485    return binary_count; 
    491486} 
    492487 
    493488// ----------------------------------------------- 
    494489 
    495 enum BadpixelFSM 
    496 { 
     490enum BadpixelFSM { 
    497491    BADPIX_START, 
    498492    BADPIX_S1, 
     
    500494}; 
    501495 
    502 int badpixel_task_stack(long p) 
    503 { 
     496int badpixel_task_stack(long p) { 
    504497    static unsigned int badpix_cnt1; 
    505498 
    506     switch(p) 
    507     { 
    508  
    509     case BADPIX_START: 
    510         action_pop(); 
    511  
    512         console_clear(); 
    513         console_add_line("Wait please... "); 
    514         console_add_line("This takes a few seconds,"); 
    515         console_add_line("don't panic!"); 
    516  
    517         init_badpixel_bin_flag = INIT_BADPIXEL_COUNT; 
    518  
    519         shooting_set_tv96_direct(96, SET_LATER); 
    520         action_push(BADPIX_S1); 
    521         action_push(AS_SHOOT); 
    522         action_push_delay(3000); 
    523         break; 
    524     case BADPIX_S1: 
    525         action_pop();         
    526  
    527         badpix_cnt1 = init_badpixel_bin_flag; 
    528         init_badpixel_bin_flag = INIT_BADPIXEL_FILE; 
    529         shooting_set_tv96_direct(96, SET_LATER); 
    530  
    531         action_push(BADPIX_S2); 
    532         action_push(AS_SHOOT); 
    533         break; 
    534     case BADPIX_S2: 
    535         action_pop(); 
    536  
    537         console_clear(); 
    538         if (badpix_cnt1 == init_badpixel_bin_flag) 
    539         { 
    540             // TODO script asked confirmation first 
    541             // should sanity check bad pixel count at least, 
    542             /// wrong buffer address could make badpixel bigger than available mem 
    543             char msg[32]; 
    544             console_add_line("badpixel.bin created."); 
    545             sprintf(msg, "Bad pixel count: %d", badpix_cnt1); 
    546             console_add_line(msg); 
    547             DeleteFile_Fut(PATH_BADPIXEL_BIN); 
    548             RenameFile_Fut(PATH_BAD_TMP_BIN,PATH_BADPIXEL_BIN); 
    549         } 
    550         else 
    551         { 
    552             console_add_line("badpixel.bin failed."); 
    553             console_add_line("Please try again."); 
    554         } 
    555         init_badpixel_bin_flag = 0; 
    556         DeleteFile_Fut(PATH_BAD_TMP_BIN); 
    557  
    558         action_push_delay(3000); 
    559         break; 
    560     default: 
    561         action_stack_standard(p); 
    562         break; 
     499    switch(p) { 
     500        case BADPIX_START: 
     501            action_pop(); 
     502 
     503            console_clear(); 
     504            console_add_line("Wait please... "); 
     505            console_add_line("This takes a few seconds,"); 
     506            console_add_line("don't panic!"); 
     507 
     508            init_badpixel_bin_flag = INIT_BADPIXEL_COUNT; 
     509 
     510            shooting_set_tv96_direct(96, SET_LATER); 
     511            action_push(BADPIX_S1); 
     512            action_push(AS_SHOOT); 
     513            action_push_delay(3000); 
     514            break; 
     515        case BADPIX_S1: 
     516            action_pop(); 
     517 
     518            badpix_cnt1 = init_badpixel_bin_flag; 
     519            init_badpixel_bin_flag = INIT_BADPIXEL_FILE; 
     520            shooting_set_tv96_direct(96, SET_LATER); 
     521 
     522            action_push(BADPIX_S2); 
     523            action_push(AS_SHOOT); 
     524            break; 
     525        case BADPIX_S2: 
     526            action_pop(); 
     527 
     528            console_clear(); 
     529            if (badpix_cnt1 == init_badpixel_bin_flag) { 
     530                // TODO script asked confirmation first 
     531                // should sanity check bad pixel count at least, 
     532                // wrong buffer address could make badpixel bigger than available mem 
     533                char msg[32]; 
     534                console_add_line("badpixel.bin created."); 
     535                sprintf(msg, "Bad pixel count: %d", badpix_cnt1); 
     536                console_add_line(msg); 
     537                DeleteFile_Fut(PATH_BADPIXEL_BIN); 
     538                RenameFile_Fut(PATH_BAD_TMP_BIN,PATH_BADPIXEL_BIN); 
     539            } else { 
     540                console_add_line("badpixel.bin failed."); 
     541                console_add_line("Please try again."); 
     542            } 
     543            init_badpixel_bin_flag = 0; 
     544            DeleteFile_Fut(PATH_BAD_TMP_BIN); 
     545 
     546            action_push_delay(3000); 
     547            break; 
     548        default: 
     549            action_stack_standard(p); 
     550            break; 
    563551    } 
    564552 
     
    567555 
    568556 
    569 void create_badpixel_bin() 
    570 { 
    571     if (!(mode_get() & MODE_REC)) 
    572     { 
     557void create_badpixel_bin() { 
     558    if (!(mode_get() & MODE_REC)) { 
    573559        gui_mbox_init(LANG_ERROR, LANG_MSG_RECMODE_REQUIRED, MBOX_BTN_OK|MBOX_TEXT_CENTER, NULL); 
    574560        return; 
    575561    } 
    576          
     562 
    577563    gui_set_mode(GUI_MODE_ALT); 
    578564    action_stack_create(&badpixel_task_stack, BADPIX_START); 
Note: See TracChangeset for help on using the changeset viewer.