Jump to content
Sign in to follow this  
HardXOR

Optimize/speedup code for texture decoding - (Moved)

Recommended Posts

Hello AutoIt community

I run into speed problem in my script wich i cant solve myself, problem is with decoding texture loop - for better explanation, you need extract from file pallete (16x 16 RGB color) and picture data (224 * 128 byte), then use correct color for your picture data.... nothing extra hard and also texture is quite small 224*256

it is for my car model viewer/later maybe editor GranTurismo 2 from Playstation 1, so its old dataformat and i cant understand why AutoIt take so long to decode texture when good old Playstation almost 2,5 decades old can do that nearly immediately (when you list through cars in shop or garage)

My first atempt was create all trought dllstructure, because its easier approach, but it was soooo slow (40-50s for create textures) then i upgrade my routine via arrays, first 3D arrays later only 1D, next i put decoding colors outside loop but it is still not enough, my last version took cca 15s wich is still unacceptable for car model viewer when you click on one carmodel from listview (1100 cars for whole game) and you must wait 15-16s for model to load.... :( oh and i forgot mention some cars have more then 1 color (much more... 8-9-10 etc) soloading take 8-9-10 times more time

in attachment i post texture file from GranTurismo 2 for one car (contain only 1 color) and also my dll struct version and array version code

dll struct version - ± 40 sec (33 without saving)

#include <FileConstants.au3>

Global $IMDT[256][256][4]

LoadTexture("ufs9r.cdp")

Func LoadTexture($file)
    $fileHandle = FileOpen($file, $FO_BINARY)

    $header = FileRead($fileHandle, 0x20)
    ConsoleWrite("header> " & $header & @CRLF)
    $PAL = FileRead($fileHandle, 0x200)
    ConsoleWrite("PAL> " & $PAL & @CRLF)
    FileSetPos($fileHandle, 0x43A0, $FILE_BEGIN)
    $IMD = FileRead($fileHandle, 0x7000)
    ConsoleWrite("IMD> " & $IMD & @CRLF)

    $st = DllStructCreate("BYTE[512]")
    DllStructSetData($st, 1, $PAL)
    $struct_PAL = DllStructCreate("WORD[16];WORD[16];WORD[16];WORD[16];WORD[16];WORD[16];WORD[16];WORD[16];WORD[16];WORD[16];WORD[16];WORD[16];WORD[16];WORD[16];WORD[16];WORD[16]", DllStructGetPtr($st))

    $struct_IMD = DllStructCreate("BYTE[" & 0x7000 & "]")
    DllStructSetData($struct_IMD, 1, $IMD)


    $start = TimerInit()
    For $i = 0 To 15
        For $j = 0 To 223
            $cn = 0
            For $k = 0 To 127
                $bt = DllStructGetData($struct_IMD, 1, $j * 128 + $k + 1)

                $blue = BitShift(DllStructGetData($struct_PAL, $i + 1, BitAND($bt, 0x0F) + 1), 7)
                $IMDT[$j][$cn][0] = $blue
                $green = BitShift(DllStructGetData($struct_PAL, $i + 1, BitAND($bt, 0x0F) + 1), 2)
                $IMDT[$j][$cn][1] = $green
                $red = BitShift(DllStructGetData($struct_PAL, $i + 1, BitAND($bt, 0x0F) + 1), - 3)
                $IMDT[$j][$cn][2] = $red

                If DllStructGetData($struct_PAL, $i + 1, BitAND($bt, 0x0F) + 1) = 0 Then
                    $IMDT[$j][$cn][3] = 0x00
                Else
                    $IMDT[$j][$cn][3] = 0xFF
                EndIf

                $cn += 1

                $blue = BitShift(DllStructGetData($struct_PAL, $i + 1, BitShift($bt, 4) + 1), 7)
                $IMDT[$j][$cn][0] = $blue
                $green = BitShift(DllStructGetData($struct_PAL, $i + 1, BitShift($bt, 4) + 1), 2)
                $IMDT[$j][$cn][1] = $green
                $red = BitAND(BitShift(DllStructGetData($struct_PAL, $i + 1, BitShift($bt, 4) + 1), - 3), 0xFF)
                $IMDT[$j][$cn][2] = $red

                If DllStructGetData($struct_PAL, $i + 1, BitShift($bt, 4) + 1) = 0 Then
                    $IMDT[$j][$cn][3] = 0x00
                Else
                    $IMDT[$j][$cn][3] = 0xFF
                EndIf

                $cn += 1
            Next
        Next
        saveTGA($i)
    Next
    ConsoleWrite("t " & TimerDiff($start) & @CRLF) ; +- 40 seconds

    FileClose($fileHandle)
EndFunc

Func saveTGA($name)
    Local $tga[18] = [0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x20, 0x20]
    Local $data
    for $i = 0 To 17
        $data &= Hex($tga[$i], 2)
    Next
    For $i = 0 To 255
        For $j = 0 To 255
            For $k = 0 To 3
                $data &= hex($IMDT[$i][$j][$k], 2)
            Next
        Next
    Next
    $binary = FileOpen("test\" & $name & ".tga", BitOR($FO_BINARY, $FO_OVERWRITE, $FO_CREATEPATH))
    FileWrite($binary, "0x" & $data)
    FileClose($binary)
EndFunc

 

array version - ± 15 sec (under 10s without saving)

#include <FileConstants.au3>

LoadTexture2("ufs9r.cdp")

Func LoadTexture2($file)
    $fileHandle = FileOpen($file, $FO_BINARY)

$a = TimerInit()
    Global $header[0x20]
    For $i = 0 To UBound($header) - 1
        $header[$i] = Int(String(FileRead($fileHandle, 1))) ; read 0x20 bytes
    Next
ConsoleWrite("header " & TimerDiff($a) & @CRLF)

$a = TimerInit()
    Global $PAL[0x100]
    For $i = 0 To UBound($PAL) - 1
        $PAL[$i] = Number(FileRead($fileHandle, 2)) ; read 0x200 (16*16) words
    Next

    Global $PALcolor[16 * 16 * 4]
    For $i = 0 To UBound($PAL) - 1
        $PALcolor[$i * 4 + 0] = BitShift($PAL[$i], 7)
        $PALcolor[$i * 4 + 1] = BitShift($PAL[$i], 2)
        $PALcolor[$i * 4 + 2] = BitShift($PAL[$i], -3)
        If $PAL[$i] = 0 Then
            $PALcolor[$i * 4 + 3] = 0x00
        Else
            $PALcolor[$i * 4 + 3] = 0xFF
        EndIf
    Next


ConsoleWrite("PAL " & TimerDiff($a) & @CRLF)

$a = TimerInit()
    FileSetPos($fileHandle, 0x43A0, $FILE_BEGIN)
    Global $IMD[0x7000]
    For $i = 0 To UBound($IMD) - 1
        $IMD[$i] = Int(String(FileRead($fileHandle, 1))) ; read 0x7000 bytes
    Next
ConsoleWrite("IMD " & TimerDiff($a) & @CRLF)

Global $IMDT[256*256*4]

$a = TimerInit()
    For $i = 0 To 15
        For $j = 0 To 223
            $cn = 0
            For $k = 0 To 127
                $byte = $IMD[$j * 128 + $k] ; byte for decode

                $index = $j * 1024 + $cn * 4
                $index2 = $i * 0x40 + BitAND($byte, 0x0F) * 4
                $IMDT[$index + 0] = $PALcolor[$index2 + 0] ; blue
                $IMDT[$index + 1] = $PALcolor[$index2 + 1] ; green
                $IMDT[$index + 2] = $PALcolor[$index2 + 2] ; red
                $IMDT[$index + 3] = $PALcolor[$index2 + 3] ; alpha
                $cn += 1

                $index = $j * 1024 + $cn * 4
                $index2 = $i * 0x40 + BitShift($byte, 4) * 4
                $IMDT[$index + 0] = $PALcolor[$index2 + 0] ; blue
                $IMDT[$index + 1] = $PALcolor[$index2 + 1] ; green
                $IMDT[$index + 2] = $PALcolor[$index2 + 2] ; red
                $IMDT[$index + 3] = $PALcolor[$index2 + 3] ; alpha
                $cn += 1
            Next
        Next
;~      $b = TimerInit()
        saveTGA2($i)
;~      ConsoleWrite("save TGA " & TimerDiff($b) & @CRLF)
    Next

ConsoleWrite("full time " & TimerDiff($a) & @CRLF) ; 16 seconds


    FileClose($fileHandle)
EndFunc

Func saveTGA2($name)
    Local $tga[18] = [0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x20, 0x20]
    Local $data

    For $i = 0 To 17
        $data &= Hex($tga[$i], 2)
    Next
    For $i = 0 To UBound($IMDT) - 1
        $data &= Hex($IMDT[$i], 2)
    Next

    $binary = FileOpen("test\" & $name & ".tga", BitOR($FO_BINARY, $FO_OVERWRITE, $FO_CREATEPATH))
    FileWrite($binary, "0x" & $data)
    FileClose($binary)
EndFunc

if anyone can optimize my code I would be very grateful, or pointing me to better solution, thx

ufs9r.cdp

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Colduction
      Hi dear friends!, i'm sorry for creating a new thread (a new problem), i have over than 9 lists that i want to combine them to be this (in this example, there are 3 test files):


      I've written a little code for splitting main information, but i really confused how to make results as "Output.txt", here is that code:
       
      $sRegex_1 = StringRegExp(FileRead("1.txt"), '(?s:(?<=\=\=\r\n)(.*?)(?=\r\n\=\=))', 3) $sRegex_2 = StringRegExp(FileRead("2.txt"), '(?s:(?<=\=\=\r\n)(.*?)(?=\r\n\=\=))', 3) $sRegex_3 = StringRegExp(FileRead("3.txt"), '(?s:(?<=\=\=\r\n)(.*?)(?=\r\n\=\=))', 3) For $i = 0 To UBound($sRegex_1) - 1 ConsoleWrite($sRegex_1[$i] & @CRLF) For $j = 0 To UBound($sRegex_2) - 1 ConsoleWrite($sRegex_2[$j] & @CRLF) For $k = 0 To UBound($sRegex_3) - 1 ConsoleWrite($sRegex_3[$k] & @CRLF) Next Next Next  
    • By nacerbaaziz
      hello evrybody
      here is an example about how to split your texts using a delimiter with the ability to select how much of delimiters shows in each colum  with $i_number
      e.g
      you have a long text and you want to split it in an array
      that evry colum have a number (n) of lines
      i made a function that do that for you
      just call it with a three params
      $s_text
      your text
      $i_number
      the number that you want to put in each col
      $s_siparator
      the siparator
      default is "|"
      here is the function with example
      i hope that it will be useful for you
       
      ****
       
      #include <Array.au3> $s_txt = "some text1some text2|some text3|some text4|some text5|some text6" $array = splitText($s_txt, 2) _ArrayDisplay($array) Func splitText($s_text, $i_number, $s_siparator = "|") Local $a_TXT = StringSplit($s_text, $s_siparator) Local $a_Return[$a_TXT[0] + 1] If ($a_TXT[0] <= $i_number) Or ($i_number <= 0) Then ReDim $a_Return[2] $a_Return[0] = 1 $a_Return[1] = $s_text Return $a_Return EndIf Local $i_Processed = 1, $i_arrayProcessed = 1 Do For $i = $i_Processed To ($i_Processed + $i_number) - 1 If ($a_TXT[0] < $i) Then ExitLoop If Not ($a_Return[$i_arrayProcessed]) Then $a_Return[$i_arrayProcessed] = $a_TXT[$i] Else $a_Return[$i_arrayProcessed] &= $s_siparator & $a_TXT[$i] EndIf $i_Processed += 1 Next $i_arrayProcessed += 1 Until ($a_TXT[0] < $i_Processed) ReDim $a_Return[$i_arrayProcessed] $a_Return[0] = $i_arrayProcessed - 1 Return $a_Return EndFunc ;==>splitText
      accept my greetings
      thanks to
      @Dan_555
      for his notes
       
    • By MesterPerfect
      good morning
      this is the first post here in the autoit forums
      i hope that you can help me in my problem
      i have a JSON encoded
      it a map of my forums
      where i want to make a treeview that have the same type of map
      e.g
      a system (as category)
      windows (as sub category)
      software (as an child item in the windows category)
      .....
      i don't know how to do that
      so, i know that i can do that using the json functions
      but i need your help about how we can do it as the type that i told you
      by the way i need to put the sub info for each item in an array that give me the ability to manage my items
      e.g
      can post thread
      can reply
      message cound ...
      you just give me a small example and i can continue.
      am sorry if this against the rules of the forum.
      but i realy searched a lot but i couldn't
      i hope some one give me the way.
      thank you very much in advance
       
      here is the link of json forum
      https://www.autoitscript.com/forum/topic/148114-a-non-strict-json-udf-jsmn/
      and here is my encoded json file
       
      { "tree_map": { "0": [ 1, 5, 6, 7 ], "1": [ 2 ], "2": [ 4 ], "5": [ 3 ], "6": [ 8 ], "8": [ 9, 10 ] }, "nodes": [ { "breadcrumbs": [], "description": "", "display_in_list": true, "display_order": 1, "node_id": 1, "node_name": null, "node_type_id": "Category", "parent_node_id": 0, "title": "Main category", "type_data": {} }, { "breadcrumbs": [ { "node_id": 1, "title": "Main category", "node_type_id": "Category" } ], "description": "", "display_in_list": true, "display_order": 1, "node_id": 2, "node_name": null, "node_type_id": "Forum", "parent_node_id": 1, "title": "Main forum", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [ { "node_id": 1, "title": "Main category", "node_type_id": "Category" }, { "node_id": 2, "title": "Main forum", "node_type_id": "Forum" } ], "description": "", "display_in_list": true, "display_order": 1, "node_id": 4, "node_name": null, "node_type_id": "Forum", "parent_node_id": 2, "title": "my forums1", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [], "description": "", "display_in_list": true, "display_order": 2, "node_id": 5, "node_name": null, "node_type_id": "Category", "parent_node_id": 0, "title": "Perfect", "type_data": {} }, { "breadcrumbs": [ { "node_id": 5, "title": "Perfect", "node_type_id": "Category" } ], "description": "", "display_in_list": true, "display_order": 2, "node_id": 3, "node_name": null, "node_type_id": "Forum", "parent_node_id": 5, "title": "ahmed", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [], "description": "", "display_in_list": true, "display_order": 3, "node_id": 6, "node_name": null, "node_type_id": "Forum", "parent_node_id": 0, "title": "autoit", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [ { "node_id": 6, "title": "autoit", "node_type_id": "Forum" } ], "description": "", "display_in_list": true, "display_order": 3, "node_id": 8, "node_name": null, "node_type_id": "Forum", "parent_node_id": 6, "title": "examples", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [ { "node_id": 6, "title": "autoit", "node_type_id": "Forum" }, { "node_id": 8, "title": "examples", "node_type_id": "Forum" } ], "description": "", "display_in_list": true, "display_order": 3, "node_id": 9, "node_name": null, "node_type_id": "Forum", "parent_node_id": 8, "title": "GUI", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [ { "node_id": 6, "title": "autoit", "node_type_id": "Forum" }, { "node_id": 8, "title": "examples", "node_type_id": "Forum" } ], "description": "", "display_in_list": true, "display_order": 31, "node_id": 10, "node_name": null, "node_type_id": "Forum", "parent_node_id": 8, "title": "windowEX", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [], "description": "", "display_in_list": true, "display_order": 4, "node_id": 7, "node_name": null, "node_type_id": "Category", "parent_node_id": 0, "title": "vbs", "type_data": {} } ] }  
    • By nooneclose
      I need to dynamically resize my 2d array while looping. 
      I know this code:
      ReDim $rArray[UBound($rArray) + 1] works for the rows, however, I also need to increase the columns. How would i go about increasing both rows and columns while looping? 
    • By Zaoka
      Hi guys
      Need little help with filtering.
      I'm trying to filter specific weeks from power pivot table using this code recorded with Excel VBA :
       
      #include <Excel.au3> Global $oExcel = _Excel_Open() Global $oWorkbook = _Excel_BookOpen($oExcel, "C:\Users\....\Orders.xlsb") $oWorkbook.PivotTables("PivotTable1").PivotFields( _ "[Report 2].[Week].[Week]").VisibleItemsList = Array( _ "[Report 2].[Week].&[10]", "[Report 2].[Week].&[11]", _ "[Report 2].[Week].&[12]", "[Report 2].[Week].&[13]", _ "[Report 2].[Week].&[14]", "[Report 2].[Week].&[15]") But get error
      error: Array(): undefined function. Not sure how to resolve this.
×
×
  • Create New...