s LearnSqueak.com

SMALLTALK LAB

Seam Carving in Squeak: Find and Remove a Low-Energy Path

Remove one column’s worth of cells without taking a straight column. Compute the cheapest connected seam in a small synthetic energy grid.

6 tested checkpointsNative Squeak screenshotsComputed visual replay

Before you begin

Open a Squeak Workspace. Use Print it on the complete checkpoint, including its temporary variable declarations.

These are native Squeak 6.1 examples. Each checkpoint is independent: open the download as text, copy one complete expression into Workspace and choose Print it. Do not file in the checkpoint collection as a class definition.

Expected values are Smalltalk expressions. The printer may show explicit Symbol markers: #(A B) and #(#A #B) represent the same array of Symbols.

01 - Describe the choices

A vertical seam has one cell per row. Its next column may move left by one, stay put, or move right by one. From column 3, the previous row’s allowed columns are 2, 3, and 4.

Independent Workspace expression - select all, then Print it
| costs c |
costs := #(6 1 5 8).
c := 3.
((c - 1) to: (c + 1)) collect: [:p | costs at: p]

Expected: #(1 5 8)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Describe the choices
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: For column 1, clip the range so column zero cannot be selected.

Download this checkpoint

02 - Accumulate one row of costs

The cheapest way to enter each new cell is its fixed energy plus the smallest reachable total from the previous row. By using cumulative totals, we do not risk choosing a cheap local cell on an expensive path.

Independent Workspace expression - select all, then Print it
| old row |
old := #(6 1 5 8).
row := #(4 2 7 4).
(1 to: row size) collect: [:c | | lo hi |
 lo := (c - 1) max: 1. hi := (c + 1) min: row size.
 (row at: c) + (old copyFrom: lo to: hi) min]

Expected: #(5 3 8 9)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Accumulate one row of costs
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: For column 2, the best predecessor cost is 1 and the new cell costs 2, giving 3.

Download this checkpoint

03 - Remember predecessors and reconstruct

Store the winning predecessor in each row as you accumulate costs. Choose the cheapest bottom cell, then follow its saved predecessors upward. When costs tie, choosing the leftmost predecessor makes the outcome repeatable.

Independent Workspace expression - select all, then Print it
| energy trace |
energy := #((6 1 5 8) (4 2 7 4) (9 9 1 9) (5 4 6 0)).
trace := [:grid | | height width costs parents route best |
 height := grid size. width := grid first size.
 costs := grid first copy.
 parents := Array new: height.
 2 to: height do: [:r | | next from |
  next := Array new: width. from := Array new: width.
  1 to: width do: [:c | | pick |
   pick := (c - 1) max: 1.
   pick + 1 to: ((c + 1) min: width) do: [:p |
    (costs at: p) < (costs at: pick) ifTrue: [pick := p]].
   from at: c put: pick.
   next at: c put: ((costs at: pick) + ((grid at: r) at: c))].
  parents at: r put: from. costs := next].
 best := 1.
 2 to: width do: [:c |
  (costs at: c) < (costs at: best) ifTrue: [best := c]].
 route := Array new: height.
 route at: height put: best.
 height to: 2 by: -1 do: [:r |
  route at: r - 1 put: ((parents at: r) at: (route at: r))].
 {route. costs at: best}].
trace value: energy

Expected: #((2 2 3 4) 4)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Remember predecessors and reconstruct
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: The winning path bends right twice. Adjacent columns never differ by more than one.

Download this checkpoint

04 - Check the seam’s actual cost

The chosen cell values are 1, 2, 1, and 0, which sum to 4. This verifies that path coordinates and the advertised cost agree.

Independent Workspace expression - select all, then Print it
| energy trace path total |
energy := #((6 1 5 8) (4 2 7 4) (9 9 1 9) (5 4 6 0)).
trace := [:grid | | height width costs parents route best |
 height := grid size. width := grid first size.
 costs := grid first copy.
 parents := Array new: height.
 2 to: height do: [:r | | next from |
  next := Array new: width. from := Array new: width.
  1 to: width do: [:c | | pick |
   pick := (c - 1) max: 1.
   pick + 1 to: ((c + 1) min: width) do: [:p |
    (costs at: p) < (costs at: pick) ifTrue: [pick := p]].
   from at: c put: pick.
   next at: c put: ((costs at: pick) + ((grid at: r) at: c))].
  parents at: r put: from. costs := next].
 best := 1.
 2 to: width do: [:c |
  (costs at: c) < (costs at: best) ifTrue: [best := c]].
 route := Array new: height.
 route at: height put: best.
 height to: 2 by: -1 do: [:r |
  route at: r - 1 put: ((parents at: r) at: (route at: r))].
 {route. costs at: best}].
path := (trace value: energy) first.
total := (1 to: path size) inject: 0 into: [:sum :r |
 sum + ((energy at: r) at: (path at: r))].
{path. total. total = (trace value: energy) second}

Expected: #((2 2 3 4) 4 true)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Check the seam’s actual cost
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Inspect the marked cells in the native replay before checking the sum.

Download this checkpoint

05 - Remove exactly one cell per row

Remove the selected column from each row and keep all other values in order. On an image, you would remove the corresponding pixels, not the energy values. We show numbers to make the operation inspectable.

Independent Workspace expression - select all, then Print it
| energy trace path |
energy := #((6 1 5 8) (4 2 7 4) (9 9 1 9) (5 4 6 0)).
trace := [:grid | | height width costs parents route best |
 height := grid size. width := grid first size.
 costs := grid first copy.
 parents := Array new: height.
 2 to: height do: [:r | | next from |
  next := Array new: width. from := Array new: width.
  1 to: width do: [:c | | pick |
   pick := (c - 1) max: 1.
   pick + 1 to: ((c + 1) min: width) do: [:p |
    (costs at: p) < (costs at: pick) ifTrue: [pick := p]].
   from at: c put: pick.
   next at: c put: ((costs at: pick) + ((grid at: r) at: c))].
  parents at: r put: from. costs := next].
 best := 1.
 2 to: width do: [:c |
  (costs at: c) < (costs at: best) ifTrue: [best := c]].
 route := Array new: height.
 route at: height put: best.
 height to: 2 by: -1 do: [:r |
  route at: r - 1 put: ((parents at: r) at: (route at: r))].
 {route. costs at: best}].
path := (trace value: energy) first.
(1 to: energy size) collect: [:r | | row |
 row := energy at: r.
 (((1 to: row size) reject: [:c | c = (path at: r)])
  collect: [:c | row at: c]) asArray]

Expected: #((6 5 8) (4 7 4) (9 9 9) (5 4 6))

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Remove exactly one cell per row
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: The returned grid still has four rows; each row now has three cells.

Download this checkpoint

06 - Make ties explicit

In a flat equal-cost grid there can be many equally good seams. The strict less-than comparison preserves the leftmost tie rather than pretending one path is better.

Independent Workspace expression - select all, then Print it
| energy trace |
energy := #((6 1 5 8) (4 2 7 4) (9 9 1 9) (5 4 6 0)).
trace := [:grid | | height width costs parents route best |
 height := grid size. width := grid first size.
 costs := grid first copy.
 parents := Array new: height.
 2 to: height do: [:r | | next from |
  next := Array new: width. from := Array new: width.
  1 to: width do: [:c | | pick |
   pick := (c - 1) max: 1.
   pick + 1 to: ((c + 1) min: width) do: [:p |
    (costs at: p) < (costs at: pick) ifTrue: [pick := p]].
   from at: c put: pick.
   next at: c put: ((costs at: pick) + ((grid at: r) at: c))].
  parents at: r put: from. costs := next].
 best := 1.
 2 to: width do: [:c |
  (costs at: c) < (costs at: best) ifTrue: [best := c]].
 route := Array new: height.
 route at: height put: best.
 height to: 2 by: -1 do: [:r |
  route at: r - 1 put: ((parents at: r) at: (route at: r))].
 {route. costs at: best}].
trace value: #((3 3 3) (2 2 2))

Expected: #((1 1) 5)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Make ties explicit
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Try a grid with just one column. Every row then uses column 1.

Download this checkpoint

Replay the checkpoints

NATIVE SQUEAK CAPTURE
Native Squeak view highlighting the first seam cell, row 1 column 2
Native Morphic replay of the four selected cells. Yellow marks the latest cell and green marks earlier cells.

Download the native energy-grid demo. Do it in Workspace. Close the window, change shown := 1. to a number from 2 to 4, and run again.

Reason about correctness and limits

The grid contains synthetic fixed energies, not image intensities. A real implementation computes an energy from the pixels, removes the matching pixels, then recomputes the affected energy before another removal. This tutorial does only one seam.

The block assumes a nonempty rectangular grid with at least one column. Time is O(rows × columns); the prior-cost row is reused, but predecessor rows stay for backtracking.

Three practice challenges

Count an edge’s options

How many predecessor columns can the rightmost cell of a four-column grid use?

Show solution

Two: 3 and 4.

Check connectivity

Would columns 2, 4, 4, 4 form a valid seam?

Show solution

No. It jumps by two between the first and second rows.

Change the energy

Put a high cost in the currently selected first-row cell. What should you recompute?

Show solution

Re-run the cost accumulation and backtracking; changing one cost can change the best path through several rows.

Further reading

Avidan and Shamir’s original seam-carving paper defines content-aware image resizing and energy functions. This tutorial implements a small fixed-energy removal example.

Continue learning

Pathfinding | Flood Fill | First Point

Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.