In the second part of this chapter, a region finding procedure is described which takes a list of ranges and labels each pixel of an image according to which of the ranges the corresponding grey-level is contained within or 0 if it is not contained within any of the ranges.
The following procedure performs this labelling for a given image and list of ranges:
define findregions(image, ranges) -> regionmap;
  vars a, b, table, i, label, xsize, ysize, x, y;
  /* build a table mapping grey-levels to labels   */
  /* (2nd arg. to newarray initializes cells to 0) */
  newarray([0 99],0) -> table;
  0 -> label;
  foreach [?a ?b] in ranges do
    label + 1 -> label;
    for i from a to b do
      label -> table(i)
    endfor
  endforeach;
  /* construct a region map from image by assigning label
     from table to each pixel according to grey-level */
  boundslist(image) --> [1 ?xsize 1 ?ysize];
  newarray([1 ^xsize 1 ^ysize]) -> regionmap;
  for y from 1 to ysize do
    for x from 1 to xsize do
      table(image(x,y)) -> regionmap(x,y)
    endfor
  endfor;
enddefine;