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