Talk:LZW compression: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎C example problem: new section)
(Some help with this →‎C example problem)
Line 60: Line 60:


Again, terrible code. Depends on 4 other badly written files, so it's incovenient to test at least; doesn't actually compile after pulling all files together; after some quick fix (probably incorrectly, but I don't have the patience to wade through all the mess), program outputs garbage. By the look of it, the dictionary data structure is quite suboptimal, to put it lightly. --[[User:Ledrug|Ledrug]] 23:13, 5 August 2011 (UTC)
Again, terrible code. Depends on 4 other badly written files, so it's incovenient to test at least; doesn't actually compile after pulling all files together; after some quick fix (probably incorrectly, but I don't have the patience to wade through all the mess), program outputs garbage. By the look of it, the dictionary data structure is quite suboptimal, to put it lightly. --[[User:Ledrug|Ledrug]] 23:13, 5 August 2011 (UTC)
:I've added the following message to the C section. I was able to adapt this code to produce a GIF encoder seeing that it was nearly there. This is a specific variant of LZW that probably should have its own page on this wiki with a redirect at the top of this page. Unfortunately the code could use a complete rewrite, which I intend to do for myself, but I've no need for a decoder, and have already used C++ in my changes, so I must leave this as an exercise (for others to do for themself.) Furthermore I've noticed that one or two other languages on this page have adapted this C code (they would require repair as well.) As for a GIF-LZW encoder, it only implements 8bit color. GIF can also do 1~7 bit color. Just something to keep in mind. By "efficient" the comments probably mean how GIF uses a variable length code size that increases as the codes require more bits to be represented. I do not believe it's a commentary on its algorithm; although it looks fine to me (aside from being poorly worded.) ''I THINK it is an interesting example, because it's unlikely anyone would want an LZW encoder for anything other than GIF files, since other formats that use LZW are unlikely to be in use today. And no one would use LZW otherwise except to work with old files/systems!''

:'''WARNING: This code appears to have come from a GIF codec that has been modified to meet the requirements of this page, provided that the decoder works with the encoder to produce correct output. For writing GIF files the write_bits subroutine is wrong for Little Endian systems (it may be wrong for Big Endian as well.) The encoder also increases the number of bits in the variable length GIF-LZW after the N-2 code, whereas this must be done after N-1 to produce a working GIF file (just looking at the encoder, it's easy to see how this mistake could be made.)''' --[[User:Mick P.|Mick P.]] ([[User talk:Mick P.|talk]]) 03:17, 21 October 2015 (UTC)

Revision as of 03:17, 21 October 2015

Task and example

The task talks about compression, but I've seen a lot of examples implement both compression and decompression; should we create a new task (LZW decompression) where to move the decompression related code, or change this one and add decompression to those examples that still does not implement it? --ShinTakezou 16:51, 31 January 2009 (UTC)

I'd be in favor of splitting them apart, for clarity. (Never overestimate the understanding of an individual referred here by Google. Well, you should never underestimate them, either, but I think there's a greater risk of overestimating in this case.) --Short Circuit 21:32, 31 January 2009 (UTC)
I see it hard (1), since most OO-lang implementation must be split by the people who wrote the code or know the language; (1) by hard I mean, I can't do it by myself, I could just add a new decompression task and write an impl. for Obj-C that would be the "complement" of the one here, then mark all other examples as ... hm... they do the task, but do also more... so they are task-compliant in some way... (what?) --ShinTakezou 16:43, 13 February 2009 (UTC)
Not a problem, currently. I can understand the currently-listed languages enough to do the separation myself. I'll get to it as soon as I can, which will likely be some time after midnight EST. --Short Circuit 21:27, 13 February 2009 (UTC)

C# Decompression function

I'm not sure if it would be acceptable to just post the decompression, so I'm posting it here. Since I'm lazy, and only need decomp (which seems to be easier anyway), I only wrote that. <lang csharp>

   class LZWDecompress
   {
       static void Main(string[] args)
       {
           int[] Compressed = { 84, 79, 66, 69, 79, 82, 78, 79, 84, 256, 258, 260, 265, 259, 261, 263 };
           String Decompressed = DeCompress(Compressed);
           //Decompressed should now contain "TOBEORNOTTOBEORTOBEORNOT"
       }
       static String DeCompress(int[] Compressed)
       {
           ArrayList Dictionary = new ArrayList();
           String Output = "";
           String tmpOut = "";
           String tmpIn = "";
           bool JustOnce = false;
           //Build dictionary
           for (int i = 0; i < 256; i++)
               Dictionary.Add(Convert.ToChar(i).ToString());
           foreach (int Character in Compressed)
           {
               if (JustOnce == false)
               {
                   JustOnce = true;
                   Output = tmpIn = (String)Dictionary[Character];
                   continue;
               }
               if (Character < Dictionary.Count)
                   tmpOut = (String)Dictionary[Character];
               else if (Character == Dictionary.Count)
                   tmpOut = tmpIn + tmpIn.Substring(0, 1);
               else
                   break;
               Output = Output + tmpOut;
               Dictionary.Add((tmpIn + tmpOut.Substring(0, 1)));
               tmpIn = tmpOut;
           }
           return Output;
       }
   }

</lang> Cyberman (not registered here) --85.127.79.163 19:10, 2 April 2011 (UTC) You can go ahead an add it. Just add {{incomplete|C sharp|It does not have a compress function.}} underneath the header. Hopefully someone can help you out with that eventually. --Mwn3d 19:24, 2 April 2011 (UTC)

C example problem

Again, terrible code. Depends on 4 other badly written files, so it's incovenient to test at least; doesn't actually compile after pulling all files together; after some quick fix (probably incorrectly, but I don't have the patience to wade through all the mess), program outputs garbage. By the look of it, the dictionary data structure is quite suboptimal, to put it lightly. --Ledrug 23:13, 5 August 2011 (UTC)

I've added the following message to the C section. I was able to adapt this code to produce a GIF encoder seeing that it was nearly there. This is a specific variant of LZW that probably should have its own page on this wiki with a redirect at the top of this page. Unfortunately the code could use a complete rewrite, which I intend to do for myself, but I've no need for a decoder, and have already used C++ in my changes, so I must leave this as an exercise (for others to do for themself.) Furthermore I've noticed that one or two other languages on this page have adapted this C code (they would require repair as well.) As for a GIF-LZW encoder, it only implements 8bit color. GIF can also do 1~7 bit color. Just something to keep in mind. By "efficient" the comments probably mean how GIF uses a variable length code size that increases as the codes require more bits to be represented. I do not believe it's a commentary on its algorithm; although it looks fine to me (aside from being poorly worded.) I THINK it is an interesting example, because it's unlikely anyone would want an LZW encoder for anything other than GIF files, since other formats that use LZW are unlikely to be in use today. And no one would use LZW otherwise except to work with old files/systems!
WARNING: This code appears to have come from a GIF codec that has been modified to meet the requirements of this page, provided that the decoder works with the encoder to produce correct output. For writing GIF files the write_bits subroutine is wrong for Little Endian systems (it may be wrong for Big Endian as well.) The encoder also increases the number of bits in the variable length GIF-LZW after the N-2 code, whereas this must be done after N-1 to produce a working GIF file (just looking at the encoder, it's easy to see how this mistake could be made.) --Mick P. (talk) 03:17, 21 October 2015 (UTC)