Ultimate Amiga

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Binary Trees  (Read 2477 times)

0 Members and 1 Guest are viewing this topic.

Lonewolf10

  • AMOS Extensions Developer
  • AMOS Dev
  • A2000
  • *****
  • Karma: 3
  • Offline Offline
  • Gender: Male
  • Posts: 618
    • http://www.aliensrcooluk.com
Binary Trees
« on: May 19, 2013, 09:34:13 PM »


Hi,

Over the last 2 and a half months I have been working on a ping (.png) decoder in AMOS. It works for files saved by MS Paint (from Windows XP) and Personal Paint (1996 version). I haven't tested it with Adam7 (interlaced) images yet and I have only implemented decoding of a handful of chunks, aswell as filters 0 and 1 (none and reverse sub). (I need images that use filters 2,3 & 4 before I know that code is good)

At present I find all the codes and reorder them from shortest to longest, then start scanning all codes of the same length of the code I'm trying to match. Whilst this is quicker than scanning through all the codes each time, it still takes an hour(!) to fully decode a 320x200 image (IDAT chunk size ~9000 bytes) on a 68K based (emulated) Amiga.

I was wondering if there is a quicker way to match the code found to the huffman codes?


I plan to port the code from AMOS to 68K assembler and then use it in a game I'm making. If I can use .png files instead or .raw (or .iff) files then I can get approximately x3 more graphics into the game. More, if I use the same technique to compress (deflate) the sound and level data files too!
Of course, I can only do that if the decompress (inflate) time is reasonable. (Some of that can be disquised by intro screens and pre-level screens)

Any help would be much appreciated.
Logged

bruceuncle

  • AMOS Dev
  • A500
  • *****
  • Karma: 6
  • Offline Offline
  • Gender: Male
  • Posts: 425
  • WINUAE Amiga User
Re: Binary Trees
« Reply #1 on: May 20, 2013, 12:57:38 AM »

Ah.  Similar to the problem I've got with storing results from the AMOS XRef app.  Each time I come across a Variable, Proc, Label or Function token, I need to:

  • Scan the existing results for a match to the name.
  • Insert a record for it if it doesn't exist.
  • Insert a line number and type ("definition" or "usage") record for it.

Then, after the source scan is completed, output them in sorted order.

The 'linear' scans on the name records would give me the same problem you're getting - lengthy run times.

One solution is to use a Binary Tree structure to store the records in sorted order.  They're very quick to search but have one big drawback.  If the tree gets too 'unbalanced', the search time approaches that of a 'linear' scan.  Worst case scenario is when the records are already in sorted order before they're inserted into the tree.  The scan is then 'linear'.

A 'well-balanced' Binary Tree only has a maximum search time equal to its 'depth'.  Which is not much gain for small collections of data but makes an enormous difference for large collections.  For example: 1,000,000 records can be searched with a maximum of just 20 comparisons per search.

Putting it back-to-front:

2 ^ maximum_search_depth = number_of_records

There are ways to produce self-balancing Binary Trees when the records are inserted, but coding one is complex.  I've shelved it for the time being to get stuck into the bug-fixing and docs projects.

I've attached a zip of some of the more useful stuff I downloaded when I was researching it.  They will also explain some of the terminology I used above.
Logged
Repeat after me ...  "The AMOS Pro architecture is complex but it is not complicated."

SamuraiCrow

  • compile-time wierdo
  • Forum Mod
  • A1200
  • *****
  • Karma: 5
  • Offline Offline
  • Gender: Male
  • Posts: 946
  • Compile-time wierdo
Re: Binary Trees
« Reply #2 on: May 20, 2013, 11:52:33 AM »

All of the implementations of LibPNG in C use ZLib to do their decoding grunt work.  There is a shared library implementation here.  You'll have to translate the FD file into LibCall commands or figure out the FD-to-extension converter but as an added bonus, when running on PPC accelerated Amigas it will unpack faster still.
Logged

Lonewolf10

  • AMOS Extensions Developer
  • AMOS Dev
  • A2000
  • *****
  • Karma: 3
  • Offline Offline
  • Gender: Male
  • Posts: 618
    • http://www.aliensrcooluk.com
Re: Binary Trees
« Reply #3 on: May 20, 2013, 05:59:39 PM »


Thanks for the help guys :)
Logged
Pages: [1]   Go Up
 

TinyPortal 2.2.2 © 2005-2022