1 /* === S Y N F I G ========================================================= */
3 ** \brief BMP Target Module
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 /* === H E A D E R S ======================================================= */
36 #include <ETL/stringf>
41 /* === M A C R O S ========================================================= */
43 using namespace synfig;
47 #define MAX_FRAME_RATE (20.0)
49 /* === G L O B A L S ======================================================= */
51 SYNFIG_TARGET_INIT(gif);
52 SYNFIG_TARGET_SET_NAME(gif,"gif");
53 SYNFIG_TARGET_SET_EXT(gif,"gif");
54 SYNFIG_TARGET_SET_VERSION(gif,"0.1");
55 SYNFIG_TARGET_SET_CVS_ID(gif,"$Id$");
57 /* === M E T H O D S ======================================================= */
59 gif::gif(const char *filename_):
61 file( (filename=="-")?stdout:fopen(filename_,POPEN_BINARY_WRITE_TYPE) ),
77 fputc(';',file.get()); // Image terminator
81 gif::set_rend_desc(RendDesc *given_desc)
83 if(given_desc->get_frame_rate()>MAX_FRAME_RATE)
84 given_desc->set_frame_rate(MAX_FRAME_RATE);
88 if(desc.get_frame_end()-desc.get_frame_start()>0)
92 imagecount=desc.get_frame_end()-desc.get_frame_start();
102 int w=desc.get_w(),h=desc.get_h();
106 synfig::error(strprintf(_("Unable to open \"%s\" for write access!"),filename.c_str()));
110 rootsize=color_bits; // Size of pixel bits
112 curr_frame.set_wh(w,h);
113 prev_frame.set_wh(w,h);
114 curr_surface.set_wh(w,h);
117 curr_surface.clear();
125 fprintf(file.get(),"GIF89a");
126 fputc(w&0x000000ff,file.get());
127 fputc((w&0x0000ff00)>>8,file.get());
128 fputc(h&0x000000ff,file.get());
129 fputc((h&0x0000ff00)>>8,file.get());
131 fputc(0xF0+(rootsize-1),file.get()); // flags
133 fputc((0xF0+(rootsize-1))&~(1<<7),file.get()); // flags
135 fputc(0,file.get()); // background color
136 fputc(0,file.get()); // Pixel Aspect Ratio
140 curr_palette=Palette::grayscale(256/(1<<(8-rootsize))-1);
141 output_curr_palette();
144 if(loop_count && multi_image)
146 fputc(33,file.get()); // 33 (hex 0x21) GIF Extension code
147 fputc(255,file.get()); // 255 (hex 0xFF) Application Extension Label
148 fputc(11,file.get()); // 11 (hex (0x0B) Length of Application Block
149 fprintf(file.get(),"NETSCAPE2.0");
150 fputc(3,file.get()); // 3 (hex 0x03) Length of Data Sub-Block
151 fputc(1,file.get()); // 1 (hex 0x01)
152 fputc(loop_count&0x000000ff,file.get());
153 fputc((loop_count&0x0000ff00)>>8,file.get());
154 fputc(0,file.get()); // 0 (hex 0x00) a Data Sub-block Terminator.
161 gif::output_curr_palette()
163 // Output the color table
164 for(i=0;i<256/(1<<(8-rootsize));i++)
166 if(i<(signed)curr_palette.size())
168 Color color(curr_palette[i].color.clamped());
169 //fputc(i*(1<<(8-rootsize)),file.get());
170 //fputc(i*(1<<(8-rootsize)),file.get());
171 //fputc(i*(1<<(8-rootsize)),file.get());
172 fputc(gamma().r_F32_to_U8(color.get_r()),file.get());
173 fputc(gamma().g_F32_to_U8(color.get_g()),file.get());
174 fputc(gamma().b_F32_to_U8(color.get_b()),file.get());
178 fputc(255,file.get());
180 fputc(255,file.get());
186 gif::start_frame(synfig::ProgressCallback *callback)
194 if(callback)callback->error(string("BUG:")+_("Description not set!"));
198 if(callback)callback->task(filename+strprintf(" %d",imagecount));
208 int w=desc.get_w(),h=desc.get_h(),i;
211 delaytime=round_to_int(100.0/desc.get_frame_rate());
213 bool build_off_previous(multi_image);
215 Palette prev_palette(curr_palette);
217 // Fill in the background color
218 if(!get_remove_alpha())
220 Surface::alpha_pen pen(curr_surface.begin(),1.0,Color::BLEND_BEHIND);
221 pen.set_value(get_canvas()->rend_desc().get_bg_color());
222 for(int y=0;y<curr_surface.get_h();y++,pen.inc_y())
225 for(x=0;x<curr_surface.get_w();x++,pen.inc_x())
227 if(pen.get_value().get_a()>0.1)
230 pen[0][0]=Color::alpha();
238 curr_palette=Palette(curr_surface,256/(1<<(8-rootsize))-build_off_previous-1);
239 synfig::info("curr_palette.size()=%d",curr_palette.size());
242 int transparent_index(curr_palette.find_closest(Color(1,0,1,0))-curr_palette.begin());
243 bool has_transparency(curr_palette[transparent_index].color.get_a()<=0.00001);
246 build_off_previous=false;
248 if(build_off_previous)
251 has_transparency=true;
254 #define DISPOSE_UNDEFINED (0)
255 #define DISPOSE_NONE (1<<2)
256 #define DISPOSE_RESTORE_BGCOLOR (2<<2)
257 #define DISPOSE_RESTORE_PREVIOUS (3<<2)
259 if(build_off_previous)
260 gec_flags|=DISPOSE_NONE;
262 gec_flags|=DISPOSE_RESTORE_PREVIOUS;
266 // output the Graphic Control Extension
267 fputc(0x21,file.get()); // Extension introducer
268 fputc(0xF9,file.get()); // Graphic Control Label
269 fputc(4,file.get()); // Block Size
270 fputc(gec_flags,file.get()); // Flags (Packed Fields)
271 fputc(delaytime&0x000000ff,file.get()); // Delay Time (MSB)
272 fputc((delaytime&0x0000ff00)>>8,file.get()); // Delay Time (LSB)
273 fputc(transparent_index,file.get()); // Transparent Color Index
274 fputc(0,file.get()); // Block Terminator
276 // output the image header
277 fputc(',',file.get());
278 fputc(0,file.get()); // image left
279 fputc(0,file.get()); // image left
280 fputc(0,file.get()); // image top
281 fputc(0,file.get()); // image top
282 fputc(w&0x000000ff,file.get());
283 fputc((w&0x0000ff00)>>8,file.get());
284 fputc(h&0x000000ff,file.get());
285 fputc((h&0x0000ff00)>>8,file.get());
287 fputc(0x80|(rootsize-1),file.get()); // flags
289 fputc(0x00+ rootsize-1,file.get()); // flags
294 Palette out(curr_palette);
296 if(build_off_previous)
297 curr_palette.insert(curr_palette.begin(),Color(1,0,1,0));
298 output_curr_palette();
304 // Prepare ourselves for LZW compression
306 nextcode=(1<<rootsize)+2;
307 table=lzwcode::NewTable((1<<rootsize));
310 // Output the rootsize
311 fputc(rootsize,file.get()); // rootsize;
313 // Push a table reset into the bitstream
314 bs.push_value(1<<rootsize,codesize);
316 for(int cur_scanline=0;cur_scanline<desc.get_h();cur_scanline++)
318 //convert_color_format(curr_frame[cur_scanline], curr_surface[cur_scanline], desc.get_w(), PF_GRAY, gamma());
320 // Now we compress it!
323 Color color(curr_surface[cur_scanline][i].clamped());
324 Palette::iterator iter(curr_palette.find_closest(color));
328 Color error(color-iter->color);
330 if(curr_surface.get_h()>cur_scanline+1)
332 curr_surface[cur_scanline+1][i-1] += error * ((float)3/(float)16);
333 curr_surface[cur_scanline+1][i] += error * ((float)5/(float)16);
334 if(curr_surface.get_w()>i+1)
335 curr_surface[cur_scanline+1][i+1] += error * ((float)1/(float)16);
337 if(curr_surface.get_w()>i+1)
338 curr_surface[cur_scanline][i+1] += error * ((float)7/(float)16);
341 curr_frame[cur_scanline][i]=iter-curr_palette.begin();
343 value=curr_frame[cur_scanline][i];
344 if(build_off_previous)
346 if(value>(unsigned)(1<<rootsize)-1)
347 value=(1<<rootsize)-1;
349 // If the pixel is the same as the one that
350 // is already there, then we should make it
352 if(build_off_previous)
359 abs( ( iter->color-prev_palette[prev_frame[cur_scanline][i]-1].color ).get_y() ) > (1.0/16.0) ||
360 // abs((int)value-(int)prev_frame[cur_scanline][i])>2||
361 // (value<=2 && value!=prev_frame[cur_scanline][i]) ||
362 (imagecount%iframe_density)==0 || imagecount==desc.get_frame_end()-1 ) // lossy version
363 prev_frame[cur_scanline][i]=value;
366 prev_frame[cur_scanline][i]=value;
373 if(value!=prev_frame[cur_scanline][i])
374 prev_frame[cur_scanline][i]=value;
380 prev_frame[cur_scanline][i]=value;
382 next=node->FindCode(value);
387 node->AddNode(nextcode, value);
388 bs.push_value(node->code, codesize);
389 node = table->FindCode(value);
391 // Check to see if we need to increase the codesize
392 if (nextcode == ( 1 << codesize))
397 // check to see if we have filled up the table
398 if (nextcode == 4096)
400 // output the clear code: make sure to use the current
402 bs.push_value((unsigned) 1 << rootsize, codesize);
405 table = lzwcode::NewTable((1<<rootsize));
406 codesize = rootsize + 1;
407 nextcode = (1 << rootsize) + 2;
409 // since we have a new table, need the correct prefix
410 node = table->FindCode(value);
420 // Push the last code onto the bitstream
421 bs.push_value(node->code,codesize);
423 // Push a end-of-stream code onto the bitstream
424 bs.push_value((1<<rootsize)+1,codesize);
426 // Make sure everything is dumped out
431 fputc(0,file.get()); // Block terminator
438 gif::start_scanline(int scanline)
440 cur_scanline=scanline;
441 return curr_surface[scanline];
450 // int w=desc.get_w(),i;
451 // unsigned int value;