# This file is a Tcl script to test out BMP reading and writing.
# It is organized in the standard fashion for Tcl tests.
#

if {[string compare test [info procs test]] == 1} {
    source defs
}

set bmpdata \
{Qk12AgAAAAAAADYAAAAoAAAAEAAAAAwAAAABABgAAAAAAEACAABtCwAAbQsAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA2dnZ2dnZ2dnZAAAAgP/w
gP/wgP/wgP/wgP/wgP/wgP/wgP/wgP/wgP/wgP/wAAAA2dnZ2dnZ2dnZAAAAgP/wgP/wgP/w
gP/wgP/wgP/wgP/wgP/wgP/wgP/wgP/wAAAA2dnZ2dnZ2dnZAAAAgP/wgP/wgP/wgP/wgP/w
gP/wgP/wgP/wgP/wgP/wgP/wAAAA2dnZ2dnZ2dnZAAAAgP/wgP/wgP/wgP/wgP/wgP/wgP/w
gP/wgP/wgP/wgP/wAAAA2dnZ2dnZ2dnZAAAAgP/wgP/wgP/wgP/wgP/wgP/wgP/wgP/wgP/w
gP/wgP/wAAAA2dnZ2dnZ2dnZAAAAgP/wgP/wgP/wgP/wgP/wgP/wgP/wgP/wgP/wgP/wgP/w
AAAA2dnZ2dnZ2dnZAAAAgP/wgP/wgP/wgP/wgP/wgP/wgP/wgP/wgP/wgP/wgP/wAAAA2dnZ
2dnZ2dnZAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA2dnZ2dnZ2dnZ
2dnZAAAAgP/wgP/wgP/wgP/wgP/wgP/wAAAA2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ
AAAAgP/wgP/wgP/wgP/wAAAA2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZAAAA
AAAAAAAAAAAA2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ}

catch {image delete i}
test bmp-1.1 {} {
    image create photo i -file folder.bmp
    i data -format bmp
} $bmpdata
test bmp-1.2 {} {
    catch {image delete i}
    image create photo i -data $bmpdata
    set data [i data -format bmp]
} $bmpdata
test bmp-1.3 {} {
    i blank
    i put $bmpdata
    set data [i data -format bmp]
} $bmpdata
test bmp-1.4 {} {
    i blank
    i put $bmpdata -format bmp
    set data [i data -format bmp]
} $bmpdata
if {[info tclversion] < 8.0} {
    return
}

test bmp-2.0 {Binary I/O with BMP images} {
    i blank
    set f [open folder.bmp r]
    fconfigure $f -translation binary
    set return [catch {i put [read $f]} msg]
    close $f
    lappend return $msg
} {0 {}}

test bmp-2.1 {Save with given aspect ratio} {
    i blank
    i put $bmpdata -format bmp
    i write folder_out.bmp -format {bmp -resolution {300 100}}
    set f [open folder_out.bmp r]
    fconfigure $f -translation binary
    binary scan [read $f] x38i2 xyres
    set return [read $f]
    close $f
    set xyres
} {2925 975}

test bmp-2.2 {Save with constant resolution of 300 dpi} {
    i blank
    i put $bmpdata -format bmp
    i write folder_out.bmp -format {bmp -resolution {300 i}}
    set f [open folder_out.bmp r]
    fconfigure $f -translation binary
    binary scan [read $f] x38i2 xyres
    set return [read $f]
    close $f
    set xyres
} {11811 11811}

test bmp-2.3 {Save with given resolution of x 100dpi and y 50dpi} {
    i blank
    i put $bmpdata -format bmp
    i write folder_out.bmp -format {bmp -resolution {100 50 i}}
    set f [open folder_out.bmp r]
    fconfigure $f -translation binary
    binary scan [read $f] x38i2 xyres
    set return [read $f]
    close $f
    set xyres
} {3937 1969}
catch {file delete folder_out.bmp}
