Add PixelBuffer#from_bytes and width/height header to to_bytes

This commit is contained in:
Kirill Osipov
2026-07-16 19:03:34 +02:00
parent 35ec1107b6
commit 2fa7b24345
3 changed files with 83 additions and 7 deletions
+21
View File
@@ -57,6 +57,27 @@ class LAYPixelBufferTests(unittest.TestCase):
pb_copy = pya.PixelBuffer.read_png(tmp)
self.assertEqual(compare(pb, pb_copy), True)
def test_2(self):
# to_bytes / from_bytes round-trip
import struct
pb = pya.PixelBuffer(10, 20)
pb.transparent = True
pb.fill(0xf0010203)
pb.set_pixel(1, 2, 0x80102030)
data = bytes(pb.to_bytes())
self.assertEqual(len(data), 8 + 10 * 20 * 4)
self.assertEqual(struct.unpack("=II", data[0:8]), (10, 20)) # width, height header
self.assertEqual(pb == pya.PixelBuffer.from_bytes(data), True)
# a mismatched stream is rejected
with self.assertRaises(Exception):
pya.PixelBuffer.from_bytes(data[:-4])
# run unit tests
if __name__ == '__main__':
+26
View File
@@ -152,6 +152,32 @@ class LAYPixelBuffer_TestClass < TestBase
end
def test_5
# to_bytes / from_bytes round-trip
pb = RBA::PixelBuffer::new(10, 20)
pb.transparent = true
pb.fill(0xf0010203)
pb.set_pixel(1, 2, 0x80102030)
bytes = pb.to_bytes
assert_equal(bytes.size, 8 + 10 * 20 * 4)
assert_equal(bytes[0, 8].unpack("VV"), [ 10, 20 ]) # width, height header
assert_equal(pb == RBA::PixelBuffer.from_bytes(bytes), true)
# a mismatched stream is rejected
error = false
begin
RBA::PixelBuffer.from_bytes(bytes[0, bytes.size - 4])
rescue
error = true
end
assert_equal(error, true)
end
def test_11
pb = RBA::BitmapBuffer::new