![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Graphical demo for the LPC4088 Experiment Base Board with one of the Display Expansion Kits. This demo shows a rotating 3D cube with and without textures.
Image.cpp@0:c828045bbe69, 2014-10-03 (annotated)
- Committer:
- embeddedartists
- Date:
- Fri Oct 03 12:48:37 2014 +0000
- Revision:
- 0:c828045bbe69
First version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
embeddedartists | 0:c828045bbe69 | 1 | |
embeddedartists | 0:c828045bbe69 | 2 | #include "mbed.h" |
embeddedartists | 0:c828045bbe69 | 3 | #include "Image.h" |
embeddedartists | 0:c828045bbe69 | 4 | |
embeddedartists | 0:c828045bbe69 | 5 | #include "bmp.h" |
embeddedartists | 0:c828045bbe69 | 6 | #include "lodepng.h" |
embeddedartists | 0:c828045bbe69 | 7 | |
embeddedartists | 0:c828045bbe69 | 8 | int Image::decode(const unsigned char* pDataIn, unsigned int sizeIn, ImageData_t* pDataOut) |
embeddedartists | 0:c828045bbe69 | 9 | { |
embeddedartists | 0:c828045bbe69 | 10 | Image::Type type = imageType(pDataIn, sizeIn); |
embeddedartists | 0:c828045bbe69 | 11 | int result = -1; |
embeddedartists | 0:c828045bbe69 | 12 | switch (type) |
embeddedartists | 0:c828045bbe69 | 13 | { |
embeddedartists | 0:c828045bbe69 | 14 | case BMP: |
embeddedartists | 0:c828045bbe69 | 15 | { |
embeddedartists | 0:c828045bbe69 | 16 | struct BMPHeader* hdr = (struct BMPHeader *) pDataIn; |
embeddedartists | 0:c828045bbe69 | 17 | pDataOut->pixels = (uint16_t*)malloc(hdr->width * hdr->height * 2); |
embeddedartists | 0:c828045bbe69 | 18 | if (pDataOut->pixels != NULL) |
embeddedartists | 0:c828045bbe69 | 19 | { |
embeddedartists | 0:c828045bbe69 | 20 | unsigned char error = BMP_Decode((void*)pDataIn, (unsigned char*)pDataOut->pixels, hdr->width, hdr->height, 24); |
embeddedartists | 0:c828045bbe69 | 21 | if (error == 0) |
embeddedartists | 0:c828045bbe69 | 22 | { |
embeddedartists | 0:c828045bbe69 | 23 | pDataOut->width = hdr->width; |
embeddedartists | 0:c828045bbe69 | 24 | pDataOut->height = hdr->height; |
embeddedartists | 0:c828045bbe69 | 25 | return 0; |
embeddedartists | 0:c828045bbe69 | 26 | } |
embeddedartists | 0:c828045bbe69 | 27 | free(pDataOut->pixels); |
embeddedartists | 0:c828045bbe69 | 28 | } |
embeddedartists | 0:c828045bbe69 | 29 | } |
embeddedartists | 0:c828045bbe69 | 30 | break; |
embeddedartists | 0:c828045bbe69 | 31 | |
embeddedartists | 0:c828045bbe69 | 32 | case PNG: |
embeddedartists | 0:c828045bbe69 | 33 | { |
embeddedartists | 0:c828045bbe69 | 34 | unsigned char* pTmp; |
embeddedartists | 0:c828045bbe69 | 35 | unsigned error = lodepng_decode24(&pTmp, &pDataOut->width, &pDataOut->height, pDataIn, sizeIn); |
embeddedartists | 0:c828045bbe69 | 36 | if (error == 0) |
embeddedartists | 0:c828045bbe69 | 37 | { |
embeddedartists | 0:c828045bbe69 | 38 | int x, y; |
embeddedartists | 0:c828045bbe69 | 39 | uint16_t* pConverted; |
embeddedartists | 0:c828045bbe69 | 40 | uint8_t r; |
embeddedartists | 0:c828045bbe69 | 41 | uint8_t g; |
embeddedartists | 0:c828045bbe69 | 42 | uint8_t b; |
embeddedartists | 0:c828045bbe69 | 43 | int off = 0; |
embeddedartists | 0:c828045bbe69 | 44 | |
embeddedartists | 0:c828045bbe69 | 45 | pDataOut->pixels = (uint16_t*)malloc(pDataOut->width * pDataOut->height * 2); |
embeddedartists | 0:c828045bbe69 | 46 | if (pDataOut->pixels != NULL) |
embeddedartists | 0:c828045bbe69 | 47 | { |
embeddedartists | 0:c828045bbe69 | 48 | pConverted = pDataOut->pixels; |
embeddedartists | 0:c828045bbe69 | 49 | |
embeddedartists | 0:c828045bbe69 | 50 | for (y = 0; y < pDataOut->height; y++) { |
embeddedartists | 0:c828045bbe69 | 51 | for (x = 0; x < pDataOut->width; x++) { |
embeddedartists | 0:c828045bbe69 | 52 | r = pTmp[off ]; |
embeddedartists | 0:c828045bbe69 | 53 | g = pTmp[off + 1]; |
embeddedartists | 0:c828045bbe69 | 54 | b = pTmp[off + 2]; |
embeddedartists | 0:c828045bbe69 | 55 | *pConverted = (((unsigned short)r & 0xF8) << 8) | |
embeddedartists | 0:c828045bbe69 | 56 | (((unsigned short)g & 0xFC) << 3) | |
embeddedartists | 0:c828045bbe69 | 57 | (((unsigned short)b & 0xF8) >> 3); |
embeddedartists | 0:c828045bbe69 | 58 | pConverted++; |
embeddedartists | 0:c828045bbe69 | 59 | off += 3; |
embeddedartists | 0:c828045bbe69 | 60 | } |
embeddedartists | 0:c828045bbe69 | 61 | } |
embeddedartists | 0:c828045bbe69 | 62 | } |
embeddedartists | 0:c828045bbe69 | 63 | free(pTmp); |
embeddedartists | 0:c828045bbe69 | 64 | return 0; |
embeddedartists | 0:c828045bbe69 | 65 | } |
embeddedartists | 0:c828045bbe69 | 66 | } |
embeddedartists | 0:c828045bbe69 | 67 | break; |
embeddedartists | 0:c828045bbe69 | 68 | |
embeddedartists | 0:c828045bbe69 | 69 | default: |
embeddedartists | 0:c828045bbe69 | 70 | break; |
embeddedartists | 0:c828045bbe69 | 71 | } |
embeddedartists | 0:c828045bbe69 | 72 | |
embeddedartists | 0:c828045bbe69 | 73 | pDataOut->pixels = NULL; |
embeddedartists | 0:c828045bbe69 | 74 | pDataOut->width = 0; |
embeddedartists | 0:c828045bbe69 | 75 | pDataOut->height = 0; |
embeddedartists | 0:c828045bbe69 | 76 | return result; |
embeddedartists | 0:c828045bbe69 | 77 | } |
embeddedartists | 0:c828045bbe69 | 78 | |
embeddedartists | 0:c828045bbe69 | 79 | Image::Type Image::imageType(const unsigned char* pDataIn, unsigned int sizeIn) |
embeddedartists | 0:c828045bbe69 | 80 | { |
embeddedartists | 0:c828045bbe69 | 81 | if (sizeIn > 4) |
embeddedartists | 0:c828045bbe69 | 82 | { |
embeddedartists | 0:c828045bbe69 | 83 | if (pDataIn[0] == 0x89 && pDataIn[1] == 'P' && pDataIn[2] == 'N' && pDataIn[3] == 'G') |
embeddedartists | 0:c828045bbe69 | 84 | { |
embeddedartists | 0:c828045bbe69 | 85 | return PNG; |
embeddedartists | 0:c828045bbe69 | 86 | } |
embeddedartists | 0:c828045bbe69 | 87 | } |
embeddedartists | 0:c828045bbe69 | 88 | if (BMP_IsValid((void*)pDataIn)) |
embeddedartists | 0:c828045bbe69 | 89 | { |
embeddedartists | 0:c828045bbe69 | 90 | return BMP; |
embeddedartists | 0:c828045bbe69 | 91 | } |
embeddedartists | 0:c828045bbe69 | 92 | return UNKNOWN; |
embeddedartists | 0:c828045bbe69 | 93 | } |
embeddedartists | 0:c828045bbe69 | 94 | |
embeddedartists | 0:c828045bbe69 | 95 |