function I=bitmaptext(lines,I,pos,options)
% The function BITMAPTEXT will insert textline(s) on the specified position
% in the image.
%
% I=bitmaptext(Text,Ibackground,Position,options)
%
% inputs,
% Text : Cell array with text lines
% Ibackground: the bitmap used as background when a m x n x 3 matrix
% color plots are made, when m x n a greyscale plot. If empty []
% autosize to fit text.
% Position: x,y position of the text
% options: struct with options such as color
%
% outputs,
% Iplot: The bitmap containing the plotted text
%
% note,
% Colors are always [r(ed) g(reen) b(lue) a(pha)], with range 0..1.
% when Ibackground is grayscale, the mean of r,g,b is used as grey value.
%
% options,
% options.Color: The color of the text.
% options.FontSize: The size of the font, 1,2 or 3 (small,medium,large).
%
% example,
%
% % The text consisting of 2 lines
% lines={'a_A_j_J?,','ImageText version 1.1'};
% % Background image
% I=ones([256 256 3]);
% % Plot text into background image
% I=bitmaptext(lines,I,[1 1],struct('FontSize',3));
% % Show the result
% figure, imshow(I),
%
% Function is written by D.Kroon University of Twente (March 2009)
global character_images;
% Process inputs
defaultoptions=struct('Color',[0 0 1 1],'FontSize',1);
if(~exist('options','var')),
options=defaultoptions;
else
tags = fieldnames(defaultoptions);
for i=1:length(tags)
if(~isfield(options,tags{i})), options.(tags{i})=defaultoptions.(tags{i}); end
end
if(length(tags)~=length(fieldnames(options))),
warning('register_images:unknownoption','unknown options found');
end
end
% If single line make it a cell array
if(~iscell(lines)), lines={lines}; end
if(~exist('I','var')), I=[]; end
if(exist('pos','var')),
if(length(pos)~=2)
error('imagtext:inputs','position must have x,y coordinates');
end
else
pos=[1 1];
end
% Set the size of the font
fsize=options.FontSize;
% The character bitmap and character set;
character_set='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()_+-=[]\;'''',./{}|:"<>?';
if(isempty(character_images)), character_images=load_font(); end
% Font parameters
Font_sizes_x=[8 10 11];
Font_sizes_y=[13 15 18];
Font_startx=[1 1 1];
Font_starty=[1 14 29];
% Get maximum sentence length
lengths=zeros(1,length(lines));
for i=1:length(lines), lengths(i)=length(lines{i}); end
max_line_length=max(lengths);
% Make text image from the lines
lines_image=zeros([(Font_sizes_y(fsize)+4)*length(lines),max_line_length*Font_sizes_x(fsize)],'double');
for j=1:length(lines)
line=lines{j};
for i=1:length(line),
[t,p]=find(character_set==line(i));
if(~isempty(p))
p=p(1)-1;
character_bitmap=character_images(Font_starty(fsize):(Font_starty(fsize)+Font_sizes_y(fsize)-1),Font_startx(fsize)+(1+p*Font_sizes_x(fsize)):Font_startx(fsize)+((p+1)*Font_sizes_x(fsize)));
posx=Font_sizes_x(fsize)*(i-1);
posy=(Font_sizes_y(fsize)+4)*(j-1);
lines_image((1:Font_sizes_y(fsize))+posy,(1:Font_sizes_x(fsize))+posx)=character_bitmap;
end
end
end
if(isempty(I)), I=zeros([size(lines_image) 3]); end
% Remove part of textimage which will be outside of the output image
if(pos(1)<1), lines_image=lines_image(2-pos(1):end,:); pos(1)=1; end
if(pos(2)<2), lines_image=lines_image(:,2-pos(2):end); pos(2)=1; end
if((pos(1)+size(lines_image,1))>size(I,1)), dif=size(I,1)-(pos(1)+size(lines_image,1)); lines_image=lines_image(1:end+dif,:); end
if((pos(2)+size(lines_image,2))>size(I,2)), dif=size(I,2)-(pos(2)+size(lines_image,2)); lines_image=lines_image(:,1:end+dif); end
% Make text image the same size as background image
I_line=zeros([size(I,1) size(I,2)]);
I_line(pos(1):(pos(1)+size(lines_image,1)-1),pos(2):(pos(2)+size(lines_image,2)-1))=lines_image;
I_line=I_line*options.Color(4);
% Insert the text image into the output image
if(~isempty(lines_image))
if(size(I,3)==3)
for i=1:3
I(:,:,i)=I(:,:,i).*(1-I_line)+options.Color(i)*(I_line);
end
else
I=I.*(1-I_line)+mean(options.Color(1:3))*(I_line);
end
end
function character_images=load_font()
character_images=uint8([0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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