1 /*! ========================================================================
4 ** $Id: mptr_bmp.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
8 ** This software and associated documentation
9 ** are CONFIDENTIAL and PROPRIETARY property of
10 ** the above-mentioned copyright holder.
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.
17 ** === N O T E S ===========================================================
19 ** ========================================================================= */
21 /* === H E A D E R S ======================================================= */
23 #define SINFG_NO_ANGLE
33 #include <sinfg/general.h>
34 #include <sinfg/surface.h>
40 /* === U S I N G =========================================================== */
42 using namespace sinfg;
46 /* === G L O B A L S ======================================================= */
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 $");
54 /* === M E T H O D S ======================================================= */
56 struct BITMAPFILEHEADER
58 unsigned char bfType[2];
60 unsigned short bfReserved1;
61 unsigned short bfReserved2;
62 unsigned long bfOffsetBits;
65 struct BITMAPINFOHEADER
70 unsigned short biPlanes;
71 unsigned short biBitCount;
72 unsigned long biCompression;
73 unsigned long biSizeImage;
76 unsigned long biClrUsed;
77 unsigned long biClrImportant;
80 #ifdef WORDS_BIGENDIAN
81 inline long little_endian(const long &x)
84 char *big_e=(char *)&ret;
85 char *lit_e=(char *)&x;
92 inline short little_endian_short(const short &x)
95 char *big_e=(char *)&ret;
96 char *lit_e=(char *)&x;
102 #define little_endian(x) (x)
103 #define little_endian_short(x) (x)
111 bmp_mptr::bmp_mptr(const char *file)
116 bmp_mptr::~bmp_mptr()
121 bmp_mptr::get_frame(sinfg::Surface &surface,Time, sinfg::ProgressCallback *cb)
123 FILE *file=fopen(filename.c_str(),"rb");
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()));
131 BITMAPFILEHEADER fileheader;
132 BITMAPINFOHEADER infoheader;
133 char b_char=fgetc(file);
134 char m_char=fgetc(file);
136 if(b_char!='B' || m_char!='M')
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()));
143 if(fread(&fileheader.bfSize, 1, sizeof(BITMAPFILEHEADER)-4, file)!=sizeof(BITMAPFILEHEADER)-4)
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);
151 if(fread(&infoheader, 1, sizeof(BITMAPINFOHEADER), file)!=sizeof(BITMAPINFOHEADER))
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);
159 int offset=little_endian(fileheader.bfOffsetBits);
161 if(offset!=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)-2)
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);
169 if(little_endian(infoheader.biSize)!=little_endian(40))
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);
179 w=little_endian(infoheader.biWidth);
180 h=little_endian(infoheader.biHeight);
181 bit_count=little_endian_short(infoheader.biBitCount);
183 sinfg::warning("w:%d\n",w);
184 sinfg::warning("h:%d\n",h);
185 sinfg::warning("bit_count:%d\n",bit_count);
187 if(little_endian(infoheader.biCompression))
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")));
194 if(bit_count!=24 && bit_count!=32)
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));
204 for(y=0;y<surface.get_h();y++)
205 for(x=0;x<surface.get_w();x++)
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));
214 surface[h-y-1][x]=Color(