Index: modules/png/ChangeLog ================================================================== --- modules/png/ChangeLog +++ modules/png/ChangeLog @@ -1,5 +1,9 @@ +2018-12-12 Harald Oehlmann + + * png.tcl: add function getPixelDimension to get physical pixel size + 2013-02-01 Andreas Kupries * * Released and tagged Tcllib 1.15 ======================== * Index: modules/png/pkgIndex.tcl ================================================================== --- modules/png/pkgIndex.tcl +++ modules/png/pkgIndex.tcl @@ -1,2 +1,2 @@ if {![package vsatisfies [package provide Tcl] 8.2]} {return} -package ifneeded png 0.2 [list source [file join $dir png.tcl]] +package ifneeded png 0.3 [list source [file join $dir png.tcl]] Index: modules/png/png.man ================================================================== --- modules/png/png.man +++ modules/png/png.man @@ -1,6 +1,7 @@ -[manpage_begin png n 0.1.2] +[vset VERSION 0.3] +[manpage_begin png n [vset VERSION]] [keywords comment] [keywords image] [keywords png] [keywords timestamp] [copyright {2004, Code: Aaron Faupell }] @@ -8,11 +9,11 @@ [moddesc {Image manipulation}] [titledesc {PNG querying and manipulation of meta data}] [category {File formats}] [require Tcl 8.2] [require crc32] -[require png [opt 0.1.2]] +[require png [opt [vset VERSION]]] [description] [para] This package provides commands to query and modify PNG images. PNG stands for [term {Portable Network Graphics}] and is specified at @@ -123,10 +124,23 @@ is found. [arg keyword] has to be less than 80 characters long to conform to the PNG specification. [arg keyword2] is the translated [arg keyword], in the language specified by the language identifier [arg lang]. +[call [cmd ::png::getPixelDimension] [arg file]] + +Returns a dictionary with keys [const ppux], [const ppuy] and +[const unit] if the information is present. Otherwise, it returns the empty +string. + +[para] The values of [const ppux] and [const ppuy] return the pixel +per unit value in X or Y direction. + +[para] The allowed values for key [const unit] are [const meter] and +[const unknown]. In the case of meter, the dpi value can be calculated +by multiplying with the conversion factor [const 0.0254]. + [call [cmd ::png::image] [arg file]] Given a PNG file returns the image in the list of scanlines format used by Tk_GetColor. [call [cmd ::png::write] [arg file] [arg data]] Index: modules/png/png.tcl ================================================================== --- modules/png/png.tcl +++ modules/png/png.tcl @@ -7,11 +7,11 @@ # See the file "license.terms" for information on usage and redistribution # of this file, and for a DISCLAIMER OF ALL WARRANTIES. # # RCS: @(#) $Id: png.tcl,v 1.11 2012/07/09 16:35:04 afaupell Exp $ -package provide png 0.2 +package provide png 0.3 namespace eval ::png {} proc ::png::_openPNG {file {mode r}} { set fh [open $file $mode] @@ -207,10 +207,32 @@ seek $fh [expr {$len + 4}] current } close $fh return -code error "no data chunk found" } + +proc ::png::getPixelDimension {file} { + set fh [_openPNG $file] + + while {[set r [read $fh 8]] != ""} { + binary scan $r Ia4 len type + if {$type == "pHYs"} { + set r [read $fh [expr {$len + 4}]] + binary scan $r IIc ppux ppuy unit + close $fh + # mask out sign bit, from tcl 8.5, one may use u specifier + set res [list ppux [expr {$ppux & 0xFFFFFFFF}]\ + ppuy [expr {$ppuy & 0xFFFFFFFF}]\ + unit] + if {$unit == 1} {lappend res meter} else {lappend res unknown} + return $res + } + seek $fh [expr {$len + 4}] current + } + close $fh + return +} proc ::png::image {file} { set fh [_openPNG $file] set chunks [_chunks $fh] set cdata {} Index: modules/png/png.test ================================================================== --- modules/png/png.test +++ modules/png/png.test @@ -299,8 +299,23 @@ set res [list [::png::validate $copy] [::png::getComments $copy]] removeFile pngrc.$root set res } {OK {}} } + +# ------------------------------------------------------------------------- +# PhysicalDimensions + +test png-physical-dimensions-missing {Test for empty list if no physical dimensions present} { + ::png::getPixelDimension [file join\ + [file dirname [file join [pwd] [info script]]]\ + testimages/physicaldimensions/basi0g01.png] +} {} + +test png-physical-dimensions-missing {Test for empty list if no physical dimensions present} { + ::png::getPixelDimension [file join\ + [file dirname [file join [pwd] [info script]]]\ + testimages/physicaldimensions/basi0g01_300dpi.png] +} {ppux 11811 ppuy 11811 unit meter} # ------------------------------------------------------------------------- testsuiteCleanup ADDED modules/png/testimages/physicaldimensions/basi0g01.png Index: modules/png/testimages/physicaldimensions/basi0g01.png ================================================================== --- /dev/null +++ modules/png/testimages/physicaldimensions/basi0g01.png cannot compute difference between binary files ADDED modules/png/testimages/physicaldimensions/basi0g01_300dpi.png Index: modules/png/testimages/physicaldimensions/basi0g01_300dpi.png ================================================================== --- /dev/null +++ modules/png/testimages/physicaldimensions/basi0g01_300dpi.png cannot compute difference between binary files