Create streamed bitmap (Sample)

From SEGGER Wiki
Revision as of 15:21, 17 April 2020 by Florian (talk | contribs) (Created page with "{| class="wikitable" style="float:right; margin-left: 10px; background-color: #f9f9f9;" ! colspan="2" style="font-weight:bold; font-size:17px; font-family:Arial, Helvetica, sa...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
GUI_CreateBitmapFromStream.c
GUI CreateBitmapFromStream.png
File(s) required
  • GUI_CreateBitmapFromStream.c
  • segger-logo.dta
Runs in simulation Yes
Runs on target Yes
Download GUI_CreateBitmapFromStream.zip

This sample creates and draws a streamed bitmap from an external .dta file. The example supports both the usage of Windows file system and emFile, if run on a target.

Code

/*********************************************************************
*                    SEGGER Microcontroller GmbH                     *
*        Solutions for real time microcontroller applications        *
**********************************************************************
*                                                                    *
*        (c) 1996 - 2020  SEGGER Microcontroller GmbH                *
*                                                                    *
*        Internet: www.segger.com    Support:  support@segger.com    *
*                                                                    *
**********************************************************************

** emWin V6.10 - Graphical user interface for embedded applications **
emWin is protected by international copyright laws.   Knowledge of the
source code may not be used to write a similar product.  This file may
only  be used  in accordance  with  a license  and should  not be  re-
distributed in any way. We appreciate your understanding and fairness.
----------------------------------------------------------------------
File        : GUI_CreateBitmapFromStream.c
Purpose     : Sample that demonstrates how to create and draw a
              streamed bitmap from an external file.
              This sample supports both usage in the Windows Simulation as well
              as usage on a target device using emFile.
Requirements: WindowManager - ( )
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
---------------------------END-OF-HEADER------------------------------
*/

#include "GUI.h"
#include <stdlib.h>
#ifdef WIN32
  #include <Windows.h>
#else
  #include "FS.h"
#endif

/*********************************************************************
*
*       Defines
*
**********************************************************************
*/

#ifdef WIN32
  #define FILE_PATH "c:\\Work\\emWin\\Intern\\TutorialV6\\Core\\Bitmaps\\segger-logo.dta"
#else
  #define FILE_PATH "segger-logo.dta"
#endif

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
#ifdef WIN32
  HANDLE          hFile;
  int             NumBytesRead;
  int             Size;
#else
  FS_FILE_INFO    Info;
  FS_FILE       * pFile;
  char            acVolumeName[10];
  int             i;
#endif
  GUI_BITMAP      Bitmap;
  GUI_LOGPALETTE  Palette;
  void          * pData;
  
  GUI_Init();

 #ifndef WIN32
  //
  // Start emFile
  //
  FS_Init();
  //
  // Enable long file name support
  //
  FS_FAT_SupportLFN();
  //
  // Mount volume
  //
  FS_GetVolumeName(0, acVolumeName, sizeof(acVolumeName));
  if (FS_Mount(acVolumeName) > 0) {
    //
    // Open file, retrieve file size
    // Allocate memory and read file into memory
    //
    pFile = FS_FOpen(FILE_PATH, "rb");
    FS_GetFileInfo(FILE_PATH, &Info);
    pData = malloc(Info.FileSize);
    do {
      i = FS_FRead(pData, 1, Info.FileSize, pFile);
    } while(i);
    //
    // Close file
    //
    FS_FClose(pFile);
  }
#else
  //
  // Create file handle
  //
  hFile = CreateFile(FILE_PATH, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  //
  // Alloc memory and read file data into buffer
  //
  Size = GetFileSize(hFile, NULL);
  pData = malloc(Size);
  ReadFile(hFile, pData, Size, (LPDWORD)&NumBytesRead, NULL);
  //
  // Close file
  //
  CloseHandle(hFile);
#endif
  //
  // Create bitmap from data and draw it
  //
  GUI_CreateBitmapFromStream(&Bitmap, &Palette, pData);
  GUI_DrawBitmap(&Bitmap, 10, 10);
  //
  // Free allocated memory
  //
  free(pData);

  while (1) {
    GUI_Delay(100);
  }
}

/*************************** End of file ****************************/