[4] | 1 | function cdata = loadLayoutIcon(imagefilename,bgcol) |
---|
| 2 | %loadLayoutIcon Load an icon and set the transparent color |
---|
| 3 | % |
---|
| 4 | % cdata = uiextras.loadLayoutIcon(filename) tries to load the icon specified by |
---|
| 5 | % filename. If the icon is a PNG file with transparency then transparent |
---|
| 6 | % pixels are set to NaN. If not, then any pixel that is pure green is set |
---|
| 7 | % to transparent (i.e. "green screen"). The resulting CDATA is an RGB |
---|
| 8 | % double array. |
---|
| 9 | % |
---|
| 10 | % cdata = uiextras.loadLayoutIcon(filename,bgcol) tries to merge with the |
---|
| 11 | % specified background colour bgcol. Fully transparent pixels are still |
---|
| 12 | % set to NaN, but partially transparent ones are merged with the |
---|
| 13 | % background. |
---|
| 14 | % |
---|
| 15 | % See also: IMREAD |
---|
| 16 | |
---|
| 17 | % Copyright 2005-2010 The MathWorks Ltd. |
---|
| 18 | % 1.1 |
---|
| 19 | % 2012/05/08 08:02:58 |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | error( nargchk( 1, 2, nargin ) ); |
---|
| 23 | if nargin < 2 |
---|
| 24 | bgcol = get( 0, 'DefaultUIControlBackgroundColor' ); |
---|
| 25 | end |
---|
| 26 | |
---|
| 27 | % First try normally |
---|
| 28 | this_dir = fileparts( mfilename( 'fullpath' ) ); |
---|
| 29 | icon_dir = fullfile( this_dir, 'Resources' ); |
---|
| 30 | if exist( imagefilename, 'file' ) |
---|
| 31 | [cdata,map,alpha] = imread( imagefilename ); |
---|
| 32 | elseif exist( fullfile( icon_dir, imagefilename ), 'file' ) |
---|
| 33 | [cdata,map,alpha] = imread( fullfile( icon_dir, imagefilename )); |
---|
| 34 | else |
---|
| 35 | error( 'GUILayout:loadIcon:FileNotFound', 'Cannot open file ''%s''.', imagefilename ); |
---|
| 36 | end |
---|
| 37 | |
---|
| 38 | if ~isempty( map ) |
---|
| 39 | cdata = ind2rgb( cdata, map ); |
---|
| 40 | end |
---|
| 41 | |
---|
| 42 | % Convert to double before applying transparency |
---|
| 43 | cdata = convertToDouble( cdata ); |
---|
| 44 | |
---|
| 45 | [rows,cols,depth] = size( cdata ); %#ok<NASGU> |
---|
| 46 | if ~isempty( alpha ) |
---|
| 47 | alpha = convertToDouble( alpha ); |
---|
| 48 | f = find( alpha==0 ); |
---|
| 49 | if ~isempty( f ) |
---|
| 50 | cdata(f) = nan; |
---|
| 51 | cdata(f + rows*cols) = nan; |
---|
| 52 | cdata(f + 2*rows*cols) = nan; |
---|
| 53 | end |
---|
| 54 | |
---|
| 55 | % Now blend partial alphas |
---|
| 56 | f = find( alpha(:)>0 & alpha(:)<1 ); |
---|
| 57 | if ~isempty(f) |
---|
| 58 | cdata(f) = cdata(f).*alpha(f) + bgcol(1)*(1-alpha(f)); |
---|
| 59 | cdata(f + rows*cols) = cdata(f + rows*cols).*alpha(f) + bgcol(2)*(1-alpha(f)); |
---|
| 60 | cdata(f + 2*rows*cols) = cdata(f + 2*rows*cols).*alpha(f) + bgcol(3)*(1-alpha(f)); |
---|
| 61 | end |
---|
| 62 | |
---|
| 63 | else |
---|
| 64 | % Instead do a "green screen", treating anything pure-green as transparent |
---|
| 65 | f = find((cdata(:,:,1)==0) & (cdata(:,:,2)==1) & (cdata(:,:,3)==0)); |
---|
| 66 | cdata(f) = nan; |
---|
| 67 | cdata(f + rows*cols) = nan; |
---|
| 68 | cdata(f + 2*rows*cols) = nan; |
---|
| 69 | |
---|
| 70 | end |
---|
| 71 | |
---|
| 72 | %-------------------------------------------------------------------------% |
---|
| 73 | function cdata = convertToDouble( cdata ) |
---|
| 74 | % Convert an image to double precision in the range 0 to 1 |
---|
| 75 | switch lower( class( cdata ) ) |
---|
| 76 | case 'double' |
---|
| 77 | % Do nothing |
---|
| 78 | case 'single' |
---|
| 79 | cdata = double( cdata ); |
---|
| 80 | case 'uint8' |
---|
| 81 | cdata = double( cdata ) / 255; |
---|
| 82 | case 'uint16' |
---|
| 83 | cdata = double( cdata ) / 65535; |
---|
| 84 | case 'int8' |
---|
| 85 | cdata = ( double( cdata ) + 128 ) / 255; |
---|
| 86 | case 'int16' |
---|
| 87 | cdata = ( double( cdata ) + 32768 ) / 65535; |
---|
| 88 | otherwise |
---|
| 89 | error( 'GUILayout:LoadIcon:BadCData', ... |
---|
| 90 | 'Image data of type ''%s'' is not supported.', class( cdata ) ); |
---|
| 91 | end |
---|