//// //// RASTER.DBN //// //// Warren Sack //// February 2003 //// //// This is a slightly modified version of PETRI.DBN. It can be used to //// experiment with image processing using convolution filters. Implement //// the command "convolveOp" in order to create a new filter. //// //// Colors are encoded as six digit numbers: from 0 to 999999. //// The first two digits encode the percentage of red, the next //// two encode the percentage of green and the last two blue; //// i.e., the six digit numnbers should be read like this: RRGGBB. //// There are 625 elements in the gridded image. Each element is //// stored as a six digit number in the array. //// set NumberOfRows 25 set NumberOfColumns 25 set SquareWidth 4 set SquareHeight 4 set OnColor 0 set OffColor 100 set ProgressBar 0 set ProgessBarColor 50 number row Square { value ((Square - 1) / NumberOfColumns) } number column Square { value ((Square - 1) % NumberOfColumns) } command paintPetriSquare Square Color { set Row set Column set LeftPixel (SquareWidth * Column) set BottomPixel (SquareHeight * Row) repeat X 0 (SquareWidth - 1) { repeat Y 0 (SquareHeight - 1) { set [(LeftPixel + X) (BottomPixel + Y)] Color } } } // This version of paintSquare uses colors. command paintSquare Square Color { set RedPortion (Color / 10000) set GreenPortion ((Color - (RedPortion*10000)) / 100) set BluePortion (Color - (RedPortion*10000) - (GreenPortion*100)) set Row set Column set LeftPixel (SquareWidth * Column) set BottomPixel (SquareHeight * Row) repeat X 0 (SquareWidth - 1) { repeat Y 0 (SquareHeight - 1) { set [(LeftPixel + X) (BottomPixel + Y) red] RedPortion set [(LeftPixel + X) (BottomPixel + Y) green] GreenPortion set [(LeftPixel + X) (BottomPixel + Y) blue] BluePortion } } } command turnOn Square { paintSquare Square OnColor } command turnOff Square { paintSquare Square OffColor } number colorOfSquare Square { value } number isOn? Square { set Result 0 same? 1 { set Result 1 } value Result } number isOff? Square { set Result 0 same? 0 { set Result 1 } value Result } number north Square { set Row set Column set ResultRow (Row + 1) same? ResultRow NumberOfRows { set ResultRow 0 } value ((ResultRow * NumberOfColumns) + Column + 1) } number south Square { set Row set Column set ResultRow (Row - 1) same? Row 0 { set ResultRow (NumberOfRows - 1) } value ((ResultRow * NumberOfColumns) + Column + 1) } number east Square { set Row set Column set ResultColumn (Column + 1) same? ResultColumn NumberOfColumns { set ResultColumn 0 } value ((Row * NumberOfColumns) + ResultColumn + 1) } number west Square { set Row set Column set ResultColumn (Column - 1) same? Column 0 { set ResultColumn (NumberOfColumns - 1) } value ((Row * NumberOfColumns) + ResultColumn + 1) } number northwest Square { value > } number northeast Square { value > } number southwest Square { value > } number southeast Square { value > } number shouldTurnOn? Square { set Result 0 same? 1 { set NeighborsOn 0 set NeighborsOn (NeighborsOn + >) set NeighborsOn (NeighborsOn + >) set NeighborsOn (NeighborsOn + >) set NeighborsOn (NeighborsOn + >) set NeighborsOn (NeighborsOn + >) set NeighborsOn (NeighborsOn + >) set NeighborsOn (NeighborsOn + >) set NeighborsOn (NeighborsOn + >) same? NeighborsOn 2 { set Result 1 } } value Result } number convolveOp Square { value } command nextStateOfRow Row StartIndex { set Square (1 + (Row * NumberOfColumns)) set Index StartIndex repeat Column 0 (NumberOfColumns - 1) { set // set set Square (Square + 1) set Index (Index + 1) } } command updateRow Row StartIndex { set Square (1 + (Row * NumberOfColumns)) set Index StartIndex repeat Column 0 (NumberOfColumns - 1) { set set Square (Square + 1) set Index (Index + 1) } } command repaintPetri { paper OffColor repeat Square 1 (NumberOfRows * NumberOfColumns) { same? 1 { turnOn Square } } } command repaint { repeat Square 1 (NumberOfRows * NumberOfColumns) { paintSquare Square } } command initializePetri { paper OffColor set Square 610 set 1 turnOn Square set EastSquare set 1 turnOn EastSquare set SouthwestSquare set 1 turnOn SouthwestSquare set EastSoutheastSquare > set 1 turnOn EastSoutheastSquare set Square 320 set 1 turnOn Square set Square 51 set 1 turnOn Square set EastNortheastSquare > set 1 turnOn EastNortheastSquare set SouthSquare set 1 turnOn SouthSquare set EastSoutheastSouthSquare >> set 1 turnOn EastSoutheastSouthSquare } // This initialize command paints an image of banded colors. // replace it with a drawing program or another image generator command initialize { set ColorIncrement (999999 / (NumberOfRows * NumberOfColumns)) set Color 0 repeat S 1 (NumberOfRows * NumberOfColumns) { paintSquare S Color set Color (Color + ColorIncrement) } } initialize // show the initial image repaint // forever // { set IndexAfterLastRow ((NumberOfRows * NumberOfColumns) + 1) nextStateOfRow (NumberOfRows - 1) IndexAfterLastRow nextStateOfRow 0 (IndexAfterLastRow + NumberOfColumns) set ProgressBar 0 repeat Y 1 (NumberOfRows - 2) { set ProgressBar (ProgressBar + 2) pen ProgessBarColor line 100 0 100 ProgressBar set Offset (1 + (Y % 2)) set LastOffset 2 same? Offset 2 { set LastOffset 1 } nextStateOfRow Y (IndexAfterLastRow + (Offset * NumberOfColumns)) updateRow (Y - 1) (IndexAfterLastRow + (LastOffset * NumberOfColumns)) } updateRow (NumberOfRows - 2) (IndexAfterLastRow + (Offset * NumberOfColumns)) updateRow (NumberOfRows - 1) IndexAfterLastRow // show the image after the filter has been run repaint // }