Publish 50 reviewed original terrain HD remasters

This commit is contained in:
raptorreplica committed 2026-10-20 21:36:43 +11:00
1 parent b2e19b8b86
commit 443d77852b
10031 files changed
+4124394 -121

No files matched your search

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 MiB

@@ -0,0 +1,113 @@
{
"method": "terrain-six-Source-separated-materials-genuine-fine-RGB-band-v1",
"palette": {
"path": "inputs/palette.bin",
"sha256": "1cf6adb926d1dee1888b3e3d33b51a7049b3e5eb5f90a3067ef71c8970547204"
},
"frames": [
{
"asset": "2069f",
"source": {
"path": "inputs/2069f.bin",
"sha256": "abd15ac0040097763ad755ee11da01ff528f3643c59ac9b039da7ae5ff6ba513"
},
"donor": {
"path": "inputs/2069f-genuine-builtin-v1.png",
"sha256": "fde55fcd27e8b1e880e04fab7de1e5baa7026f9a55edf3a2b29673b276efb772",
"box": [
0,
0,
1254,
1254
]
}
},
{
"asset": "206a2",
"source": {
"path": "inputs/206a2.bin",
"sha256": "99b6c1679a0830e4d1ec61c3be09d8e7a24706f76e83b2c13e9ab911dbd0bdd1"
},
"donor": {
"path": "inputs/206a2-genuine-builtin-v1.png",
"sha256": "d417fb362001319ead7b5b584c726b5044717e68a0266d4a7ff301a7249abf6e",
"box": [
0,
0,
1254,
1254
]
}
},
{
"asset": "206a7",
"source": {
"path": "inputs/206a7.bin",
"sha256": "c0aaf8b37f9b0e70abb59f985d1b92079af3206bbb1c01b8ffc7c70631a745ca"
},
"donor": {
"path": "inputs/206a7-genuine-builtin-v1.png",
"sha256": "1ef6ff128cd0e1ff75f5a26f6f156c5d431f83c2b98c14925b73cb6aa7fba786",
"box": [
0,
0,
1254,
1254
]
}
},
{
"asset": "301fc",
"source": {
"path": "inputs/301fc.bin",
"sha256": "b7b1754f0c5222035f8f46df242822de26dfc7bc11bcc63e87cec3353b22f7a1"
},
"donor": {
"path": "inputs/301fc-genuine-builtin-v1.png",
"sha256": "535159e5a33281728b9b6397342075347085be10ebeee4aee82b609bcdc3991b",
"box": [
0,
0,
1254,
1254
]
}
},
{
"asset": "301fd",
"source": {
"path": "inputs/301fd.bin",
"sha256": "80e924120e1167274f4ede7b3766032bc97e62039eae878705405d7e987499cc"
},
"donor": {
"path": "inputs/301fd-genuine-builtin-v1.png",
"sha256": "51b79475ac45229987f969be7bc66b3c6ba6789a0da5459b4b0e829e44483e37",
"box": [
0,
0,
1254,
1254
]
}
},
{
"asset": "30201",
"source": {
"path": "inputs/30201.bin",
"sha256": "8b41d8298e48eedfcc5a4a9e0d0a6af79044ba54b80f8a393e068abbbd6b5583"
},
"donor": {
"path": "inputs/30201-genuine-builtin-v1.png",
"sha256": "9c213ecb6f5f2ab7cfceef4cc014a81e1ab43ce42471813f83997b4cb26b64e7",
"box": [
0,
0,
1254,
1254
]
}
}
],
"lineage": "DERIVED: fixed authentic SourceScale4 separated material fields plus genuine generated donor fine RGB band; no coarse generated shape/color and no synthesized procedural texture. Exactly one complete fullcanvas donor per Source tile, no tiling.",
"functional_contract": "Keep exact union of Source tiny-color80..95/112..191 and cycling240..255 footprints across native1\u20134 as explicit retained Original detail; green96..111 is genuine fine material, never misclassified retention. Source coarse geometry/color and retained Original carry zero generated HD credit."
}
@@ -0,0 +1,179 @@
"""Private Source-shaped genuine-RGB derivation proposal. Never a generator return.
The single32x32 opaque format is decoded here honestly, including protected labels;
the ordinary-only decoder in hd_terrain_cleanup is neither called nor bypassed.
"""
from pathlib import Path
import argparse, hashlib, json, sys, struct, os, stat, io
import shutil
import numpy as np
from PIL import Image
ROOT=next((p for p in Path(__file__).resolve().parents if (p/'tools/hd_terrain_cleanup.py').is_file()),None)
if ROOT is None: raise RuntimeError('place this portable helper under the game root containing tools/hd_terrain_cleanup.py')
MAX_INPUT_BYTES=16*1024*1024
MAX_RECIPE_BYTES=64*1024
MAX_PNG_PIXELS=4*1024*1024
def read_contained(root,relative,limit=MAX_INPUT_BYTES):
if not isinstance(relative,str) or relative.startswith('/') or any(x in ('','.','..') for x in relative.split('/')):
raise ValueError('strict contained relative path required')
root=Path(root).absolute()
if '..' in root.parts:raise ValueError('noncanonical root path')
flags=os.O_RDONLY|os.O_CLOEXEC|os.O_NOFOLLOW|os.O_NONBLOCK
fd=os.open('/',flags|os.O_DIRECTORY)
try:
for part in root.parts[1:]+tuple(relative.split('/')[:-1]):
following=os.open(part,flags|os.O_DIRECTORY,dir_fd=fd);os.close(fd);fd=following
source=os.open(relative.split('/')[-1],flags,dir_fd=fd)
try:
before=os.fstat(source)
if not stat.S_ISREG(before.st_mode) or not 0<=before.st_size<=limit:raise ValueError('regular bounded input required')
chunks=[];size=0
while True:
chunk=os.read(source,min(65536,limit+1-size))
if not chunk:break
chunks.append(chunk);size+=len(chunk)
if size>limit:raise ValueError('bounded EOF limit exceeded')
after=os.fstat(source)
identity=lambda x:(x.st_dev,x.st_ino,x.st_size,x.st_mtime_ns,x.st_mode)
if identity(before)!=identity(after) or size!=before.st_size:raise ValueError('input changed or non-EOF read')
payload=b''.join(chunks)
return payload,dict(path=str(root/relative),sha256=hashlib.sha256(payload).hexdigest(),size=size,mtime_ns=before.st_mtime_ns,mode=before.st_mode)
finally:os.close(source)
finally:os.close(fd)
class BufferedInputs:
def __init__(self,root):self.root=Path(root);self.cache={};self.physical_reads=0
def read(self,binding):
rel=binding['path']
if rel not in self.cache:
self.cache[rel]=read_contained(self.root,rel);self.physical_reads+=1
payload,pin=self.cache[rel]
if pin['sha256']!=binding['sha256']:raise ValueError('input SHA mismatch')
return payload,pin
def decode_png(payload):
if len(payload)>MAX_INPUT_BYTES:raise ValueError('encoded PNG too large')
with Image.open(io.BytesIO(payload)) as im:
if im.format!='PNG' or im.width<1 or im.height<1 or im.width*im.height>MAX_PNG_PIXELS:raise ValueError('bounded PNG required')
if getattr(im,'n_frames',1)!=1 or im.mode not in ('RGB','RGBA'):raise ValueError('single RGB/RGBA PNG required')
if im.mode=='RGBA' and im.getchannel('A').getextrema()!=(255,255):raise ValueError('opaque PNG donor required')
return np.array(im.convert('RGB'))
EXPECTED_DEPENDENCY_SHA256='3284cf6f85ced107dfc364ea90e15aed341d478bea7e308024b5bc5218b03092'
_dependency_bytes,_dependency_pin=read_contained(ROOT/'tools','hd_terrain_cleanup.py',1024*1024)
if _dependency_pin['sha256']!=EXPECTED_DEPENDENCY_SHA256:raise RuntimeError('reviewed terrain math dependency changed')
_dependency_namespace={'__name__':'reviewed_component_terrain_math','__file__':str(ROOT/'tools/hd_terrain_cleanup.py')}
exec(compile(_dependency_bytes,_dependency_namespace['__file__'],'exec'),_dependency_namespace)
scale_nx,gaussian,resize_float,palette_rgb=(_dependency_namespace[x]for x in ('scale_nx','gaussian','resize_float','palette_rgb'))
METHOD="terrain-six-Source-separated-materials-genuine-fine-RGB-band-v1"
COHORTS={("2069f","206a2","206a7","301fc","301fd","30201")}
def decode_source32(payload):
if len(payload)!=1044 or struct.unpack_from('<5i',payload)!=(1,1,0,32,32):
raise ValueError('requires exact opaque single32x32 Source PIC')
return np.frombuffer(payload[20:],np.uint8).reshape(32,32).copy()
def donor_field(rgb,box,size,tile_size=None):
if len(box)!=4 or any(type(v)is not int for v in box):raise ValueError('integer donor box required')
x,y,w,h=box
if min(x,y)<0 or min(w,h)<1 or x+w>rgb.shape[1]or y+h>rgb.shape[0]:raise ValueError('donor crop outside genuine image')
im=Image.fromarray(rgb[y:y+h,x:x+w])
if tile_size:
sw,sh=tile_size
if min(sw,sh)<1:raise ValueError('bad texture period')
tile=np.array(im.resize((sw,sh),Image.Resampling.LANCZOS))
return np.tile(tile,(int(np.ceil(size[1]/sh)),int(np.ceil(size[0]/sw)),1))[:size[1],:size[0]].astype(float)
return np.array(im.resize(size,Image.Resampling.LANCZOS),dtype=float)
def functional_support_union(indices):
if indices.shape!=(32,32)or indices.dtype!=np.uint8:raise ValueError('exact Source32 plane required')
cells=set()
for factor in range(1,5):
y,x=np.where(protected_indices(scale_nx(indices,factor)))
cells.update((int(xx)//factor,int(yy)//factor)for xx,yy in zip(x,y))
return [list(p)for p in sorted(cells)]
def protected_indices(a):
a=np.asarray(a)
return ((a>=80)&(a<=95))|((a>=112)&(a<=191))|(a>=240)
def material_labels(a):
a=np.asarray(a)
if a.ndim!=2 or a.dtype!=np.uint8:raise ValueError('indexed Source plane required')
labels=np.full(a.shape,-1,np.int8)
labels[a<24]=0
labels[(a>=24)&(a<32)]=1
labels[(a>=32)&(a<=47)]=2
labels[(a>=192)&(a<=239)]=3
labels[(a>=58)&(a<=63)]=4
labels[(a>=64)&(a<=79)]=5
labels[(a>=96)&(a<=111)]=6
labels[protected_indices(a)]=7
if np.any(labels<0):raise ValueError('unsupported actual six-terrain Source label')
return labels
def derive_tile(indices,palette,field):
if indices.shape!=(32,32)or indices.dtype!=np.uint8:raise ValueError('exact Source32 plane required')
if np.asarray(field).shape!=(512,512,3):raise ValueError('registered genuine donor field required')
guide=scale_nx(indices,4);labels=material_labels(guide)
owners=np.repeat(np.repeat(labels,4,0),4,1)
source_rgb=palette[guide].astype(float);output=np.zeros((512,512,3),float);base_output=np.zeros_like(output)
donor=np.asarray(field,float);band=np.clip(donor-gaussian(donor,3.0),-16,16)*0.5
for component in range(7):
mask=(labels==component).astype(float)
if not np.any(mask):continue
weights=resize_float(gaussian(mask,1.0),(512,512))
numerator=resize_float(gaussian(source_rgb*mask[...,None],1.0),(512,512))
base=numerator/np.maximum(weights[...,None],1e-12)
detail=band if component not in (0,1)else np.repeat(np.mean(band,axis=2,keepdims=True),3,axis=2)
output[owners==component]=(base+detail)[owners==component]
base_output[owners==component]=base[owners==component]
output=np.clip(np.rint(output),0,255).astype(np.uint8)
exact=np.repeat(np.repeat(palette[guide],4,0),4,1)
protected=np.repeat(np.repeat(protected_indices(guide),4,0),4,1);output[protected]=exact[protected]
union=functional_support_union(indices);retained=np.zeros((32,32),bool)
for x,y in union:retained[y,x]=True
owner_retained=np.repeat(np.repeat(retained,16,0),16,1)
base8=np.clip(np.rint(base_output),0,255).astype(np.uint8)
changed=int(np.any(output!=base8,axis=2)[~owner_retained].sum())
if changed==0:raise ValueError('no generated detail survives; Original-only is not HD')
metrics=dict(original_detail_cells=union,raw_Source_reserved_cells=int((indices>=240).sum()),functional_support_union_cells=len(union),all_functional_SourceNx_RGB_exact=bool(np.array_equal(output[protected],exact[protected])),nonfunctional_HDR_pixels=int((~owner_retained).sum()),genuine_RGB_band_changed_nonfunctional_HDR_pixels=changed,geometry_owner_sha256=hashlib.sha256(owners.tobytes()).hexdigest(),Original_functional_HD_credit=0)
return output,metrics
MIN_FREE_BYTES=1_932_735_284 # conservative1.8GiB floor, plus separate bounded reserve
def check_fresh_output(path,frames):
path=Path(path).absolute()
if '..'in path.parts or path.name in ('','.','..'):raise ValueError('canonical fresh output required')
parent=path.parent
if not parent.is_dir():raise ValueError('existing output parent directory required')
for ancestor in (parent,*parent.parents):
if ancestor.is_symlink():raise ValueError('output parent symlink forbidden')
if os.path.lexists(path):raise ValueError('new output directory required')
reserve=32*1024*1024+frames*512*512*3*2
free=shutil.disk_usage(parent).free
if free<MIN_FREE_BYTES+reserve:raise ValueError('private HD1.8GiB disk floor plus bounded output reserve required')
return path,dict(minimum_free_bytes=MIN_FREE_BYTES,output_reserve_bytes=reserve,actual_free_bytes=free,existing_nonsymlink_parent=str(parent))
def main():
ap=argparse.ArgumentParser();ap.add_argument('--recipe',type=Path,required=True);ap.add_argument('--output',type=Path,required=True);args=ap.parse_args()
recipe_bytes,recipe_pin=read_contained(args.recipe.parent,args.recipe.name,MAX_RECIPE_BYTES);recipe=json.loads(recipe_bytes)
if set(recipe)!={'method','palette','frames','lineage','functional_contract'}or recipe['method']!=METHOD:raise ValueError('exact six-terrain recipe required')
frames=recipe['frames']
if not isinstance(frames,list)or tuple(f['asset']for f in frames)not in COHORTS:raise ValueError('exact selected complete cohort required')
out,output_guard=check_fresh_output(args.output,len(frames))
inputs=BufferedInputs(args.recipe.parent);pb,ppin=inputs.read(recipe['palette']);pal=palette_rgb(pb);aud=[];prepared=[]
_,tool_pin=read_contained(Path(__file__).parent,Path(__file__).name,1024*1024)
for f in frames:
if set(f)!={'asset','source','donor'}:raise ValueError('exact frame fields required')
payload,sp=inputs.read(f['source']);idx=decode_source32(payload);rgb_payload,dp=inputs.read(f['donor']);rgb=decode_png(rgb_payload)
box=f['donor']['box']
field=donor_field(rgb,box,(512,512));result,metrics=derive_tile(idx,pal,field);prepared.append((f['asset'],result,metrics));aud.extend([sp,dp])
out.mkdir(parents=True)
for aid,result,metrics in prepared:Image.fromarray(result).save(out/f'{aid}-derived.png')
receipt=dict(output_guard=output_guard,implementation=tool_pin,executed_dependency=_dependency_pin,recipe_input=recipe_pin,physical_authenticated_input_reads=inputs.physical_reads,method=METHOD,input_pins=aud,palette=ppin,recipe_sha256=hashlib.sha256(recipe_bytes).hexdigest(),metrics=dict(selected_ids=[f['asset']for f in frames],assets=[dict(asset=aid,**one)for aid,result,one in prepared],fine_band_sigma=3.0,fine_band_clip=16,fine_band_strength=0.5,donor_repetition=False,global_SourceNx_nearest_fallback=False),output_is_derived_not_untouched=True,functional_retention_no_HD_credit='Protected Source tiny-color labels80..95/112..191 and cycling240..255 and union of logical owners supporting Scale1..4 retain exact SourceNx where protected, no original-only HD credit.',outputs=[dict(file=p.name,sha256=hashlib.sha256(p.read_bytes()).hexdigest())for p in sorted(out.glob('*.png'))])
receipt['execution']=dict(status='success',method=METHOD,tool_sha256=tool_pin['sha256'],recipe_sha256=recipe_pin['sha256'],parent_raw_sha256=sorted({pin['sha256']for pin in aud if pin['path'].endswith('.png')}),source_sha256={f['asset']:f['source']['sha256']for f in frames},output_sha256={f['asset']:hashlib.sha256((out/(f['asset']+'-derived.png')).read_bytes()).hexdigest()for f in frames})
(out/'ACTUAL-DERIVATION-RECEIPT.json').write_text(json.dumps(receipt,indent=2)+'\n');print(json.dumps(receipt['metrics']))
if __name__=='__main__':main()
@@ -0,0 +1,120 @@
import unittest,importlib.util,json,tempfile,os,hashlib,struct
from pathlib import Path
import numpy as np
from PIL import Image
p=Path(__file__).with_name('terrain_six_fine_cleanup.py');spec=importlib.util.spec_from_file_location('terrain_six_tested',p);m=importlib.util.module_from_spec(spec);spec.loader.exec_module(m)
class IndustrialContract(unittest.TestCase):
def setUp(self):self.pal=np.arange(768,dtype=np.uint16).reshape(256,3).astype(np.uint8)
def test_source_truncated(self):
with self.assertRaises(ValueError):m.decode_source32(b'0'*1043)
def test_source_wrongcanvas(self):
with self.assertRaises(ValueError):m.decode_source32(struct.pack('<5i',1,1,0,16,64)+b'0'*1024)
def test_source_roundtrip_reserved(self):
a=np.arange(1024,dtype=np.uint16).astype(np.uint8).reshape(32,32);self.assertTrue(np.array_equal(a,m.decode_source32(struct.pack('<5i',1,1,0,32,32)+a.tobytes())))
def test_source_badkind(self):
with self.assertRaises(ValueError):m.decode_source32(struct.pack('<5i',2,1,0,32,32)+b'0'*1024)
def test_material_partition(self):
a=np.array([[0,23,24,31,32,47,58,79,100,111,192,239,240,255]],np.uint8);self.assertEqual(m.material_labels(a).tolist(),[[0,0,1,1,2,2,4,5,6,6,3,3,7,7]])
def test_unknown_index_rejected(self):
for label in [48,49,57]:
with self.subTest(label=label),self.assertRaises(ValueError):m.material_labels(np.array([[label]],np.uint8))
def test_wrong_labeltype_rejected(self):
with self.assertRaises(ValueError):m.material_labels(np.array([[1]],np.int32))
def test_union_no_reserved(self):self.assertEqual(m.functional_support_union(np.full((32,32),200,np.uint8)),[])
def test_union_reserved_near_corner(self):
a=np.full((32,32),200,np.uint8);a[0,0]=245;u=m.functional_support_union(a);self.assertIn([0,0],u);self.assertTrue(all(0<=x<32 and 0<=y<32 for x,y in u))
def test_union_wrongcanvas(self):
with self.assertRaises(ValueError):m.functional_support_union(np.zeros((16,64),np.uint8))
def test_constant_donor_cannot_claim_HD(self):
with self.assertRaises(ValueError):m.derive_tile(np.full((32,32),200,np.uint8),self.pal,np.full((512,512,3),100.))
def test_derive_protected_exact(self):
a=np.full((32,32),200,np.uint8);a[4,7]=246;field=np.random.default_rng(4).integers(0,256,(512,512,3)).astype(float);out,metrics=m.derive_tile(a,self.pal,field);g=m.scale_nx(a,4);mask=np.repeat(np.repeat(g>=240,4,0),4,1);exact=np.repeat(np.repeat(self.pal[g],4,0),4,1);self.assertTrue(np.array_equal(out[mask],exact[mask]));self.assertGreater(metrics['genuine_RGB_band_changed_nonfunctional_HDR_pixels'],0);self.assertEqual(metrics['Original_functional_HD_credit'],0)
def test_donor_wrongcanvas(self):
with self.assertRaises(ValueError):m.derive_tile(np.full((32,32),200,np.uint8),self.pal,np.zeros((511,512,3)))
def test_field_box_no_tiling(self):
a=np.arange(24*18*3,dtype=np.uint16).reshape(18,24,3).astype(np.uint8);self.assertEqual(m.donor_field(a,[0,0,24,18],(512,512)).shape,(512,512,3))
def test_field_invalid_box(self):
for box in [[-1,0,8,8],[0,0,25,8],[0,0,0,1],[0.,0,1,1]]:
with self.subTest(box=box),self.assertRaises(ValueError):m.donor_field(np.zeros((18,24,3),np.uint8),box,(512,512))
def test_io_traversal(self):
with tempfile.TemporaryDirectory()as d:
with self.assertRaises(ValueError):m.read_contained(Path(d),'../file')
def test_io_symlink(self):
with tempfile.TemporaryDirectory()as d:
p=Path(d);(p/'a').write_bytes(b'x');(p/'s').symlink_to(p/'a')
with self.assertRaises(OSError):m.read_contained(p,'s')
def test_io_bound(self):
with tempfile.TemporaryDirectory()as d:
p=Path(d);(p/'a').write_bytes(b'abc')
with self.assertRaises(ValueError):m.read_contained(p,'a',2)
def test_io_hash_mismatch(self):
with tempfile.TemporaryDirectory()as d:
p=Path(d);(p/'a').write_bytes(b'abc')
with self.assertRaises(ValueError):m.BufferedInputs(p).read(dict(path='a',sha256='0'*64))
def test_png_alpha_rejected(self):
import io
b=io.BytesIO();Image.new('RGBA',(8,8),(10,10,10,100)).save(b,format='PNG')
with self.assertRaises(ValueError):m.decode_png(b.getvalue())
class OutputGuards(unittest.TestCase):
def test_reject_output_parent_symlink(self):
with tempfile.TemporaryDirectory()as d:
root=Path(d);(root/'real').mkdir();(root/'link').symlink_to(root/'real',target_is_directory=True)
with self.assertRaises(ValueError):m.check_fresh_output(root/'link'/'new',2)
def test_reject_existing_file_or_directory(self):
with tempfile.TemporaryDirectory()as d:
root=Path(d);(root/'file').write_text('x');(root/'dir').mkdir()
for name in ['file','dir']:
with self.subTest(name=name),self.assertRaises(ValueError):m.check_fresh_output(root/name,2)
def test_reject_missing_parent(self):
with tempfile.TemporaryDirectory()as d:
with self.assertRaises(ValueError):m.check_fresh_output(Path(d)/'missing'/'new',2)
def test_reject_disk_floor_before_pixels(self):
from unittest.mock import patch
with tempfile.TemporaryDirectory()as d,patch.object(m.shutil,'disk_usage',return_value=type('Space',(),dict(free=m.MIN_FREE_BYTES))()):
with self.assertRaises(ValueError):m.check_fresh_output(Path(d)/'new',5)
def test_exact_disk_floor_plus_reserve(self):
from unittest.mock import patch
reserve=32*1024*1024+5*512*512*3*2
with tempfile.TemporaryDirectory()as d,patch.object(m.shutil,'disk_usage',return_value=type('Space',(),dict(free=m.MIN_FREE_BYTES+reserve))()):
out,guard=m.check_fresh_output(Path(d)/'new',5);self.assertFalse(out.exists());self.assertEqual(guard['output_reserve_bytes'],reserve)
def test_reject_dotdot_output(self):
with tempfile.TemporaryDirectory()as d:
with self.assertRaises(ValueError):m.check_fresh_output(Path(d)/'child'/'..'/'new',2)
class SixSourceSemantics(unittest.TestCase):
def test_olive_rust_green_owners_stay_separate(self):
a=np.array([[58,63,64,79,100,111]],np.uint8);self.assertEqual(m.material_labels(a).tolist(),[[4,4,5,5,6,6]])
def test_tiny_ordinary_and_cycling_partition(self):
a=np.array([[80,95,112,134,147,152,171,183,191,240,255]],np.uint8)
self.assertTrue(np.all(m.material_labels(a)==7));self.assertTrue(np.all(m.protected_indices(a)))
def test_tiny_ordinary_is_exact_retention_not_generated_band(self):
a=np.full((32,32),108,np.uint8);a[3,4]=134;a[9,12]=152;a[22,29]=183
yy,xx=np.indices((512,512));field=np.stack([(xx%5)*40,(yy%7)*32,((xx+yy)%9)*24],axis=2)
pal=np.arange(768,dtype=np.uint16).reshape(256,3).astype(np.uint8);out,proof=m.derive_tile(a,pal,field);g=m.scale_nx(a,4);mask=np.repeat(np.repeat(m.protected_indices(g),4,0),4,1);exact=np.repeat(np.repeat(pal[g],4,0),4,1)
self.assertTrue(np.array_equal(out[mask],exact[mask]));self.assertTrue({(4,3),(12,9),(29,22)}<=set(map(tuple,proof['original_detail_cells'])));self.assertEqual(proof['Original_functional_HD_credit'],0)
def test_union_covers_all_native_scales_for_rare_and_cycling(self):
a=np.full((32,32),62,np.uint8);a[5,5]=82;a[5,6]=82;a[6,5]=82;a[18,20]=250;u=set(map(tuple,m.functional_support_union(a)))
for scale in range(1,5):
yy,xx=np.where(m.protected_indices(m.scale_nx(a,scale)));self.assertTrue({(int(x)//scale,int(y)//scale)for x,y in zip(xx,yy)}<=u)
def test_nonmatching_crop_uses_own_pixels(self):
rgb=np.zeros((8,16,3),np.uint8);rgb[:,:8]=[50,70,90];rgb[:,8:]=[160,140,120]
self.assertTrue(np.all(m.donor_field(rgb,[0,0,8,8],(512,512))==[50,70,90]));self.assertTrue(np.all(m.donor_field(rgb,[8,0,8,8],(512,512))==[160,140,120]))
def test_real_buffered_complete6_subprocess_receipt(self):
import subprocess,sys
with tempfile.TemporaryDirectory()as d:
root=Path(d);(root/'inputs').mkdir();pal=np.arange(768,dtype=np.uint16).astype(np.uint8).tobytes();(root/'inputs/pal').write_bytes(pal)
yy,xx=np.indices((128,192));raw=np.stack([(xx%3)*90,(yy%5)*50,((xx+yy)%7)*35],axis=2).astype(np.uint8);Image.fromarray(raw).save(root/'inputs/raw.png')
h=lambda p:hashlib.sha256(p.read_bytes()).hexdigest();frames=[]
for n,aid in enumerate(['2069f','206a2','206a7','301fc','301fd','30201']):
src=root/f'inputs/{aid}.bin';a=np.full((32,32),200,np.uint8);a[7,8]=250;a[20,21]=147;src.write_bytes(struct.pack('<5i',1,1,0,32,32)+a.tobytes());frames.append(dict(asset=aid,source=dict(path=f'inputs/{aid}.bin',sha256=h(src)),donor=dict(path='inputs/raw.png',sha256=h(root/'inputs/raw.png'),box=[(n%3)*64,(n//3)*64,64,64])))
recipe=dict(method=m.METHOD,palette=dict(path='inputs/pal',sha256=h(root/'inputs/pal')),frames=frames,lineage='synthetic true32x32 PIC and structured RGB donor; not production artwork',functional_contract='tests only');r=root/'recipe.json';r.write_text(json.dumps(recipe));out=root/'fresh';z=subprocess.run([sys.executable,str(p),'--recipe',str(r),'--output',str(out)],capture_output=True,text=True,timeout=60);self.assertEqual(z.returncode,0,z.stderr);receipt=json.loads((out/'ACTUAL-DERIVATION-RECEIPT.json').read_text());e=receipt['execution'];self.assertEqual(e['status'],'success');self.assertEqual(e['tool_sha256'],h(p));self.assertEqual(e['recipe_sha256'],h(r));self.assertEqual(e['parent_raw_sha256'],[h(root/'inputs/raw.png')]);self.assertEqual(receipt['physical_authenticated_input_reads'],8);self.assertEqual(len(list(out.glob('*-derived.png'))),6)
for f in frames:
aid=f['asset'];self.assertEqual(e['source_sha256'][aid],f['source']['sha256']);self.assertEqual(e['output_sha256'][aid],h(out/(aid+'-derived.png')))
class NewGreenRange(unittest.TestCase):
def test_entire96_to111_range_is_green_not_retained_Original(self):
a=np.arange(96,112,dtype=np.uint8).reshape(1,16);self.assertTrue(np.all(m.material_labels(a)==6));self.assertFalse(m.protected_indices(a).any())
def test_green_low96_keeps_genuine_band_not_Original_only(self):
a=np.full((32,32),96,np.uint8);yy,xx=np.indices((512,512));field=np.stack([(xx%5)*40,(yy%7)*32,((xx+yy)%9)*24],axis=2);pal=np.arange(768,dtype=np.uint16).reshape(256,3).astype(np.uint8);out,proof=m.derive_tile(a,pal,field);self.assertEqual(proof['original_detail_cells'],[]);self.assertGreater(proof['genuine_RGB_band_changed_nonfunctional_HDR_pixels'],0)
if __name__=='__main__':unittest.main()