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 ======================================================= */
35 #include <synfig/general.h>
36 #include <synfig/surface.h>
42 /* === U S I N G =========================================================== */
44 using namespace synfig;
48 /* === G L O B A L S ======================================================= */
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$");
56 /* === M E T H O D S ======================================================= */
59 struct BITMAPFILEHEADER
61 unsigned char bfType[2];
63 unsigned short bfReserved1;
64 unsigned short bfReserved2;
65 unsigned long bfOffsetBits;
68 struct BITMAPINFOHEADER
73 unsigned short biPlanes;
74 unsigned short biBitCount;
75 unsigned long biCompression;
76 unsigned long biSizeImage;
79 unsigned long biClrUsed;
80 unsigned long biClrImportant;
85 #ifdef WORDS_BIGENDIAN
86 inline long little_endian(const long &x)
89 char *big_e=(char *)&ret;
90 char *lit_e=(char *)&x;
97 inline short little_endian_short(const short &x)
100 char *big_e=(char *)&ret;
101 char *lit_e=(char *)&x;
107 #define little_endian(x) (x)
108 #define little_endian_short(x) (x)
116 bmp_mptr::bmp_mptr(const char *file)
121 bmp_mptr::~bmp_mptr()
126 bmp_mptr::get_frame(synfig::Surface &surface, const synfig::RendDesc &/*renddesc*/, Time /*time*/, synfig::ProgressCallback *cb)
128 FILE *file=fopen(filename.c_str(),"rb");
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()));
136 synfig::BITMAPFILEHEADER fileheader;
137 synfig::BITMAPINFOHEADER infoheader;
138 char b_char=fgetc(file);
139 char m_char=fgetc(file);
141 if(b_char!='B' || m_char!='M')
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()));
148 if(fread(&fileheader.bfSize, 1, sizeof(synfig::BITMAPFILEHEADER)-4, file)!=sizeof(synfig::BITMAPFILEHEADER)-4)
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);
156 if(fread(&infoheader, 1, sizeof(synfig::BITMAPINFOHEADER), file)!=sizeof(synfig::BITMAPINFOHEADER))
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);
164 int offset=little_endian(fileheader.bfOffsetBits);
166 if(offset!=sizeof(synfig::BITMAPFILEHEADER)+sizeof(synfig::BITMAPINFOHEADER)-2)
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);
174 if(little_endian(infoheader.biSize)!=little_endian(40))
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);
184 w=little_endian(infoheader.biWidth);
185 h=little_endian(infoheader.biHeight);
186 bit_count=little_endian_short(infoheader.biBitCount);
188 synfig::warning("w:%d\n",w);
189 synfig::warning("h:%d\n",h);
190 synfig::warning("bit_count:%d\n",bit_count);
192 if(little_endian(infoheader.biCompression))
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")));
199 if(bit_count!=24 && bit_count!=32)
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));
209 for(y=0;y<surface.get_h();y++)
210 for(x=0;x<surface.get_w();x++)
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));
219 surface[h-y-1][x]=Color(