% Cmatscii - Function to generate Chinese character image, like ASCII arts % % Usage: % picture=Cmatscii(im,grayLookUp,tooDark,sizeXY,inverted) % % Arguments: % im - 2D array of image brightness colours. % Image can be grey scale or colour. % grayLookUp - data structure that store the available chinese characters % with corresponding grayscale value % Optional: % tooDark - pixel value to trim off characters that are too dark % can be 2 elements, then it specify 'too bright' as well % [120 225] is default value % sizeXY - [width,height] of output picture in number of characters % [50 70] for full screen (default) % [125 180] for A4 print % inverted - 0 for black character on white background (default) % 1 for white character on black background % % % Output: % picture - Output picture of black and white chinese character art! % % Author: % Tzu Yen Wong % wongt AT csse DOT uwa DOT edu DOT au % Department of Computer Science & Software Engineering % The University of Western Australia % % Mar 2004 % % Some codes and ideas are adapted from pk's matscii function %1234567890123456789012345678901234567890123456789012345678901234567890123456789 function picture=Cmatscii(im,grayLookUp,tooDark,sizeXY,inverted) %MMMMMM Setting up optional arguments and default values MMMMMM eval('inverted;','inverted=0;'); eval('tooDark;','tooDark = 120;'); eval('sizeXY;','sizeXY = [70 50];'); % sizeXY is the number of characters (each char is 13 pixals) if isempty(tooDark) || ~isnumeric(tooDark) tooDark = 120; %disp(sprintf('pixel value < %d defined to be too dark.',tooDark)) end if length(tooDark)==2, tooBright=max(tooDark); tooDark =min(tooDark); else tooBright=255; end if any(sizeXY<10)||length(sizeXY)<1, sizeXY = [70 50]; end if length(sizeXY)~=2, sizeXY(2)=sizeXY(1)*0.75; % assume landscape end %MMMMMM End Setting up optional arguments and default values MMMMMM %MMMMMM Initialise variables and values MMMMMM I = grayLookUp{257}{1}; textWidth = grayLookUp{259}{1}(1); textHeight = grayLookUp{259}{1}(2); im = double(im); width = sizeXY(1); %180 for print, 70 for Full screen height = sizeXY(2); %125 for print, 50 for Full screen %MMMMMM End Initialise variables and values MMMMMM charAspect = textWidth/textHeight; % Width/height aspect ratio of characters if ndims(im) == 3 % We have a colour image im = (im(:,:,1) + im(:,:,2) + im(:,:,2))/3; % Grey value = (R+G+B)/3 end if inverted==1, im=255-im; end [rows, cols] = size(im); % making sure potrait & landscape are not mixed up if rows>cols && height100, showprogress(1,[1 rows],2,2); end for r = 1: rows if sizeXY(1)>100, showprogress(2,[1 rows],r,2); end for c = 1:cols % getting the closest intensity value [Y,index]=min(abs(I-im(r,c))); g=I(index)+1; % compositing the output picture picture((r-1)*textHeight+1:r*textHeight,(c-1)*textWidth+1:c*textWidth)... = grayLookUp{g}{ceil(rand*length(grayLookUp{g}))}; end end if inverted==1, picture = ~picture; end if sizeXY(1)>100, showprogress(3,[1 rows],r,2); end show(picture), truesize