Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6475299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:41:16+00:00 2026-05-25T06:41:16+00:00

I’m working on a JavaScript application that can generate animated GIF images. GIF images

  • 0

I’m working on a JavaScript application that can generate animated GIF images. GIF images use the LZW compression algorithm, and as such I need to implement it in JavaScript.

So far, I’ve got this code that can compress a string (binary or not) to an array of integers, then decompress it all right. See the pastebin link for the full code (if you think it’s necessary), but here’s what I get running this snippet, which should demonstrate that the compression and decompression itself works all right:

var lzw = new LZW(8);
var input = "TOBEORNOTTOBEORTOBEORNOT#";
var compressed = lzw.compress(input);

console.log(input);
// "TOBEORNOTTOBEORTOBEORNOT#"

console.log(compressed);
// [84, 79, 66, 69, 79, 82, 78, 79, 84, 256, 258, 260, 265, 259, 261, 263, 35]

console.log(lzw.decompress(compressed));
// "TOBEORNOTTOBEORTOBEORNOT#"

The problem, now, is that I don’t know how to do the binary packing. If I understand correctly the LZW algorithm, only the first bit pattern from compressed can be fit neatly on 8-bit boundaries, the rest needs to be on 9-bit boundaries.

I went through Wikipedia’s article on Lempel-Ziv-Welch, and it seems that for GIF image data, I would always align the least significant bits of a compressed pattern with the least significant bits of a byte; but I tried it and my GIFs don’t work, and GIF parsers aren’t very explicit about failures, so I’m not sure if it’s because of the compression or something else. To me it also looks like a huge waste of bits, so it only makes the approach more dubious.

Can anyone point me in the right direction?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:41:17+00:00Added an answer on May 25, 2026 at 6:41 am

    GIF packs data into the file as a “bit stream” — for instance, the first eight bits of the first 9-bit value go into one byte and the last bit goes into the second byte; the second 9-bit value fills up the rest of the second byte and two bits of the third, and so on. Plan on lots of bit shifting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.