]> git.tdb.fi Git - libs/gui.git/blobdiff - source/graphics/bmploader.cpp
Update .gitignore to include build products on Windows
[libs/gui.git] / source / graphics / bmploader.cpp
index 71de507d1f8b2681a76ddf3bf861a8677a4990e8..eb069a11dceea856174040ecf29427cbd36028f9 100644 (file)
@@ -1,5 +1,5 @@
-#include <msp/core/inttypes.h>
 #include "bmploader.h"
+#include <cstdint>
 
 using namespace std;
 
@@ -15,7 +15,7 @@ void read_full(Msp::IO::Base &io, char *buffer, unsigned size)
 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);
@@ -36,15 +36,15 @@ BmpLoader::BmpLoader(IO::Base &i, unsigned sb):
                throw invalid_argument("BmpLoader::BmpLoader");
 }
 
-bool BmpLoader::detect(const std::string &sig)
+bool BmpLoader::detect(const string &sig)
 {
-       static const char bmp_sig[] = "BM";
+       static const char bmp_sig[] = { 'B', 'M' };
        if(sig.size()<sizeof(bmp_sig))
                return false;
-       return !sig.compare(0, 2, 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);
@@ -52,25 +52,25 @@ void BmpLoader::load(Image::Data &data)
        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");
 
@@ -92,16 +92,20 @@ void BmpLoader::load(Image::Data &data)
                skip -= size;
        }
 
-       data.data = 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.data+(data.height-1-y)*data.stride, data.stride);
+                       read_full(io, data.pixels+(data.height-1-y)*data.stride, data.stride);
        }
        else
        {
                for(unsigned y=0; y<data.height; ++y)
-                       read_full(io, data.data+y*data.stride, data.stride);
+                       read_full(io, data.pixels+y*data.stride, data.stride);
        }
 }