File library for Bitmap images (*.bmp, *.dib). Currently supports only Windows V3 format with 24-bit-per-pixel color depth.
BMPFile.h@1:8cf4beca9695, 2015-03-04 (annotated)
- Committer:
- kayekss
- Date:
- Wed Mar 04 20:56:34 2015 +0000
- Revision:
- 1:8cf4beca9695
- Parent:
- 0:4617bf407fe5
- Child:
- 2:89b273c12b0a
Support OS/2 V1 format and 1-, 4-, 8-, 16-, 32-bit-per-pixel color depth.
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:4617bf407fe5 | 73 | * @param filename Input file name string. |
kayekss | 0:4617bf407fe5 | 74 | */ |
kayekss | 0:4617bf407fe5 | 75 | BMPFile(const char* filename); |
kayekss | 0:4617bf407fe5 | 76 | |
kayekss | 0:4617bf407fe5 | 77 | /** Destructor of struct BMPFile. */ |
kayekss | 0:4617bf407fe5 | 78 | ~BMPFile(); |
kayekss | 1:8cf4beca9695 | 79 | |
kayekss | 1:8cf4beca9695 | 80 | /** Get red value of specified pixel. |
kayekss | 1:8cf4beca9695 | 81 | * @param x X-coordinate of the pixel. |
kayekss | 1:8cf4beca9695 | 82 | * @param y Y-coordinate of the pixel. |
kayekss | 1:8cf4beca9695 | 83 | */ |
kayekss | 1:8cf4beca9695 | 84 | int32_t red(uint32_t x, uint32_t y); |
kayekss | 1:8cf4beca9695 | 85 | |
kayekss | 1:8cf4beca9695 | 86 | /** Get green 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 green(uint32_t x, uint32_t y); |
kayekss | 1:8cf4beca9695 | 91 | |
kayekss | 1:8cf4beca9695 | 92 | /** Get blue value of specified pixel. |
kayekss | 1:8cf4beca9695 | 93 | * @param x X-coordinate of the pixel. |
kayekss | 1:8cf4beca9695 | 94 | * @param y Y-coordinate of the pixel. |
kayekss | 1:8cf4beca9695 | 95 | */ |
kayekss | 1:8cf4beca9695 | 96 | int32_t blue(uint32_t x, uint32_t y); |
kayekss | 1:8cf4beca9695 | 97 | |
kayekss | 1:8cf4beca9695 | 98 | int32_t paletteRed(uint8_t index); |
kayekss | 1:8cf4beca9695 | 99 | |
kayekss | 1:8cf4beca9695 | 100 | int32_t paletteGreen(uint8_t index); |
kayekss | 1:8cf4beca9695 | 101 | |
kayekss | 1:8cf4beca9695 | 102 | int32_t paletteBlue(uint8_t index); |
kayekss | 1:8cf4beca9695 | 103 | |
kayekss | 1:8cf4beca9695 | 104 | private: |
kayekss | 1:8cf4beca9695 | 105 | static uint32_t paletteElemSize(Format format); |
kayekss | 0:4617bf407fe5 | 106 | }; |
kayekss | 0:4617bf407fe5 | 107 | |
kayekss | 0:4617bf407fe5 | 108 | #endif |