94834c7ffe2b74e477fa7463c8731b3059cac4a4
[synfig.git] / synfig-core / trunk / src / modules / mod_bmp / trgt_bmp.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file trgt_bmp.cpp
3 **      \brief Bitmap Target
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 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #define SYNFIG_NO_ANGLE
26
27 #ifdef USING_PCH
28 #       include "pch.h"
29 #else
30 #ifdef HAVE_CONFIG_H
31 #       include <config.h>
32 #endif
33
34 #include "trgt_bmp.h"
35 #include <synfig/general.h>
36
37 #include <cstdio>
38 #include <algorithm>
39 #include <functional>
40 #endif
41
42 /* === U S I N G =========================================================== */
43
44 using namespace synfig;
45 using namespace std;
46 using namespace etl;
47
48 /* === I N F O ============================================================= */
49
50 SYNFIG_TARGET_INIT(bmp);
51 SYNFIG_TARGET_SET_NAME(bmp,"bmp");
52 SYNFIG_TARGET_SET_EXT(bmp,"bmp");
53 SYNFIG_TARGET_SET_VERSION(bmp,"0.1");
54 SYNFIG_TARGET_SET_CVS_ID(bmp,"$Id$");
55
56 /* === C L A S S E S & S T R U C T S ======================================= */
57
58 struct BITMAPFILEHEADER
59 {
60         unsigned char   bfType[2];
61         unsigned long   bfSize;
62         unsigned short  bfReserved1;
63         unsigned short  bfReserved2;
64         unsigned long   bfOffsetBits;
65 };
66
67 struct BITMAPINFOHEADER
68 {
69         unsigned long   biSize;
70         long                    biWidth;
71         long                    biHeight;
72         unsigned short  biPlanes;
73         unsigned short  biBitCount;
74         unsigned long   biCompression;
75         unsigned long   biSizeImage;
76         long                    biXPelsPerMeter;
77         long                    biYPelsPerMeter;
78         unsigned long   biClrUsed;
79         unsigned long   biClrImportant;
80 };
81
82 /* === M E T H O D S ======================================================= */
83
84 #ifdef WORDS_BIGENDIAN
85 inline long little_endian(const long &x)
86 {
87         long ret;
88         char *big_e=(char *)&ret;
89         char *lit_e=(char *)&x;
90         big_e[0]=lit_e[3];
91         big_e[1]=lit_e[2];
92         big_e[2]=lit_e[1];
93         big_e[3]=lit_e[0];
94         return ret;
95 }
96 inline short little_endian_short(const short &x)
97 {
98         short ret;
99         char *big_e=(char *)&ret;
100         char *lit_e=(char *)&x;
101         big_e[0]=lit_e[1];
102         big_e[1]=lit_e[0];
103         return ret;
104 }
105 #else
106 #define little_endian(x)        (x)
107 #define little_endian_short(x)  (x)
108 #endif
109
110 bmp::bmp(const char *Filename)
111 {
112         file=NULL;
113         filename=Filename;
114         multi_image=false;
115         buffer=0;
116         color_buffer=0;
117         set_remove_alpha();
118
119 }
120
121 bmp::~bmp()
122 {
123         if(file)
124                 fclose(file);
125         file=NULL;
126         delete [] buffer;
127         delete [] color_buffer;
128 }
129
130 bool
131 bmp::set_rend_desc(RendDesc *given_desc)
132 {
133         pf=PF_BGR;
134
135     // Flip the image upside down,
136         // because bitmaps are upside down.
137     given_desc->set_flags(0);
138         Point tl=given_desc->get_tl();
139         Point br=given_desc->get_br();
140         Point::value_type tmp;
141         tmp=tl[1];
142         tl[1]=br[1];
143         br[1]=tmp;
144         given_desc->set_tl(tl);
145         given_desc->set_br(br);
146
147         desc=*given_desc;
148         if(desc.get_frame_end()-desc.get_frame_start()>0)
149         {
150                 multi_image=true;
151                 imagecount=desc.get_frame_start();
152         }
153         else
154                 multi_image=false;
155
156     return true;
157 }
158
159 void
160 bmp::end_frame()
161 {
162         if(file)
163                 fclose(file);
164         delete [] color_buffer;
165         color_buffer=0;
166         file=NULL;
167         imagecount++;
168 }
169
170 bool
171 bmp::start_frame(synfig::ProgressCallback *callback)
172 {
173         int w=desc.get_w(),h=desc.get_h();
174
175         rowspan=4*((w*(channels(pf)*8)+31)/32);
176         if(multi_image)
177         {
178                 String newfilename(filename_sans_extension(filename) +
179                                                    etl::strprintf(".%04d",imagecount) +
180                                                    filename_extension(filename));
181                 file=fopen(newfilename.c_str(),POPEN_BINARY_WRITE_TYPE);
182                 if(callback)callback->task(newfilename+_(" (animated)"));
183         }
184         else
185         {
186                 file=fopen(filename.c_str(),POPEN_BINARY_WRITE_TYPE);
187                 if(callback)callback->task(filename);
188         }
189
190         if(!file)
191         {
192                 if(callback)callback->error(_("Unable to open file"));
193                 else synfig::error(_("Unable to open file"));
194                 return false;
195         }
196
197         BITMAPFILEHEADER fileheader;
198         BITMAPINFOHEADER infoheader;
199
200         fileheader.bfType[0]='B';
201         fileheader.bfType[1]='M';
202         fileheader.bfSize=little_endian(sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+rowspan*h);
203         fileheader.bfReserved1=0;
204         fileheader.bfReserved2=0;
205         fileheader.bfOffsetBits=little_endian(sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)-2);
206
207         infoheader.biSize=little_endian(40);
208         infoheader.biWidth=little_endian(w);
209         infoheader.biHeight=little_endian(h);
210         infoheader.biPlanes=little_endian_short((short)1);
211         infoheader.biBitCount=little_endian_short((short)(channels(pf)*8));
212         infoheader.biCompression=little_endian(0);
213         infoheader.biSizeImage=little_endian(0);
214         infoheader.biXPelsPerMeter=little_endian((int)rend_desc().get_x_res());
215         infoheader.biYPelsPerMeter=little_endian((int)rend_desc().get_y_res()); // pels per meter...?
216         infoheader.biClrUsed=little_endian(0);
217         infoheader.biClrImportant=little_endian(0);
218
219         fprintf(file,"BM");
220
221         if(!fwrite(&fileheader.bfSize,sizeof(BITMAPFILEHEADER)-4,1,file))
222         {
223                 if(callback)callback->error(_("Unable to write file header to file"));
224                 else synfig::error(_("Unable to write file header to file"));
225                 return false;
226         }
227
228         if(!fwrite(&infoheader,sizeof(BITMAPINFOHEADER),1,file))
229         {
230                 if(callback)callback->error(_("Unable to write info header"));
231                 else synfig::error(_("Unable to write info header"));
232                 return false;
233         }
234
235         delete [] buffer;
236         buffer=new unsigned char[rowspan];
237
238         delete [] color_buffer;
239         color_buffer=new Color[desc.get_w()];
240
241         return true;
242 }
243
244 Color *
245 bmp::start_scanline(int /*scanline*/)
246 {
247         return color_buffer;
248 }
249
250 bool
251 bmp::end_scanline()
252 {
253         if(!file)
254                 return false;
255
256         convert_color_format(buffer, color_buffer, desc.get_w(), pf, gamma());
257
258         if(!fwrite(buffer,1,rowspan,file))
259                 return false;
260
261         return true;
262 }