source: MML/trunk/machine/SOLEIL/common/toolbox/GUILayout/+uiextras/loadLayoutIcon.m @ 4

Last change on this file since 4 was 4, checked in by zhangj, 11 years ago

Initial import--MML version from SOLEIL@2013

File size: 2.9 KB
Line 
1function 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
22error( nargchk( 1, 2, nargin ) );
23if nargin < 2
24    bgcol = get( 0, 'DefaultUIControlBackgroundColor' );
25end
26
27% First try normally
28this_dir = fileparts( mfilename( 'fullpath' ) );
29icon_dir = fullfile( this_dir, 'Resources' );
30if exist( imagefilename, 'file' )
31    [cdata,map,alpha] = imread( imagefilename );
32elseif exist( fullfile( icon_dir, imagefilename ), 'file' )
33    [cdata,map,alpha] = imread( fullfile( icon_dir, imagefilename ));
34else
35    error( 'GUILayout:loadIcon:FileNotFound', 'Cannot open file ''%s''.', imagefilename );
36end
37
38if ~isempty( map )
39    cdata = ind2rgb( cdata, map );
40end
41
42% Convert to double before applying transparency
43cdata = convertToDouble( cdata );
44
45[rows,cols,depth] = size( cdata ); %#ok<NASGU>
46if ~isempty( alpha )
47alpha = 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   
63else
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   
70end
71
72%-------------------------------------------------------------------------%
73function cdata = convertToDouble( cdata )
74% Convert an image to double precision in the range 0 to 1
75switch 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 ) );
91end
Note: See TracBrowser for help on using the repository browser.