This is a static copy of a profile report

Home

imagesci/private/writepng (3 calls, 0.323 sec)
Generated 14-Nov-2016 07:47:22 using cpu time.
function in file /usr/local/MATLAB/MATLAB_Production_Server/R2015a/toolbox/matlab/imagesci/private/writepng.m
Copy to new window for comparing multiple runs

Parents (calling functions)

Function NameFunction TypeCalls
imwritefunction3
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
280
pngwritec(data, map, filename,...
30.272 s84.4%
232
textItem = datestr(datenum(cti...
30.020 s6.2%
20
[results, unmatched] = parseIn...
30.020 s6.2%
51
end
30.010 s3.1%
284
resunit, textchunks, imagemodt...
30 s0%
All other lines  0 s0%
Totals  0.323 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
imagesci/private/pngwritecMEX-file30.272 s84.4%
datenumfunction30.020 s6.2%
imagesci/private/writepng>parseInputssubfunction30.020 s6.2%
datestrfunction30 s0%
imagesci/private/writepng>CheckTextItemsubfunction30 s0%
validatestringfunction60 s0%
ismemberfunction90 s0%
Self time (built-ins, overhead, etc.)  0.010 s3.1%
Totals  0.323 s100% 
Code Analyzer results
Line numberMessage
Coverage results
Show coverage for parent directory
Total lines in function284
Non-code lines (comments, blank lines)94
Code lines (lines that can run)190
Code lines that did run79
Code lines that did not run111
Coverage (did run/can run)41.58 %
Function listing
time 
calls 
 line
   1 
function writepng(data, map, filename, varargin)
   2 
%WRITEPNG Write a PNG file to disk.
   3 
%   WRITEPNG(I,[],FILENAME) writes the grayscale image I
   4 
%   to the file specified by the string FILENAME.
   5 
%
   6 
%   WRITEPNG(RGB,[],FILENAME) writes the truecolor image
   7 
%   represented by the M-by-N-by-3 array RGB.
   8 
%
   9 
%   WRITEPNG(X,MAP,FILENAME) writes the indexed image X with
  10 
%   colormap MAP.  The resulting file will contain the equivalent
  11 
%   truecolor image.
  12 
%
  13 
%   WRITEPNG(...,PARAM,VAL,...) sets the specified parameters.
  14 
%
  15 
%   See also IMREAD, IMWRITE, IMFINFO.
  16 

  17 
%   Copyright 1984-2014 The MathWorks, Inc.
  18 

  19 
% Parse the inputs 
  0.02 
      3 
  20 
[results, unmatched] = parseInputs(data,map,filename,varargin{:}); 
  21 

      3 
  22 
if ((ndims(data) > 3) || (~ismember(size(data,3), [1 3]))) 
  23 
    error(message('MATLAB:imagesci:writepng:wrongImageDimensions'));
  24 
end
  25 

      3 
  26 
alpha = results.alpha; 
  27 

  28 
% Identify color type
      3 
  29 
isTruecolor = (size(data,3) == 3); 
      3 
  30 
paletteUsed = ~isempty(map) && ~isTruecolor; 
      3 
  31 
colorUsed = paletteUsed || isTruecolor; 
      3 
  32 
alphaUsed = ~isempty(alpha); 
      3 
  33 
colortype = paletteUsed + 2*colorUsed + 4*alphaUsed; 
      3 
  34 
if (colortype == 7) 
  35 
    error(message('MATLAB:imagesci:writepng:alphaNotSupportedForIndexed'));
  36 
end
  37 

  38 
% Set default bitdepth if not specified
      3 
  39 
bitdepth = results.bitdepth; 
      3 
  40 
if (isempty(bitdepth)) 
      3 
  41 
    switch class(data) 
      3 
  42 
        case 'logical' 
  43 
            bitdepth = 1;
  44 
            
      3 
  45 
        case {'uint8', 'double', 'single'} 
      3 
  46 
            bitdepth = 8; 
  47 
            
  48 
        case 'uint16'
  49 
            bitdepth = 16;
  50 
    end
  0.01 
      3 
  51 
end 
  52 

  53 

  54 
% Color type values (as in PNG library defs)
      3 
  55 
PNG_COLOR_TYPE_GRAY = 0; 
      3 
  56 
PNG_COLOR_TYPE_RGB = 2; 
      3 
  57 
PNG_COLOR_TYPE_PALETTE = 3; 
      3 
  58 
PNG_COLOR_TYPE_GRAY_ALPHA = 4; 
      3 
  59 
PNG_COLOR_TYPE_RGB_ALPHA = 6; 
  60 

  61 

  62 
% Validate bitdepth
      3 
  63 
switch colortype 
      3 
  64 
    case PNG_COLOR_TYPE_GRAY 
  65 
        if (~ismember(bitdepth, [1 2 4 8 16]))
  66 
            error(message('MATLAB:imagesci:writepng:invalidGrayscaleBitDepth'));
  67 
        end
  68 
        
      3 
  69 
    case { PNG_COLOR_TYPE_RGB, PNG_COLOR_TYPE_RGB_ALPHA } 
      3 
  70 
        if (~ismember(bitdepth, [8 16])) 
  71 
            error(message('MATLAB:imagesci:writepng:invalidRgbBitDepth'));
  72 
        end
  73 
        
  74 
    case PNG_COLOR_TYPE_PALETTE
  75 
        if (~ismember(bitdepth, [1 2 4 8]))
  76 
            error(message('MATLAB:imagesci:writepng:invalidIndexedBitDepth'));
  77 
        end
  78 
        
  79 
    case PNG_COLOR_TYPE_GRAY_ALPHA
  80 
        if (~ismember(bitdepth, [8 16]))
  81 
            error(message('MATLAB:imagesci:writepng:invalidGrayscaleAlphaBitDepth'));
  82 
        end
  83 
        
  84 
end
  85 

  86 
%
  87 
% Scale image if necessary to match requested bitdepth
  88 
%
      3 
  89 
switch class(data) 
      3 
  90 
    case {'double', 'single'} 
  91 
        if (colortype == PNG_COLOR_TYPE_PALETTE)
  92 
            data = data - 1;
  93 
            data = uint8(data);
  94 
            
  95 
        else
  96 
            % Grayscale or RGB; clamp data to [0,1] dynamic range before
  97 
            % scaling, rounding, and casting.
  98 
            data = max(min(data,1),0);
  99 
            switch bitdepth
 100 
                case 8
 101 
                    data = uint8(255*data);
 102 
                    
 103 
                case 16
 104 
                    data = uint16(65535*data);
 105 
                    
 106 
                case 4
 107 
                    data = uint8(15*data);
 108 
                    
 109 
                case 2
 110 
                    data = uint8(3*data);
 111 
                    
 112 
                case 1
 113 
                    data = uint8(data ~= 0);
 114 
            end
 115 
        end
 116 
        
      3 
 117 
    case 'uint8' 
      3 
 118 
        if (colortype == PNG_COLOR_TYPE_PALETTE) 
 119 
            % Nothing to do
 120 
            
      3 
 121 
        else 
      3 
 122 
            switch bitdepth 
      3 
 123 
                case 16 
 124 
                    data = uint16(data);
 125 
                    data = bitor(bitshift(data,8),data);
 126 
                    
      3 
 127 
                case 8 
 128 
                    % Nothing to do
 129 
                    
 130 
                case 4
 131 
                    data = bitshift(data,-4);
 132 
                    
 133 
                case 2
 134 
                    data = bitshift(data,-6);
 135 
                    
 136 
                case 1
 137 
                    % Nothing to do
 138 
            end
      3 
 139 
        end 
 140 
        
 141 
    case 'uint16'
 142 
        switch bitdepth
 143 
            case 16
 144 
                % Nothing to do
 145 
                
 146 
            case 8
 147 
                data = uint8(bitshift(data,-8));
 148 
                
 149 
            case 4
 150 
                data = uint8(bitshift(data,-12));
 151 
                
 152 
            case 2
 153 
                data = uint8(bitshift(data,-14));
 154 
                
 155 
            case 1
 156 
                data = uint8(data ~= 0);
 157 
        end
 158 
end
 159 

      3 
 160 
if (ismember(colortype, [PNG_COLOR_TYPE_GRAY_ALPHA, ... 
 161 
        PNG_COLOR_TYPE_RGB_ALPHA]))
 162 
    %
 163 
    % Scale alpha data if necessary to match data class
 164 
    %
 165 
    switch bitdepth
 166 
        case 8
 167 
            switch class(alpha)
 168 
                case {'double', 'single'}
 169 
                    alpha = max(min(alpha,1),0);
 170 
                    alpha = uint8(255 * alpha);
 171 
                    
 172 
                case 'uint16'
 173 
                    alpha = uint8(bitshift(alpha, -8));
 174 
                    
 175 
                case 'uint8'
 176 
                    % nothing to do
 177 
                    
 178 
            end
 179 
            
 180 
        case 16
 181 
            switch class(alpha)
 182 
                case {'double', 'single'}
 183 
                    alpha = max(min(alpha,1),0);
 184 
                    alpha = uint16(65535 * alpha);
 185 
                    
 186 
                case 'uint16'
 187 
                    % nothing to do
 188 
                    
 189 
                case 'uint8'
 190 
                    alpha = uint16(alpha);
 191 
                    alpha = bitor(bitshift(alpha, 8), alpha);
 192 
                    
 193 
            end
 194 
    end
 195 
end
 196 

      3 
 197 
sigbits        = results.significantbits; 
      3 
 198 
transparency   = results.transparency; 
      3 
 199 
background     = results.background; 
      3 
 200 
gamma          = results.gamma; 
      3 
 201 
chromaticities = results.chromaticities; 
      3 
 202 
xres           = results.xresolution; 
      3 
 203 
yres           = results.yresolution; 
      3 
 204 
if ischar(results.resolutionunit) 
      3 
 205 
    resunit        = validatestring(results.resolutionunit,{'unknown','meter'}); 
 206 
else
 207 
    resunit = results.resolutionunit;
 208 
end
      3 
 209 
interlace      = validatestring(results.interlacetype,{'none','adam7'}); 
      3 
 210 
imagemodtime   = results.imagemodtime; 
 211 

      3 
 212 
textchunks = cell(0,2); 
      3 
 213 
strs = {'Title','Author','Description','Copyright','Software', ... 
 214 
    'Disclaimer', 'Warning','Source','Comment'};
      3 
 215 
for j = 1:numel(strs) 
     27 
 216 
    param_name = lower(strs{j}); 
     27 
 217 
    if isfield(results,lower(param_name)) 
     27 
 218 
        param_value = results.(param_name); 
     27 
 219 
        if ~isempty(param_value) 
      3 
 220 
            textchunks{end+1,1} = strs{j}; %#ok<AGROW> 
      3 
 221 
            textItem = CheckTextItem(param_value); 
      3 
 222 
            textchunks{end,2} = textItem; 
      3 
 223 
        end 
     27 
 224 
    end 
     27 
 225 
end 
 226 

 227 

      3 
 228 
if ~isempty(results.creationtime) 
      3 
 229 
    keyword = 'Creation Time'; 
      3 
 230 
    ctime = results.creationtime; 
      3 
 231 
    if ischar(ctime) 
  0.02 
      3 
 232 
        textItem = datestr(datenum(ctime), 0); 
 233 
    else
 234 
        textItem = datestr(ctime, 0);
 235 
    end
      3 
 236 
    textchunks{end+1,1} = keyword; 
      3 
 237 
    textchunks{end,2} = textItem; 
      3 
 238 
end 
      3 
 239 
if ~isempty(results.imagemodtime) 
 240 
    try
 241 
        imagemodtime = fix(datevec(results.imagemodtime));
 242 
    catch me
 243 
        error(message('MATLAB:imagesci:writepng:invalidImageModTime'));
 244 
    end
 245 
    
 246 
    if (numel(imagemodtime) > 6)
 247 
        error(message('MATLAB:imagesci:writepng:tooMuchImageModTimeData'))
 248 
    end
 249 
end
 250 

 251 
% validate and process any unmatched parameters.
      3 
 252 
if ~isempty(unmatched) 
      3 
 253 
    param_names = fields(unmatched); 
      3 
 254 
    nelts = numel(param_names); 
      3 
 255 
    for j = 1:nelts 
 256 
        param_name = param_names{j};
 257 
        keyword = CheckKeyword(param_name);
 258 
        item = CheckTextItem(unmatched.(param_name));
 259 
        textchunks{end+1,1} = keyword; %#ok<AGROW>
 260 
        textchunks{end,2} = item;
 261 
    end
      3 
 262 
end 
 263 

 264 
% Be friendly about specifying resolutions
      3 
 265 
if (~isempty(xres) && isempty(yres)) 
 266 
    yres = xres;
 267 
    
      3 
 268 
elseif (~isempty(yres) && isempty(xres)) 
 269 
    xres = yres;
 270 
end
 271 

      3 
 272 
if (~isempty(xres) && isempty(resunit)) 
 273 
    resunit = 'unknown';
 274 
end
 275 

      3 
 276 
if (isempty(xres) && isempty(yres) && ~isempty(resunit)) 
 277 
    error(message('MATLAB:imagesci:writepng:resolutionsRequired'));
 278 
end
 279 

  0.27 
      3 
 280 
pngwritec(data, map, filename, colortype, bitdepth, ... 
 281 
    sigbits, alpha, interlace, ...
 282 
    transparency, background, gamma, ...
 283 
    chromaticities, xres, yres, ...
      3 
 284 
    resunit, textchunks, imagemodtime); 

Other subfunctions in this file are not included in this listing.