ppm_to_array.awk 515 B

12345678910111213141516171819202122232425
  1. #!/usr/bin/awk -f
  2. # Converts a ppm P3 file to a C-compatible array of the coordinates of all pixels
  3. # with an intensity lesser than THRESHOLD (0-255)
  4. BEGIN {
  5. i = -3
  6. THRESHOLD = 150
  7. }
  8. /^[0-9]+\ [0-9]+$/ {
  9. width = $1
  10. }
  11. /^[0-9]+$/ {
  12. color += $1
  13. if (++i % 3 == 0) { # Three rows define a pixel's color
  14. pixel = i / 3
  15. row = int(pixel / width)
  16. column = pixel % width
  17. if (color < 3 * THRESHOLD)
  18. print "{" column "," row "},"
  19. color = 0
  20. }
  21. }