Changeset 1497
- Timestamp:
- 12/22/11 04:57:12 (17 months ago)
- Location:
- trunk/core
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/core/conf.c
r1490 r1497 6 6 #include "camera.h" 7 7 #include "raw.h" 8 #include "dng.h" 8 9 #include "gui_draw.h" 9 10 #include "gui_osd.h" -
trunk/core/dng.c
r1460 r1497 6 6 #include "string.h" 7 7 #include "platform.h" 8 #include "conf.h" 8 9 #include "math.h" 10 #include "console.h" 9 11 #include "dng.h" 12 #include "raw.h" 13 #include "action_stack.h" 14 #include "gui_mbox.h" 15 #include "gui_lang.h" 16 17 //thumbnail 18 #define DNG_TH_WIDTH 128 19 #define DNG_TH_HEIGHT 96 20 // higly recommended that DNG_TH_WIDTH*DNG_TH_HEIGHT would be divisible by 512 21 22 struct dir_entry{unsigned short tag; unsigned short type; unsigned int count; unsigned int offset;}; 23 24 #define T_BYTE 1 25 #define T_ASCII 2 26 #define T_SHORT 3 27 #define T_LONG 4 28 #define T_RATIONAL 5 29 #define T_SBYTE 6 30 #define T_UNDEFINED 7 31 #define T_SSHORT 8 32 #define T_SLONG 9 33 #define T_SRATIONAL 10 34 #define T_FLOAT 11 35 #define T_DOUBLE 12 36 37 #define CAM_DEFAULT_CROP_ORIGIN_W ((CAM_ACTIVE_AREA_X2-CAM_ACTIVE_AREA_X1-CAM_JPEG_WIDTH )/2) 38 #define CAM_DEFAULT_CROP_ORIGIN_H ((CAM_ACTIVE_AREA_Y2-CAM_ACTIVE_AREA_Y1-CAM_JPEG_HEIGHT)/2) 39 40 unsigned short get_exp_program_for_exif(int exp_program); 41 unsigned short get_orientation_for_exif(short orientation); 42 unsigned short get_flash_mode_for_exif(short mode, short fired); 43 unsigned short get_metering_mode_for_exif(short metering_mode); 10 44 11 45 const int cam_DefaultCropSize[]={CAM_JPEG_WIDTH, CAM_JPEG_HEIGHT}; // jpeg size … … 85 119 {0x115, T_SHORT, 1, 1}, // SamplesPerPixel: 1 86 120 {0x116, T_SHORT, 1, CAM_RAW_ROWS}, //RowsPerStrip 87 {0x117, T_LONG, 1, CAM_RAW_ROW PIX*CAM_RAW_ROWS*CAM_SENSOR_BITS_PER_PIXEL/8}, // StripByteCounts = CHDK RAW size121 {0x117, T_LONG, 1, CAM_RAW_ROWS*RAW_ROWLEN}, // StripByteCounts = CHDK RAW size 88 122 {0x11A, T_RATIONAL, 1, (int)cam_Resolution}, // XResolution 89 123 {0x11B, T_RATIONAL, 1, (int)cam_Resolution}, // YResolution … … 101 135 102 136 137 static int cam_shutter[2] = { 0, 1000000 }; // Shutter speed 138 static int cam_aperture[2] = { 0, 10 }; // Aperture 139 static char cam_datetime[20] = ""; // DateTimeOriginal 140 static int cam_apex_shutter[2] = { 0, 96 }; // Shutter speed in APEX units 141 static int cam_apex_aperture[2] = { 0, 96 }; // Aperture in APEX units 142 static int cam_exp_bias[2] = { 0, 96 }; 143 static int cam_max_av[2] = { 0, 96 }; 144 static int cam_focal_length[2] = { 0, 1000 }; 145 103 146 struct dir_entry EXIF_IFD[]={ 104 {0x829A, T_RATIONAL, 1, 0}, //ExposureTime105 {0x829D, T_RATIONAL, 1, 0}, //FNumber106 {0x8822, T_SHORT, 1, 0}, //ExposureProgram107 {0x8827, T_SHORT, 1, 0}, //ISOSpeedRatings108 {0x9000, T_UNDEFINED, 4, 0x31323230}, // ExifVersion: 2.21109 {0x9003, T_ASCII, 20, 0},// DateTimeOriginal110 {0x9201, T_SRATIONAL, 1, 0}, // ShutterSpeedValue111 {0x9202, T_RATIONAL, 1, 0}, // ApertureValue112 {0x9204, T_SRATIONAL, 1, 0},// ExposureBias113 {0x9205, T_RATIONAL, 1, 0}, //MaxApertureValue114 {0x9207, T_SHORT, 1, 0}, // Metering mode115 {0x9209, T_SHORT, 1, 0}, // Flash mode116 {0x920A, T_RATIONAL, 1, 0}, //FocalLength117 {0xA405, T_SHORT, 1, 0}, //FocalLengthIn35mmFilm147 {0x829A, T_RATIONAL, 1, (int)cam_shutter}, // Shutter speed 148 {0x829D, T_RATIONAL, 1, (int)cam_aperture}, // Aperture 149 {0x8822, T_SHORT, 1, 0}, // ExposureProgram 150 {0x8827, T_SHORT, 1, 0}, // ISOSpeedRatings 151 {0x9000, T_UNDEFINED, 4, 0x31323230}, // ExifVersion: 2.21 152 {0x9003, T_ASCII, 20, (int)cam_datetime}, // DateTimeOriginal 153 {0x9201, T_SRATIONAL, 1, (int)cam_apex_shutter}, // ShutterSpeedValue (APEX units) 154 {0x9202, T_RATIONAL, 1, (int)cam_apex_aperture}, // ApertureValue (APEX units) 155 {0x9204, T_SRATIONAL, 1, (int)cam_exp_bias}, // ExposureBias 156 {0x9205, T_RATIONAL, 1, (int)cam_max_av}, // MaxApertureValue 157 {0x9207, T_SHORT, 1, 0}, // Metering mode 158 {0x9209, T_SHORT, 1, 0}, // Flash mode 159 {0x920A, T_RATIONAL, 1, (int)cam_focal_length}, // FocalLength 160 {0xA405, T_SHORT, 1, 0}, // FocalLengthIn35mmFilm 118 161 {0} 119 162 }; … … 167 210 #define TIFF_HDR_SIZE (8) 168 211 169 char* tmp_buf; 170 int tmp_buf_size; 171 int tmp_buf_offset; 172 void add_to_buf(void* var, int size){ 173 memcpy(tmp_buf+tmp_buf_offset,var,size); 174 tmp_buf_offset+=size; 175 } 176 177 void create_dng_header(struct t_data_for_exif* exif_data){ 212 char* dng_header_buf; 213 int dng_header_buf_size; 214 int dng_header_buf_offset; 215 char *thumbnail_buf; 216 217 void add_to_buf(void* var, int size) 218 { 219 memcpy(dng_header_buf+dng_header_buf_offset,var,size); 220 dng_header_buf_offset+=size; 221 } 222 223 struct t_data_for_exif{ 224 short iso; 225 int exp_program; 226 int effective_focal_length; 227 short orientation; 228 short flash_mode; 229 short flash_fired; 230 short metering_mode; 231 }; 232 233 static struct t_data_for_exif exif_data; 234 235 void create_dng_header(){ 178 236 int var; 179 237 int i,j; … … 209 267 case 0xC614: IFD_LIST[j].entry[i].count = strlen((char*)IFD_LIST[j].entry[i].offset) + 1; break; // UniqueCameraModel 210 268 case 0x132 : 211 case 0x9003: IFD_LIST[j].entry[i].offset=(int)get_date_for_exif(exif_data->time); break; //DateTimeOriginal 212 case 0x8827: IFD_LIST[j].entry[i].offset=exif_data->iso; break;//ISOSpeedRatings 213 case 0x829D: IFD_LIST[j].entry[i].offset=(int)get_av_for_exif(exif_data->av); break; //FNumber 214 case 0x829A: IFD_LIST[j].entry[i].offset=(int)get_tv_for_exif(exif_data->tv); break; //ExposureTime 215 case 0x9205: IFD_LIST[j].entry[i].offset=(int)get_max_av_for_exif(exif_data->max_av); break; //MaxApertureValue 216 case 0x9204: IFD_LIST[j].entry[i].offset=(int)get_exp_bias_for_exif(exif_data->exp_bias); break; //ExposureBias 217 case 0x8822: IFD_LIST[j].entry[i].offset=get_exp_program_for_exif(exif_data->exp_program); break;//ExposureProgram 218 case 0x920A: IFD_LIST[j].entry[i].offset=(int)get_focal_length_for_exif(exif_data->focal_length); break; //FocalLength 219 case 0xA405: IFD_LIST[j].entry[i].offset=exif_data->effective_focal_length/1000; break; ////FocalLengthIn35mmFilm 220 case 0x0112: IFD_LIST[j].entry[i].offset=get_orientation_for_exif(exif_data->orientation); break; //Orientation 221 case 0x9209: IFD_LIST[j].entry[i].offset=get_flash_mode_for_exif(exif_data->flash_mode, exif_data->flash_fired); break; //Flash mode 222 case 0x9207: IFD_LIST[j].entry[i].offset=get_metering_mode_for_exif(exif_data->metering_mode); break; // Metering mode 223 case 0x9201: IFD_LIST[j].entry[i].offset=(int)get_shutter_speed_for_exif(exif_data->tv); break; // ShutterSpeedValue 224 case 0x9202: IFD_LIST[j].entry[i].offset=(int)get_aperture_for_exif(exif_data->av); break; // ApertureValue 269 case 0x8827: IFD_LIST[j].entry[i].offset=exif_data.iso; break;//ISOSpeedRatings 270 case 0x8822: IFD_LIST[j].entry[i].offset=get_exp_program_for_exif(exif_data.exp_program); break;//ExposureProgram 271 case 0xA405: IFD_LIST[j].entry[i].offset=exif_data.effective_focal_length/1000; break; ////FocalLengthIn35mmFilm 272 case 0x0112: IFD_LIST[j].entry[i].offset=get_orientation_for_exif(exif_data.orientation); break; //Orientation 273 case 0x9209: IFD_LIST[j].entry[i].offset=get_flash_mode_for_exif(exif_data.flash_mode, exif_data.flash_fired); break; //Flash mode 274 case 0x9207: IFD_LIST[j].entry[i].offset=get_metering_mode_for_exif(exif_data.metering_mode); break; // Metering mode 225 275 #if defined(OPT_GPS) 226 276 case 0x0001: IFD_LIST[j].entry[i].offset=gps.latitudeRef; break; … … 257 307 // creating buffer for writing data 258 308 raw_offset=(raw_offset/512+1)*512; // exlusively for CHDK fast file writing 259 tmp_buf_size=raw_offset; 260 tmp_buf=umalloc(raw_offset); 261 tmp_buf_offset=0; 262 if (!tmp_buf) return; 309 dng_header_buf_size=raw_offset; 310 dng_header_buf=umalloc(raw_offset); 311 dng_header_buf_offset=0; 312 if (!dng_header_buf) return; 313 314 // create buffer for thumbnail 315 thumbnail_buf = malloc(DNG_TH_WIDTH*DNG_TH_HEIGHT*3); 316 if (!thumbnail_buf) 317 { 318 ufree(dng_header_buf); 319 dng_header_buf = 0; 320 return; 321 } 263 322 264 323 // writing offsets for EXIF IFD and RAW data and calculating offset for extra data … … 327 386 } 328 387 329 330 388 // writing zeros to tail of dng header (just for fun) 331 for (i=tmp_buf_offset; i<tmp_buf_size; i++) tmp_buf[i]=0; 332 333 } 334 335 void free_dng_header(void){ 336 ufree(tmp_buf); 337 tmp_buf=NULL; 338 } 339 340 char* get_dng_header(void){ 341 return tmp_buf; 342 } 343 344 int get_dng_header_size(void){ 345 return tmp_buf_size; 346 } 347 348 char *get_date_for_exif(unsigned long time){ 349 static char buf[20]; 350 struct tm *ttm; 351 ttm = localtime(&time); 352 sprintf(buf, "%04d:%02d:%02d %02d:%02d:%02d", ttm->tm_year+1900, ttm->tm_mon+1, ttm->tm_mday, ttm->tm_hour, ttm->tm_min, ttm->tm_sec); 353 return buf; 354 } 355 356 unsigned int* get_av_for_exif(short av){ 357 static unsigned int fnumber[2]={0,10}; 358 fnumber[0]=10*pow(2,av/192.0); 359 return fnumber; 360 } 361 362 int* get_tv_for_exif(short tv){ 363 static int exp_time[2]={0,1000000}; 364 exp_time[0]=1000000*pow(2,-tv/96.0); 365 return exp_time; 366 } 367 368 unsigned int* get_max_av_for_exif(short max_av){ 369 static unsigned int mav[2]={0,96}; 370 mav[0]=max_av; 371 return mav; 372 } 373 374 int* get_exp_bias_for_exif(short exp_bias){ 375 static int bias[2]={0,96}; 376 bias[0]=exp_bias; 377 return bias; 378 } 379 380 int* get_shutter_speed_for_exif(short tv){ 381 static int speed[2]={0,96}; 382 speed[0]=tv; 383 return speed; 384 } 385 386 int* get_aperture_for_exif(short av){ 387 static int aperture[2]={0,96}; 388 aperture[0]=av; 389 return aperture; 390 } 391 389 for (i=dng_header_buf_offset; i<dng_header_buf_size; i++) dng_header_buf[i]=0; 390 } 391 392 void free_dng_header(void) 393 { 394 if (dng_header_buf) 395 { 396 ufree(dng_header_buf); 397 dng_header_buf=NULL; 398 } 399 if (thumbnail_buf) 400 { 401 free(thumbnail_buf); 402 thumbnail_buf = 0; 403 } 404 } 392 405 393 406 unsigned short get_exp_program_for_exif(int exp_program){ … … 399 412 default: return 0; 400 413 } 401 }402 403 unsigned int* get_focal_length_for_exif(int focal_length){404 static unsigned int fl[2]={0,1000};405 fl[0]=focal_length;406 return fl;407 414 } 408 415 … … 436 443 } 437 444 438 struct t_data_for_exif* capture_data_for_exif(void){ 439 static struct t_data_for_exif data; 445 void capture_data_for_exif(void) 446 { 447 short short_prop_val; 448 unsigned long datetime; 449 struct tm *ttm; 440 450 extern volatile long shutter_open_time; // defined in platform/generic/capt_seq.c 441 451 int wb[3]; 442 data.iso=shooting_get_iso_market(); 443 get_property_case(PROPCASE_TV, &data.tv, sizeof(data.tv)); 444 if (shutter_open_time) { data.time=shutter_open_time+pow(2,-data.tv/96.0); shutter_open_time=0;} // shutter closing time 445 else data.time=time(NULL); 446 get_property_case(PROPCASE_AV, &data.av, sizeof(data.av)); 447 get_property_case(PROPCASE_MIN_AV, &data.max_av, sizeof(data.max_av)); 448 get_property_case(PROPCASE_EV_CORRECTION_2, &data.exp_bias, sizeof(data.exp_bias)); 449 data.exp_program=mode_get() & MODE_SHOOTING_MASK; 450 data.focal_length=get_focal_length(shooting_get_zoom()); 451 data.effective_focal_length=get_effective_focal_length(shooting_get_zoom()); 452 get_property_case(PROPCASE_ORIENTATION_SENSOR, &data.orientation, sizeof(data.orientation)); 452 453 exif_data.iso=shooting_get_iso_market(); 454 455 // Shutter speed tags 456 get_property_case(PROPCASE_TV, &short_prop_val, sizeof(short_prop_val)); 457 cam_shutter[0] = 1000000 * pow(2,-short_prop_val / 96.0); 458 cam_apex_shutter[0] = short_prop_val; 459 460 // Date & time tag (note - uses shutter speed from 'short_prop_val' code above) 461 if (shutter_open_time) { datetime = shutter_open_time+pow(2,-short_prop_val/96.0); shutter_open_time=0;} // shutter closing time 462 else datetime = time(NULL); 463 ttm = localtime(&datetime); 464 sprintf(cam_datetime, "%04d:%02d:%02d %02d:%02d:%02d", ttm->tm_year+1900, ttm->tm_mon+1, ttm->tm_mday, ttm->tm_hour, ttm->tm_min, ttm->tm_sec); 465 466 get_property_case(PROPCASE_AV, &short_prop_val, sizeof(short_prop_val)); 467 cam_aperture[0] = 10 * pow(2,short_prop_val / 192.0); 468 cam_apex_aperture[0] = short_prop_val; 469 470 get_property_case(PROPCASE_MIN_AV, &short_prop_val, sizeof(short_prop_val)); 471 cam_max_av[0] = short_prop_val; 472 473 get_property_case(PROPCASE_EV_CORRECTION_2, &short_prop_val, sizeof(short_prop_val)); 474 cam_exp_bias[0] = short_prop_val; 475 476 exif_data.exp_program=mode_get() & MODE_SHOOTING_MASK; 477 478 cam_focal_length[0] = get_focal_length(shooting_get_zoom()); 479 exif_data.effective_focal_length = get_effective_focal_length(shooting_get_zoom()); 480 481 get_property_case(PROPCASE_ORIENTATION_SENSOR, &exif_data.orientation, sizeof(exif_data.orientation)); 453 482 get_parameter_data(PARAM_CAMERA_NAME, &cam_name, sizeof(cam_name)); 454 get_property_case(PROPCASE_FLASH_MODE, &data.flash_mode, sizeof(data.flash_mode)); 455 get_property_case(PROPCASE_FLASH_FIRE, &data.flash_fired, sizeof(data.flash_fired)); 456 get_property_case(PROPCASE_METERING_MODE, &data.metering_mode, sizeof(data.metering_mode)); 483 get_property_case(PROPCASE_FLASH_MODE, &exif_data.flash_mode, sizeof(exif_data.flash_mode)); 484 get_property_case(PROPCASE_FLASH_FIRE, &exif_data.flash_fired, sizeof(exif_data.flash_fired)); 485 get_property_case(PROPCASE_METERING_MODE, &exif_data.metering_mode, sizeof(exif_data.metering_mode)); 486 457 487 get_property_case(PROPCASE_WB_ADJ, &wb, sizeof(wb)); 458 488 cam_AsShotNeutral[1]=wb[1]; 459 489 cam_AsShotNeutral[3]=wb[0]; 460 490 cam_AsShotNeutral[5]=wb[2]; 461 return &data; 462 } 491 } 492 493 //------------------------------------------------------------------- 463 494 464 495 void convert_dng_to_chdk_raw(char* fn){ … … 503 534 } 504 535 536 //------------------------------------------------------------------- 537 // Functions for creating DNG thumbnail image 538 539 static unsigned char gamma[256]; 540 541 void fill_gamma_buf(void) { 542 int i; 543 if (gamma[255]) return; 544 #if defined(CAMERA_sx30) || defined(CAMERA_sx40hs) || defined(CAMERA_g12) || defined(CAMERA_ixus310_elph500hs) 545 for (i=0; i<12; i++) gamma[i]=255*pow(i/255.0, 0.5); 546 for (i=12; i<64; i++) gamma[i]=255*pow(i/255.0, 0.4); 547 for (i=64; i<=255; i++) gamma[i]=255*pow(i/255.0, 0.25); 548 #else 549 for (i=0; i<=255; i++) gamma[i]=255*pow(i/255.0, 0.5); 550 #endif 551 } 552 553 void create_thumbnail() { 554 register unsigned int i, j, x, y; 555 register char *buf = thumbnail_buf; 556 557 for (i=0; i<DNG_TH_HEIGHT; i++) 558 for (j=0; j<DNG_TH_WIDTH; j++) 559 { 560 x = (CAM_ACTIVE_AREA_X1+((CAM_ACTIVE_AREA_X2-CAM_ACTIVE_AREA_X1)*j)/DNG_TH_WIDTH) & 0xFFFFFFFE; 561 y = (CAM_ACTIVE_AREA_Y1+((CAM_ACTIVE_AREA_Y2-CAM_ACTIVE_AREA_Y1)*i)/DNG_TH_HEIGHT) & 0xFFFFFFFE; 562 563 #if cam_CFAPattern==0x02010100 // Red Green Green Blue 564 *buf++ = gamma[get_raw_pixel(x,y)>>(CAM_SENSOR_BITS_PER_PIXEL-8)]; // red pixel 565 *buf++ = gamma[6*(get_raw_pixel(x+1,y)>>(CAM_SENSOR_BITS_PER_PIXEL-8))/10]; // green pixel 566 *buf++ = gamma[get_raw_pixel(x+1,y+1)>>(CAM_SENSOR_BITS_PER_PIXEL-8)]; // blue pixel 567 #elif cam_CFAPattern==0x01000201 // Green Blue Red Green 568 *buf++ = gamma[get_raw_pixel(x,y+1)>>(CAM_SENSOR_BITS_PER_PIXEL-8)]; // red pixel 569 *buf++ = gamma[6*(get_raw_pixel(x,y)>>(CAM_SENSOR_BITS_PER_PIXEL-8))/10]; // green pixel 570 *buf++ = gamma[get_raw_pixel(x+1,y)>>(CAM_SENSOR_BITS_PER_PIXEL-8)]; // blue pixel 571 #else 572 #error please define new pattern here 573 #endif 574 } 575 } 576 577 //------------------------------------------------------------------- 578 // Functions for handling DNG bad pixel file creation and bad pixel 579 // removal from images. 580 581 #define INIT_BADPIXEL_COUNT -1 582 #define INIT_BADPIXEL_FILE -2 583 584 #define PATH_BADPIXEL_BIN "A/CHDK/badpixel.bin" 585 #define PATH_BAD_TMP_BIN "A/CHDK/bad_tmp.bin" 586 587 int init_badpixel_bin_flag; // contants above to count/create file, > 0 num bad pixel 588 589 int raw_init_badpixel_bin() { 590 int count; 591 unsigned short c[2]; 592 FILE*f; 593 if(init_badpixel_bin_flag == INIT_BADPIXEL_FILE) { 594 f=fopen(PATH_BAD_TMP_BIN,"w+b"); 595 } else if (init_badpixel_bin_flag == INIT_BADPIXEL_COUNT) { 596 f=NULL; 597 } else { 598 return 0; 599 } 600 count = 0; 601 #ifdef DNG_VERT_RLE_BADPIXELS 602 for (c[0]=CAM_ACTIVE_AREA_X1; c[0]<CAM_ACTIVE_AREA_X2; c[0]++) 603 { 604 for (c[1]=CAM_ACTIVE_AREA_Y1; c[1]<CAM_ACTIVE_AREA_Y2; c[1]++) 605 { 606 if (get_raw_pixel(c[0],c[1])==0) 607 { 608 unsigned short l; 609 for (l=0; l<7; l++) if (get_raw_pixel(c[0],c[1]+l+1)!=0) break; 610 c[1] = c[1] | (l << 13); 611 if (f) fwrite(c, 1, 4, f); 612 c[1] = (c[1] & 0x1FFF) + l; 613 count = count + l + 1; 614 } 615 } 616 } 617 #else 618 for (c[1]=CAM_ACTIVE_AREA_Y1; c[1]<CAM_ACTIVE_AREA_Y2; c[1]++) 619 { 620 for (c[0]=CAM_ACTIVE_AREA_X1; c[0]<CAM_ACTIVE_AREA_X2; c[0]++) 621 { 622 if (get_raw_pixel(c[0],c[1])==0) 623 { 624 if (f) fwrite(c, 1, 4, f); 625 count++; 626 } 627 } 628 } 629 #endif 630 if (f) fclose(f); 631 init_badpixel_bin_flag = count; 632 state_shooting_progress = SHOOTING_PROGRESS_PROCESSING; 633 return 1; 634 } 635 636 short* binary_list=NULL; 637 int binary_count=-1; 638 639 void load_bad_pixels_list_b(char* filename) { 640 struct stat st; 641 long filesize; 642 void* ptr; 643 FILE *fd; 644 binary_count=-1; 645 if (stat(filename,&st)!=0) return; 646 filesize=st.st_size; 647 if (filesize%(2*sizeof(short)) != 0) return; 648 if (filesize == 0) { binary_count = 0; return; } // Allow empty badpixel.bin file 649 ptr=malloc(filesize); 650 if (!ptr) return; 651 fd=fopen(filename, "rb"); 652 if (fd) { 653 fread(ptr,1, filesize,fd); 654 fclose(fd); 655 binary_list=ptr; 656 binary_count=filesize/(2*sizeof(short)); 657 } 658 else free(ptr); 659 } 660 661 void unload_bad_pixels_list_b(void) { 662 if (binary_list) free(binary_list); 663 binary_list=NULL; 664 binary_count=-1; 665 } 666 667 void patch_bad_pixels_b(void) { 668 int i; 669 short* ptr=binary_list; 670 #ifdef DNG_VERT_RLE_BADPIXELS 671 short y, cnt; 672 for (i=0; i<binary_count; i++, ptr+=2) 673 { 674 y = ptr[1] & 0x1FFF; 675 cnt = (ptr[1] >> 13) & 7; 676 for (; cnt>=0; cnt--, y++) 677 if (get_raw_pixel(ptr[0], y)==0) 678 patch_bad_pixel(ptr[0], y); 679 } 680 #else 681 for (i=0; i<binary_count; i++, ptr+=2) 682 if (get_raw_pixel(ptr[0], ptr[1])==0) 683 patch_bad_pixel(ptr[0], ptr[1]); 684 #endif 685 } 686 687 int badpixel_list_loaded_b(void) { 688 return (binary_count >= 0) ? 1 : 0; 689 } 690 691 // ----------------------------------------------- 692 693 enum BadpixelFSM { 694 BADPIX_START, 695 BADPIX_S1, 696 BADPIX_S2 697 }; 698 699 int badpixel_task_stack(long p) { 700 static unsigned int badpix_cnt1; 701 702 switch(p) { 703 case BADPIX_START: 704 action_pop(); 705 706 console_clear(); 707 console_add_line("Wait please... "); 708 console_add_line("This takes a few seconds,"); 709 console_add_line("don't panic!"); 710 711 init_badpixel_bin_flag = INIT_BADPIXEL_COUNT; 712 713 shooting_set_tv96_direct(96, SET_LATER); 714 action_push(BADPIX_S1); 715 action_push(AS_SHOOT); 716 action_push_delay(3000); 717 break; 718 case BADPIX_S1: 719 action_pop(); 720 721 badpix_cnt1 = init_badpixel_bin_flag; 722 init_badpixel_bin_flag = INIT_BADPIXEL_FILE; 723 shooting_set_tv96_direct(96, SET_LATER); 724 725 action_push(BADPIX_S2); 726 action_push(AS_SHOOT); 727 break; 728 case BADPIX_S2: 729 action_pop(); 730 731 console_clear(); 732 if (badpix_cnt1 == init_badpixel_bin_flag) { 733 // TODO script asked confirmation first 734 // should sanity check bad pixel count at least, 735 // wrong buffer address could make badpixel bigger than available mem 736 char msg[32]; 737 console_add_line("badpixel.bin created."); 738 sprintf(msg, "Bad pixel count: %d", badpix_cnt1); 739 console_add_line(msg); 740 remove(PATH_BADPIXEL_BIN); 741 rename(PATH_BAD_TMP_BIN,PATH_BADPIXEL_BIN); 742 } else { 743 console_add_line("badpixel.bin failed."); 744 console_add_line("Please try again."); 745 } 746 init_badpixel_bin_flag = 0; 747 remove(PATH_BAD_TMP_BIN); 748 749 action_push_delay(3000); 750 break; 751 default: 752 action_stack_standard(p); 753 break; 754 } 755 756 return 1; 757 } 758 759 760 void create_badpixel_bin() { 761 if (!(mode_get() & MODE_REC)) { 762 gui_mbox_init(LANG_ERROR, LANG_MSG_RECMODE_REQUIRED, MBOX_BTN_OK|MBOX_TEXT_CENTER, NULL); 763 return; 764 } 765 766 gui_set_mode(GUI_MODE_ALT); 767 action_stack_create(&badpixel_task_stack, BADPIX_START); 768 } 769 770 //------------------------------------------------------------------- 771 // Write DNG header and thumbnail to file 772 773 void write_dng_header(int fd) 774 { 775 if (dng_header_buf) 776 { 777 fill_gamma_buf(); 778 patch_bad_pixels_b(); 779 create_thumbnail(); 780 write(fd, dng_header_buf, dng_header_buf_size); 781 write(fd, thumbnail_buf, DNG_TH_WIDTH*DNG_TH_HEIGHT*3); 782 } 783 } 784 505 785 #endif //DNG_SUPPORT -
trunk/core/dng.h
r744 r1497 4 4 #define DNG_H 5 5 6 struct dir_entry{unsigned short tag; unsigned short type; unsigned int count; unsigned int offset;}; 6 #if DNG_SUPPORT 7 7 8 #define T_BYTE 1 9 #define T_ASCII 2 10 #define T_SHORT 3 11 #define T_LONG 4 12 #define T_RATIONAL 5 13 #define T_SBYTE 6 14 #define T_UNDEFINED 7 15 #define T_SSHORT 8 16 #define T_SLONG 9 17 #define T_SRATIONAL 10 18 #define T_FLOAT 11 19 #define T_DOUBLE 12 8 extern int raw_init_badpixel_bin(); 9 extern void capture_data_for_exif(void); 10 extern void create_dng_header(); 11 extern void free_dng_header(void); 12 extern void write_dng_header(int fd); 20 13 21 #define CAM_DEFAULT_CROP_ORIGIN_W ((CAM_ACTIVE_AREA_X2-CAM_ACTIVE_AREA_X1-CAM_JPEG_WIDTH )/2) 22 #define CAM_DEFAULT_CROP_ORIGIN_H ((CAM_ACTIVE_AREA_Y2-CAM_ACTIVE_AREA_Y1-CAM_JPEG_HEIGHT)/2) 14 extern void convert_dng_to_chdk_raw(char* fn); 23 15 24 //thumbnail 25 #define DNG_TH_WIDTH 128 26 #define DNG_TH_HEIGHT 96 27 // higly recommended that DNG_TH_WIDTH*DNG_TH_HEIGHT would be divisible by 512 28 29 struct t_data_for_exif{ 30 unsigned long time; 31 short iso; 32 short tv; 33 short av; 34 short exp_bias; 35 int exp_program; 36 short max_av; 37 int focal_length; 38 int effective_focal_length; 39 short orientation; 40 short flash_mode; 41 short flash_fired; 42 short metering_mode; 43 }; 44 45 void create_dng_header(struct t_data_for_exif* exif_data); 46 void free_dng_header(void); 47 char* get_dng_header(void); 48 int get_dng_header_size(void); 49 50 struct t_data_for_exif* capture_data_for_exif(void); 51 52 char *get_date_for_exif(unsigned long time); 53 unsigned int* get_av_for_exif(short av); 54 int* get_tv_for_exif(short tv); 55 unsigned int* get_max_av_for_exif(short max_av); 56 int* get_exp_bias_for_exif(short exp_bias); 57 unsigned short get_exp_program_for_exif(int exp_program); 58 unsigned int* get_focal_length_for_exif(int focal_length); 59 unsigned short get_orientation_for_exif(short orientation); 60 unsigned short get_flash_mode_for_exif(short mode, short fired); 61 unsigned short get_metering_mode_for_exif(short metering_mode); 62 int* get_shutter_speed_for_exif(short tv); 63 int* get_aperture_for_exif(short av); 64 65 void convert_dng_to_chdk_raw(char* fn); 16 extern void load_bad_pixels_list_b(char* filename); 17 extern void unload_bad_pixels_list_b(void); 18 extern int badpixel_list_loaded_b(void); 19 extern void create_badpixel_bin(); 66 20 67 21 #endif 22 23 #endif -
trunk/core/gui.c
r1484 r1497 46 46 #include "motion_detector.h" 47 47 #include "raw.h" 48 #include "dng.h" 48 49 #ifdef OPT_CURVES 49 50 #include "curves.h" -
trunk/core/raw.c
r1491 r1497 4 4 #include "raw.h" 5 5 #include "console.h" 6 #if DNG_SUPPORT 7 #include "dng.h" 8 #include "math.h" 9 #include "keyboard.h" 10 #include "action_stack.h" 11 #include "gui_draw.h" 12 #include "gui_mbox.h" 13 #include "gui_lang.h" 14 #endif 6 #include "dng.h" 15 7 #ifdef OPT_CURVES 16 8 #include "curves.h" … … 23 15 #define RAW_TARGET_FILENAME "%s%04d%s" 24 16 #define RAW_BRACKETING_FILENAME "%s%04d_%02d%s" 25 26 #define PATH_BADPIXEL_BIN "A/CHDK/badpixel.bin"27 #define PATH_BAD_TMP_BIN "A/CHDK/bad_tmp.bin"28 17 29 18 //------------------------------------------------------------------- … … 55 44 else return (char*) ((int)hook_alt_raw_image_addr()&~CAM_UNCACHED_BIT); 56 45 } 57 58 //-------------------------------------------------------------------59 60 #if DNG_SUPPORT61 #define INIT_BADPIXEL_COUNT -162 #define INIT_BADPIXEL_FILE -263 64 int init_badpixel_bin_flag; // contants above to count/create file, > 0 num bad pixel65 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 #ifdef DNG_VERT_RLE_BADPIXELS78 for (c[0]=CAM_ACTIVE_AREA_X1; c[0]<CAM_ACTIVE_AREA_X2; c[0]++)79 {80 for (c[1]=CAM_ACTIVE_AREA_Y1; c[1]<CAM_ACTIVE_AREA_Y2; c[1]++)81 {82 if (get_raw_pixel(c[0],c[1])==0)83 {84 unsigned short l;85 for (l=0; l<7; l++) if (get_raw_pixel(c[0],c[1]+l+1)!=0) break;86 c[1] = c[1] | (l << 13);87 if (f) fwrite(c, 1, 4, f);88 c[1] = (c[1] & 0x1FFF) + l;89 count = count + l + 1;90 }91 }92 }93 #else94 for (c[1]=CAM_ACTIVE_AREA_Y1; c[1]<CAM_ACTIVE_AREA_Y2; c[1]++)95 {96 for (c[0]=CAM_ACTIVE_AREA_X1; c[0]<CAM_ACTIVE_AREA_X2; c[0]++)97 {98 if (get_raw_pixel(c[0],c[1])==0)99 {100 if (f) fwrite(c, 1, 4, f);101 count++;102 }103 }104 }105 #endif106 if (f) fclose(f);107 init_badpixel_bin_flag = count;108 state_shooting_progress = SHOOTING_PROGRESS_PROCESSING;109 return 1;110 }111 112 unsigned short get_raw_pixel(unsigned int x,unsigned int y);113 114 static unsigned char gamma[256];115 116 void fill_gamma_buf(void) {117 int i;118 if (gamma[255]) return;119 #if defined(CAMERA_sx30) || defined(CAMERA_sx40hs) || defined(CAMERA_g12) || defined(CAMERA_ixus310_elph500hs)120 for (i=0; i<12; i++) gamma[i]=255*pow(i/255.0, 0.5);121 for (i=12; i<64; i++) gamma[i]=255*pow(i/255.0, 0.4);122 for (i=64; i<=255; i++) gamma[i]=255*pow(i/255.0, 0.25);123 #else124 for (i=0; i<=255; i++) gamma[i]=255*pow(i/255.0, 0.5);125 #endif126 }127 128 void create_thumbnail(char* buf) {129 unsigned int i, j, x, y;130 unsigned char r, g, b;131 for (i=0; i<DNG_TH_HEIGHT; i++)132 for (j=0; j<DNG_TH_WIDTH; j++) {133 x=CAM_ACTIVE_AREA_X1+((CAM_ACTIVE_AREA_X2-CAM_ACTIVE_AREA_X1)*j)/DNG_TH_WIDTH;134 y=CAM_ACTIVE_AREA_Y1+((CAM_ACTIVE_AREA_Y2-CAM_ACTIVE_AREA_Y1)*i)/DNG_TH_HEIGHT;135 #if cam_CFAPattern==0x02010100 // Red Green Green Blue136 r=gamma[get_raw_pixel((x/2)*2,(y/2)*2)>>(CAM_SENSOR_BITS_PER_PIXEL-8)]; // red pixel137 g=gamma[6*(get_raw_pixel((x/2)*2+1,(y/2)*2)>>(CAM_SENSOR_BITS_PER_PIXEL-8))/10]; // green pixel138 b=gamma[get_raw_pixel((x/2)*2+1,(y/2)*2+1)>>(CAM_SENSOR_BITS_PER_PIXEL-8)]; //blue pixel139 #elif cam_CFAPattern==0x01000201 // Green Blue Red Green140 r=gamma[get_raw_pixel((x/2)*2,(y/2)*2+1)>>(CAM_SENSOR_BITS_PER_PIXEL-8)]; // red pixel141 g=gamma[6*(get_raw_pixel((x/2)*2,(y/2)*2)>>(CAM_SENSOR_BITS_PER_PIXEL-8))/10]; // green pixel142 b=gamma[get_raw_pixel((x/2)*2+1,(y/2)*2)>>(CAM_SENSOR_BITS_PER_PIXEL-8)]; //blue pixel143 #else144 #error please define new pattern here145 #endif146 *buf++=r; *buf++=g; *buf++=b;147 }148 }149 #else // no DNG_SUPPORT150 static inline int __attribute__((always_inline)) raw_init_badpixel_bin(void) {return 0;}151 #endif152 46 //------------------------------------------------------------------- 153 47 … … 158 52 static int br_counter; 159 53 #if DNG_SUPPORT 160 struct t_data_for_exif* exif_data = NULL; 161 char *thumbnail_buf = NULL; 162 if (conf.dng_raw) exif_data=capture_data_for_exif(); 54 if (conf.dng_raw) capture_data_for_exif(); 163 55 #endif 164 56 if (state_kbd_script_run && shot_histogram_isenabled()) build_shot_histogram(); … … 168 60 char* altrawadr = get_alt_raw_image_addr(); 169 61 62 #if DNG_SUPPORT 170 63 // count/save badpixels if requested 171 64 if(raw_init_badpixel_bin()) { 172 65 return 0; 173 66 } 67 #endif 174 68 175 69 if (develop_raw) { … … 248 142 if (conf.dng_raw) 249 143 { 250 fill_gamma_buf(); 251 create_dng_header(exif_data); 252 thumbnail_buf = malloc(DNG_TH_WIDTH*DNG_TH_HEIGHT*3); 253 if (get_dng_header() && thumbnail_buf) { 254 patch_bad_pixels_b(); 255 create_thumbnail(thumbnail_buf); 256 write(fd, get_dng_header(), get_dng_header_size()); 257 write(fd, thumbnail_buf, DNG_TH_WIDTH*DNG_TH_HEIGHT*3); 258 reverse_bytes_order2(rawadr, altrawadr, hook_raw_size()); 259 } 144 create_dng_header(); 145 write_dng_header(fd); 146 reverse_bytes_order2(rawadr, altrawadr, hook_raw_size()); 260 147 // Write alternate (inactive) buffer that we reversed the bytes into above (if only one buffer then it will be the active buffer instead) 261 148 write(fd, (char*)(((unsigned long)altrawadr)|CAM_UNCACHED_BIT), hook_raw_size()); … … 269 156 utime(fn, &t); 270 157 271 if (conf.dng_raw) { 272 if (get_dng_header() && thumbnail_buf) { 273 if (rawadr == altrawadr) // If only one RAW buffer then we have to swap the bytes back 274 reverse_bytes_order2(rawadr, altrawadr, hook_raw_size()); 275 //unpatch_bad_pixels_b(); 276 } 277 if (get_dng_header()) free_dng_header(); 278 if (thumbnail_buf) free(thumbnail_buf); 158 if (conf.dng_raw) 159 { 160 if (rawadr == altrawadr) // If only one RAW buffer then we have to swap the bytes back 161 reverse_bytes_order2(rawadr, altrawadr, hook_raw_size()); 162 free_dng_header(); 279 163 } 280 164 #else … … 452 336 453 337 } 454 455 #if DNG_SUPPORT456 short* binary_list=NULL;457 int binary_count=-1;458 459 void load_bad_pixels_list_b(char* filename) {460 struct stat st;461 long filesize;462 void* ptr;463 FILE *fd;464 binary_count=-1;465 if (stat(filename,&st)!=0) return;466 filesize=st.st_size;467 if (filesize%(2*sizeof(short)) != 0) return;468 if (filesize == 0) { binary_count = 0; return; } // Allow empty badpixel.bin file469 ptr=malloc(filesize);470 if (!ptr) return;471 fd=fopen(filename, "rb");472 if (fd) {473 fread(ptr,1, filesize,fd);474 fclose(fd);475 binary_list=ptr;476 binary_count=filesize/(2*sizeof(short));477 }478 else free(ptr);479 }480 481 void unload_bad_pixels_list_b(void) {482 if (binary_list) free(binary_list);483 binary_list=NULL;484 binary_count=-1;485 }486 487 void patch_bad_pixels_b(void) {488 int i;489 short* ptr=binary_list;490 #ifdef DNG_VERT_RLE_BADPIXELS491 short y, cnt;492 for (i=0; i<binary_count; i++, ptr+=2)493 {494 y = ptr[1] & 0x1FFF;495 cnt = (ptr[1] >> 13) & 7;496 for (; cnt>=0; cnt--, y++)497 if (get_raw_pixel(ptr[0], y)==0)498 patch_bad_pixel(ptr[0], y);499 }500 #else501 for (i=0; i<binary_count; i++, ptr+=2)502 if (get_raw_pixel(ptr[0], ptr[1])==0)503 patch_bad_pixel(ptr[0], ptr[1]);504 #endif505 }506 /*507 void unpatch_bad_pixels_b(void) {508 int i;509 short* ptr=binary_list;510 for (i=0; i<binary_count; i++, ptr+=2) set_raw_pixel(ptr[0], ptr[1], 0);511 }512 */513 int badpixel_list_loaded_b(void) {514 return (binary_count >= 0) ? 1 : 0;515 }516 517 // -----------------------------------------------518 519 enum BadpixelFSM {520 BADPIX_START,521 BADPIX_S1,522 BADPIX_S2523 };524 525 int badpixel_task_stack(long p) {526 static unsigned int badpix_cnt1;527 528 switch(p) {529 case BADPIX_START:530 action_pop();531 532 console_clear();533 console_add_line("Wait please... ");534 console_add_line("This takes a few seconds,");535 console_add_line("don't panic!");536 537 init_badpixel_bin_flag = INIT_BADPIXEL_COUNT;538 539 shooting_set_tv96_direct(96, SET_LATER);540 action_push(BADPIX_S1);541 action_push(AS_SHOOT);542 action_push_delay(3000);543 break;544 case BADPIX_S1:545 action_pop();546 547 badpix_cnt1 = init_badpixel_bin_flag;548 init_badpixel_bin_flag = INIT_BADPIXEL_FILE;549 shooting_set_tv96_direct(96, SET_LATER);550 551 action_push(BADPIX_S2);552 action_push(AS_SHOOT);553 break;554 case BADPIX_S2:555 action_pop();556 557 console_clear();558 if (badpix_cnt1 == init_badpixel_bin_flag) {559 // TODO script asked confirmation first560 // should sanity check bad pixel count at least,561 // wrong buffer address could make badpixel bigger than available mem562 char msg[32];563 console_add_line("badpixel.bin created.");564 sprintf(msg, "Bad pixel count: %d", badpix_cnt1);565 console_add_line(msg);566 remove(PATH_BADPIXEL_BIN);567 rename(PATH_BAD_TMP_BIN,PATH_BADPIXEL_BIN);568 } else {569 console_add_line("badpixel.bin failed.");570 console_add_line("Please try again.");571 }572 init_badpixel_bin_flag = 0;573 remove(PATH_BAD_TMP_BIN);574 575 action_push_delay(3000);576 break;577 default:578 action_stack_standard(p);579 break;580 }581 582 return 1;583 }584 585 586 void create_badpixel_bin() {587 if (!(mode_get() & MODE_REC)) {588 gui_mbox_init(LANG_ERROR, LANG_MSG_RECMODE_REQUIRED, MBOX_BTN_OK|MBOX_TEXT_CENTER, NULL);589 return;590 }591 592 gui_set_mode(GUI_MODE_ALT);593 action_stack_create(&badpixel_task_stack, BADPIX_START);594 }595 596 #endif -
trunk/core/raw.h
r1035 r1497 24 24 extern void raw_prepare_develop(const char* filename); 25 25 extern void load_bad_pixels_list(const char* filename); 26 unsigned short get_raw_pixel(unsigned int x,unsigned int y); 27 28 #if DNG_SUPPORT 29 void load_bad_pixels_list_b(char* filename); 30 void unload_bad_pixels_list_b(void); 31 void patch_bad_pixels_b(void); 32 void unpatch_bad_pixels_b(void); 33 int badpixel_list_loaded_b(void); 34 char* get_raw_image_addr(void); 35 void create_badpixel_bin(); 36 #endif 26 extern unsigned short get_raw_pixel(unsigned int x,unsigned int y); 27 extern char* get_raw_image_addr(void); 28 extern void patch_bad_pixel(unsigned int x,unsigned int y); 37 29 38 30 //-------------------------------------------------------------------
Note: See TracChangeset
for help on using the changeset viewer.