sourceextensions/image/lib/Image.xtl

1⍝# Image.xtl -- the facade of the image extension: PNG and JPEG files as 2⍝# Float arrays from 0 to 1, and back. Gray is h by w, color h by w by 3 3⍝# (red, green, blue); alpha is dropped. 4⍝# Import: "im:" u_se< "Image" 5⍝# Files are named by paths under the working directory (or 6⍝# XETAL_IMAGE_ROOT), relative, with no .. 7⍝# Run with xetal-x (xetal-x --ext DIR puts this directory on XETAL_PATH). 8⍝# Each signature line is written out by the binding macro (lib/Ffi.xtlm). 9 10ᶠᶠⁱ⁼u̲se< "Ffi" 11 12⍝# im:r_ead "photo.png": the picture as it is (gray or color). 13"r_ead : text -> floats" ᶠᶠⁱb̲ind< "image/read"
ᶠᶠⁱb̲ind< expands to
ˡr̲ead ← { a →
  pa ← a ⎕N̲PUT "ext:image/read?text"
  v ← n̲umbers ⎕N̲GET "ext:image/read"
  (f̲loor n̲umbers ⎕N̲GET "ext:image/read?shape") r̲eshape v
}
14⍝# im:g_ray "photo.jpg": the picture in gray (its luminance), h by w. 15"g_ray : text -> floats" ᶠᶠⁱb̲ind< "image/gray"
ᶠᶠⁱb̲ind< expands to
ˡg̲ray ← { a →
  pa ← a ⎕N̲PUT "ext:image/gray?text"
  v ← n̲umbers ⎕N̲GET "ext:image/gray"
  (f̲loor n̲umbers ⎕N̲GET "ext:image/gray?shape") r̲eshape v
}
16⍝# im:s_ize "photo.jpg": height and width, without reading the pixels. 17"s_ize : text -> ints" ᶠᶠⁱb̲ind< "image/size"
ᶠᶠⁱb̲ind< expands to
ˡs̲ize ← { a →
  pa ← a ⎕N̲PUT "ext:image/size?text"
  v ← f̲loor n̲umbers ⎕N̲GET "ext:image/size"
  (f̲loor n̲umbers ⎕N̲GET "ext:image/size?shape") r̲eshape v
}
18⍝# "out.png" im:w_rite! a: the array as a PNG (or a JPEG, by the name), 19⍝# values clamped to 0..1; how many pixels. 20"w_rite! : text float -> int" ᶠᶠⁱb̲ind< "image/write"
ᶠᶠⁱb̲ind< expands to
ˡw̲rite! ← { a b →
  pa ← a ⎕N̲PUT "ext:image/write?text"
  pb ← (f̲ormat r̲avel f̲loat b) ⎕N̲PUT "ext:image/write?float=" c̲at f̲ormat s̲hape b
  f̲loor f̲irst n̲umbers ⎕N̲GET "ext:image/write"
}
21⍝# (h c_at w) im:r_esize a: resampled to h by w (Lanczos), gray or color. 22"r_esize : float float -> floats" ᶠᶠⁱb̲ind< "image/resize"
ᶠᶠⁱb̲ind< expands to
ˡr̲esize ← { a b →
  pa ← (f̲ormat r̲avel f̲loat a) ⎕N̲PUT "ext:image/resize?float=" c̲at f̲ormat s̲hape a
  pb ← (f̲ormat r̲avel f̲loat b) ⎕N̲PUT "ext:image/resize?float=" c̲at f̲ormat s̲hape b
  v ← n̲umbers ⎕N̲GET "ext:image/resize"
  (f̲loor n̲umbers ⎕N̲GET "ext:image/resize?shape") r̲eshape v
}
23 24⍝# The native package behind these functions. 25ˡpackage ← "image"