Skip to content
View in the app

A better way to browse. Learn more.

ResHax

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
Help us keep the site running.

Resident Evil 4 PS2 swizzled textures

Featured Replies

  • Author
  • Localization

HardRain, posted Wed Jan 04, 2023 2:20 am (74854)


Hi everyone, I am creating a simple tool for rendering textures of Resident Evil 4 game, from PS2 version.
However, there are some swizzled textures which I can't understand in which order the pixels are being read, they appear like this:
Image

And here are the main header bytes (just for faster understanding):
Image

I also have this texture unswizzled, just in case someone wants to compare.
All unswizzled textures pixels points to a color in the palette.
I just need an algorithm to render them correctly, if someone can help me, I will be very grateful.

BGR (no swizzle):
https://drive.google.com/file/d/1VuEoPUdhO6TmV67UdpsJn0HK-5dJn_8o/view?usp=share_link
PS2 swizzled:
https://drive.google.com/file/d/1VWrUVYJUVoC2XWctM5ZYljuhP-7pMxMu/view?usp=share_link
  • Author
  • Localization

h3x3r, posted Wed Jan 04, 2023 6:43 pm (74865)


Well my try. Need more samples to make it clear.
Code:
uint texMagic;
uint texNum;
uint texPalColors;
uint null;
ushort texWidth;
ushort texHeight;
ushort texFlag0;
ushort texFlag1;
uint texPaletteSize;
uint texPaletteTotalSize;
uint texDummy[4];
uint texMainLayerOffset;
uint texPaletteOffset;
uint texFileId; //Not sure
ushort texMips; //Not sure
ushort texMainLayerSize;


And here's Noesis script. Sorry I can't provide anything about untwiddle or how palette are applied. Everything is processed by Noesis api code.
Code:
from inc_noesis import *
import noesis
import rapi
import os

def registerNoesisTypes():
   handle = noesis.register("RE4 - Texture", ".tpl")
   noesis.setHandlerTypeCheck(handle, noepyCheckType)
   noesis.setHandlerLoadRGBA(handle, noepyLoadRGBA)
   noesis.logPopup()
   return 1
       
def noepyCheckType(data):
   bs = NoeBitStream(data)
   if len(data)       return 0
   if bs.readUInt() != 0x00001000:
      return 0
   return 1
   
def noepyLoadRGBA(data, texList):
    bs = NoeBitStream(data)
    texBaseName = rapi.getExtensionlessName(rapi.getLocalFileName(rapi.getInputName()))
    texMagic = bs.readUInt()
    texNum = bs.readUInt()
    texPalColors = bs.readUInt()
    bs.readBytes(4)
    texWidth = bs.readUShort()
    texHeight = bs.readUShort()
    texFlag0 = bs.readUShort()
    texFlag1 = bs.readUShort()
    texPalSize = bs.readUInt() * texPalColors
    texTotalPalSize = bs.readUInt()
    bs.readBytes(16)
    texMainLayerOffset = bs.readUInt()
    texPalOffset = bs.readUInt()
    texFileId = bs.readUInt()
    texMips = bs.readUShort()
    texMainLayerSize = bs.readUShort()
   
    bs.seek(texPalOffset, NOESEEK_ABS)
    texPalette = bs.readBytes(texPalSize)
    bs.texPalette = texPalette
       
    bs.seek(texMainLayerOffset, NOESEEK_ABS)       
    data = bs.readBytes(texMainLayerSize)
    if texFlag1 == 0:
        data = rapi.imageDecodeRawPal(data, bs.texPalette, texWidth, texHeight, 8, "r8 g8 b8 a8")
        texFmt = noesis.NOESISTEX_RGBA32
    elif texFlag1 == 2:
        data = rapi.imageUntwiddlePS2(data, texWidth, texHeight, 8)
        data = rapi.imageDecodeRawPal(data, bs.texPalette, texWidth, texHeight, 8, "r8 g8 b8 a8", noesis.DECODEFLAG_PS2SHIFT)
        texFmt = noesis.NOESISTEX_RGBA32
    texList.append(NoeTexture(rapi.getInputName(), texWidth, texHeight, data, texFmt))
    return 1


Result:
RGB
Image
PS2 with noesis.DECODEFLAG_PS2SHIFT
Image

At PS2 i added noesis.DECODEFLAG_PS2SHIFT just to see how it will be different. You can remove that code.
  • Author
  • Localization

HardRain, posted Thu Jan 05, 2023 12:15 am (74870)


Ooh too bad we can't see what happens underneath then :(
I needed that because my tool is being made using JavaScript, so I would just adapt the code, but that's okay.
Thank you for the tip with python!
Guest
This topic is now closed to further replies.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.