-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtf_image.m
62 lines (55 loc) · 1.64 KB
/
tf_image.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
function tf_image(options, xy, image, wh)
% low-level version of tfImage
%
% tf_image(options, xy, image, wh)
% tf_image(options, xy, image)
%
% xy and wh are given in cm, xy with respect to the lower left corner
if nargin < 4
wh = [0 0];
end
% possibly flip dimensions
swh = sign(wh);
swh(swh == 0) = 1;
options = sprintf('xscale=%d,yscale=%d,%s', swh, options);
wh = abs(wh);
icopt = '';
if wh(1) > 1e-12
icopt = sprintf('width=%.6fcm', wh(1));
end
if wh(2) > 1e-12
if ~isempty(icopt)
icopt = [icopt ','];
end
icopt = [icopt, sprintf('height=%.6fcm', wh(2))];
end
if ~isempty(icopt)
icopt = ['[' icopt ']'];
end
if ischar(image)
[~, name, ext] = fileparts(image);
name = [tempname(tf_get('temp')) '_' name ext];
copyfile(image, name)
else
name = [tempname(tf_get('temp')) '.png'];
if isnumeric(image) % truecolor or grayscale
imwrite(image, name)
elseif iscell(image)
im2 = image{2};
image = image{1};
if isinteger(image) % indexed with map
if size(im2, 1) < 256
imwrite(uint8(image), im2, name);
else
% convert indexed to truecolor to overcome png limitation
% of a maximum of 256 palette entries
image = reshape(im2(image + 1, :), [size(image), 3]);
imwrite(image, name)
end
else % truecolor or grayscale with alpha
imwrite(image, name, 'Alpha', im2)
end
end
end
tf_path('', xy, ...
sprintf('node[%s] {\\includegraphics%s{%s}}', options, icopt, name))