-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmovie2gif.m
108 lines (102 loc) · 3.88 KB
/
movie2gif.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
function movie2gif(mov, gifFile, varargin)
% ==================
% Matlab movie to GIF Converter.
%
% Syntax: movie2gif(mov, gifFile, prop, value, ...)
% =================================================
% The list of properties is the same like for the command 'imwrite' for the
% file format gif:
%
% 'BackgroundColor' - A scalar integer. This value specifies which index in
% the colormap should be treated as the transparent
% color for the image and is used for certain disposal
% methods in animated GIFs. If X is uint8 or logical,
% then indexing starts at 0. If X is double, then
% indexing starts at 1.
%
% 'Comment' - A string or cell array of strings containing a comment to be
% added to the image. For a cell array of strings, a carriage
% return is added after each row.
%
% 'DelayTime' - A scalar value between 0 and 655 inclusive, that specifies
% the delay in seconds before displaying the next image.
%
% 'DisposalMethod' - One of the following strings, which sets the disposal
% method of an animated GIF: 'leaveInPlace',
% 'restoreBG', 'restorePrevious', or 'doNotSpecify'.
%
% 'LoopCount' - A finite integer between 0 and 65535 or the value Inf (the
% default) which specifies the number of times to repeat the
% animation. By default, the animation loops continuously.
% For a value of 0, the animation will be played once. For a
% value of 1, the animation will be played twice, etc.
%
% 'TransparentColor' - A scalar integer. This value specifies which index
% in the colormap should be treated as the transparent
% color for the image. If X is uint8 or logical, then
% indexing starts at 0. If X is double, then indexing
% starts at 1
%
% *************************************************************************
% Copyright 2007-2013 by Nicolae Cindea.
if (nargin < 2)
error('Too few input arguments');
end
if (nargin == 2)
frameNb = size(mov, 2);
isFirst = true;
h = waitbar(0, 'Generate GIF file...');
for i = 1:frameNb
waitbar((i-1)/frameNb, h);
[RGB, ~] = frame2im(mov(i));
if (exist('rgb2ind', 'file'))
[IND, map] = rgb2ind(RGB,256);
else
[IND, map] = aRGB2IND(RGB);
end
if isFirst
imwrite(IND, map, gifFile, 'gif');
isFirst=false;
else
imwrite(IND, map, gifFile, 'gif', 'WriteMode', 'append');
end
end
close(h);
end
if (nargin > 2)
h = waitbar(0, 'Generate GIF file...');
frameNb = size(mov, 2);
isFirst = true;
for i = 1:frameNb
waitbar((i-1)/frameNb, h);
[RGB, ~] = frame2im(mov(i));
if (exist('rgb2ind', 'file'))
[IND, map] = rgb2ind(RGB,256);
else
[IND, map] = aRGB2IND(RGB);
end
if isFirst
args = varargin;
imwrite(IND, map, gifFile, 'gif', args{:});
isFirst=false;
% supress 'LoopCount' option from the args!!
args = varargin;
l = length(args);
posLoopCount = 0;
for ii = 1:l
if(ischar(args{ii}))
if strcmp(args{ii}, 'LoopCount')
posLoopCount = ii;
end
end
end
if (posLoopCount)
args = {args{1:posLoopCount-1}, args{posLoopCount+2:end}};
end
else
imwrite(IND, map, gifFile, 'gif', 'WriteMode', 'append', ...
args{:});
end
end
close(h);
end