# ibisa - Image-Based Identification/Search for Archaeology
# Copyright (C) 2008, 2009 Anthony Beurive'

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#----------------------------------------------------------------------

package provide octave 1.0
package require libibisa

namespace eval ::octave {
    variable liboctava 0

    set problem [catch {
        package require liboctava
        set liboctava 1
    } result]
}

proc ::octave::has:support {} {
    return $::octave::liboctava
}

proc ::octave::init {args} {
    eval:string "
        saving_history(0);
        history_size(0);
        warning(\"off\");
        ignore_function_time_stamp(\"all\");"

    foreach dir $args {
        eval:string "addpath(\"$dir\");"
    }
}

proc ::octave::eval_ {delete function args} {
    if !$::octave::liboctava return
    ::libibisa::octave_setFunction $function
    foreach arg $args {
        ::libibisa::octave_addParameter $arg
    }

    ::libibisa::octave_evaluate
    if $delete {
        ::eval ::octave::delete $args
    }

    set result [::octave::result]
    return $result
}

proc ::octave::eval {function args} {
    return [::eval ::octave::eval_ 1 $function $args]
}

proc ::octave::eval:no:delete {function args} {
    return [::eval ::octave::eval_ 0 $function $args]
}

proc ::octave::eval:string {string} {
    if !$::octave::liboctava return
    #::libibisa::octave_setDebug 1
    ::libibisa::octave_setFunction eval
    ::libibisa::octave_addParameter_string $string
    ::libibisa::octave_evaluate
}

proc ::octave::result {} {
    if !$::octave::liboctava return
    set result [::libibisa::octave_getResult]
    if {$result == "NULL"} return

    set m [$result rows]
    set n [$result cols]

    set matrix {}

    if {$m == 1} {
        for {set j 0} {$j < $n} {incr j} {
            lappend matrix [$result get 0 $j]
        }

    } elseif {$n == 1} {
        for {set i 0} {$i < $m} {incr i} {
            lappend matrix [$result get $i 0]
        }

    } else {
        for {set i 0} {$i < $m} {incr i} {
            set row {}
            for {set j 0} {$j < $n} {incr j} {
                lappend row [$result get $i $j]
            }
            lappend matrix $row
        }
    }

    ::octave::delete $result
    return $matrix
}

proc ::octave::delete {args} {
    foreach arg $args {
        $arg -acquire
        $arg -delete
    }
}

proc ::octave::matrix {m n} {
    if !$::octave::liboctava return
    return [::libibisa::octave_matrix_newInstance $m $n]
}

proc ::octave::image:matrix {img} {
    if !$::octave::liboctava return
    return [::libibisa::octave_matrix_newInstance $img]
}

