Graphical demo for the LPC4088 Experiment Base Board with one of the Display Expansion Kits. This program decodes decodes and shows two png images.

Dependencies:   EALib mbed

Committer:
embeddedartists
Date:
Fri Oct 03 13:30:09 2014 +0000
Revision:
0:b567d56a59d7
First version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 0:b567d56a59d7 1
embeddedartists 0:b567d56a59d7 2 #include "mbed.h"
embeddedartists 0:b567d56a59d7 3 #include "Image.h"
embeddedartists 0:b567d56a59d7 4
embeddedartists 0:b567d56a59d7 5 #include "bmp.h"
embeddedartists 0:b567d56a59d7 6 #include "lodepng.h"
embeddedartists 0:b567d56a59d7 7
embeddedartists 0:b567d56a59d7 8 int Image::decode(const unsigned char* pDataIn, unsigned int sizeIn, ImageData_t* pDataOut)
embeddedartists 0:b567d56a59d7 9 {
embeddedartists 0:b567d56a59d7 10 Image::Type type = imageType(pDataIn, sizeIn);
embeddedartists 0:b567d56a59d7 11 int result = -1;
embeddedartists 0:b567d56a59d7 12 switch (type)
embeddedartists 0:b567d56a59d7 13 {
embeddedartists 0:b567d56a59d7 14 case BMP:
embeddedartists 0:b567d56a59d7 15 {
embeddedartists 0:b567d56a59d7 16 struct BMPHeader* hdr = (struct BMPHeader *) pDataIn;
embeddedartists 0:b567d56a59d7 17 pDataOut->pixels = (uint16_t*)malloc(hdr->width * hdr->height * 2);
embeddedartists 0:b567d56a59d7 18 if (pDataOut->pixels != NULL)
embeddedartists 0:b567d56a59d7 19 {
embeddedartists 0:b567d56a59d7 20 unsigned char error = BMP_Decode((void*)pDataIn, (unsigned char*)pDataOut->pixels, hdr->width, hdr->height, 24);
embeddedartists 0:b567d56a59d7 21 if (error == 0)
embeddedartists 0:b567d56a59d7 22 {
embeddedartists 0:b567d56a59d7 23 pDataOut->width = hdr->width;
embeddedartists 0:b567d56a59d7 24 pDataOut->height = hdr->height;
embeddedartists 0:b567d56a59d7 25 return 0;
embeddedartists 0:b567d56a59d7 26 }
embeddedartists 0:b567d56a59d7 27 free(pDataOut->pixels);
embeddedartists 0:b567d56a59d7 28 }
embeddedartists 0:b567d56a59d7 29 }
embeddedartists 0:b567d56a59d7 30 break;
embeddedartists 0:b567d56a59d7 31
embeddedartists 0:b567d56a59d7 32 case PNG:
embeddedartists 0:b567d56a59d7 33 {
embeddedartists 0:b567d56a59d7 34 unsigned char* pTmp;
embeddedartists 0:b567d56a59d7 35 unsigned error = lodepng_decode24(&pTmp, &pDataOut->width, &pDataOut->height, pDataIn, sizeIn);
embeddedartists 0:b567d56a59d7 36 if (error == 0)
embeddedartists 0:b567d56a59d7 37 {
embeddedartists 0:b567d56a59d7 38 int x, y;
embeddedartists 0:b567d56a59d7 39 uint16_t* pConverted;
embeddedartists 0:b567d56a59d7 40 uint8_t r;
embeddedartists 0:b567d56a59d7 41 uint8_t g;
embeddedartists 0:b567d56a59d7 42 uint8_t b;
embeddedartists 0:b567d56a59d7 43 int off = 0;
embeddedartists 0:b567d56a59d7 44
embeddedartists 0:b567d56a59d7 45 pDataOut->pixels = (uint16_t*)malloc(pDataOut->width * pDataOut->height * 2);
embeddedartists 0:b567d56a59d7 46 if (pDataOut->pixels != NULL)
embeddedartists 0:b567d56a59d7 47 {
embeddedartists 0:b567d56a59d7 48 pConverted = pDataOut->pixels;
embeddedartists 0:b567d56a59d7 49
embeddedartists 0:b567d56a59d7 50 for (y = 0; y < pDataOut->height; y++) {
embeddedartists 0:b567d56a59d7 51 for (x = 0; x < pDataOut->width; x++) {
embeddedartists 0:b567d56a59d7 52 r = pTmp[off ];
embeddedartists 0:b567d56a59d7 53 g = pTmp[off + 1];
embeddedartists 0:b567d56a59d7 54 b = pTmp[off + 2];
embeddedartists 0:b567d56a59d7 55 *pConverted = (((unsigned short)r & 0xF8) << 8) |
embeddedartists 0:b567d56a59d7 56 (((unsigned short)g & 0xFC) << 3) |
embeddedartists 0:b567d56a59d7 57 (((unsigned short)b & 0xF8) >> 3);
embeddedartists 0:b567d56a59d7 58 pConverted++;
embeddedartists 0:b567d56a59d7 59 off += 3;
embeddedartists 0:b567d56a59d7 60 }
embeddedartists 0:b567d56a59d7 61 }
embeddedartists 0:b567d56a59d7 62 }
embeddedartists 0:b567d56a59d7 63 free(pTmp);
embeddedartists 0:b567d56a59d7 64 return 0;
embeddedartists 0:b567d56a59d7 65 }
embeddedartists 0:b567d56a59d7 66 }
embeddedartists 0:b567d56a59d7 67 break;
embeddedartists 0:b567d56a59d7 68
embeddedartists 0:b567d56a59d7 69 default:
embeddedartists 0:b567d56a59d7 70 break;
embeddedartists 0:b567d56a59d7 71 }
embeddedartists 0:b567d56a59d7 72
embeddedartists 0:b567d56a59d7 73 pDataOut->pixels = NULL;
embeddedartists 0:b567d56a59d7 74 pDataOut->width = 0;
embeddedartists 0:b567d56a59d7 75 pDataOut->height = 0;
embeddedartists 0:b567d56a59d7 76 return result;
embeddedartists 0:b567d56a59d7 77 }
embeddedartists 0:b567d56a59d7 78
embeddedartists 0:b567d56a59d7 79 Image::Type Image::imageType(const unsigned char* pDataIn, unsigned int sizeIn)
embeddedartists 0:b567d56a59d7 80 {
embeddedartists 0:b567d56a59d7 81 if (sizeIn > 4)
embeddedartists 0:b567d56a59d7 82 {
embeddedartists 0:b567d56a59d7 83 if (pDataIn[0] == 0x89 && pDataIn[1] == 'P' && pDataIn[2] == 'N' && pDataIn[3] == 'G')
embeddedartists 0:b567d56a59d7 84 {
embeddedartists 0:b567d56a59d7 85 return PNG;
embeddedartists 0:b567d56a59d7 86 }
embeddedartists 0:b567d56a59d7 87 }
embeddedartists 0:b567d56a59d7 88 if (BMP_IsValid((void*)pDataIn))
embeddedartists 0:b567d56a59d7 89 {
embeddedartists 0:b567d56a59d7 90 return BMP;
embeddedartists 0:b567d56a59d7 91 }
embeddedartists 0:b567d56a59d7 92 return UNKNOWN;
embeddedartists 0:b567d56a59d7 93 }
embeddedartists 0:b567d56a59d7 94
embeddedartists 0:b567d56a59d7 95
embeddedartists 0:b567d56a59d7 96