root/modules/raw_merge.c

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

DEFINITIONS

This source file includes following definitions.
  1. raw_subtract_values
  2. raw_subtract
  3. raw_merge_start
  4. raw_merge_add_file
  5. raw_merge_end
  6. _module_can_unload

   1 #include "camera_info.h"
   2 #include "stdlib.h"
   3 #include "debug_led.h"
   4 #include "sd_card.h"
   5 #include "gui_mbox.h"
   6 #include "raw.h"
   7 #include "gui_lang.h"
   8 #include "lang.h"
   9 #include "conf.h"
  10 
  11 #include "raw_merge.h"
  12 #include "module_def.h"
  13 
  14 #define TEMP_FILE_NAME   "A/raw16.tmp"
  15 #define TEMP_FILE_NAME_1 "A/raw16_1.tmp"
  16 
  17 static int running = 0;
  18 static int raw_action;
  19 static int raw_count;
  20 static unsigned short *row;
  21 static unsigned char *rawrow;
  22 static char namebuf[100];
  23 
  24 extern void reverse_bytes_order2(char* from, char* to, int count);
  25 
  26 // note: if processing with dcraw etc, zeros may get replaced with interpolated values
  27 // this may or may not be what you want
  28 static int raw_subtract_values(int from, int sub)
  29 {
  30     int result;
  31     if ((from==0) || (sub==0)) return 0; // bad pixel
  32     result = from - sub + camera_sensor.black_level;
  33     if (result < camera_sensor.black_level) result = camera_sensor.black_level;
  34     if (result > camera_sensor.white_level) result = camera_sensor.white_level;
  35     return result;
  36 }
  37 
  38 // subtract "sub" from "from"
  39 int raw_subtract(const char *fromName, const char* fromDir, const char *subName, const char* subDir)
  40 {
  41     char from[100];
  42     char sub[100];
  43 
  44     unsigned int req = (camera_sensor.raw_size >> 20) + 1;      // Raw size in MB
  45     unsigned int avail = GetFreeCardSpaceKb() >> 10;            // Free space in MB
  46 
  47     if (avail < req)
  48     {
  49         sprintf(namebuf,lang_str(LANG_AVERAGE_NO_CARD_SPACE),req,avail);
  50         gui_mbox_init((int)"RAW OP", (int)namebuf, MBOX_BTN_OK|MBOX_TEXT_CENTER, NULL);
  51         return 0;
  52     }
  53 
  54     int fromDNG = 0;
  55     int subDNG = 0;
  56     struct stat st;
  57 
  58     sprintf(from, "%s/%s", fromDir, fromName);
  59     if (stat(from,&st) != 0 || st.st_size < camera_sensor.raw_size)
  60     {
  61         // TODO: error popup
  62         return 0;
  63     }
  64     fromDNG = st.st_size - camera_sensor.raw_size;
  65 
  66     sprintf(sub, "%s/%s", subDir, subName);
  67     if (stat(sub,&st) != 0 || st.st_size < camera_sensor.raw_size)
  68     {
  69         // TODO: error popup
  70         return 0;
  71     }
  72     subDNG = st.st_size - camera_sensor.raw_size;
  73 
  74     sprintf(namebuf, "%s/%s%s", fromDir, img_prefixes[conf.sub_batch_prefix], fromName+4);
  75     strcpy(namebuf + strlen(namebuf) - 4, (fromDNG && conf.raw_dng_ext) ? ".DNG" : img_exts[conf.sub_batch_ext]);
  76 
  77     FILE *ffrom = fopen(from, "rb");
  78     FILE *fsub = fopen(sub, "rb");
  79     FILE *fdest = fopen(namebuf, "wb");
  80 
  81     unsigned char *bacc = malloc(camera_sensor.raw_rowlen);
  82     unsigned char *bsub = malloc(camera_sensor.raw_rowlen);
  83 
  84     int status = 0;
  85     unsigned short s,d;
  86     int i,j;
  87 
  88     running = 1;
  89 
  90     if (bacc && bsub && ffrom && fsub && fdest)
  91     {
  92         started();
  93 
  94         if (fromDNG)
  95         {
  96             // Copy DNG header to output file
  97             int len = fromDNG;
  98             while (len > 0)
  99             {
 100                 int l = len;
 101                 if (l > camera_sensor.raw_rowlen) l = camera_sensor.raw_rowlen;
 102                 fread(bacc, 1, l, ffrom);
 103                 fwrite(bacc, 1, l, fdest);
 104                 len -= l;
 105             }
 106             fseek(ffrom, fromDNG, SEEK_SET);
 107         }
 108         if (subDNG)
 109         {
 110             fseek(fsub, subDNG, SEEK_SET);
 111         }
 112 
 113         for (j = 0; j < camera_sensor.raw_rows; j++)
 114         {
 115             fread(bacc, 1, camera_sensor.raw_rowlen, ffrom);
 116             if (fromDNG) reverse_bytes_order2((char*)bacc, (char*)bacc, camera_sensor.raw_rowlen);
 117             fread(bsub, 1, camera_sensor.raw_rowlen, fsub);
 118             if (subDNG) reverse_bytes_order2((char*)bsub, (char*)bsub, camera_sensor.raw_rowlen);
 119 
 120             if (camera_sensor.bits_per_pixel == 10)
 121             {
 122                 for (i=0; i<camera_sensor.raw_rowlen; i+=10)
 123                 {
 124                     s = ((0x3fc&(((unsigned short)bsub[i+1])<<2)) | (bsub[i+0] >> 6));
 125                     d = ((0x3fc&(((unsigned short)bacc[i+1])<<2)) | (bacc[i+0] >> 6));
 126                     d = raw_subtract_values(d,s);
 127                     bacc[i+0] = (bacc[i+0]&0x3F)|(d<<6); 
 128                     bacc[i+1] = d>>2;
 129 
 130                     s = ((0x3f0&(((unsigned short)bsub[i+0])<<4)) | (bsub[i+3] >> 4));
 131                     d = ((0x3f0&(((unsigned short)bacc[i+0])<<4)) | (bacc[i+3] >> 4));
 132                     d = raw_subtract_values(d,s);
 133                     bacc[i+0] = (bacc[i+0]&0xC0)|(d>>4);
 134                     bacc[i+3] = (bacc[i+3]&0x0F)|(d<<4);
 135 
 136                     s = ((0x3c0&(((unsigned short)bsub[i+3])<<6)) | (bsub[i+2] >> 2));
 137                     d = ((0x3c0&(((unsigned short)bacc[i+3])<<6)) | (bacc[i+2] >> 2));
 138                     d = raw_subtract_values(d,s);
 139                     bacc[i+2] = (bacc[i+2]&0x03)|(d<<2);
 140                     bacc[i+3] = (bacc[i+3]&0xF0)|(d>>6);
 141 
 142                     s = ((0x300&(((unsigned short)bsub[i+2])<<8)) | (bsub[i+5])); 
 143                     d = ((0x300&(((unsigned short)bacc[i+2])<<8)) | (bacc[i+5])); 
 144                     d = raw_subtract_values(d,s);
 145                     bacc[i+2] = (bacc[i+2]&0xFC)|(d>>8); 
 146                     bacc[i+5] = d;
 147 
 148                     s = ((0x3fc&(((unsigned short)bsub[i+4])<<2)) | (bsub[i+7] >> 6)); 
 149                     d = ((0x3fc&(((unsigned short)bacc[i+4])<<2)) | (bacc[i+7] >> 6)); 
 150                     d = raw_subtract_values(d,s);
 151                     bacc[i+4] = d>>2;
 152                     bacc[i+7] = (bacc[i+7]&0x3F)|(d<<6);
 153 
 154                     s = ((0x3f0&(((unsigned short)bsub[i+7])<<4)) | (bsub[i+6] >> 4)); 
 155                     d = ((0x3f0&(((unsigned short)bacc[i+7])<<4)) | (bacc[i+6] >> 4)); 
 156                     d = raw_subtract_values(d,s);
 157                     bacc[i+6] = (bacc[i+6]&0x0F)|(d<<4);
 158                     bacc[i+7] = (bacc[i+7]&0xC0)|(d>>4);
 159 
 160                     s = ((0x3c0&(((unsigned short)bsub[i+6])<<6)) | (bsub[i+9] >> 2)); 
 161                     d = ((0x3c0&(((unsigned short)bacc[i+6])<<6)) | (bacc[i+9] >> 2)); 
 162                     d = raw_subtract_values(d,s);
 163                     bacc[i+6] = (bacc[i+6]&0xF0)|(d>>6);
 164                     bacc[i+9] = (bacc[i+9]&0x03)|(d<<2);
 165 
 166                     s = ((0x300&(((unsigned short)bsub[i+9])<<8)) | (bsub[i+8])); 
 167                     d = ((0x300&(((unsigned short)bacc[i+9])<<8)) | (bacc[i+8])); 
 168                     d = raw_subtract_values(d,s);
 169                     bacc[i+8] = d;
 170                     bacc[i+9] = (bacc[i+9]&0xFC)|(d>>8);
 171                 }
 172             }
 173             else if (camera_sensor.bits_per_pixel == 12)
 174             {
 175                 for (i=0; i<camera_sensor.raw_rowlen; i+=6)
 176                 {
 177                     s = ((0xFF0&(((unsigned short)bsub[i+1])<<4)) | (bsub[i+0] >> 4));
 178                     d = ((0xFF0&(((unsigned short)bacc[i+1])<<4)) | (bacc[i+0] >> 4));
 179                     d = raw_subtract_values(d,s);
 180                     bacc[i+0] = (bacc[i+0]&0x0F)|(d<<4);
 181                     bacc[i+1] = d>>4;
 182 
 183                     s = ((0xF00&(((unsigned short)bsub[i+0])<<8)) | (bsub[i+3]     ));
 184                     d = ((0xF00&(((unsigned short)bacc[i+0])<<8)) | (bacc[i+3]   ));
 185                     d = raw_subtract_values(d,s);
 186                     bacc[i+0] = (bacc[i+0]&0xF0)|(d>>8);
 187                     bacc[i+3] = d;
 188 
 189                     s = ((0xFF0&(((unsigned short)bsub[i+2])<<4)) | (bsub[i+5] >> 4));
 190                     d = ((0xFF0&(((unsigned short)bacc[i+2])<<4)) | (bacc[i+5] >> 4));
 191                     d = raw_subtract_values(d,s);
 192                     bacc[i+2] = d>>4;
 193                     bacc[i+5] = (bacc[i+5]&0x0F)|(d<<4);
 194 
 195                     s = ((0xF00&(((unsigned short)bsub[i+5])<<8)) | (bsub[i+4]     ));
 196                     d = ((0xF00&(((unsigned short)bacc[i+5])<<8)) | (bacc[i+4]     ));
 197                     d = raw_subtract_values(d,s);
 198                     bacc[i+4] = d;
 199                     bacc[i+5] = (bacc[i+5]&0xF0)|(d>>8);
 200                 }
 201             }
 202             else if (camera_sensor.bits_per_pixel == 14)
 203             {
 204                 for (i=0; i<camera_sensor.raw_rowlen; i+=14)
 205                 {
 206                     s = ((unsigned short)(bsub[i+ 1])        <<  6) | (bsub[i+ 0] >> 2);
 207                     d = ((unsigned short)(bacc[i+ 1])        <<  6) | (bacc[i+ 0] >> 2);
 208                     d = raw_subtract_values(d,s);
 209                     bacc[i+ 0] = (bacc[i+ 0]&0x03)|(d<< 2);
 210                     bacc[i+ 1] = d>>6;
 211 
 212                     s = ((unsigned short)(bsub[i+ 0] & 0x03) << 12) | ((unsigned short)(bsub[i+ 3]) << 4) | (bsub[i+ 2] >> 4);
 213                     d = ((unsigned short)(bacc[i+ 0] & 0x03) << 12) | ((unsigned short)(bacc[i+ 3]) << 4) | (bacc[i+ 2] >> 4);
 214                     d = raw_subtract_values(d,s);
 215                     bacc[i+ 0] = (bacc[i+ 0]&0xFC)|(d>>12);
 216                     bacc[i+ 2] = (bacc[i+ 2]&0x0F)|(d<< 4);
 217                     bacc[i+ 3] = d>>4;
 218 
 219                     s = ((unsigned short)(bsub[i+ 2] & 0x0F) << 10) | ((unsigned short)(bsub[i+ 5]) << 2) | (bsub[i+ 4] >> 6);
 220                     d = ((unsigned short)(bacc[i+ 2] & 0x0F) << 10) | ((unsigned short)(bacc[i+ 5]) << 2) | (bacc[i+ 4] >> 6);
 221                     d = raw_subtract_values(d,s);
 222                     bacc[i+ 2] = (bacc[i+ 2]&0xF0)|(d>>10);
 223                     bacc[i+ 4] = (bacc[i+ 4]&0x3F)|(d<< 6);
 224                     bacc[i+ 5] = d>>2;
 225 
 226                     s = ((unsigned short)(bsub[i+ 4] & 0x3F) <<  8) | (bsub[i+ 7]);
 227                     d = ((unsigned short)(bacc[i+ 4] & 0x3F) <<  8) | (bacc[i+ 7]);
 228                     d = raw_subtract_values(d,s);
 229                     bacc[i+ 4] = (bacc[i+ 4]&0xC0)|(d>> 8);
 230                     bacc[i+ 7] = d;
 231 
 232                     s = ((unsigned short)(bsub[i+ 6])        <<  6) | (bsub[i+ 9] >> 2);
 233                     d = ((unsigned short)(bacc[i+ 6])        <<  6) | (bacc[i+ 9] >> 2);
 234                     d = raw_subtract_values(d,s);
 235                     bacc[i+ 6] = d>>6;
 236                     bacc[i+ 9] = (bacc[i+ 9]&0x03)|(d<< 2);
 237 
 238                     s = ((unsigned short)(bsub[i+ 9] & 0x03) << 12) | ((unsigned short)(bsub[i+ 8]) << 4) | (bsub[i+11] >> 4);
 239                     d = ((unsigned short)(bacc[i+ 9] & 0x03) << 12) | ((unsigned short)(bacc[i+ 8]) << 4) | (bacc[i+11] >> 4);
 240                     d = raw_subtract_values(d,s);
 241                     bacc[i+ 8] = d>>4;
 242                     bacc[i+ 9] = (bacc[i+ 9]&0xFC)|(d>>12);
 243                     bacc[i+11] = (bacc[i+11]&0x0F)|(d<< 4);
 244 
 245                     s = ((unsigned short)(bsub[i+11] & 0x0F) << 10) | ((unsigned short)(bsub[i+10]) << 2) | (bsub[i+13] >> 6);
 246                     d = ((unsigned short)(bacc[i+11] & 0x0F) << 10) | ((unsigned short)(bacc[i+10]) << 2) | (bacc[i+13] >> 6);
 247                     d = raw_subtract_values(d,s);
 248                     bacc[i+10] = d>>2;
 249                     bacc[i+11] = (bacc[i+11]&0xF0)|(d>>10);
 250                     bacc[i+13] = (bacc[i+13]&0x3F)|(d<< 6);
 251 
 252                     s = ((unsigned short)(bsub[i+13] & 0x3F) <<  8) | (bsub[i+12]);
 253                     d = ((unsigned short)(bacc[i+13] & 0x3F) <<  8) | (bacc[i+12]);
 254                     d = raw_subtract_values(d,s);
 255                     bacc[i+12] = d;
 256                     bacc[i+13] = (bacc[i+13]&0xC0)|(d>> 8);
 257                 }
 258             }
 259 
 260             if (fromDNG) reverse_bytes_order2((char*)bacc, (char*)bacc, camera_sensor.raw_rowlen);
 261             fwrite(bacc, 1, camera_sensor.raw_rowlen, fdest);
 262             if ((j & 0x1F) == 0)
 263                 gui_browser_progress_show(namebuf, j*100/camera_sensor.raw_rows);
 264         }
 265         gui_browser_progress_show(namebuf, 100);
 266         finished();
 267         status = 1;
 268     }
 269 
 270     if (bacc)   free(bacc);
 271     if (bsub)   free(bsub);
 272     if (ffrom)  fclose(ffrom);
 273     if (fsub)   fclose(fsub);
 274     if (fdest)  
 275     {
 276         fclose(fdest);
 277         struct utimbuf t;
 278         t.actime = t.modtime = time(NULL);
 279         utime(namebuf, &t);
 280     }
 281 
 282     running = 0;
 283     return status;
 284 }
 285 
 286 int raw_merge_start(int action)
 287 {
 288     unsigned int req = (camera_sensor.raw_size >> 18) + 1;      // Raw size * 4 in MB
 289     unsigned int avail = GetFreeCardSpaceKb() >> 10;            // Free space in MB
 290     if (avail < req)
 291     {
 292         sprintf(namebuf,lang_str(LANG_AVERAGE_NO_CARD_SPACE),req,avail);
 293         gui_mbox_init((int)"RAW OP", (int)namebuf, MBOX_BTN_OK|MBOX_TEXT_CENTER, NULL);
 294         return 0;
 295     }
 296 
 297     raw_action = action;
 298     raw_count = 0;
 299     row = malloc(camera_sensor.raw_rowpix*sizeof(unsigned short));
 300     if (!row)
 301         return 0;
 302     rawrow = malloc(camera_sensor.raw_rowlen);
 303     if (!rawrow)
 304     {
 305         free(row);
 306         return 0;
 307     }
 308     running = 1;
 309     return 1;
 310 }
 311 
 312 int raw_merge_add_file(const char * filename)
 313 {
 314     int  src,i,j;
 315     FILE *fbrawin=NULL,*fbrawout,*fcraw;
 316     struct stat st;
 317 
 318     if (!filename)
 319     {
 320         // TODO: error popup?
 321         return 0;
 322     }
 323 
 324     if (stat(filename,&st) != 0 || st.st_size < camera_sensor.raw_size)
 325     {
 326         // TODO: error popup
 327         return 0;
 328     }
 329     int fcrawDNG = st.st_size - camera_sensor.raw_size;
 330 
 331     started();
 332 
 333     fcraw = fopen(filename,"rb");
 334     if (fcraw)
 335     {
 336         if (raw_count)
 337             fbrawin = fopen(TEMP_FILE_NAME,"rb");
 338 
 339         if (!raw_count || fbrawin)
 340         {
 341             fbrawout = fopen(TEMP_FILE_NAME_1,"wb");
 342             if (fbrawout)
 343             {
 344                 if (fcrawDNG)
 345                     fseek(fcraw, fcrawDNG, SEEK_SET);
 346 
 347                 for (j=0; j<camera_sensor.raw_rows; j++)
 348                 {
 349                     if (raw_count)
 350                         fread(row, 1, camera_sensor.raw_rowpix*sizeof(unsigned short), fbrawin);
 351                     else
 352                         memset(row, 0, camera_sensor.raw_rowpix*sizeof(unsigned short));
 353 
 354                     fread(rawrow, 1, camera_sensor.raw_rowlen, fcraw);
 355                     if (fcrawDNG) reverse_bytes_order2((char*)rawrow, (char*)rawrow, camera_sensor.raw_rowlen);
 356 
 357                     if (camera_sensor.bits_per_pixel == 10)
 358                     {
 359                         for (i=0, src=0; i<camera_sensor.raw_rowpix; i+=8, src+=10)
 360                         {
 361                             row[i+0] += ((0x3fc&(((unsigned short)rawrow[src+1])<<2)) | (rawrow[src+0] >> 6));
 362                             row[i+1] += ((0x3f0&(((unsigned short)rawrow[src+0])<<4)) | (rawrow[src+3] >> 4));
 363                             row[i+2] += ((0x3c0&(((unsigned short)rawrow[src+3])<<6)) | (rawrow[src+2] >> 2));
 364                             row[i+3] += ((0x300&(((unsigned short)rawrow[src+2])<<8)) | (rawrow[src+5])); 
 365                             row[i+4] += ((0x3fc&(((unsigned short)rawrow[src+4])<<2)) | (rawrow[src+7] >> 6)); 
 366                             row[i+5] += ((0x3f0&(((unsigned short)rawrow[src+7])<<4)) | (rawrow[src+6] >> 4)); 
 367                             row[i+6] += ((0x3c0&(((unsigned short)rawrow[src+6])<<6)) | (rawrow[src+9] >> 2)); 
 368                             row[i+7] += ((0x300&(((unsigned short)rawrow[src+9])<<8)) | (rawrow[src+8])); 
 369                         }
 370                     }
 371                     else if (camera_sensor.bits_per_pixel == 12)
 372                     {
 373                         for (i=0, src=0; i<camera_sensor.raw_rowpix; i+=4, src+=6)
 374                         {
 375                             row[i+0] += ((0xFF0&(((unsigned short)rawrow[src+1])<<4)) | (rawrow[src+0] >> 4));
 376                             row[i+1] += ((0xF00&(((unsigned short)rawrow[src+0])<<8)) | (rawrow[src+3]     ));
 377                             row[i+2] += ((0xFF0&(((unsigned short)rawrow[src+2])<<4)) | (rawrow[src+5] >> 4));
 378                             row[i+3] += ((0xF00&(((unsigned short)rawrow[src+5])<<8)) | (rawrow[src+4]     ));
 379                         }
 380                     }
 381                     else if (camera_sensor.bits_per_pixel == 14)
 382                     {
 383                         for (i=0, src=0; i<camera_sensor.raw_rowpix; i+=8, src+=14)
 384                         {
 385                             row[i+0] += ((unsigned short)(rawrow[src+ 1])        <<  6) | (rawrow[src+ 0] >> 2);
 386                             row[i+1] += ((unsigned short)(rawrow[src+ 0] & 0x03) << 12) | (rawrow[src+ 3] << 4) | (rawrow[src+ 2] >> 4);
 387                             row[i+2] += ((unsigned short)(rawrow[src+ 2] & 0x0F) << 10) | (rawrow[src+ 5] << 2) | (rawrow[src+ 4] >> 6);
 388                             row[i+3] += ((unsigned short)(rawrow[src+ 4] & 0x3F) <<  8) | (rawrow[src+ 7]);
 389                             row[i+4] += ((unsigned short)(rawrow[src+ 6])        <<  6) | (rawrow[src+ 9] >> 2);
 390                             row[i+5] += ((unsigned short)(rawrow[src+ 9] & 0x03) << 12) | (rawrow[src+ 8] << 4) | (rawrow[src+11] >> 4);
 391                             row[i+6] += ((unsigned short)(rawrow[src+11] & 0x0F) << 10) | (rawrow[src+10] << 2) | (rawrow[src+13] >> 6);
 392                             row[i+7] += ((unsigned short)(rawrow[src+13] & 0x3F) <<  8) | (rawrow[src+12]);
 393                         }
 394                     }
 395 
 396                     fwrite(row, 1, camera_sensor.raw_rowpix*sizeof(unsigned short), fbrawout);
 397                     if ((j & 0x1F) == 0)
 398                         gui_browser_progress_show(filename, j*100/camera_sensor.raw_rows);
 399                 }
 400                 raw_count++;
 401                 strcpy(namebuf,filename);
 402                 fclose(fbrawout);
 403             }
 404             if (fbrawin)
 405                 fclose(fbrawin);
 406         }
 407         fclose(fcraw);
 408     }
 409 
 410     remove(TEMP_FILE_NAME);
 411     rename(TEMP_FILE_NAME_1,TEMP_FILE_NAME);
 412 
 413     finished();
 414     return 1;
 415 }
 416 
 417 void raw_merge_end(void)
 418 {
 419     int src,i,j;
 420     FILE *fbraw, *fcraw, *fdng = 0;
 421     char dest[100];
 422 
 423     if (!raw_count)
 424     {
 425         free(rawrow);
 426         free(row);
 427         return;
 428     } 
 429 
 430     struct stat st;
 431     if (stat(namebuf,&st) != 0 || st.st_size < camera_sensor.raw_size)
 432     {
 433         // TODO: error popup
 434         return;
 435     }
 436     int destDNG = st.st_size - camera_sensor.raw_size;
 437 
 438     if (destDNG)
 439         fdng = fopen(namebuf,"rb");
 440 
 441     strcpy(dest, namebuf);
 442     char *n = strrchr(dest, '/');
 443     if (n == 0) n = dest;
 444     else n++;
 445 
 446     strncpy(n, img_prefixes[conf.sub_batch_prefix], 4);
 447     strcpy(dest + strlen(dest) - 4, (destDNG && conf.raw_dng_ext) ? ".DNG" : img_exts[conf.sub_batch_ext]);
 448 
 449     // Check if overwriting input file
 450     if (strcmp(namebuf, dest) == 0)
 451     {
 452         if (strncmp(n, "IMG", 3) == 0)
 453             strncpy(n, "CRW", 3);
 454         else
 455             strncpy(n, "IMG", 3);
 456     }
 457 
 458     started();
 459 
 460     fbraw = fopen(TEMP_FILE_NAME,"rb");
 461     if (fbraw)
 462     {
 463         fcraw = fopen(dest,"wb");
 464         if (fcraw)
 465         {
 466             if (destDNG)
 467             {
 468                 // Copy DNG header to output file
 469                 int len = destDNG;
 470                 while (len > 0)
 471                 {
 472                     int l = len;
 473                     if (l > camera_sensor.raw_rowlen) l = camera_sensor.raw_rowlen;
 474                     fread(rawrow, 1, l, fdng);
 475                     fwrite(rawrow, 1, l, fcraw);
 476                     len -= l;
 477                 }
 478             }
 479 
 480             for (j=0; j<camera_sensor.raw_rows; j++)
 481             {
 482                 fread(row, 1, camera_sensor.raw_rowpix*sizeof(unsigned short), fbraw);
 483 
 484                 if (raw_count > 1)
 485                 {
 486                     for (i=0; i<camera_sensor.raw_rowpix; i++)
 487                     {
 488                         if (raw_action == RAW_OPERATION_AVERAGE)
 489                             row[i] /= raw_count;
 490                         else
 491                         {
 492                             if (row[i] > camera_sensor.black_level*(raw_count-1))
 493                                 row[i] -= camera_sensor.black_level*(raw_count-1);
 494                             else
 495                                 row[i] = 0;
 496                             if (row[i] > camera_sensor.white_level)
 497                                 row[i] = camera_sensor.white_level;
 498                         }
 499                     }  
 500                 }
 501                 if (camera_sensor.bits_per_pixel == 10)
 502                 {
 503                     for (i=0,src=0; i<camera_sensor.raw_rowpix; i+=8, src+=10)
 504                     {
 505                         rawrow[src+0] = (row[i+0]<<6)|(row[i+1]>>4);
 506                         rawrow[src+1] = (row[i+0]>>2);
 507                         rawrow[src+2] = (row[i+2]<<2)|(row[i+3]>>8);
 508                         rawrow[src+3] = (row[i+1]<<4)|(row[i+2]>>6);
 509                         rawrow[src+4] = (row[i+4]>>2);
 510                         rawrow[src+5] = (row[i+3]);
 511                         rawrow[src+6] = (row[i+5]<<4)|(row[i+6]>>6);
 512                         rawrow[src+7] = (row[i+4]<<6)|(row[i+5]>>4);
 513                         rawrow[src+8] = (row[i+7]);
 514                         rawrow[src+9] = (row[i+6]<<2)|(row[i+7]>>8);
 515                     }
 516                 }
 517                 else if (camera_sensor.bits_per_pixel == 12)
 518                 {
 519                     for (i=0,src=0; i<camera_sensor.raw_rowpix; i+=4, src+=6)
 520                     {
 521                         rawrow[src+0] = (row[i+0]<<4)|(row[i+1]>>8);
 522                         rawrow[src+1] = (row[i+0]>>4);
 523                         rawrow[src+2] = (row[i+2]>>4);
 524                         rawrow[src+3] = (row[i+1]);
 525                         rawrow[src+4] = (row[i+3]);
 526                         rawrow[src+5] = (row[i+2]<<4)|(row[i+3]>>8);
 527                     }
 528                 }
 529                 else if (camera_sensor.bits_per_pixel == 14)
 530                 {
 531                     for (i=0,src=0; i<camera_sensor.raw_rowpix; i+=8, src+=14)
 532                     {
 533                         rawrow[src+ 0] = (row[i+0]<<2)|(row[i+1]>>12);
 534                         rawrow[src+ 1] = (row[i+0]>>6);
 535                         rawrow[src+ 2] = (row[i+1]<<4)|(row[i+2]>>10);
 536                         rawrow[src+ 3] = (row[i+1]>>4);
 537                         rawrow[src+ 4] = (row[i+2]<<6)|(row[i+3]>>8);
 538                         rawrow[src+ 5] = (row[i+2]>>2);
 539                         rawrow[src+ 6] = (row[i+4]>>6);
 540                         rawrow[src+ 7] = (row[i+3]);
 541                         rawrow[src+ 8] = (row[i+5]>>4);
 542                         rawrow[src+ 9] = (row[i+4]<<2)|(row[i+5]>>12);
 543                         rawrow[src+10] = (row[i+6]>>2);
 544                         rawrow[src+11] = (row[i+5]<<4)|(row[i+6]>>10);
 545                         rawrow[src+12] = (row[i+7]);
 546                         rawrow[src+13] = (row[i+6]<<6)|(row[i+7]>>8);
 547                     }
 548                 }
 549 
 550                 if (destDNG) reverse_bytes_order2((char*)rawrow, (char*)rawrow, camera_sensor.raw_rowlen);
 551                 fwrite(rawrow, 1, camera_sensor.raw_rowlen, fcraw);
 552                 if ((j & 0x1F) == 0)
 553                     gui_browser_progress_show(dest, j*100/camera_sensor.raw_rows);
 554             }
 555             fclose(fcraw);
 556         }
 557         fclose(fbraw);
 558     }
 559 
 560     if (fdng) fclose(fdng);
 561 
 562     struct utimbuf t;
 563     t.actime = t.modtime = time(NULL);
 564     utime(namebuf, &t);
 565     remove(TEMP_FILE_NAME);
 566 
 567     finished();
 568     free(rawrow);
 569     free(row);
 570 
 571     running = 0;
 572 }
 573 
 574 
 575 // =========  MODULE INIT =================
 576 
 577 /***************** BEGIN OF AUXILARY PART *********************
 578   ATTENTION: DO NOT REMOVE OR CHANGE SIGNATURES IN THIS SECTION
 579  **************************************************************/
 580 
 581 int _module_can_unload()
 582 {
 583     return running == 0;
 584 }
 585 
 586 /******************** Module Information structure ******************/
 587 
 588 librawop_sym _librawop = 
 589 {
 590     {
 591         0, 0, _module_can_unload, 0, 0
 592     },
 593 
 594     raw_merge_start,
 595     raw_merge_add_file,
 596     raw_merge_end,
 597     raw_subtract
 598 };
 599 
 600 ModuleInfo _module_info =
 601 {
 602     MODULEINFO_V1_MAGICNUM,
 603     sizeof(ModuleInfo),
 604     RAW_MERGE_VERSION,                  // Module version
 605 
 606     ANY_CHDK_BRANCH, 0, OPT_ARCHITECTURE,                       // Requirements of CHDK version
 607     ANY_PLATFORM_ALLOWED,               // Specify platform dependency
 608 
 609     (int32_t)"RAW operations (dll)",
 610     MTYPE_EXTENSION,            //Implementation of RAW operations\n(Avg, Sum, Sub)
 611 
 612     &_librawop.base,
 613 
 614     ANY_VERSION,                // CONF version
 615     ANY_VERSION,                // CAM SCREEN version
 616     CAM_SENSOR_VERSION,         // CAM SENSOR version
 617     ANY_VERSION,                // CAM INFO version
 618 };
 619 
 620 /*************** END OF AUXILARY PART *******************/

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