1 /* === S Y N F I G ========================================================= */
2 /*! \file mptr_bmp.cpp
3 ** \brief bmp Target Module
4 **
5 ** $Id$
6 **
7 ** \legal
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
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.
14 **
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.
19 ** \endlegal
20 **
21 ** === N O T E S ===========================================================
22 **
23 ** ========================================================================= */
24
25 /* === H E A D E R S ======================================================= */
26
27 #ifdef USING_PCH
28 # include "pch.h"
29 #else
30 #ifdef HAVE_CONFIG_H
31 # include <config.h>
32 #endif
33
34 #include "mptr_bmp.h"
35 #include <synfig/general.h>
36 #include <synfig/localization.h>
37 #include <synfig/surface.h>
38
39 #include <algorithm>
40 #include <functional>
41 #endif
42
43 /* === U S I N G =========================================================== */
44
45 using namespace synfig;
46 using namespace std;
47 using namespace etl;
48
49 /* === G L O B A L S ======================================================= */
50
51 SYNFIG_IMPORTER_INIT(bmp_mptr);
52 SYNFIG_IMPORTER_SET_NAME(bmp_mptr,"bmp");
53 SYNFIG_IMPORTER_SET_EXT(bmp_mptr,"bmp");
54 SYNFIG_IMPORTER_SET_VERSION(bmp_mptr,"0.1");
55 SYNFIG_IMPORTER_SET_CVS_ID(bmp_mptr,"$Id$");
56 SYNFIG_IMPORTER_SET_SUPPORTS_FILE_SYSTEM_WRAPPER(bmp_mptr, true);
57
58 /* === M E T H O D S ======================================================= */
59 namespace synfig {
60
61 #pragma pack(push, 1)
62
63 namespace BITMAP
64 {
65 typedef unsigned char uint8_t;
66 typedef unsigned short int uint16_t;
67 typedef int int32_t;
68 typedef unsigned int uint32_t;
69
70 struct FILEHEADER
71 {
72 uint8_t bfType[2];
73 uint32_t bfSize;
74 uint16_t bfReserved1;
75 uint16_t bfReserved2;
76 uint32_t bfOffsetBits;
77 };
78
79 struct INFOHEADER
80 {
81 uint32_t biSize;
82 int32_t biWidth;
83 int32_t biHeight;
84 uint16_t biPlanes;
85 uint16_t biBitCount;
86 uint32_t biCompression;
87 uint32_t biSizeImage;
88 int32_t biXPelsPerMeter;
89 int32_t biYPelsPerMeter;
90 uint32_t biClrUsed;
91 uint32_t biClrImportant;
92 };
93 }
94
95
96 #pragma pack(pop)
97
98 }
99
100 #ifdef WORDS_BIGENDIAN
little_endian(const long & x)101 inline long little_endian(const long &x)
102 {
103 long ret;
104 char *big_e=(char *)&ret;
105 char *lit_e=(char *)&x;
106 big_e[0]=lit_e[3];
107 big_e[1]=lit_e[2];
108 big_e[2]=lit_e[1];
109 big_e[3]=lit_e[0];
110 return ret;
111 }
little_endian_short(const short & x)112 inline short little_endian_short(const short &x)
113 {
114 short ret;
115 char *big_e=(char *)&ret;
116 char *lit_e=(char *)&x;
117 big_e[0]=lit_e[1];
118 big_e[1]=lit_e[0];
119 return ret;
120 }
121 #else
122 #define little_endian(x) (x)
123 #define little_endian_short(x) (x)
124 #endif
125
126
127 bool
get_frame(synfig::Surface & surface,const synfig::RendDesc &,Time,synfig::ProgressCallback * cb)128 bmp_mptr::get_frame(synfig::Surface &surface, const synfig::RendDesc &/*renddesc*/, Time /*time*/, synfig::ProgressCallback *cb)
129 {
130 FileSystem::ReadStream::Handle stream = identifier.get_read_stream();
131 if(!stream)
132 {
133 if(cb)cb->error("bmp_mptr::GetFrame(): "+strprintf(_("Unable to open %s"),identifier.filename.c_str()));
134 else synfig::error("bmp_mptr::GetFrame(): "+strprintf(_("Unable to open %s"),identifier.filename.c_str()));
135 return false;
136 }
137
138 synfig::BITMAP::FILEHEADER fileheader;
139 synfig::BITMAP::INFOHEADER infoheader;
140
141 if (!stream->read_variable(fileheader.bfType)
142 || fileheader.bfType[0] != 'B'
143 || fileheader.bfType[1] != 'M')
144 {
145 if(cb)cb->error("bmp_mptr::GetFrame(): "+strprintf(_("%s is not in BMP format"),identifier.filename.c_str()));
146 else synfig::error("bmp_mptr::GetFrame(): "+strprintf(_("%s is not in BMP format"),identifier.filename.c_str()));
147 return false;
148 }
149
150 if(!stream->read_whole_block(&fileheader.bfSize, sizeof(synfig::BITMAP::FILEHEADER)-2))
151 {
152 String str("bmp_mptr::get_frame(): "+strprintf(_("Failure while reading BITMAP::FILEHEADER from %s"),identifier.filename.c_str()));
153 if(cb)cb->error(str);
154 else synfig::error(str);
155 return false;
156 }
157
158 if(!stream->read_whole_block(&infoheader, sizeof(synfig::BITMAP::INFOHEADER)))
159 {
160 String str("bmp_mptr::get_frame(): "+strprintf(_("Failure while reading BITMAP::INFOHEADER from %s"),identifier.filename.c_str()));
161 if(cb)cb->error(str);
162 else synfig::error(str);
163 return false;
164 }
165
166 int offset=little_endian(fileheader.bfOffsetBits);
167
168 if(offset!=sizeof(synfig::BITMAP::FILEHEADER)+sizeof(synfig::BITMAP::INFOHEADER))
169 {
170 String str("bmp_mptr::get_frame(): "+strprintf(_("Bad BITMAP::FILEHEADER in %s. (bfOffsetBits=%d, should be %d)"),identifier.filename.c_str(),offset,sizeof(synfig::BITMAP::FILEHEADER)+sizeof(synfig::BITMAP::INFOHEADER)));
171 if(cb)cb->error(str);
172 else synfig::error(str);
173 return false;
174 }
175
176 if(little_endian(infoheader.biSize)!=sizeof(synfig::BITMAP::INFOHEADER))
177 {
178 String str("bmp_mptr::get_frame(): "+strprintf(_("Bad BITMAP::INFOHEADER in %s. (biSize=%d, should be %d)"),identifier.filename.c_str(),little_endian(infoheader.biSize),sizeof(synfig::BITMAP::INFOHEADER)));
179 if(cb)cb->error(str);
180 else synfig::error(str);
181 return false;
182 }
183
184 int w,h,bit_count;
185
186 w=little_endian(infoheader.biWidth);
187 h=little_endian(infoheader.biHeight);
188 bit_count=little_endian_short(infoheader.biBitCount);
189
190 synfig::warning("w:%d\n",w);
191 synfig::warning("h:%d\n",h);
192 synfig::warning("bit_count:%d\n",bit_count);
193
194 if(little_endian(infoheader.biCompression))
195 {
196 if(cb)cb->error("bmp_mptr::GetFrame(): "+string(_("Reading compressed bitmaps is not supported")));
197 else synfig::error("bmp_mptr::GetFrame(): "+string(_("Reading compressed bitmaps is not supported")));
198 return false;
199 }
200
201 if(bit_count!=24 && bit_count!=32)
202 {
203 if(cb)cb->error("bmp_mptr::GetFrame(): "+strprintf(_("Unsupported bit depth (bit_count=%d, should be 24 or 32)"),bit_count));
204 else synfig::error("bmp_mptr::GetFrame(): "+strprintf(_("Unsupported bit depth (bit_count=%d, should be 24 or 32)"),bit_count));
205 return false;
206 }
207
208 int x;
209 int y;
210 surface.set_wh(w,h);
211 for(y=0;y<surface.get_h();y++)
212 for(x=0;x<surface.get_w();x++)
213 {
214 // float b=(float)(unsigned char)stream->getc()*(1.0/255.0);
215 // float g=(float)(unsigned char)stream->getc()*(1.0/255.0);
216 // float r=(float)(unsigned char)stream->getc()*(1.0/255.0);
217 float b=gamma().b_U8_to_F32((unsigned char)stream->get());
218 float g=gamma().g_U8_to_F32((unsigned char)stream->get());
219 float r=gamma().r_U8_to_F32((unsigned char)stream->get());
220
221 surface[h-y-1][x]=Color(
222 r,
223 g,
224 b,
225 1.0
226 );
227 if(bit_count==32)
228 stream->get();
229 }
230
231
232 return true;
233 }
234
235