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
Application/Image.cpp@10:651861441108, 2015-01-26 (annotated)
- Committer:
- embeddedartists
- Date:
- Mon Jan 26 10:06:58 2015 +0100
- Revision:
- 10:651861441108
- Parent:
- 6:7917b0894655
- Child:
- 17:6e2abf107800
- Added virtual destructors to avoid undefined behaviour
- Fixed compiler warnings
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
embeddedartists | 1:46c8df4608c8 | 1 | /* |
embeddedartists | 1:46c8df4608c8 | 2 | * Copyright 2014 Embedded Artists AB |
embeddedartists | 1:46c8df4608c8 | 3 | * |
embeddedartists | 1:46c8df4608c8 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
embeddedartists | 1:46c8df4608c8 | 5 | * you may not use this file except in compliance with the License. |
embeddedartists | 1:46c8df4608c8 | 6 | * You may obtain a copy of the License at |
embeddedartists | 1:46c8df4608c8 | 7 | * |
embeddedartists | 1:46c8df4608c8 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
embeddedartists | 1:46c8df4608c8 | 9 | * |
embeddedartists | 1:46c8df4608c8 | 10 | * Unless required by applicable law or agreed to in writing, software |
embeddedartists | 1:46c8df4608c8 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
embeddedartists | 1:46c8df4608c8 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
embeddedartists | 1:46c8df4608c8 | 13 | * See the License for the specific language governing permissions and |
embeddedartists | 1:46c8df4608c8 | 14 | * limitations under the License. |
embeddedartists | 1:46c8df4608c8 | 15 | */ |
embeddedartists | 0:4977187e90c7 | 16 | |
embeddedartists | 0:4977187e90c7 | 17 | #include "mbed.h" |
embeddedartists | 0:4977187e90c7 | 18 | #include "Image.h" |
embeddedartists | 0:4977187e90c7 | 19 | |
embeddedartists | 0:4977187e90c7 | 20 | #include "bmp.h" |
embeddedartists | 0:4977187e90c7 | 21 | #include "lodepng.h" |
embeddedartists | 0:4977187e90c7 | 22 | |
embeddedartists | 5:f4de114c31c3 | 23 | struct eaimg_header_t |
embeddedartists | 5:f4de114c31c3 | 24 | { |
embeddedartists | 5:f4de114c31c3 | 25 | char prefix[6]; |
embeddedartists | 5:f4de114c31c3 | 26 | uint16_t width; |
embeddedartists | 5:f4de114c31c3 | 27 | uint16_t height; |
embeddedartists | 5:f4de114c31c3 | 28 | } __attribute__ ((packed)); |
embeddedartists | 5:f4de114c31c3 | 29 | |
embeddedartists | 0:4977187e90c7 | 30 | int Image::decode(const unsigned char* pDataIn, unsigned int sizeIn, Resolution resolution, ImageData_t* pDataOut) |
embeddedartists | 0:4977187e90c7 | 31 | { |
embeddedartists | 0:4977187e90c7 | 32 | Image::Type type = imageType(pDataIn, sizeIn); |
embeddedartists | 0:4977187e90c7 | 33 | int result = -1; |
embeddedartists | 0:4977187e90c7 | 34 | switch (type) |
embeddedartists | 0:4977187e90c7 | 35 | { |
embeddedartists | 0:4977187e90c7 | 36 | case BMP: |
embeddedartists | 0:4977187e90c7 | 37 | { |
embeddedartists | 0:4977187e90c7 | 38 | struct BMPHeader* hdr = (struct BMPHeader *) pDataIn; |
embeddedartists | 0:4977187e90c7 | 39 | if (resolution == RES_16BIT) { |
embeddedartists | 0:4977187e90c7 | 40 | pDataOut->pixels = (uint16_t*)malloc(hdr->width * hdr->height * 2); |
embeddedartists | 0:4977187e90c7 | 41 | } else if (resolution == RES_24BIT) { |
embeddedartists | 0:4977187e90c7 | 42 | pDataOut->pixels = (uint16_t*)malloc(hdr->width * hdr->height * 4); |
embeddedartists | 0:4977187e90c7 | 43 | } else { |
embeddedartists | 0:4977187e90c7 | 44 | return -1; |
embeddedartists | 0:4977187e90c7 | 45 | } |
embeddedartists | 5:f4de114c31c3 | 46 | pDataOut->pointerToFree = pDataOut->pixels; |
embeddedartists | 0:4977187e90c7 | 47 | if (pDataOut->pixels != NULL) |
embeddedartists | 0:4977187e90c7 | 48 | { |
embeddedartists | 0:4977187e90c7 | 49 | unsigned char error = BMP_Decode((void*)pDataIn, (unsigned char*)pDataOut->pixels, |
embeddedartists | 0:4977187e90c7 | 50 | hdr->width, hdr->height, 24, ((resolution == RES_16BIT) ? 16 : 24)); |
embeddedartists | 0:4977187e90c7 | 51 | if (error == 0) |
embeddedartists | 0:4977187e90c7 | 52 | { |
embeddedartists | 0:4977187e90c7 | 53 | pDataOut->width = hdr->width; |
embeddedartists | 0:4977187e90c7 | 54 | pDataOut->height = hdr->height; |
embeddedartists | 0:4977187e90c7 | 55 | pDataOut->res = resolution; |
embeddedartists | 0:4977187e90c7 | 56 | return 0; |
embeddedartists | 0:4977187e90c7 | 57 | } |
embeddedartists | 0:4977187e90c7 | 58 | free(pDataOut->pixels); |
embeddedartists | 5:f4de114c31c3 | 59 | pDataOut->pointerToFree = NULL; |
embeddedartists | 0:4977187e90c7 | 60 | } |
embeddedartists | 0:4977187e90c7 | 61 | } |
embeddedartists | 0:4977187e90c7 | 62 | break; |
embeddedartists | 0:4977187e90c7 | 63 | |
embeddedartists | 0:4977187e90c7 | 64 | case PNG: |
embeddedartists | 0:4977187e90c7 | 65 | { |
embeddedartists | 0:4977187e90c7 | 66 | unsigned char* pTmp; |
embeddedartists | 10:651861441108 | 67 | unsigned error = lodepng_decode24(&pTmp, (unsigned*)&pDataOut->width, (unsigned*)&pDataOut->height, pDataIn, sizeIn); |
embeddedartists | 0:4977187e90c7 | 68 | pDataOut->res = resolution; |
embeddedartists | 0:4977187e90c7 | 69 | if (error == 0) |
embeddedartists | 0:4977187e90c7 | 70 | { |
embeddedartists | 10:651861441108 | 71 | uint32_t x, y; |
embeddedartists | 0:4977187e90c7 | 72 | uint8_t r; |
embeddedartists | 0:4977187e90c7 | 73 | uint8_t g; |
embeddedartists | 0:4977187e90c7 | 74 | uint8_t b; |
embeddedartists | 0:4977187e90c7 | 75 | int off = 0; |
embeddedartists | 0:4977187e90c7 | 76 | |
embeddedartists | 0:4977187e90c7 | 77 | result = 0; |
embeddedartists | 0:4977187e90c7 | 78 | if (resolution == RES_16BIT) { |
embeddedartists | 0:4977187e90c7 | 79 | pDataOut->pixels = (uint16_t*)malloc(pDataOut->width * pDataOut->height * 2); |
embeddedartists | 5:f4de114c31c3 | 80 | pDataOut->pointerToFree = pDataOut->pixels; |
embeddedartists | 0:4977187e90c7 | 81 | if (pDataOut->pixels != NULL) |
embeddedartists | 0:4977187e90c7 | 82 | { |
embeddedartists | 0:4977187e90c7 | 83 | uint16_t* pConverted = pDataOut->pixels; |
embeddedartists | 0:4977187e90c7 | 84 | |
embeddedartists | 0:4977187e90c7 | 85 | for (y = 0; y < pDataOut->height; y++) { |
embeddedartists | 0:4977187e90c7 | 86 | for (x = 0; x < pDataOut->width; x++) { |
embeddedartists | 0:4977187e90c7 | 87 | r = pTmp[off ]; |
embeddedartists | 0:4977187e90c7 | 88 | g = pTmp[off + 1]; |
embeddedartists | 0:4977187e90c7 | 89 | b = pTmp[off + 2]; |
embeddedartists | 0:4977187e90c7 | 90 | *pConverted = (((unsigned short)r & 0xF8) << 8) | |
embeddedartists | 0:4977187e90c7 | 91 | (((unsigned short)g & 0xFC) << 3) | |
embeddedartists | 0:4977187e90c7 | 92 | (((unsigned short)b & 0xF8) >> 3); |
embeddedartists | 0:4977187e90c7 | 93 | pConverted++; |
embeddedartists | 0:4977187e90c7 | 94 | off += 3; |
embeddedartists | 0:4977187e90c7 | 95 | } |
embeddedartists | 0:4977187e90c7 | 96 | } |
embeddedartists | 0:4977187e90c7 | 97 | } |
embeddedartists | 0:4977187e90c7 | 98 | } else if (resolution == RES_24BIT) { |
embeddedartists | 0:4977187e90c7 | 99 | uint32_t* pConverted = (uint32_t*)malloc(pDataOut->width * pDataOut->height * 4); |
embeddedartists | 0:4977187e90c7 | 100 | pDataOut->pixels = (uint16_t*)pConverted; |
embeddedartists | 5:f4de114c31c3 | 101 | pDataOut->pointerToFree = pDataOut->pixels; |
embeddedartists | 0:4977187e90c7 | 102 | if (pDataOut->pixels != NULL) |
embeddedartists | 0:4977187e90c7 | 103 | { |
embeddedartists | 0:4977187e90c7 | 104 | uint8_t* p = pTmp; |
embeddedartists | 0:4977187e90c7 | 105 | int num = pDataOut->width * pDataOut->height; |
embeddedartists | 0:4977187e90c7 | 106 | for (int i = 0; i < num; i++) { |
embeddedartists | 0:4977187e90c7 | 107 | uint32_t a = 0; |
embeddedartists | 0:4977187e90c7 | 108 | a |= (*p++) << 16; // red |
embeddedartists | 0:4977187e90c7 | 109 | a |= (*p++) << 8; // green |
embeddedartists | 0:4977187e90c7 | 110 | a |= (*p++) << 0; // blue |
embeddedartists | 0:4977187e90c7 | 111 | *pConverted++ = a; |
embeddedartists | 0:4977187e90c7 | 112 | } |
embeddedartists | 0:4977187e90c7 | 113 | } |
embeddedartists | 0:4977187e90c7 | 114 | } else { |
embeddedartists | 0:4977187e90c7 | 115 | // unknown format |
embeddedartists | 0:4977187e90c7 | 116 | result = -2; |
embeddedartists | 0:4977187e90c7 | 117 | } |
embeddedartists | 0:4977187e90c7 | 118 | free(pTmp); |
embeddedartists | 0:4977187e90c7 | 119 | return result; |
embeddedartists | 0:4977187e90c7 | 120 | } |
embeddedartists | 0:4977187e90c7 | 121 | } |
embeddedartists | 0:4977187e90c7 | 122 | break; |
embeddedartists | 0:4977187e90c7 | 123 | |
embeddedartists | 5:f4de114c31c3 | 124 | case RAW: |
embeddedartists | 5:f4de114c31c3 | 125 | { |
embeddedartists | 5:f4de114c31c3 | 126 | eaimg_header_t* hdr = (eaimg_header_t*)pDataIn; |
embeddedartists | 5:f4de114c31c3 | 127 | pDataOut->width = hdr->width; |
embeddedartists | 5:f4de114c31c3 | 128 | pDataOut->height = hdr->height; |
embeddedartists | 5:f4de114c31c3 | 129 | pDataOut->pointerToFree = malloc(sizeIn-sizeof(eaimg_header_t)); |
embeddedartists | 5:f4de114c31c3 | 130 | pDataOut->pixels = (uint16_t*)pDataOut->pointerToFree; |
embeddedartists | 5:f4de114c31c3 | 131 | pDataOut->res = RES_16BIT; |
embeddedartists | 5:f4de114c31c3 | 132 | if (pDataOut->pixels != NULL) |
embeddedartists | 5:f4de114c31c3 | 133 | { |
embeddedartists | 5:f4de114c31c3 | 134 | memcpy(pDataOut->pixels, pDataIn+sizeof(eaimg_header_t), sizeIn-sizeof(eaimg_header_t)); |
embeddedartists | 5:f4de114c31c3 | 135 | return 0; |
embeddedartists | 5:f4de114c31c3 | 136 | } |
embeddedartists | 5:f4de114c31c3 | 137 | } |
embeddedartists | 5:f4de114c31c3 | 138 | break; |
embeddedartists | 5:f4de114c31c3 | 139 | |
embeddedartists | 0:4977187e90c7 | 140 | default: |
embeddedartists | 0:4977187e90c7 | 141 | break; |
embeddedartists | 0:4977187e90c7 | 142 | } |
embeddedartists | 0:4977187e90c7 | 143 | |
embeddedartists | 0:4977187e90c7 | 144 | pDataOut->pixels = NULL; |
embeddedartists | 5:f4de114c31c3 | 145 | pDataOut->pointerToFree = NULL; |
embeddedartists | 0:4977187e90c7 | 146 | pDataOut->width = 0; |
embeddedartists | 0:4977187e90c7 | 147 | pDataOut->height = 0; |
embeddedartists | 5:f4de114c31c3 | 148 | pDataOut->res = resolution; |
embeddedartists | 0:4977187e90c7 | 149 | return result; |
embeddedartists | 0:4977187e90c7 | 150 | } |
embeddedartists | 0:4977187e90c7 | 151 | |
embeddedartists | 5:f4de114c31c3 | 152 | int Image::decode(const char* filename, Resolution res, ImageData_t* pDataOut, Mutex* pLock) |
embeddedartists | 0:4977187e90c7 | 153 | { |
embeddedartists | 0:4977187e90c7 | 154 | FILE* fh = NULL; |
embeddedartists | 0:4977187e90c7 | 155 | uint8_t* buff = NULL; |
embeddedartists | 0:4977187e90c7 | 156 | int result = 1; |
embeddedartists | 0:4977187e90c7 | 157 | |
embeddedartists | 5:f4de114c31c3 | 158 | pDataOut->height = 0; |
embeddedartists | 5:f4de114c31c3 | 159 | pDataOut->width = 0; |
embeddedartists | 5:f4de114c31c3 | 160 | pDataOut->pixels = NULL; |
embeddedartists | 5:f4de114c31c3 | 161 | pDataOut->pointerToFree = NULL; |
embeddedartists | 5:f4de114c31c3 | 162 | pDataOut->res = res; |
embeddedartists | 5:f4de114c31c3 | 163 | |
embeddedartists | 5:f4de114c31c3 | 164 | if (pLock != NULL) { |
embeddedartists | 5:f4de114c31c3 | 165 | pLock->lock(); |
embeddedartists | 5:f4de114c31c3 | 166 | } |
embeddedartists | 5:f4de114c31c3 | 167 | |
embeddedartists | 0:4977187e90c7 | 168 | do |
embeddedartists | 0:4977187e90c7 | 169 | { |
embeddedartists | 0:4977187e90c7 | 170 | fh = fopen(filename, "r"); |
embeddedartists | 0:4977187e90c7 | 171 | if (fh == NULL) { |
embeddedartists | 0:4977187e90c7 | 172 | break; |
embeddedartists | 0:4977187e90c7 | 173 | } |
embeddedartists | 0:4977187e90c7 | 174 | |
embeddedartists | 0:4977187e90c7 | 175 | uint32_t size = fileSize(fh); |
embeddedartists | 0:4977187e90c7 | 176 | buff = (uint8_t*)malloc(size); |
embeddedartists | 0:4977187e90c7 | 177 | if (buff == NULL) { |
embeddedartists | 0:4977187e90c7 | 178 | return 1; |
embeddedartists | 0:4977187e90c7 | 179 | } |
embeddedartists | 0:4977187e90c7 | 180 | |
embeddedartists | 0:4977187e90c7 | 181 | uint32_t num; |
embeddedartists | 0:4977187e90c7 | 182 | uint32_t left = size; |
embeddedartists | 0:4977187e90c7 | 183 | uint32_t off = 0; |
embeddedartists | 0:4977187e90c7 | 184 | do |
embeddedartists | 0:4977187e90c7 | 185 | { |
embeddedartists | 0:4977187e90c7 | 186 | num = fread(buff+off, 1, left, fh); |
embeddedartists | 0:4977187e90c7 | 187 | if (num > 0) { |
embeddedartists | 0:4977187e90c7 | 188 | left -= num; |
embeddedartists | 0:4977187e90c7 | 189 | off += num; |
embeddedartists | 0:4977187e90c7 | 190 | } |
embeddedartists | 0:4977187e90c7 | 191 | } while (left > 0 && num > 0); |
embeddedartists | 0:4977187e90c7 | 192 | if (left > 0) { |
embeddedartists | 0:4977187e90c7 | 193 | break; |
embeddedartists | 0:4977187e90c7 | 194 | } |
embeddedartists | 0:4977187e90c7 | 195 | |
embeddedartists | 5:f4de114c31c3 | 196 | fclose(fh); |
embeddedartists | 5:f4de114c31c3 | 197 | if (pLock != NULL) { |
embeddedartists | 5:f4de114c31c3 | 198 | pLock->unlock(); |
embeddedartists | 5:f4de114c31c3 | 199 | } |
embeddedartists | 5:f4de114c31c3 | 200 | |
embeddedartists | 5:f4de114c31c3 | 201 | Type type = imageType(buff, size); |
embeddedartists | 5:f4de114c31c3 | 202 | if (type == RAW) { |
embeddedartists | 5:f4de114c31c3 | 203 | pDataOut->width = ((eaimg_header_t*)buff)->width; |
embeddedartists | 5:f4de114c31c3 | 204 | pDataOut->height = ((eaimg_header_t*)buff)->height; |
embeddedartists | 5:f4de114c31c3 | 205 | pDataOut->pointerToFree = buff; |
embeddedartists | 5:f4de114c31c3 | 206 | pDataOut->res = RES_16BIT; |
embeddedartists | 5:f4de114c31c3 | 207 | pDataOut->pixels = (uint16_t*)(buff + sizeof(eaimg_header_t)); |
embeddedartists | 5:f4de114c31c3 | 208 | } else { |
embeddedartists | 5:f4de114c31c3 | 209 | result = Image::decode(buff, size, res, pDataOut); |
embeddedartists | 5:f4de114c31c3 | 210 | free(buff); |
embeddedartists | 6:7917b0894655 | 211 | return result; |
embeddedartists | 0:4977187e90c7 | 212 | } |
embeddedartists | 0:4977187e90c7 | 213 | |
embeddedartists | 0:4977187e90c7 | 214 | // success |
embeddedartists | 5:f4de114c31c3 | 215 | return 0; |
embeddedartists | 0:4977187e90c7 | 216 | |
embeddedartists | 0:4977187e90c7 | 217 | } while (false); |
embeddedartists | 0:4977187e90c7 | 218 | |
embeddedartists | 0:4977187e90c7 | 219 | if (fh != NULL) { |
embeddedartists | 0:4977187e90c7 | 220 | fclose(fh); |
embeddedartists | 0:4977187e90c7 | 221 | } |
embeddedartists | 0:4977187e90c7 | 222 | if (buff != NULL) { |
embeddedartists | 0:4977187e90c7 | 223 | free(buff); |
embeddedartists | 0:4977187e90c7 | 224 | } |
embeddedartists | 5:f4de114c31c3 | 225 | if (pLock != NULL) { |
embeddedartists | 5:f4de114c31c3 | 226 | pLock->unlock(); |
embeddedartists | 5:f4de114c31c3 | 227 | } |
embeddedartists | 0:4977187e90c7 | 228 | return result; |
embeddedartists | 0:4977187e90c7 | 229 | } |
embeddedartists | 0:4977187e90c7 | 230 | |
embeddedartists | 0:4977187e90c7 | 231 | Image::Type Image::imageType(const unsigned char* pDataIn, unsigned int sizeIn) |
embeddedartists | 0:4977187e90c7 | 232 | { |
embeddedartists | 0:4977187e90c7 | 233 | if (sizeIn > 4) |
embeddedartists | 0:4977187e90c7 | 234 | { |
embeddedartists | 0:4977187e90c7 | 235 | if (pDataIn[0] == 0x89 && pDataIn[1] == 'P' && pDataIn[2] == 'N' && pDataIn[3] == 'G') |
embeddedartists | 0:4977187e90c7 | 236 | { |
embeddedartists | 0:4977187e90c7 | 237 | return PNG; |
embeddedartists | 0:4977187e90c7 | 238 | } |
embeddedartists | 0:4977187e90c7 | 239 | } |
embeddedartists | 0:4977187e90c7 | 240 | if (BMP_IsValid((void*)pDataIn)) |
embeddedartists | 0:4977187e90c7 | 241 | { |
embeddedartists | 0:4977187e90c7 | 242 | return BMP; |
embeddedartists | 0:4977187e90c7 | 243 | } |
embeddedartists | 5:f4de114c31c3 | 244 | if (sizeIn >= sizeof(eaimg_header_t)) |
embeddedartists | 5:f4de114c31c3 | 245 | { |
embeddedartists | 5:f4de114c31c3 | 246 | eaimg_header_t* hdr = (eaimg_header_t*)pDataIn; |
embeddedartists | 5:f4de114c31c3 | 247 | if (memcmp(hdr->prefix, "eaimg:", 6) == 0) |
embeddedartists | 5:f4de114c31c3 | 248 | { |
embeddedartists | 5:f4de114c31c3 | 249 | return RAW; |
embeddedartists | 5:f4de114c31c3 | 250 | } |
embeddedartists | 5:f4de114c31c3 | 251 | } |
embeddedartists | 0:4977187e90c7 | 252 | return UNKNOWN; |
embeddedartists | 0:4977187e90c7 | 253 | } |
embeddedartists | 0:4977187e90c7 | 254 | |
embeddedartists | 0:4977187e90c7 | 255 | uint32_t Image::fileSize(FILE* f) |
embeddedartists | 0:4977187e90c7 | 256 | { |
embeddedartists | 0:4977187e90c7 | 257 | uint32_t pos = ftell(f); |
embeddedartists | 0:4977187e90c7 | 258 | fseek(f, 0, SEEK_END); |
embeddedartists | 0:4977187e90c7 | 259 | uint32_t size = ftell(f); |
embeddedartists | 0:4977187e90c7 | 260 | fseek(f, pos, SEEK_SET); |
embeddedartists | 0:4977187e90c7 | 261 | return size; |
embeddedartists | 0:4977187e90c7 | 262 | } |