|
| 1 | +#include "bmpimage.h" |
| 2 | +#define BUF_SIZE 64000 |
| 3 | + |
| 4 | +byte * _buffer = nullptr; |
| 5 | + |
| 6 | +uint16_t read16(File &f) { |
| 7 | + uint16_t result; |
| 8 | + f.read((uint8_t *)&result,2); |
| 9 | + return result; |
| 10 | +} |
| 11 | + |
| 12 | +uint32_t read32(File &f) { |
| 13 | + uint32_t result; |
| 14 | + f.read((uint8_t *)&result,4); |
| 15 | + return result; |
| 16 | +} |
| 17 | + |
| 18 | +bool BMPimage::init(const char * fn) { |
| 19 | + File bmpFile; |
| 20 | + int bmpDepth; |
| 21 | + //first, check if filename exists |
| 22 | + if (!WLED_FS.exists(fn)) { |
| 23 | + return false; |
| 24 | + } |
| 25 | + |
| 26 | + bmpFile = WLED_FS.open(fn); |
| 27 | + if (!bmpFile) { |
| 28 | + _valid=false; |
| 29 | + return false; |
| 30 | + } |
| 31 | + |
| 32 | + //so, the file exists and is opened |
| 33 | + // Parse BMP header |
| 34 | + uint16_t header = read16(bmpFile); |
| 35 | + if(header != 0x4D42) { // BMP signature |
| 36 | + _valid=false; |
| 37 | + bmpFile.close(); |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + //read and ingnore file size |
| 42 | + read32(bmpFile); |
| 43 | + (void)read32(bmpFile); // Read & ignore creator bytes |
| 44 | + _imageOffset = read32(bmpFile); // Start of image data |
| 45 | + // Read DIB header |
| 46 | + read32(bmpFile); |
| 47 | + _width = read32(bmpFile); |
| 48 | + _height = read32(bmpFile); |
| 49 | + if(read16(bmpFile) != 1) { // # planes -- must be '1' |
| 50 | + _valid=false; |
| 51 | + bmpFile.close(); |
| 52 | + return false; |
| 53 | + } |
| 54 | + bmpDepth = read16(bmpFile); // bits per pixel |
| 55 | + if((bmpDepth != 24) || (read32(bmpFile) != 0)) { // 0 = uncompressed { |
| 56 | + _width=0; |
| 57 | + _valid=false; |
| 58 | + bmpFile.close(); |
| 59 | + return false; |
| 60 | + } |
| 61 | + // If _height is negative, image is in top-down order. |
| 62 | + // This is not canon but has been observed in the wild. |
| 63 | + if(_height < 0) { |
| 64 | + _height = -_height; |
| 65 | + } |
| 66 | + //now, we have successfully got all the basics |
| 67 | + // BMP rows are padded (if needed) to 4-byte boundary |
| 68 | + _rowSize = (_width * 3 + 3) & ~3; |
| 69 | + //check image size - if it is too large, it will be unusable |
| 70 | + if (_rowSize*_height>BUF_SIZE) { |
| 71 | + _valid=false; |
| 72 | + bmpFile.close(); |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + bmpFile.close(); |
| 77 | + // Ensure filename fits our buffer (segment name length constraint). |
| 78 | + size_t len = strlen(fn); |
| 79 | + if (len > WLED_MAX_SEGNAME_LEN) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + strncpy(filename, fn, sizeof(filename)); |
| 83 | + filename[sizeof(filename) - 1] = '\0'; |
| 84 | + _valid = true; |
| 85 | + return true; |
| 86 | +} |
| 87 | + |
| 88 | +void BMPimage::clear(){ |
| 89 | + strcpy(filename, ""); |
| 90 | + _width=0; |
| 91 | + _height=0; |
| 92 | + _rowSize=0; |
| 93 | + _imageOffset=0; |
| 94 | + _loaded=false; |
| 95 | + _valid=false; |
| 96 | +} |
| 97 | + |
| 98 | +bool BMPimage::load(){ |
| 99 | + const size_t size = (size_t)_rowSize * (size_t)_height; |
| 100 | + if (size > BUF_SIZE) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + File bmpFile = WLED_FS.open(filename); |
| 104 | + if (!bmpFile) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + if (_buffer != nullptr) free(_buffer); |
| 109 | + _buffer = (byte*)malloc(size); |
| 110 | + if (_buffer == nullptr) return false; |
| 111 | + |
| 112 | + bmpFile.seek(_imageOffset); |
| 113 | + const size_t readBytes = bmpFile.read(_buffer, size); |
| 114 | + bmpFile.close(); |
| 115 | + if (readBytes != size) { |
| 116 | + _loaded = false; |
| 117 | + return false; |
| 118 | + } |
| 119 | + _loaded = true; |
| 120 | + return true; |
| 121 | +} |
| 122 | + |
| 123 | +byte* BMPimage::line(uint16_t n){ |
| 124 | + if (_loaded) { |
| 125 | + return (_buffer+n*_rowSize); |
| 126 | + } else { |
| 127 | + return NULL; |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +uint32_t BMPimage::pixelColor(uint16_t x, uint16_t y){ |
| 132 | + uint32_t pos; |
| 133 | + byte b,g,r; //colors |
| 134 | + if (! _loaded) { |
| 135 | + return 0; |
| 136 | + } |
| 137 | + if ( (x>=_width) || (y>=_height) ) { |
| 138 | + return 0; |
| 139 | + } |
| 140 | + pos=y*_rowSize + 3*x; |
| 141 | + //get colors. Note that in BMP files, they go in BGR order |
| 142 | + b= _buffer[pos++]; |
| 143 | + g= _buffer[pos++]; |
| 144 | + r= _buffer[pos]; |
| 145 | + return (r<<16|g<<8|b); |
| 146 | +} |
0 commit comments