dcraw/filecr2 rework, cpozer zoom slider resize fix, rename mpeg3 utils as zmpeg3
[goodguy/history.git] / cinelerra-5.1 / cinelerra / dcraw.c
index 98106822cf9859eca5a0dae0de9c175fd4740cb9..b78bba0477070642deb5b6e4325d6eefba5c58de 100644 (file)
@@ -193,7 +193,6 @@ static struct ph1 {
 #define ULIM(x,y,z) ((y) < (z) ? LIM(x,y,z) : LIM(x,z,y))
 #define CLIP(x) LIM((int)(x),0,65535)
 #define SWAP(a,b) { a=a+b; b=a-b; a=a-b; }
-
 /*
    In order to inline this calculation, I make the risky
    assumption that all filter patterns can be described
@@ -273,27 +272,114 @@ int CLASS fcol (int row, int col)
 // CINELERRA
 static void reset()
 {
-       shot_select=0;
-       multi_out=0;
-       aber[0]=1; aber[1]=1; aber[2]=1; aber[3]=1;
-       gamm[0]=0.45; gamm[1]=4.5; gamm[2]=0; gamm[3]=0; gamm[4]=0; gamm[5]=0;
-       bright=1;
-       user_mul[0]=0; user_mul[1]=0; user_mul[2]=0; user_mul[3]=0;
-       threshold=0;
-       half_size=0;
-       four_color_rgb=0;
-       document_mode=0;
-       highlight=0;
-       verbose=0;
-       use_auto_wb=0;
-       use_camera_wb=0;
-       use_camera_matrix=1;
-       output_color=1;
-       output_bps=8;
-       output_tiff=0;
-       med_passes=0;
-       no_auto_bright=0;
-       greybox[0]=0; greybox[1]=0; greybox[2]=UINT_MAX; greybox[3]=UINT_MAX;
+// uninitialized
+#define ZERO(var) memset(&var, 0, sizeof var);
+       aperture = 0;
+       ZERO(artist);
+       black = 0;
+       ZERO(cam_mul)
+       canon_ev = 0;
+       ZERO(cblack);
+       ZERO(cmatrix);
+       ZERO(cdesc);
+       colors = 0;
+       ZERO(cr2_slice);
+       ZERO(curve);
+       data_error = 0;
+       data_offset = 0;
+       dng_version = 0;
+       exif_cfa = 0;
+       ZERO(failure);
+       filters = 0;
+       ZERO(first_decode);
+       flash_used = 0;
+       flip = 0;
+       focal_len = 0;
+       fuji_layout = 0;
+       fuji_width = 0;
+       ZERO(gpsdata);
+       height = 0;
+       ZERO(histogram);
+       ifname = 0;
+       ifp = 0;
+       iheight = 0;
+       is_foveon = 0;
+       iso_speed = 0;
+       is_raw = 0;
+       iwidth = 0;
+       kodak_cbpp = 0;
+       left_margin = 0;
+       load_flags = 0;
+       load_raw = 0;
+       maximum = 0;
+       meta_data = 0;
+       meta_length = 0;
+       meta_offset = 0;
+       mix_green = 0;
+       ZERO(model);
+       ZERO(model2);
+       ofp = 0;
+       oprof = 0;
+       order = 0;
+       ZERO(ph1);
+       pixel_aspect = 0;
+       memset(pre_mul, 0, sizeof pre_mul);
+       profile_length = 0;
+       profile_offset = 0;
+       raw_color = 0;
+       raw_height = 0;
+       raw_width = 0;
+       raw_image = 0;
+       memset(rgb_cam, 0, sizeof rgb_cam);
+       shot_order = 0;
+       shrink = 0;
+       shutter = 0;
+       strip_offset = 0;
+       thumb_height = 0;
+       thumb_length = 0;
+       thumb_load_raw = 0;
+       thumb_misc = 0;
+       thumb_offset = 0;
+       thumb_width = 0;
+       tiff_bps = 0;
+       tiff_compress = 0;
+       tiff_flip = 0;
+       tiff_nifds = 0;
+       tiff_samples = 0;
+       tile_length = 0;
+       tile_width = 0;
+       timestamp = 0;
+       top_margin = 0;
+       unique_id = 0;
+       ZERO(white);
+       width = 0;
+       zero_after_ff = 0;
+       zero_is_bad = 0;
+
+// initializer data
+       shot_select = 0;
+       multi_out = 0;
+       aber[0] = aber[1] = aber[2] = aber[3] = 1;
+       gamm[0] = 0.45; gamm[1] = 4.5;  gamm[2] = 0;
+       gamm[3] = 0;    gamm[4] = 0;    gamm[5] = 0;
+       bright = 1;
+       ZERO(user_mul);
+       threshold = 0;
+       half_size = 0;
+       four_color_rgb = 0;
+       document_mode = 0;
+       highlight = 0;
+       verbose = 0;
+       use_auto_wb = 0;
+       use_camera_wb = 0;
+       use_camera_matrix = 1;
+       output_color = 1;
+       output_bps = 8;
+       output_tiff = 0;
+       med_passes = 0;
+       no_auto_bright = 0;
+       greybox[0] = 0;         greybox[1] = 0;
+       greybox[2] = UINT_MAX;  greybox[3] = UINT_MAX;
 }
 
 #if 0
@@ -3325,8 +3411,7 @@ int CLASS foveon_fixed (void *ptr, int size, const char *name)
 {
   void *dp;
   unsigned dim[3];
-// CINELERRA
-  memset (ptr, 0, size*4);
+
   if (!name) return 0;
   dp = foveon_camf_matrix (dim, name);
   if (!dp) return 0;
@@ -9454,10 +9539,16 @@ notraw:
   if (flip == UINT_MAX) flip = 0;
 
 // CINELERRA
-  if (flip & 4)
-        sprintf(dcraw_info, "%d %d", height, width);
+{ unsigned flp = flip;
+  switch ((flp+3600) % 360) {
+    case 270:  flp = 5;  break;
+    case 180:  flp = 3;  break;
+    case  90:  flp = 6;
+  }
+  if( (flp & 4) )
+    sprintf(dcraw_info, "%d %d", height, width);
   else
-        sprintf(dcraw_info, "%d %d", width, height);
+    sprintf(dcraw_info, "%d %d", width, height); }
 }
 
 #ifndef NO_LCMS
@@ -9905,42 +9996,46 @@ void CLASS write_ppm_tiff()
 }
 
 // CINELERRA
-void CLASS write_cinelerra (FILE *ofp)
-{
-       int row, col;
-       float *output;
-
-       for (row = 0; row < height; row++)
-       {
-               output = dcraw_data[row];
-
-               if(document_mode)
-               {
-               for (col = 0; col < width; col++)
-                       {
-                               ushort *pixel = image[row * width + col];
-
-                               *output++ = (float)pixel[0] / 0xffff;
-                               *output++ = (float)pixel[1] / 0xffff;
-                               *output++ = (float)pixel[2] / 0xffff;
-
-                               if(dcraw_alpha) *output++ = 1.0;
+void CLASS write_cinelerra()
+{
+       int c, row, col, soff, cstep, rstep;
+       float scale = 1. / 0xffff;
+       iheight = height;  iwidth  = width;
+       if( (flip & 4) != 0 ) SWAP(height,width);
+       soff  = flip_index(0, 0);
+       cstep = flip_index(0, 1) - soff;
+       rstep = flip_index(1, 0) - flip_index(0, width);
+       if( document_mode ) {
+               for( row=0; row<height; ++row, soff += rstep ) {
+                       float *output = dcraw_data[row];
+                       for( col=0; col<width; ++col, soff += cstep ) {
+                               ushort *pixel = image[soff];
+                               FORC3 *output++ = (float)*pixel++ * scale;
+                               if( dcraw_alpha ) *output++ = 1.0;
                        }
                }
-               else
-               {
-               for (col = 0; col < width; col++)
-                       {
-                               ushort *pixel = image[row * width + col];
-
-                               *output++ = (float)pixel[0] / 0xffff;
-                               *output++ = (float)pixel[1] / 0xffff;
-                               *output++ = (float)pixel[2] / 0xffff;
-
-                               if(dcraw_alpha) *output++ = 1.0;
+       }
+       else {
+               int val, total, white=0x2000;
+               int perc = width * height * 0.01;         /* 99th percentile white level */
+               if( fuji_width ) perc /= 2;
+               if( !((highlight & ~2) || no_auto_bright) ) {
+                       for( white=c=0; c < colors; ++c ) {
+                               for( val=0x2000, total=0; --val > 32; )
+                               if( (total += histogram[c][val]) > perc ) break;
+                               if( white < val ) white = val;
+                       }
+               }
+               gamma_curve(gamm[0], gamm[1], 2, (white << 3)/bright);
+               for( row=0; row<height; ++row, soff += rstep ) {
+                       float *output = dcraw_data[row];
+                       for( col=0; col<width; ++col, soff += cstep ) {
+                               ushort *pixel = image[soff];
+                               FORC3 *output++ = (float)curve[*pixel++] * scale;
+                               if( dcraw_alpha ) *output++ = 1.0;
                        }
                }
-   }
+       }
 }
 
 // CINELERRA
@@ -10139,7 +10234,7 @@ int CLASS dcraw_main (int argc, const char **argv)
     }
 // CINELERRA
 //    write_fun = &CLASS write_ppm_tiff;
-    write_fun = write_cinelerra;
+    write_fun = &CLASS write_cinelerra;
 
     if (thumbnail_only) {
       if ((status = !thumb_offset)) {