"Fossies" - the Fresh Open Source Software Archive

Member "pfstools-2.2.0/src/matlab/pfs_read_image.m" (12 Aug 2021, 2845 Bytes) of package /linux/privat/pfstools-2.2.0.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Matlab source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "pfs_read_image.m": 2.1.0_vs_2.2.0.

    1 function img = pfs_read_image( file_name, pfsin_command, extra_arguments )
    2 %PFS_READ_IMAGE Read image file and return RGB, luminance or multichannel matrix.
    3 %
    4 % IMG = PFS_READ_IMAGE( file_name )
    5 % IMG = PFS_READ_IMAGE( file_name, pfsin_command )
    6 % IMG = PFS_READ_IMAGE( file_name, pfsin_command, extra_arguments )
    7 %
    8 % Read an HDR or SDR image using pfstools. 
    9 %
   10 % pfsin_command could be one of pfsin* command tools, such as 'pfsindcraw' or
   11 % 'pfsinimgmagick'. By default 'pfsin' is used, which can automatically
   12 % detect the image format from an extension and call the right pfsin
   13 % commnand. This option can be useful if pfsin does not recognize the image
   14 % format or you want to pass extra options to the command with
   15 % "extra_arguments".
   16 %
   17 % extra_arguments is a string that specified extra options to be passed to
   18 % pfsin command. For example, when used in combination with 'pfsindcraw',
   19 % you can pass '-n' to return the image in the native camera color space
   20 % instead of rec.709 RGB. 
   21 %
   22 % If input is a gray-scale or luminance image, IMG is a 2D matrix. If input is
   23 % a color image, IMG(:,:,k) represents red, blue and green color channels for k=1,2 and 3.
   24 % If input is a multi-channel image (channel names C1, C2, ..., Cn), IMG is a
   25 % 3D matrix with 3rd dimension corresponding to the channels. 
   26 %
   27 % PFS_READ_IMAGE accepts all formats recognized by the shell "pfsin"
   28 % command.
   29 %
   30 % Example: 
   31 %   img = PFS_READ_IMAGE( 'hdr_images/memorial.exr' );
   32 %
   33 % See also: PFS_READ_RGB, PFS_READ_LUMINANCE, PFS_READ_XYZ,
   34 % PFS_WRITE_IMAGE, PFSVIEW.
   35 %
   36 % Copyright 2009 Rafal Mantiuk
   37 
   38 
   39 if ~exist( 'pfs_command', 'var' )
   40     pfsin_command = 'pfsin';
   41 end
   42 
   43 if ~exist( 'extra_arguments', 'var' )
   44     extra_arguments = '';
   45 end
   46 
   47   %Check if file exists
   48   fid = fopen( file_name, 'rb' );
   49   if( fid == -1 ) 
   50     error( 'pfs_read_image: File "%s" does not exist', file_name );
   51   end
   52   fclose( fid );
   53 
   54   fid = pfspopen( sprintf( '%s%s %s ''%s''%s', pfs_shell(), pfsin_command, extra_arguments, pfs_fix_path(file_name), pfs_shell( 1 ) ), 'r' );
   55   pin = pfsopen( fid );
   56   pin = pfsget( pin );
   57 
   58   if( isfield( pin.channels, 'X' ) && isfield( pin.channels, 'Z' ) )
   59       img = pfs_transform_colorspace( 'XYZ', pin.channels.X, pin.channels.Y, pin.channels.Z, 'RGB' );
   60   elseif( isfield( pin.channels, 'Y' ) )
   61       img = pin.channels.Y;
   62   elseif( isfield( pin.channels, 'C1' ) )
   63       ch=1;
   64       % count the number of channels
   65       while( isfield( pin.channels, sprintf( 'C%d', ch ) ) )
   66           ch = ch+1;
   67       end
   68       ch_max = ch-1;
   69       img = zeros(pin.rows, pin.columns, ch_max);
   70       for ch=1:ch_max
   71           img(:,:,ch) = pin.channels.(sprintf( 'C%d', ch ));
   72       end
   73   else
   74       error( 'Color channels missing in the pfs frame' );
   75   end  
   76   
   77   pfsclose( pin );
   78   % TODO: Check why crashes on windows
   79   if ~ispc()
   80       pfspclose( fid );
   81   end
   82 end