| 1 | #include "platform.h" |
|---|
| 2 | #include "conf.h" |
|---|
| 3 | #include "stdlib.h" |
|---|
| 4 | #include "raw.h" |
|---|
| 5 | #if CURVES |
|---|
| 6 | #include "curves.h" |
|---|
| 7 | #endif |
|---|
| 8 | #include "shot_histogram.h" |
|---|
| 9 | |
|---|
| 10 | //------------------------------------------------------------------- |
|---|
| 11 | #define RAW_TARGET_DIRECTORY "A/DCIM/%03dCANON" |
|---|
| 12 | //#define RAW_TMP_FILENAME "HDK_RAW.TMP" |
|---|
| 13 | #define RAW_TARGET_FILENAME "%s%04d%s" |
|---|
| 14 | |
|---|
| 15 | //------------------------------------------------------------------- |
|---|
| 16 | static char fn[64]; |
|---|
| 17 | static char dir[32]; |
|---|
| 18 | static int develop_raw=0; |
|---|
| 19 | //------------------------------------------------------------------- |
|---|
| 20 | void raw_prepare_develop(char* filename){ |
|---|
| 21 | develop_raw=1; |
|---|
| 22 | strcpy(fn,filename); |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | //------------------------------------------------------------------- |
|---|
| 26 | void patch_bad_pixels(void); |
|---|
| 27 | //------------------------------------------------------------------- |
|---|
| 28 | int raw_savefile() { |
|---|
| 29 | int fd, m=(mode_get()&MODE_SHOOTING_MASK); |
|---|
| 30 | static struct utimbuf t; |
|---|
| 31 | |
|---|
| 32 | if (state_kbd_script_run && shot_histogram_enabled) build_shot_histogram(); |
|---|
| 33 | |
|---|
| 34 | if (develop_raw) { |
|---|
| 35 | started(); |
|---|
| 36 | fd = open(fn, O_RDONLY, 0777); |
|---|
| 37 | if (fd>=0) { |
|---|
| 38 | read(fd, hook_raw_image_addr(), hook_raw_size()); |
|---|
| 39 | close(fd); |
|---|
| 40 | } |
|---|
| 41 | #if CURVES |
|---|
| 42 | if (conf.curve_enable) curve_apply(); |
|---|
| 43 | #endif |
|---|
| 44 | finished(); |
|---|
| 45 | develop_raw=0; |
|---|
| 46 | return 0; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | if (conf.bad_pixel_removal) patch_bad_pixels(); |
|---|
| 50 | |
|---|
| 51 | shooting_bracketing(); |
|---|
| 52 | |
|---|
| 53 | // got here second time in a row. Skip second RAW saving. |
|---|
| 54 | if (conf.raw_save_first_only && state_shooting_progress == SHOOTING_PROGRESS_PROCESSING) { |
|---|
| 55 | return 0; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | state_shooting_progress = SHOOTING_PROGRESS_PROCESSING; |
|---|
| 59 | |
|---|
| 60 | if (conf.save_raw && (!((movie_status > 1) && conf.save_raw_in_video )) && (!((m==MODE_SPORTS) && conf.save_raw_in_sports)) && (!((shooting_get_prop(PROPCASE_DRIVE_MODE)==1) && conf.save_raw_in_burst && !(m==MODE_SPORTS))) && (!((shooting_get_prop(PROPCASE_DRIVE_MODE)>=2) && conf.save_raw_in_timer)) && (!((shooting_get_prop(PROPCASE_BRACKET_MODE)==1) && conf.save_raw_in_ev_bracketing)) ) { |
|---|
| 61 | long v; |
|---|
| 62 | |
|---|
| 63 | started(); |
|---|
| 64 | |
|---|
| 65 | t.actime = t.modtime = time(NULL); |
|---|
| 66 | |
|---|
| 67 | mkdir("A/DCIM"); |
|---|
| 68 | sprintf(dir, RAW_TARGET_DIRECTORY, (conf.raw_in_dir)?get_target_dir_num():100); |
|---|
| 69 | mkdir(dir); |
|---|
| 70 | |
|---|
| 71 | sprintf(fn, "%s/", dir); |
|---|
| 72 | sprintf(fn+strlen(fn), RAW_TARGET_FILENAME, img_prefixes[conf.raw_prefix], get_target_file_num(), img_exts[conf.raw_ext]); |
|---|
| 73 | |
|---|
| 74 | fd = open(fn, O_WRONLY|O_CREAT, 0777); |
|---|
| 75 | if (fd>=0) { |
|---|
| 76 | write(fd, hook_raw_image_addr(), hook_raw_size()); |
|---|
| 77 | close(fd); |
|---|
| 78 | utime(fn, &t); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | finished(); |
|---|
| 82 | |
|---|
| 83 | return (fd >= 0); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | #if CURVES |
|---|
| 87 | if (conf.curve_enable) curve_apply(); |
|---|
| 88 | #endif |
|---|
| 89 | return 0; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | //------------------------------------------------------------------- |
|---|
| 93 | void raw_postprocess() { |
|---|
| 94 | /* |
|---|
| 95 | // int fd; |
|---|
| 96 | long v=get_file_counter(); |
|---|
| 97 | static struct utimbuf t; |
|---|
| 98 | |
|---|
| 99 | started(); |
|---|
| 100 | |
|---|
| 101 | // sprintf(fn, RAW_TARGET_FILENAME, (v>>18)&0x3FF, (v>>4)&0x3FFF); |
|---|
| 102 | sprintf(fn, RAW_TARGET_FILENAME, prefixes[conf.raw_prefix], (v>>4)&0x3FFF, exts[conf.raw_ext]); |
|---|
| 103 | chdir(dir); |
|---|
| 104 | rename(RAW_TMP_FILENAME, fn); |
|---|
| 105 | |
|---|
| 106 | t.actime = t.modtime = time(NULL); |
|---|
| 107 | utime(fn, &t); |
|---|
| 108 | |
|---|
| 109 | // chdir("A/DCIM"); |
|---|
| 110 | // fd = open( "A/1", O_RDONLY, 0 ); |
|---|
| 111 | // eeee = fd; |
|---|
| 112 | // xxxx = ((int (*)(int fd, int function, int arg))(0xFFEBBF9C))(fd, 47, (int)"A/DCIM/1"); //move |
|---|
| 113 | // close(fd); |
|---|
| 114 | // xxxx=rename("1", "DCIM/1"); |
|---|
| 115 | // eeee=*((int*)0xA021C); //errno |
|---|
| 116 | |
|---|
| 117 | finished(); |
|---|
| 118 | */ |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | //------------------------------------------------------------------- |
|---|
| 122 | |
|---|
| 123 | void set_raw_pixel(unsigned int x, unsigned int y, unsigned short value){ |
|---|
| 124 | unsigned char* addr=hook_raw_image_addr()+y*RAW_ROWLEN+(x/8)*10; |
|---|
| 125 | switch (x%8) { |
|---|
| 126 | case 0: addr[0]=(addr[0]&0x3F)|(value<<6); addr[1]=value>>2; break; |
|---|
| 127 | case 1: addr[0]=(addr[0]&0xC0)|(value>>4); addr[3]=(addr[3]&0x0F)|(value<<4); break; |
|---|
| 128 | case 2: addr[2]=(addr[2]&0x03)|(value<<2); addr[3]=(addr[3]&0xF0)|(value>>6); break; |
|---|
| 129 | case 3: addr[2]=(addr[2]&0xFC)|(value>>8); addr[5]=value; break; |
|---|
| 130 | case 4: addr[4]=value>>2; addr[7]=(addr[7]&0x3F)|(value<<6); break; |
|---|
| 131 | case 5: addr[6]=(addr[6]&0x0F)|(value<<4); addr[7]=(addr[7]&0xC0)|(value>>4); break; |
|---|
| 132 | case 6: addr[6]=(addr[6]&0xF0)|(value>>6); addr[9]=(addr[9]&0x03)|(value<<2); break; |
|---|
| 133 | case 7: addr[8]=value; addr[9]=(addr[9]&0xFC)|(value>>8); break; |
|---|
| 134 | } |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | //------------------------------------------------------------------- |
|---|
| 138 | unsigned short get_raw_pixel(unsigned int x,unsigned int y){ |
|---|
| 139 | unsigned char* addr=hook_raw_image_addr()+y*RAW_ROWLEN+(x/8)*10; |
|---|
| 140 | switch (x%8) { |
|---|
| 141 | case 0: return ((0x3fc&(((unsigned short)addr[1])<<2)) | (addr[0] >> 6)); |
|---|
| 142 | case 1: return ((0x3f0&(((unsigned short)addr[0])<<4)) | (addr[3] >> 4)); |
|---|
| 143 | case 2: return ((0x3c0&(((unsigned short)addr[3])<<6)) | (addr[2] >> 2)); |
|---|
| 144 | case 3: return ((0x300&(((unsigned short)addr[2])<<8)) | (addr[5])); |
|---|
| 145 | case 4: return ((0x3fc&(((unsigned short)addr[4])<<2)) | (addr[7] >> 6)); |
|---|
| 146 | case 5: return ((0x3f0&(((unsigned short)addr[7])<<4)) | (addr[6] >> 4)); |
|---|
| 147 | case 6: return ((0x3c0&(((unsigned short)addr[6])<<6)) | (addr[9] >> 2)); |
|---|
| 148 | case 7: return ((0x300&(((unsigned short)addr[9])<<8)) | (addr[8])); |
|---|
| 149 | } |
|---|
| 150 | return 0; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | //------------------------------------------------------------------- |
|---|
| 154 | void patch_bad_pixel(unsigned int x,unsigned int y){ |
|---|
| 155 | if ((x>=2) && (x<CAM_RAW_ROWPIX-2) && (y>=2) && (y<CAM_RAW_ROWS-2)) { |
|---|
| 156 | if (conf.bad_pixel_removal==1) // interpolation |
|---|
| 157 | set_raw_pixel(x,y,(get_raw_pixel(x-2,y)+get_raw_pixel(x+2,y)+get_raw_pixel(x,y-2)+get_raw_pixel(x,y+2))/4); |
|---|
| 158 | else if (conf.bad_pixel_removal==2) // or this makes RAW converter (internal/external) |
|---|
| 159 | set_raw_pixel(x,y,0); |
|---|
| 160 | } |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | struct point{ |
|---|
| 164 | int x; |
|---|
| 165 | int y; |
|---|
| 166 | struct point *next; |
|---|
| 167 | } *pixel_list=NULL; |
|---|
| 168 | |
|---|
| 169 | void patch_bad_pixels(void){ |
|---|
| 170 | struct point *pixel=pixel_list; |
|---|
| 171 | while (pixel){ |
|---|
| 172 | patch_bad_pixel((*pixel).x,(*pixel).y); |
|---|
| 173 | pixel=(*pixel).next; |
|---|
| 174 | } |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | void make_pixel_list(char * ptr){ |
|---|
| 178 | int x,y; |
|---|
| 179 | struct point *pixel; |
|---|
| 180 | char *endptr; |
|---|
| 181 | while(*ptr){ |
|---|
| 182 | while (*ptr==' ' || *ptr=='\t') ++ptr; // whitespaces |
|---|
| 183 | x=strtol(ptr, &endptr, 0); |
|---|
| 184 | if (endptr != ptr) { |
|---|
| 185 | ptr = endptr; |
|---|
| 186 | if (*ptr++==',') { |
|---|
| 187 | while (*ptr==' ' || *ptr=='\t') ++ptr; // whitespaces |
|---|
| 188 | if (*ptr!='\n' && *ptr!='\r'){ |
|---|
| 189 | y=strtol(ptr, &endptr, 0); |
|---|
| 190 | if (endptr != ptr) { |
|---|
| 191 | ptr = endptr; |
|---|
| 192 | pixel=malloc(sizeof(struct point)); |
|---|
| 193 | if (pixel) { |
|---|
| 194 | (*pixel).x=x; |
|---|
| 195 | (*pixel).y=y; |
|---|
| 196 | (*pixel).next=pixel_list; |
|---|
| 197 | pixel_list=pixel; |
|---|
| 198 | } |
|---|
| 199 | } |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | } |
|---|
| 203 | while (*ptr && *ptr!='\n') ++ptr; // unless end of line |
|---|
| 204 | if (*ptr) ++ptr; |
|---|
| 205 | } |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | #define PIXELS_BUF_SIZE 8192 |
|---|
| 209 | void load_bad_pixels_list(char* filename){ |
|---|
| 210 | char *buf; |
|---|
| 211 | int fd; |
|---|
| 212 | |
|---|
| 213 | if (filename) { |
|---|
| 214 | buf = umalloc(PIXELS_BUF_SIZE); |
|---|
| 215 | if (!buf) return; |
|---|
| 216 | |
|---|
| 217 | fd = open(filename, O_RDONLY, 0777); |
|---|
| 218 | if (fd>=0) { |
|---|
| 219 | int rcnt = read(fd, buf, PIXELS_BUF_SIZE); |
|---|
| 220 | if (rcnt > 0) { |
|---|
| 221 | if (rcnt == PIXELS_BUF_SIZE) |
|---|
| 222 | buf[PIXELS_BUF_SIZE-1] = 0; |
|---|
| 223 | else |
|---|
| 224 | buf[rcnt] = 0; |
|---|
| 225 | } |
|---|
| 226 | close(fd); |
|---|
| 227 | } |
|---|
| 228 | make_pixel_list(buf); |
|---|
| 229 | ufree(buf); |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | } |
|---|