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

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

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kayekss 0:4617bf407fe5 1 // ==================================================== Mar 04 2015, kayeks ==
kayekss 0:4617bf407fe5 2 // BMPFile.h
kayekss 0:4617bf407fe5 3 // ===========================================================================
kayekss 0:4617bf407fe5 4 // File library for Bitmap images (*.bmp, *.dib).
kayekss 0:4617bf407fe5 5
kayekss 0:4617bf407fe5 6 #ifndef BMPFILE_H_
kayekss 0:4617bf407fe5 7 #define BMPFILE_H_
kayekss 0:4617bf407fe5 8
kayekss 0:4617bf407fe5 9 #include "mbed.h"
kayekss 0:4617bf407fe5 10
kayekss 1:8cf4beca9695 11 #define ALIGN_BY_4(x) (((x) + 3) / 4 * 4)
kayekss 1:8cf4beca9695 12
kayekss 0:4617bf407fe5 13 struct BMPFile {
kayekss 0:4617bf407fe5 14 /** Object status.
kayekss 0:4617bf407fe5 15 * This indicates the error type when reading/parsing file is failed. */
kayekss 0:4617bf407fe5 16 enum Status {
kayekss 0:4617bf407fe5 17 Success /** Read successfully. (no errors) */
kayekss 0:4617bf407fe5 18 , NullFilename /** File name string is missing. */
kayekss 0:4617bf407fe5 19 , NoSuchFile /** File is missing or cannot be opened. */
kayekss 0:4617bf407fe5 20 , NotABitmapFile /** File is not a bitmap image. (wrong magic number) */
kayekss 0:4617bf407fe5 21 , UnsupportedFormat /** Unsupported bitmap format type. */
kayekss 0:4617bf407fe5 22 , UnsupportedDepth /** Unsupported color depth configuration.
kayekss 0:4617bf407fe5 23 This library does not support any indexed color depth (1/4/8 bpp). */
kayekss 0:4617bf407fe5 24 , AllocationFailed /** Data space allocation failed. */
kayekss 0:4617bf407fe5 25 };
kayekss 0:4617bf407fe5 26
kayekss 0:4617bf407fe5 27 /** Bitmap format type. */
kayekss 0:4617bf407fe5 28 enum Format {
kayekss 1:8cf4beca9695 29 OS2_V1 /** OS/2 bitmap version 1. header size is 12 bytes. */
kayekss 1:8cf4beca9695 30 , OS2_V2 /** OS/2 bitmap version 2. header size is 64 bytes. */
kayekss 1:8cf4beca9695 31 , Windows_V3 /** Windows bitmap version 3. header size is 40 bytes. */
kayekss 0:4617bf407fe5 32 , Windows_V4 /** Windows bitmap version 4. header size is 108 bytes. */
kayekss 0:4617bf407fe5 33 , Windows_V5 /** Windows bitmap version 5. header size is 124 bytes. */
kayekss 0:4617bf407fe5 34 , Unknown /** Unknown bitmap format type. */
kayekss 0:4617bf407fe5 35 };
kayekss 0:4617bf407fe5 36 static const char* StatusString[];
kayekss 0:4617bf407fe5 37 static const char* FormatString[];
kayekss 0:4617bf407fe5 38
kayekss 0:4617bf407fe5 39 /** The object status. */
kayekss 0:4617bf407fe5 40 Status status;
kayekss 0:4617bf407fe5 41
kayekss 0:4617bf407fe5 42 /** The bitmap format type. */
kayekss 0:4617bf407fe5 43 Format format;
kayekss 0:4617bf407fe5 44
kayekss 0:4617bf407fe5 45 /** The image file size in bytes, including its header part. */
kayekss 0:4617bf407fe5 46 uint32_t fileSize;
kayekss 1:8cf4beca9695 47
kayekss 1:8cf4beca9695 48 /** The image palette size in bytes. */
kayekss 1:8cf4beca9695 49 uint32_t paletteSize;
kayekss 0:4617bf407fe5 50
kayekss 0:4617bf407fe5 51 /** The image data part size in bytes. */
kayekss 0:4617bf407fe5 52 uint32_t dataSize;
kayekss 0:4617bf407fe5 53
kayekss 1:8cf4beca9695 54 /** The image stride (size per line) in bytes. */
kayekss 1:8cf4beca9695 55 uint32_t stride;
kayekss 1:8cf4beca9695 56
kayekss 0:4617bf407fe5 57 /** The image width in pixels. */
kayekss 0:4617bf407fe5 58 uint32_t width;
kayekss 0:4617bf407fe5 59
kayekss 0:4617bf407fe5 60 /** The image height in pixels. */
kayekss 0:4617bf407fe5 61 uint32_t height;
kayekss 0:4617bf407fe5 62
kayekss 0:4617bf407fe5 63 /** The color depth of the image in bit-per-pixel. */
kayekss 0:4617bf407fe5 64 uint16_t colorDepth;
kayekss 0:4617bf407fe5 65
kayekss 1:8cf4beca9695 66 /** The indexed color palette of the image (raw array). */
kayekss 1:8cf4beca9695 67 uint8_t* palette;
kayekss 1:8cf4beca9695 68
kayekss 0:4617bf407fe5 69 /** The data part of image (raw array). */
kayekss 0:4617bf407fe5 70 uint8_t* data;
kayekss 0:4617bf407fe5 71
kayekss 0:4617bf407fe5 72 /** Constructor of struct BMPFile.
kayekss 3:be3e831a86c1 73 * @param filename Input file name string.
kayekss 3:be3e831a86c1 74 * @param fetchData Fetch image data on construct. Default value: true.
kayekss 0:4617bf407fe5 75 */
kayekss 3:be3e831a86c1 76 BMPFile(const char* filename, bool fetchData=true);
kayekss 3:be3e831a86c1 77 /** Constructor of struct BMPFile.
kayekss 3:be3e831a86c1 78 * @param fp Input file pointer.
kayekss 3:be3e831a86c1 79 * @param fetchData Fetch image data on construct. Default value: true.
kayekss 3:be3e831a86c1 80 */
kayekss 3:be3e831a86c1 81 BMPFile(FILE* fp, bool fetchData=true);
kayekss 0:4617bf407fe5 82
kayekss 0:4617bf407fe5 83 /** Destructor of struct BMPFile. */
kayekss 0:4617bf407fe5 84 ~BMPFile();
kayekss 1:8cf4beca9695 85
kayekss 1:8cf4beca9695 86 /** Get red value of specified pixel.
kayekss 1:8cf4beca9695 87 * @param x X-coordinate of the pixel.
kayekss 1:8cf4beca9695 88 * @param y Y-coordinate of the pixel.
kayekss 1:8cf4beca9695 89 */
kayekss 1:8cf4beca9695 90 int32_t red(uint32_t x, uint32_t y);
kayekss 1:8cf4beca9695 91
kayekss 1:8cf4beca9695 92 /** Get green value of specified pixel.
kayekss 2:89b273c12b0a 93 * This method returns -1 for outranged pixels.
kayekss 1:8cf4beca9695 94 * @param x X-coordinate of the pixel.
kayekss 1:8cf4beca9695 95 * @param y Y-coordinate of the pixel.
kayekss 1:8cf4beca9695 96 */
kayekss 1:8cf4beca9695 97 int32_t green(uint32_t x, uint32_t y);
kayekss 1:8cf4beca9695 98
kayekss 1:8cf4beca9695 99 /** Get blue value of specified pixel.
kayekss 2:89b273c12b0a 100 * This method returns -1 for outranged pixels.
kayekss 1:8cf4beca9695 101 * @param x X-coordinate of the pixel.
kayekss 1:8cf4beca9695 102 * @param y Y-coordinate of the pixel.
kayekss 1:8cf4beca9695 103 */
kayekss 1:8cf4beca9695 104 int32_t blue(uint32_t x, uint32_t y);
kayekss 2:89b273c12b0a 105
kayekss 2:89b273c12b0a 106 /** Get red value from palette.
kayekss 2:89b273c12b0a 107 * This method returns -1 for non-indexed image or outranged indexes.
kayekss 2:89b273c12b0a 108 * @param index Palette color index.
kayekss 2:89b273c12b0a 109 */
kayekss 1:8cf4beca9695 110 int32_t paletteRed(uint8_t index);
kayekss 1:8cf4beca9695 111
kayekss 2:89b273c12b0a 112 /** Get green value from palette.
kayekss 2:89b273c12b0a 113 * This method returns -1 for non-indexed image or outranged indexes.
kayekss 2:89b273c12b0a 114 * @param index Palette color index.
kayekss 2:89b273c12b0a 115 */
kayekss 1:8cf4beca9695 116 int32_t paletteGreen(uint8_t index);
kayekss 1:8cf4beca9695 117
kayekss 2:89b273c12b0a 118 /** Get blue value from palette.
kayekss 2:89b273c12b0a 119 * This method returns -1 for non-indexed image or outranged indexes.
kayekss 2:89b273c12b0a 120 * @param index Palette color index.
kayekss 2:89b273c12b0a 121 */
kayekss 1:8cf4beca9695 122 int32_t paletteBlue(uint8_t index);
kayekss 1:8cf4beca9695 123
kayekss 1:8cf4beca9695 124 private:
kayekss 3:be3e831a86c1 125 void readFile(FILE* fp, bool fetchData);
kayekss 3:be3e831a86c1 126 void readImageData(FILE* fp);
kayekss 1:8cf4beca9695 127 static uint32_t paletteElemSize(Format format);
kayekss 0:4617bf407fe5 128 };
kayekss 0:4617bf407fe5 129
kayekss 0:4617bf407fe5 130 #endif