/usr/lib/ruby/2.0.0/tk/image.rb is in ruby2.0-tcltk 2.0.0.484-1ubuntu2.
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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | #
# tk/image.rb : treat Tk image objects
#
require 'tk'
class TkImage<TkObject
include Tk
TkCommandNames = ['image'.freeze].freeze
Tk_IMGTBL = TkCore::INTERP.create_table
(Tk_Image_ID = ['i'.freeze, TkUtil.untrust('00000')]).instance_eval{
@mutex = Mutex.new
def mutex; @mutex; end
freeze
}
TkCore::INTERP.init_ip_env{
Tk_IMGTBL.mutex.synchronize{ Tk_IMGTBL.clear }
}
def self.new(keys=nil)
if keys.kind_of?(Hash)
name = nil
if keys.key?(:imagename)
name = keys[:imagename]
elsif keys.key?('imagename')
name = keys['imagename']
end
if name
if name.kind_of?(TkImage)
obj = name
else
name = _get_eval_string(name)
obj = nil
Tk_IMGTBL.mutex.synchronize{
obj = Tk_IMGTBL[name]
}
end
if obj
if !(keys[:without_creating] || keys['without_creating'])
keys = _symbolkey2str(keys)
keys.delete('imagename')
keys.delete('without_creating')
obj.instance_eval{
tk_call_without_enc('image', 'create',
@type, @path, *hash_kv(keys, true))
}
end
return obj
end
end
end
(obj = self.allocate).instance_eval{
Tk_IMGTBL.mutex.synchronize{
initialize(keys)
Tk_IMGTBL[@path] = self
}
}
obj
end
def initialize(keys=nil)
@path = nil
without_creating = false
if keys.kind_of?(Hash)
keys = _symbolkey2str(keys)
@path = keys.delete('imagename')
without_creating = keys.delete('without_creating')
end
unless @path
Tk_Image_ID.mutex.synchronize{
@path = Tk_Image_ID.join(TkCore::INTERP._ip_id_)
Tk_Image_ID[1].succ!
}
end
unless without_creating
tk_call_without_enc('image', 'create',
@type, @path, *hash_kv(keys, true))
end
end
def delete
Tk_IMGTBL.mutex.synchronize{
Tk_IMGTBL.delete(@id) if @id
}
tk_call_without_enc('image', 'delete', @path)
self
end
def height
number(tk_call_without_enc('image', 'height', @path))
end
def inuse
bool(tk_call_without_enc('image', 'inuse', @path))
end
def itemtype
tk_call_without_enc('image', 'type', @path)
end
def width
number(tk_call_without_enc('image', 'width', @path))
end
def TkImage.names
Tk_IMGTBL.mutex.synchronize{
Tk.tk_call_without_enc('image', 'names').split.collect!{|id|
(Tk_IMGTBL[id])? Tk_IMGTBL[id] : id
}
}
end
def TkImage.types
Tk.tk_call_without_enc('image', 'types').split
end
end
class TkBitmapImage<TkImage
def __strval_optkeys
super() + ['maskdata', 'maskfile']
end
private :__strval_optkeys
def initialize(*args)
@type = 'bitmap'
super(*args)
end
end
# A photo is an image whose pixels can display any color or be transparent.
# At present, only GIF and PPM/PGM formats are supported, but an interface
# exists to allow additional image file formats to be added easily.
#
# This class documentation is a copy from the original Tcl/Tk at
# http://www.tcl.tk/man/tcl8.5/TkCmd/photo.htm with some rewrited parts.
class TkPhotoImage<TkImage
NullArgOptionKeys = [ "shrink", "grayscale" ]
def _photo_hash_kv(keys)
keys = _symbolkey2str(keys)
NullArgOptionKeys.collect{|opt|
if keys[opt]
keys[opt] = None
else
keys.delete(opt)
end
}
keys.collect{|k,v|
['-' << k, v]
}.flatten
end
private :_photo_hash_kv
# Create a new image with the given options.
# == Examples of use :
# === Create an empty image of 300x200 pixels
#
# image = TkPhotoImage.new(:height => 200, :width => 300)
#
# === Create an image from a file
#
# image = TkPhotoImage.new(:file: => 'my_image.gif')
#
# == Options
# Photos support the following options:
# * :data
# Specifies the contents of the image as a string.
# * :format
# Specifies the name of the file format for the data.
# * :file
# Gives the name of a file that is to be read to supply data for the image.
# * :gamma
# Specifies that the colors allocated for displaying this image in a window
# should be corrected for a non-linear display with the specified gamma
# exponent value.
# * height
# Specifies the height of the image, in pixels. This option is useful
# primarily in situations where the user wishes to build up the contents of
# the image piece by piece. A value of zero (the default) allows the image
# to expand or shrink vertically to fit the data stored in it.
# * palette
# Specifies the resolution of the color cube to be allocated for displaying
# this image.
# * width
# Specifies the width of the image, in pixels. This option is useful
# primarily in situations where the user wishes to build up the contents of
# the image piece by piece. A value of zero (the default) allows the image
# to expand or shrink horizontally to fit the data stored in it.
def initialize(*args)
@type = 'photo'
super(*args)
end
# Blank the image; that is, set the entire image to have no data, so it will
# be displayed as transparent, and the background of whatever window it is
# displayed in will show through.
def blank
tk_send_without_enc('blank')
self
end
def cget_strict(option)
case option.to_s
when 'data', 'file'
tk_send 'cget', '-' << option.to_s
else
tk_tcl2ruby(tk_send('cget', '-' << option.to_s))
end
end
# Returns the current value of the configuration option given by option.
# Example, display name of the file from which <tt>image</tt> was created:
# puts image.cget :file
def cget(option)
unless TkConfigMethod.__IGNORE_UNKNOWN_CONFIGURE_OPTION__
cget_strict(option)
else
begin
cget_strict(option)
rescue => e
if current_configinfo.has_key?(option.to_s)
# error on known option
fail e
else
# unknown option
nil
end
end
end
end
# Copies a region from the image called source to the image called
# destination, possibly with pixel zooming and/or subsampling. If no options
# are specified, this method copies the whole of source into destination,
# starting at coordinates (0,0) in destination. The following options may be
# specified:
#
# * :from [x1, y1, x2, y2]
# Specifies a rectangular sub-region of the source image to be copied.
# (x1,y1) and (x2,y2) specify diagonally opposite corners of the rectangle.
# If x2 and y2 are not specified, the default value is the bottom-right
# corner of the source image. The pixels copied will include the left and
# top edges of the specified rectangle but not the bottom or right edges.
# If the :from option is not given, the default is the whole source image.
# * :to [x1, y1, x2, y2]
# Specifies a rectangular sub-region of the destination image to be
# affected. (x1,y1) and (x2,y2) specify diagonally opposite corners of the
# rectangle. If x2 and y2 are not specified, the default value is (x1,y1)
# plus the size of the source region (after subsampling and zooming, if
# specified). If x2 and y2 are specified, the source region will be
# replicated if necessary to fill the destination region in a tiled fashion.
# * :shrink
# Specifies that the size of the destination image should be reduced, if
# necessary, so that the region being copied into is at the bottom-right
# corner of the image. This option will not affect the width or height of
# the image if the user has specified a non-zero value for the :width or
# :height configuration option, respectively.
# * :zoom [x, y]
# Specifies that the source region should be magnified by a factor of x
# in the X direction and y in the Y direction. If y is not given, the
# default value is the same as x. With this option, each pixel in the
# source image will be expanded into a block of x x y pixels in the
# destination image, all the same color. x and y must be greater than 0.
# * :subsample [x, y]
# Specifies that the source image should be reduced in size by using only
# every xth pixel in the X direction and yth pixel in the Y direction.
# Negative values will cause the image to be flipped about the Y or X axes,
# respectively. If y is not given, the default value is the same as x.
# * :compositingrule rule
# Specifies how transparent pixels in the source image are combined with
# the destination image. When a compositing rule of <tt>overlay</tt> is set,
# the old contents of the destination image are visible, as if the source
# image were printed on a piece of transparent film and placed over the
# top of the destination. When a compositing rule of <tt>set</tt> is set,
# the old contents of the destination image are discarded and the source
# image is used as-is. The default compositing rule is <tt>overlay</tt>.
def copy(src, *opts)
if opts.size == 0
tk_send('copy', src)
elsif opts.size == 1 && opts[0].kind_of?(Hash)
tk_send('copy', src, *_photo_hash_kv(opts[0]))
else
# for backward compatibility
args = opts.collect{|term|
if term.kind_of?(String) && term.include?(?\s)
term.split
else
term
end
}.flatten
tk_send('copy', src, *args)
end
self
end
# Returns image data in the form of a string. The following options may be
# specified:
# * :background color
# If the color is specified, the data will not contain any transparency
# information. In all transparent pixels the color will be replaced by the
# specified color.
# * :format format-name
# Specifies the name of the image file format handler to be used.
# Specifically, this subcommand searches for the first handler whose name
# matches an initial substring of format-name and which has the capability
# to read this image data. If this option is not given, this subcommand
# uses the first handler that has the capability to read the image data.
# * :from [x1, y1, x2, y2]
# Specifies a rectangular region of imageName to be returned. If only x1
# and y1 are specified, the region extends from (x1,y1) to the bottom-right
# corner of imageName. If all four coordinates are given, they specify
# diagonally opposite corners of the rectangular region, including x1,y1
# and excluding x2,y2. The default, if this option is not given, is the
# whole image.
# * :grayscale
# If this options is specified, the data will not contain color information.
# All pixel data will be transformed into grayscale.
def data(keys={})
tk_split_list(tk_send('data', *_photo_hash_kv(keys)))
end
# Returns the color of the pixel at coordinates (x,y) in the image as a list
# of three integers between 0 and 255, representing the red, green and blue
# components respectively.
def get(x, y)
tk_send('get', x, y).split.collect{|n| n.to_i}
end
def put(data, *opts)
if opts.empty?
tk_send('put', data)
elsif opts.size == 1 && opts[0].kind_of?(Hash)
tk_send('put', data, *_photo_hash_kv(opts[0]))
else
# for backward compatibility
tk_send('put', data, '-to', *opts)
end
self
end
def read(file, *opts)
if opts.size == 0
tk_send('read', file)
elsif opts.size == 1 && opts[0].kind_of?(Hash)
tk_send('read', file, *_photo_hash_kv(opts[0]))
else
# for backward compatibility
args = opts.collect{|term|
if term.kind_of?(String) && term.include?(?\s)
term.split
else
term
end
}.flatten
tk_send('read', file, *args)
end
self
end
def redither
tk_send 'redither'
self
end
# Returns a boolean indicating if the pixel at (x,y) is transparent.
def get_transparency(x, y)
bool(tk_send('transparency', 'get', x, y))
end
# Makes the pixel at (x,y) transparent if <tt>state</tt> is true, and makes
# that pixel opaque otherwise.
def set_transparency(x, y, state)
tk_send('transparency', 'set', x, y, state)
self
end
def write(file, *opts)
if opts.size == 0
tk_send('write', file)
elsif opts.size == 1 && opts[0].kind_of?(Hash)
tk_send('write', file, *_photo_hash_kv(opts[0]))
else
# for backward compatibility
args = opts.collect{|term|
if term.kind_of?(String) && term.include?(?\s)
term.split
else
term
end
}.flatten
tk_send('write', file, *args)
end
self
end
end
|