1#!/bin/bash 2# Copyright (c) 2021 Huawei Device Co., Ltd. 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14set -e 15GENERATE_RAW=bootanimation.raw 16 17declare -A HEADER_TYPE=( 18 [NONE]=0 19 [RAW]=1 20 [COMPRESSED]=2 21) 22 23echo_int32_t_to_bin() 24{ 25 local int32_t=$1 26 27 local big_endian=$(printf "%08x" $int32_t) 28 local little_endian=${big_endian:6:2}${big_endian:4:2}${big_endian:2:2}${big_endian:0:2} 29 echo "0: $little_endian" | xxd -r 30} 31 32get_file_size() 33{ 34 local filename=$1 35 36 du -b $filename | awk '{print $1}' 37} 38 39echo_file_part() 40{ 41 local filename=$1 42 local position=$2 43 local length=$3 44 45 xxd -l $length -s $position -o $((-$position)) $filename | xxd -r 46} 47 48main() 49{ 50 ls * | while read filename; do 51 # check mime type 52 file $filename | grep "image" >/dev/null 2>&1 53 if [ $? != 0 ]; then 54 continue 55 fi 56 57 # generate raw data 58 convert $filename $filename.rgba 59 xxd -c 1 $filename.rgba > $filename.xxd 60 ofilename=$filename.rgba 61 62 if [ "$last_filename" = "" ]; then 63 # first image 64 header_type=${HEADER_TYPE["RAW"]} 65 position=0 66 length="$(get_file_size $ofilename)" 67 else 68 # damage range 69 result="$(diff -y $filename.xxd $last_filename.xxd | grep " |$(echo -e "\x09")")" 70 if [ "$result" = "" ]; then 71 header_type=${HEADER_TYPE["NONE"]} 72 position=0 73 length=0 74 else 75 header_type=${HEADER_TYPE["RAW"]} 76 position="$(printf "%d\n" 0x$(echo "$result" | head -1 | awk '{print $1}' | cut -d: -f1))" 77 length="$(printf "%d\n" 0x$(echo "$result" | tail -1 | awk '{print $1}' | cut -d: -f1))" 78 ((length -= ${position})) 79 ((length = $length / 4 * 4)) 80 fi 81 fi 82 83 # compress 84 clen=$length 85 if [ "$header_type" = "${HEADER_TYPE["RAW"]}" ]; then 86 header_type=${HEADER_TYPE["COMPRESSED"]} 87 ofilename=$filename.compress 88 echo_file_part $filename.rgba $position $length | zlib-flate -compress=9 > $ofilename 89 clen=$(get_file_size $ofilename) 90 else 91 if [ "$header_type" = "${HEADER_TYPE["NONE"]}" ]; then 92 clen=0 93 fi 94 fi 95 96 if [ "$last_filename" = "" ]; then 97 echo -en "RAW.diff" > $GENERATE_RAW 98 fi 99 echo_int32_t_to_bin $header_type >> $GENERATE_RAW 100 echo_int32_t_to_bin $position >> $GENERATE_RAW 101 echo_int32_t_to_bin $length >> $GENERATE_RAW 102 echo_int32_t_to_bin $clen >> $GENERATE_RAW 103 echo_file_part $ofilename 0 $clen >> $GENERATE_RAW 104 105 # for BUS_ADRALN 106 (( align = $clen - $clen / 4 * 4 )) 107 [ "$align" != "0" ] && (( align = 4 - $align )) 108 [ "$align" != "0" ] && echo -en "\x00" >> $GENERATE_RAW && (( align-- )) 109 [ "$align" != "0" ] && echo -en "\x00" >> $GENERATE_RAW && (( align-- )) 110 [ "$align" != "0" ] && echo -en "\x00" >> $GENERATE_RAW && (( align-- )) 111 112 echo $filename $header_type $position $length $clen 113 114 last_filename=$filename 115 done 116 117 rm -f *.rgba 118 rm -f *.xxd 119 rm -f *.compress 120 return 0 121} 122 123main $* 124exit $? 125