#!/usr/bin/python
#==============================================================================
#/** @file splash_logo_gen.py
#
# GENERAL DESCRIPTION
#  Packages a custom splash image bmp into a loadable splash partition image
#  Fastboot command:
#  fastboot flash splash <img>
#
#  Copyright (c) 2016 Qualcomm Technologies, Inc .
#  All Rights Reserved.
#  Confidential and Proprietary - Qualcomm Technologies, Inc.
#==============================================================================

#==============================================================================
# Copyright (c) 2013,2015, The Linux Foundation. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
#       copyright notice, this list of conditions and the following
#       disclaimer in the documentation and/or other materials provided
#       with the distribution.
#     * Neither the name of The Linux Foundation nor the names of its
#       contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
# ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#===========================================================================

import sys
import os

## make a image
def MakeLogoImage(out):
    outfile = open(out, "wb")
    filler = b'\0'*0x4000
    outfile.write(filler)
    body = GetBMPData()
    outfile.write(body[0])
    ##outfile.seek(0,0)
    ##outfile.seek(10485760,1)
    ##outfile.write(body[0])
    outfile.close()

    outfile = open(out, "a")
    filler = b'\0'*0x8000
    outfile.write(filler)
    body = GetBMPData2()
    outfile.write(body[0])
    ##outfile.seek(0,0)
    ##outfile.seek(10485760,1)
    ##outfile.write(body[0])
    outfile.close()

def GetBMPData2():
    global imgconv
    #infile = "logo.bmp" #default file name
    num = len(sys.argv)

    if num != 3:
        ShowUsage()
        sys.exit(1); # error arg

    infile = sys.argv[2]

    if os.access(infile, os.R_OK) != True:
        print("Error: Input file <%s> is not readable" % infile)
        ShowUsage()
        sys.exit(2); # error file
    data =[0]
    with open(infile, mode='rb') as imgfile:
        data[0] = imgfile.read()
    return data


## usage
def ShowUsage():
    print("**********************************************")
    print("\nUsage:\npython splash_logo_gen.py <image file>")
    print("\nSupported image formats:\n *  8-bit BMP\n * 24-bit BMP\n * 32-bit BMP")
    print("\n**********************************************")

## get BMP data from input file. if input is png convert to bmp
def GetBMPData():
    global imgconv
    #infile = "logo.bmp" #default file name
    num = len(sys.argv)

    if num != 3:
        ShowUsage()
        sys.exit(1); # error arg

    infile = sys.argv[1]

    if os.access(infile, os.R_OK) != True:
        print("Error: Input file <%s> is not readable" % infile)
        ShowUsage()
        sys.exit(2); # error file
    data =[0]
    with open(infile, mode='rb') as imgfile:
        data[0] = imgfile.read()
    return data

##main
if __name__ == "__main__":
    MakeLogoImage("spunvm.img")
