def decryptHenonImage(imageName):
    imageMatrix = cim.getImageMatrix(imageName)
    transformationMatrix = ghm.genTransformationMatrix(len(imageMatrix))

    henonDecryptedImage = []
    for i in range(len(imageMatrix)):
        row = []
        for j in range(len(imageMatrix)):
            try:
                row.append(imageMatrix[i][j] ^ transformationMatrix[i][j])
            except:
                row = [imageMatrix[i][j] ^ transformationMatrix[i][j]]

        try:
            henonDecryptedImage.append(row)
        except:
            henonDecryptedImage = [row]

    width = len(imageMatrix[0])
    height = len(imageMatrix)

    im = Image.new("L", (width, height))
    pix = im.load()
    for x in range(width):
        for y in range(height):
            pix[x, y] = henonDecryptedImage[x][y]
    im.save("HenonDecryptedImage.bmp", "BMP")
    #return henonDecryptedImage
    return os.path.abspath("HenonDecryptedImage.bmp")
def dH(filePath):
    start_time = time.time()
    print("\nDe Henon")
    print(filePath)
    fileName = os.path.split(filePath)[-1]
    fileName = fileName.split(".")[0]
    # path reader
    imageMatrix = cim.getImageMatrix(filePath)
    # process
    resImage = hD.decryptHenonImage(imageMatrix)
    # saver
    savepath = IMSaver(resImage, fileName + "_dH")
    print('Henon ended {}\n'.format(time.time() - start_time))
    return savepath
def eH(filePath):
    start_time = time.time()
    print("\nEn Henon")
    print(filePath)
    fileName = os.path.split(filePath)[-1]
    fileName = fileName.split(".")[0]
    # path reader
    imageMatrix = cim.getImageMatrix(filePath)
    # process
    resImage = iT.pixelManipulation(imageMatrix)
    # saver
    savepath = IMSaver(resImage, fileName + "_eH")
    print('Henon ended {}\n'.format(time.time() - start_time))
    return savepath
def dA(filePath):
    start_time = time.time()
    print("\nEn Arnold")
    fileName = os.path.split(filePath)[-1]
    fileName = fileName.split(".")[0]
    # path reader
    imageMatrix = cim.getImageMatrix(filePath)
    # process
    width = len(imageMatrix)
    height = len(imageMatrix[0])
    sp = square_part(height, width)
    parted = partitor(imageMatrix, sp)
    # part_saver(parted, fileName)
    arnoldList = arnoldDeBuilder(parted)
    tmp = part_joiner(arnoldList, sp)
    # saver
    savepath = IMSaver(tmp, fileName + "_dA")
    print('Arnold ended {}\n'.format(time.time() - start_time))
    return savepath
def dAH(filePath):
    start_time = time.time()
    print("\nDe Arnold-Henon")
    print(filePath)
    fileName = os.path.split(filePath)[-1]
    fileName = fileName.split(".")[0]
    # path reader
    imageMatrix = cim.getImageMatrix(filePath)
    # process: Arnold
    width = len(imageMatrix)
    height = len(imageMatrix[0])
    sp = square_part(height, width)
    parted = partitor(imageMatrix, sp)
    # part_saver(parted, fileName)
    arnoldList = arnoldDeBuilder(parted)
    tmp = part_joiner(arnoldList, sp)
    # process: Henon
    resImage = hD.decryptHenonImage(tmp)
    # saver
    savepath = IMSaver(resImage, fileName + "_dAH")
    print('Arnold-Henon Decrypt ended {}\n'.format(time.time() - start_time))
    return savepath
def eHA(filePath):
    start_time = time.time()
    print("\nEn Henon-Arnold")
    print(filePath)
    fileName = os.path.split(filePath)[-1]
    fileName = fileName.split(".")[0]
    # path reader
    imageMatrix = cim.getImageMatrix(filePath)
    # process: Henon
    resImage = iT.pixelManipulation(imageMatrix)
    # process: Arnold
    width = len(resImage)
    height = len(resImage[0])
    sp = square_part(height, width)
    parted = partitor(resImage, sp)
    # part_saver(parted, fileName)
    arnoldList = arnoldEnBuilder(parted)
    tmp = part_joiner(arnoldList, sp)
    # saver
    savepath = IMSaver(tmp, fileName + "_eHA")
    print('Henon-Arnold ended {}\n'.format(time.time() - start_time))
    return savepath
def decryptArnoldImage(width, height, numberOfIterations, modN, imageName):

    im = Image.open(imageName)
    image_size = im.size
    width = image_size[0]
    height = image_size[1]

    mapList = gam.driverProgram(width, height, numberOfIterations, modN)
    print(mapList)
    henonDecryptedImage = cim.getImageMatrix(imageName)
    print(henonDecryptedImage)
    arnoldDecryptedImage = []

    for i in range(width):
        row = []
        for j in range(height):
            try:
                row.append((0))
            except:
                row = [(0)]
        try:
            arnoldDecryptedImage.append(row)
        except:
            arnoldDecryptedImage = [row]

    for map in mapList:
        for key, value in map.items():
            print(key[0], key[1], value[0], value[1])
            arnoldDecryptedImage[key[0]][key[1]] = (henonDecryptedImage[int(
                value[0])][int(value[1])])

    im = Image.new("L", (width, height))
    pix = im.load()
    for x in range(width):
        for y in range(height):
            pix[x, y] = arnoldDecryptedImage[x][y]
    im.save("ArnoldDecryptedImage.bmp", "BMP")
    return os.path.abspath("ArnoldDecryptedImage.bmp")