/usr/share/tcltk/tcllib1.14/png/png.tcl is in tcllib 1.14-dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | # png.tcl --
#
# Querying and modifying PNG image files.
#
# Copyright (c) 2004 Aaron Faupell <afaupell@users.sourceforge.net>
#
# 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.10 2007/08/20 22:06:58 andreas_kupries Exp $
package provide png 0.1.2
namespace eval ::png {}
proc ::png::_openPNG {file {mode r}} {
set fh [open $file $mode]
fconfigure $fh -encoding binary -translation binary -eofchar {}
if {[read $fh 8] != "\x89PNG\r\n\x1a\n"} { close $fh; return -code error "not a png file" }
return $fh
}
proc ::png::isPNG {file} {
if {[catch {_openPNG $file} fh]} { return 0 }
close $fh
return 1
}
proc ::png::validate {file} {
package require crc32
if {[catch {_openPNG $file} fh]} { return SIG }
set num 0
set idat 0
set last {}
while {[set r [read $fh 8]] != ""} {
binary scan $r Ia4 len type
if {$len < 0} { close $fh; return BADLEN }
set r [read $fh $len]
binary scan [read $fh 4] I crc
if {$crc < 0} {set crc [format %u [expr {$crc & 0xffffffff}]]}
if {[eof $fh]} { close $fh; return EOF }
if {($num == 0) && ($type != "IHDR")} { close $fh; return NOHDR }
if {$type == "IDAT"} { set idat 1 }
if {[::crc::crc32 $type$r] != $crc} { close $fh; return CKSUM }
set last $type
incr num
}
close $fh
if {!$idat} { return NODATA }
if {$last != "IEND"} { return NOEND }
return OK
}
proc ::png::imageInfo {file} {
set fh [_openPNG $file]
binary scan [read $fh 8] Ia4 len type
set r [read $fh $len]
if {![eof $fh] && $type == "IHDR"} {
binary scan $r IIccccc width height depth color compression filter interlace
binary scan [read $fh 4] I check
if {$check < 0} {set check [format %u [expr {$check & 0xffffffff}]]}
if {[::crc::crc32 IHDR$r] != $check} {
return -code error "header checksum failed"
}
close $fh
return [list width $width height $height depth $depth color $color \
compression $compression filter $filter interlace $interlace]
}
close $fh
return
}
proc ::png::getTimestamp {file} {
set fh [_openPNG $file]
while {[set r [read $fh 8]] != ""} {
binary scan $r Ia4 len type
if {$type == "tIME"} {
set r [read $fh [expr {$len + 4}]]
binary scan $r Sccccc year month day hour minute second
close $fh
return [clock scan "$month/$day/$year $hour:$minute:$second" -gmt 1]
}
seek $fh [expr {$len + 4}] current
}
close $fh
return
}
proc ::png::setTimestamp {file time} {
set fh [_openPNG $file r+]
set time [eval binary format Sccccc [string map {" 0" " "} [clock format $time -format "%Y %m %d %H %M %S" -gmt 1]]]
if {![catch {package present crc32}]} {
append time [binary format I [::crc::crc32 tIME$time]]
} else {
append time [binary format I 0]
}
while {[set r [read $fh 8]] != ""} {
binary scan $r Ia4 len type
if {[eof $fh]} { close $fh; return }
if {$type == "tIME"} {
seek $fh 0 current
puts -nonewline $fh $time
close $fh
return
}
if {$type == "IDAT" && ![info exists idat]} { set idat [expr {[tell $fh] - 8}] }
seek $fh [expr {$len + 4}] current
}
if {![info exists idat]} { close $fh; return -code error "no timestamp or data chunk found" }
seek $fh $idat start
set data [read $fh]
seek $fh $idat start
puts -nonewline $fh [binary format I 7]tIME$time$data
close $fh
return
}
proc ::png::getComments {file} {
set fh [_openPNG $file]
set text {}
while {[set r [read $fh 8]] != ""} {
binary scan $r Ia4 len type
set pos [tell $fh]
if {$type == "tEXt"} {
set r [read $fh $len]
lappend text [split $r \x00]
} elseif {$type == "iTXt"} {
set r [read $fh $len]
set keyword [lindex [split $r \x00] 0]
set r [string range $r [expr {[string length $keyword] + 1}] end]
binary scan $r cc comp method
if {$comp == 0} {
lappend text [linsert [split [string range $r 2 end] \x00] 0 $keyword]
}
}
seek $fh [expr {$pos + $len + 4}] start
}
close $fh
return $text
}
proc ::png::removeComments {file} {
set fh [_openPNG $file r+]
set data "\x89PNG\r\n\x1a\n"
while {[set r [read $fh 8]] != ""} {
binary scan $r Ia4 len type
if {$type == "zTXt" || $type == "iTXt" || $type == "tEXt"} {
seek $fh [expr {$len + 4}] current
} else {
seek $fh -8 current
append data [read $fh [expr {$len + 12}]]
}
}
close $fh
set fh [open $file w]
fconfigure $fh -encoding binary -translation binary -eofchar {}
puts -nonewline $fh $data
close $fh
}
proc ::png::addComment {file keyword arg1 args} {
if {[llength $args] > 0 && [llength $args] != 2} { close $fh; return -code error "wrong number of arguments" }
set fh [_openPNG $file r+]
if {[llength $args] > 0} {
set comment "iTXt$keyword\x00\x00\x00$arg1\x00[encoding convertto utf-8 [lindex $args 0]]\x00[encoding convertto utf-8 [lindex $args 1]]"
} else {
set comment "tEXt$keyword\x00$arg1"
}
if {![catch {package present crc32}]} {
append comment [binary format I [::crc::crc32 $comment]]
} else {
append comment [binary format I 0]
}
while {[set r [read $fh 8]] != ""} {
binary scan $r Ia4 len type
if {$type == "IDAT"} {
seek $fh -8 current
set pos [tell $fh]
set data [read $fh]
seek $fh $pos start
set 1 [tell $fh]
puts -nonewline $fh $comment
set clen [binary format I [expr {[tell $fh] - $1 - 8}]]
seek $fh $pos start
puts -nonewline $fh $clen$comment$data
close $fh
return
}
seek $fh [expr {$len + 4}] current
}
close $fh
return -code error "no data chunk found"
}
|