Browse Source

Add ppm_to_array script

Converts ppm image data to C array of pixels.
Frans Bergman 5 years ago
parent
commit
e65112e4b1
1 changed files with 25 additions and 0 deletions
  1. 25 0
      .scripts/ppm_to_array.awk

+ 25 - 0
.scripts/ppm_to_array.awk

@@ -0,0 +1,25 @@
+#!/usr/bin/awk -f
+
+# Converts a ppm P3 file to a C-compatible array of the coordinates of all pixels
+# with an intensity lesser than THRESHOLD (0-255)
+
+BEGIN {
+    i = -3
+    THRESHOLD = 150
+}
+
+/^[0-9]+\ [0-9]+$/ {
+    width = $1
+}
+
+/^[0-9]+$/ {
+    color += $1
+    if (++i % 3 == 0) {     # Three rows define a pixel's color
+        pixel = i / 3
+        row = int(pixel / width)
+        column = pixel % width
+        if (color < 3 * THRESHOLD)
+            print "{" column "," row "},"
+        color = 0
+    }
+}