XeNTaX Forum Index
Forum MultiEx Commander Tools Tools Home
It is currently Sat Nov 18, 2017 12:22 am

All times are UTC + 1 hour


Forum rules


Please click here to view the forum rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: PS2 texture unswizzling (4-bit textures)
PostPosted: Sat May 30, 2009 3:38 am 
Offline
ultra-n00b

Joined: Mon May 25, 2009 3:04 pm
Posts: 3
Has thanked: 0 time
Have thanks: 1 time

To make a long story short, I'm working on the SVR image format and I'm having trouble unswizzling 4-bit textures. I've already got 8-bit texture unswizzling working fine.

The image on the left is what it should look like, the image on the right is how it currently works with my unswizzle code.

Image Image

I'm currently using Sparky's Swizzle Code for this, obviously modified so it unswizzles rather than swizzles again.

Here is the code I'm currently using (C#):
Code:
        // Unswizzle a 4-bit PS2 texture
        public static void UnSwizzle4(byte[] Buf, int Width, int Height, int Where)
        {
            // Don't swizzle if size if width or height is less than 128
            if (Width < 128 || Height < 128)
                return;

            // Make a copy of the swizzled input and clear buffer
            byte[] Swizzled = new byte[Buf.Length - Where];
            Array.Copy(Buf, Where, Swizzled, 0, Swizzled.Length);

            for (int y = 0; y < Height; y++)
            {
                for (int x = 0; x < Width; x++)
                {
                    // get the pen
                    int index = y * Width + x;
                    //byte uPen = (byte)(Swizzled[index >> 1] >> ((index & 1) * 4) & 0xF);

                    // swizzle
                    int pageX = x & (~0x7f);
                    int pageY = y & (~0x7f);

                    int pages_horz = (Width + 127) / 128;
                    int pages_vert = (Height + 127) / 128;

                    int page_number = (pageY / 128) * pages_horz + (pageX / 128);

                    int page32Y = (page_number / pages_vert) * 32;
                    int page32X = (page_number % pages_vert) * 64;

                    int page_location = page32Y * Height * 2 + page32X * 4;

                    int locX = x & 0x7f;
                    int locY = y & 0x7f;

                    int block_location = ((locX & (~0x1f)) >> 1) * Height + (locY & (~0xf)) * 2;
                    int swap_selector = (((y + 2) >> 2) & 0x1) * 4;
                    int posY = (((y & (~3)) >> 1) + (y & 1)) & 0x7;

                    int column_location = posY * Height * 2 + ((x + swap_selector) & 0x7) * 4;

                    int byte_num = (x >> 3) & 3;     // 0,1,2,3
                    int bits_set = (y >> 1) & 1;     // 0,1            (lower/upper 4 bits)

                    byte uPen = (byte)(Swizzled[page_location + block_location + column_location + byte_num] >> ((index & 1) * 4) & 0xF);
                    Buf[Where + (index >> 1)] = (byte)((Buf[Where + (index >> 1)] & -bits_set) | (uPen << (bits_set * 4)));
                }
            }
        }


Here's the SVR of that file, if it helps at all (CLUT starts at 0x20 and is ARGB8888, image data at 0x60):
http://www.puyonexus.net/files/misc/accord_dress.svr

You can make the ads go away by registering



Top
 Profile  
 
 Post subject: Re: PS2 texture unswizzling (4-bit textures)
PostPosted: Thu Mar 21, 2013 4:36 am 
Offline
VIP member
VIP member

Joined: Mon May 02, 2005 1:48 pm
Posts: 288
Location: Louisville, KY
Has thanked: 10 times
Have thanks: 68 times
I'm in the same exact boat, and every bit of source code I've found doesn't work, although the source code from Puyo Tools and Sparky's Swizzle Code (they are the same thing and not sure who is the original publisher of the code) has been the closest one so far.

_________________
XHP Creations Twitter
XHP Creations GitHub
XHP YouTube Page (no longer maintained)
XHP Creations Facebook Page (no longer maintained)


Top
 Profile  
 
 Post subject: Re: PS2 texture unswizzling (4-bit textures)
PostPosted: Mon Nov 11, 2013 6:05 am 
Offline
advanced

Joined: Thu Sep 10, 2009 2:31 am
Posts: 41
Has thanked: 0 time
Have thanks: 6 times
Check this. It's sort of a mess but it for sure works. It's based on Puyo Tools. https://github.com/neko68k/rtftool/blob ... p6t_v2.cpp

_________________
http://neko68k.blogspot.com


Top
 Profile  
 
 Post subject: Re: PS2 texture unswizzling (4-bit textures)
PostPosted: Tue Nov 07, 2017 9:28 am 
Offline
advanced
User avatar

Joined: Thu Mar 08, 2012 6:17 pm
Posts: 47
Location: California, USA
Has thanked: 6 times
Have thanks: 15 times
After 8 years of this originally being posted, I can finally provide a concrete answer to 4-bit PS2 texture unswizzling. I wrote it in C# and is based on Delphi code originally by Dageron. Full credit goes to him for being smart enough to know how this stuff works!

A permanent link to the code can be found here. If you use this in an open-source project, a link back to the gist would be appreciated. But do make sure you give full credit to Dageron, as he's the one who made this possible in the first place!

Happy unswizzling!


Top
 Profile  
 
 Post subject: Re: PS2 texture unswizzling (4-bit textures)
PostPosted: Mon Nov 13, 2017 12:11 am 
Offline
veteran

Joined: Mon Aug 06, 2012 4:14 am
Posts: 95
Has thanked: 0 time
Have thanks: 62 times
The
Quote:
//if ((mw % 32) > 0)
// mw = ((mw / 32) * 32) + 32;

part is for alignment to multiples of 32.

Though the typical implementation would be:
Code:
(value + align-1)/align*align

instead of having the remainder check.



Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group