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