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

Files at this revision

API Documentation at this revision

Comitter:
kayekss
Date:
Sun Apr 05 14:19:49 2015 +0000
Parent:
2:89b273c12b0a
Commit message:
(1) Add constructor switch: fetch/not fetch image data. (2) RGB return value is doubled in 16-bit-per-pixel mode.

Changed in this revision

BMPFile.cpp Show annotated file Show diff for this revision Revisions of this file
BMPFile.h Show annotated file Show diff for this revision Revisions of this file
--- 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
--- a/BMPFile.h	Thu Mar 05 01:56:29 2015 +0000
+++ b/BMPFile.h	Sun Apr 05 14:19:49 2015 +0000
@@ -70,9 +70,15 @@
     uint8_t* data;
     
     /** Constructor of struct BMPFile.
-     *  @param filename  Input file name string.
+     *  @param filename   Input file name string.
+     *  @param fetchData  Fetch image data on construct. Default value: true.
      */
-    BMPFile(const char* filename);
+    BMPFile(const char* filename, bool fetchData=true);
+    /** Constructor of struct BMPFile.
+     *  @param fp         Input file pointer.
+     *  @param fetchData  Fetch image data on construct. Default value: true.
+     */
+    BMPFile(FILE* fp, bool fetchData=true);
     
     /** Destructor of struct BMPFile. */
     ~BMPFile();
@@ -116,6 +122,8 @@
     int32_t paletteBlue(uint8_t index);
 
 private:
+    void readFile(FILE* fp, bool fetchData);
+    void readImageData(FILE* fp);
     static uint32_t paletteElemSize(Format format);
 };