summaryrefslogtreecommitdiff
path: root/scripts/newbitmaps/images/build_images
blob: 269d5e6c5a6a3c046ef845c4593dcf16c7aec265 (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
137
#!/bin/sh
# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Prepares image resources into output folder.

# Composition settings
BACKGROUND_COLOR=white

# The only file that uses different scaling parameter.
BACKGROUND_IMAGE=Background_white.bmp

die() {
  echo "ERROR: $*" >&2
  exit 1
}

convert_to_bmp3() {
  local input="$1"
  local folder="$2"
  local param="$3"
  local output="$(basename "$input")"

  # Always output as .bmp
  output="${output%.*}.bmp"
  mkdir -p "$folder"

  # When input has Alpha channel, we need to fill the background properly
  # otherwise ImageMagick will fill it with black.  The operation (-flatten) is
  # so slow so we only want to do that if the input source really has
  # transparency.
  if [ "$#" -gt 3 ]; then
    flatten="$4"
  else
    # Auto-detect
    if [ "$(identify -format "%A" "$input")" = "True" ]; then
      flatten="-background $BACKGROUND_COLOR -flatten"
    else
      flatten=""
    fi
  fi

  echo "$input -> $folder/$output $flatten"
  convert "$input" $flatten \
    -compress none -alpha off -colors 256 \
    $param "BMP3:$folder/$output"

  # ImageMagic quantization may choose arbitrary color depth, even if we assign
  # -depth or -colors; so a single-color background may become 1 bit per pixel
  # after convertion. To workaround that, we use Python Image Library which
  # always generates 8bpp BMP file.
  # TODO(hungte) Find a better way to decide if PIL is required. Unfortunately,
  # ImageMagic identify "%z" is not always what we're looking for...
  local fn="$folder/$output"
  local param="'P', dither=None, palette=Image.ADAPTIVE"
  python -c "import Image; Image.open('$fn').convert($param).save('$fn')"
}

main() {
  local profile="$1"
  local output="out_$1"
  local scale_param="" background_scale_param=""
  local base locale X

  # Currently we use image resources originally designed for 1366x768, and
  # re-scale to different aspects on demand.
  case "$profile" in
    x86 )
      # The image size with x86 UEFI BIOS (also applies to coreboot) is always
      # 800x600, which is stretched to fill the entire screen. With previous
      # devices the physical screen size was either 1280x800 (16:10) or 1366x768
      # (16:9).  There's not a lot of difference between those, so let's just
      # assume 16:9 for future platforms to make things simpler.
      scale_param="-scale 59%x78%"
      background_scale_param="-scale 800x600!"
      ;;
    arm )
      # On ARM platforms, we need to provide a bitmap with full size.
      # TODO(hungte) Support more profiles, ex 1280x800.
      true
      ;;
    * )
      die "Sorry, unknown profile $profile."
  esac

  # Prepare output folder
  mkdir -p "$output"

  # Prepare images in current folder
  # TODO(hungte) Deprecate arrow*.bmp by markup ◀ and ▶, and
  # Url.bmp by <span foreground="blue">http://</span>.
  for X in $(ls *.bmp *.png assets/*.png); do
    if [ "$X" = "$BACKGROUND_IMAGE" ]; then
      convert_to_bmp3 "$X" "$output" "$background_scale_param"
    else
      convert_to_bmp3 "$X" "$output" "$scale_param"
    fi
  done

  # Prepares strings and localized images. All these images were rendered by
  # pango-view and should not have transparency, so we specify flatten="" to
  # speed up.
  echo "Preparing common strings..."
  base="../strings"
  for X in $base/*.png; do
    convert_to_bmp3 "$X" "$output" "$scale_param" ""
  done
  echo "Preparing localized messages... $LOCALES"
  base="../strings/localized_text"
  if [ -z "$LOCALES" ]; then
    # Collect all locales
    for X in $(cd $base; ls); do
      if [ -d "$base/$X" ]; then
        LOCALES="${LOCALES}${X} "
      fi
    done
    echo "Found locales: $LOCALES"
  fi
  for locale in $LOCALES; do
    # Prepare all locales.
    for X in $base/$locale/*.png; do
      convert_to_bmp3 "$X" "$output/locale/$locale" "$scale_param" ""
    done
  done

  # Prepare fonts
  base="../strings/font"
  for X in $base/*.png; do
    convert_to_bmp3 "$X" "$output/font" "$scale_param" ""
  done
  bmpblk_font --outfile "$output/hwid_fonts.bin" "$output"/font/*.bmp
}

set -e
main "$@"