Commit b46a9a1d authored by Andrey Filippov's avatar Andrey Filippov
Browse files

trimming images according to exif lengths form the 0-padded raw data

parent ff5ea51b
Loading
Loading
Loading
Loading
+16 −8
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ Found problem - does not process files that start in one chunk and end in anothe
*/
   define('START_TIFF',     hex2bin('4d4d002a')); // start of a TIFF, JP/JP4 also have this TIFF marker
   define('START_JP',       hex2bin('ffd8ffe1')); // start of JPEG/JP4, TIFF usually does not have JPEG/JP4 markers
   define('MAX_HEAD_LEN',   0x300);               // maximal header length that includes complete header
//disable the default time limit for php scripts.
set_time_limit(0);

@@ -205,16 +206,23 @@ function split_file($path,$file,$destination,$add_to_chn=-1){
      fseek($f,$markers[$i]);
      $s = fread($f,$markers[$i+1]-$markers[$i]);

      $tmp_name = "$path/$destination/image.tmp";
      file_put_contents($tmp_name,$s);

      $result_name = elphel_specific_result_name($tmp_name,$add_to_chn);
      $tmp_name = "$path/$destination/header.tmp"; // image.tmp";
//      file_put_contents($tmp_name,$s);
      file_put_contents($tmp_name,substr($s, 0, MAX_HEAD_LEN)); // only save beginning of the file
//      printf ("header length = %d\n", strlen(substr($s, 0, MAX_HEAD_LEN)));
      $image_length=-1;
      $result_name = elphel_specific_result_name($tmp_name, $image_length, $add_to_chn);
      if ($image_length < 0){
          printf ("Wrong image length from Exif header: %d", $image_length);
          exit(1);
      }
      $dest_image = "$path/$destination/$result_name"; // $result_name may now include "/"
      $dest_set_dir = dirname($dest_image);
      if (!is_dir($dest_set_dir)){
          mkdir($dest_set_dir,0777);
      }
      rename($tmp_name, $dest_image); // "$path/$destination/$result_name");
//      rename($tmp_name, $dest_image); // "$path/$destination/$result_name");
      file_put_contents($dest_image,substr($s, 0, $image_length));
    }
    
    return 0;
@@ -224,8 +232,7 @@ function split_file($path,$file,$destination,$add_to_chn=-1){
}


function elphel_specific_result_name($file,$add_to_chn=-10){

function elphel_specific_result_name($file, &$image_length, $add_to_chn=-10){
  global $forced_ext;
  // $add_to_chn<0 - do not use it and do not use scene directories
  $use_scene_dirs = $add_to_chn >= 0;
@@ -234,7 +241,8 @@ function elphel_specific_result_name($file,$add_to_chn=-10){
  }

  $exif = exif_read_data($file); // gets false find, what is wrong
  
  // image size = ([StripOffsets] => 557)+ ([StripByteCounts] => 656640)
  $image_length = intval($exif['StripOffsets']) + intval($exif['StripByteCounts']);
  $ext = elphel_specific_result_ext($exif,$forced_ext);
  
  //converting GMT a local time GMT+7