;;;; SUMMARY ;; Import PPM images into turtle-based sprites ;;;; Copyright ;; Copyright (C) 2004/2005 James P. Steiner ;; Portions (C) 2004 Uri Wilensky ;; See code for licensing and usage. ;; globals [ ] breed [ pixels ] breed [ sprites ] sprites-own [ spr-height spr-width spr-max-value spr-first-pixel spr-pixel-list spr-pixel-set spr-name spr-scale spr-trans-color ;; the transparent color ] pixels-own [ pix-x pix-y pix-r ; original r value pix-g ; original b value pix-b ; original g value pix-c ; original color value pix-spr-id ] to import-sprite ask new-sprite [ sprite-import-ppm sprite-file-name ] clear-output output-print "Sprites:" ask sprites [ output-print self ] end to spin-sprites no-display ask sprites [ home set heading heading + 5 + 5 * who mod 3 jump max-pxcor * .66 sprite-paint-nowrap ] every 1 / stamp-rate [ if stamp?[ stamp-sprites ]] display end to stamp-sprites ask sprites [ sprite-to-patches ] end ;; the sprite turtle is used to manage the pixel turtles to-report new-sprite let results nobody create-custom-sprites 1 [ set results self ] report results end ;; these procedures import a "plain" (ascii) PPM (Portable Pixel Map). ;; a sprite turtle is created to manage the pixel turtles. to sprite-import-ppm [ pathname ] ;; error trapping wrapper for sprite-import-ppm-contents ;; reports a turtle of breed sprite ;; USER-CHOOSE-FILE returns false if the user cancels, so this ;; check is here to handle that case, or if the path is blank if not is-string? pathname or pathname = "" [ stop ] carefully [ file-open (word "data/" pathname ) sprite-import-ppm-contents ] ;; error handler for "carefully" [ user-message (word pathname " is not a valid ASCII format PPM file: (" error-message ")." ) ] file-close end to sprite-to-patches ;; run by a sprite turtle ;; causes the pixel turtles to imprint their color into the pcolor of the patch. ;; if patches own ppix-r, ppix-g and ppix-b, will copy pix-r, -g, -b into the patch, too. ;; this preserves the original color values, rather than the "covertered" values ;; one might get using "extract-rgb" on the pcolor. ifelse is-patch-variable? "ppix-r" [ ask pixels with [ pix-spr-id = myself and not (transparent? and color = spr-trans-color-of myself )] [ set pcolor color run "set ppix-r pix-r set ppix-g pix-g set ppix-b pix-b" ] ] [ ask pixels with [ pix-spr-id = myself and not (transparent? and color = spr-trans-color-of myself )] [ set pcolor color ] ] end to-report is-patch-variable? [ varname-string ] let is-a-variable? false carefully [ ask patch 0 0 [ set is-a-variable? run-result "ppix-r = ppix-r" ] set is-a-variable? true ] [ set is-a-variable? false ] report is-a-variable? end to sprite-import-ppm-contents ;; check magic number if read-pnm-entry != "P3" [ user-message "This is not a valid ASCII format PPM file (magic number is not P3)" stop ] ;; assumes this code is run by a turtle. ;; any turtle that runs this code becomes a sprite set breed sprites set shape "blank" set color white set heading 0 set spr-scale 1 set hidden? true ;; get width, height, and white value set spr-width read-from-string read-pnm-entry set spr-height read-from-string read-pnm-entry set spr-max-value read-from-string read-pnm-entry let max-value spr-max-value ;; adjust x and y to new origin. ;; sprite x y is origin let x-offset int ( spr-width / -2 ) let y-offset int ( spr-height / 2 ) ;; read the actual pixel values ;; create pixel turtles, assign to sprite let x 0 let y 0 while [y < spr-height] [ set x 0 while [x < spr-width] [ ;; convert from sprite coordinates to relative patch-at coordinates ;; (in patch-at coordinates, the origin is in the center, ;; not the top left, and y coordinates increase going up, ;; not going down) hatch 1 [ set breed pixels set heading 0 set shape "square-pixel" ;; read R G B values from file set pix-r ( read-from-string read-pnm-entry ) / max-value set pix-g ( read-from-string read-pnm-entry ) / max-value set pix-b ( read-from-string read-pnm-entry ) / max-value ;; set location set pix-x x-offset + x set pix-y y-offset - y ;; assign to sprite turtle set pix-spr-id myself set pix-c rgb pix-r pix-g pix-b set color pix-c setxy pix-x pix-y set hidden? true ] set x x + 1 ] set y y + 1 ] ;; assign pixel agentset to sprite set spr-pixel-set pixels with [ pix-spr-id = myself ] set spr-trans-color 9.9 end to hide-pixel ifelse (transparent? and (color = spr-trans-color-of pix-spr-id) ) [ if not hidden? [ hide-turtle ] ] [ if hidden? [ show-turtle ] ] end to hide-pixel-nowrap [ wrapped? ] ifelse wrapped? or (transparent? and (color = spr-trans-color-of pix-spr-id) ) [ if not hidden? [ hide-turtle ] ] [ if hidden? [ show-turtle ] ] end to sprite-paint let ox xcor let oy ycor ask spr-pixel-set [ setxy ( ox + pix-x ) ( oy + pix-y ) hide-pixel ] end to sprite-paint-nowrap ifelse xcor + .5 * spr-width <= max-pxcor and xcor - .5 * spr-width >= min-pxcor and ycor + .5 * spr-height <= max-pycor and ycor - .5 * spr-height >= min-pycor [ sprite-paint ] [ let ox xcor let oy ycor ask spr-pixel-set [ let new-x ox + pix-x let new-y oy + pix-y setxy new-x new-y hide-pixel-nowrap (new-x != xcor or new-y != ycor) ] ] end to sprite-paint-scale [ amount pix-amount ] ;; amount adjusts pixel spacing ;; pix-amount adjusts relative pixel size set spr-scale amount let ox xcor let oy ycor ask spr-pixel-set [ let new-x ox + pix-x * amount let new-y oy + pix-y * amount set hidden? off-screen? new-x new-y setxy new-x new-y set size amount * pix-amount ] end to sprites-reset ask sprites [ sprite-paint-scale 1 1 ] ask pixels [ set heading 0 ] end to-report on-screen? [ x y ] report (abs x <= max-pxcor or abs y <= max-pycor) end to-report off-screen? [ x y ] report (abs x > max-pxcor or abs y > max-pycor) end to sprite-hide hide-turtle ask spr-pixel-set [ hide-turtle ] end to sprite-show show-turtle ask spr-pixel-set [ show-turtle ] end to sprite-die ask spr-pixel-set [ die ] die end to-report pix-real-x report (xcor-of pix-spr-id) + pix-x end to-report pix-real-y report (ycor-of pix-spr-id) + pix-y end ;; reads the next entry in a "plain" (ascii) pnm file ;; i.e. pgm P2, ppm P3 ;; - an error occurs if there are no more entries ;; - entries are separated by arbitrary whitespace ;; - characters on a line after "#" are comments to-report read-pnm-entry ;; get next character let c file-read-characters 1 ;; ignore leading whitespace while [whitespace-char? c] [ set c file-read-characters 1 ] ;; skip comments if c = "#" [ ;; first skip the comment itself while [c != "\n" and c != "\r"] [ set c file-read-characters 1 ] ;; then skip linefeeds and/or newlines at end of comment while [c = "\n" or c = "\r"] [ set c file-read-characters 1 ] ] ;; read the entry let str "" while [not whitespace-char? c] [ set str str + c set c file-read-characters 1 ] report str end ;; reports true if c is a single whitespace character to-report whitespace-char? [c] report c = " " or c = "\t" or ;; tab c = "\n" or ;; newline c = "\r" ;; linefeed end ; (C) 2004 James Steiner, Uri Wilensky. ; This code may be freely copied, distributed, ; altered, or otherwise used by anyone for any legal purpose. ; ; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ; OWNERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. @#$#@#$#@ GRAPHICS-WINDOW 197 10 712 546 50 50 5.0 1 10 1 1 1 0 1 1 1 -50 50 -50 50 CC-WINDOW 5 570 721 665 Command Center 0 TEXTBOX 15 59 105 77 Import BUTTON 17 174 169 207 import ppm into turtles import-sprite NIL 1 T OBSERVER NIL NIL BUTTON 64 212 127 245 demo spin-sprites T 1 T OBSERVER NIL NIL BUTTON 50 10 125 43 NIL clear-all NIL 1 T OBSERVER T NIL OUTPUT 13 395 194 556 SWITCH 35 273 162 306 transparent? transparent? 1 1 -1000 SWITCH 45 309 148 342 stamp? stamp? 1 1 -1000 SLIDER 44 345 150 378 stamp-rate stamp-rate 1 10 3 1 1 NIL CHOOSER 22 95 160 140 sprite-file-name sprite-file-name "z-ricciolina.ppm" "z-vtricorn.ppm" "z-vcolore.ppm" 2 @#$#@#$#@ TO DO ----- Needs more comments in the code. WAAAAAY more. WHAT IS IT? ----------- This code example is a heavy modification of the PPM image import example from the models library. This code example shows how to import and manipulate bitmap images with NetLogo. It reads ASCII format PPM (Portable GrayMap) files. PPM, along with its cousins PGM and PPM, is a very simple format for bitmap images. HOW IT WORKS ------------ A PGM file is a text file with a simple header followed by pixel values. Values are separated by whitespace. Anything on a line after a "#" is a comment. Here is an example PGM file: P2 # magic number 24 7 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 15 15 15 0 0 3 0 0 0 0 0 7 0 0 7 0 0 11 0 0 0 0 0 15 0 0 15 0 0 3 0 0 0 0 0 7 0 0 7 0 0 11 0 11 11 0 0 15 0 0 15 0 0 3 0 0 0 0 0 7 0 0 7 0 0 11 0 0 0 0 0 15 0 0 15 0 0 3 3 3 3 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 15 15 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 The first value is a "magic number" that specifies the format of the file: "P2" indicates a gray-scale file with ASCII values. (This model can only read "P2" PGM files.) The second and third values are the image's X and Y dimensions. The fourth value is the highest gray value which can appear -- the value which indicates white. The remainder of the file is a list of gray-scale values, ranging from 0 to the white value and separated by whitespace. Any text after a "#" is a comment. In the example, one line of the file corresponds to one row of the image, but that isn't a requirement. The model uses NetLogo's file I/O commands to read the PPM file. First a new sprite is created and saved in a variable. Then the sprite turtle is told to load in an image file. When it does so, it creates a pixel turtle for every pixel in the image. The pixel turtles read in the RGB values for their pixel. To move a sprite, you move the sprite turtle, then issue the sprite-paint command. This makes all the pixel turtles for that sprite move to the correct location. I have provided normal and no-wrap versions of both. Both paint commands support transparancy. In this demp, the transparent color is always white. You can implement any scheme you like to set the transparant color. For example, the lower-left pixel defines the trans. color. Or perhaps you may wish to load in a another PPM or PGM file to use as a trans mask. That's up to you! HOW TO USE IT ------------- Use the IMPORT PPM INTO TURTLES button to import a sample PPM file. Click DEMO to show the sprite, and start it moving around. You can import more than one image. The transparent? switch turns transparency on and off. The stamp? switch makes the sprites periodically stamp their colors into the patches. THINGS TO TRY ------------- Try importing one of your own images. You'll have to convert the image beforehand to ASCII format PPM. There are many applications, both free and commercial, that convert image files to PPM. For example, XnView is a free, multi-platform graphics converter available at http://www.xnview.com. If you use XnView, be sure to check the "ASCII" checkbox when saving PPM files. Once you have a PPM file ready, you may wish to edit the graphics window to be large enough to accomodate your image. If you don't do that, the image will get cut off. When you're editing the graphics window, remember that "screen edge x" and "screen edge y" are the distance from the center of the screen to the edge, so you should enter values that are half of one less than the size of your image. For example, entering 250 and 250 will produce a world 501x501 patches in size. (The current version of NetLogo only supports odd world dimensions, not even.) It's OK if the graphics window is a little bit too big for your image. Note that in the current version of NetLogo is not possible to set screen-edge-x and screen-edge-y from code; they can only be set manually, by the user. Note that it is possible to scale the painting of the sprite. The sprite-paint-scale command shows one way to do that. CREDITS AND REFERENCES ---------------------- For more information on the PGM format, see the Linux manual page for "pgm", available at http://techpubs.sgi.com/library/tpl/cgi-bin/getdoc.cgi?coll=linux&db=man&fname=/usr/share/catman/man5/pgm.5.html @#$#@#$#@ default true 0 Polygon -7500403 true true 150 5 40 250 150 205 260 250 link true 0 Line -7500403 true 150 0 150 300 link direction true 0 Line -7500403 true 150 150 30 225 Line -7500403 true 150 150 270 225 airplane true 0 Polygon -7500403 true true 150 0 135 15 120 60 120 105 15 165 15 195 120 180 135 240 105 270 120 285 150 270 180 285 210 270 165 240 180 180 285 195 285 165 180 105 180 60 165 15 arrow true 0 Polygon -7500403 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150 blank false 0 box false 0 Polygon -7500403 true true 150 285 285 225 285 75 150 135 Polygon -7500403 true true 150 135 15 75 150 15 285 75 Polygon -7500403 true true 15 75 15 225 150 285 150 135 Line -16777216 false 150 285 150 135 Line -16777216 false 150 135 15 75 Line -16777216 false 150 135 285 75 bug true 0 Circle -7500403 true true 96 182 108 Circle -7500403 true true 110 127 80 Circle -7500403 true true 110 75 80 Line -7500403 true 150 100 80 30 Line -7500403 true 150 100 220 30 butterfly true 0 Polygon -7500403 true true 150 165 209 199 225 225 225 255 195 270 165 255 150 240 Polygon -7500403 true true 150 165 89 198 75 225 75 255 105 270 135 255 150 240 Polygon -7500403 true true 139 148 100 105 55 90 25 90 10 105 10 135 25 180 40 195 85 194 139 163 Polygon -7500403 true true 162 150 200 105 245 90 275 90 290 105 290 135 275 180 260 195 215 195 162 165 Polygon -16777216 true false 150 255 135 225 120 150 135 120 150 105 165 120 180 150 165 225 Circle -16777216 true false 135 90 30 Line -16777216 false 150 105 195 60 Line -16777216 false 150 105 105 60 car false 0 Polygon -7500403 true true 300 180 279 164 261 144 240 135 226 132 213 106 203 84 185 63 159 50 135 50 75 60 0 150 0 165 0 225 300 225 300 180 Circle -16777216 true false 180 180 90 Circle -16777216 true false 30 180 90 Polygon -16777216 true false 162 80 132 78 134 135 209 135 194 105 189 96 180 89 Circle -7500403 true true 47 195 58 Circle -7500403 true true 195 195 58 circle false 0 Circle -7500403 true true 30 30 240 circle 2 false 0 Circle -7500403 true true 16 16 270 Circle -16777216 true false 46 46 210 cow false 0 Polygon -7500403 true true 200 193 197 249 179 249 177 196 166 187 140 189 93 191 78 179 72 211 49 209 48 181 37 149 25 120 25 89 45 72 103 84 179 75 198 76 252 64 272 81 293 103 285 121 255 121 242 118 224 167 Polygon -7500403 true true 73 210 86 251 62 249 48 208 Polygon -7500403 true true 25 114 16 195 9 204 23 213 25 200 39 123 face happy false 0 Circle -7500403 true true 8 8 285 Circle -16777216 true false 60 75 60 Circle -16777216 true false 180 75 60 Polygon -16777216 true false 150 255 90 239 62 213 47 191 67 179 90 203 109 218 150 225 192 218 210 203 227 181 251 194 236 217 212 240 face neutral false 0 Circle -7500403 true true 8 7 285 Circle -16777216 true false 60 75 60 Circle -16777216 true false 180 75 60 Rectangle -16777216 true false 60 195 240 225 face sad false 0 Circle -7500403 true true 8 8 285 Circle -16777216 true false 60 75 60 Circle -16777216 true false 180 75 60 Polygon -16777216 true false 150 168 90 184 62 210 47 232 67 244 90 220 109 205 150 198 192 205 210 220 227 242 251 229 236 206 212 183 fish false 0 Polygon -1 true false 44 131 21 87 15 86 0 120 15 150 0 180 13 214 20 212 45 166 Polygon -1 true false 135 195 119 235 95 218 76 210 46 204 60 165 Polygon -1 true false 75 45 83 77 71 103 86 114 166 78 135 60 Polygon -7500403 true true 30 136 151 77 226 81 280 119 292 146 292 160 287 170 270 195 195 210 151 212 30 166 Circle -16777216 true false 215 106 30 flag false 0 Rectangle -7500403 true true 60 15 75 300 Polygon -7500403 true true 90 150 270 90 90 30 Line -7500403 true 75 135 90 135 Line -7500403 true 75 45 90 45 flower false 0 Polygon -10899396 true false 135 120 165 165 180 210 180 240 150 300 165 300 195 240 195 195 165 135 Circle -7500403 true true 85 132 38 Circle -7500403 true true 130 147 38 Circle -7500403 true true 192 85 38 Circle -7500403 true true 85 40 38 Circle -7500403 true true 177 40 38 Circle -7500403 true true 177 132 38 Circle -7500403 true true 70 85 38 Circle -7500403 true true 130 25 38 Circle -7500403 true true 96 51 108 Circle -16777216 true false 113 68 74 Polygon -10899396 true false 189 233 219 188 249 173 279 188 234 218 Polygon -10899396 true false 180 255 150 210 105 210 75 240 135 240 house false 0 Rectangle -7500403 true true 45 120 255 285 Rectangle -16777216 true false 120 210 180 285 Polygon -7500403 true true 15 120 150 15 285 120 Line -16777216 false 30 120 270 120 leaf false 0 Polygon -7500403 true true 150 210 135 195 120 210 60 210 30 195 60 180 60 165 15 135 30 120 15 105 40 104 45 90 60 90 90 105 105 120 120 120 105 60 120 60 135 30 150 15 165 30 180 60 195 60 180 120 195 120 210 105 240 90 255 90 263 104 285 105 270 120 285 135 240 165 240 180 270 195 240 210 180 210 165 195 Polygon -7500403 true true 135 195 135 240 120 255 105 255 105 285 135 285 165 240 165 195 line true 0 Line -7500403 true 150 0 150 300 pentagon false 0 Polygon -7500403 true true 150 15 15 120 60 285 240 285 285 120 person false 0 Circle -7500403 true true 110 5 80 Polygon -7500403 true true 105 90 120 195 90 285 105 300 135 300 150 225 165 300 195 300 210 285 180 195 195 90 Rectangle -7500403 true true 127 79 172 94 Polygon -7500403 true true 195 90 240 150 225 180 165 105 Polygon -7500403 true true 105 90 60 150 75 180 135 105 plant false 0 Rectangle -7500403 true true 135 90 165 300 Polygon -7500403 true true 135 255 90 210 45 195 75 255 135 285 Polygon -7500403 true true 165 255 210 210 255 195 225 255 165 285 Polygon -7500403 true true 135 180 90 135 45 120 75 180 135 210 Polygon -7500403 true true 165 180 165 210 225 180 255 120 210 135 Polygon -7500403 true true 135 105 90 60 45 45 75 105 135 135 Polygon -7500403 true true 165 105 165 135 225 105 255 45 210 60 Polygon -7500403 true true 135 90 120 45 150 15 180 45 165 90 square false 0 Rectangle -7500403 true true 30 30 270 270 square 2 false 0 Rectangle -7500403 true true 30 30 270 270 Rectangle -16777216 true false 60 60 240 240 square-pixel false 0 Rectangle -7500403 true true 0 0 300 300 star false 0 Polygon -7500403 true true 60 270 150 0 240 270 15 105 285 105 Polygon -7500403 true true 75 120 105 210 195 210 225 120 150 75 target false 0 Circle -7500403 true true 0 0 300 Circle -16777216 true false 30 30 240 Circle -7500403 true true 60 60 180 Circle -16777216 true false 90 90 120 Circle -7500403 true true 120 120 60 tree false 0 Circle -7500403 true true 118 3 94 Rectangle -6459832 true false 120 195 180 300 Circle -7500403 true true 65 21 108 Circle -7500403 true true 116 41 127 Circle -7500403 true true 45 90 120 Circle -7500403 true true 104 74 152 triangle false 0 Polygon -7500403 true true 150 30 15 255 285 255 triangle 2 false 0 Polygon -7500403 true true 150 30 15 255 285 255 Polygon -16777216 true false 151 99 225 223 75 224 truck false 0 Rectangle -7500403 true true 4 45 195 187 Polygon -7500403 true true 296 193 296 150 259 134 244 104 208 104 207 194 Rectangle -1 true false 195 60 195 105 Polygon -16777216 true false 238 112 252 141 219 141 218 112 Circle -16777216 true false 234 174 42 Rectangle -7500403 true true 181 185 214 194 Circle -16777216 true false 144 174 42 Circle -16777216 true false 24 174 42 Circle -7500403 false true 24 174 42 Circle -7500403 false true 144 174 42 Circle -7500403 false true 234 174 42 turtle true 0 Polygon -10899396 true false 215 204 240 233 246 254 228 266 215 252 193 210 Polygon -10899396 true false 195 90 225 75 245 75 260 89 269 108 261 124 240 105 225 105 210 105 Polygon -10899396 true false 105 90 75 75 55 75 40 89 31 108 39 124 60 105 75 105 90 105 Polygon -10899396 true false 132 85 134 64 107 51 108 17 150 2 192 18 192 52 169 65 172 87 Polygon -10899396 true false 85 204 60 233 54 254 72 266 85 252 107 210 Polygon -7500403 true true 119 75 179 75 209 101 224 135 220 225 175 261 128 261 81 224 74 135 88 99 wheel false 0 Circle -7500403 true true 3 3 294 Circle -16777216 true false 30 30 240 Line -7500403 true 150 285 150 15 Line -7500403 true 15 150 285 150 Circle -7500403 true true 120 120 60 Line -7500403 true 216 40 79 269 Line -7500403 true 40 84 269 221 Line -7500403 true 40 216 269 79 Line -7500403 true 84 40 221 269 x false 0 Polygon -7500403 true true 270 75 225 30 30 225 75 270 Polygon -7500403 true true 30 75 75 30 270 225 225 270 @#$#@#$#@ NetLogo 3.1.1 @#$#@#$#@ import-pgm "sample.pgm" @#$#@#$#@ @#$#@#$#@ @#$#@#$#@