Skip to content

Commit

Permalink
Crop images in image space (between 0 - 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
nmichaud committed May 22, 2024
1 parent 4c8cd3f commit 5a95e4d
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions virtual-programs/display/image.folk
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ On process "display" {
set rotate $::rotate
# TODO: Do this with a wish, instead of hard-coding the global dict.
dict set ::pipelines "image" [Gpu::pipeline {sampler2D image vec2 imageSize
vec2 pos float radians vec2 scale
vec2 pos float radians vec2 scale vec4 crop
fn rotate} {
vec2 a = pos + rotate(scale*-imageSize/2, -radians);
vec2 b = pos + rotate(scale*vec2(imageSize.x, -imageSize.y)/2, -radians);
Expand All @@ -19,7 +19,10 @@ On process "display" {
vec2 p = gl_FragCoord.xy;
vec2 uv = invBilinear(p, a, b, c, d);
if( max( abs(uv.x-0.5), abs(uv.y-0.5))<0.5 ) {
return texture(image, uv);
if ((crop[0] < uv.x) && (uv.x < crop[2]) &&
(crop[1] < uv.y) && (uv.y < crop[3])) {
return texture(image, uv);
}
}
return vec4(0.0, 0.0, 0.0, 0.0);
}]
Expand Down Expand Up @@ -108,6 +111,7 @@ On process "display" {
set im [dict get $options image]
set radians [dict get $options radians]
set scale [dict_getdef $options scale 1.0]
set crop [dict_getdef $options crop [list 0. 0. 1.0 1.0]]
if {[llength $scale] == 1} {
set scale [list $scale $scale]
}
Expand All @@ -116,6 +120,6 @@ On process "display" {

Wish the GPU draws pipeline "image" with arguments \
[list $gim [list [image_t width $im] [image_t height $im]] \
$center $radians $scale]
$center $radians $scale $crop]
}
}

0 comments on commit 5a95e4d

Please sign in to comment.