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