Code covered by the BSD License  

Highlights from
Hieroglyphs

image thumbnail
from Hieroglyphs by Ligong Han
Latin alphabet to Egyptian hieroglyphs Translator

latin2hieroglyph(str,nl,showim)
function cpic = latin2hieroglyph(str,nl,showim)
%LATIN2HIEROGLYPH   translate Latin alphabet to Egyptian hieroglyphs
%
%   $Revision: 1.1 $  $Date: 08-Jul-2013 14:07:16 $  $ improved: bug fixes.
%   $Revision: 1.2 $  $Date: 08-Jul-2013 19:12:27 $  $ added: support color images.
%
%   Copyright Phymhan Studio.

y = []; %stores data of each letter
picsize = []; %[height, width]
widthm  = []; %max of width
heightm = []; %max of height

if nargin > 0
    if ~iscell(str)
        str = {str};
    end
    n_c = length(str);
    if nargin == 1
        nl = Inf(1,n_c);
        showim = true;
    elseif nargin == 2
        if length(nl) < n_c
            nl = [nl,Inf(1,n_c-length(nl))];
        end
        showim = true;
    elseif nargin > 3
        fprintf('Too many input arguments.\n');
        cpic = {};
        return
    end
else
    %GUI
    answer = inputdlg({'Enter the string:',...
        'Number of letters in a line:'},...
        'Input for Hieroglyph Convertor',...
        3,{'HIEROGLYPH','Inf'},'on');
    if isempty(answer)
        return
    end
    str = answer(1);
    nl = str2double(answer{2});
    showim = true;
end

if ~init_rawpics
    return
end

n_c = length(str);
% cpic = cell(1,nc);
N = zeros(1,n_c);
nline = zeros(1,n_c); %number of letters in a line
mline = zeros(1,n_c); %number of lines
for k_c = 1:n_c
    N(k_c) = length(str{k_c});
    nline(k_c) = min(nl(k_c),N(k_c));
    mline(k_c) = ceil(N(k_c)/nline(k_c));
end
mm = sum(mline)*heightm; %max of m
nm = 0; %max of n
if ~ismatrix(y{1})
    n_clr = 3;
else
    n_clr = 1;
end

mpic = uint8(255*ones(mm,max(nline)*widthm,n_clr));
for k_c = 1:n_c
    for k = 0:mline(k_c)-1
        m = (k+sum(mline(1:k_c-1)))*heightm;
        n = 0;
        for kk = 0:nline(k_c)-1
            kn = k*nline(k_c)+kk+1;
            if kn <= N(k_c)
                idx = letteridx(str{k_c}(kn));
                if idx > 0 %letters or numbers
                    dm = floor((heightm-picsize(idx,1))/2);
                    dn = 0;%floor((widthm -picsize(idx,2))/2);
                    for k_clr = 1:n_clr
                        mpic(m+dm+1:m+dm+picsize(idx,1),...
                            n+dn+1:n+dn+picsize(idx,2),k_clr) = ...
                            y{idx}(:,:,k_clr);
                    end
                    n = n+picsize(idx,2);
                else %space
                    n = n+widthm;
                end
            end
        end
        if n > nm
            nm = n;
        end
    end
end
mpic = mpic(1:mm,1:nm,:);
if showim
    %adjust and show image
    offset = 10;
    figure('name','Egyptian Hieroglyphs',...
        'numbertitle','off','menubar','none','resize','off');
    imshow(mpic);
    hf = gcf;
    ha = gca;
    set(ha,'unit','pixels');
    fpos = get(hf,'position');
    apos = get(ha,'position');
    set(hf,'position',[fpos(1:2) apos(3:4)+2*offset]);
    set(ha,'position',[offset offset apos(3:4)]);
    %save image
    answer = inputdlg({'File name:','Save as type:'},...
        'Save file',...
        1,{str{1},'.bmp'},'on');
    if ~isempty(answer)
        filename = uigetdir(matlabroot,'Select Directory to Save');
        fname = answer{1};
        ftype = answer{2};
        if ftype ~= '.'
            ftype = ['.' ftype];
        end
        if ischar(filename) && exist(filename,'dir')
            imwrite(mpic,fullfile(filename,[fname ftype]));
        end
    end
end
cpic = {mpic};

    function idx = letteridx(s)
        if s>=48 && s<=57 %number
            idx = 52+s-'0'+1;
        elseif s>=65 && s<=90 %uppercase
            idx = s-'A'+1;
        elseif s>=97 && s<=122 %lowercase
            idx = 26+s-'a'+1;
        else
            idx = -1;
        end
    end

    function stt = init_rawpics
        stt = true;
        % Raw Data Processing
        if exist('hieroglyphs.mat','file')
            s = load('hieroglyphs.mat');
            y = s.y;
            picsize = s.picsize;
            widthm  = s.widthm;
            heightm = s.heightm;
        else
            fprintf(['LATIN2HIEROGLYPH(): hieroglyphs.mat not found.\n'...
                'finding pictures ...\n\n']);
            %find pictures
            pic_exist = false(1,62);
            for ii = 1:26
                if exist(['A'+ii-1 '.PNG'],'file')
                    pic_exist(ii) = true;
                end
            end %uppercase
            for ii = 1:26
                pic_exist(26+ii) = pic_exist(ii);
            end %lowercase
            for ii = 1:10
                if exist(['0'+ii-1 '.PNG'],'file')
                    pic_exist(52+ii) = true;
                end
            end %number
            if ~all(pic_exist(1:26))
                fprintf(['LATIN2HIEROGLYPH(): picture missed.\n'...
                    'finding hieroglyphs.zip ...\n\n']);
                %find hieroglyphs.zip
                if exist('hieroglyphs.zip','file')
                    unzip('hieroglyphs.zip',dir_uplevel(which(mfilename)));
                    file_dir = fullfile(dir_uplevel(which(mfilename)),'pics');
                else
                    fprintf(['LATIN2HIEROGLYPH(): hieroglyphs.zip not found.\n'...
                        'download it from the webpage:\n'...
                        'http://ishare.iask.sina.com.cn/f/37476663.html\n']);
                    stt = false;
                    return
                end
            else
                file_dir = dir_uplevel(which('A.PNG'));
            end
            %read and processing
            m_pic = imread(fullfile(file_dir,'A.PNG'));
            if ~ismatrix(m_pic)
                n_color = 3;
            else
                n_color = 1;
            end
            y = cell(1,26*2+10); %'A' to 'Z', 'a' to 'z', '0' to '9'
            picsize = zeros(26*2+10,n_color);
            %uppercase
            for ii = 1:26
                if exist(fullfile(file_dir,['A'+ii-1 '.PNG']),'file')
                    m_pic = imread(fullfile(file_dir,['A'+ii-1 '.PNG']));
                    y{ii} = m_pic;%rgb2gray(m_pic);
                else
                    y{ii} = uint8(repmat(zeros(32),[1,1,n_color]));
                end
                picsize(ii,:) = size(y{ii});
            end
            %lowercase
            for ii = 1:26
                y{26+ii} = y{ii}(:,end:-1:1,:);%fliplr(y{ii});
                picsize(26+ii,:) = size(y{26+ii});
            end
            %number
            for ii = 1:10
                if exist(fullfile(file_dir,['0'+ii-1 '.PNG']),'file')
                    m_pic = imread(fullfile(file_dir,['0'+ii-1 '.PNG']));
                    y{52+ii} = m_pic;%rgb2gray(m_pic);
                else
                    y{ii} = uint8(repmat(zeros(32),[1,1,n_color]));
                end
                picsize(52+ii,:) = size(y{52+ii});
            end
            widthm  = max(picsize(:,2));
            heightm = max(picsize(:,1));
            %save vars to hieroglyphs.mat
            save(fullfile(dir_uplevel(file_dir),'hieroglyphs.mat'),...
                'y','picsize','widthm','heightm');
        end
    end

    function s = dir_uplevel(folderpath,levels)
        if nargin < 2
            levels = 1;
        end
        if iscell(folderpath)
            nfolder = length(folderpath);
            s = cell(1,nfolder);
            for ii = 1:length(folderpath)
                ix = find(folderpath{ii} == '\'|folderpath{ii} == '/');
                s{ii} = folderpath{ii}(1:ix(end-levels+1)-1);
            end
        else
            ix = find(folderpath == '\' | folderpath == '/');
            if ~isempty(ix)
                s = folderpath(1:ix(end-levels+1)-1);
            else
                s = '';
            end
        end
    end

end

Contact us