
#define _CRT_SECURE_NO_WARNINGS	  /* disable compiler warnings for standard C functions like strcpy() */

#define SQR(x)	((x)*(x))
#define	PLOT_MARGIN	5			  /* horizontal margin of empty pixels within sensel before/after data plot */ 
#define	PLOT_SHRINK	3			  /* vertical scale factor, 0...255 is divided by this number to determine pixel resolution */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <windows.h>
#include <winuser.h>

#include "globals.h"
#include "resource.h"

void PaintImage()

{
PAINTSTRUCT			Painter;
HDC					hDC;
SCROLLINFO			bar_pos;
BITMAPINFOHEADER	bm_info_header;
BITMAPINFO			bm_info;
char				textout[320];
RECT				rect,fillrect;
HBRUSH				brush;
HPEN				pen;
int					r,c,grey;
int					drawx,drawy;
POINT				PointPlot[SENSEL_SIZE+1];
int					TotalPoints,dt;

if (DataLoaded)
  {
  hDC=GetDC(MainWnd);
		  /* set horizontal scroll bar */
  GetClientRect(MainWnd,&rect);
  rect.left+=DISPLAY_COLS;
  DataDisplayWidth=rect.right-rect.left;
  bar_pos.cbSize=sizeof(SCROLLINFO);
  bar_pos.fMask=SIF_ALL;
  bar_pos.nMin=0; 
  bar_pos.nMax=TotalData-1+DataDisplayWidth-1;
  bar_pos.nPage=DataDisplayWidth;
  bar_pos.nPos=TimeIndex;
  SetScrollInfo(MainWnd,SB_HORZ,&bar_pos,TRUE);
  ShowScrollBar(MainWnd,SB_HORZ,TRUE);
		  /* determine the part of screen in which to draw data, bounded by rect */
  GetClientRect(MainWnd,&rect);		/* vertical size has changed due to horizontal scroll bar */
  rect.left+=DISPLAY_COLS;
  fillrect=rect;
  if (0)
	{	  /* clearing causes flicker -- not needed if redrawing every rectangle below */
	brush=CreateSolidBrush(RGB(255,255,255));
	FillRect(hDC,&fillrect,brush);		/* clear right-side half of window */
	fillrect.top+=DISPLAY_ROWS;
	fillrect.left=0; fillrect.right=DISPLAY_COLS-1;
	FillRect(hDC,&fillrect,brush);		/* clear bottom-left quarter of window */
	DeleteObject(brush);
	}

		/* Segment data.  Should probably do this elsewhere.  I will reorganize later. */
  SegmentRegions();

		/* draw scale image for this time index */
  pen=CreatePen(PS_SOLID,2,RGB(0,255,0));
  SelectObject(hDC,pen);
  for (r=0; r<GRID_ROWS; r++)
	{
	for (c=0; c<GRID_COLS; c++)
	  {
	  drawx=DISPLAY_COLS+50+c*SENSEL_SIZE;
	  drawy=50+r*SENSEL_SIZE;
	  if (Data[(GRID_ROWS*GRID_COLS*TimeIndex)+(r*GRID_COLS+c)] <= 0.0)
		grey=0;
	  else if (Data[(GRID_ROWS*GRID_COLS*TimeIndex)+(r*GRID_COLS+c)] >= 63.0)
		grey=255;
	  else
		grey=(int)(Data[(GRID_ROWS*GRID_COLS*TimeIndex)+(r*GRID_COLS+c)]*4.0);
	  brush=CreateSolidBrush(RGB(grey,grey,grey));
	  SelectObject(hDC,brush);
	  Rectangle(hDC,drawx,drawy,drawx+SENSEL_SIZE,drawy+SENSEL_SIZE);
	  DeleteObject(brush);
	  if (DisplayWeights == 1)
		{
		sprintf(textout,"%.1lf",Data[(GRID_ROWS*GRID_COLS*TimeIndex)+(r*GRID_COLS+c)]);
		TextOut(hDC,drawx+5,drawy+5,textout,strlen(textout));
		}
	  if (DisplayRegions == 1)
		{
		if (Regions[r*GRID_COLS+c] != 0)
		  {
		  if (RegionWeight[Regions[r*GRID_COLS+c]] <= 0.0)
			grey=0;
		  else if (RegionWeight[Regions[r*GRID_COLS+c]] >= 500.0)
			grey=255;
		  else
			grey=(int)(RegionWeight[Regions[r*GRID_COLS+c]]/2.0);
		  brush=CreateSolidBrush(RGB(grey,grey,grey));
		  SelectObject(hDC,brush);
		  Rectangle(hDC,drawx,drawy,drawx+SENSEL_SIZE,drawy+SENSEL_SIZE);
		  DeleteObject(brush);
		  sprintf(textout,"%d",Regions[r*GRID_COLS+c]);
		  TextOut(hDC,drawx+SENSEL_SIZE-15,drawy+5,textout,strlen(textout));
		  sprintf(textout,"%.1lf",RegionWeight[Regions[r*GRID_COLS+c]]);
		  TextOut(hDC,drawx+5,drawy+35,textout,strlen(textout));
		  }
		}
	  if (DisplayPlots == 1)
		{
		TotalPoints=0;
		for (dt=-SENSEL_SIZE/2+PLOT_MARGIN; dt<=SENSEL_SIZE/2-PLOT_MARGIN; dt++)
		  {
		  if (dt+TimeIndex < 0  ||  dt+TimeIndex >= TotalData)
			continue;
		  PointPlot[TotalPoints].x=drawx+5+TotalPoints;
		  PointPlot[TotalPoints].y=drawy+SENSEL_SIZE-PLOT_MARGIN-(int)(Data[(GRID_ROWS*GRID_COLS*(TimeIndex+dt))+(r*GRID_COLS+c)])/PLOT_SHRINK;
		  TotalPoints++;
		  }
		Polyline(hDC,PointPlot,TotalPoints);
		}
	  }
	}
  DeleteObject(pen);

  sprintf(textout,"Data %d of %d  time %02d:%02d     ",TimeIndex,TotalData,(TimeIndex/10)/60,(TimeIndex/10)%60);
  TextOut(hDC,DISPLAY_COLS+50,20,textout,strlen(textout));

  ReleaseDC(MainWnd,hDC);
  }

if (VideoLoaded  &&  disp_image != NULL)
  {
  ReadVideoFrame(TimeIndex*1000/10+VideoSyncOffset,disp_image);	/* *1000/10 converts 10Hz data to milliseconds */

  BeginPaint(MainWnd,&Painter);
  hDC=GetDC(MainWnd);
  bm_info_header.biSize=sizeof(BITMAPINFOHEADER); 
  bm_info_header.biWidth=DISPLAY_COLS;
  bm_info_header.biHeight=-DISPLAY_ROWS; 
  bm_info_header.biPlanes=1;
  bm_info_header.biBitCount=24; 
  bm_info_header.biCompression=BI_RGB; 
  bm_info_header.biSizeImage=0; 
  bm_info_header.biXPelsPerMeter=0; 
  bm_info_header.biYPelsPerMeter=0;
  bm_info_header.biClrUsed=0; 
  bm_info_header.biClrImportant=0;
  // bm_info.bmiColors=NULL;
  bm_info.bmiHeader=bm_info_header;
  SetDIBitsToDevice(hDC,0,0,DISPLAY_COLS,DISPLAY_ROWS,0,0,
				0, /* first scan line */
				DISPLAY_ROWS, /* number of scan lines */
				disp_image,&bm_info,DIB_RGB_COLORS);
  ReleaseDC(MainWnd,hDC);
  EndPaint(MainWnd,&Painter);
  }
}




LRESULT CALLBACK ChangeFFSpeed(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)

{
char	  text[320];

switch(uMsg)
  {
  case WM_INITDIALOG:
	sprintf(text,"%d",FFspeed);
	SetDlgItemText(hDlg,IDC_EDIT1,text);
	sprintf(text,"%d",PlayDelay);
	SetDlgItemText(hDlg,IDC_EDIT2,text);
	break;
  case WM_COMMAND:
	switch(LOWORD(wParam))
	  {
	  case IDOK:
		GetDlgItemText(hDlg,IDC_EDIT1,text,320);
		FFspeed=atoi(text);
		if (FFspeed < 2)
		  FFspeed=2;
		if (FFspeed > 100)
		  FFspeed=100;
		GetDlgItemText(hDlg,IDC_EDIT2,text,320);
		PlayDelay=atoi(text);
		if (PlayDelay < 1)
		  PlayDelay=1;
		if (PlayDelay > 999)
		  PlayDelay=999;
		EndDialog(hDlg,IDOK);
		break;
		}
	  break;
	default:
	  return(FALSE);
  }
return(TRUE);
}




