File library for Bitmap images (*.bmp, *.dib). Currently supports only Windows V3 format with 24-bit-per-pixel color depth.

Revision:
3:be3e831a86c1
Parent:
2:89b273c12b0a
diff -r 89b273c12b0a -r be3e831a86c1 BMPFile.cpp
--- a/BMPFile.cpp	Thu Mar 05 01:56:29 2015 +0000
+++ b/BMPFile.cpp	Sun Apr 05 14:19:49 2015 +0000
@@ -23,30 +23,11 @@
     "Unknown"
 };
 
-BMPFile::BMPFile(const char* filename) {
-    FILE*    fp;
+void BMPFile::readFile(FILE* fp, bool fetchData) {
     uint8_t  buf8[2];
     uint16_t buf16[2];
     uint32_t buf32[3];
     
-    status = Success;
-    format = Unknown;
-    fileSize = 0;
-    paletteSize = 0;
-    dataSize = 0;
-    stride = 0;
-    width = 0;
-    height = 0;
-    colorDepth = 0;
-    palette = NULL;
-    data = NULL;
-    
-    // Open file
-    if (!filename) {
-        status = NullFilename;
-        return;
-    }
-    fp = fopen(filename, "rb");
     if (!fp) {
         status = NoSuchFile;
         return;
@@ -176,13 +157,54 @@
     }
     dataSize = stride * height;
     
-    // Allocate data space
-    data = new uint8_t[dataSize];
+    // Read image data
+    if (fetchData) {
+        readImageData(fp);
+    }
+    fclose(fp);
+}
+
+BMPFile::BMPFile(const char* filename, bool fetchData) {
+    FILE* fp;
+    
+    status = Success;
+    format = Unknown;
+    fileSize = 0;
+    paletteSize = 0;
+    dataSize = 0;
+    stride = 0;
+    width = 0;
+    height = 0;
+    colorDepth = 0;
+    palette = NULL;
+    data = NULL;
     
-    // Read bitmap data
-    fread(data, 1, dataSize, fp);
+    // Open file
+    if (!filename) {
+        status = NullFilename;
+        return;
+    }
+    fp = fopen(filename, "rb");
     
-    fclose(fp);
+    // Read file
+    readFile(fp, fetchData);
+}
+
+BMPFile::BMPFile(FILE* fp, bool fetch) {
+    status = Success;
+    format = Unknown;
+    fileSize = 0;
+    paletteSize = 0;
+    dataSize = 0;
+    stride = 0;
+    width = 0;
+    height = 0;
+    colorDepth = 0;
+    palette = NULL;
+    data = NULL;
+    
+    // Read file
+    readFile(fp, fetch);
 }
 
 BMPFile::~BMPFile() {
@@ -194,6 +216,14 @@
     }
 }
 
+void BMPFile::readImageData(FILE* fp) {
+    // Allocate data space
+    data = new uint8_t[dataSize];
+    
+    // Read bitmap data
+    fread(data, 1, dataSize, fp);
+}
+
 uint32_t BMPFile::paletteElemSize(BMPFile::Format format) {
     switch (format) {
     case OS2_V1: case OS2_V2:
@@ -270,7 +300,7 @@
     case 8:  // Indexed from palette
         return paletteRed(data[stride * y + x]);
     case 16:  // BGR565 (bbbbbggg:gggrrrrr)
-        return data[stride * y + 2 * x + 1] & 0x1f;
+        return (data[stride * y + 2 * x + 1] & 0x1f) * 2;
     case 24:  // BGR888
         return data[stride * y + 3 * x + 2];
     case 32:  // BGRX8888
@@ -295,8 +325,8 @@
     case 8:  // Indexed from palette
         return paletteGreen(data[stride * y + x]);
     case 16:  // BGR565 (bbbbbggg:gggrrrrr)
-        return (data[stride * y + 2 * x] & 0x07) << 3
-               | data[stride * y + 2 * x + 1] >> 5;
+        return ((data[stride * y + 2 * x] & 0x07) << 3
+                | data[stride * y + 2 * x + 1] >> 5) * 2;
     case 24:  // BGR888
         return data[stride * y + 3 * x + 1];
     case 32:  // BGRX8888
@@ -321,7 +351,7 @@
     case 8:  // Indexed from palette
         return paletteBlue(data[stride * y + x]);
     case 16:  // RGB565 (bbbbbggg:gggrrrrr)
-        return data[stride * y + 2 * x] >> 3;
+        return (data[stride * y + 2 * x] >> 3) * 2;
     case 24:  // BGR888
         return data[stride * y + 3 * x];
     case 32:  // BGRX8888