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>
38 #include <synfig/targetparam.h>
40 /* === M A C R O S ========================================================= */
42 /* === T Y P E D E F S ===================================================== */
44 /* === C L A S S E S & S T R U C T S ======================================= */
46 class gif : public synfig::Target_Scanline
48 SYNFIG_TARGET_MODULE_EXT
50 // Class for abstracting the
51 // output of the codes
54 synfig::SmartFILE file;
57 bitstream():pool(0),curr_bit(0),curr_pos(0) {}
58 bitstream(synfig::SmartFILE file):file(file),pool(0),curr_bit(0),curr_pos(0) {}
59 unsigned char buffer[256];
62 // Pushes a single bit onto the bit
63 void push_bit(bool bit)
66 pool|=(1<<(curr_bit));
72 // Empties out the current pool into
73 // the buffer. Calls 'dump()' if the
77 buffer[curr_pos++]=pool;
80 if(curr_pos==255)dump();
83 // If there is anything in the
84 // buffer or in the pool, it
85 // dumps it to the filestream.
86 // Buffer and pool are cleared.
91 if(curr_pos || curr_bit)
93 fputc(curr_pos,file.get());
94 fwrite(buffer,curr_pos,1,file.get());
99 // Pushes a symbol of the given size
100 // onto the bitstream.
101 void push_value(int value, int size)
105 push_bit((value>>(i))&1);
109 // Class for dealing with the LZW codes
112 int value; // the data element or character
114 struct lzwcode* kids; // children of this node
115 struct lzwcode* next; // siblings of this node
117 lzwcode():value(0),code(0),kids(0),next(0) { }
119 lzwcode *FindCode(int value)
123 // check the children (kids) of the node for the value
124 for (node = node->kids; node != 0; node = node->next)
125 if (node->value == value)
130 void AddNode(unsigned short code, unsigned short value)
132 lzwcode *n = new lzwcode;
134 // add a new child to node; the child will have code and value
138 n->next = this->kids;
142 static lzwcode * NewTable(int values)
145 lzwcode * table = new lzwcode;
148 for (i = 0; i < values; i++)
149 table->AddNode( i, i);
154 // Destructor just deletes any
155 // children and siblings.
167 synfig::String filename;
168 synfig::SmartFILE file;
170 i, // General-purpose index
171 codesize, // Current code size
172 rootsize, // Size of pixel bits (will be recalculated)
173 nextcode; // Next code to use
174 lzwcode *table,*next,*node;
176 synfig::Surface curr_surface;
177 etl::surface<unsigned char> curr_frame;
178 etl::surface<unsigned char> prev_frame;
184 // GIF compression parameters
193 synfig::Palette curr_palette;
195 void output_curr_palette();
198 gif(const char *filename, const synfig::TargetParam& /* params */);
200 virtual bool set_rend_desc(synfig::RendDesc *desc);
202 virtual bool start_frame(synfig::ProgressCallback *cb);
203 virtual void end_frame();
207 virtual synfig::Color * start_scanline(int scanline);
208 virtual bool end_scanline(void);
212 /* === E N D =============================================================== */