1 /* === S I N F G =========================================================== */
3 ** \brief Bitmap Target
5 ** $Id: trgt_bmp.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $
8 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
14 ** You may not copy, print, publish, or in any
15 ** other way distribute this software without
16 ** a prior written agreement with
17 ** the copyright holder.
20 /* ========================================================================= */
22 /* === H E A D E R S ======================================================= */
24 #define SINFG_NO_ANGLE
34 #include <sinfg/general.h>
41 /* === U S I N G =========================================================== */
43 using namespace sinfg;
47 /* === I N F O ============================================================= */
49 SINFG_TARGET_INIT(bmp);
50 SINFG_TARGET_SET_NAME(bmp,"bmp");
51 SINFG_TARGET_SET_EXT(bmp,"bmp");
52 SINFG_TARGET_SET_VERSION(bmp,"0.1");
53 SINFG_TARGET_SET_CVS_ID(bmp,"$Id: trgt_bmp.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $");
55 /* === C L A S S E S & S T R U C T S ======================================= */
57 struct BITMAPFILEHEADER
59 unsigned char bfType[2];
61 unsigned short bfReserved1;
62 unsigned short bfReserved2;
63 unsigned long bfOffsetBits;
66 struct BITMAPINFOHEADER
71 unsigned short biPlanes;
72 unsigned short biBitCount;
73 unsigned long biCompression;
74 unsigned long biSizeImage;
77 unsigned long biClrUsed;
78 unsigned long biClrImportant;
81 /* === M E T H O D S ======================================================= */
83 #ifdef WORDS_BIGENDIAN
84 inline long little_endian(const long &x)
87 char *big_e=(char *)&ret;
88 char *lit_e=(char *)&x;
95 inline short little_endian_short(const short &x)
98 char *big_e=(char *)&ret;
99 char *lit_e=(char *)&x;
105 #define little_endian(x) (x)
106 #define little_endian_short(x) (x)
109 bmp::bmp(const char *Filename)
124 delete [] color_buffer;
128 bmp::set_rend_desc(RendDesc *given_desc)
132 // Flip the image upside down,
133 // because bitmaps are upside down.
134 Point tl=given_desc->get_tl();
135 Point br=given_desc->get_br();
136 Point::value_type tmp;
140 given_desc->set_tl(tl);
141 given_desc->set_br(br);
144 if(desc.get_frame_end()-desc.get_frame_start()>0)
147 imagecount=desc.get_frame_start();
160 delete [] color_buffer;
167 bmp::start_frame(sinfg::ProgressCallback *callback)
169 int w=desc.get_w(),h=desc.get_h();
171 rowspan=4*((w*(channels(pf)*8)+31)/32);
176 newfilename(filename),
177 ext(find(filename.begin(),filename.end(),'.'),filename.end());
178 newfilename.erase(find(newfilename.begin(),newfilename.end(),'.'),newfilename.end());
180 newfilename+=etl::strprintf("%04d",imagecount)+ext;
181 file=fopen(newfilename.c_str(),"wb");
182 if(callback)callback->task(newfilename+_(" (animated)"));
186 file=fopen(filename.c_str(),"wb");
187 if(callback)callback->task(filename);
192 if(callback)callback->error(_("Unable to open file"));
193 else sinfg::error(_("Unable to open file"));
197 BITMAPFILEHEADER fileheader;
198 BITMAPINFOHEADER infoheader;
200 fileheader.bfType[0]='B';
201 fileheader.bfType[1]='M';
202 fileheader.bfSize=little_endian(sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+rowspan*h);
203 fileheader.bfReserved1=0;
204 fileheader.bfReserved2=0;
205 fileheader.bfOffsetBits=little_endian(sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)-2);
207 infoheader.biSize=little_endian(40);
208 infoheader.biWidth=little_endian(w);
209 infoheader.biHeight=little_endian(h);
210 infoheader.biPlanes=little_endian_short((short)1);
211 infoheader.biBitCount=little_endian_short((short)(channels(pf)*8));
212 infoheader.biCompression=little_endian(0);
213 infoheader.biSizeImage=little_endian(0);
214 infoheader.biXPelsPerMeter=little_endian((int)rend_desc().get_x_res());
215 infoheader.biYPelsPerMeter=little_endian((int)rend_desc().get_y_res()); // pels per meter...?
216 infoheader.biClrUsed=little_endian(0);
217 infoheader.biClrImportant=little_endian(0);
221 if(!fwrite(&fileheader.bfSize,sizeof(BITMAPFILEHEADER)-4,1,file))
223 if(callback)callback->error(_("Unable to write file header to file"));
224 else sinfg::error(_("Unable to write file header to file"));
228 if(!fwrite(&infoheader,sizeof(BITMAPINFOHEADER),1,file))
230 if(callback)callback->error(_("Unable to write info header"));
231 else sinfg::error(_("Unable to write info header"));
236 buffer=new unsigned char[rowspan];
238 delete [] color_buffer;
239 color_buffer=new Color[desc.get_w()];
245 bmp::start_scanline(int scanline)
256 convert_color_format(buffer, color_buffer, desc.get_w(), pf, gamma());
258 if(!fwrite(buffer,1,rowspan,file))