function m = readimage(fil)
    lignecourante=[]
    m=[]
    u=file("open",fil,"old")
    head=read(u,1,1,"(a)")
    niveau=1;
    if head<>"P2"
        error("Not a PGM file")
    end

    c="#";
    while c=="#",
        lignecourante=read(u,1,1,"(a)");
        c=sscanf(lignecourante,"%c");
    end;
    [sz1,sz2]=sscanf(lignecourante,"%d%d");
    m=matrix(read(u,1,sz1*sz2),sz1,sz2);

    file("close",u)
    m = m';
endfunction