Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / 0.61.09 / src / modules / mod_gif / trgt_gif.h
1 /* === S Y N F I G ========================================================= */
2 /*!     \file trgt_gif.h
3 **      \brief Template Header
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
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.
14 **
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.
19 **      \endlegal
20 **
21 ** === N O T E S ===========================================================
22 **
23 ** ========================================================================= */
24
25 /* === S T A R T =========================================================== */
26
27 #ifndef __SYNFIG_TRGT_GIF_H
28 #define __SYNFIG_TRGT_GIF_H
29
30 /* === H E A D E R S ======================================================= */
31
32 #include <synfig/target_scanline.h>
33 #include <synfig/string.h>
34 #include <synfig/smartfile.h>
35 #include <cstdio>
36 #include <synfig/surface.h>
37 #include <synfig/palette.h>
38
39 /* === M A C R O S ========================================================= */
40
41 /* === T Y P E D E F S ===================================================== */
42
43 /* === C L A S S E S & S T R U C T S ======================================= */
44
45 class gif : public synfig::Target_Scanline
46 {
47         SYNFIG_TARGET_MODULE_EXT
48 private:
49         // Class for abstracting the
50         // output of the codes
51         struct bitstream
52         {
53                 synfig::SmartFILE file;
54                 unsigned char pool;
55                 char curr_bit;
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];
59                 int curr_pos;
60
61                 // Pushes a single bit onto the bit
62                 void push_bit(bool bit)
63                 {
64                         if(bit)
65                                 pool|=(1<<(curr_bit));
66                         curr_bit++;
67                         if(curr_bit==8)
68                                 empty();
69                 }
70
71                 // Empties out the current pool into
72                 // the buffer. Calls 'dump()' if the
73                 // buffer is full.
74                 void empty()
75                 {
76                         buffer[curr_pos++]=pool;
77                         curr_bit=0;
78                         pool=0;
79                         if(curr_pos==255)dump();
80                 }
81
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.
86                 void dump()
87                 {
88                         if(curr_bit)
89                                 empty();
90                         if(curr_pos || curr_bit)
91                         {
92                                 fputc(curr_pos,file.get());
93                                 fwrite(buffer,curr_pos,1,file.get());
94                                 curr_pos=0;
95                         }
96                 }
97
98                 // Pushes a symbol of the given size
99                 // onto the bitstream.
100                 void push_value(int value, int size)
101                 {
102                         int i;
103                         for(i=0;i<size;i++)
104                                 push_bit((value>>(i))&1);
105                 }
106         };
107
108         // Class for dealing with the LZW codes
109         struct lzwcode
110         {
111                 int value; // the data element or character
112                 int code; // lzwcode
113                 struct lzwcode* kids; // children of this node
114                 struct lzwcode* next; // siblings of this node
115
116                 lzwcode():value(0),code(0),kids(0),next(0) { }
117
118                 lzwcode *FindCode(int value)
119                 {
120                         lzwcode *node=this;
121
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)
125                                         return(node);
126                         return(0);
127                 }
128
129                 void AddNode(unsigned short code, unsigned short value)
130                 {
131                         lzwcode *n = new lzwcode;
132
133                         // add a new child to node; the child will have code and value
134                         n->value = value;
135                         n->code = code;
136                         n->kids = 0;
137                         n->next = this->kids;
138                         this->kids = n;
139                 }
140
141                 static lzwcode * NewTable(int values)
142                 {
143                         int i;
144                         lzwcode * table = new lzwcode;
145
146                         table->kids = 0;
147                         for (i = 0; i < values; i++)
148                                 table->AddNode( i, i);
149
150                         return(table);
151                 }
152
153                 // Destructor just deletes any
154                 // children and siblings.
155                 ~lzwcode()
156                 {
157                         if(kids)
158                                 delete kids;
159                         if(next)
160                                 delete next;
161                 }
162         };
163
164 private:
165         bitstream bs;
166         synfig::String filename;
167         synfig::SmartFILE file;
168         int
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;
174
175         synfig::Surface curr_surface;
176         etl::surface<unsigned char> curr_frame;
177         etl::surface<unsigned char> prev_frame;
178
179         int imagecount;
180         int cur_scanline;
181
182
183         // GIF compression parameters
184         bool lossy;
185         bool multi_image;
186         bool dithering;
187         int color_bits;
188         int iframe_density;
189         int loop_count;
190         bool local_palette;
191
192         synfig::Palette curr_palette;
193
194         void output_curr_palette();
195
196 public:
197         gif(const char *filename);
198
199         virtual bool set_rend_desc(synfig::RendDesc *desc);
200         virtual bool init();
201         virtual bool start_frame(synfig::ProgressCallback *cb);
202         virtual void end_frame();
203
204         virtual ~gif();
205
206         virtual synfig::Color * start_scanline(int scanline);
207         virtual bool end_scanline(void);
208
209 };
210
211 /* === E N D =============================================================== */
212
213 #endif