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 Robert B. Quattlebaum Jr.
8 ** This software and associated documentation
9 ** are CONFIDENTIAL and PROPRIETARY property of
10 ** the above-mentioned copyright holder.
12 ** You may not copy, print, publish, or in any
13 ** other way distribute this software without
14 ** a prior written agreement with
15 ** the copyright holder.
17 ** === N O T E S ===========================================================
19 ** ========================================================================= */
21 /* === S T A R T =========================================================== */
23 #ifndef __SYNFIG_TRGT_GIF_H
24 #define __SYNFIG_TRGT_GIF_H
26 /* === H E A D E R S ======================================================= */
28 #include <synfig/target_scanline.h>
29 #include <synfig/string.h>
30 #include <synfig/smartfile.h>
32 #include <synfig/surface.h>
33 #include <synfig/palette.h>
35 /* === M A C R O S ========================================================= */
37 /* === T Y P E D E F S ===================================================== */
39 /* === C L A S S E S & S T R U C T S ======================================= */
41 class gif : public synfig::Target_Scanline
43 SYNFIG_TARGET_MODULE_EXT
45 // Class for abstracting the
46 // output of the codes
49 synfig::SmartFILE file;
52 bitstream():pool(0),curr_bit(0),curr_pos(0) {}
53 bitstream(synfig::SmartFILE file):file(file),pool(0),curr_bit(0),curr_pos(0) {}
54 unsigned char buffer[256];
57 // Pushes a single bit onto the bit
58 void push_bit(bool bit)
61 pool|=(1<<(curr_bit));
67 // Emptys out the current pool into
68 // the buffer. Calls 'dump()' if the
72 buffer[curr_pos++]=pool;
75 if(curr_pos==255)dump();
78 // If there is anything in the
79 // buffer or in the pool, it
80 // dumps it to the filestream.
81 // Buffer and pool are cleared.
86 if(curr_pos || curr_bit)
88 fputc(curr_pos,file.get());
89 fwrite(buffer,curr_pos,1,file.get());
94 // Pushes a symbol of the given size
95 // onto the bitstream.
96 void push_value(int value, int size)
100 push_bit((value>>(i))&1);
104 // Class for dealing with the LZW codes
107 int value; // the data element or character
109 struct lzwcode* kids; // children of this node
110 struct lzwcode* next; // siblings of this node
112 lzwcode():value(0),code(0),kids(0),next(0) { }
114 lzwcode *FindCode(int value)
118 // check the children (kids) of the node for the value
119 for (node = node->kids; node != 0; node = node->next)
120 if (node->value == value)
125 void AddNode(unsigned short code, unsigned short value)
127 lzwcode *n = new lzwcode;
129 // add a new child to node; the child will have code and value
133 n->next = this->kids;
137 static lzwcode * NewTable(int values)
140 lzwcode * table = new lzwcode;
143 for (i = 0; i < values; i++)
144 table->AddNode( i, i);
149 // Destructor just deletes any
150 // children and sibblings.
162 synfig::String filename;
163 synfig::SmartFILE file;
165 i, // General-purpose index
166 codesize, // Current code size
167 rootsize, // Size of pixel bits (will be recalculted)
168 nextcode; // Next code to use
169 lzwcode *table,*next,*node;
171 synfig::Surface curr_surface;
172 etl::surface<unsigned char> curr_frame;
173 etl::surface<unsigned char> prev_frame;
179 // GIF compression parameters
188 synfig::Palette curr_palette;
190 void output_curr_palette();
193 gif(const char *filename);
195 virtual bool set_rend_desc(synfig::RendDesc *desc);
197 virtual bool start_frame(synfig::ProgressCallback *cb);
198 virtual void end_frame();
202 virtual synfig::Color * start_scanline(int scanline);
203 virtual bool end_scanline(void);
207 /* === E N D =============================================================== */