883 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
31 lines
1.6 KiB
Python
31 lines
1.6 KiB
Python
"""Raw3 BLP2 decoder boundaries and channel order; no client or archive IO."""
|
|
import hashlib,io,struct,sys,unittest
|
|
from pathlib import Path
|
|
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'ops'))
|
|
import octo_icon_assets as m
|
|
from PIL import Image
|
|
|
|
def fixture():
|
|
raw=bytearray(1172);raw[:4]=b'BLP2';struct.pack_into('<I4BII',raw,4,1,3,8,8,0,2,1)
|
|
struct.pack_into('<I',raw,20,1172);struct.pack_into('<I',raw,84,8)
|
|
return bytes(raw)+bytes((30,20,10,40,90,80,70,60))
|
|
|
|
class RawBgra(unittest.TestCase):
|
|
def test_bgra_channel_order_and_alpha_preserved(self):
|
|
d=m.decode_blp(fixture())
|
|
self.assertEqual((d['width'],d['height'],d['mip_levels']),(2,1,1))
|
|
self.assertEqual(d['rgba_sha256'],hashlib.sha256(bytes((10,20,30,40,70,80,90,60))).hexdigest())
|
|
def test_malformed_pixels_and_mip_header_rejected(self):
|
|
for offset,value in ((4,0),(20,1171),(84,7),(88,4),(12,3),(16,0)):
|
|
raw=bytearray(fixture());struct.pack_into('<I',raw,offset,value)
|
|
with self.subTest(offset=offset),self.assertRaises(ValueError):m.decode_blp(bytes(raw))
|
|
for raw in(fixture()[:-1],fixture()+b'\0'):
|
|
with self.assertRaises(ValueError):m.decode_blp(raw)
|
|
def test_unknown_alpha_encoding_not_reinterpreted(self):
|
|
raw=bytearray(fixture());raw[10]=7
|
|
with self.assertRaisesRegex(ValueError,'raw BGRA8 BLP2 header'):m.decode_blp(bytes(raw))
|
|
def test_existing_paletted_blp_path_unchanged(self):
|
|
image=Image.new('P',(8,8));image.putpalette([0,0,0]*256);stream=io.BytesIO();image.save(stream,format='BLP')
|
|
d=m.decode_blp(stream.getvalue());self.assertEqual((d['width'],d['height'],d['decoded']),(8,8,True));self.assertNotIn('decoder',d)
|
|
if __name__=='__main__':unittest.main()
|