Browse Source

Add the post about Netpbm files

Getty Ritter 3 years ago
commit
d9c018a2ec

File diff suppressed because it is too large
+ 177 - 0
in-praise-of-pbm/POST.md


+ 30 - 0
in-praise-of-pbm/bricoleur

@@ -0,0 +1,30 @@
+(document "POST.md"
+
+  {
+    name "pbm"
+    dir "pbm"
+    cmd [ "wren main.wren" ]
+    expose (sections "main.wren")
+  }
+
+  {
+    name "pgm"
+    dir "pgm"
+    cmd [ "wren main.wren" ]
+    expose (sections "main.wren")
+  }
+
+  {
+    name "ppm"
+    dir "ppm"
+    cmd [ "wren main.wren" ]
+    expose (sections "main.wren")
+  }
+
+  {
+    name "rectangle"
+    dir "rectangles"
+    cmd [ "wren main.wren" ]
+    expose (sections "main.wren")
+  }
+)

+ 28 - 0
in-praise-of-pbm/pbm/main.wren

@@ -0,0 +1,28 @@
+// «init»
+var width = 3
+var height = 3
+var image = []
+
+for (y in 0...height) {
+  image.add(List.filled(width, 0))
+}
+// «end»
+
+// «checkers»
+for (x in 0...width) {
+  for (y in 0...height) {
+    image[y][x] = (x + y) % 2
+  }
+}
+// «end»
+
+// «print»
+System.print("P1")
+System.print("%(width) %(height)")
+for (y in 0...height) {
+  for (x in 0...width) {
+    System.write("%(image[y][x]) ")
+  }
+  System.print()
+}
+// «end»

+ 27 - 0
in-praise-of-pbm/pgm/main.wren

@@ -0,0 +1,27 @@
+var width = 12
+var height = 12
+var image = []
+
+for (y in 0...height) {
+  image.add(List.filled(width, 0))
+}
+
+// «checkers»
+for (x in 0...width) {
+  for (y in 0...height) {
+    image[y][x] = (x + y) % 4
+  }
+}
+// «end»
+
+// «print»
+System.print("P2") // the PGM header
+System.print("%(width) %(height)")
+System.print(3) // the maximum value which will appear
+for (y in 0...height) {
+  for (x in 0...width) {
+    System.write("%(image[y][x]) ")
+  }
+  System.print()
+}
+// «end»

+ 37 - 0
in-praise-of-pbm/ppm/main.wren

@@ -0,0 +1,37 @@
+// «init»
+var width = 24
+var height = 24
+var depth = 24
+var image = []
+
+for (y in 0...height) {
+  var row = []
+  for (x in 0...width) {
+    row.add({"r": 0, "g": 0, "b": 0})
+  }
+  image.add(row)
+}
+// «end»
+
+// «checkers»
+for (x in 0...width) {
+  for (y in 0...height) {
+    image[y][x]["r"] = ((x / width) * depth).floor
+    image[y][x]["g"] = ((y / height) * depth).floor
+    image[y][x]["b"] = ((x + y) % 2) * depth
+  }
+}
+// «end»
+
+// «print»
+System.print("P3")
+System.print("%(width) %(height)")
+System.print(depth)
+for (y in 0...height) {
+  for (x in 0...width) {
+    var p = image[y][x]
+    System.write("%(p["r"]) %(p["g"]) %(p["b"]) ")
+  }
+  System.print()
+}
+// «end»

+ 69 - 0
in-praise-of-pbm/rectangles/main.wren

@@ -0,0 +1,69 @@
+import "random" for Random
+
+class GrayscaleImage {
+  construct new(width, height, depth) {
+    _width = width
+    _height = height
+    _depth = depth
+    _image = []
+    for (y in 0...height) {
+      _image.add(List.filled(width, 0))
+    }
+  }
+
+  pixel(x, y, shade) {
+    _image[y][x] = shade
+  }
+
+// «rectangle»
+  rectangle(x, y, width, height, shade) {
+    // the two horizontal lines
+    for (dx in 0..width) {
+      pixel(x + dx, y,          shade)
+      pixel(x + dx, y + height, shade)
+    }
+
+    // the two vertical lines
+    for (dy in 0..height) {
+      pixel(x,         y + dy, shade)
+      pixel(x + width, y + dy, shade)
+    }
+  }
+// «end»
+
+  showPGM() {
+    System.print("P2") // the PGM header
+    System.print("%(_width) %(_height)")
+    System.print(_depth) // the maximum value which will appear
+    for (y in 0..._height) {
+      for (x in 0..._width) {
+        System.write("%(_image[y][x]) ")
+      }
+      System.print()
+    }
+  }
+}
+
+var rand = Random.new()
+
+var width = 24
+var height = 24
+var depth = 8
+
+// «main»
+var image = GrayscaleImage.new(width, height, depth)
+// create up to 6 rectancles
+for (i in 0..rand.int(3, 6)) {
+  // choose the color from the depth
+  var color = rand.int(2, 8)
+  // choose top-left point randomly
+  var x = rand.int(0, width-3)
+  var y = rand.int(0, height-3)
+  // choose width and height from remaining
+  var w = rand.int(x+2, width) - x
+  var h = rand.int(y+2, height) - y
+  // draw the rectangle
+  image.rectangle(x, y, w, h, color)
+}
+image.showPGM()
+// «end»