1 /* === S Y N F I G ========================================================= */
3 ** \brief Bitmap Target
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
26 #define SYNFIG_NO_ANGLE
36 #include <synfig/general.h>
43 /* === U S I N G =========================================================== */
45 using namespace synfig;
49 /* === I N F O ============================================================= */
51 SYNFIG_TARGET_INIT(bmp);
52 SYNFIG_TARGET_SET_NAME(bmp,"bmp");
53 SYNFIG_TARGET_SET_EXT(bmp,"bmp");
54 SYNFIG_TARGET_SET_VERSION(bmp,"0.1");
55 SYNFIG_TARGET_SET_CVS_ID(bmp,"$Id$");
57 /* === C L A S S E S & S T R U C T S ======================================= */
60 struct BITMAPFILEHEADER
62 unsigned char bfType[2];
64 unsigned short bfReserved1;
65 unsigned short bfReserved2;
66 unsigned long bfOffsetBits;
69 struct BITMAPINFOHEADER
74 unsigned short biPlanes;
75 unsigned short biBitCount;
76 unsigned long biCompression;
77 unsigned long biSizeImage;
80 unsigned long biClrUsed;
81 unsigned long biClrImportant;
85 /* === M E T H O D S ======================================================= */
87 #ifdef WORDS_BIGENDIAN
88 inline long little_endian(const long &x)
91 char *big_e=(char *)&ret;
92 char *lit_e=(char *)&x;
99 inline short little_endian_short(const short &x)
102 char *big_e=(char *)&ret;
103 char *lit_e=(char *)&x;
109 #define little_endian(x) (x)
110 #define little_endian_short(x) (x)
113 bmp::bmp(const char *Filename)
130 delete [] color_buffer;
134 bmp::set_rend_desc(RendDesc *given_desc)
138 // Flip the image upside down,
139 // because bitmaps are upside down.
140 given_desc->set_flags(0);
141 Point tl=given_desc->get_tl();
142 Point br=given_desc->get_br();
143 Point::value_type tmp;
147 given_desc->set_tl(tl);
148 given_desc->set_br(br);
151 if(desc.get_frame_end()-desc.get_frame_start()>0)
154 imagecount=desc.get_frame_start();
167 delete [] color_buffer;
174 bmp::start_frame(synfig::ProgressCallback *callback)
176 int w=desc.get_w(),h=desc.get_h();
178 rowspan=4*((w*(channels(pf)*8)+31)/32);
181 String newfilename(filename_sans_extension(filename) +
182 etl::strprintf(".%04d",imagecount) +
183 filename_extension(filename));
184 file=fopen(newfilename.c_str(),POPEN_BINARY_WRITE_TYPE);
185 if(callback)callback->task(newfilename+_(" (animated)"));
189 file=fopen(filename.c_str(),POPEN_BINARY_WRITE_TYPE);
190 if(callback)callback->task(filename);
195 if(callback)callback->error(_("Unable to open file"));
196 else synfig::error(_("Unable to open file"));
200 synfig::BITMAPFILEHEADER fileheader;
201 synfig::BITMAPINFOHEADER infoheader;
203 fileheader.bfType[0]='B';
204 fileheader.bfType[1]='M';
205 fileheader.bfSize=little_endian(sizeof(synfig::BITMAPFILEHEADER)+sizeof(synfig::BITMAPINFOHEADER)+rowspan*h);
206 fileheader.bfReserved1=0;
207 fileheader.bfReserved2=0;
208 fileheader.bfOffsetBits=little_endian(sizeof(synfig::BITMAPFILEHEADER)+sizeof(synfig::BITMAPINFOHEADER)-2);
210 infoheader.biSize=little_endian(40);
211 infoheader.biWidth=little_endian(w);
212 infoheader.biHeight=little_endian(h);
213 infoheader.biPlanes=little_endian_short((short)1);
214 infoheader.biBitCount=little_endian_short((short)(channels(pf)*8));
215 infoheader.biCompression=little_endian(0);
216 infoheader.biSizeImage=little_endian(0);
217 infoheader.biXPelsPerMeter=little_endian((int)rend_desc().get_x_res());
218 infoheader.biYPelsPerMeter=little_endian((int)rend_desc().get_y_res()); // pels per meter...?
219 infoheader.biClrUsed=little_endian(0);
220 infoheader.biClrImportant=little_endian(0);
224 if(!fwrite(&fileheader.bfSize,sizeof(synfig::BITMAPFILEHEADER)-4,1,file))
226 if(callback)callback->error(_("Unable to write file header to file"));
227 else synfig::error(_("Unable to write file header to file"));
231 if(!fwrite(&infoheader,sizeof(synfig::BITMAPINFOHEADER),1,file))
233 if(callback)callback->error(_("Unable to write info header"));
234 else synfig::error(_("Unable to write info header"));
239 buffer=new unsigned char[rowspan];
241 delete [] color_buffer;
242 color_buffer=new Color[desc.get_w()];
248 bmp::start_scanline(int /*scanline*/)
259 convert_color_format(buffer, color_buffer, desc.get_w(), pf, gamma());
261 if(!fwrite(buffer,1,rowspan,file))