-#include <msp/core/inttypes.h>
#include "bmploader.h"
+#include <cstdint>
using namespace std;
template<typename T>
T decode(const char *data)
{
- const Msp::UInt8 *udata = reinterpret_cast<const Msp::UInt8 *>(data);
+ const uint8_t *udata = reinterpret_cast<const uint8_t *>(data);
T result = 0;
for(unsigned i=0; i<sizeof(T); ++i)
result |= udata[i]<<(i*8);
throw invalid_argument("BmpLoader::BmpLoader");
}
-bool BmpLoader::detect(const std::string &sig)
+bool BmpLoader::detect(const string &sig)
{
static const char bmp_sig[] = { 'B', 'M' };
if(sig.size()<sizeof(bmp_sig))
return !sig.compare(0, sizeof(bmp_sig), bmp_sig, sizeof(bmp_sig));
}
-void BmpLoader::load_(Image::Data &data)
+void BmpLoader::load_headers_(Image::Data &data)
{
char bm_header[14];
read_full(io, bm_header+sig_bytes, sizeof(bm_header)-sig_bytes);
if(!sig_bytes && (bm_header[0]!='B' || bm_header[1]!='M'))
throw bad_image_data("bitmap header mismatch");
- unsigned data_offset = decode<UInt32>(bm_header+10);
+ unsigned data_offset = decode<uint32_t>(bm_header+10);
char dib_header[124];
read_full(io, dib_header, 12);
- unsigned dib_length = min<unsigned>(decode<UInt32>(dib_header), sizeof(dib_header));
+ unsigned dib_length = min<unsigned>(decode<uint32_t>(dib_header), sizeof(dib_header));
if(dib_length<40)
throw bad_image_data("DIB header too short (very old bmp file?)");
read_full(io, dib_header+12, dib_length-12);
- data.width = decode<UInt32>(dib_header+4);
- Int32 height = decode<UInt32>(dib_header+8);
+ data.width = decode<uint32_t>(dib_header+4);
+ int32_t height = decode<uint32_t>(dib_header+8);
data.height = abs(height);
- unsigned color_planes = decode<UInt16>(dib_header+12);
+ unsigned color_planes = decode<uint16_t>(dib_header+12);
if(color_planes!=1)
throw bad_image_data("color_planes!=1");
- unsigned bits_per_pixel = decode<UInt16>(dib_header+14);
- unsigned compression = decode<UInt32>(dib_header+16);
+ unsigned bits_per_pixel = decode<uint16_t>(dib_header+14);
+ unsigned compression = decode<uint32_t>(dib_header+16);
if(compression)
throw unsupported_image_format("compression not supported");
skip -= size;
}
- data.pixels = new char[data.stride*data.height];
- if(height<0)
+ invert_row_order = (height<0);
+}
+
+void BmpLoader::load_pixels_(Image::Data &data)
+{
+ if(invert_row_order)
{
for(unsigned y=0; y<data.height; ++y)
read_full(io, data.pixels+(data.height-1-y)*data.stride, data.stride);