# mupdf-images.test --
#
#	list and extract images.
#

package require TTXN
  # package under test is tclMuPdf 2.0
package require tclMuPDF 2.0

set thisDir [file dirname [file normalize [info script]]]
set samplesDir [file join $thisDir samples]

set TmpResultDir [file join $thisDir tmpResults]
file mkdir $TmpResultDir
file delete {*}[glob -nocomplain $TmpResultDir/*]

#
# -- Extracting images from sample file PdfRef-1.7.pdf
#
# All the following tests are based on a common setup:

    set pdf [mupdf::open [file join $samplesDir PdfRef-1.7.pdf]]


Name: images-list
Description: {count images}
Test: {
    set p [$pdf getpage 493]
    set L [$p images list]
    llength $L
}
Expected: 4

Name: images-list-1
Description: {list images}
Test: {
    set p [$pdf getpage 493]
    set L [$p images list]
    lindex $L 0
}
Expected: {i01 281 601 {Separation DeviceCMYK} 1 0}

Name: images-list-2a
Description: {list images with a given -id}
Test: {
    set p [$pdf getpage 493]
    set L [$p images list -id i01]
    lindex $L 0
}
Expected: {i01 281 601 {Separation DeviceCMYK} 1 0}

Name: images-list-2b
Description: {list images with a given wrong -id}
Test: {
    set p [$pdf getpage 493]
    set L [$p images list -id xxx]
    lindex $L 0
}
Expected: {}



Name: images-defaultformat-get
Description: {get the default factory setting}
Test: {
    mupdf::imagepattern
}
Expected: IM-%4p

Name: images-defaultformat-set
Description: {get the default factory setting}
Setup: {
    set oldFormat [mupdf::imagepattern]
}
Test: {
    mupdf::imagepattern "XX-%5p(%2i)"
    mupdf::imagepattern
}
Cleanup: {
    mupdf::imagepattern $oldFormat
}
Expected: XX-%5p(%2i)


Name: images-extract
Description: {extract a given image}
Test: {
    set p [$pdf getpage 493]
    set L [$p images extract -id i01 -dir $TmpResultDir]
     # note result is a list of list, even if only one element is extracted
    lassign [lindex $L 0] id name fullpath
    file exists $fullpath
}
Expected: 1


Name: images-extract-1
Description: {extract a given image (with the default imagesnameformat}
Test: {
    set p [$pdf getpage 493]
    set L [$p images extract -id i01 -dir $TmpResultDir]
     # note result is a list of list, even if only one element is extracted
}
Expected: [list [list i01 Im0 $TmpResultDir/IM-0494.png]]

Name: images-extract-2
Description: {extract a given image (with a new imagepattern}
Test: {
    set p [$pdf getpage 493]
    set L [$p images extract -id i01 -dir $TmpResultDir -as "A%5pZZ"]
     # note result is a list of list, even if only one element is extracted
}
Expected: [list [list i01 Im0 $TmpResultDir/A00494ZZ.png]]

Name: images-extract-all-as
Description: {extract all images in a page (with custom imagesnameformat}
Test: {
    set p [$pdf getpage 1140]
     # note that the %i specifier (image index) has not been used
    set L [$p images extract -dir $TmpResultDir -as "X%4p"]
     # extract only filenames
    set L2 {}
    foreach el $L {
        set fname [lindex $el 2]
        lappend L2 [file tail $fname]
    }
    return $L2     
}
Expected: {X1141-1.png X1141-2.png X1141-3.png X1141-4.png}

Name: images-extract-all-as-default
Description: {extract all images in a page (with custom imagepattern}
Test: {
    set p [$pdf getpage 1140]
    mupdf::imagepattern "B%5p(%2i)"

    set L [$p images extract -dir $TmpResultDir]
     # extract only filenames
    set L2 {}
    foreach el $L {
        set fname [lindex $el 2]
        lappend L2 [file tail $fname]
    }
    return $L2     
}
Expected: {B01141(01).png B01141(02).png B01141(03).png B01141(04).png}

Name: images-extract-all-as-default2
Description: {extract all images in a page (with custom imagepattern)}
Test: {
    set p [$pdf getpage 1140]
    mupdf::imagepattern "xImage"

    set L [$p images extract -dir $TmpResultDir]
     # extract only filenames
    set L2 {}
    foreach el $L {
        set fname [lindex $el 2]
        lappend L2 [file tail $fname]
    }
    return $L2     
}
Expected: {xImage-1.png xImage-2.png xImage-3.png xImage-4.png}


#
# -- Common cleanup for tests based on PdfRef-1.7.pdf
#
    $pdf quit
    # don't cleanup TmpResultDir; it may be used for inspection

# #############################################################################

#
# -- Extracting images from sample file Sample-Images-01.pdf 
#
# All the following tests are based on a common setup:

    set pdf [mupdf::open [file join $samplesDir Sample-Images-01.pdf]]

 # Test for different image formats (gray, cmyk, transparency ....)

Name: images-list-10
Description: {list images; some of this have transparency}
Test: {
    set p [$pdf getpage 0]
    set L [$p images list]
}
Expected: {{i01 1197 600 ICCBased 8 0} {i02 135 170 Indexed 8 1} {i03 316 412 ICCBased 8 1}}

Name: images-extract-10
Description: {extract image i1 jpeg}
Test: {
    set p [$pdf getpage 0]
    set L [$p images extract -id i01 -dir $TmpResultDir -as "B00-01"]
     # what could i check ??
    lassign [lindex $L 0] id name fullpath
    file tail $fullpath
}
Expected: B00-01.jpg

Name: images-extract-10a
Description: {extract image i03 (transparent) with transparency yes}
Test: {
    set p [$pdf getpage 0]
    set L [$p images extract -id i03 -dir $TmpResultDir -as "B00-03(T)" -transparency true]
     # what could i check ??
    lassign [lindex $L 0] id name fullpath
    file tail $fullpath
}
Expected: B00-03(T).png

Name: images-extract-10b
Description: {extract image i02 (transparent) with transparency no}
Test: {
    set p [$pdf getpage 0]
    set L [$p images extract -id i03 -dir $TmpResultDir -as "B00-03" -transparency false]
     # what could i check ??
    lassign [lindex $L 0] id name fullpath
    file tail $fullpath
}
Expected: B00-03.png

#
# -- Common cleanup for tests based on Sample-Images-01.pdf 
#
    $pdf quit
    # don't cleanup TmpResultDir; it may be used for inspection






# === cleanup ========================================
::tcltest::cleanupTests
