Filled in the parameter names in the prototype of get_frame() to keep doxygen quiet...
[synfig.git] / synfig-core / trunk / src / modules / mod_bmp / mptr_bmp.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file mptr_bmp.cpp
3 **      \brief bmp Target Module
4 **
5 **      \legal
6 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
7 **
8 **      This package is free software; you can redistribute it and/or
9 **      modify it under the terms of the GNU General Public License as
10 **      published by the Free Software Foundation; either version 2 of
11 **      the License, or (at your option) any later version.
12 **
13 **      This package is distributed in the hope that it will be useful,
14 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
15 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 **      General Public License for more details.
17 **      \endlegal
18 **
19 ** === N O T E S ===========================================================
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 "mptr_bmp.h"
35 #include <synfig/general.h>
36 #include <synfig/surface.h>
37
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 /* === G L O B A L S ======================================================= */
49
50 SYNFIG_IMPORTER_INIT(bmp_mptr);
51 SYNFIG_IMPORTER_SET_NAME(bmp_mptr,"bmp");
52 SYNFIG_IMPORTER_SET_EXT(bmp_mptr,"bmp");
53 SYNFIG_IMPORTER_SET_VERSION(bmp_mptr,"0.1");
54 SYNFIG_IMPORTER_SET_CVS_ID(bmp_mptr,"$Id: mptr_bmp.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $");
55
56 /* === M E T H O D 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 #ifdef WORDS_BIGENDIAN
83 inline long little_endian(const long &x)
84 {
85         long ret;
86         char *big_e=(char *)&ret;
87         char *lit_e=(char *)&x;
88         big_e[0]=lit_e[3];
89         big_e[1]=lit_e[2];
90         big_e[2]=lit_e[1];
91         big_e[3]=lit_e[0];
92         return ret;
93 }
94 inline short little_endian_short(const short &x)
95 {
96         short ret;
97         char *big_e=(char *)&ret;
98         char *lit_e=(char *)&x;
99         big_e[0]=lit_e[1];
100         big_e[1]=lit_e[0];
101         return ret;
102 }
103 #else
104 #define little_endian(x)        (x)
105 #define little_endian_short(x)  (x)
106 #endif
107
108
109
110
111
112
113 bmp_mptr::bmp_mptr(const char *file)
114 {
115         filename=file;
116 }
117
118 bmp_mptr::~bmp_mptr()
119 {
120 }
121
122 bool
123 bmp_mptr::get_frame(synfig::Surface &surface,Time time, synfig::ProgressCallback *cb)
124 {
125         FILE *file=fopen(filename.c_str(),"rb");
126         if(!file)
127         {
128                 if(cb)cb->error("bmp_mptr::GetFrame(): "+strprintf(_("Unable to open %s"),filename.c_str()));
129                 else synfig::error("bmp_mptr::GetFrame(): "+strprintf(_("Unable to open %s"),filename.c_str()));
130                 return false;
131         }
132
133         BITMAPFILEHEADER fileheader;
134         BITMAPINFOHEADER infoheader;
135         char b_char=fgetc(file);
136         char m_char=fgetc(file);
137
138         if(b_char!='B' || m_char!='M')
139         {
140                 if(cb)cb->error("bmp_mptr::GetFrame(): "+strprintf(_("%s is not in BMP format"),filename.c_str()));
141                 else synfig::error("bmp_mptr::GetFrame(): "+strprintf(_("%s is not in BMP format"),filename.c_str()));
142                 return false;
143         }
144
145         if(fread(&fileheader.bfSize, 1, sizeof(BITMAPFILEHEADER)-4, file)!=sizeof(BITMAPFILEHEADER)-4)
146         {
147                 String str("bmp_mptr::get_frame(): "+strprintf(_("Failure while reading BITMAPFILEHEADER from %s"),filename.c_str()));
148                 if(cb)cb->error(str);
149                 else synfig::error(str);
150                 return false;
151         }
152
153         if(fread(&infoheader, 1, sizeof(BITMAPINFOHEADER), file)!=sizeof(BITMAPINFOHEADER))
154         {
155                 String str("bmp_mptr::get_frame(): "+strprintf(_("Failure while reading BITMAPINFOHEADER from %s"),filename.c_str()));
156                 if(cb)cb->error(str);
157                 else synfig::error(str);
158                 return false;
159         }
160
161         int offset=little_endian(fileheader.bfOffsetBits);
162
163         if(offset!=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)-2)
164         {
165                 String str("bmp_mptr::get_frame(): "+strprintf(_("Bad BITMAPFILEHEADER in %s. (bfOffsetBits=%d, should be %d)"),filename.c_str(),offset,sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)-2));
166                 if(cb)cb->error(str);
167                 else synfig::error(str);
168                 return false;
169         }
170
171         if(little_endian(infoheader.biSize)!=little_endian(40))
172         {
173                 String str("bmp_mptr::get_frame(): "+strprintf(_("Bad BITMAPINFOHEADER in %s. (biSize=%d, should be 40)"),filename.c_str(),little_endian(infoheader.biSize)));
174                 if(cb)cb->error(str);
175                 else synfig::error(str);
176                 return false;
177         }
178
179         int w,h,bit_count;
180
181         w=little_endian(infoheader.biWidth);
182         h=little_endian(infoheader.biHeight);
183         bit_count=little_endian_short(infoheader.biBitCount);
184
185         synfig::warning("w:%d\n",w);
186         synfig::warning("h:%d\n",h);
187         synfig::warning("bit_count:%d\n",bit_count);
188
189         if(little_endian(infoheader.biCompression))
190         {
191                 if(cb)cb->error("bmp_mptr::GetFrame(): "+string(_("Reading compressed bitmaps is not supported")));
192                 else synfig::error("bmp_mptr::GetFrame(): "+string(_("Reading compressed bitmaps is not supported")));
193                 return false;
194         }
195
196         if(bit_count!=24 && bit_count!=32)
197         {
198                 if(cb)cb->error("bmp_mptr::GetFrame(): "+strprintf(_("Unsupported bit depth (bit_count=%d, should be 24 or 32)"),bit_count));
199                 else synfig::error("bmp_mptr::GetFrame(): "+strprintf(_("Unsupported bit depth (bit_count=%d, should be 24 or 32)"),bit_count));
200                 return false;
201         }
202
203         int x;
204         int y;
205         surface.set_wh(w,h);
206         for(y=0;y<surface.get_h();y++)
207                 for(x=0;x<surface.get_w();x++)
208                 {
209 //                      float b=(float)(unsigned char)fgetc(file)*(1.0/255.0);
210 //                      float g=(float)(unsigned char)fgetc(file)*(1.0/255.0);
211 //                      float r=(float)(unsigned char)fgetc(file)*(1.0/255.0);
212                         float b=gamma().b_U8_to_F32((unsigned char)fgetc(file));
213                         float g=gamma().g_U8_to_F32((unsigned char)fgetc(file));
214                         float r=gamma().r_U8_to_F32((unsigned char)fgetc(file));
215
216                         surface[h-y-1][x]=Color(
217                                 r,
218                                 g,
219                                 b,
220                                 1.0
221                         );
222                         if(bit_count==32)
223                                 fgetc(file);
224                 }
225
226
227         fclose(file);
228         return true;
229 }
230