Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JImage Implementation for J-Objects #399

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8f62037
Exported the JImage function from the JImage shorthand
TheCedarPrince Aug 14, 2021
f1e0822
Possible implementation of adding an image easily to JObjects
TheCedarPrince Aug 14, 2021
e1f5cd8
Made argument names less confusing
TheCedarPrince Aug 14, 2021
de8f54f
Merge branch 'master' into tcp-image-jobjects
TheCedarPrince Sep 28, 2021
7ffc1ba
Added scaling capabilities
TheCedarPrince Sep 28, 2021
e908f6d
Added docstring, changed default for shape
TheCedarPrince Oct 5, 2021
da0184c
In process of adding tests for JImage
TheCedarPrince Oct 5, 2021
1ac4713
Merge branch 'master' into tcp-image-jobjects
TheCedarPrince Dec 29, 2021
c891118
Added type signature from Cairo
TheCedarPrince Dec 30, 2021
27620ba
Reference image for JImage
TheCedarPrince Dec 30, 2021
80957f1
Fixed docstrings
TheCedarPrince Dec 30, 2021
6017adc
Revised using dispatch image
TheCedarPrince Dec 30, 2021
10c9686
Merge branch 'master' into tcp-image-jobjects
TheCedarPrince Jan 5, 2022
68a452a
Added TODO for scaling logic
TheCedarPrince Jan 5, 2022
36bbcbd
Temporary disabling of tests
TheCedarPrince Jan 5, 2022
5811f1a
Added TODO for tests, fixed test cases, and fixed references
TheCedarPrince Jan 5, 2022
2debc17
Added individual tests; working on scaling
TheCedarPrince Jan 10, 2022
545268c
Begin adding autoscaling support for JImage
TheCedarPrince Jan 10, 2022
80ca14e
Added image support dispatch for JBox
TheCedarPrince Jan 16, 2022
92f36ca
Added image support dispatch for JPoly
TheCedarPrince Jan 16, 2022
6e91da7
Added image support dispatch for JStar
TheCedarPrince Jan 16, 2022
2c18b27
Added image support dispatch for JRect
TheCedarPrince Jan 17, 2022
d6331a2
Added image support dispatch for JCircle
TheCedarPrince Jan 17, 2022
51dde2c
Added image support dispatch for JEllipse
TheCedarPrince Jan 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Javis.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Javis

using Animations
import Cairo: CairoImageSurface, image
import Cairo: CairoImageSurface, CairoSurfaceBase, image
using FFMPEG
using Gtk
using GtkReactive
Expand Down Expand Up @@ -726,6 +726,7 @@ include("shorthands/JEllipse.jl")
include("shorthands/JStar.jl")
include("shorthands/JPoly.jl")
include("shorthands/JShape.jl")
include("shorthands/JImage.jl")

export render, latex
export Video, Object, Background, Action, RFrames, GFrames
Expand All @@ -740,7 +741,7 @@ export act!
export anim_translate, anim_rotate, anim_rotate_around, anim_scale

export @Frames, prev_start, prev_end, startof, endof
export JBox, JCircle, JEllipse, JLine, JPoly, JRect, JStar, @JShape
export JBox, JCircle, JEllipse, JImage, JLine, JPoly, JRect, JStar, @JShape

# custom override of luxor extensions
export setline, setopacity, fontsize, get_fontsize, scale, text
Expand Down
70 changes: 70 additions & 0 deletions src/shorthands/JBox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,73 @@ JBox(
color = color,
action = action,
) -> _JBox(pt, width, height, cornerradius, color, action)

"""
TODO: Add documentation
"""
JBox(
pt::Point,
width::Real,
height::Real,
color = "",
action = :stroke,
vertices::Bool = false,
image_path::String = "",
scale_factor::Symbol = :inset,
) =
(
args...;
pt = pt,
width = width,
height = height,
color = color,
action = action,
vertices = vertices,
image_path = image_path,
scale_factor = scale_factor,
) -> _JBox(pt, width, height, color, action, vertices, image_path, scale_factor)

function _JBox(
pt::Point,
width::Real,
height::Real,
color,
action::Symbol,
vertices::Bool,
image_path::String,
scale_factor::Symbol,
)
bbox = BoundingBox(box(pt, width, height, :path, vertices = vertices))

if !isnothing(color)
sethue(color)
box(pt, width, height, :fill, vertices = vertices)
end

img = readpng(image_path)

if scale_factor == :inset
boxside = max(boxwidth(bbox), boxheight(bbox))
imageside = max(img.width, img.height)

scaling = boxside / imageside

box(bbox, :clip)

translate(boxmiddlecenter(bbox))
scale(scaling)
elseif scale_factor == :clip
boxside = min(boxwidth(bbox), boxheight(bbox))
imageside = min(img.width, img.height)

box(bbox, :clip)

scaling = boxside / imageside

translate(boxmiddlecenter(bbox))
scale(scaling)
end

placeimage(img, pt, centered = true)
return Point(pt.x - width / 2, pt.y + height / 2)
end
55 changes: 55 additions & 0 deletions src/shorthands/JCircle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,58 @@ JCircle(p1::Point, p2::Point; kwargs...) =
JCircle(midpoint(p1, p2), distance(p1, p2) / 2; kwargs...)

JCircle(radius::Real; kwargs...) = JCircle(O, radius; kwargs...)

"""
TODO: Add Documentation!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently I have no idea what :inset and :clip mean to be honest 😄
Seems like :clip positions the image somewhere else but I don't really have the option to move the image or have I?
Also for me a scale_factor is a number and not a symbol. Maybe scale_type or something but I would first need to know what they do to give my comment on that 😄

"""
JCircle(
center::Point,
radius::Real,
color = "black",
linewidth = 1,
action = :stroke,
image_path = "",
scale_factor = :inset,
) =
(
args...;
center = center,
radius = radius,
color = color,
linewidth = linewidth,
action = action,
image_path = image_path,
scale_factor = scale_factor,
) -> _JCircle(center, radius, color, linewidth, action, image_path, scale_factor)

function _JCircle(center, radius, color, linewidth, action, image_path, scale_factor)

bbox = BoundingBox(circle(center, radius, :path))

if !isnothing(color)
sethue(color)
circle(center, radius, :fill)
end

img = readpng(image_path)

if scale_factor == :inset
side = sqrt((radius * 2)^2 / 2)
bbox = BoundingBox(box(O, side, side, action = :path))
boxside = max(boxwidth(bbox), boxheight(bbox))
imageside = max(img.width, img.height)
box(O, side, side, action = :clip)
elseif scale_factor == :clip
bbox = BoundingBox(circle(O, radius, action = :path))
boxside = min(boxwidth(bbox), boxheight(bbox))
imageside = min(img.width, img.height)
circle(O, radius, action = :clip)
end

scalefactor = boxside / imageside

translate(boxmiddlecenter(bbox))
scale(scalefactor)
placeimage(img, centered = true)
return center
end
67 changes: 67 additions & 0 deletions src/shorthands/JEllipse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,70 @@ JEllipse(
linewidth = linewidth,
action = action,
) -> _JEllipse(focus1, focus2, pt, color, linewidth, action, stepvalue, reversepath)


"""
TODO: Add documentation
"""
JEllipse(
cpt::Point,
w::Real,
h::Real,
color = "black",
linewidth = 1,
action = :stroke,
image_path::String = "",
scale_factor::Symbol = :inset,
) =
(
args...;
cpt = cpt,
w = w,
h = h,
color = color,
linewidth = linewidth,
action = action,
image_path = image_path,
scale_factor = scale_factor,
) -> _JEllipse(cpt, w, h, color, linewidth, action, image_path, scale_factor)

function _JEllipse(
cpt::Point,
w::Real,
h::Real,
color,
linewidth,
action::Symbol,
image_path::String = "",
scale_factor = :inset,
)

bbox = BoundingBox(ellipse(O, w, h, :path))

if !isnothing(color)
sethue(color)
ellipse(O, w, h, :fill)
end

img = readpng(image_path)

if scale_factor == :inset
inset_radius = minimum([w, h]) / 2
bbox = BoundingBox(circle(O, inset_radius, :path))
boxside = max(boxwidth(bbox), boxheight(bbox))
imageside = max(img.width, img.height)
circle(O, inset_radius, :clip)
elseif scale_factor == :clip
boxside = min(boxwidth(bbox), boxheight(bbox))
imageside = min(img.width, img.height)
ellipse(O, 200, 100, :clip)
end

scalefactor = boxside / imageside

translate(boxmiddlecenter(bbox))
scale(scalefactor)
placeimage(img, centered = true)
return cpt
end

137 changes: 137 additions & 0 deletions src/shorthands/JImage.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# TODO: Add scaling logic for each shape; see https://github.com/JuliaAnimators/Javis.jl/pull/399#issuecomment-1003549900 for details on what to do

function _JImage(pos, img, centering, shapeargs, shape, scaleargs)
if !isnothing(shape)
bbox = BoundingBox(shape(shapeargs...))
end

if scaleargs == :inset
if Symbol(shape) == :box
boxside = max(boxwidth(bbox), boxheight(bbox))
imageside = max(img.width, img.height)

scalefactor = boxside / imageside

box(bbox, :clip)

translate(boxmiddlecenter(bbox))
scale(scalefactor)
elseif Symbol(shape) == :star
bbox = BoundingBox(circle(shapeargs.center, shapeargs.radius * shapeargs.ratio))
boxside = max(boxwidth(bbox), boxheight(bbox))
imageside = max(img.width, img.height)
circle(shapeargs.center, shapeargs.radius * shapeargs.ratio, action = :clip)

scalefactor = boxside / imageside

translate(boxmiddlecenter(bbox))
scale(scalefactor)
end
elseif scaleargs == :clip
if Symbol(shape) == :box
boxside = min(boxwidth(bbox), boxheight(bbox))
imageside = min(img.width, img.height)

box(bbox, :clip)

scalefactor = boxside / imageside

translate(boxmiddlecenter(bbox))
scale(scalefactor)
elseif Symbol(shape) == :star
boxside = min(boxwidth(bbox), boxheight(bbox))
imageside = min(img.width, img.height)

star(
shapeargs.center,
shapeargs.radius,
shapeargs.npoints,
shapeargs.ratio,
shapeargs.orientation,
action = :clip,
)

scalefactor = boxside / imageside

translate(boxmiddlecenter(bbox))
scale(scalefactor)
end
else
scale(scaleargs)
end

placeimage(img, pos, centered = centering)
TheCedarPrince marked this conversation as resolved.
Show resolved Hide resolved
return pos
end

"""
JImage(pos::Point, img::CairoSurfaceBase{UInt32}, centering = true; shapeargs = (), shape = nothing, scaleargs = 1)

Place a given image at a given location as a `Javis` object.
Images can be cropped to different shapes and scaled to different sizes while being placed.
TheCedarPrince marked this conversation as resolved.
Show resolved Hide resolved

# Arguments
- `pos::Point`: Where to place the image inside a shape.
- `img::CairoSurfaceBase{UInt32}`: Expects a CairoSurfaceBase object via `readpng("your_image.png")`
- `centering::Bool`: Centers the object at `pos`
- `shapeargs`: Arguments to be passed to a given shape type
- `shape`: A Luxor shape function such as `circle`, `box`, etc.
- `scaleargs`: The arguments used for scaling the image used on the shape

# Return

Returns the position of the image location, `pos`.
"""
JImage(
pos::Point,
img::CairoSurfaceBase{UInt32},
centering::Bool = true;
shapeargs = (),
shape = nothing,
scaleargs = :inset,
) =
(
args...;
pos = pos,
img = img,
centering = centering,
shapeargs = shapeargs,
shape = shape,
scaleargs = scaleargs,
) -> _JImage(pos, img, centering, shapeargs, shape, scaleargs)

"""
JImage(pos::Point, img::String, centering = true; shapeargs = (), shape = nothing, scaleargs = 1)

Place a given image at a given location as a `Javis` object.
Images can be cropped to different shapes and scaled to different sizes while being placed.

# Arguments
- `pos::Point`: Where to place the image inside a shape
- `img::String`: Expects the path to an image
- `centering::Bool`: Centers the object at `pos`
- `shapeargs`: Arguments to be passed to a given shape type
- `shape`: A Luxor shape function such as `circle`, `box`, etc.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would have shape before shapeargs and maybe add an example which shows what shapeargs and scaleargs do?

- `scaleargs`: The arguments used for scaling the image used on the shape

# Return

Returns the position of the image location, `pos`.
"""
JImage(
pos::Point,
img::String,
centering::Bool = true;
shapeargs = (),
shape = nothing,
scaleargs = :inset,
) =
(
args...;
pos = pos,
img = readpng(img),
centering = centering,
shapeargs = shapeargs,
shape = shape,
scaleargs = scaleargs,
) -> _JImage(pos, img, centering, shapeargs, shape, scaleargs)
Loading