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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers bmp.cpp Source File

bmp.cpp

00001 /* ----------------------------------------------------------------------------
00002  *         ATMEL Microcontroller Software Support  -  ROUSSET  -
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2006, Atmel Corporation
00005 
00006  * All rights reserved.
00007  * 
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  * 
00011  * - Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the disclaiimer below.
00013  * 
00014  * - Redistributions in binary form must reproduce the above copyright notice,
00015  * this list of conditions and the disclaimer below in the documentation and/or
00016  * other materials provided with the distribution. 
00017  * 
00018  * Atmel's name may not be used to endorse or promote products derived from
00019  * this software without specific prior written permission. 
00020  * 
00021  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
00022  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00023  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
00024  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
00025  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00026  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00027  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00030  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  * ----------------------------------------------------------------------------
00032  */
00033 
00034 //------------------------------------------------------------------------------
00035 //         Headers
00036 //------------------------------------------------------------------------------
00037 
00038 #include "mbed.h"
00039 #include "bmp.h"
00040 
00041 //------------------------------------------------------------------------------
00042 //         Internal constants
00043 //------------------------------------------------------------------------------
00044 
00045 /// BMP magic number ('BM').
00046 #define BMP_TYPE        0x4D42
00047 
00048 //------------------------------------------------------------------------------
00049 //         Internal types
00050 //------------------------------------------------------------------------------
00051 
00052 struct BMPPaletteEntry {
00053 
00054     unsigned char b;
00055     unsigned char g;
00056     unsigned char r;
00057     unsigned char filler;
00058 };
00059 
00060 //------------------------------------------------------------------------------
00061 //         Exported functions
00062 //------------------------------------------------------------------------------
00063 
00064 //------------------------------------------------------------------------------
00065 /// Returns 1 if the header of a BMP file is valid; otherwise returns 0.
00066 /// \param file  Buffer holding the file to examinate.
00067 //------------------------------------------------------------------------------
00068 unsigned char BMP_IsValid(void *file)
00069 {
00070     return ((struct BMPHeader *) file)->type == BMP_TYPE;
00071 }
00072 
00073 //------------------------------------------------------------------------------
00074 /// Returns the size of a BMP image given at least its header (the file does
00075 /// not have to be complete).
00076 /// \param file  Pointer to the buffer which holds the BMP file.
00077 //------------------------------------------------------------------------------
00078 unsigned int BMP_GetFileSize(void *file)
00079 {
00080     return ((struct BMPHeader *) file)->fileSize;
00081 }
00082 
00083 //------------------------------------------------------------------------------
00084 /// Loads a BMP image located at the given address, decodes it and stores the
00085 /// resulting image inside the provided buffer. Image must have the specified
00086 /// width & height.
00087 /// Returns 0 if the image has been loaded; otherwise returns an error code.
00088 /// \param file  Buffer which holds the BMP file.
00089 /// \param buffer  Buffer in which to store the decoded image.
00090 /// \param width  Buffer width in pixels.
00091 /// \param height  Buffer height in pixels.
00092 /// \param bpp  Number of bits per pixels that the buffer stores.
00093 //------------------------------------------------------------------------------
00094 unsigned char BMP_Decode(
00095     void *file,
00096     unsigned char *buffer,
00097     unsigned int width,
00098     unsigned int height,
00099     unsigned char bpp)
00100 {
00101     struct BMPHeader *header;
00102     unsigned int i, j;
00103     unsigned char r, g, b;
00104     unsigned char *image;
00105 
00106   // Read header information
00107     header = (struct BMPHeader *) file;
00108 
00109     // Verify that the file is valid
00110     if (!BMP_IsValid(file)) {
00111 
00112         printf("BMP_Decode: File type is not 'BM' (0x%x).\n\r",
00113                 header->type);
00114         return 1;
00115     }
00116     
00117     // Check that parameters match
00118     if ((header->compression != 0)
00119         || (header->width != width)
00120         || (header->height != height)) {
00121 
00122         printf("BMP_Decode: File format not supported\n\r");
00123         printf(" -> .compression = %d\n\r", header->compression);
00124         printf(" -> .width = %d\n\r", header->width);
00125         printf(" -> .height = %d\n\r", header->height);
00126         printf(" -> .bits = %d\n\r", header->bits);
00127         return 2;
00128     }
00129 
00130     // Get image data
00131     image = (unsigned char *) ((unsigned int) file + header->offset);
00132 
00133     // Check that the bpp resolution is supported
00134     // Only a 24-bit output & 24- or 8-bit input are supported
00135     if (bpp != 24) {
00136 
00137         printf("BMP_Decode: Output resolution not supported\n\r");
00138         return 3;
00139     }
00140     else if (header->bits == 24) {
00141     
00142         // Modified by Embedded Artists. This padding was not handled in the original
00143         // implementation. Each row in the Pixel Array is padded to a multiple of 4 bytes in size
00144         int rowWidthBytes = width * 3;
00145         rowWidthBytes += (rowWidthBytes % 4);
00146 
00147         // Copy raw data from BMP to buffer (reversing row order)
00148         for (i=0; i < height; i++) {
00149 #if 1
00150           // Modified by Embedded Artists to convert to 565-format instead of 24 bit
00151           int x;
00152           for(x=0; x<width; x++)
00153           {          
00154             r = *(unsigned char *)(image + (height - i - 1) * rowWidthBytes + 3*x + 2);
00155             g = *(unsigned char *)(image + (height - i - 1) * rowWidthBytes + 3*x + 1);
00156             b = *(unsigned char *)(image + (height - i - 1) * rowWidthBytes + 3*x + 0);
00157             *(unsigned short *)(buffer + (i * width) * 2 + 2*x) = (((unsigned short)r & 0xF8) << 8) |
00158                                                                   (((unsigned short)g & 0xFC) << 3) |
00159                                                                   (((unsigned short)b & 0xF8) >> 3);
00160           }
00161 #else
00162             memcpy(buffer + (i * width) * 3,
00163                    image + ((height - i - 1) * width) * 3,
00164                    width * 3);
00165 #endif
00166         }
00167 
00168 #if 0
00169         // Swap red and blue
00170         for (i=0; i < height; i++) {
00171             for (j=0; j < width; j++) {
00172 
00173                 r = buffer[(i * width + j) * 3 + 2];
00174                 g = buffer[(i * width + j) * 3 + 1];
00175                 b = buffer[(i * width + j) * 3];
00176 
00177                 buffer[(i * width + j) * 3] = r;
00178                 buffer[(i * width + j) * 3 + 1] = g;
00179                 buffer[(i * width + j) * 3 + 2] = b;
00180             }
00181         }
00182 #endif
00183     }
00184     else if (header->bits == 8) {
00185 
00186         // Retrieve palette
00187         struct BMPPaletteEntry palette[256];
00188         memcpy(palette,
00189                (unsigned char *) ((unsigned int) file + sizeof(struct BMPHeader)),
00190                header->offset - sizeof(struct BMPHeader));
00191 
00192         // Decode image (reversing row order)
00193         for (i=0; i < height; i++) {
00194             for (j=0; j < width; j++) {
00195 
00196                 r = palette[image[(height - i - 1) * width + j]].r;
00197                 g = palette[image[(height - i - 1) * width + j]].g;
00198                 b = palette[image[(height - i - 1) * width + j]].b;
00199 
00200                 buffer[(i * width + j) * 3] = r;
00201                 buffer[(i * width + j) * 3 + 1] = g;
00202                 buffer[(i * width + j) * 3 + 2] = b;
00203             }
00204         }
00205     }
00206     else {
00207 
00208         printf("BMP_Decode: Input resolution not supported\n\r");
00209         return 4;
00210     }
00211 
00212     return 0;
00213 }
00214 
00215