summaryrefslogtreecommitdiffstats
path: root/util/bin/make-realwallpaper
blob: 787112c3c170fb28cca259a2076b7c1a628acfb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/sh
set -euf

###### USAGE #####
#run in new directory(will be polluted with images
#just run ./make-realwallpaper

main() {
  # fetch source images in parallel
  fetch nightmap.jpg \
    http://awka.sourceforge.net/Night_le_huge.jpg &
  fetch daymap.png \
    http://www.nnvl.noaa.gov/images/globaldata/SnowIceCover_Daily.png &
  fetch cloud-layer.jpg \
    http://user.chol.com/~winxplanet/cloud_data/clouds_2048.jpg &
  wait

  check_type nightmap.jpg image
  check_type daymap.png image
  check_type cloud-layer.jpg image

  # downscale daymap to match nightmap
  needs_rebuild daymap-final.png \
    daymap.png \
    && convert daymap.png -scale $(image_size nightmap.jpg) daymap-final.png

  # create nightmap-fullsnow
  needs_rebuild nightmap-fullsnow.png \
    && convert \
        -size $(image_size nightmap.jpg) \
        'xc:#0a3b5c' nightmap-fullsnow.png

  # extract daymap-snowmask from daymap-fonal
  needs_rebuild daymap-snowmask.png \
    daymap-final.png \
    && convert daymap-final.png -threshold 95% daymap-snowmask.png

  # extract nightmap-lightmask from nightmap
  needs_rebuild nightmap-lightmask.png \
    nightmap.jpg \
    && convert nightmap.jpg -threshold 25% nightmap-lightmask.png

  # create layers
  make_layer nightmap-snowlayer.png nightmap-fullsnow.png daymap-snowmask.png
  make_layer nightmap-lightlayer.png nightmap.jpg nightmap-lightmask.png

  # apply layers
  flatten nightmap-lightsnowlayer.png \
    nightmap-lightlayer.png \
    nightmap-snowlayer.png

  flatten nightmap-final.png \
    nightmap-lightsnowlayer.png \
    nightmap.jpg

  # create xplanet output
  needs_rebuild xplanet.config \
    && cat >xplanet.config <<EOF
[earth]
"Earth"
map=daymap-final.png
night_map=nightmap-final.png
cloud_map=cloud-layer.jpg
satellite_file=krebs.sat
marker_file=krebs.mar
EOF
  # rebuild every time to update shadow
  xplanet --num_times 1 --geometry 1466x1200 \
    --output xplanet-output.png --projection merc -config xplanet.config

  # trim xplanet output
  needs_rebuild realwallpaper.png \
    xplanet-output.png \
    && convert xplanet-output.png -crop 1366x768+100+160 realwallpaper.png
}

# usage: getimg FILENAME URL
fetch() {
  echo "fetch $1"
  curl -sS -z "$1" -o "$1" "$2"
}

# usage: check_type FILENAME TYPE
check_type() {
  if ! file -ib "$1" | grep -q "^$2/"; then
    echo "$1 is not of type $2" >&2
    rm "$1"
    return 1
  fi
}

# usage: image_size FILENAME
image_size() {
  identify "$1" | awk '{print$3}'
}

# usage: make_mask DST SRC MASK 
make_layer() {
  if needs_rebuild "$@"; then
    echo "make $1 (apply mask)" >&2
    convert "$2" "$3" -alpha off -compose copy_opacity -composite "$1"
  fi
}

# usage: flatten DST HILAYER LOLAYER
flatten() {
  if needs_rebuild "$@"; then
    echo "make $1 (flatten)" >&2
    composite "$2" "$3" "$1"
  fi
}

# usage: needs_rebuild DST SRC...
needs_rebuild() {
  a="$1"
  shift
  if ! test -e "$a"; then
    #echo "  $a does not exist" >&2
    result=0
  else
    result=1
    for b; do
      if test "$b" -nt "$a"; then
        #echo "  $b is newer than $a" >&2
        result=0
      fi
    done
  fi
  #case $result in
  #  0) echo "$a needs rebuild" >&2;;
  #esac
  return $result
}


main "$@"