1 /* === S Y N F I G ========================================================= */
3 ** \brief Template Header
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 ** === N O T E S ===========================================================
23 ** ========================================================================= */
25 /* === S T A R T =========================================================== */
27 #ifndef __SYNFIG_TRGT_GIF_H
28 #define __SYNFIG_TRGT_GIF_H
30 /* === H E A D E R S ======================================================= */
32 #include <synfig/target_scanline.h>
33 #include <synfig/string.h>
34 #include <synfig/smartfile.h>
36 #include <synfig/surface.h>
37 #include <synfig/palette.h>
39 /* === M A C R O S ========================================================= */
41 /* === T Y P E D E F S ===================================================== */
43 /* === C L A S S E S & S T R U C T S ======================================= */
45 class gif : public synfig::Target_Scanline
47 SYNFIG_TARGET_MODULE_EXT
49 // Class for abstracting the
50 // output of the codes
53 synfig::SmartFILE file;
56 bitstream():pool(0),curr_bit(0),curr_pos(0) {}
57 bitstream(synfig::SmartFILE file):file(file),pool(0),curr_bit(0),curr_pos(0) {}
58 unsigned char buffer[256];
61 // Pushes a single bit onto the bit
62 void push_bit(bool bit)
65 pool|=(1<<(curr_bit));
71 // Empties out the current pool into
72 // the buffer. Calls 'dump()' if the
76 buffer[curr_pos++]=pool;
79 if(curr_pos==255)dump();
82 // If there is anything in the
83 // buffer or in the pool, it
84 // dumps it to the filestream.
85 // Buffer and pool are cleared.
90 if(curr_pos || curr_bit)
92 fputc(curr_pos,file.get());
93 fwrite(buffer,curr_pos,1,file.get());
98 // Pushes a symbol of the given size
99 // onto the bitstream.
100 void push_value(int value, int size)
104 push_bit((value>>(i))&1);
108 // Class for dealing with the LZW codes
111 int value; // the data element or character
113 struct lzwcode* kids; // children of this node
114 struct lzwcode* next; // siblings of this node
116 lzwcode():value(0),code(0),kids(0),next(0) { }
118 lzwcode *FindCode(int value)
122 // check the children (kids) of the node for the value
123 for (node = node->kids; node != 0; node = node->next)
124 if (node->value == value)
129 void AddNode(unsigned short code, unsigned short value)
131 lzwcode *n = new lzwcode;
133 // add a new child to node; the child will have code and value
137 n->next = this->kids;
141 static lzwcode * NewTable(int values)
144 lzwcode * table = new lzwcode;
147 for (i = 0; i < values; i++)
148 table->AddNode( i, i);
153 // Destructor just deletes any
154 // children and siblings.
166 synfig::String filename;
167 synfig::SmartFILE file;
169 i, // General-purpose index
170 codesize, // Current code size
171 rootsize, // Size of pixel bits (will be recalculated)
172 nextcode; // Next code to use
173 lzwcode *table,*next,*node;
175 synfig::Surface curr_surface;
176 etl::surface<unsigned char> curr_frame;
177 etl::surface<unsigned char> prev_frame;
183 // GIF compression parameters
192 synfig::Palette curr_palette;
194 void output_curr_palette();
197 gif(const char *filename);
199 virtual bool set_rend_desc(synfig::RendDesc *desc);
201 virtual bool start_frame(synfig::ProgressCallback *cb);
202 virtual void end_frame();
206 virtual synfig::Color * start_scanline(int scanline);
207 virtual bool end_scanline(void);
211 /* === E N D =============================================================== */