The task:
I want to write a C++ header file that can load a given GIF file and turn it into RGBA array of pixels.
The problems:
I found trying to read the specification confusing because certain blocks (such as comments) can occur anywhere in the file. I also tried reading source code for other GIF loading files but I found these also highly confusing. I had attempted writing code for it, but after it went horribly wrong, I then deleted it and postponed trying to do it as it wasn’t required: it now is.
The restrictions:
I don’t wish to use an external library (suggesting boost is a bit of a cliche here) or pre-built code, as I want to understand what is going, but to write my own code notably for several reasons:
- So I can repair the code if it goes wrong.
- So I can expand the features and incorporate them with other pre-existing classes I have.
- To avoid licencing problems:
- So I don’t get sued for misuse (I neither want to pay or be forced to make my code open-source).
- So I can bundle the code with, or sell the program (this is unlikely but I’m covering bases here) without any copyright/copyleft issues.
- So I can safely give the code to others and be able to tell them they don’t have to worry about a licence.
- So I can offer the code to a library that lacks GIF loading so they can use it without worrying about licence issues.
- To keep the codebase small (compared to a library of other features).
I know that’s quite a list and seems unnecessary but when I previously say on stack overflow ‘no libraries’ people assume it’s something optional that they can argue me into submission or force a library down my throat.
The questions:
What is the best way to both understand (I found the specification confusing remember) and implement GIF loading so it converts a GIF (which might be a singular image or a series of images) into an array of pixels? And how best to ignore ‘unnecessary’ data (at least in regards to the task at hand, so ignoring comments etc) when reading?
For convenience you can assume a pixel array class already exists.
Clear-cut code snippets with explanations would be appreciated on this one.
Ok, since i can’t edit my comment anymore…. As Mat suggested, try to understand the spec. I recommend the site http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp#comment_extension_block since in my opinion it is a good source of information about the structure of the gif format, with much graphics and byte-code-examples. For example your question about the comments: When you find an byte-pair
21 FEin your datastream this is introducing a comment block. The following bytes indicate the ascii characters and at the end of the block you will find a00.