1 /* === S Y N F I G ========================================================= */
3 ** \brief Template Header
6 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** General Public License for more details.
19 ** === N O T E S ===========================================================
21 ** ========================================================================= */
23 /* === S T A R T =========================================================== */
25 #ifndef __SYNFIG_TRGT_GIF_H
26 #define __SYNFIG_TRGT_GIF_H
28 /* === H E A D E R S ======================================================= */
30 #include <synfig/target_scanline.h>
31 #include <synfig/string.h>
32 #include <synfig/smartfile.h>
34 #include <synfig/surface.h>
35 #include <synfig/palette.h>
37 /* === M A C R O S ========================================================= */
39 /* === T Y P E D E F S ===================================================== */
41 /* === C L A S S E S & S T R U C T S ======================================= */
43 class gif : public synfig::Target_Scanline
45 SYNFIG_TARGET_MODULE_EXT
47 // Class for abstracting the
48 // output of the codes
51 synfig::SmartFILE file;
54 bitstream():pool(0),curr_bit(0),curr_pos(0) {}
55 bitstream(synfig::SmartFILE file):file(file),pool(0),curr_bit(0),curr_pos(0) {}
56 unsigned char buffer[256];
59 // Pushes a single bit onto the bit
60 void push_bit(bool bit)
63 pool|=(1<<(curr_bit));
69 // Emptys out the current pool into
70 // the buffer. Calls 'dump()' if the
74 buffer[curr_pos++]=pool;
77 if(curr_pos==255)dump();
80 // If there is anything in the
81 // buffer or in the pool, it
82 // dumps it to the filestream.
83 // Buffer and pool are cleared.
88 if(curr_pos || curr_bit)
90 fputc(curr_pos,file.get());
91 fwrite(buffer,curr_pos,1,file.get());
96 // Pushes a symbol of the given size
97 // onto the bitstream.
98 void push_value(int value, int size)
102 push_bit((value>>(i))&1);
106 // Class for dealing with the LZW codes
109 int value; // the data element or character
111 struct lzwcode* kids; // children of this node
112 struct lzwcode* next; // siblings of this node
114 lzwcode():value(0),code(0),kids(0),next(0) { }
116 lzwcode *FindCode(int value)
120 // check the children (kids) of the node for the value
121 for (node = node->kids; node != 0; node = node->next)
122 if (node->value == value)
127 void AddNode(unsigned short code, unsigned short value)
129 lzwcode *n = new lzwcode;
131 // add a new child to node; the child will have code and value
135 n->next = this->kids;
139 static lzwcode * NewTable(int values)
142 lzwcode * table = new lzwcode;
145 for (i = 0; i < values; i++)
146 table->AddNode( i, i);
151 // Destructor just deletes any
152 // children and sibblings.
164 synfig::String filename;
165 synfig::SmartFILE file;
167 i, // General-purpose index
168 codesize, // Current code size
169 rootsize, // Size of pixel bits (will be recalculted)
170 nextcode; // Next code to use
171 lzwcode *table,*next,*node;
173 synfig::Surface curr_surface;
174 etl::surface<unsigned char> curr_frame;
175 etl::surface<unsigned char> prev_frame;
181 // GIF compression parameters
190 synfig::Palette curr_palette;
192 void output_curr_palette();
195 gif(const char *filename);
197 virtual bool set_rend_desc(synfig::RendDesc *desc);
199 virtual bool start_frame(synfig::ProgressCallback *cb);
200 virtual void end_frame();
204 virtual synfig::Color * start_scanline(int scanline);
205 virtual bool end_scanline(void);
209 /* === E N D =============================================================== */