class Image
class Image
lib/image/image.tya:5
Image provides the image/Image standard library API.
Source
# Image provides the image/Image standard library API.
class Image
# Image.format stores instance state.
# @type Nil
format: nil
# Image.height stores instance state.
# @type Nil
height: nil
# Image.pixels stores instance state.
# @type Array
pixels: []
# Image.width stores instance state.
# @type Nil
width: nil
# Image.initialize provides the image/Image standard library operation.
# @param width Int width value.
# @param height Int height value.
# @param fill Any fill value.
# @return Self the initialized object.
initialize: width = 1, height = 1, fill = nil ->
self.check_dimensions(width, height)
if fill == nil
fill = color.Color(0, 0, 0, 0)
self.check_color(fill)
self.width = width
self.height = height
self.format = nil
self.pixels = []
i = 0
while i < width * height
self.pixels.push(fill.r)
self.pixels.push(fill.g)
self.pixels.push(fill.b)
self.pixels.push(fill.a)
i = i + 1
# Image.bytes provides the image/Image standard library operation.
# @return Any the resulting value.
bytes: ->
self.to_bytes()
# Image.check_channel provides the image/Image standard library operation.
# @param value String value value.
# @return Any the resulting value.
check_channel: value ->
if (
value.class != Number or value < 0 or value > 255 or value != value.to_i()
)
raise error("image: channel out of range")
# Image.check_color provides the image/Image standard library operation.
# @param value String value value.
# @return Any the resulting value.
check_color: value ->
if value.class != color.Color
raise error("image: expected color.Color")
# Image.check_dimensions provides the image/Image standard library operation.
# @param width Int width value.
# @param height Int height value.
# @return Any the resulting value.
check_dimensions: width, height ->
if (
width.class != Number or height.class != Number or width != width.to_i() or height != height.to_i()
)
raise error("image: dimensions must be integers")
if width <= 0 or height <= 0 or width * height > 10000000
raise error("image: invalid dimensions")
# Image.check_encode_options provides the image/Image standard library operation.
# @param format String format value.
# @param options Dict options value.
# @return Dict the resulting value.
check_encode_options: format, options ->
if options == nil
return nil
keys = options.keys()
i = 0
while i < keys.len()
key = keys[i]
if (
key != "format" and key != "compression" and key != "quality" and key != "background"
)
raise error("image.encode: unknown option " + key)
i = i + 1
if format == "jpeg" and options.has?("quality")
q = options["quality"]
if q.class != Number or q < 1 or q > 100
raise error("image.encode: jpeg quality out of range")
# Image.check_xy provides the image/Image standard library operation.
# @param x Int x value.
# @param y Int y value.
# @return Any the resulting value.
check_xy: x, y ->
if x.class != Number or y.class != Number or x != x.to_i() or y != y.to_i()
raise error("image: coordinates must be integers")
if x < 0 or y < 0 or x >= self.width or y >= self.height
raise error("image: pixel coordinate out of bounds")
# Image.composite provides the image/Image standard library operation.
# @param over Any over value.
# @param x Int x value.
# @param y Int y value.
# @return Any the resulting value.
composite: over, x, y ->
out = self.from_pixels(self.width, self.height, self.format, self.pixels)
yy = 0
while yy < over.height
xx = 0
while xx < over.width
dx = x + xx
dy = y + yy
if dx >= 0 and dy >= 0 and dx < out.width and dy < out.height
under = out.pixel(dx, dy)
top = over.pixel(xx, yy)
a = top.a / 255.0
inv = 1 - a
r = (top.r * a + under.r * inv).to_i()
g = (top.g * a + under.g * inv).to_i()
b = (top.b * a + under.b * inv).to_i()
out.set_pixel(dx, dy, color.Color(r, g, b, 255))
xx = xx + 1
yy = yy + 1
out
# Image.crop provides the image/Image standard library operation.
# @param x Int x value.
# @param y Int y value.
# @param width Int width value.
# @param height Int height value.
# @return Any the resulting value.
crop: x, y, width, height ->
self.check_xy(x, y)
self.check_dimensions(width, height)
if x + width > self.width or y + height > self.height
raise error("image.crop: rectangle out of bounds")
out = Image(width, height, nil)
yy = 0
while yy < height
xx = 0
while xx < width
out.set_pixel(xx, yy, self.pixel(x + xx, y + yy))
xx = xx + 1
yy = yy + 1
out
# Image.decode provides the image/Image standard library operation.
# @param data Array data value.
# @param options Dict options value.
# @return String the resulting value.
decode: data, options ->
if options != nil and options.has?("frame") and options["frame"] != 0
raise error("image.decode: frame out of range")
meta = Codec(nil).metadata(data)
text = bytes_text(data)
lines = text.split("\n")
if lines.len() < 2
raise error("image.decode: malformed " + meta["format"])
values = lines[1].split(",")
expected = meta["width"] * meta["height"] * 4
if values.len() != expected
raise error("image.decode: malformed " + meta["format"] + " pixel data")
img = Image(meta["width"], meta["height"], nil)
img.format = meta["format"]
img.pixels = []
i = 0
while i < values.len()
channel = values[i].to_i()
self.check_channel(channel)
img.pixels.push(channel)
i = i + 1
img
# Image.decode_frames provides the image/Image standard library operation.
# @param data Array data value.
# @return Any the resulting value.
decode_frames: data ->
[self.decode(data, { frame: 0 })]
# Image.encodable_format? provides the image/Image standard library operation.
# @param format String format value.
# @return Boolean whether the condition is true.
encodable_format?: format ->
format == "png" or format == "jpeg" or format == "bmp" or format == "ppm"
# Image.encode provides the image/Image standard library operation.
# @param format String format value.
# @param options Dict options value.
# @return String the resulting value.
encode: format, options ->
if format == nil
format = self.format
if not self.encodable_format?(format)
raise error("image.encode: unsupported format " + format.to_s())
self.check_encode_options(format, options)
alpha = "0"
if self.has_alpha?()
alpha = "1"
header = "TYAIMG:"
+ format
+ ":"
+ self.width.to_s()
+ ":"
+ self.height.to_s()
+ ":"
+ alpha
+ ":1\n"
body = ""
i = 0
while i < self.pixels.len()
if i > 0
body = body + ","
body = body + self.pixels[i].to_s()
i = i + 1
bytes_of(header + body)
# Image.flip_horizontal provides the image/Image standard library operation.
# @return Any the resulting value.
flip_horizontal: ->
out = Image(self.width, self.height, nil)
y = 0
while y < self.height
x = 0
while x < self.width
out.set_pixel(self.width - x - 1, y, self.pixel(x, y))
x = x + 1
y = y + 1
out
# Image.flip_vertical provides the image/Image standard library operation.
# @return Any the resulting value.
flip_vertical: ->
out = Image(self.width, self.height, nil)
y = 0
while y < self.height
x = 0
while x < self.width
out.set_pixel(x, self.height - y - 1, self.pixel(x, y))
x = x + 1
y = y + 1
out
# Image.format_from_path provides the image/Image standard library operation.
# @param path String path value.
# @return String the resulting value.
format_from_path: path ->
lower = path.lower()
if lower.ends_with(".png")
return "png"
if lower.ends_with(".jpg") or lower.ends_with(".jpeg")
return "jpeg"
if lower.ends_with(".gif")
return "gif"
if lower.ends_with(".bmp")
return "bmp"
if lower.ends_with(".ppm")
return "ppm"
raise error("image: unknown image format")
# Image.from_pixels provides the image/Image standard library operation.
# @param width Int width value.
# @param height Int height value.
# @param format String format value.
# @param pixels Any pixels value.
# @return Self the resulting value.
from_pixels: width, height, format, pixels ->
out = Image(width, height, nil)
out.format = format
out.pixels = []
i = 0
while i < pixels.len()
out.pixels.push(pixels[i])
i = i + 1
out
# Image.grayscale provides the image/Image standard library operation.
# @return Any the resulting value.
grayscale: ->
out = Image(self.width, self.height, nil)
y = 0
while y < self.height
x = 0
while x < self.width
c = self.pixel(x, y)
g = (c.r * 30 + c.g * 59 + c.b * 11) / 100.to_i()
out.set_pixel(x, y, color.Color(g, g, g, c.a))
x = x + 1
y = y + 1
out
# Image.has_alpha? provides the image/Image standard library operation.
# @return Boolean whether the condition is true.
has_alpha?: ->
i = 3
while i < self.pixels.len()
if self.pixels[i] < 255
return true
i = i + 4
false
# Image.new provides the image/Image standard library operation.
# @param width Int width value.
# @param height Int height value.
# @param fill Any fill value.
# @return Self the resulting value.
new: width, height, fill ->
Image(width, height, fill)
# Image.pixel provides the image/Image standard library operation.
# @param x Int x value.
# @param y Int y value.
# @return Any the resulting value.
pixel: x, y ->
self.check_xy(x, y)
i = (y * self.width + x) * 4
color.Color(
self.pixels[i],
self.pixels[i + 1],
self.pixels[i + 2],
self.pixels[i + 3]
)
# Image.read provides the image/Image standard library operation.
# @param path String path value.
# @return Any the resulting value.
read: path ->
img = self.decode(file.File(path).read_bytes(), nil)
img.format = self.format_from_path(path)
img
# Image.resize provides the image/Image standard library operation.
# @param width Int width value.
# @param height Int height value.
# @param options Dict options value.
# @return Any the resulting value.
resize: width, height, options ->
self.check_dimensions(width, height)
opts = self.resize_options(options)
if opts["fit"] == "contain"
return self.resize_contain(width, height, opts)
if opts["fit"] == "cover"
return self.resize_cover(width, height, opts)
self.resize_stretch(width, height)
# Image.resize_contain provides the image/Image standard library operation.
# @param width Int width value.
# @param height Int height value.
# @param opts Any opts value.
# @return Any the resulting value.
resize_contain: width, height, opts ->
scale_w = width.to_f() / self.width
scale_h = height.to_f() / self.height
scale = scale_w
if scale_h < scale_w
scale = scale_h
new_w = (self.width * scale).to_i()
new_h = (self.height * scale).to_i()
if new_w < 1
new_w = 1
if new_h < 1
new_h = 1
resized = self.resize_stretch(new_w, new_h)
out = Image(width, height, opts["background"])
ox = ((width - new_w) / 2.0).to_i()
oy = ((height - new_h) / 2.0).to_i()
out.composite(resized, ox, oy)
# Image.resize_cover provides the image/Image standard library operation.
# @param width Int width value.
# @param height Int height value.
# @param opts Any opts value.
# @return Any the resulting value.
resize_cover: width, height, opts ->
if opts["filter"] == "bilinear"
nil
scale_w = width.to_f() / self.width
scale_h = height.to_f() / self.height
scale = scale_w
if scale_h > scale_w
scale = scale_h
new_w = (self.width * scale).to_i()
new_h = (self.height * scale).to_i()
resized = self.resize_stretch(new_w, new_h)
ox = ((new_w - width) / 2.0).to_i()
oy = ((new_h - height) / 2.0).to_i()
resized.crop(ox, oy, width, height)
# Image.resize_options provides the image/Image standard library operation.
# @param options Dict options value.
# @return Dict the resulting value.
resize_options: options ->
opts =
filter: "nearest"
fit: "stretch"
background: color.Color(0, 0, 0, 0)
if options == nil
return opts
keys = options.keys()
i = 0
while i < keys.len()
key = keys[i]
if key != "filter" and key != "fit" and key != "background"
raise error("image.resize: unknown option " + key)
opts[key] = options[key]
i = i + 1
if opts["filter"] != "nearest" and opts["filter"] != "bilinear"
raise error("image.resize: unsupported filter")
if (
opts["fit"] != "stretch" and opts["fit"] != "contain" and opts["fit"] != "cover"
)
raise error("image.resize: unsupported fit")
self.check_color(opts["background"])
opts
# Image.resize_stretch provides the image/Image standard library operation.
# @param width Int width value.
# @param height Int height value.
# @return Any the resulting value.
resize_stretch: width, height ->
out = Image(width, height, nil)
y = 0
while y < height
x = 0
while x < width
sx = ((x * self.width) / width.to_f()).to_i()
sy = ((y * self.height) / height.to_f()).to_i()
out.set_pixel(x, y, self.pixel(sx, sy))
x = x + 1
y = y + 1
out
# Image.rotate90 provides the image/Image standard library operation.
# @return Any the resulting value.
rotate90: ->
out = Image(self.height, self.width, nil)
y = 0
while y < self.height
x = 0
while x < self.width
out.set_pixel(self.height - y - 1, x, self.pixel(x, y))
x = x + 1
y = y + 1
out
# Image.set_pixel provides the image/Image standard library operation.
# @param x Int x value.
# @param y Int y value.
# @param value String value value.
# @return Any the resulting value.
set_pixel: x, y, value ->
self.check_xy(x, y)
self.check_color(value)
i = (y * self.width + x) * 4
self.pixels[i] = value.r
self.pixels[i + 1] = value.g
self.pixels[i + 2] = value.b
self.pixels[i + 3] = value.a
nil
# Image.to_bytes provides the image/Image standard library operation.
# @return Any the resulting value.
to_bytes: ->
bytes(self.pixels)
# Image.write provides the image/Image standard library operation.
# @param path String path value.
# @param options Dict options value.
# @return Any the resulting value.
write: path, options ->
file.File(path).write_bytes(
self.encode(self.write_format(path, options), options)
)
nil
# Image.write_format provides the image/Image standard library operation.
# @param path String path value.
# @param options Dict options value.
# @return Any the resulting value.
write_format: path, options ->
if options != nil and options.has?("format")
return options["format"]
self.format_from_path(path)
Instance Variables
format
Image.format
lib/image/image.tya:8
Image.format stores instance state.
Source
# Image.format stores instance state.
# @type Nil
format: nil
height
Image.height
lib/image/image.tya:12
Image.height stores instance state.
Source
# Image.height stores instance state.
# @type Nil
height: nil
pixels
Image.pixels
lib/image/image.tya:16
Image.pixels stores instance state.
Source
# Image.pixels stores instance state.
# @type Array
pixels: []
width
Image.width
lib/image/image.tya:20
Image.width stores instance state.
Source
# Image.width stores instance state.
# @type Nil
width: nil
Methods
bytes
Image.bytes()
lib/image/image.tya:46
Image.bytes provides the image/Image standard library operation.
Source
# Image.bytes provides the image/Image standard library operation.
# @return Any the resulting value.
bytes: ->
self.to_bytes()
check_channel
Image.check_channel(value)
lib/image/image.tya:52
Image.check_channel provides the image/Image standard library operation.
Source
# Image.check_channel provides the image/Image standard library operation.
# @param value String value value.
# @return Any the resulting value.
check_channel: value ->
if (
value.class != Number or value < 0 or value > 255 or value != value.to_i()
)
raise error("image: channel out of range")
check_color
Image.check_color(value)
lib/image/image.tya:61
Image.check_color provides the image/Image standard library operation.
Source
# Image.check_color provides the image/Image standard library operation.
# @param value String value value.
# @return Any the resulting value.
check_color: value ->
if value.class != color.Color
raise error("image: expected color.Color")
check_dimensions
Image.check_dimensions(width, height)
lib/image/image.tya:69
Image.check_dimensions provides the image/Image standard library operation.
Source
# Image.check_dimensions provides the image/Image standard library operation.
# @param width Int width value.
# @param height Int height value.
# @return Any the resulting value.
check_dimensions: width, height ->
if (
width.class != Number or height.class != Number or width != width.to_i() or height != height.to_i()
)
raise error("image: dimensions must be integers")
if width <= 0 or height <= 0 or width * height > 10000000
raise error("image: invalid dimensions")
check_encode_options
Image.check_encode_options(format, options)
lib/image/image.tya:81
Image.check_encode_options provides the image/Image standard library operation.
Source
# Image.check_encode_options provides the image/Image standard library operation.
# @param format String format value.
# @param options Dict options value.
# @return Dict the resulting value.
check_encode_options: format, options ->
if options == nil
return nil
keys = options.keys()
i = 0
while i < keys.len()
key = keys[i]
if (
key != "format" and key != "compression" and key != "quality" and key != "background"
)
raise error("image.encode: unknown option " + key)
i = i + 1
if format == "jpeg" and options.has?("quality")
q = options["quality"]
if q.class != Number or q < 1 or q > 100
raise error("image.encode: jpeg quality out of range")
check_xy
Image.check_xy(x, y)
lib/image/image.tya:102
Image.check_xy provides the image/Image standard library operation.
Source
# Image.check_xy provides the image/Image standard library operation.
# @param x Int x value.
# @param y Int y value.
# @return Any the resulting value.
check_xy: x, y ->
if x.class != Number or y.class != Number or x != x.to_i() or y != y.to_i()
raise error("image: coordinates must be integers")
if x < 0 or y < 0 or x >= self.width or y >= self.height
raise error("image: pixel coordinate out of bounds")
composite
Image.composite(over, x, y)
lib/image/image.tya:113
Image.composite provides the image/Image standard library operation.
Source
# Image.composite provides the image/Image standard library operation.
# @param over Any over value.
# @param x Int x value.
# @param y Int y value.
# @return Any the resulting value.
composite: over, x, y ->
out = self.from_pixels(self.width, self.height, self.format, self.pixels)
yy = 0
while yy < over.height
xx = 0
while xx < over.width
dx = x + xx
dy = y + yy
if dx >= 0 and dy >= 0 and dx < out.width and dy < out.height
under = out.pixel(dx, dy)
top = over.pixel(xx, yy)
a = top.a / 255.0
inv = 1 - a
r = (top.r * a + under.r * inv).to_i()
g = (top.g * a + under.g * inv).to_i()
b = (top.b * a + under.b * inv).to_i()
out.set_pixel(dx, dy, color.Color(r, g, b, 255))
xx = xx + 1
yy = yy + 1
out
crop
Image.crop(x, y, width, height)
lib/image/image.tya:140
Image.crop provides the image/Image standard library operation.
Source
# Image.crop provides the image/Image standard library operation.
# @param x Int x value.
# @param y Int y value.
# @param width Int width value.
# @param height Int height value.
# @return Any the resulting value.
crop: x, y, width, height ->
self.check_xy(x, y)
self.check_dimensions(width, height)
if x + width > self.width or y + height > self.height
raise error("image.crop: rectangle out of bounds")
out = Image(width, height, nil)
yy = 0
while yy < height
xx = 0
while xx < width
out.set_pixel(xx, yy, self.pixel(x + xx, y + yy))
xx = xx + 1
yy = yy + 1
out
decode
Image.decode(data, options)
lib/image/image.tya:159
Image.decode provides the image/Image standard library operation.
Source
# Image.decode provides the image/Image standard library operation.
# @param data Array data value.
# @param options Dict options value.
# @return String the resulting value.
decode: data, options ->
if options != nil and options.has?("frame") and options["frame"] != 0
raise error("image.decode: frame out of range")
meta = Codec(nil).metadata(data)
text = bytes_text(data)
lines = text.split("\n")
if lines.len() < 2
raise error("image.decode: malformed " + meta["format"])
values = lines[1].split(",")
expected = meta["width"] * meta["height"] * 4
if values.len() != expected
raise error("image.decode: malformed " + meta["format"] + " pixel data")
img = Image(meta["width"], meta["height"], nil)
img.format = meta["format"]
img.pixels = []
i = 0
while i < values.len()
channel = values[i].to_i()
self.check_channel(channel)
img.pixels.push(channel)
i = i + 1
img
decode_frames
Image.decode_frames(data)
lib/image/image.tya:185
Image.decode_frames provides the image/Image standard library operation.
Source
# Image.decode_frames provides the image/Image standard library operation.
# @param data Array data value.
# @return Any the resulting value.
decode_frames: data ->
[self.decode(data, { frame: 0 })]
encodable_format?
Image.encodable_format?(format)
lib/image/image.tya:191
Image.encodable_format? provides the image/Image standard library operation.
Source
# Image.encodable_format? provides the image/Image standard library operation.
# @param format String format value.
# @return Boolean whether the condition is true.
encodable_format?: format ->
format == "png" or format == "jpeg" or format == "bmp" or format == "ppm"
encode
Image.encode(format, options)
lib/image/image.tya:198
Image.encode provides the image/Image standard library operation.
Source
# Image.encode provides the image/Image standard library operation.
# @param format String format value.
# @param options Dict options value.
# @return String the resulting value.
encode: format, options ->
if format == nil
format = self.format
if not self.encodable_format?(format)
raise error("image.encode: unsupported format " + format.to_s())
self.check_encode_options(format, options)
alpha = "0"
if self.has_alpha?()
alpha = "1"
header = "TYAIMG:"
+ format
+ ":"
+ self.width.to_s()
+ ":"
+ self.height.to_s()
+ ":"
+ alpha
+ ":1\n"
body = ""
i = 0
while i < self.pixels.len()
if i > 0
body = body + ","
body = body + self.pixels[i].to_s()
i = i + 1
bytes_of(header + body)
flip_horizontal
Image.flip_horizontal()
lib/image/image.tya:227
Image.flip_horizontal provides the image/Image standard library operation.
Source
# Image.flip_horizontal provides the image/Image standard library operation.
# @return Any the resulting value.
flip_horizontal: ->
out = Image(self.width, self.height, nil)
y = 0
while y < self.height
x = 0
while x < self.width
out.set_pixel(self.width - x - 1, y, self.pixel(x, y))
x = x + 1
y = y + 1
out
flip_vertical
Image.flip_vertical()
lib/image/image.tya:240
Image.flip_vertical provides the image/Image standard library operation.
Source
# Image.flip_vertical provides the image/Image standard library operation.
# @return Any the resulting value.
flip_vertical: ->
out = Image(self.width, self.height, nil)
y = 0
while y < self.height
x = 0
while x < self.width
out.set_pixel(x, self.height - y - 1, self.pixel(x, y))
x = x + 1
y = y + 1
out
format_from_path
Image.format_from_path(path)
lib/image/image.tya:254
Image.format_from_path provides the image/Image standard library operation.
Source
# Image.format_from_path provides the image/Image standard library operation.
# @param path String path value.
# @return String the resulting value.
format_from_path: path ->
lower = path.lower()
if lower.ends_with(".png")
return "png"
if lower.ends_with(".jpg") or lower.ends_with(".jpeg")
return "jpeg"
if lower.ends_with(".gif")
return "gif"
if lower.ends_with(".bmp")
return "bmp"
if lower.ends_with(".ppm")
return "ppm"
raise error("image: unknown image format")
from_pixels
Image.from_pixels(width, height, format, pixels)
lib/image/image.tya:274
Image.from_pixels provides the image/Image standard library operation.
Source
# Image.from_pixels provides the image/Image standard library operation.
# @param width Int width value.
# @param height Int height value.
# @param format String format value.
# @param pixels Any pixels value.
# @return Self the resulting value.
from_pixels: width, height, format, pixels ->
out = Image(width, height, nil)
out.format = format
out.pixels = []
i = 0
while i < pixels.len()
out.pixels.push(pixels[i])
i = i + 1
out
grayscale
Image.grayscale()
lib/image/image.tya:286
Image.grayscale provides the image/Image standard library operation.
Source
# Image.grayscale provides the image/Image standard library operation.
# @return Any the resulting value.
grayscale: ->
out = Image(self.width, self.height, nil)
y = 0
while y < self.height
x = 0
while x < self.width
c = self.pixel(x, y)
g = (c.r * 30 + c.g * 59 + c.b * 11) / 100.to_i()
out.set_pixel(x, y, color.Color(g, g, g, c.a))
x = x + 1
y = y + 1
out
has_alpha?
Image.has_alpha?()
lib/image/image.tya:301
Image.has_alpha? provides the image/Image standard library operation.
Source
# Image.has_alpha? provides the image/Image standard library operation.
# @return Boolean whether the condition is true.
has_alpha?: ->
i = 3
while i < self.pixels.len()
if self.pixels[i] < 255
return true
i = i + 4
false
initialize
Image.initialize(width = 1, height = 1, fill = nil)
lib/image/image.tya:27
Image.initialize provides the image/Image standard library operation.
Source
# Image.initialize provides the image/Image standard library operation.
# @param width Int width value.
# @param height Int height value.
# @param fill Any fill value.
# @return Self the initialized object.
initialize: width = 1, height = 1, fill = nil ->
self.check_dimensions(width, height)
if fill == nil
fill = color.Color(0, 0, 0, 0)
self.check_color(fill)
self.width = width
self.height = height
self.format = nil
self.pixels = []
i = 0
while i < width * height
self.pixels.push(fill.r)
self.pixels.push(fill.g)
self.pixels.push(fill.b)
self.pixels.push(fill.a)
i = i + 1
new
Image.new(width, height, fill)
lib/image/image.tya:314
Image.new provides the image/Image standard library operation.
Source
# Image.new provides the image/Image standard library operation.
# @param width Int width value.
# @param height Int height value.
# @param fill Any fill value.
# @return Self the resulting value.
new: width, height, fill ->
Image(width, height, fill)
pixel
Image.pixel(x, y)
lib/image/image.tya:321
Image.pixel provides the image/Image standard library operation.
Source
# Image.pixel provides the image/Image standard library operation.
# @param x Int x value.
# @param y Int y value.
# @return Any the resulting value.
pixel: x, y ->
self.check_xy(x, y)
i = (y * self.width + x) * 4
color.Color(
self.pixels[i],
self.pixels[i + 1],
self.pixels[i + 2],
self.pixels[i + 3]
)
read
Image.read(path)
lib/image/image.tya:334
Image.read provides the image/Image standard library operation.
Source
# Image.read provides the image/Image standard library operation.
# @param path String path value.
# @return Any the resulting value.
read: path ->
img = self.decode(file.File(path).read_bytes(), nil)
img.format = self.format_from_path(path)
img
resize
Image.resize(width, height, options)
lib/image/image.tya:344
Image.resize provides the image/Image standard library operation.
Source
# Image.resize provides the image/Image standard library operation.
# @param width Int width value.
# @param height Int height value.
# @param options Dict options value.
# @return Any the resulting value.
resize: width, height, options ->
self.check_dimensions(width, height)
opts = self.resize_options(options)
if opts["fit"] == "contain"
return self.resize_contain(width, height, opts)
if opts["fit"] == "cover"
return self.resize_cover(width, height, opts)
self.resize_stretch(width, height)
resize_contain
Image.resize_contain(width, height, opts)
lib/image/image.tya:358
Image.resize_contain provides the image/Image standard library operation.
Source
# Image.resize_contain provides the image/Image standard library operation.
# @param width Int width value.
# @param height Int height value.
# @param opts Any opts value.
# @return Any the resulting value.
resize_contain: width, height, opts ->
scale_w = width.to_f() / self.width
scale_h = height.to_f() / self.height
scale = scale_w
if scale_h < scale_w
scale = scale_h
new_w = (self.width * scale).to_i()
new_h = (self.height * scale).to_i()
if new_w < 1
new_w = 1
if new_h < 1
new_h = 1
resized = self.resize_stretch(new_w, new_h)
out = Image(width, height, opts["background"])
ox = ((width - new_w) / 2.0).to_i()
oy = ((height - new_h) / 2.0).to_i()
out.composite(resized, ox, oy)
resize_cover
Image.resize_cover(width, height, opts)
lib/image/image.tya:381
Image.resize_cover provides the image/Image standard library operation.
Source
# Image.resize_cover provides the image/Image standard library operation.
# @param width Int width value.
# @param height Int height value.
# @param opts Any opts value.
# @return Any the resulting value.
resize_cover: width, height, opts ->
if opts["filter"] == "bilinear"
nil
scale_w = width.to_f() / self.width
scale_h = height.to_f() / self.height
scale = scale_w
if scale_h > scale_w
scale = scale_h
new_w = (self.width * scale).to_i()
new_h = (self.height * scale).to_i()
resized = self.resize_stretch(new_w, new_h)
ox = ((new_w - width) / 2.0).to_i()
oy = ((new_h - height) / 2.0).to_i()
resized.crop(ox, oy, width, height)
resize_options
Image.resize_options(options)
lib/image/image.tya:399
Image.resize_options provides the image/Image standard library operation.
Source
# Image.resize_options provides the image/Image standard library operation.
# @param options Dict options value.
# @return Dict the resulting value.
resize_options: options ->
opts =
filter: "nearest"
fit: "stretch"
background: color.Color(0, 0, 0, 0)
if options == nil
return opts
keys = options.keys()
i = 0
while i < keys.len()
key = keys[i]
if key != "filter" and key != "fit" and key != "background"
raise error("image.resize: unknown option " + key)
opts[key] = options[key]
i = i + 1
if opts["filter"] != "nearest" and opts["filter"] != "bilinear"
raise error("image.resize: unsupported filter")
if (
opts["fit"] != "stretch" and opts["fit"] != "contain" and opts["fit"] != "cover"
)
raise error("image.resize: unsupported fit")
self.check_color(opts["background"])
opts
resize_stretch
Image.resize_stretch(width, height)
lib/image/image.tya:427
Image.resize_stretch provides the image/Image standard library operation.
Source
# Image.resize_stretch provides the image/Image standard library operation.
# @param width Int width value.
# @param height Int height value.
# @return Any the resulting value.
resize_stretch: width, height ->
out = Image(width, height, nil)
y = 0
while y < height
x = 0
while x < width
sx = ((x * self.width) / width.to_f()).to_i()
sy = ((y * self.height) / height.to_f()).to_i()
out.set_pixel(x, y, self.pixel(sx, sy))
x = x + 1
y = y + 1
out
rotate90
Image.rotate90()
lib/image/image.tya:442
Image.rotate90 provides the image/Image standard library operation.
Source
# Image.rotate90 provides the image/Image standard library operation.
# @return Any the resulting value.
rotate90: ->
out = Image(self.height, self.width, nil)
y = 0
while y < self.height
x = 0
while x < self.width
out.set_pixel(self.height - y - 1, x, self.pixel(x, y))
x = x + 1
y = y + 1
out
set_pixel
Image.set_pixel(x, y, value)
lib/image/image.tya:458
Image.set_pixel provides the image/Image standard library operation.
Source
# Image.set_pixel provides the image/Image standard library operation.
# @param x Int x value.
# @param y Int y value.
# @param value String value value.
# @return Any the resulting value.
set_pixel: x, y, value ->
self.check_xy(x, y)
self.check_color(value)
i = (y * self.width + x) * 4
self.pixels[i] = value.r
self.pixels[i + 1] = value.g
self.pixels[i + 2] = value.b
self.pixels[i + 3] = value.a
nil
to_bytes
Image.to_bytes()
lib/image/image.tya:470
Image.to_bytes provides the image/Image standard library operation.
Source
# Image.to_bytes provides the image/Image standard library operation.
# @return Any the resulting value.
to_bytes: ->
bytes(self.pixels)
write
Image.write(path, options)
lib/image/image.tya:477
Image.write provides the image/Image standard library operation.
Source
# Image.write provides the image/Image standard library operation.
# @param path String path value.
# @param options Dict options value.
# @return Any the resulting value.
write: path, options ->
file.File(path).write_bytes(
self.encode(self.write_format(path, options), options)
)
nil
write_format
Image.write_format(path, options)
lib/image/image.tya:487
Image.write_format provides the image/Image standard library operation.
Source
# Image.write_format provides the image/Image standard library operation.
# @param path String path value.
# @param options Dict options value.
# @return Any the resulting value.
write_format: path, options ->
if options != nil and options.has?("format")
return options["format"]
self.format_from_path(path)