LZW compression/decompression under low memory conditions

前端 未结 8 1223
Happy的楠姐
Happy的楠姐 2020-12-14 05:13

Can anybody give pointers how I can implement lzw compression/decompression in low memory conditions (< 2k). is that possible?

8条回答
  •  不知归路
    2020-12-14 05:55

    It has been over 15 years since I last played with the LZW compression algorithm, so take the following with a grain of salt.

    Given the memory constraints, this is going to be difficult at best. The dictionary you build is going to consume the vast majority of what you have available. (Assuming that code + memory <= 2k.)

    Pick a small fixed size for your dictionary. Say 1024 entries.

    Let each dictionary entry take the form of ....

     struct entry {
        intType   prevIdx;
        charType  newChar;
     };
    

    This structure makes the dictionary recursive. You need the item at the previous index to be valid in order for it to work properly. Is this workable? I'm not sure. However, let us assume for the moment that it is and find out where it leads us ....

    If you use the standard types for int and char, you are going to run out of memory fast. You will want to pack things together as tightly as possible. 1024 entries will take 10 bits to store. Your new character, will likely take 8 bits. Total = 18 bits.

    18 bits * 1024 entries = 18432 bits or 2304 bytes.

    At first glance this appears too large. What do we do? Take advantage of the fact that the first 256 entries are already known--your typical extended ascii set or what have you. This means we really need 768 entries.

    768 * 18 bits = 13824 bits or 1728 bytes.

    This leaves you with about 320 bytes to play with for code. Naturally, you can play around with the dictionary size and see what's good for you, but you will not end up with very much space for your code. Since you are looking at so little code space, I would expect that you would end up coding in assembly.

    I hope this helps.

提交回复
热议问题