1 /* === S Y N F I G ========================================================= */
3 ** \brief bmp Target Module
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 ** === N O T E S ===========================================================
23 ** ========================================================================= */
25 /* === H E A D E R S ======================================================= */
27 #define SYNFIG_NO_ANGLE
37 #include <synfig/general.h>
38 #include <synfig/surface.h>
44 /* === U S I N G =========================================================== */
46 using namespace synfig;
50 /* === G L O B A L S ======================================================= */
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$");
58 /* === M E T H O D S ======================================================= */
60 struct BITMAPFILEHEADER
62 unsigned char bfType[2];
64 unsigned short bfReserved1;
65 unsigned short bfReserved2;
66 unsigned long bfOffsetBits;
69 struct BITMAPINFOHEADER
74 unsigned short biPlanes;
75 unsigned short biBitCount;
76 unsigned long biCompression;
77 unsigned long biSizeImage;
80 unsigned long biClrUsed;
81 unsigned long biClrImportant;
84 #ifdef WORDS_BIGENDIAN
85 inline long little_endian(const long &x)
88 char *big_e=(char *)&ret;
89 char *lit_e=(char *)&x;
96 inline short little_endian_short(const short &x)
99 char *big_e=(char *)&ret;
100 char *lit_e=(char *)&x;
106 #define little_endian(x) (x)
107 #define little_endian_short(x) (x)
115 bmp_mptr::bmp_mptr(const char *file)
120 bmp_mptr::~bmp_mptr()
125 bmp_mptr::get_frame(synfig::Surface &surface,Time time, synfig::ProgressCallback *cb)
127 FILE *file=fopen(filename.c_str(),"rb");
130 if(cb)cb->error("bmp_mptr::GetFrame(): "+strprintf(_("Unable to open %s"),filename.c_str()));
131 else synfig::error("bmp_mptr::GetFrame(): "+strprintf(_("Unable to open %s"),filename.c_str()));
135 BITMAPFILEHEADER fileheader;
136 BITMAPINFOHEADER infoheader;
137 char b_char=fgetc(file);
138 char m_char=fgetc(file);
140 if(b_char!='B' || m_char!='M')
142 if(cb)cb->error("bmp_mptr::GetFrame(): "+strprintf(_("%s is not in BMP format"),filename.c_str()));
143 else synfig::error("bmp_mptr::GetFrame(): "+strprintf(_("%s is not in BMP format"),filename.c_str()));
147 if(fread(&fileheader.bfSize, 1, sizeof(BITMAPFILEHEADER)-4, file)!=sizeof(BITMAPFILEHEADER)-4)
149 String str("bmp_mptr::get_frame(): "+strprintf(_("Failure while reading BITMAPFILEHEADER from %s"),filename.c_str()));
150 if(cb)cb->error(str);
151 else synfig::error(str);
155 if(fread(&infoheader, 1, sizeof(BITMAPINFOHEADER), file)!=sizeof(BITMAPINFOHEADER))
157 String str("bmp_mptr::get_frame(): "+strprintf(_("Failure while reading BITMAPINFOHEADER from %s"),filename.c_str()));
158 if(cb)cb->error(str);
159 else synfig::error(str);
163 int offset=little_endian(fileheader.bfOffsetBits);
165 if(offset!=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)-2)
167 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));
168 if(cb)cb->error(str);
169 else synfig::error(str);
173 if(little_endian(infoheader.biSize)!=little_endian(40))
175 String str("bmp_mptr::get_frame(): "+strprintf(_("Bad BITMAPINFOHEADER in %s. (biSize=%d, should be 40)"),filename.c_str(),little_endian(infoheader.biSize)));
176 if(cb)cb->error(str);
177 else synfig::error(str);
183 w=little_endian(infoheader.biWidth);
184 h=little_endian(infoheader.biHeight);
185 bit_count=little_endian_short(infoheader.biBitCount);
187 synfig::warning("w:%d\n",w);
188 synfig::warning("h:%d\n",h);
189 synfig::warning("bit_count:%d\n",bit_count);
191 if(little_endian(infoheader.biCompression))
193 if(cb)cb->error("bmp_mptr::GetFrame(): "+string(_("Reading compressed bitmaps is not supported")));
194 else synfig::error("bmp_mptr::GetFrame(): "+string(_("Reading compressed bitmaps is not supported")));
198 if(bit_count!=24 && bit_count!=32)
200 if(cb)cb->error("bmp_mptr::GetFrame(): "+strprintf(_("Unsupported bit depth (bit_count=%d, should be 24 or 32)"),bit_count));
201 else synfig::error("bmp_mptr::GetFrame(): "+strprintf(_("Unsupported bit depth (bit_count=%d, should be 24 or 32)"),bit_count));
208 for(y=0;y<surface.get_h();y++)
209 for(x=0;x<surface.get_w();x++)
211 // float b=(float)(unsigned char)fgetc(file)*(1.0/255.0);
212 // float g=(float)(unsigned char)fgetc(file)*(1.0/255.0);
213 // float r=(float)(unsigned char)fgetc(file)*(1.0/255.0);
214 float b=gamma().b_U8_to_F32((unsigned char)fgetc(file));
215 float g=gamma().g_U8_to_F32((unsigned char)fgetc(file));
216 float r=gamma().r_U8_to_F32((unsigned char)fgetc(file));
218 surface[h-y-1][x]=Color(