import random
import subprocess

def readImage(filename):
	with open(filename, "rb") as image:
		f = image.read()
		b = bytearray(f)
	return b

def getListOfRandNums(sampleSize=1, population=1):
		result = []
		index = []
		for i in range(sampleSize):
				result.append(random.choice([x for x in range(256)]))
				index.append(random.choice([x for x in range(population)]))
		return [index, result]

def mutateImage(image_byte_array, mutation):
	indexes = mutation[0]
	nums = mutation[1]
	for i in range(len(nums)):
		image_byte_array[indexes[i]] = nums[i]

	return image_byte_array

def saveMutatedImage(file_name, image_input):
	f = open(file_name, 'wb')
	f.write(bytearray(image_input))
	f.close()

def statsFile(file_name, error_message):
	file = open(file_name, 'a')
	file.write(error_message + "\r\n")
	file.close()

file = open("StatsFile.txt", "w")
file.close() # This clears previous data
occurances = []
stats_dict = {'1':0, '2':0, '3':0, '4':0, '5':0, '6':0, '7':0, '8':0}

for i in range(0,1): # execute the code 'i' number of times
	for j in range(0,100): # Create 'j' number of mutation images
		originalImage = readImage("cross.jpg")
		mutation = getListOfRandNums(sampleSize=1, population=len(originalImage))
		mutatedImage = mutateImage(originalImage, mutation)
		saveMutatedImage('imageinput.jpg', mutatedImage)
		output = subprocess.run(["./jpg2bmp", "imageinput.jpg", "test.bmp"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
		stderr = output.stderr.decode('utf-8')
		my_output_list = stderr.split(" ")
		for word in my_output_list :
			if word == "Bug":
				statsFile("StatsFile.txt", stderr)
				print("STDERR:", stderr)
				imageName = "Test-" + my_output_list[1][1] + ".jpg"
				occurances.append(my_output_list[1][1])
				saveMutatedImage(imageName, mutatedImage) 
	for key in stats_dict:
		stats_dict[key] += occurances.count(key)
print("Number of times each bug is triggered with ",(i + 1), " number of iteration(s): ", stats_dict)


b494cfc30c4f96e2baf79172c285b77795910efa
<mask token>


def getListOfRandNums(sampleSize=1, population=1):
    result = []
    index = []
    for i in range(sampleSize):
        result.append(random.choice([x for x in range(256)]))
        index.append(random.choice([x for x in range(population)]))
    return [index, result]


<mask token>


def saveMutatedImage(file_name, image_input):
    f = open(file_name, 'wb')
    f.write(bytearray(image_input))
    f.close()


def statsFile(file_name, error_message):
    file = open(file_name, 'a')
    file.write(error_message + '\r\n')
    file.close()


<mask token>

<mask token>


def readImage(filename):
    with open(filename, 'rb') as image:
        f = image.read()
        b = bytearray(f)
    return b


def getListOfRandNums(sampleSize=1, population=1):
    result = []
    index = []
    for i in range(sampleSize):
        result.append(random.choice([x for x in range(256)]))
        index.append(random.choice([x for x in range(population)]))
    return [index, result]


def mutateImage(image_byte_array, mutation):
    indexes = mutation[0]
    nums = mutation[1]
    for i in range(len(nums)):
        image_byte_array[indexes[i]] = nums[i]
    return image_byte_array


def saveMutatedImage(file_name, image_input):
    f = open(file_name, 'wb')
    f.write(bytearray(image_input))
    f.close()


def statsFile(file_name, error_message):
    file = open(file_name, 'a')
    file.write(error_message + '\r\n')
    file.close()


<mask token>

<mask token>


def readImage(filename):
    with open(filename, 'rb') as image:
        f = image.read()
        b = bytearray(f)
    return b


def getListOfRandNums(sampleSize=1, population=1):
    result = []
    index = []
    for i in range(sampleSize):
        result.append(random.choice([x for x in range(256)]))
        index.append(random.choice([x for x in range(population)]))
    return [index, result]


def mutateImage(image_byte_array, mutation):
    indexes = mutation[0]
    nums = mutation[1]
    for i in range(len(nums)):
        image_byte_array[indexes[i]] = nums[i]
    return image_byte_array


def saveMutatedImage(file_name, image_input):
    f = open(file_name, 'wb')
    f.write(bytearray(image_input))
    f.close()


def statsFile(file_name, error_message):
    file = open(file_name, 'a')
    file.write(error_message + '\r\n')
    file.close()


<mask token>
file.close()
<mask token>
for i in range(0, 1):
    for j in range(0, 100):
        originalImage = readImage('cross.jpg')
        mutation = getListOfRandNums(sampleSize=1, population=len(
            originalImage))
        mutatedImage = mutateImage(originalImage, mutation)
        saveMutatedImage('imageinput.jpg', mutatedImage)
        output = subprocess.run(['./jpg2bmp', 'imageinput.jpg', 'test.bmp'],
            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stderr = output.stderr.decode('utf-8')
        my_output_list = stderr.split(' ')
        for word in my_output_list:
            if word == 'Bug':
                statsFile('StatsFile.txt', stderr)
                print('STDERR:', stderr)
                imageName = 'Test-' + my_output_list[1][1] + '.jpg'
                occurances.append(my_output_list[1][1])
                saveMutatedImage(imageName, mutatedImage)
    for key in stats_dict:
        stats_dict[key] += occurances.count(key)
print('Number of times each bug is triggered with ', i + 1,
    ' number of iteration(s): ', stats_dict)

import random
import subprocess


def readImage(filename):
    with open(filename, 'rb') as image:
        f = image.read()
        b = bytearray(f)
    return b


def getListOfRandNums(sampleSize=1, population=1):
    result = []
    index = []
    for i in range(sampleSize):
        result.append(random.choice([x for x in range(256)]))
        index.append(random.choice([x for x in range(population)]))
    return [index, result]


def mutateImage(image_byte_array, mutation):
    indexes = mutation[0]
    nums = mutation[1]
    for i in range(len(nums)):
        image_byte_array[indexes[i]] = nums[i]
    return image_byte_array


def saveMutatedImage(file_name, image_input):
    f = open(file_name, 'wb')
    f.write(bytearray(image_input))
    f.close()


def statsFile(file_name, error_message):
    file = open(file_name, 'a')
    file.write(error_message + '\r\n')
    file.close()


file = open('StatsFile.txt', 'w')
file.close()
occurances = []
stats_dict = {'1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0, '8': 0}
for i in range(0, 1):
    for j in range(0, 100):
        originalImage = readImage('cross.jpg')
        mutation = getListOfRandNums(sampleSize=1, population=len(
            originalImage))
        mutatedImage = mutateImage(originalImage, mutation)
        saveMutatedImage('imageinput.jpg', mutatedImage)
        output = subprocess.run(['./jpg2bmp', 'imageinput.jpg', 'test.bmp'],
            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stderr = output.stderr.decode('utf-8')
        my_output_list = stderr.split(' ')
        for word in my_output_list:
            if word == 'Bug':
                statsFile('StatsFile.txt', stderr)
                print('STDERR:', stderr)
                imageName = 'Test-' + my_output_list[1][1] + '.jpg'
                occurances.append(my_output_list[1][1])
                saveMutatedImage(imageName, mutatedImage)
    for key in stats_dict:
        stats_dict[key] += occurances.count(key)
print('Number of times each bug is triggered with ', i + 1,
    ' number of iteration(s): ', stats_dict)

import random
import subprocess

def readImage(filename):
	with open(filename, "rb") as image:
		f = image.read()
		b = bytearray(f)
	return b

def getListOfRandNums(sampleSize=1, population=1):
		result = []
		index = []
		for i in range(sampleSize):
				result.append(random.choice([x for x in range(256)]))
				index.append(random.choice([x for x in range(population)]))
		return [index, result]

def mutateImage(image_byte_array, mutation):
	indexes = mutation[0]
	nums = mutation[1]
	for i in range(len(nums)):
		image_byte_array[indexes[i]] = nums[i]

	return image_byte_array

def saveMutatedImage(file_name, image_input):
	f = open(file_name, 'wb')
	f.write(bytearray(image_input))
	f.close()

def statsFile(file_name, error_message):
	file = open(file_name, 'a')
	file.write(error_message + "\r\n")
	file.close()

file = open("StatsFile.txt", "w")
file.close() # This clears previous data
occurances = []
stats_dict = {'1':0, '2':0, '3':0, '4':0, '5':0, '6':0, '7':0, '8':0}

for i in range(0,1): # execute the code 'i' number of times
	for j in range(0,100): # Create 'j' number of mutation images
		originalImage = readImage("cross.jpg")
		mutation = getListOfRandNums(sampleSize=1, population=len(originalImage))
		mutatedImage = mutateImage(originalImage, mutation)
		saveMutatedImage('imageinput.jpg', mutatedImage)
		output = subprocess.run(["./jpg2bmp", "imageinput.jpg", "test.bmp"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
		stderr = output.stderr.decode('utf-8')
		my_output_list = stderr.split(" ")
		for word in my_output_list :
			if word == "Bug":
				statsFile("StatsFile.txt", stderr)
				print("STDERR:", stderr)
				imageName = "Test-" + my_output_list[1][1] + ".jpg"
				occurances.append(my_output_list[1][1])
				saveMutatedImage(imageName, mutatedImage) 
	for key in stats_dict:
		stats_dict[key] += occurances.count(key)
print("Number of times each bug is triggered with ",(i + 1), " number of iteration(s): ", stats_dict)

