<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def calculate_preimages(tx_obj, inputs_parameters):
 """Calculates preimages for provided transaction structure and input values. :param tx_obj: The transaction object used to calculate preimage from using a transaction digest algorithm, such as BIP-143 for Segwit inputs. This transaction object must hence have scriptCodes filled into the corresponding scriptSigs in the inputs. :type tx_obj: :object:`~bit.transaction.TxObj` :param inputs_parameters: A list of tuples with input index as integer, hash type as integer and a boolean flag to denote if the input is spending from a Segwit output. :type inputs_parameters: A `list` of `tuple` """


    # Tx object data:
    input_count = int_to_varint(len(tx_obj.TxIn))
    output_count = int_to_varint(len(tx_obj.TxOut))
    output_block = b''.join([bytes(o) for o in tx_obj.TxOut])

    hashPrevouts = double_sha256(b''.join([i.txid+i.txindex for i in tx_obj.TxIn]))
    hashSequence = double_sha256(b''.join([i.sequence for i in tx_obj.TxIn]))
    hashOutputs = double_sha256(output_block)

    preimages = []
    for input_index, hash_type, segwit_input in inputs_parameters:
        # We can only handle hashType == 1:
        if hash_type != HASH_TYPE:
            raise ValueError('Bit only support hashType of value 1.')
        # Calculate prehashes:
        if segwit_input:
            # BIP-143 preimage:
            hashed = sha256(
                tx_obj.version +
                hashPrevouts +
                hashSequence +
                tx_obj.TxIn[input_index].txid +
                tx_obj.TxIn[input_index].txindex +
                tx_obj.TxIn[input_index].script_sig_len +  # scriptCode length
                tx_obj.TxIn[input_index].script_sig +  # scriptCode (includes amount)
                tx_obj.TxIn[input_index].sequence +
                hashOutputs +
                tx_obj.locktime +
                hash_type
            )
        else:
            hashed = sha256(
                tx_obj.version +
                input_count +
                b''.join(ti.txid + ti.txindex + OP_0 + ti.sequence
                         for ti in islice(tx_obj.TxIn, input_index)) +
                tx_obj.TxIn[input_index].txid +
                tx_obj.TxIn[input_index].txindex +
                tx_obj.TxIn[input_index].script_sig_len +  # scriptCode length
                tx_obj.TxIn[input_index].script_sig +  # scriptCode
                tx_obj.TxIn[input_index].sequence +
                b''.join(ti.txid + ti.txindex + OP_0 + ti.sequence
                         for ti in islice(tx_obj.TxIn, input_index + 1, None)) +
                output_count +
                output_block +
                tx_obj.locktime +
                hash_type
            )
        preimages.append(hashed)
    return preimages