11 Images and Surfaces
This section covers image loading, saving, and surface operations using SDL_image.
For texture loading directly to GPU, see the Textures section. Surfaces are CPU-side images useful for pixel manipulation, screenshots, and window icons.
11.1 Loading Images
procedure
(load-surface source [ #:type type #:custodian cust]) → surface? source : (or/c string? path? bytes? input-port?) type : (or/c symbol? string? #f) = #f cust : custodian? = (current-custodian)
Supports PNG, JPEG, BMP, GIF, WebP, and many other formats via SDL_image.
The #:type hint specifies the format when loading from bytes or a port (e.g., 'png, 'jpg).
;; Load from file (define surf (load-surface "image.png")) ;; Load from bytes with format hint (define surf2 (load-surface image-bytes #:type 'png))
procedure
(surface-ptr surf) → cpointer?
surf : surface?
procedure
(surface-destroy! surf) → void?
surf : surface?
Note: Surfaces are automatically destroyed when their custodian shuts down.
11.2 Creating Surfaces
procedure
(make-surface width height [ #:format format #:custodian cust]) → surface? width : exact-nonnegative-integer? height : exact-nonnegative-integer? format : (or/c symbol? exact-nonnegative-integer?) = 'rgba32 cust : custodian? = (current-custodian)
Format symbols include: 'rgba32, 'rgba8888, 'argb8888, 'rgb24.
procedure
(duplicate-surface surf [#:custodian cust]) → surface?
surf : surface? cust : custodian? = (current-custodian)
procedure
(convert-surface surf format [ #:custodian cust]) → surface? surf : surface? format : (or/c symbol? exact-nonnegative-integer?) cust : custodian? = (current-custodian)
procedure
(surface->texture renderer surf [ #:custodian cust]) → texture? renderer : renderer? surf : surface? cust : custodian? = (current-custodian)
The surface is not destroyed; you can create multiple textures from it.
11.3 Surface Properties
procedure
(surface-width surf) → exact-integer?
surf : surface?
procedure
(surface-height surf) → exact-integer?
surf : surface?
procedure
(surface-pitch surf) → exact-integer?
surf : surface?
procedure
(surface-format surf) → symbol?
surf : surface?
procedure
(surface-pixels surf) → cpointer?
surf : surface?
Only use when the surface is locked.
11.4 Pixel Access
procedure
(surface-get-pixel surf x y) →
byte? byte? byte? byte? surf : surface? x : exact-integer? y : exact-integer?
Returns (values r g b a) where each component is 0-255.
procedure
(surface-set-pixel! surf x y r g b [a]) → void?
surf : surface? x : exact-integer? y : exact-integer? r : byte? g : byte? b : byte? a : byte? = 255
procedure
(surface-get-pixel-float surf x y) →
real? real? real? real? surf : surface? x : exact-integer? y : exact-integer?
procedure
(surface-set-pixel-float! surf x y r g b [a]) → void?
surf : surface? x : exact-integer? y : exact-integer? r : real? g : real? b : real? a : real? = 1.0
11.5 Bulk Pixel Access
procedure
(surface-fill-pixels! surf generator) → void?
surf : surface?
generator :
(-> exact-integer? exact-integer? (values byte? byte? byte? byte?))
The generator receives (x y) and returns (values r g b a).
;; Create a gradient (surface-fill-pixels! surf (lambda (x y) (values (quotient (* x 255) width) ; red gradient 128 ; constant green (quotient (* y 255) height) ; blue gradient 255))) ; opaque
procedure
(call-with-surface-pixels surf proc) → any
surf : surface?
proc :
(-> cpointer? exact-integer? exact-integer? exact-integer? exact-integer? any)
The procedure receives: (pixels width height pitch bytes-per-pixel).
11.6 Surface Locking
Some operations require locking the surface first.
procedure
(surface-lock! surf) → #t
surf : surface?
procedure
(surface-unlock! surf) → void?
surf : surface?
procedure
(call-with-locked-surface surf proc) → any
surf : surface?
proc :
(-> surface? cpointer? exact-integer? exact-integer? exact-integer? any)
The procedure receives: (surface pixels width height pitch).
11.7 Blitting
procedure
(blit-surface! src dst [ #:src-rect src-rect #:dst-rect dst-rect]) → void? src : surface? dst : surface? src-rect : (or/c (list/c real? real? real? real?) #f) = #f dst-rect : (or/c (list/c real? real? real? real?) #f) = #f
Rectangles are specified as (list x y w h).
procedure
(blit-surface-scaled! src dst [ #:src-rect src-rect #:dst-rect dst-rect #:scale-mode scale-mode]) → void? src : surface? dst : surface? src-rect : (or/c (list/c real? real? real? real?) #f) = #f dst-rect : (or/c (list/c real? real? real? real?) #f) = #f scale-mode : (or/c 'nearest 'linear) = 'nearest
11.8 Filling and Clearing
procedure
(fill-surface! surf color [#:rect rect]) → void?
surf : surface?
color :
(or/c (list/c byte? byte? byte?) (list/c byte? byte? byte? byte?)) rect : (or/c (list/c real? real? real? real?) #f) = #f
Color is (list r g b) or (list r g b a).
procedure
(clear-surface! surf r g b [a]) → void?
surf : surface? r : real? g : real? b : real? a : real? = 1.0
11.9 Transformations
procedure
(flip-surface! surf mode) → void?
surf : surface? mode : (or/c 'horizontal 'vertical)
To flip both ways, call twice with different modes.
procedure
(scale-surface surf width height [ #:mode mode #:custodian cust]) → surface? surf : surface? width : exact-nonnegative-integer? height : exact-nonnegative-integer? mode : (or/c 'nearest 'linear) = 'nearest cust : custodian? = (current-custodian)
11.10 Saving Images
procedure
surf : surface? path : (or/c string? path?) quality : exact-nonnegative-integer? = 90
quality is 0-100 (higher = better quality, larger file).
procedure
path : (or/c string? path?) cust : custodian? = (current-custodian)
For other formats (PNG, JPEG, etc.), use load-surface.
11.11 Screenshots
procedure
(render-read-pixels renderer [ #:custodian cust]) → surface? renderer : renderer? cust : custodian? = (current-custodian)
Use this to take screenshots:
(define screenshot (render-read-pixels ren)) (save-png! screenshot "screenshot.png")
11.12 Format Detection
procedure
(image-format source) → (or/c symbol? #f)
source : (or/c bytes? input-port?)
Returns a symbol like 'png, 'jpg, 'gif, or #f if the format is unknown.
11.13 Color Key and Modulation
procedure
(set-surface-color-key! surf color) → void?
surf : surface? color : (or/c (list/c byte? byte? byte?) #f)
Pass #f to disable the color key.
procedure
(surface-has-color-key? surf) → boolean?
surf : surface?
procedure
(set-surface-alpha-mod! surf alpha) → void?
surf : surface? alpha : byte?
procedure
(set-surface-blend-mode! surf mode) → void?
surf : surface? mode : (or/c 'none 'blend 'add 'mod 'mul)