/**
  * @brief  draw the image after scaling it
  * @param  hFile: pointer to the image file
  * @param  Enlarge: 0 = window mode
  *                  1 = full screen mode
  * @param  type:    specify the image type
  *                  this parameter can be:
  *                  - IMAGE_TYPE_JPG
  *                  - IMAGE_TYPE_BMP
  * @retval None
  */
static int _DrawImage(FIL *hFile, uint8_t Enlarge, uint8_t type)
{
  int XPos, YPos, XSize, YSize, nx, ny, n;
  GUI_JPEG_INFO Info;

  if(type == IMAGE_TYPE_BMP)
  {
    Info.XSize = GUI_BMP_GetXSizeEx(_GetData, hFile);
    Info.YSize = GUI_BMP_GetYSizeEx(_GetData, hFile);
  }
  else if (type == IMAGE_TYPE_JPG)
  {
    GUI_JPEG_GetInfoEx(_GetData, hFile, &Info);
  }

  if((Info.XSize == 0) || (Info.YSize == 0) ||
     (Info.XSize > 1024) || (Info.YSize > 768))
  {
    return 1;
  }
  if(Enlarge == 0)
  {
    nx = 220000 / Info.XSize;
    ny = 134000 / Info.YSize;
  }
  else
  {
    nx = 320000 / Info.XSize;
    ny = 240000 / Info.YSize;
  }

  if (nx < ny) {
    n = nx;
  } else {
    n = ny;
  }

  XSize = Info.XSize * n / 1000;
  YSize = Info.YSize * n / 1000;
  if(Enlarge == 0)
  {
    XPos = (220 - XSize) / 2;
    YPos = (134 - YSize) / 2;
  }
  else
  {
    XPos = (320 - XSize) / 2;
    YPos = (240 - YSize) / 2;
  }
  if(type == IMAGE_TYPE_BMP)
  {
    return GUI_BMP_DrawScaledEx(_GetData, hFile, XPos, YPos, n, 1000);
  }
  else if (type == IMAGE_TYPE_JPG)
  {
    return GUI_JPEG_DrawScaledEx(_GetData, hFile, XPos, YPos, n, 1000);
  }

  return 0;
}
/*********************************************************************
*
*       _DrawJPEG
*/
static void _DrawJPEG(const char * sFilename) {
    int XPos, YPos, XSize, YSize, nx, ny, n;
    HANDLE hFile;
    GUI_JPEG_INFO Info;
    hFile = CreateFile(sFilename, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
    GUI_JPEG_GetInfoEx(_GetData, &hFile, &Info);
    GUI_ClearRect(0, 60, 319, 239);
    nx = 320000 / Info.XSize;
    ny = 180000 / Info.YSize;
    if (nx < ny) {
        n = nx;
    } else {
        n = ny;
    }
    XSize = Info.XSize * n / 1000;
    YSize = Info.YSize * n / 1000;
    XPos = (320 - XSize) / 2;
    YPos = (180 - YSize) / 2 + 60;
    if (n > 1000) {
        int temp=0;
    }
    GUI_JPEG_DrawScaledEx(_GetData, &hFile, XPos, YPos, n, 1000);
    GUI_Delay(1000);
    CloseHandle(hFile);
}
/**
  * @brief  draw the image after scaling it
  * @param  hFile: pointer to the MJPEG file
  * @retval None
  */
static int _PlayMJPEG(FIL *hFile) 
{
  unsigned int  NumBytesRead;
  char *ptr;
  int timeout = 0xFFFF;
  static int counter = 0;
  GUI_JPEG_INFO video_info;

  f_lseek(hFile, ImageOffset);
  
  t0 = GUI_GetTime();
  s0 = hFile->fptr;
  
 
  if(VIDEO_Enlarge == 1)
  {
    GUI_JPEG_DrawEx(_GetData, hFile, 0, 0);
  }
  else
  {
    GUI_JPEG_DrawScaledEx(_GetData, hFile, 7, 2, 2, 3);  
  }
  
  if(counter ++ > 20 )
  {
    frame_average_size =  (hFile->fptr - s0 + frame_average_size)/2; 
    frame_average_period = (GUI_GetTime() - t0 + frame_average_period)/2;
    counter = 0;
  }
  
  if(frame_average_size != 0)
    frame_speed = frame_average_size / frame_average_period ;
  else
    frame_speed = 70;
  
  ImageOffset = hFile->fptr;
  
  while ( --timeout > 0 )
  {
    f_lseek(hFile, ImageOffset - VIDEO_BUFFER_SIZE);
    f_read(hFile, (char *)_acVideoBuffer, VIDEO_BUFFER_SIZE, &NumBytesRead); 
    
    /* Get image offset */
    ptr = _acVideoBuffer;
    while (NumBytesRead --)
    {
      if((*ptr == 0xFF) && ((*(ptr + 1)) == M_SOI))
      {
        ImageOffset -=  (NumBytesRead + 1) ;
        GUI_JPEG_GetInfoEx   (_GetData, hFile, &video_info);
        if((video_info.XSize != 320) || (video_info.YSize != 240))
        {
          return -1; 
        }
        else
        {
          return 0;
        }
      }
      ptr++;
    }
    ImageOffset +=  2 * VIDEO_BUFFER_SIZE; 
    
    if (ImageOffset >= hFile->fsize)
    {
      return -1;
    }
    
  }
  return -1;
}