LazyDuchess / OpenTS2

Open source re-implementation of The Sims 2 in Unity
Mozilla Public License 2.0
221 stars 17 forks source link

Figure out JFIF/JPEG ALFA blocks. #4

Closed LazyDuchess closed 1 year ago

LazyDuchess commented 1 year ago

The game generates thumbnails as JPEG images, and embeds a custom ALFA block in them to achieve transparency:

alfablock

The image above is a Sim thumbnail opened with a HEX editor. They don't look terribly complicated to figure out, they're Run Length Encoded.

LazyDuchess commented 1 year ago

For the time being I'll be doing color picking and some other prost processing to get rid of the black bg. Obvs not ideal but will do for now. image

ammaraskar commented 1 year ago

Got it :)

Raw

image

Transparency Mask

image

Details

Python Parsing Code ```python def setTransparencyFromALFA(im, alfa): im2 = im.convert('RGBA').copy() transparency = [] i = 0 while i < len(alfa): rle_byte = int.from_bytes(alfa[i:i+1], byteorder='big', signed=True) i += 1 if rle_byte < 0: # Repeat the next transparency n times. t = alfa[i] transparency.extend([t] * ((-rle_byte) + 1)) i += 1 else: # Read n unique transparency values. for _ in range(rle_byte + 1): transparency.append(alfa[i]) i += 1 print(len(transparency)) ctr = 0 for j in range(im.size[0]): for i in range(im.size[1]): pixel = im2.getpixel((i, j)) pixel = (transparency[ctr], pixel[1], pixel[2], pixel[3]) ctr += 1 im2.putpixel((i, j), pixel) return im2 ```

It uses a similar RLE scheme to the .reia files:

Let me see if I can send a pull request to add this to IMGCodec.cs to get familiarized with the codebase.

LazyDuchess commented 1 year ago

Awesome! Cool that you figured out the RLE - I did notice there was some different behavior depending on if the byte was positive or negative but I hadn't thought of this.