Re: Embedding Graphics.

new topic     » goto parent     » topic index » view thread      » older message » newer message

For those of you who missed it:


-- Begin INCBM.E --

--- Include Bitmap --- BMP to ASCII with mild compression   ---   G.Burke 1/98
--- Include Sequence - Added 2/98
--
--- (c)1998 HMI Software.                            --- SHAREWARE ---
--
--
--  Usage:      i1 = incbm( s1 , i2 )
--
--              Where s1 is the full bitmap path
--                    i1 will return 0 if successful or a
--                          read_bitmap() error code.
--                    i2 is the desired number of characters
--                          per line in the created file (max 197)
--
--              --------------------------------------------------------------
--
--              include name.e
--              bm = ebm(name)
--
--              Where name is the name of the origional bitmap.
--                    bm will be a {palette,bitmap} sequence as returned
--                    by read_bitmap()
--
--              --------------------------------------------------------------
--
--  Examples:   include incbm.e
--              integer ret
--
--              ret=incbm("c:\\this.bmp",80)
--
--              if ret then
--                  puts(1,"Error loading .BMP")
--                  abort(0)
--              end if
--
--              --- --- --- --- --- --- --- --- ---
--
--              include this.e
--              include ebm.e
--              include image.e
--              sequence bm
--
--              if graphics_mode(261) then end if
--
--              bm=ebm(this)
--
--              all_palette(bm[1])
--              display_image({0,0},bm[2])
--
--
--  incbm() generates a file with the same name as the origional bitmap
--  file but with a .E extension. This file comtains the data for the
--  bitmap and can be included in your programs like any other .E file.
--
--- incbm() works with any 256 color bitmap file readable by read_bitmap().
--
--  Euphoria currently (1.5a) has limits to the size of a single command,
--  so large complex bitmaps may generate an error when you attempt to
--  retrieve them. This problem is compounded when a program is bound with
--  the hide strings option. According to Rob future releases of Euphoria
--  will not have this limit.
--
--- Including a bitmap in this fashion is *NOT* the most efficiant way to
--- package graphics with a program. It usually results in two copies of
--- the image being kept in memory (and probably swapped out). It is however
--- the easiest way I know of to achieve this. This progam was designed for
--- no-fuss including of small or simple bitmaps with programs.
--
--  For simple images, despite the conversion to ASCII, and the char(34)
--  overhead, the generated .e file will be smaller than the origional bitmap
--  and will also compress to a smaller .ZIP file. If the image is complex
--  (i.e. digitized photos) the .E/.ZIP may be slightly larger than the
--  origional.
--
--- If you are including a file for your use in a program only, use 197
--- characters per line. If you want the code to be visable in a standard
--- editor, use 80.
--
--
-------------------------------------------------------------------------------
--  INCSEQ --
--
--  incseq() & eseq() work in the same fashion as incbm() & ebm().
--
--  incseq only works with 1D sequences and 2D sequences that are 'rectangular'
--  i.e all level 2 sequences are of equal length;- tipically a bitmap. In both
--  cases, the sequences must contain only 8 bit integers.

include machine.e
include image.e

integer     fn,cpl,op


procedure cr()
    if op=cpl-4 then
        puts(fn,{34,'\n','&',34})
        op=0
    end if
    op=op+1
end procedure

procedure pts(integer p)
    if p='\\' then
        if op>cpl-6 then
            puts(fn,{34,'\n','&',34,'\\','\\'})
            op=2
        else
            puts(fn,{'\\','\\'})
            op=op+1
        end if
    else
        puts(fn,p)
    end if
end procedure

procedure putseven(integer c)
    c=c+35
    if c>255 then
        cr() puts(fn,32)
        cr() pts(c-200)
    else
        cr() pts(c)
    end if
end procedure

global function incbm(sequence path,integer cperl)
    sequence data,s,name,al,bmp
    integer  xx,n,w,h,p,f,c
    object   rb
    if cperl>197 then
        cperl=197
    end if
    cpl=cperl
    rb=read_bitmap(path)
    if atom(rb) then
        return rb
    end if
    bmp=rb[2] name=path al=rb[1]
    c=find('\\',name)
    while c do
        name=name[c+1..length(name)]
        c=find('\\',name)
    end while
    name=name[1..length(name)-4]
    fn=open(name&".e","wb")
    puts(fn,"\nglobal constant "&name&"="&"\n"&34)
    w=length(bmp[1])
    h=length(bmp)
    s=int_to_bytes(w)
    op=0
    putseven(s[1])
    putseven(s[2])
    putseven(length(al))
    for x=1 to length(al) do
        for q=1 to 3 do
            putseven(al[x][q])
        end for
    end for
    for y=1 to h do
        xx=1 data={}
        while xx<=w do
            n=1 f=1
            p=bmp[y][xx]
            for q= xx+1 to length(bmp[y]) do
                if bmp[y][q]=p then
                    n=n+1
                    if n>255 then
                        n=255 f=0 exit
                    end if
                else
                    f=0 exit
                end if
            end for
            if f then n= 258 end if
            if n<4 then
                putseven(bmp[y][xx])
                xx=xx+1
            else
                cr()
                puts(fn,33)
                putseven(n-4)
                putseven(bmp[y][xx])
                if n=258 then
                    exit
                end if
                xx=xx+n
            end if
        end while
    end for
    bmp={}
    puts(fn,34&"\n\n")
    close(fn)
    return 0
end function

global function incseq(sequence bmp,integer cperl)
    sequence data,s,name
    integer  xx,n,w,h,p,f
    if cperl>197 then
        cperl=197
    end if
    cpl=cperl
    puts(1,"Enter a name for the generated file :")
    name=gets(0)
    puts(1,"\n")
    name=name[1..length(name)-1]
    fn=open(name&".e","wb")
    puts(fn,"\nglobal constant "&name&"="&"\n"&34)
    if atom(bmp[1]) then
        puts(fn,'o')
        w=length(bmp)
        s=int_to_bytes(w)
        op=1
        putseven(s[1])
        putseven(s[2])
        xx=1 data={}
        while xx<=w do
            n=1 f=1
            p=bmp[xx]
            for q= xx+1 to w do
                if bmp[q]=p then
                    n=n+1
                    if n>255 then
                        n=255 f=0 exit
                    end if
                else
                    f=0 exit
                end if
            end for
            if f then n= 258 end if
            if n<4 then
                putseven(bmp[xx])
                xx=xx+1
            else
                cr()
                puts(fn,33)
                putseven(n-4)
                putseven(bmp[xx])
                if n=258 then
                    exit
                end if
                xx=xx+n
            end if
        end while
        bmp={}
        puts(fn,34&"\n\n")
        close(fn)
        return 0
    else
        puts(fn,'t')
        w=length(bmp[1])
        h=length(bmp)
        s=int_to_bytes(w)
        op=1
        putseven(s[1])
        putseven(s[2])
        for y=1 to h do
            xx=1 data={}
            while xx<=w do
                n=1 f=1
                p=bmp[y][xx]
                for q= xx+1 to length(bmp[y]) do
                    if bmp[y][q]=p then
                        n=n+1
                        if n>255 then
                            n=255 f=0 exit
                        end if
                    else
                        f=0 exit
                    end if
                end for
                if f then n= 258 end if
                if n<4 then
                    putseven(bmp[y][xx])
                    xx=xx+1
                else
                    cr()
                    puts(fn,33)
                    putseven(n-4)
                    putseven(bmp[y][xx])
                    if n=258 then
                        exit
                    end if
                    xx=xx+n
                end if
            end while
        end for
        bmp={}
        puts(fn,34&"\n\n")
        close(fn)
        return 0
    end if
end function

global function eseq(sequence dat)
    -- optomized for speed
    sequence b,l,s
    integer  x,c,n,w,nc
    s=repeat(0,4)
    x=2
    nc=dat[1]
    s[1]=dat[x]-35
    if s[1]=-3 then
        x=x+1
        s[1]=dat[x]+165
    end if
    x=x+1
    s[2]=dat[x]-35
    if s[2]=-3 then
        x=x+1
        s[2]=dat[x]+165
    end if
    w=bytes_to_int(s)
    l={} b={}
    x=x+1
    while x<= length(dat) do
        if dat[x]!=33 then
            if dat[x]!=32 then
                l=l&dat[x]-35
                x=x+1
            else
                l=l&dat[x+1]+165
                x=x+2
            end if
        else
            x=x+1
            if dat[x]=32 then
                x=x+1
                c=dat[x]+165
            else
                c=dat[x]-35
            end if
            if c=254 then
                x=x+1
                if dat[x]=32 then
                    x=x+1
                    c=dat[x]+165
                else
                    c=dat[x]-35
                end if
                l=l&repeat(c,w-length(l))
                x=x+1
            else
                x=x+1
                if dat[x]=32 then
                    x=x+1
                    n=dat[x]+165
                else
                    n=dat[x]-35
                end if
                l=l&repeat(n,c+4)
                x=x+1
            end if
        end if
        if length(l)=w then
            if nc='o' then
                return l
            end if
            b=append(b,l)
            l={}
        end if
    end while
    return b
end function

global function ebm(sequence dat)
    -- optomized for speed
    sequence b,l,s,p
    integer  x,c,n,w,nc
    s=repeat(0,4)
    x=1
    s[1]=dat[x]-35
    if s[1]=-3 then
        x=x+1
        s[1]=dat[x]+165
    end if
    x=x+1
    s[2]=dat[x]-35
    if s[2]=-3 then
        x=x+1
        s[2]=dat[x]+165
    end if
    x=x+1
    nc=dat[x]-35
    if nc=-3 then
        x=x+1
        nc=dat[x]+165
    end if
    w=bytes_to_int(s)
    l={} b={}
    x=x+1
    p=repeat(repeat(0,3),nc)
    for g=1 to nc do
        for q=1 to 3 do
            p[g][q]=dat[x]-35
            if p[g][q]=-3 then
                x=x+1
                p[g][q]=dat[x]+165
            end if
            x=x+1
        end for
    end for
    while x<= length(dat) do
        if dat[x]!=33 then
            if dat[x]!=32 then
                l=l&dat[x]-35
                x=x+1
            else
                l=l&dat[x+1]+165
                x=x+2
            end if
        else
            x=x+1
            if dat[x]=32 then
                x=x+1
                c=dat[x]+165
            else
                c=dat[x]-35
            end if
            if c=254 then
                x=x+1
                if dat[x]=32 then
                    x=x+1
                    c=dat[x]+165
                else
                    c=dat[x]-35
                end if
                l=l&repeat(c,w-length(l))
                x=x+1
            else
                x=x+1
                if dat[x]=32 then
                    x=x+1
                    n=dat[x]+165
                else
                    n=dat[x]-35
                end if
                l=l&repeat(n,c+4)
                x=x+1
            end if
        end if
        if length(l)=w then
            b=append(b,l)
            l={}
        end if
    end while
    return {p,b}
end function

-- END INCBM.E --

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu