PFS_READ_IMAGE Read image file and return RGB, luminance or multichannel matrix. IMG = PFS_READ_IMAGE( file_name ) If input is a gray-scale or luminance image, IMG is a 2D matrix. If input is a color image, IMG(:,:,k) represents red, blue and green color channels for k=1,2 and 3. If input is a multi-channel image (channel names C1, C2, ..., Cn), IMG is a 3D matrix with 3rd dimension corresponding to the channels. PFS_READ_IMAGE accepts all formats recognized by the shell "pfsin" command. Example: img = PFS_READ_IMAGE( 'hdr_images/memorial.exr' ); See also: PFS_READ_RGB, PFS_READ_LUMINANCE, PFS_READ_XYZ, PFS_WRITE_IMAGE, PFSVIEW. Copyright 2009 Rafal Mantiuk
0001 function img = pfs_read_image( fileName ) 0002 %PFS_READ_IMAGE Read image file and return RGB, luminance or multichannel matrix. 0003 % 0004 % IMG = PFS_READ_IMAGE( file_name ) 0005 % 0006 % If input is a gray-scale or luminance image, IMG is a 2D matrix. If input is 0007 % a color image, IMG(:,:,k) represents red, blue and green color channels for k=1,2 and 3. 0008 % If input is a multi-channel image (channel names C1, C2, ..., Cn), IMG is a 0009 % 3D matrix with 3rd dimension corresponding to the channels. 0010 % 0011 % PFS_READ_IMAGE accepts all formats recognized by the shell "pfsin" 0012 % command. 0013 % 0014 % Example: 0015 % img = PFS_READ_IMAGE( 'hdr_images/memorial.exr' ); 0016 % 0017 % See also: PFS_READ_RGB, PFS_READ_LUMINANCE, PFS_READ_XYZ, 0018 % PFS_WRITE_IMAGE, PFSVIEW. 0019 % 0020 % Copyright 2009 Rafal Mantiuk 0021 0022 %Check if file exists 0023 fid = fopen( fileName, 'rb' ); 0024 if( fid == -1 ) 0025 error( 'pfs_read_image: File "%s" does not exist', fileName ); 0026 end 0027 fclose( fid ); 0028 0029 fid = pfspopen( sprintf( '%spfsin ''%s''%s', pfs_shell(), fileName, pfs_shell( 1 ) ), 'r' ); 0030 pin = pfsopen( fid ); 0031 pin = pfsget( pin ); 0032 0033 if( isfield( pin.channels, 'X' ) && isfield( pin.channels, 'Z' ) ) 0034 img = pfs_transform_colorspace( 'XYZ', pin.channels.X, pin.channels.Y, pin.channels.Z, 'RGB' ); 0035 elseif( isfield( pin.channels, 'Y' ) ) 0036 img = pin.channels.Y; 0037 elseif( isfield( pin.channels, 'C1' ) ) 0038 ch=1; 0039 % count the number of channels 0040 while( isfield( pin.channels, sprintf( 'C%d', ch ) ) ) 0041 ch = ch+1; 0042 end 0043 ch_max = ch-1; 0044 img = zeros(pin.rows, pin.columns, ch_max); 0045 for ch=1:ch_max 0046 img(:,:,ch) = pin.channels.(sprintf( 'C%d', ch )); 0047 end 0048 else 0049 error( 'Color channels missing in the pfs frame' ); 0050 end 0051 0052 pfsclose( pin ); 0053 % TODO: Check why crashes on windows 0054 if ~ispc() 0055 pfspclose( fid ); 0056 end 0057 end