File library for Bitmap images (*.bmp, *.dib). Currently supports only Windows V3 format with 24-bit-per-pixel color depth.
BMPFile.h@2:89b273c12b0a, 2015-03-05 (annotated)
- Committer:
- kayekss
- Date:
- Thu Mar 05 01:56:29 2015 +0000
- Revision:
- 2:89b273c12b0a
- Parent:
- 1:8cf4beca9695
- Child:
- 3:be3e831a86c1
Fixed wrong array index in function "red"
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 | 2:89b273c12b0a | 87 | * This method returns -1 for outranged pixels. |
kayekss | 1:8cf4beca9695 | 88 | * @param x X-coordinate of the pixel. |
kayekss | 1:8cf4beca9695 | 89 | * @param y Y-coordinate of the pixel. |
kayekss | 1:8cf4beca9695 | 90 | */ |
kayekss | 1:8cf4beca9695 | 91 | int32_t green(uint32_t x, uint32_t y); |
kayekss | 1:8cf4beca9695 | 92 | |
kayekss | 1:8cf4beca9695 | 93 | /** Get blue value of specified pixel. |
kayekss | 2:89b273c12b0a | 94 | * This method returns -1 for outranged pixels. |
kayekss | 1:8cf4beca9695 | 95 | * @param x X-coordinate of the pixel. |
kayekss | 1:8cf4beca9695 | 96 | * @param y Y-coordinate of the pixel. |
kayekss | 1:8cf4beca9695 | 97 | */ |
kayekss | 1:8cf4beca9695 | 98 | int32_t blue(uint32_t x, uint32_t y); |
kayekss | 2:89b273c12b0a | 99 | |
kayekss | 2:89b273c12b0a | 100 | /** Get red value from palette. |
kayekss | 2:89b273c12b0a | 101 | * This method returns -1 for non-indexed image or outranged indexes. |
kayekss | 2:89b273c12b0a | 102 | * @param index Palette color index. |
kayekss | 2:89b273c12b0a | 103 | */ |
kayekss | 1:8cf4beca9695 | 104 | int32_t paletteRed(uint8_t index); |
kayekss | 1:8cf4beca9695 | 105 | |
kayekss | 2:89b273c12b0a | 106 | /** Get green 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 paletteGreen(uint8_t index); |
kayekss | 1:8cf4beca9695 | 111 | |
kayekss | 2:89b273c12b0a | 112 | /** Get blue 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 paletteBlue(uint8_t index); |
kayekss | 1:8cf4beca9695 | 117 | |
kayekss | 1:8cf4beca9695 | 118 | private: |
kayekss | 1:8cf4beca9695 | 119 | static uint32_t paletteElemSize(Format format); |
kayekss | 0:4617bf407fe5 | 120 | }; |
kayekss | 0:4617bf407fe5 | 121 | |
kayekss | 0:4617bf407fe5 | 122 | #endif |