1 /*! ========================================================================
3 ** Template Header File
4 ** $Id: trgt_gif.h,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $
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.
18 ** === N O T E S ===========================================================
20 ** ========================================================================= */
22 /* === S T A R T =========================================================== */
24 #ifndef __SYNFIG_TRGT_GIF_H
25 #define __SYNFIG_TRGT_GIF_H
27 /* === H E A D E R S ======================================================= */
29 #include <synfig/target_scanline.h>
30 #include <synfig/string.h>
31 #include <synfig/smartfile.h>
33 #include <synfig/surface.h>
34 #include <synfig/palette.h>
36 /* === M A C R O S ========================================================= */
38 /* === T Y P E D E F S ===================================================== */
40 /* === C L A S S E S & S T R U C T S ======================================= */
42 class gif : public synfig::Target_Scanline
44 SYNFIG_TARGET_MODULE_EXT
46 // Class for abstracting the
47 // output of the codes
50 synfig::SmartFILE file;
53 bitstream():pool(0),curr_bit(0),curr_pos(0) {}
54 bitstream(synfig::SmartFILE file):file(file),pool(0),curr_bit(0),curr_pos(0) {}
55 unsigned char buffer[256];
58 // Pushes a single bit onto the bit
59 void push_bit(bool bit)
62 pool|=(1<<(curr_bit));
68 // Emptys out the current pool into
69 // the buffer. Calls 'dump()' if the
73 buffer[curr_pos++]=pool;
76 if(curr_pos==255)dump();
79 // If there is anything in the
80 // buffer or in the pool, it
81 // dumps it to the filestream.
82 // Buffer and pool are cleared.
87 if(curr_pos || curr_bit)
89 fputc(curr_pos,file.get());
90 fwrite(buffer,curr_pos,1,file.get());
95 // Pushes a symbol of the given size
96 // onto the bitstream.
97 void push_value(int value, int size)
101 push_bit((value>>(i))&1);
105 // Class for dealing with the LZW codes
108 int value; // the data element or character
110 struct lzwcode* kids; // children of this node
111 struct lzwcode* next; // siblings of this node
113 lzwcode():value(0),code(0),kids(0),next(0) { }
115 lzwcode *FindCode(int value)
119 // check the children (kids) of the node for the value
120 for (node = node->kids; node != 0; node = node->next)
121 if (node->value == value)
126 void AddNode(unsigned short code, unsigned short value)
128 lzwcode *n = new lzwcode;
130 // add a new child to node; the child will have code and value
134 n->next = this->kids;
138 static lzwcode * NewTable(int values)
141 lzwcode * table = new lzwcode;
144 for (i = 0; i < values; i++)
145 table->AddNode( i, i);
150 // Destructor just deletes any
151 // children and sibblings.
163 synfig::String filename;
164 synfig::SmartFILE file;
166 i, // General-purpose index
167 codesize, // Current code size
168 rootsize, // Size of pixel bits (will be recalculted)
169 nextcode; // Next code to use
170 lzwcode *table,*next,*node;
172 synfig::Surface curr_surface;
173 etl::surface<unsigned char> curr_frame;
174 etl::surface<unsigned char> prev_frame;
180 // GIF compression parameters
189 synfig::Palette curr_palette;
191 void output_curr_palette();
194 gif(const char *filename);
196 virtual bool set_rend_desc(synfig::RendDesc *desc);
198 virtual bool start_frame(synfig::ProgressCallback *cb);
199 virtual void end_frame();
203 virtual synfig::Color * start_scanline(int scanline);
204 virtual bool end_scanline(void);
208 /* === E N D =============================================================== */