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