A basic graphics package for the LPC4088 Display Module.
Dependents: lpc4088_displaymodule_demo_sphere sampleGUI sampleEmptyGUI lpc4088_displaymodule_fs_aid ... more
Fork of DMBasicGUI by
Diff: Application/Image.cpp
- Revision:
- 5:f4de114c31c3
- Parent:
- 1:46c8df4608c8
- Child:
- 6:7917b0894655
--- a/Application/Image.cpp Fri Dec 19 16:40:30 2014 +0100 +++ b/Application/Image.cpp Sun Dec 21 13:53:07 2014 +0100 @@ -20,6 +20,13 @@ #include "bmp.h" #include "lodepng.h" +struct eaimg_header_t +{ + char prefix[6]; + uint16_t width; + uint16_t height; +} __attribute__ ((packed)); + int Image::decode(const unsigned char* pDataIn, unsigned int sizeIn, Resolution resolution, ImageData_t* pDataOut) { Image::Type type = imageType(pDataIn, sizeIn); @@ -36,6 +43,7 @@ } else { return -1; } + pDataOut->pointerToFree = pDataOut->pixels; if (pDataOut->pixels != NULL) { unsigned char error = BMP_Decode((void*)pDataIn, (unsigned char*)pDataOut->pixels, @@ -48,6 +56,7 @@ return 0; } free(pDataOut->pixels); + pDataOut->pointerToFree = NULL; } } break; @@ -68,6 +77,7 @@ result = 0; if (resolution == RES_16BIT) { pDataOut->pixels = (uint16_t*)malloc(pDataOut->width * pDataOut->height * 2); + pDataOut->pointerToFree = pDataOut->pixels; if (pDataOut->pixels != NULL) { uint16_t* pConverted = pDataOut->pixels; @@ -88,6 +98,7 @@ } else if (resolution == RES_24BIT) { uint32_t* pConverted = (uint32_t*)malloc(pDataOut->width * pDataOut->height * 4); pDataOut->pixels = (uint16_t*)pConverted; + pDataOut->pointerToFree = pDataOut->pixels; if (pDataOut->pixels != NULL) { uint8_t* p = pTmp; @@ -110,22 +121,50 @@ } break; + case RAW: + { + eaimg_header_t* hdr = (eaimg_header_t*)pDataIn; + pDataOut->width = hdr->width; + pDataOut->height = hdr->height; + pDataOut->pointerToFree = malloc(sizeIn-sizeof(eaimg_header_t)); + pDataOut->pixels = (uint16_t*)pDataOut->pointerToFree; + pDataOut->res = RES_16BIT; + if (pDataOut->pixels != NULL) + { + memcpy(pDataOut->pixels, pDataIn+sizeof(eaimg_header_t), sizeIn-sizeof(eaimg_header_t)); + return 0; + } + } + break; + default: break; } pDataOut->pixels = NULL; + pDataOut->pointerToFree = NULL; pDataOut->width = 0; pDataOut->height = 0; + pDataOut->res = resolution; return result; } -int Image::decode(const char* filename, Resolution res, ImageData_t* pDataOut) +int Image::decode(const char* filename, Resolution res, ImageData_t* pDataOut, Mutex* pLock) { FILE* fh = NULL; uint8_t* buff = NULL; int result = 1; + pDataOut->height = 0; + pDataOut->width = 0; + pDataOut->pixels = NULL; + pDataOut->pointerToFree = NULL; + pDataOut->res = res; + + if (pLock != NULL) { + pLock->lock(); + } + do { fh = fopen(filename, "r"); @@ -154,12 +193,25 @@ break; } - if (Image::decode(buff, size, res, pDataOut) == 1) { - break; + fclose(fh); + if (pLock != NULL) { + pLock->unlock(); + } + + Type type = imageType(buff, size); + if (type == RAW) { + pDataOut->width = ((eaimg_header_t*)buff)->width; + pDataOut->height = ((eaimg_header_t*)buff)->height; + pDataOut->pointerToFree = buff; + pDataOut->res = RES_16BIT; + pDataOut->pixels = (uint16_t*)(buff + sizeof(eaimg_header_t)); + } else { + result = Image::decode(buff, size, res, pDataOut); + free(buff); } // success - result = 0; + return 0; } while (false); @@ -169,6 +221,9 @@ if (buff != NULL) { free(buff); } + if (pLock != NULL) { + pLock->unlock(); + } return result; } @@ -185,6 +240,14 @@ { return BMP; } + if (sizeIn >= sizeof(eaimg_header_t)) + { + eaimg_header_t* hdr = (eaimg_header_t*)pDataIn; + if (memcmp(hdr->prefix, "eaimg:", 6) == 0) + { + return RAW; + } + } return UNKNOWN; }