size = len(data)
pixel_list = []
# Default value of bytes per pixel value for RGB
- bytes_per_px = 3
if colorspace == 'RGB':
- assert (size == (width * height * bytes_per_px))
- # BMP is stored bottom to top. Reverse the order
- for h in range(height - 1, -1, -1):
- for w in range(0, width):
- pos = 3 * (w + width * h)
- pixel_list.append(data[pos + 2])
- pixel_list.append(data[pos + 1])
- pixel_list.append(data[pos + 0])
- for x in range(0, (width * 3) % 4):
- pixel_list.append(0)
+ bytes_per_px = 3
+ poss = [2, 1, 0]
elif colorspace == 'BGRx':
bytes_per_px = 4
- assert (size == (width * height * bytes_per_px))
- # BMP is stored bottom to top. Reverse the order
- for h in range(height - 1, -1, -1):
- for w in range(0, width):
- pos = bytes_per_px * (w + width * h)
- pixel_list.append(data[pos + 0])
- pixel_list.append(data[pos + 1])
- pixel_list.append(data[pos + 2])
- for x in range(0, (width * 3) % 4):
- pixel_list.append(0)
+ poss = [0, 1, 2]
elif colorspace == 'GRAY8':
bytes_per_px = 1
- assert (size == (width * height * bytes_per_px))
- # BMP is stored bottom to top. Reverse the order
- for h in range(height - 1, -1, -1):
- for w in range(0, width):
- pos = bytes_per_px * (w + width * h)
- pixel_list.append(data[pos])
- for x in range(0, (width * 3) % 4):
- pixel_list.append(0)
+ poss = [0]
else:
print('Unrecognized colorspace %', colorspace)
sys.exit(1)
+ assert (size == (width * height * bytes_per_px))
+ # BMP is stored bottom to top. Reverse the order
+ for h in range(height - 1, -1, -1):
+ for w in range(0, width):
+ pos = bytes_per_px * (w + width * h)
+ for pp in poss:
+ pixel_list.append(data[pos + pp])
+ for x in range(0, (width * 3) % 4):
+ pixel_list.append(0)
+
graphics = pack('%dB' % (len(pixel_list)), *pixel_list)
# BMP file header
if colorspace == 'GRAY8':
# data should be tuple (data, width, height)
data1, width1, height1 = d1
data2, width2, height2 = d2
- count1 = count2 = 0
len1 = len(data1)
len2 = len(data2)
print(str(len1 * width2 * height2) + ' / ' + str(len2 * width1 * height1))
return 1
- while count1 < len1:
- # Terminated incorrectly
- if (count1 + (innerdim * width1 * height1)) > len1:
- return 2
- if (count2 + (innerdim * width2 * height2)) > len2:
- return 3
- if count2 >= len2:
- return 4
+ if (len1 != width1 * height1 * innerdim) or (len2 != width2 * height2 * innerdim):
+ return 2
- for y, x, c in product(range(0, height2), range(0, width2), range(0, innerdim)):
- ix = x * width1 // width2
- iy = y * height1 // height2
- if data1[count1 + c + ix * innerdim + iy * width1 * innerdim] != \
- data2[count2 + c + x * innerdim + y * width2 * innerdim]:
- print('At ' + str(x) + ',' + str(y))
- return 5
+ for y, x, c in product(range(0, height2), range(0, width2), range(0, innerdim)):
+ ix = x * width1 // width2
+ iy = y * height1 // height2
+ if data1[c + ix * innerdim + iy * width1 * innerdim] != \
+ data2[c + x * innerdim + y * width2 * innerdim]:
+ print('At ' + str(x) + ',' + str(y))
+ return 5
- count1 = count1 + innerdim * width1 * height1
- count2 = count2 + innerdim * width2 * height2
-
- if count1 > len1:
- return 6
- if count2 > len2:
- return 7
return 0
else:
return None
+##
+# @brief Auxiliary function to check args of test_arithmetic
+#
+def _check_args(lena, typeasize, lenb, typebsize):
+ if (0 < (lena % typeasize)) or (0 < (lenb % typebsize)):
+ return False
+ if (lena // typeasize) != (lenb // typebsize):
+ return False
+ return True
+
+
+##
+# @brief Auxiliary function to check difference
+#
+def _check_diff(mode, va, vb, val1, val2):
+ if mode == 'add':
+ ret = va + val1
+ elif mode == 'mul':
+ ret = va * val1
+ elif mode == 'add-mul':
+ ret = (va + val1) * val2
+ elif mode == 'mul-add':
+ ret = (va * val1) + val2
+ else:
+ return False
+
+ diff = ret - vb
+ if abs(diff) > 0.01:
+ return False
+ else:
+ return True
+
##
# @brief Check typecast from typea to typeb with file fna/fnb
lena = len(fna)
lenb = len(fnb)
- if (0 < (lena % typeasize)) or (0 < (lenb % typebsize)):
+ if not _check_args(lena, typeasize, lenb, typebsize):
return 10
+
num = lena // typeasize
- if num != (lenb // typebsize):
- return 11
val1 = get_value(v1[0], v1[1])
val2 = get_value(v2[0], v2[1])
if val1 is None or val2 is None:
return 12
for x in range(0, num):
- vala = struct.unpack(typeapack, fna[x * typeasize: x * typeasize + typeasize])[0]
- valb = struct.unpack(typebpack, fnb[x * typebsize: x * typebsize + typebsize])[0]
- if mode == 'add':
- diff = vala + val1 - valb
- elif mode == 'mul':
- diff = vala * val1 - valb
- elif mode == 'add-mul':
- diff = (vala + val1) * val2 - valb
- elif mode == 'mul-add':
- diff = (vala * val1) + val2 - valb
- else:
- return 21
-
- if diff > 0.01 or diff < -0.01:
+ astart = x * typeasize
+ bstart = x * typebsize
+ vala = struct.unpack(typeapack, fna[astart: astart + typeasize])[0]
+ valb = struct.unpack(typebpack, fnb[bstart: bstart + typebsize])[0]
+ if not _check_diff(mode, vala, valb, val1, val2):
return 20
return 0
##
+# @brief Auxiliary function to check args of test_typecast
+#
+def _check_args(lena, typeasize, lenb, typebsize):
+ if (0 < (lena % typeasize)) or (0 < (lenb % typebsize)):
+ return False
+ if (lena // typeasize) != (lenb // typebsize):
+ return False
+ return True
+
+
+##
+# @brief Auxiliary function to compare values
+#
+def _compare(vala, valb, typeb, maskb):
+ if typeb[0:5] == 'float':
+ if not compare_float(vala, valb):
+ return False
+ elif typeb[0:4] == 'uint' or typeb[0:3] == 'int':
+ if not compare_int(vala, valb, maskb):
+ return False
+ else:
+ return False
+
+ return True
+
+
+##
# @brief Check typecast from typea to typeb with file fna/fnb
#
def test_typecast(d1, d2):
lena = len(fna)
lenb = len(fnb)
- if (0 < (lena % typeasize)) or (0 < (lenb % typebsize)):
+ if not _check_args(lena, typeasize, lenb, typebsize):
return 10
+
num = lena // typeasize
- if num != (lenb // typebsize):
- return 11
limitb = 2 ** (8 * typebsize)
maskb = limitb - 1
for x in range(0, num):
- vala = struct.unpack(typeapack, fna[x * typeasize: x * typeasize + typeasize])[0]
- valb = struct.unpack(typebpack, fnb[x * typebsize: x * typebsize + typebsize])[0]
- if typeb[0:5] == 'float':
- if not compare_float(vala, valb):
- return 20
- elif typeb[0:4] == 'uint' or typeb[0:3] == 'int':
- if not compare_int(vala, valb, maskb):
- return 20
- else:
+ astart = x * typeasize
+ bstart = x * typebsize
+ vala = struct.unpack(typeapack, fna[astart: astart + typeasize])[0]
+ valb = struct.unpack(typebpack, fnb[bstart: bstart + typebsize])[0]
+ if not _compare(vala, valb, typeb, maskb):
return 21
return 0