This is a static copy of a profile report

Home

imagesci/private/writepng (40 calls, 1.519 sec)
Generated 05-Aug-2011 13:01:28 using cpu time.
function in file /usr/local/MATLAB/R2011a/toolbox/matlab/imagesci/private/writepng.m
Copy to new window for comparing multiple runs

Parents (calling functions)

Function NameFunction TypeCalls
imwritefunction40
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
429
png('write', data, map, filena...
401.104 s72.7%
153
textItem = datestr(datenum(var...
400.240 s15.8%
214
if (~ismember({class(data)}, {...
400.055 s3.6%
86
elseif (length(idx) > 1)
2000.022 s1.4%
369
if (ismember(colortype, [PNG_C...
400.011 s0.7%
All other lines  0.087 s5.8%
Totals  1.519 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
imagesci/private/pngMEX-file401.104 s72.7%
datenumfunction400.197 s12.9%
cell.ismemberfunction400.055 s3.6%
datestrfunction400.044 s2.9%
ismemberfunction1200.033 s2.2%
imagesci/private/writepng>CheckTextItemsubfunction400 s0%
Self time (built-ins, overhead, etc.)  0.087 s5.8%
Totals  1.519 s100% 
Code Analyzer results
Line numberMessage
83The variable 'textchunks' appears to change size on every loop iteration. Consider preallocating for speed.
129The variable 'textchunks' appears to change size on every loop iteration. Consider preallocating for speed.
135The variable 'textchunks' appears to change size on every loop iteration. Consider preallocating for speed.
141The variable 'textchunks' appears to change size on every loop iteration. Consider preallocating for speed.
147The variable 'textchunks' appears to change size on every loop iteration. Consider preallocating for speed.
157The variable 'textchunks' appears to change size on every loop iteration. Consider preallocating for speed.
163The variable 'textchunks' appears to change size on every loop iteration. Consider preallocating for speed.
169The variable 'textchunks' appears to change size on every loop iteration. Consider preallocating for speed.
175The variable 'textchunks' appears to change size on every loop iteration. Consider preallocating for speed.
181The variable 'textchunks' appears to change size on every loop iteration. Consider preallocating for speed.
187The variable 'textchunks' appears to change size on every loop iteration. Consider preallocating for speed.
193Best practice is for CATCH to be followed by an identifier that gets the error information.
Coverage results
[ Show coverage for parent directory ]
Total lines in function433
Non-code lines (comments, blank lines)164
Code lines (lines that can run)269
Code lines that did run95
Code lines that did not run174
Coverage (did run/can run)35.32 %
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-2010 The MathWorks, Inc.
18 % $Revision: 1.1.6.7 $ $Date: 2010/07/19 12:54:37 $
19
40 20 if (ndims(data) > 3)
21 error('MATLAB:writepng:tooManyDims', ...
22 '%d-D data not supported for PNG files', ndims(data));
23 end
24
25 % Color type values (as in PNG library defs)
40 26 PNG_COLOR_TYPE_GRAY = 0;
40 27 PNG_COLOR_TYPE_RGB = 2;
40 28 PNG_COLOR_TYPE_PALETTE = 3;
40 29 PNG_COLOR_TYPE_GRAY_ALPHA = 4;
40 30 PNG_COLOR_TYPE_RGB_ALPHA = 6;
31
32 % Set default parameters
40 33 bitdepth = [];
40 34 sigbits = [];
40 35 interlace = 'none';
40 36 transparency = [];
40 37 alpha = [];
40 38 background = [];
40 39 gamma = [];
40 40 chromaticities = [];
40 41 xres = [];
40 42 yres = [];
40 43 resunit = [];
40 44 textchunks = cell(0,2);
40 45 imagemodtime = [];
46
47 % Process param/value pairs
40 48 propStrings = {'interlacetype ', ...
49 'transparency ', ...
50 'bitdepth ', ...
51 'significantbits', ...
52 'alpha ', ...
53 'background ', ...
54 'gamma ', ...
55 'chromaticities ', ...
56 'xresolution ', ...
57 'yresolution ', ...
58 'resolutionunit ', ...
59 'title ', ...
60 'author ', ...
61 'description ', ...
62 'copyright ', ...
63 'creationtime ', ...
64 'software ', ...
65 'disclaimer ', ...
66 'warning ', ...
67 'source ', ...
68 'comment ', ...
69 'imagemodtime '};
70
40 71 for k = 1:2:length(varargin)
200 72 prop = lower(varargin{k});
200 73 if (~ischar(prop))
74 error('MATLAB:writepng:badParameterName', ...
75 'Parameter name must be a string');
76 end
200 77 idx = find(strncmp(prop, propStrings, numel(prop)));
200 78 if (isempty(idx))
79 keyword = varargin{k};
80 textItem = varargin{k+1};
81 keyword = CheckKeyword(keyword);
82 textItem = CheckTextItem(textItem);
83 textchunks{end+1,1} = keyword;
84 textchunks{end,2} = textItem;
85
0.02 200 86 elseif (length(idx) > 1)
87 error('MATLAB:writepng:ambiguousParameter', ...
88 'Ambiguous parameter name "%s"', prop);
89
200 90 else
200 91 prop = deblank(propStrings{idx});
200 92 switch prop
200 93 case 'bitdepth'
94 bitdepth = varargin{k+1};
95
200 96 case 'significantbits'
97 sigbits = varargin{k+1};
98
200 99 case 'interlacetype'
100 interlace = varargin{k+1};
101
200 102 case 'transparency'
103 transparency = varargin{k+1};
104
200 105 case 'alpha'
106 alpha = varargin{k+1};
107
200 108 case 'background'
109 background = varargin{k+1};
110
200 111 case 'gamma'
112 gamma = varargin{k+1};
113
200 114 case 'chromaticities'
115 chromaticities = varargin{k+1};
116
0.01 200 117 case 'xresolution'
40 118 xres = varargin{k+1};
119
160 120 case 'yresolution'
40 121 yres = varargin{k+1};
122
120 123 case 'resolutionunit'
40 124 resunit = varargin{k+1};
125
80 126 case 'title'
127 keyword = 'Title';
128 textItem = CheckTextItem(varargin{k+1});
129 textchunks{end+1,1} = keyword;
130 textchunks{end,2} = textItem;
131
80 132 case 'author'
133 keyword = 'Author';
134 textItem = CheckTextItem(varargin{k+1});
135 textchunks{end+1,1} = keyword;
136 textchunks{end,2} = textItem;
137
80 138 case 'description'
139 keyword = 'Description';
140 textItem = CheckTextItem(varargin{k+1});
141 textchunks{end+1,1} = keyword;
142 textchunks{end,2} = textItem;
143
80 144 case 'copyright'
145 keyword = 'Copyright';
146 textItem = CheckTextItem(varargin{k+1});
147 textchunks{end+1,1} = keyword;
148 textchunks{end,2} = textItem;
149
80 150 case 'creationtime'
40 151 keyword = 'Creation Time';
40 152 if (ischar(varargin{k+1}))
0.24 40 153 textItem = datestr(datenum(varargin{k+1}), 0);
154 else
155 textItem = datestr(varargin{k+1}, 0);
156 end
40 157 textchunks{end+1,1} = keyword;
40 158 textchunks{end,2} = textItem;
159
40 160 case 'software'
40 161 keyword = 'Software';
40 162 textItem = CheckTextItem(varargin{k+1});
40 163 textchunks{end+1,1} = keyword;
40 164 textchunks{end,2} = textItem;
165
166 case 'disclaimer'
167 keyword = 'Disclaimer';
168 textItem = CheckTextItem(varargin{k+1});
169 textchunks{end+1,1} = keyword;
170 textchunks{end,2} = textItem;
171
172 case 'warning'
173 keyword = 'Warning';
174 textItem = CheckTextItem(varargin{k+1});
175 textchunks{end+1,1} = keyword;
176 textchunks{end,2} = textItem;
177
178 case 'source'
179 keyword = 'Source';
180 textItem = CheckTextItem(varargin{k+1});
181 textchunks{end+1,1} = keyword;
182 textchunks{end,2} = textItem;
183
184 case 'comment'
185 keyword = 'Comment';
186 textItem = CheckTextItem(varargin{k+1});
187 textchunks{end+1,1} = keyword;
188 textchunks{end,2} = textItem;
189
190 case 'imagemodtime'
191 try
192 imagemodtime = fix(datevec(varargin{k+1}));
193 catch
194 error('MATLAB:writepng:invalidImageModTime', ...
195 ['ImageModTime could not be converted to a date vector. ' ...
196 'See "help datevec" for allowable syntaxes.']);
197 end
198
199 if (numel(imagemodtime) > 6)
200 error('MATLAB:writepng:tooMuchImageModTimeData', ...
201 'ImageModTime did not result in a six element date vector.')
202 end
203
204 end
200 205 end
206
0.01 200 207 end
208
0.01 40 209 if ((ndims(data) > 3) || (~ismember(size(data,3), [1 3])))
210 error('MATLAB:writepng:wrongImageDimensions', ...
211 'Invalid input image.');
212 end
213
0.05 40 214 if (~ismember({class(data)}, {'double', 'single', 'logical', 'uint8', 'uint16'}))
215 error('MATLAB:writepng:unsupportedImageClass', ...
216 'Unsupported input data class.');
217 end
218
40 219 if (~isempty(alpha) && ((size(alpha,1) ~= size(data,1)) || ...
220 (size(alpha,2) ~= size(data,2))))
221 error('MATLAB:writepng:alphaDoesNotMatchImage', ...
222 'ALPHA must have the same number of rows and columns as the image data.');
223 end
224
225 %
226 % Identify color type
227 %
40 228 isTruecolor = (size(data,3) == 3);
40 229 paletteUsed = ~isempty(map) && ~isTruecolor;
40 230 colorUsed = paletteUsed || isTruecolor;
40 231 alphaUsed = ~isempty(alpha);
40 232 colortype = paletteUsed + 2*colorUsed + 4*alphaUsed;
40 233 if (colortype == 7)
234 error('MATLAB:writepng:alphaNotSupportedForIndexed', ...
235 'Cannot specify alpha channel with an indexed image.');
236 end
237
238 %
239 % Set default bitdepth if not specified
240 %
40 241 if (isempty(bitdepth))
40 242 switch class(data)
40 243 case 'logical'
244 bitdepth = 1;
245
40 246 case {'uint8', 'double', 'single'}
40 247 bitdepth = 8;
248
249 case 'uint16'
250 bitdepth = 16;
251 end
0.01 40 252 end
253
254 %
255 % Validate bitdepth
256 %
40 257 switch colortype
40 258 case PNG_COLOR_TYPE_GRAY
259 if (~ismember(bitdepth, [1 2 4 8 16]))
260 error('MATLAB:writepng:invalidGrayscaleBitDepth', ...
261 'Invalid bitdepth for grayscale image; must be 1, 2, 4, 8, or 16.');
262 end
263
40 264 case PNG_COLOR_TYPE_RGB
0.01 40 265 if (~ismember(bitdepth, [8 16]))
266 error('MATLAB:writepng:invalidRgbBitDepth', ...
267 'Invalid bitdepth for RGB image; must be 8 or 16.');
268 end
269
270 case PNG_COLOR_TYPE_PALETTE
271 if (~ismember(bitdepth, [1 2 4 8]))
272 error('MATLAB:writepng:invalidIndexedBitDepth', ...
273 'Invalid bitdepth for indexed image; must be 1, 2, 4, or 8.');
274 end
275
276 case PNG_COLOR_TYPE_GRAY_ALPHA
277 if (~ismember(bitdepth, [8 16]))
278 error('MATLAB:writepng:invalidGrayscaleAlphaBitDepth', ...
279 'Invalid bitdepth for grayscale image with alpha; must be 8 or 16.');
280 end
281
282 case PNG_COLOR_TYPE_RGB_ALPHA
283 if (~ismember(bitdepth, [8 16]))
284 error('MATLAB:writepng:invalidRgbAlphaBitDepth', ...
285 'Invalid bitdepth for RGB image with alpha; must be 8 or 16.');
286 end
287 end
288
289 %
290 % Scale image if necessary to match requested bitdepth
291 %
40 292 switch class(data)
0.01 40 293 case {'double', 'single'}
294 if (colortype == PNG_COLOR_TYPE_PALETTE)
295 data = data - 1;
296 data = uint8(data);
297
298 else
299 % Grayscale or RGB; clamp data to [0,1] dynamic range before
300 % scaling, rounding, and casting.
301 data = max(min(data,1),0);
302 switch bitdepth
303 case 8
304 data = uint8(255*data);
305
306 case 16
307 data = uint16(65535*data);
308
309 case 4
310 data = uint8(15*data);
311
312 case 2
313 data = uint8(3*data);
314
315 case 1
316 data = uint8(data ~= 0);
317 end
318 end
319
40 320 case 'uint8'
40 321 if (colortype == PNG_COLOR_TYPE_PALETTE)
322 % Nothing to do
323
40 324 else
40 325 switch bitdepth
40 326 case 16
327 data = uint16(data);
328 data = bitor(bitshift(data,8),data);
329
40 330 case 8
331 % Nothing to do
332
333 case 4
334 data = bitshift(data,-4);
335
336 case 2
337 data = bitshift(data,-6);
338
339 case 1
340 % Nothing to do
341 end
40 342 end
343
344 case 'uint16'
345 if (colortype == PNG_COLOR_TYPE_PALETTE)
346 error('MATLAB:writepng:invalidIndexedBitDepth', ...
347 'PNG does not allow 16-bit indexed images.');
348
349 else
350 switch bitdepth
351 case 16
352 % Nothing to do
353
354 case 8
355 data = uint8(bitshift(data,-8));
356
357 case 4
358 data = uint8(bitshift(data,-12));
359
360 case 2
361 data = uint8(bitshift(data,-14));
362
363 case 1
364 data = uint8(data ~= 0);
365 end
366 end
367 end
368
0.01 40 369 if (ismember(colortype, [PNG_COLOR_TYPE_GRAY_ALPHA, ...
370 PNG_COLOR_TYPE_RGB_ALPHA]))
371 %
372 % Scale alpha data if necessary to match data class
373 %
374 switch bitdepth
375 case 8
376 switch class(alpha)
377 case {'double', 'single'}
378 alpha = max(min(alpha,1),0);
379 alpha = uint8(255 * alpha);
380
381 case 'uint16'
382 alpha = uint8(bitshift(alpha, -8));
383
384 case 'uint8'
385 % nothing to do
386
387 otherwise
388 error('MATLAB:writepng:badAlphaClass', ...
389 'Invalid class for alpha');
390 end
391
392 case 16
393 switch class(alpha)
394 case {'double', 'single'}
395 alpha = max(min(alpha,1),0);
396 alpha = uint16(65535 * alpha);
397
398 case 'uint16'
399 % nothing to do
400
401 case 'uint8'
402 alpha = uint16(alpha);
403 alpha = bitor(bitshift(alpha, 8), alpha);
404
405 otherwise
406 error('MATLAB:writepng:badAlphaClass', ...
407 'Invalid class for alpha');
408 end
409 end
410 end
411
412 % Be friendly about specifying resolutions
40 413 if (~isempty(xres) && isempty(yres))
414 yres = xres;
415
40 416 elseif (~isempty(yres) && isempty(xres))
417 xres = yres;
418 end
419
40 420 if (~isempty(xres) && isempty(resunit))
421 resunit = 'unknown';
422 end
423
40 424 if (isempty(xres) && isempty(yres) && ~isempty(resunit))
425 error('MATLAB:writepng:resolutionsRequired', ...
426 'X and Y resolutions required when specifying resolution unit.');
427 end
428
1.10 40 429 png('write', data, map, filename, colortype, bitdepth, ...
430 sigbits, alpha, interlace, ...
431 transparency, background, gamma, ...
432 chromaticities, xres, yres, ...
40 433 resunit, textchunks, imagemodtime);

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