SHARE
TWEET

PS2 texture unswizzling procedure (4 bit)

Dageron Aug 3rd, 2011 128 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {PS2 unswizzling procedure for 4bit textures
  2. www.Dageron.com}
  3.  
  4. //in interface
  5.  
  6. procedure UnSwizzle4X(var Source, Dest; const dwWidth, dwHeight: Integer; bInverted: Boolean = False);
  7.  
  8. //in implementation
  9.  
  10. type
  11.  TBytes = array[Byte] of Byte;
  12.  
  13. const
  14.  InterlaceMatrix: array[0..7] of Byte = ($00, $10, $02, $12, $11, $01, $13, $03);
  15.  Matrix: array[0..3] of Integer = (0, 1, -1, 0);
  16.  TileMatrix: array[0..1] of Integer = (4, -4);
  17.  
  18. procedure UnSwizzle4X(var Source, Dest; const dwWidth, dwHeight: Integer; bInverted: Boolean = False);
  19. var
  20.   X, Y, XX, YY, I, J, MW: Integer; S, D: ^Byte;
  21.   Pixels, NewPixels: array of Byte;
  22. begin
  23.   if (dwWidth mod 32 > 0) or (dwHeight mod 16 > 0) then Move(Source, Dest, (dwWidth shr 1) * dwHeight)
  24.   else
  25.     begin
  26.       SetLength(Pixels, dwWidth * dwHeight);
  27.       SetLength(NewPixels, dwWidth * dwHeight);
  28.       S := Addr(Source);
  29.       D := Addr(Pixels[0]);
  30.       if not bInverted then
  31.         for Y := 0 to dwHeight - 1 do For X := 0 to dwWidth shr 1 - 1 do
  32.           begin
  33.             D^ := S^ and 15;
  34.             Inc(D);
  35.             D^ := S^ shr 4;
  36.             Inc(D);
  37.             Inc(S);
  38.           end
  39.       else
  40.         for Y := 0 to dwHeight - 1 do For X := 0 to dwWidth shr 1 - 1 do
  41.           begin
  42.             D^ := S^ shr 4;
  43.             Inc(D);
  44.             D^ := S^ and 15;
  45.             Inc(D);
  46.             Inc(S);
  47.           end;
  48.     MW := dwWidth;
  49.     if MW mod 32 > 0 then MW := (MW div 32) * 32 + 32;
  50.     for Y := 0 to dwHeight - 1 do
  51.       begin
  52.         if Odd(Y) then For X := 0 to dwWidth - 1 do
  53.           begin
  54.             XX := X + Byte(Odd(Y div 4)) * TileMatrix[Byte(Odd(X div 4))];
  55.             YY := Y + Matrix[Y mod 4];
  56.             I := InterlaceMatrix[(X div 4) mod 4 + 4] + (X * 4) mod 16 + (X div 16) * 32 + ((Y - 1) * MW);
  57.             J := YY * dwWidth + XX;
  58.             NewPixels[J] := Pixels[I];
  59.           end
  60.           else
  61.             for X := 0 to dwWidth - 1 do
  62.               begin
  63.                 XX := X + Byte(Odd(Y div 4)) * TileMatrix[Byte(Odd(X div 4))];
  64.                 YY := Y + Matrix[Y mod 4];
  65.                 I := InterlaceMatrix[(X div 4) mod 4] + (X * 4) mod 16 + (X div 16) * 32 + (Y * MW);
  66.                 J := YY * dwWidth + XX;
  67.                 NewPixels[J] := Pixels[I];
  68.               end;
  69.       end;
  70.       S := Addr(NewPixels[0]);
  71.       D := Addr(Dest);
  72.       if not bInverted then
  73.         for Y := 0 to dwHeight - 1 do For X := 0 to dwWidth shr 1 - 1 do
  74.           begin
  75.             D^ := S^ and 15;
  76.             Inc(S);
  77.             D^ := D^ or (S^ shl 4);
  78.             Inc(S);
  79.             Inc(D);
  80.           end
  81.       else
  82.         for Y := 0 to dwHeight - 1 do For X := 0 to dwWidth shr 1 - 1 do
  83.           begin
  84.             D^ := S^ shl 4;
  85.             Inc(S);
  86.             D^ := D^ or (S^ and 15);
  87.             Inc(S);
  88.             Inc(D);
  89.           end;
  90.       Finalize(NewPixels);
  91.       Finalize(Pixels);
  92.     end;
  93. end;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top