This is a static copy of a profile report

Home

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

Parents (calling functions)

Function NameFunction TypeCalls
graphics/private/renderfunction40
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
477
feval(fmt_s.write, data, map, ...
401.530 s93.3%
419
[data, map, filename, format, ...
400.066 s4.0%
468
validateSizes(data);
400.022 s1.3%
455
fid = fopen(filename, 'a');
400.022 s1.3%
476
try
400 s0%
All other lines  0.000 s0.0%
Totals  1.640 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
imagesci/private/writepngfunction401.519 s92.7%
imwrite>parse_inputssubfunction400.066 s4.0%
imwrite>validateSizessubfunction400.022 s1.3%
imformatsfunction400 s0%
Self time (built-ins, overhead, etc.)  0.033 s2.0%
Totals  1.640 s100% 
Code Analyzer results
No Code Analyzer messages.
Coverage results
[ Show coverage for parent directory ]
Total lines in function487
Non-code lines (comments, blank lines)441
Code lines (lines that can run)46
Code lines that did run19
Code lines that did not run27
Coverage (did run/can run)41.30 %
Function listing
   time   calls  line
1 function imwrite(varargin)
2 %IMWRITE Write image to graphics file.
3 % IMWRITE(A,FILENAME,FMT) writes the image A to the file specified by
4 % FILENAME in the format specified by FMT.
5 %
6 % A can be an M-by-N (grayscale image) or M-by-N-by-3 (color image)
7 % array. A cannot be an empty array. If the format specified is TIFF,
8 % IMWRITE can also accept an M-by-N-by-4 array containing color data
9 % that uses the CMYK color space.
10 %
11 % FILENAME is a string that specifies the name of the file.
12 %
13 % FMT is a string specifying the format of the file. See the reference
14 % page, or the output of the IMFORMATS function, for a list of
15 % supported formats.
16 %
17 % IMWRITE(X,MAP,FILENAME,FMT) writes the indexed image in X and its
18 % associated colormap MAP to FILENAME in the format specified by FMT.
19 % If X is of class uint8 or uint16, IMWRITE writes the actual values in
20 % the array to the file. If X is of class double, IMWRITE offsets the
21 % values in the array before writing, using uint8(X-1). MAP must be a
22 % valid MATLAB colormap. Note that most image file formats do not
23 % support colormaps with more than 256 entries.
24 %
25 % When writing multiframe GIF images, X should be an 4-dimensional
26 % M-by-N-by-1-by-P array, where P is the number of frames to write.
27 %
28 % IMWRITE(...,FILENAME) writes the image to FILENAME, inferring the
29 % format to use from the filename's extension. The extension must be
30 % one of the legal values for FMT.
31 %
32 % IMWRITE(...,PARAM1,VAL1,PARAM2,VAL2,...) specifies parameters that
33 % control various characteristics of the output file. Parameters are
34 % currently supported for GIF, HDF, JPEG, TIFF, PNG, PBM, PGM, and PPM
35 % files.
36 %
37 % Class Support
38 % -------------
39 % The input array A can be of class logical, uint8, uint16, or double.
40 % Indexed images (X) can be of class uint8, uint16, or double; the
41 % associated colormap, MAP, must be double. Input values must be
42 % full (non-sparse).
43 %
44 % The class of the image written to the file depends on the format
45 % specified. For most formats, if the input array is of class uint8,
46 % IMWRITE outputs the data as 8-bit values. If the input array is of
47 % class uint16 and the format supports 16-bit data (JPEG, PNG, and
48 % TIFF), IMWRITE outputs the data as 16-bit values. If the format does
49 % not support 16-bit values, IMWRITE issues an error. Several formats,
50 % such as JPEG and PNG, support a parameter that lets you specify the
51 % bit depth of the output data.
52 %
53 % If the input array is of class double, and the image is a grayscale
54 % or RGB color image, IMWRITE assumes the dynamic range is [0,1] and
55 % automatically scales the data by 255 before writing it to the file as
56 % 8-bit values.
57 %
58 % If the input array is of class double, and the image is an indexed
59 % image, IMWRITE converts the indices to zero-based indices by
60 % subtracting 1 from each element, and then writes the data as uint8.
61 %
62 % If the input array is of class logical, IMWRITE assumes the data is a
63 % binary image and writes it to the file with a bit depth of 1, if the
64 % format allows it. BMP, PNG, or TIFF formats accept binary images as
65 % input arrays.
66 %
67 % GIF-specific parameters
68 % -----------------------
69 %
70 % 'WriteMode' One of these strings: 'overwrite' (the default)
71 % or 'append'. In append mode, a single frame is
72 % added to the existing file.
73 %
74 % 'Comment' A string or cell array of strings containing a
75 % comment to be added to the image. For a cell
76 % array of strings, a carriage return is added
77 % after each row.
78 %
79 % 'DisposalMethod' One of the following strings, which sets the
80 % disposal method of an animated GIF:
81 % 'leaveInPlace', 'restoreBG', 'restorePrevious',
82 % or 'doNotSpecify'.
83 %
84 % 'DelayTime' A scalar value between 0 and 655 inclusive, which
85 % specifies the delay in seconds before displaying
86 % the next image.
87 %
88 % 'TransparentColor' A scalar integer. This value specifies which
89 % index in the colormap should be treated as the
90 % transparent color for the image. If X is uint8
91 % or logical, then indexing starts at 0. If X is
92 % double, then indexing starts at 1.
93 %
94 % 'BackgroundColor' A scalar integer. This value specifies which
95 % index in the colormap should be treated as the
96 % background color for the image and is used for
97 % certain disposal methods in animated GIFs. If X
98 % is uint8 or logical, then indexing starts at 0.
99 % If X is double, then indexing starts at 1.
100 %
101 % 'LoopCount' A finite integer between 0 and 65535 or the value
102 % Inf (the default) which specifies the number of
103 % times to repeat the animation. By default, the
104 % animation will continuously loop. For a value of
105 % 0, the animation will be played once. For a
106 % value of 1, the animation will be played twice,
107 % etc.
108 %
109 % 'ScreenSize' A two element vector specifying the screen height
110 % and width of the frame. When used with
111 % 'Location', this provides a way to write frames
112 % to the image which are smaller than the whole
113 % frame. The remaining values are filled in
114 % according to the 'DisposalMethod'.
115 %
116 % 'Location' A two element vector specifying the offset of the
117 % top left corner of the screen relative to the top
118 % left corner of the image. The first element is
119 % the offset from the top, and the second element
120 % is the offset from the left.
121 %
122 % HDF-specific parameters
123 % -----------------------
124 % 'Compression' One of these strings: 'none' (the default),
125 % 'rle' (only valid for grayscale and indexed
126 % images), 'jpeg' (only valid for grayscale
127 % and RGB images)
128 %
129 % 'Quality' A number between 0 and 100; parameter applies
130 % only if 'Compression' is 'jpeg'; higher
131 % numbers mean quality is better (less image
132 % degradation due to compression), but the
133 % resulting file size is larger
134 %
135 % 'WriteMode' One of these strings: 'overwrite' (the
136 % default) or 'append'
137 %
138 % JPEG-specific parameters
139 % ------------------------
140 % 'Quality' A number between 0 and 100; higher numbers
141 % mean quality is better (less image degradation
142 % due to compression), but the resulting file
143 % size is larger
144 %
145 % 'Comment' A column vector cell array of strings or a
146 % char matrix. Each row of input is written out
147 % as a comment in the JPEG file.
148 %
149 % 'Mode' Either 'lossy' (the default) or 'lossless'
150 %
151 % 'BitDepth' A scalar value indicating desired bitdepth;
152 % for grayscale images this can be 8, 12, or 16;
153 % for truecolor images this can be 8 or 12. Only
154 % lossless mode is supported for 16-bit images.
155 %
156 % JPEG2000-specific parameters
157 % ----------------------------
158 % 'Mode' Either 'lossy' (the default) or 'lossless'.
159 %
160 % 'CompressionRatio' A real value greater than 1 specifying the target
161 % compression ratio which is defined as the ratio of
162 % input image size to the output compressed size. For
163 % example, a value of 2.0 implies that the output
164 % image size will be half of the input image size or
165 % less. A higher value implies a smaller file size and
166 % reduced image quality. This is valid only with
167 % 'lossy' mode. Note that the compression ratio
168 % doesn't take into account the header size, and hence
169 % in some cases the output file size can be larger
170 % than expected.
171 %
172 % 'ProgressionOrder' A string that is one of 'LRCP', 'RLCP', 'RPCL',
173 % 'PCRL' or 'CPRL'. The four character identifiers are
174 % interpreted as L=layer, R=resolution, C=component
175 % and P=position. The first character refers to the
176 % index which progresses most slowly, while the last
177 % refers to the index which progresses most quickly.
178 % The default value is 'LRCP'.
179 %
180 % 'QualityLayers' A positive integer (not exceeding 20) specifying the
181 % number of quality layers. The default value is 1.
182 %
183 % 'ReductionLevels' A positive integer (not exceeding 8) specifying the
184 % number of reduction levels or the wavelet
185 % decomposition levels.
186 %
187 % 'TileSize' A 2-element vector specifying tile height and tile
188 % width. The minimum tile size that can be specified
189 % is [128 128]. The default tile size is same as the
190 % image size.
191 %
192 % 'Comment' A cell array of strings or a char matrix. Each row
193 % of input is written out as a comment in the JPEG2000
194 % file.
195 %
196 % TIFF-specific parameters
197 % ------------------------
198 % 'Colorspace' One of these strings: 'rgb', 'cielab', or
199 % 'icclab'. The default value is 'rgb'. This
200 % parameter is used only when the input array,
201 % A, is M-by-N-by-3. See the reference page
202 % for more details about creating L*a*b* TIFF
203 % files.
204 %
205 % In order to create a CMYK TIFF, the colorspace
206 % parameter should not be used. It is sufficient
207 % to specify the input array A as M-by-N-by-4.
208 %
209 % 'Compression' One of these strings: 'none', 'packbits'
210 % (default for nonbinary images), 'lzw', 'deflate',
211 % 'jpeg', 'ccitt' (default for binary images),
212 % 'fax3', 'fax4'; 'ccitt', 'fax3', and
213 % 'fax4' are valid for binary images only.
214 %
215 % 'jpeg' is a lossy compression scheme; other
216 % compression modes are lossless.
217 %
218 % When using JPEG compression, the 'RowsPerStrip'
219 % parameter must be specified and must be a multiple
220 % of 8.
221 %
222 % 'Description' Any string; fills in the ImageDescription
223 % field returned by IMFINFO
224 %
225 % 'Resolution' A two-element vector containing the
226 % XResolution and YResolution, or a scalar
227 % indicating both resolutions; the default value
228 % is 72
229 %
230 % 'RowsPerStrip' A scalar value. The default will be such that each
231 % strip is about 8K bytes.
232 %
233 % 'WriteMode' One of these strings: 'overwrite' (the
234 % default) or 'append'
235 %
236 % PNG-specific parameters
237 % -----------------------
238 % 'Author' A string
239 %
240 % 'Description' A string
241 %
242 % 'Copyright' A string
243 %
244 % 'CreationTime' A string
245 %
246 % 'ImageModTime' A MATLAB datenum or a string convertible to a
247 % date vector via the DATEVEC function. Values
248 % should be in UTC time.
249 %
250 % 'Software' A string
251 %
252 % 'Disclaimer' A string
253 %
254 % 'Warning' A string
255 %
256 % 'Source' A string
257 %
258 % 'Comment' A string
259 %
260 % 'InterlaceType' Either 'none' or 'adam7'
261 %
262 % 'BitDepth' A scalar value indicating desired bitdepth;
263 % for grayscale images this can be 1, 2, 4,
264 % 8, or 16; for grayscale images with an
265 % alpha channel this can be 8 or 16; for
266 % indexed images this can be 1, 2, 4, or 8;
267 % for truecolor images with or without an
268 % alpha channel this can be 8 or 16
269 %
270 % 'Transparency' This value is used to indicate transparency
271 % information when no alpha channel is used.
272 %
273 % For indexed images: a Q-element vector in
274 % the range [0,1]; Q is no larger than the
275 % colormap length; each value indicates the
276 % transparency associated with the
277 % corresponding colormap entry
278 % For grayscale images: a scalar in the range
279 % [0,1]; the value indicates the grayscale
280 % color to be considered transparent
281 % For truecolor images: a 3-element vector in
282 % the range [0,1]; the value indicates the
283 % truecolor color to be considered
284 % transparent
285 %
286 % You cannot specify 'Transparency' and
287 % 'Alpha' at the same time.
288 %
289 % 'Background' The value specifies background color to be
290 % used when compositing transparent pixels.
291 %
292 % For indexed images: an integer in the range
293 % [1,P], where P is the colormap length
294 % For grayscale images: a scalar in the range
295 % [0,1]
296 % For truecolor images: a 3-element vector in
297 % the range [0,1]
298 %
299 % 'Gamma' A nonnegative scalar indicating the file
300 % gamma
301 %
302 % 'Chromaticities' An 8-element vector [wx wy rx ry gx gy bx
303 % by] that specifies the reference white
304 % point and the primary chromaticities
305 %
306 % 'XResolution' A scalar indicating the number of
307 % pixels/unit in the horizontal direction
308 %
309 % 'YResolution' A scalar indicating the number of
310 % pixels/unit in the vertical direction
311 %
312 % 'ResolutionUnit' Either 'unknown' or 'meter'
313 %
314 % 'Alpha' A matrix specifying the transparency of
315 % each pixel individually; the row and column
316 % dimensions must be the same as the data
317 % array; may be uint8, uint16, or double, in
318 % which case the values should be in the
319 % range [0,1]
320 %
321 % 'SignificantBits' A scalar or vector indicating how many
322 % bits in the data array should be regarded
323 % as significant; values must be in the range
324 % [1,bitdepth]
325 %
326 % For indexed images: a 3-element vector
327 % For grayscale images: a scalar
328 % For grayscale images with an alpha channel:
329 % a 2-element vector
330 % For truecolor images: a 3-element vector
331 % For truecolor images with an alpha channel:
332 % a 4-element vector
333 %
334 % In addition to these PNG parameters, you can use any
335 % parameter name that satisfies the PNG specification for
336 % keywords: only printable characters, 80 characters or
337 % fewer, and no leading or trailing spaces. The value
338 % corresponding to these user-specified parameters must be a
339 % string that contains no control characters except for
340 % linefeed.
341 %
342 % RAS-specific parameters
343 % -----------------------
344 % 'Type' One of these strings: 'standard'
345 % (uncompressed, b-g-r color order with
346 % truecolor images), 'rgb' (like 'standard',
347 % but uses r-g-b color order for truecolor
348 % images), 'rle' (run-length encoding of 1-bit
349 % and 8-bit images)
350 %
351 % 'Alpha' A matrix specifying the transparency of each
352 % pixel individually; the row and column
353 % dimensions must be the same as the data
354 % array; may be uint8, uint16, or double. May
355 % only be used with truecolor images.
356 %
357 % PBM, PGM, and PPM-specific parameters
358 % ------------------------
359 % 'Encoding' One of these strings: 'ASCII' for plain encoding
360 % or 'rawbits' for binary encoding. Default is 'rawbits'.
361 % 'MaxValue' A scalar indicating the maximum gray or color
362 % value. Available only for PGM and PPM files.
363 % For PBM files, this value is always 1. Default
364 % is 65535 if image array is 'uint16' and 255 otherwise.
365 %
366 % Table: summary of supported image types
367 % ---------------------------------------
368 % BMP 1-bit, 8-bit and 24-bit uncompressed images
369 %
370 % GIF 8-bit images
371 %
372 % HDF 8-bit raster image datasets, with or without associated
373 % colormap; 24-bit raster image datasets; uncompressed or
374 % with RLE or JPEG compression
375 %
376 % JPEG 8-bit, 12-bit, and 16-bit Baseline JPEG images
377 %
378 % JPEG2000 1-bit, 8-bit, and 16-bit JPEG2000 images
379 %
380 % PBM Any 1-bit PBM image, ASCII (plain) or raw (binary) encoding.
381 %
382 % PCX 8-bit images
383 %
384 % PGM Any standard PGM image. ASCII (plain) encoded with
385 % arbitrary color depth. Raw (binary) encoded with up
386 % to 16 bits per gray value.
387 %
388 % PNG 1-bit, 2-bit, 4-bit, 8-bit, and 16-bit grayscale
389 % images; 8-bit and 16-bit grayscale images with alpha
390 % channels; 1-bit, 2-bit, 4-bit, and 8-bit indexed
391 % images; 24-bit and 48-bit truecolor images; 24-bit
392 % and 48-bit truecolor images with alpha channels
393 %
394 % PNM Any of PPM/PGM/PBM (see above) chosen automatically.
395 %
396 % PPM Any standard PPM image. ASCII (plain) encoded with
397 % arbitrary color depth. Raw (binary) encoded with up
398 % to 16 bits per color component.
399 %
400 % RAS Any RAS image, including 1-bit bitmap, 8-bit indexed,
401 % 24-bit truecolor and 32-bit truecolor with alpha.
402 %
403 % TIFF Baseline TIFF images, including 1-bit, 8-bit, 16-bit,
404 % and 24-bit uncompressed images, images with packbits
405 % compression, images with LZW compression, and images
406 % with Deflate compression; 8-bit and 24-bit images with
407 % JPEG compression; 1-bit images with CCITT 1D, Group 3,
408 % and Group 4 compression; CIELAB, ICCLAB, and CMYK images.
409 %
410 % XWD 8-bit ZPixmaps
411 %
412 % Please read the file libtiffcopyright.txt for more information.
413 %
414 % See also IMFINFO, IMREAD, IMFORMATS, FWRITE, GETFRAME.
415
416 % Copyright 1984-2010 The MathWorks, Inc.
417 % $Revision: 1.1.6.20 $ $Date: 2010/09/24 14:34:38 $
418
0.07 40 419 [data, map, filename, format, paramPairs, msg] = parse_inputs(varargin{:});
420
40 421 if (~isempty(msg))
422 error('MATLAB:imwrite:inputParsing', msg);
423 end
424
40 425 if (isempty(data))
426 error('MATLAB:imwrite:emptyImage', 'Image data can not be empty.');
40 427 elseif (issparse(data))
428 error('MATLAB:imwrite:sparseData', 'Cannot write sparse data.');
429 end
430
40 431 if (isempty(format))
432
433 format = get_format_from_filename(filename);
434 if (isempty(format))
435 error('MATLAB:imwrite:fileFormat', 'Unable to determine the file format from the filename.');
436 end
437
438 end
439
440 % Get the format details from the registry.
40 441 fmt_s = imformats(format);
442
443 % Signed data may cause unexpected results.
40 444 switch (class(data))
40 445 case {'int8', 'int16', 'int32', 'int64'}
446
447 if (~isJpeg2000(fmt_s))
448 warning('MATLAB:imwrite:signedPixelData', ...
449 'Unexpected results may occur with signed pixel data.')
450 end
451
452 end
453
454 % Verify that the file can be written to.
0.02 40 455 fid = fopen(filename, 'a');
40 456 if (fid == -1)
457 error('MATLAB:imwrite:fileOpen', ...
458 'Can''t open file "%s" for writing.\nYou may not have write permission.', ...
459 filename);
40 460 else
461 % File can be created. Get full filename.
40 462 filename = fopen(fid);
40 463 fclose(fid);
40 464 end
465
466 % Currently all image formats use 32-bit offsets to data.
40 467 try
0.02 40 468 validateSizes(data);
469 catch myException
470 cleanupEmptyFile(filename);
471 rethrow(myException);
472 end
473
474 % Call the writing function if it exists.
40 475 if (~isempty(fmt_s.write))
40 476 try
1.53 40 477 feval(fmt_s.write, data, map, filename, paramPairs{:});
478 catch myException
479 cleanupEmptyFile(filename);
480 rethrow(myException);
481 end
482
483 else
484 cleanupEmptyFile(filename);
485 error('MATLAB:imwrite:writeFunctionRegistration', 'No writing function for format %s. See "help imformats".', ...
486 format);
487 end

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