Avoid some warnings for unused parameter.
[synfig.git] / synfig-core / 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 **      $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 /* === H E A D E R S ======================================================= */
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$");
55
56 /* === M E T H O D S ======================================================= */
57 namespace synfig {
58
59 struct BITMAPFILEHEADER
60 {
61         unsigned char   bfType[2];
62         unsigned long   bfSize;
63         unsigned short  bfReserved1;
64         unsigned short  bfReserved2;
65         unsigned long   bfOffsetBits;
66 };
67
68 struct BITMAPINFOHEADER
69 {
70         unsigned long   biSize;
71         long                    biWidth;
72         long                    biHeight;
73         unsigned short  biPlanes;
74         unsigned short  biBitCount;
75         unsigned long   biCompression;
76         unsigned long   biSizeImage;
77         long                    biXPelsPerMeter;
78         long                    biYPelsPerMeter;
79         unsigned long   biClrUsed;
80         unsigned long   biClrImportant;
81 };
82
83 }
84
85 #ifdef WORDS_BIGENDIAN
86 inline long little_endian(const long &x)
87 {
88         long ret;
89         char *big_e=(char *)&ret;
90         char *lit_e=(char *)&x;
91         big_e[0]=lit_e[3];
92         big_e[1]=lit_e[2];
93         big_e[2]=lit_e[1];
94         big_e[3]=lit_e[0];
95         return ret;
96 }
97 inline short little_endian_short(const short &x)
98 {
99         short ret;
100         char *big_e=(char *)&ret;
101         char *lit_e=(char *)&x;
102         big_e[0]=lit_e[1];
103         big_e[1]=lit_e[0];
104         return ret;
105 }
106 #else
107 #define little_endian(x)        (x)
108 #define little_endian_short(x)  (x)
109 #endif
110
111
112
113
114
115
116 bmp_mptr::bmp_mptr(const char *file)
117 {
118         filename=file;
119 }
120
121 bmp_mptr::~bmp_mptr()
122 {
123 }
124
125 bool
126 bmp_mptr::get_frame(synfig::Surface &surface, const synfig::RendDesc &/*renddesc*/, Time /*time*/, synfig::ProgressCallback *cb)
127 {
128         FILE *file=fopen(filename.c_str(),"rb");
129         if(!file)
130         {
131                 if(cb)cb->error("bmp_mptr::GetFrame(): "+strprintf(_("Unable to open %s"),filename.c_str()));
132                 else synfig::error("bmp_mptr::GetFrame(): "+strprintf(_("Unable to open %s"),filename.c_str()));
133                 return false;
134         }
135
136         synfig::BITMAPFILEHEADER fileheader;
137         synfig::BITMAPINFOHEADER infoheader;
138         char b_char=fgetc(file);
139         char m_char=fgetc(file);
140
141         if(b_char!='B' || m_char!='M')
142         {
143                 if(cb)cb->error("bmp_mptr::GetFrame(): "+strprintf(_("%s is not in BMP format"),filename.c_str()));
144                 else synfig::error("bmp_mptr::GetFrame(): "+strprintf(_("%s is not in BMP format"),filename.c_str()));
145                 return false;
146         }
147
148         if(fread(&fileheader.bfSize, 1, sizeof(synfig::BITMAPFILEHEADER)-4, file)!=sizeof(synfig::BITMAPFILEHEADER)-4)
149         {
150                 String str("bmp_mptr::get_frame(): "+strprintf(_("Failure while reading BITMAPFILEHEADER from %s"),filename.c_str()));
151                 if(cb)cb->error(str);
152                 else synfig::error(str);
153                 return false;
154         }
155
156         if(fread(&infoheader, 1, sizeof(synfig::BITMAPINFOHEADER), file)!=sizeof(synfig::BITMAPINFOHEADER))
157         {
158                 String str("bmp_mptr::get_frame(): "+strprintf(_("Failure while reading BITMAPINFOHEADER from %s"),filename.c_str()));
159                 if(cb)cb->error(str);
160                 else synfig::error(str);
161                 return false;
162         }
163
164         int offset=little_endian(fileheader.bfOffsetBits);
165
166         if(offset!=sizeof(synfig::BITMAPFILEHEADER)+sizeof(synfig::BITMAPINFOHEADER)-2)
167         {
168                 String str("bmp_mptr::get_frame(): "+strprintf(_("Bad BITMAPFILEHEADER in %s. (bfOffsetBits=%d, should be %d)"),filename.c_str(),offset,sizeof(synfig::BITMAPFILEHEADER)+sizeof(synfig::BITMAPINFOHEADER)-2));
169                 if(cb)cb->error(str);
170                 else synfig::error(str);
171                 return false;
172         }
173
174         if(little_endian(infoheader.biSize)!=little_endian(40))
175         {
176                 String str("bmp_mptr::get_frame(): "+strprintf(_("Bad BITMAPINFOHEADER in %s. (biSize=%d, should be 40)"),filename.c_str(),little_endian(infoheader.biSize)));
177                 if(cb)cb->error(str);
178                 else synfig::error(str);
179                 return false;
180         }
181
182         int w,h,bit_count;
183
184         w=little_endian(infoheader.biWidth);
185         h=little_endian(infoheader.biHeight);
186         bit_count=little_endian_short(infoheader.biBitCount);
187
188         synfig::warning("w:%d\n",w);
189         synfig::warning("h:%d\n",h);
190         synfig::warning("bit_count:%d\n",bit_count);
191
192         if(little_endian(infoheader.biCompression))
193         {
194                 if(cb)cb->error("bmp_mptr::GetFrame(): "+string(_("Reading compressed bitmaps is not supported")));
195                 else synfig::error("bmp_mptr::GetFrame(): "+string(_("Reading compressed bitmaps is not supported")));
196                 return false;
197         }
198
199         if(bit_count!=24 && bit_count!=32)
200         {
201                 if(cb)cb->error("bmp_mptr::GetFrame(): "+strprintf(_("Unsupported bit depth (bit_count=%d, should be 24 or 32)"),bit_count));
202                 else synfig::error("bmp_mptr::GetFrame(): "+strprintf(_("Unsupported bit depth (bit_count=%d, should be 24 or 32)"),bit_count));
203                 return false;
204         }
205
206         int x;
207         int y;
208         surface.set_wh(w,h);
209         for(y=0;y<surface.get_h();y++)
210                 for(x=0;x<surface.get_w();x++)
211                 {
212 //                      float b=(float)(unsigned char)fgetc(file)*(1.0/255.0);
213 //                      float g=(float)(unsigned char)fgetc(file)*(1.0/255.0);
214 //                      float r=(float)(unsigned char)fgetc(file)*(1.0/255.0);
215                         float b=gamma().b_U8_to_F32((unsigned char)fgetc(file));
216                         float g=gamma().g_U8_to_F32((unsigned char)fgetc(file));
217                         float r=gamma().r_U8_to_F32((unsigned char)fgetc(file));
218
219                         surface[h-y-1][x]=Color(
220                                 r,
221                                 g,
222                                 b,
223                                 1.0
224                         );
225                         if(bit_count==32)
226                                 fgetc(file);
227                 }
228
229
230         fclose(file);
231         return true;
232 }
233