PutImageFromSD

From VirtualFabWiki
Jump to: navigation, search

These are slightly modified versions for standard Microchip's primitive routines

  • ExternalMemoryCallback
  • PutImage
  • GetImageWidth
  • GetImageHeight

They enables your application to load all graphics from .bin files stored on a big SD/MMC card. Since FSIO.c FAT16 routines are used, the size limit is 2 GBytes.

How it works

To use it, put both the following PutImageFromSD.c:

 /*****************************************************************************
 *  Module for Microchip Graphics Library
 *  Large Bitmaps on SD handler
 *  This module takes care of handling Putimage calls in order to load bitmaps
 *  from SD/MMC memory card.
 *
 * Requisites:
 *  The following defines should be placed in HardwareProfile.h AFTER the other
 *  display defines (DISPLAY_CONTROLLER, DISP_ORIENTATION, DISP_HOR_RESOLUTION...)
 *
 * #define SD_IMAGEDIR "\\img" // replace img with your directory
 * #ifdef _GRAPHICS_H
 * #include "PutImageFromSD.h"
 * #endif
 *
 *****************************************************************************
 * FileName:        PutImageFromSD.c
 * Dependencies:    Graphics.h HardwareProfile.h FSIO.h
 * Processor:       PIC24, PIC32
 * Compiler:        MPLAB C30, MPLAB C32
 * Linker:          MPLAB LINK30, MPLAB LINK32
 * Company:         VirtualFab
 *
 * Software License Agreement
 *
 * Copyright (c) 2011 VirtualFab  All rights reserved.
 * VirtualFab licenses to you the right to use, modify, copy and distribute
 * this Software as you wish
 *
 * SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY
 * OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR
 * PURPOSE. IN NO EVENT SHALL VIRTUALFAB OR ITS LICENSORS BE LIABLE OR
 * OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION,
 * BREACH OF WARRANTY, OR OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT
 * DAMAGES OR EXPENSES INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL,
 * INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA,
 * COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY
 * CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF),
 * OR OTHER SIMILAR COSTS.
 *
 * Author               Date        Comment
 *****************************************************************************
 * VirtualFab           2011/06/17	Version 1.0 release
 *                      2012/04/23      Version 1.1 Release - not failing if media not mounted
 *                      2012/10/17      Version 1.2 Release - Working flawlessly with latest MAL
 *****************************************************************************/

#include "MDD File System/FSIO.h"
#include "PutImageFromSD.h"

FSFILE *SDImgFileHandler = NULL;
IMAGE_ON_SD *SDImgCurrent = NULL;

/*********************************************************************
 * Function: WORD ExternalMemoryCallback(IMAGE_EXTERNAL *memory, LONG offset, WORD nCount, void *buffer)
 *
 * PreCondition: FSInit() already done
 *
 * Input: memory - The pointer to image object for the Bitmap to read
 *        offset - start position in the file to be read
 *        nCount - number of bytes to read
 *        buffer - Pointer to the buffer
 *
 * Output: number of bytes read
 *         If error: returns 0xffff
 *
 * Side Effects: none
 *
 * Overview: Reads image from SD and outputs image starting from left,top coordinates
 *
 * Note: image must be located on SD card
 *
 ********************************************************************/
WORD ExternalMemoryCallback(IMAGE_EXTERNAL *memory, LONG offset, WORD nCount, void *buffer) {
    IMAGE_ON_SD *img;
    char *ImageName;
    extern DISK gDiskData;

    if (gDiskData.mount == FALSE) {
        return 0;
    }

    img = (IMAGE_ON_SD *) memory;
    if (SDImgCurrent != img) {
        SDImgCurrent = img;
        if (SDImgFileHandler != NULL) {
            FSfclose(SDImgFileHandler);
        }
        ImageName = img->filename;
        if (FSchdir(SD_IMAGEDIR) != 0) {
            return 0;
        }
        // Open image file on SD
        SDImgFileHandler = FSfopen(ImageName, FS_READ);
        if (SDImgFileHandler == NULL)
            return 0;
    }
    if (FSfseek(SDImgFileHandler, offset, SEEK_SET) != 0) // Seek from start of file
        return 0;

    if (FSfread(buffer, 1, nCount, SDImgFileHandler) != nCount)
        return 0;

    return (nCount);
}
 

--

and PutImageFromSD.h:

#ifndef _PUTIMAGEFROMSD_H
#define _PUTIMAGEFROMSD_H

#include "Graphics/Primitive.h"

#ifndef USE_BITMAP_EXTERNAL
#define USE_BITMAP_EXTERNAL	
#endif

#define BINBMP_ON_SDFAT 0x0010

typedef struct {
    GFX_RESOURCE type; // Graphics resource type, determines the type and location of data
    WORD ID;           // memory ID, user defined value to differentiate
                       // between graphics resources of the same type	
    union {
        DWORD extAddress;            // generic address	
        FLASH_BYTE *progByteAddress; // for addresses in program section
        FLASH_WORD *progWordAddress; // for addresses in program section
        const char *constAddress;    // for addresses in FLASH
        char *ramAddress;            // for addresses in RAM
#if defined(__PIC24F__) 	    
        __eds__ char *edsAddress;    // for addresses in EDS
#endif	    
    } LOCATION;

    WORD width;   // width of the image 
    WORD height;  // height of the image
    DWORD param1; // Parameters used for the GFX_RESOURCE. Depending on the GFX_RESOURCE type 
                  // definition of param1 can change. For IPU and RLE compressed images, param1 
                  // indicates the compressed size of the image.
    DWORD param2; // Parameters used for the GFX_RESOURCE. Depending on the GFX_RESOURCE type 
                  // definition of param2 can change. For IPU and RLE compressed images, param2 
                  // indicates the uncompressed size of the image.
    WORD colorDepth; // color depth of the image
    char *filename; // pointer to filename
} IMAGE_ON_SD;

WORD ExternalMemoryCallback(IMAGE_EXTERNAL *memory, LONG offset, WORD nCount, void *buffer);

#endif // ifndef _PUTIMAGEFROMSD_H

into your project.