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/bmp.cpp@22:f0d00f29bfeb, 2019-11-04 (annotated)
- Committer:
- embeddedartists
- Date:
- Mon Nov 04 14:31:50 2019 +0000
- Revision:
- 22:f0d00f29bfeb
- Parent:
- 10:651861441108
More updates related to mbed OS 5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
embeddedartists | 0:4977187e90c7 | 1 | /* ---------------------------------------------------------------------------- |
embeddedartists | 0:4977187e90c7 | 2 | * ATMEL Microcontroller Software Support - ROUSSET - |
embeddedartists | 0:4977187e90c7 | 3 | * ---------------------------------------------------------------------------- |
embeddedartists | 0:4977187e90c7 | 4 | * Copyright (c) 2006, Atmel Corporation |
embeddedartists | 0:4977187e90c7 | 5 | |
embeddedartists | 0:4977187e90c7 | 6 | * All rights reserved. |
embeddedartists | 0:4977187e90c7 | 7 | * |
embeddedartists | 0:4977187e90c7 | 8 | * Redistribution and use in source and binary forms, with or without |
embeddedartists | 0:4977187e90c7 | 9 | * modification, are permitted provided that the following conditions are met: |
embeddedartists | 0:4977187e90c7 | 10 | * |
embeddedartists | 0:4977187e90c7 | 11 | * - Redistributions of source code must retain the above copyright notice, |
embeddedartists | 0:4977187e90c7 | 12 | * this list of conditions and the disclaiimer below. |
embeddedartists | 0:4977187e90c7 | 13 | * |
embeddedartists | 0:4977187e90c7 | 14 | * - Redistributions in binary form must reproduce the above copyright notice, |
embeddedartists | 0:4977187e90c7 | 15 | * this list of conditions and the disclaimer below in the documentation and/or |
embeddedartists | 0:4977187e90c7 | 16 | * other materials provided with the distribution. |
embeddedartists | 0:4977187e90c7 | 17 | * |
embeddedartists | 0:4977187e90c7 | 18 | * Atmel's name may not be used to endorse or promote products derived from |
embeddedartists | 0:4977187e90c7 | 19 | * this software without specific prior written permission. |
embeddedartists | 0:4977187e90c7 | 20 | * |
embeddedartists | 0:4977187e90c7 | 21 | * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR |
embeddedartists | 0:4977187e90c7 | 22 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
embeddedartists | 0:4977187e90c7 | 23 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE |
embeddedartists | 0:4977187e90c7 | 24 | * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, |
embeddedartists | 0:4977187e90c7 | 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
embeddedartists | 0:4977187e90c7 | 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
embeddedartists | 0:4977187e90c7 | 27 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
embeddedartists | 0:4977187e90c7 | 28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
embeddedartists | 0:4977187e90c7 | 29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
embeddedartists | 0:4977187e90c7 | 30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
embeddedartists | 0:4977187e90c7 | 31 | * ---------------------------------------------------------------------------- |
embeddedartists | 0:4977187e90c7 | 32 | */ |
embeddedartists | 0:4977187e90c7 | 33 | |
embeddedartists | 0:4977187e90c7 | 34 | //------------------------------------------------------------------------------ |
embeddedartists | 0:4977187e90c7 | 35 | // Headers |
embeddedartists | 0:4977187e90c7 | 36 | //------------------------------------------------------------------------------ |
embeddedartists | 0:4977187e90c7 | 37 | |
embeddedartists | 0:4977187e90c7 | 38 | #include "mbed.h" |
embeddedartists | 0:4977187e90c7 | 39 | #include "bmp.h" |
embeddedartists | 0:4977187e90c7 | 40 | |
embeddedartists | 0:4977187e90c7 | 41 | //------------------------------------------------------------------------------ |
embeddedartists | 0:4977187e90c7 | 42 | // Internal constants |
embeddedartists | 0:4977187e90c7 | 43 | //------------------------------------------------------------------------------ |
embeddedartists | 0:4977187e90c7 | 44 | |
embeddedartists | 0:4977187e90c7 | 45 | /// BMP magic number ('BM'). |
embeddedartists | 0:4977187e90c7 | 46 | #define BMP_TYPE 0x4D42 |
embeddedartists | 0:4977187e90c7 | 47 | |
embeddedartists | 0:4977187e90c7 | 48 | //------------------------------------------------------------------------------ |
embeddedartists | 0:4977187e90c7 | 49 | // Internal types |
embeddedartists | 0:4977187e90c7 | 50 | //------------------------------------------------------------------------------ |
embeddedartists | 0:4977187e90c7 | 51 | |
embeddedartists | 0:4977187e90c7 | 52 | struct BMPPaletteEntry { |
embeddedartists | 0:4977187e90c7 | 53 | |
embeddedartists | 0:4977187e90c7 | 54 | unsigned char b; |
embeddedartists | 0:4977187e90c7 | 55 | unsigned char g; |
embeddedartists | 0:4977187e90c7 | 56 | unsigned char r; |
embeddedartists | 0:4977187e90c7 | 57 | unsigned char filler; |
embeddedartists | 0:4977187e90c7 | 58 | }; |
embeddedartists | 0:4977187e90c7 | 59 | |
embeddedartists | 0:4977187e90c7 | 60 | //------------------------------------------------------------------------------ |
embeddedartists | 0:4977187e90c7 | 61 | // Exported functions |
embeddedartists | 0:4977187e90c7 | 62 | //------------------------------------------------------------------------------ |
embeddedartists | 0:4977187e90c7 | 63 | |
embeddedartists | 0:4977187e90c7 | 64 | //------------------------------------------------------------------------------ |
embeddedartists | 0:4977187e90c7 | 65 | /// Returns 1 if the header of a BMP file is valid; otherwise returns 0. |
embeddedartists | 0:4977187e90c7 | 66 | /// \param file Buffer holding the file to examinate. |
embeddedartists | 0:4977187e90c7 | 67 | //------------------------------------------------------------------------------ |
embeddedartists | 0:4977187e90c7 | 68 | unsigned char BMP_IsValid(void *file) |
embeddedartists | 0:4977187e90c7 | 69 | { |
embeddedartists | 0:4977187e90c7 | 70 | return ((struct BMPHeader *) file)->type == BMP_TYPE; |
embeddedartists | 0:4977187e90c7 | 71 | } |
embeddedartists | 0:4977187e90c7 | 72 | |
embeddedartists | 0:4977187e90c7 | 73 | //------------------------------------------------------------------------------ |
embeddedartists | 0:4977187e90c7 | 74 | /// Returns the size of a BMP image given at least its header (the file does |
embeddedartists | 0:4977187e90c7 | 75 | /// not have to be complete). |
embeddedartists | 0:4977187e90c7 | 76 | /// \param file Pointer to the buffer which holds the BMP file. |
embeddedartists | 0:4977187e90c7 | 77 | //------------------------------------------------------------------------------ |
embeddedartists | 0:4977187e90c7 | 78 | unsigned int BMP_GetFileSize(void *file) |
embeddedartists | 0:4977187e90c7 | 79 | { |
embeddedartists | 0:4977187e90c7 | 80 | return ((struct BMPHeader *) file)->fileSize; |
embeddedartists | 0:4977187e90c7 | 81 | } |
embeddedartists | 0:4977187e90c7 | 82 | |
embeddedartists | 0:4977187e90c7 | 83 | //------------------------------------------------------------------------------ |
embeddedartists | 0:4977187e90c7 | 84 | /// Loads a BMP image located at the given address, decodes it and stores the |
embeddedartists | 0:4977187e90c7 | 85 | /// resulting image inside the provided buffer. Image must have the specified |
embeddedartists | 0:4977187e90c7 | 86 | /// width & height. |
embeddedartists | 0:4977187e90c7 | 87 | /// Returns 0 if the image has been loaded; otherwise returns an error code. |
embeddedartists | 0:4977187e90c7 | 88 | /// \param file Buffer which holds the BMP file. |
embeddedartists | 0:4977187e90c7 | 89 | /// \param buffer Buffer in which to store the decoded image. |
embeddedartists | 0:4977187e90c7 | 90 | /// \param width Buffer width in pixels. |
embeddedartists | 0:4977187e90c7 | 91 | /// \param height Buffer height in pixels. |
embeddedartists | 0:4977187e90c7 | 92 | /// \param bpp Number of bits per pixels that the buffer stores. |
embeddedartists | 0:4977187e90c7 | 93 | //------------------------------------------------------------------------------ |
embeddedartists | 0:4977187e90c7 | 94 | unsigned char BMP_Decode( |
embeddedartists | 0:4977187e90c7 | 95 | void *file, |
embeddedartists | 0:4977187e90c7 | 96 | unsigned char *buffer, |
embeddedartists | 0:4977187e90c7 | 97 | unsigned int width, |
embeddedartists | 0:4977187e90c7 | 98 | unsigned int height, |
embeddedartists | 0:4977187e90c7 | 99 | unsigned char bpp, |
embeddedartists | 0:4977187e90c7 | 100 | unsigned char target_bpp) // 16 or 24 |
embeddedartists | 0:4977187e90c7 | 101 | { |
embeddedartists | 0:4977187e90c7 | 102 | struct BMPHeader *header; |
embeddedartists | 0:4977187e90c7 | 103 | unsigned int i, j; |
embeddedartists | 0:4977187e90c7 | 104 | unsigned char r, g, b; |
embeddedartists | 0:4977187e90c7 | 105 | unsigned char *image; |
embeddedartists | 0:4977187e90c7 | 106 | |
embeddedartists | 0:4977187e90c7 | 107 | // Read header information |
embeddedartists | 0:4977187e90c7 | 108 | header = (struct BMPHeader *) file; |
embeddedartists | 0:4977187e90c7 | 109 | |
embeddedartists | 0:4977187e90c7 | 110 | // Verify that the file is valid |
embeddedartists | 0:4977187e90c7 | 111 | if (!BMP_IsValid(file)) { |
embeddedartists | 0:4977187e90c7 | 112 | |
embeddedartists | 0:4977187e90c7 | 113 | printf("BMP_Decode: File type is not 'BM' (0x%x).\n\r", |
embeddedartists | 0:4977187e90c7 | 114 | header->type); |
embeddedartists | 0:4977187e90c7 | 115 | return 1; |
embeddedartists | 0:4977187e90c7 | 116 | } |
embeddedartists | 0:4977187e90c7 | 117 | |
embeddedartists | 0:4977187e90c7 | 118 | // Check that parameters match |
embeddedartists | 0:4977187e90c7 | 119 | if ((header->compression != 0) |
embeddedartists | 0:4977187e90c7 | 120 | || (header->width != width) |
embeddedartists | 0:4977187e90c7 | 121 | || (header->height != height)) { |
embeddedartists | 0:4977187e90c7 | 122 | |
embeddedartists | 0:4977187e90c7 | 123 | printf("BMP_Decode: File format not supported\n\r"); |
embeddedartists | 0:4977187e90c7 | 124 | printf(" -> .compression = %d\n\r", header->compression); |
embeddedartists | 0:4977187e90c7 | 125 | printf(" -> .width = %d\n\r", header->width); |
embeddedartists | 0:4977187e90c7 | 126 | printf(" -> .height = %d\n\r", header->height); |
embeddedartists | 0:4977187e90c7 | 127 | printf(" -> .bits = %d\n\r", header->bits); |
embeddedartists | 0:4977187e90c7 | 128 | return 2; |
embeddedartists | 0:4977187e90c7 | 129 | } |
embeddedartists | 0:4977187e90c7 | 130 | |
embeddedartists | 0:4977187e90c7 | 131 | // Get image data |
embeddedartists | 0:4977187e90c7 | 132 | image = (unsigned char *) ((unsigned int) file + header->offset); |
embeddedartists | 0:4977187e90c7 | 133 | |
embeddedartists | 0:4977187e90c7 | 134 | // Check that the bpp resolution is supported |
embeddedartists | 0:4977187e90c7 | 135 | // Only a 24-bit output & 24- or 8-bit input are supported |
embeddedartists | 0:4977187e90c7 | 136 | if (bpp != 24) { |
embeddedartists | 0:4977187e90c7 | 137 | |
embeddedartists | 0:4977187e90c7 | 138 | printf("BMP_Decode: Output resolution not supported\n\r"); |
embeddedartists | 0:4977187e90c7 | 139 | return 3; |
embeddedartists | 0:4977187e90c7 | 140 | } |
embeddedartists | 0:4977187e90c7 | 141 | else if (header->bits == 24) { |
embeddedartists | 0:4977187e90c7 | 142 | |
embeddedartists | 0:4977187e90c7 | 143 | // Modified by Embedded Artists. This padding was not handled in the original |
embeddedartists | 0:4977187e90c7 | 144 | // implementation. Each row in the Pixel Array is padded to a multiple of 4 bytes in size |
embeddedartists | 0:4977187e90c7 | 145 | int rowWidthBytes = width * 3; |
embeddedartists | 0:4977187e90c7 | 146 | rowWidthBytes += (rowWidthBytes % 4); |
embeddedartists | 0:4977187e90c7 | 147 | |
embeddedartists | 0:4977187e90c7 | 148 | // Copy raw data from BMP to buffer (reversing row order) |
embeddedartists | 0:4977187e90c7 | 149 | for (i=0; i < height; i++) { |
embeddedartists | 0:4977187e90c7 | 150 | #if 1 |
embeddedartists | 0:4977187e90c7 | 151 | if (target_bpp == 16) { |
embeddedartists | 0:4977187e90c7 | 152 | // Modified by Embedded Artists to convert to 565-format instead of 24 bit |
embeddedartists | 10:651861441108 | 153 | for(unsigned int x=0; x<width; x++) |
embeddedartists | 0:4977187e90c7 | 154 | { |
embeddedartists | 0:4977187e90c7 | 155 | r = *(unsigned char *)(image + (height - i - 1) * rowWidthBytes + 3*x + 2); |
embeddedartists | 0:4977187e90c7 | 156 | g = *(unsigned char *)(image + (height - i - 1) * rowWidthBytes + 3*x + 1); |
embeddedartists | 0:4977187e90c7 | 157 | b = *(unsigned char *)(image + (height - i - 1) * rowWidthBytes + 3*x + 0); |
embeddedartists | 0:4977187e90c7 | 158 | *(unsigned short *)(buffer + (i * width) * 2 + 2*x) = (((unsigned short)r & 0xF8) << 8) | |
embeddedartists | 0:4977187e90c7 | 159 | (((unsigned short)g & 0xFC) << 3) | |
embeddedartists | 0:4977187e90c7 | 160 | (((unsigned short)b & 0xF8) >> 3); |
embeddedartists | 0:4977187e90c7 | 161 | } |
embeddedartists | 0:4977187e90c7 | 162 | } else { |
embeddedartists | 0:4977187e90c7 | 163 | // Modified by Embedded Artists to add alpha channel in 24 bit output |
embeddedartists | 10:651861441108 | 164 | for(unsigned int x=0; x<width; x++) |
embeddedartists | 0:4977187e90c7 | 165 | { |
embeddedartists | 0:4977187e90c7 | 166 | r = *(unsigned char *)(image + (height - i - 1) * rowWidthBytes + 3*x + 2); |
embeddedartists | 0:4977187e90c7 | 167 | g = *(unsigned char *)(image + (height - i - 1) * rowWidthBytes + 3*x + 1); |
embeddedartists | 0:4977187e90c7 | 168 | b = *(unsigned char *)(image + (height - i - 1) * rowWidthBytes + 3*x + 0); |
embeddedartists | 0:4977187e90c7 | 169 | *(unsigned int *)(buffer + (i * width) * 4 + 4*x) = (r << 16) | (g << 8) | b; |
embeddedartists | 0:4977187e90c7 | 170 | } |
embeddedartists | 0:4977187e90c7 | 171 | } |
embeddedartists | 0:4977187e90c7 | 172 | #else |
embeddedartists | 0:4977187e90c7 | 173 | memcpy(buffer + (i * width) * 3, |
embeddedartists | 0:4977187e90c7 | 174 | image + ((height - i - 1) * width) * 3, |
embeddedartists | 0:4977187e90c7 | 175 | width * 3); |
embeddedartists | 0:4977187e90c7 | 176 | #endif |
embeddedartists | 0:4977187e90c7 | 177 | } |
embeddedartists | 0:4977187e90c7 | 178 | |
embeddedartists | 0:4977187e90c7 | 179 | #if 0 |
embeddedartists | 0:4977187e90c7 | 180 | // Swap red and blue |
embeddedartists | 0:4977187e90c7 | 181 | for (i=0; i < height; i++) { |
embeddedartists | 0:4977187e90c7 | 182 | for (j=0; j < width; j++) { |
embeddedartists | 0:4977187e90c7 | 183 | |
embeddedartists | 0:4977187e90c7 | 184 | r = buffer[(i * width + j) * 3 + 2]; |
embeddedartists | 0:4977187e90c7 | 185 | g = buffer[(i * width + j) * 3 + 1]; |
embeddedartists | 0:4977187e90c7 | 186 | b = buffer[(i * width + j) * 3]; |
embeddedartists | 0:4977187e90c7 | 187 | |
embeddedartists | 0:4977187e90c7 | 188 | buffer[(i * width + j) * 3] = r; |
embeddedartists | 0:4977187e90c7 | 189 | buffer[(i * width + j) * 3 + 1] = g; |
embeddedartists | 0:4977187e90c7 | 190 | buffer[(i * width + j) * 3 + 2] = b; |
embeddedartists | 0:4977187e90c7 | 191 | } |
embeddedartists | 0:4977187e90c7 | 192 | } |
embeddedartists | 0:4977187e90c7 | 193 | #endif |
embeddedartists | 0:4977187e90c7 | 194 | } |
embeddedartists | 0:4977187e90c7 | 195 | else if (header->bits == 8) { |
embeddedartists | 0:4977187e90c7 | 196 | |
embeddedartists | 0:4977187e90c7 | 197 | // Retrieve palette |
embeddedartists | 0:4977187e90c7 | 198 | struct BMPPaletteEntry palette[256]; |
embeddedartists | 0:4977187e90c7 | 199 | memcpy(palette, |
embeddedartists | 0:4977187e90c7 | 200 | (unsigned char *) ((unsigned int) file + sizeof(struct BMPHeader)), |
embeddedartists | 0:4977187e90c7 | 201 | header->offset - sizeof(struct BMPHeader)); |
embeddedartists | 0:4977187e90c7 | 202 | |
embeddedartists | 0:4977187e90c7 | 203 | // Decode image (reversing row order) |
embeddedartists | 0:4977187e90c7 | 204 | for (i=0; i < height; i++) { |
embeddedartists | 0:4977187e90c7 | 205 | for (j=0; j < width; j++) { |
embeddedartists | 0:4977187e90c7 | 206 | |
embeddedartists | 0:4977187e90c7 | 207 | r = palette[image[(height - i - 1) * width + j]].r; |
embeddedartists | 0:4977187e90c7 | 208 | g = palette[image[(height - i - 1) * width + j]].g; |
embeddedartists | 0:4977187e90c7 | 209 | b = palette[image[(height - i - 1) * width + j]].b; |
embeddedartists | 0:4977187e90c7 | 210 | |
embeddedartists | 0:4977187e90c7 | 211 | buffer[(i * width + j) * 3] = r; |
embeddedartists | 0:4977187e90c7 | 212 | buffer[(i * width + j) * 3 + 1] = g; |
embeddedartists | 0:4977187e90c7 | 213 | buffer[(i * width + j) * 3 + 2] = b; |
embeddedartists | 0:4977187e90c7 | 214 | } |
embeddedartists | 0:4977187e90c7 | 215 | } |
embeddedartists | 0:4977187e90c7 | 216 | } |
embeddedartists | 0:4977187e90c7 | 217 | else { |
embeddedartists | 0:4977187e90c7 | 218 | |
embeddedartists | 0:4977187e90c7 | 219 | printf("BMP_Decode: Input resolution not supported\n\r"); |
embeddedartists | 0:4977187e90c7 | 220 | return 4; |
embeddedartists | 0:4977187e90c7 | 221 | } |
embeddedartists | 0:4977187e90c7 | 222 | |
embeddedartists | 0:4977187e90c7 | 223 | return 0; |
embeddedartists | 0:4977187e90c7 | 224 | } |
embeddedartists | 0:4977187e90c7 | 225 |