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. 14 15set -e 16 17GENERATE_RAW=raw.raw 18# put jpg 19# bash generate_raw.sh <width> <height> 20 21declare -A HEADER_TYPE=( 22 [NONE]=0 23 [RAW]=1 24 [COMPRESSED]=2 25) 26 27echo_int32_t_to_bin() 28{ 29 local int32_t=$1 30 31 local big_endian=$(printf "%08x" $int32_t) 32 local little_endian=${big_endian:6:2}${big_endian:4:2}${big_endian:2:2}${big_endian:0:2} 33 echo "0: $little_endian" | xxd -r 34} 35 36get_file_size() 37{ 38 local filename=$1 39 40 du -b $filename | awk '{print $1}' 41} 42 43echo_file_part() 44{ 45 local filename=$1 46 local position=$2 47 local length=$3 48 49 xxd -l $length -s $position -o $((-$position)) $filename | xxd -r 50} 51 52main() 53{ 54 local width=$1 55 local height=$2 56 ls * | while read filename; do 57 # check mime type 58 file $filename | grep "image" >/dev/null 2>&1 59 if [ $? != 0 ]; then 60 continue 61 fi 62 63 # generate raw data 64 convert $filename $filename.rgba 65 xxd -c 1 $filename.rgba > $filename.xxd 66 ofilename=$filename.rgba 67 68 if [ "$last_filename" = "" ]; then 69 # first image 70 header_type=${HEADER_TYPE["RAW"]} 71 position=0 72 length="$(get_file_size $ofilename)" 73 else 74 # damage range 75 result="$(diff -y $filename.xxd $last_filename.xxd | grep " |$(echo -e "\x09")")" 76 if [ "$result" = "" ]; then 77 header_type=${HEADER_TYPE["NONE"]} 78 position=0 79 length=0 80 else 81 header_type=${HEADER_TYPE["RAW"]} 82 position="$(printf "%d\n" 0x$(echo "$result" | head -1 | awk '{print $1}' | cut -d: -f1))" 83 length="$(printf "%d\n" 0x$(echo "$result" | tail -1 | awk '{print $1}' | cut -d: -f1))" 84 ((length -= ${position})) 85 ((length = $length / 4 * 4)) 86 fi 87 fi 88 89 # compress 90 clen=$length 91 if [ "$header_type" = "${HEADER_TYPE["RAW"]}" ]; then 92 header_type=${HEADER_TYPE["COMPRESSED"]} 93 ofilename=$filename.compress 94 echo_file_part $filename.rgba $position $length | zlib-flate -compress=9 > $ofilename 95 clen=$(get_file_size $ofilename) 96 else 97 if [ "$header_type" = "${HEADER_TYPE["NONE"]}" ]; then 98 clen=0 99 fi 100 fi 101 102 if [ "$last_filename" = "" ]; then 103 echo -en "RAW.dif2" > $GENERATE_RAW 104 echo_int32_t_to_bin $width >> $GENERATE_RAW 105 echo_int32_t_to_bin $height >> $GENERATE_RAW 106 fi 107 echo_int32_t_to_bin $header_type >> $GENERATE_RAW 108 echo_int32_t_to_bin $position >> $GENERATE_RAW 109 echo_int32_t_to_bin $length >> $GENERATE_RAW 110 echo_int32_t_to_bin $clen >> $GENERATE_RAW 111 echo_file_part $ofilename 0 $clen >> $GENERATE_RAW 112 113 # for BUS_ADRALN 114 (( align = $clen - $clen / 4 * 4 )) 115 [ "$align" != "0" ] && (( align = 4 - $align )) 116 [ "$align" != "0" ] && echo -en "\x00" >> $GENERATE_RAW && (( align-- )) 117 [ "$align" != "0" ] && echo -en "\x00" >> $GENERATE_RAW && (( align-- )) 118 [ "$align" != "0" ] && echo -en "\x00" >> $GENERATE_RAW && (( align-- )) 119 120 echo $filename $header_type $position $length $clen 121 122 last_filename=$filename 123 done 124 125 rm -f *.rgba 126 rm -f *.xxd 127 rm -f *.compress 128 return 0 129} 130 131main $* 132exit $? 133