def test_get_rgba(self): code = """ v1 = get_rgba(image1, 10, 10) """ image = ImageRGBA(200, 200) image.set_pixel(10, 10, 25, 77, 142, 185) p1 = StructArg('image1', image) p2 = Vec4Arg('v1', Vector4(0.0, 0.0, 0.0, 0.0)) shader = Shader(code=code, args=[p1, p2]) shader.compile() shader.prepare([Runtime()]) shader.execute() val = shader.get_value('v1') self.assertAlmostEqual(val.x, 25 * 0.0039, places=6) self.assertAlmostEqual(val.y, 77 * 0.0039) self.assertAlmostEqual(val.z, 142 * 0.0039) self.assertAlmostEqual(val.w, 185 * 0.0039)
def load_ppm(fname): f = open(fname, 'rb') identifier = None rx = ry = None max_value = None while True: #reading header information line = _read_bin_to_asci_line(f) line.strip() if line == '' or line == '#': continue if identifier is None: if line != 'P6': raise ValueError("Just P6 format is supported.") identifier = line continue if rx is None: words = line.split() rx = int(words[0]) ry = int(words[1]) if len(words) > 2: max_value = int(words[3]) break #whole header is read continue if max_value is None: max_value = int(line) break # whole header is read #read raw bytes(pixels) img = ImageRGBA(rx, ry) for y in range(ry): for x in range(rx): c = f.read(3) yy = y yy = ry - y - 1 # filp image?? img.set_pixel(x, yy, c[0], c[1], c[2]) f.close() return img