Andrew Reed / Mbed OS CITY1082-i2c_master_wifi_mqtt
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers 2DGL_DrawJPEGScaled.c Source File

2DGL_DrawJPEGScaled.c

00001 /*********************************************************************
00002 *                SEGGER Microcontroller GmbH                         *
00003 *        Solutions for real time microcontroller applications        *
00004 **********************************************************************
00005 *                                                                    *
00006 *        (c) 1996 - 2018  SEGGER Microcontroller GmbH                *
00007 *                                                                    *
00008 *        Internet: www.segger.com    Support:  support@segger.com    *
00009 *                                                                    *
00010 **********************************************************************
00011 
00012 ** emWin V5.48 - Graphical user interface for embedded applications **
00013 All  Intellectual Property rights  in the Software belongs to  SEGGER.
00014 emWin is protected by  international copyright laws.  Knowledge of the
00015 source code may not be used to write a similar product.  This file may
00016 only be used in accordance with the following terms:
00017 
00018 The software  has been licensed to  Cypress Semiconductor Corporation,
00019 whose registered  office is situated  at 198 Champion Ct. San Jose, CA 
00020 95134 USA  solely for the  purposes of creating  libraries for Cypress
00021 PSoC3 and  PSoC5 processor-based devices,  sublicensed and distributed
00022 under  the  terms  and  conditions  of  the  Cypress  End User License
00023 Agreement.
00024 Full source code is available at: www.segger.com
00025 
00026 We appreciate your understanding and fairness.
00027 ----------------------------------------------------------------------
00028 Licensing information
00029 Licensor:                 SEGGER Microcontroller Systems LLC
00030 Licensed to:              Cypress Semiconductor Corp, 198 Champion Ct., San Jose, CA 95134, USA
00031 Licensed SEGGER software: emWin
00032 License number:           GUI-00319
00033 License model:            Services and License Agreement, signed June 10th, 2009
00034 Licensed platform:        Any Cypress platform (Initial targets are: PSoC3, PSoC5)
00035 ----------------------------------------------------------------------
00036 Support and Update Agreement (SUA)
00037 SUA period:               2009-06-12 - 2022-07-27
00038 Contact to extend SUA:    sales@segger.com
00039 ----------------------------------------------------------------------
00040 File        : 2DGL_DrawJPEGScaled.c
00041 Purpose     : Sample for scaled drawing JPEG files from external memory.
00042 Requirements: WindowManager - ( )
00043               MemoryDevices - ( )
00044               AntiAliasing  - ( )
00045               VNC-Server    - ( )
00046               PNG-Library   - ( )
00047               TrueTypeFonts - ( )
00048 
00049               Can be used in a MS Windows environment only!
00050 ----------------------------------------------------------------------
00051 */
00052 
00053 #ifndef SKIP_TEST
00054 
00055 #include <windows.h>
00056 #include <stdio.h>
00057 
00058 #include "GUI.h"
00059 
00060 /*********************************************************************
00061 *
00062 *       Defines
00063 *
00064 **********************************************************************
00065 */
00066 #define FRAMED_SIZE_Y 20
00067 #define TITLE_HEIGHT  40
00068 #define BORDER_SIZE   5
00069 #define YPOS_IMAGE    73
00070 
00071 //
00072 // Recommended memory to run the sample with adequate performance
00073 //
00074 #define RECOMMENDED_MEMORY (1024L * 200)
00075 
00076 /*********************************************************************
00077 *
00078 *       Static data
00079 *
00080 **********************************************************************
00081 */
00082 static U8 _acBuffer[0x200];
00083 
00084 /*********************************************************************
00085 *
00086 *       Static functions
00087 *
00088 **********************************************************************
00089 */
00090 /*********************************************************************
00091 *
00092 *       _GetData
00093 *
00094 * Function description
00095 *   This routine is called by GUI_JPEG_DrawEx(). The routine is responsible
00096 *   for setting the data pointer to a valid data location with at least
00097 *   one valid byte.
00098 *
00099 * Parameters:
00100 *   p           - Pointer to application defined data.
00101 *   NumBytesReq - Number of bytes requested.
00102 *   ppData      - Pointer to data pointer. This pointer should be set to
00103 *                 a valid location.
00104 *   StartOfFile - If this flag is 1, the data pointer should be set to the
00105 *                 beginning of the data stream.
00106 *
00107 * Return value:
00108 *   Number of data bytes available.
00109 */
00110 static int _GetData(void * p, const U8 ** ppData, unsigned NumBytesReq, U32 Off) {
00111   HANDLE * phFile;
00112   DWORD    NumBytesRead;
00113 
00114   phFile = (HANDLE *)p;
00115   //
00116   // Check buffer size
00117   //
00118   if (NumBytesReq > sizeof(_acBuffer)) {
00119     NumBytesReq = sizeof(_acBuffer);
00120   }
00121   //
00122   // Set file pointer to the required position
00123   //
00124   SetFilePointer(*phFile, Off, 0, FILE_BEGIN);
00125   //
00126   // Read data into buffer
00127   //
00128   ReadFile(*phFile, _acBuffer, NumBytesReq, &NumBytesRead, NULL);
00129   //
00130   // Set data pointer to the beginning of the buffer
00131   //
00132   *ppData = _acBuffer;
00133   //
00134   // Return number of available bytes
00135   //
00136   return NumBytesRead;
00137 }
00138 
00139 /*********************************************************************
00140 *
00141 *       _DrawJPEGs
00142 *
00143 * Function description
00144 *   Draws the given JPEG image.
00145 */
00146 static void _DrawJPEGs(const char * sFileName) {
00147   const char    acError[] = "There is possibly not enough memory to display this JPEG image.\n\nPlease assign more memory to emWin in GUIConf.c.";
00148   GUI_JPEG_INFO Info;
00149   GUI_RECT      Rect;
00150   HANDLE        hFile;
00151   int           xSizeScreen, ySizeScreen;
00152   int           xSizeScale,  ySizeScale;
00153   int           xSize,       ySize;
00154   int           xPos,        yPos;
00155   int           xNum,        yNum;
00156   int           Num;
00157   int           i;
00158   int           r;
00159 
00160   xSizeScreen = LCD_GetXSize();
00161   ySizeScreen = LCD_GetYSize();
00162   //
00163   // Display file name.
00164   //
00165   Rect.x0 = BORDER_SIZE;
00166   Rect.y0 = TITLE_HEIGHT;
00167   Rect.x1 = xSizeScreen - BORDER_SIZE - 1;
00168   Rect.y1 = YPOS_IMAGE - 1;
00169   GUI_ClearRectEx(&Rect);
00170   GUI_SetFont(&GUI_Font8x16);
00171   GUI_DispStringInRectWrap(sFileName, &Rect, GUI_TA_HCENTER | GUI_TA_VCENTER, GUI_WRAPMODE_CHAR);
00172   GUI_SetTextMode(GUI_TM_TRANS);
00173   GUI_SetFont(&GUI_Font20F_ASCII);
00174   //
00175   // Display each image scaled to 3 different sizes.
00176   //
00177   hFile = CreateFile(sFileName, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
00178   GUI_JPEG_GetInfoEx(_GetData, &hFile, &Info);
00179   for (i = 0; i < 3; i++) {
00180     //
00181     // Clear the area in which the JPEG files are displayed and set clipping rectangle.
00182     //
00183     Rect.x0 = BORDER_SIZE;
00184     Rect.y0 = YPOS_IMAGE;
00185     Rect.x1 = xSizeScreen - BORDER_SIZE - 1;
00186     Rect.y1 = ySizeScreen - BORDER_SIZE - 1;
00187     GUI_ClearRectEx(&Rect);
00188     GUI_SetClipRect(&Rect);
00189     //
00190     // Set scaling sizes.
00191     //
00192     switch (i) {
00193     case 0:
00194       xSizeScale = (xSizeScreen - BORDER_SIZE * 2)          / 2;
00195       ySizeScale = (ySizeScreen - BORDER_SIZE + YPOS_IMAGE) / 2;
00196       break;
00197     case 2:
00198       xSizeScale = (xSizeScreen - BORDER_SIZE * 2)          * 2;
00199       ySizeScale = (ySizeScreen - BORDER_SIZE + YPOS_IMAGE) * 2;
00200       break;
00201     default:
00202       xSizeScale = xSizeScreen - BORDER_SIZE * 2;
00203       ySizeScale = ySizeScreen - BORDER_SIZE + YPOS_IMAGE;
00204       break;
00205     }
00206     //
00207     // Calculate numerator and denominator for scaling and draw the image.
00208     //
00209     xNum = xSizeScale * 1000 / Info.XSize;
00210     yNum = ySizeScale * 1000 / Info.YSize;
00211     if (xNum < yNum) {
00212       Num = xNum;
00213     } else {
00214       Num = yNum;
00215     }
00216     xSize = Info.XSize * Num / 1000;
00217     ySize = Info.YSize * Num / 1000;
00218     xPos  = BORDER_SIZE + (xSizeScreen - BORDER_SIZE * 2 - xSize) / 2;
00219     yPos  = YPOS_IMAGE  + (ySizeScreen - YPOS_IMAGE      - ySize) / 2;
00220     r = GUI_JPEG_DrawScaledEx(_GetData, &hFile, xPos, yPos, Num, 1000);
00221     if (r) {
00222       //
00223       // The image could not be displayed successfully. Show an error message.
00224       //
00225       GUI_DispStringInRectWrap(acError, &Rect, GUI_TA_HCENTER | GUI_TA_VCENTER, GUI_WRAPMODE_WORD);
00226       GUI_Delay(2000);
00227       break;
00228     }
00229     //
00230     // Provide scaling information to the user.
00231     //
00232     GUI_SetClipRect(NULL);
00233     Rect.y0 = Rect.y1 - FRAMED_SIZE_Y * 2;
00234     switch (i) {
00235     case 0:
00236       GUI_DispStringInRectWrap("Scaled to 1/4 of the display area.",  &Rect, GUI_TA_BOTTOM | GUI_TA_LEFT, GUI_WRAPMODE_WORD);
00237       break;
00238     case 1:
00239       GUI_DispStringInRectWrap("Scaled to fit the display area.",     &Rect, GUI_TA_BOTTOM | GUI_TA_LEFT, GUI_WRAPMODE_WORD);
00240       break;
00241     case 2:
00242       GUI_DispStringInRectWrap("Scaled to 4 times the display area.", &Rect, GUI_TA_BOTTOM | GUI_TA_LEFT, GUI_WRAPMODE_WORD);
00243       break;
00244     }
00245     GUI_Delay(2000);
00246   }
00247   GUI_SetTextMode(GUI_TM_NORMAL);
00248   GUI_SetFont(&GUI_Font8x16);
00249   CloseHandle(hFile);
00250 }
00251 
00252 /*********************************************************************
00253 *
00254 *       _GetFirstBitmapDirectory
00255 *
00256 * Function description
00257 *   Returns the first directory which contains one or more JPG files.
00258 */
00259 static int _GetFirstBitmapDirectory(char * pDir, char * pBuffer) {
00260   WIN32_FIND_DATA Context;
00261   HANDLE          hFind;
00262   char            acMask[_MAX_PATH];
00263   char            acPath[_MAX_PATH];
00264 
00265   sprintf(acMask, "%s\\*.jpg", pDir);
00266   hFind = FindFirstFile(acMask, &Context);
00267   if (hFind != INVALID_HANDLE_VALUE) {
00268     sprintf(pBuffer, "%s\\", pDir);
00269     return 1;
00270   }
00271   sprintf(acMask, "%s\\*.", pDir);
00272   hFind = FindFirstFile(acMask, &Context);
00273   if (hFind != INVALID_HANDLE_VALUE) {
00274     do {
00275       if ((strcmp(Context.cFileName, ".") != 0) && (strcmp(Context.cFileName, "..") != 0)) {
00276         if (Context.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
00277           sprintf(acPath, "%s\\%s", pDir, Context.cFileName);
00278           if (_GetFirstBitmapDirectory(acPath, pBuffer)) {
00279             return 1;
00280           }
00281         }
00282       }
00283     } while (FindNextFile(hFind, &Context));
00284   }
00285   return 0;
00286 }
00287 
00288 /*******************************************************************
00289 *
00290 *       _DrawJPEGsFromWindowsDir
00291 *
00292 * Function description
00293 *   Iterates over all JPEG files in a Windows sub folder.
00294 */
00295 static void _DrawJPEGsFromWindowsDir(void) {
00296   WIN32_FIND_DATA Context;
00297   HANDLE          hFind;
00298   char            acPath[_MAX_PATH];
00299   char            acMask[_MAX_PATH];
00300   char            acFile[_MAX_PATH];
00301   char            acBuffer[_MAX_PATH];
00302   int             xSize;
00303 
00304   xSize = LCD_GetXSize();
00305   GUI_SetBkColor(GUI_BLACK);
00306   GUI_Clear();
00307   GUI_SetColor(GUI_WHITE);
00308   GUI_SetFont(&GUI_Font24_ASCII);
00309   GUI_DispStringHCenterAt("Draw JPEG Scaled - Sample", xSize / 2, 5);
00310   GetWindowsDirectory(acBuffer, sizeof(acBuffer));
00311   _GetFirstBitmapDirectory(acBuffer, acPath);
00312   sprintf(acMask, "%s*.jp*", acPath);
00313   hFind = FindFirstFile(acMask, &Context);
00314   if (hFind != INVALID_HANDLE_VALUE) {
00315     do {
00316       sprintf(acFile, "%s%s", acPath, Context.cFileName);
00317       _DrawJPEGs(acFile);
00318     } while (FindNextFile(hFind, &Context));
00319   } else {
00320     GUI_DispStringHCenterAt("No JPEG files found!", 160, 60);
00321     GUI_Delay(2000);
00322   }
00323 }
00324 
00325 /*********************************************************************
00326 *
00327 *       Public code
00328 *
00329 **********************************************************************
00330 */
00331 /*********************************************************************
00332 *
00333 *       MainTask
00334 */
00335 void MainTask(void) {
00336   GUI_Init();
00337   //
00338   // Check if recommended memory for the sample is available
00339   //
00340   if (GUI_ALLOC_GetNumFreeBytes() < RECOMMENDED_MEMORY) {
00341     GUI_ErrorOut("Not enough memory available."); 
00342     return;
00343   }
00344   while (1) {
00345     _DrawJPEGsFromWindowsDir();
00346   }
00347 }
00348 
00349 #endif
00350 
00351 /*************************** End of file ****************************/