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.h@22:f0d00f29bfeb, 2019-11-04 (annotated)
- Committer:
- embeddedartists
- Date:
- Mon Nov 04 14:31:50 2019 +0000
- Revision:
- 22:f0d00f29bfeb
- Parent:
- 17:6e2abf107800
More updates related to mbed OS 5
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 | #ifndef IMAGE_H |
embeddedartists | 0:4977187e90c7 | 18 | #define IMAGE_H |
embeddedartists | 0:4977187e90c7 | 19 | |
embeddedartists | 3:3fabfe3339b8 | 20 | #include "mbed.h" |
embeddedartists | 5:f4de114c31c3 | 21 | #include "rtos.h" |
embeddedartists | 3:3fabfe3339b8 | 22 | |
embeddedartists | 17:6e2abf107800 | 23 | class Resource; |
embeddedartists | 17:6e2abf107800 | 24 | |
embeddedartists | 0:4977187e90c7 | 25 | /** |
embeddedartists | 1:46c8df4608c8 | 26 | * Image example |
embeddedartists | 0:4977187e90c7 | 27 | * |
embeddedartists | 0:4977187e90c7 | 28 | * @code |
embeddedartists | 0:4977187e90c7 | 29 | * #include "mbed.h" |
embeddedartists | 0:4977187e90c7 | 30 | * #include "Image.h" |
embeddedartists | 0:4977187e90c7 | 31 | * |
embeddedartists | 0:4977187e90c7 | 32 | * const unsigned char cube_image1[] = { 137,80,78,71, ... }; |
embeddedartists | 0:4977187e90c7 | 33 | * int cube_image1_sz = sizeof(cube_image1); |
embeddedartists | 0:4977187e90c7 | 34 | * |
embeddedartists | 0:4977187e90c7 | 35 | * int main(void) { |
embeddedartists | 0:4977187e90c7 | 36 | * // initialize the display |
embeddedartists | 0:4977187e90c7 | 37 | * ... |
embeddedartists | 0:4977187e90c7 | 38 | * |
embeddedartists | 0:4977187e90c7 | 39 | * // decode an image from an array |
embeddedartists | 0:4977187e90c7 | 40 | * Image::ImageData_t img; |
embeddedartists | 0:4977187e90c7 | 41 | * if (Image::decode(cube_image1, cube_image1_sz, &img) == 0) { |
embeddedartists | 0:4977187e90c7 | 42 | * // draw on display using img.pixels, img.width and img.height |
embeddedartists | 0:4977187e90c7 | 43 | * ... |
embeddedartists | 0:4977187e90c7 | 44 | * free(img.pixels); |
embeddedartists | 0:4977187e90c7 | 45 | * } |
embeddedartists | 0:4977187e90c7 | 46 | * |
embeddedartists | 0:4977187e90c7 | 47 | * // decode an image from a file |
embeddedartists | 0:4977187e90c7 | 48 | * if (Image::decode("/ram/image.png", &img) == 0) { |
embeddedartists | 0:4977187e90c7 | 49 | * // draw on display using img.pixels, img.width and img.height |
embeddedartists | 0:4977187e90c7 | 50 | * ... |
embeddedartists | 0:4977187e90c7 | 51 | * free(img.pixels); |
embeddedartists | 0:4977187e90c7 | 52 | * } |
embeddedartists | 0:4977187e90c7 | 53 | * } |
embeddedartists | 0:4977187e90c7 | 54 | * @endcode |
embeddedartists | 0:4977187e90c7 | 55 | */ |
embeddedartists | 0:4977187e90c7 | 56 | class Image { |
embeddedartists | 0:4977187e90c7 | 57 | public: |
embeddedartists | 0:4977187e90c7 | 58 | |
embeddedartists | 0:4977187e90c7 | 59 | enum Type { |
embeddedartists | 0:4977187e90c7 | 60 | BMP = 0, |
embeddedartists | 0:4977187e90c7 | 61 | PNG, |
embeddedartists | 5:f4de114c31c3 | 62 | RAW, /* Image prepared with the img_conv.jar tool */ |
embeddedartists | 0:4977187e90c7 | 63 | UNKNOWN |
embeddedartists | 0:4977187e90c7 | 64 | }; |
embeddedartists | 0:4977187e90c7 | 65 | |
embeddedartists | 0:4977187e90c7 | 66 | enum Resolution { |
embeddedartists | 0:4977187e90c7 | 67 | RES_16BIT, |
embeddedartists | 0:4977187e90c7 | 68 | RES_24BIT |
embeddedartists | 0:4977187e90c7 | 69 | }; |
embeddedartists | 0:4977187e90c7 | 70 | |
embeddedartists | 0:4977187e90c7 | 71 | typedef struct { |
embeddedartists | 0:4977187e90c7 | 72 | uint16_t* pixels; |
embeddedartists | 0:4977187e90c7 | 73 | uint32_t width; |
embeddedartists | 0:4977187e90c7 | 74 | uint32_t height; |
embeddedartists | 0:4977187e90c7 | 75 | Resolution res; |
embeddedartists | 5:f4de114c31c3 | 76 | void* pointerToFree; |
embeddedartists | 0:4977187e90c7 | 77 | } ImageData_t; |
embeddedartists | 0:4977187e90c7 | 78 | |
embeddedartists | 0:4977187e90c7 | 79 | /** Decodes the specified image data |
embeddedartists | 0:4977187e90c7 | 80 | * |
embeddedartists | 0:4977187e90c7 | 81 | * Note that if this function returns a zero, indicating success, |
embeddedartists | 0:4977187e90c7 | 82 | * the pixels member of the pDataOut structure must be |
embeddedartists | 1:46c8df4608c8 | 83 | * deallocated using free() when no longer needed. |
embeddedartists | 0:4977187e90c7 | 84 | * |
embeddedartists | 0:4977187e90c7 | 85 | * @param pDataIn the image data |
embeddedartists | 0:4977187e90c7 | 86 | * @param sizeIn the number of bytes in the pDataIn array |
embeddedartists | 1:46c8df4608c8 | 87 | * @param res the format of the display |
embeddedartists | 0:4977187e90c7 | 88 | * @param pDataOut the decoded image (only valid if 0 is returned) |
embeddedartists | 0:4977187e90c7 | 89 | * |
embeddedartists | 0:4977187e90c7 | 90 | * @returns |
embeddedartists | 0:4977187e90c7 | 91 | * 0 on success |
embeddedartists | 0:4977187e90c7 | 92 | * 1 on failure |
embeddedartists | 0:4977187e90c7 | 93 | */ |
embeddedartists | 0:4977187e90c7 | 94 | static int decode(const unsigned char* pDataIn, unsigned int sizeIn, Resolution res, ImageData_t* pDataOut); |
embeddedartists | 0:4977187e90c7 | 95 | |
embeddedartists | 1:46c8df4608c8 | 96 | /** Reads the specified file and decodes the image data |
embeddedartists | 1:46c8df4608c8 | 97 | * |
embeddedartists | 1:46c8df4608c8 | 98 | * Note that if this function returns a zero, indicating success, |
embeddedartists | 1:46c8df4608c8 | 99 | * the pixels member of the pDataOut structure must be |
embeddedartists | 1:46c8df4608c8 | 100 | * deallocated using free() when no longer needed. |
embeddedartists | 1:46c8df4608c8 | 101 | * |
embeddedartists | 1:46c8df4608c8 | 102 | * @param filename the file name and path |
embeddedartists | 1:46c8df4608c8 | 103 | * @param res the format of the display |
embeddedartists | 1:46c8df4608c8 | 104 | * @param pDataOut the decoded image (only valid if 0 is returned) |
embeddedartists | 5:f4de114c31c3 | 105 | * @param pLock an optional mutex to prevent multiple access to file operations |
embeddedartists | 1:46c8df4608c8 | 106 | * |
embeddedartists | 1:46c8df4608c8 | 107 | * @returns |
embeddedartists | 1:46c8df4608c8 | 108 | * 0 on success |
embeddedartists | 1:46c8df4608c8 | 109 | * 1 on failure |
embeddedartists | 1:46c8df4608c8 | 110 | */ |
embeddedartists | 5:f4de114c31c3 | 111 | static int decode(const char* filename, Resolution res, ImageData_t* pDataOut, Mutex* pLock=NULL); |
embeddedartists | 17:6e2abf107800 | 112 | |
embeddedartists | 17:6e2abf107800 | 113 | /** Decodes the specified image resource |
embeddedartists | 17:6e2abf107800 | 114 | * |
embeddedartists | 17:6e2abf107800 | 115 | * Note that if this function returns a zero, indicating success, |
embeddedartists | 17:6e2abf107800 | 116 | * the pixels member of the pDataOut structure must be |
embeddedartists | 17:6e2abf107800 | 117 | * deallocated using free() when no longer needed. |
embeddedartists | 17:6e2abf107800 | 118 | * |
embeddedartists | 17:6e2abf107800 | 119 | * The decoded image is cached in the Resource to prevent decoding |
embeddedartists | 17:6e2abf107800 | 120 | * the same resource over and over. |
embeddedartists | 17:6e2abf107800 | 121 | * |
embeddedartists | 17:6e2abf107800 | 122 | * @param res the resource to decode |
embeddedartists | 17:6e2abf107800 | 123 | * @param resolution the format of the display |
embeddedartists | 17:6e2abf107800 | 124 | * @param pDataOut the decoded image (only valid if 0 is returned) |
embeddedartists | 17:6e2abf107800 | 125 | * |
embeddedartists | 17:6e2abf107800 | 126 | * @returns |
embeddedartists | 17:6e2abf107800 | 127 | * 0 on success |
embeddedartists | 17:6e2abf107800 | 128 | * 1 on failure |
embeddedartists | 17:6e2abf107800 | 129 | */ |
embeddedartists | 17:6e2abf107800 | 130 | static int decode(Resource* res, Resolution resolution, ImageData_t* pDataOut, Mutex* pLock=NULL); |
embeddedartists | 17:6e2abf107800 | 131 | |
embeddedartists | 0:4977187e90c7 | 132 | private: |
embeddedartists | 0:4977187e90c7 | 133 | |
embeddedartists | 0:4977187e90c7 | 134 | /** No instance needed |
embeddedartists | 0:4977187e90c7 | 135 | * |
embeddedartists | 0:4977187e90c7 | 136 | */ |
embeddedartists | 0:4977187e90c7 | 137 | Image(); |
embeddedartists | 0:4977187e90c7 | 138 | |
embeddedartists | 0:4977187e90c7 | 139 | static Type imageType(const unsigned char* pDataIn, unsigned int sizeIn); |
embeddedartists | 0:4977187e90c7 | 140 | |
embeddedartists | 0:4977187e90c7 | 141 | static uint32_t fileSize(FILE* f); |
embeddedartists | 0:4977187e90c7 | 142 | }; |
embeddedartists | 0:4977187e90c7 | 143 | |
embeddedartists | 0:4977187e90c7 | 144 | #endif |