#include <windows.h>

#include "common.h"


LRESULT CALLBACK HistogramWndProc( HWND, UINT, WPARAM, LPARAM );


DWORD HistogramThread(PVOID ptr)
{
	static char szClassName[] = "Histogram Window";
	HWND		hwnd;
	MSG			msg;
	WNDCLASSEX	wndclass;

	Image_data  *piData;
	HDC			hdc, hdcMem;
	HPEN		hPen;
	HBITMAP		hBitmap;

	int			numPixels, histogram[256], i, maxval;
	

	piData = (Image_data *) ptr;

	wndclass.cbSize			= sizeof (wndclass);
	wndclass.style			= CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc	= HistogramWndProc;
	wndclass.cbClsExtra		= 0;
	wndclass.cbWndExtra		= 0;
	wndclass.hInstance		= hInst;
	wndclass.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hCursor		= LoadCursor (NULL, IDC_ARROW);
	wndclass.hbrBackground	= (HBRUSH) GetStockObject (WHITE_BRUSH);
	wndclass.lpszMenuName	= NULL;
	wndclass.lpszClassName	= szClassName;
	wndclass.hIconSm		= LoadIcon (NULL, IDI_APPLICATION);

	RegisterClassEx (&wndclass);
 

	hwnd = CreateWindow ( szClassName,
						  "Histogram",
						  WS_OVERLAPPEDWINDOW,
						  CW_USEDEFAULT, CW_USEDEFAULT,
						  307, 226,
						  NULL, NULL, 
						  hInst, NULL);
	if( ! hwnd )    
		return( 0 );
	
	++iThreadCount;  

	ShowWindow( hwnd, SW_SHOW ); 
	SetForegroundWindow(hwnd);

	hdc = GetDC(hwnd);
	hdcMem = CreateCompatibleDC(hdc);
	hBitmap = CreateCompatibleBitmap(hdc, 300, 200);
	SelectObject(hdcMem, hBitmap);
	ReleaseDC (hwnd, hdc);


	for(i = 0; i < 256; i++)
		histogram[i] = 0;

	numPixels = piData->n_rows * piData->n_cols;
	for (i = 0; i < numPixels; i++)
		++histogram[piData->image_G_ptr[i]];

	maxval = 0;
	for(i = 0; i < 256; i++)
		if (histogram[i] > maxval)
			maxval = histogram[i];

	//wsprintf(str, "max = %d", maxval);
	//MessageBox(0, str, "", MB_OK);

	SelectObject(hdcMem, GetStockObject(BLACK_PEN));
	SelectObject(hdcMem, GetStockObject(WHITE_BRUSH));
	Rectangle(hdcMem, -1, -1, 300, 200);
	Rectangle(hdcMem, 10, 10, 290, 190);

	hPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 220));
	SelectObject(hdcMem, hPen);

	for (i = 0; i < 256; i++)
	{
		MoveToEx(hdcMem, i + 20, 188, NULL);
		LineTo(hdcMem, i + 20, 188 - (histogram[i] * 160/maxval));
	}

	SetWindowLong(hwnd, GWL_USERDATA, (long)&hdcMem);


	while( IsWindow( hwnd ) &&  WaitForSingleObject( hStopEvent, 0 ) != WAIT_OBJECT_0 )
	{                                               
			WaitMessage();
			GetMessage( &msg, hwnd, 0, 0 );
			TranslateMessage( &msg );
			DispatchMessage( &msg );
	}

	DeleteDC (hdcMem);
	DeleteObject(hBitmap);
	DeleteObject(hPen);

	--iThreadCount;               
	return( 0 );

}

LRESULT CALLBACK HistogramWndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	HDC			hdc, *phdcMem;
	PAINTSTRUCT ps;
	RECT		rect = {180, 11, 285, 25};
	char		str[64];

	switch( msg )
	{
		case WM_CREATE:
			return 0;

		
		case WM_PAINT:
			hdc = BeginPaint(hwnd, &ps);
			phdcMem = (HDC *)GetWindowLong(hwnd, GWL_USERDATA);
			if (phdcMem)
			{
				BitBlt(hdc, 
					   0, 
					   0,
					   300,
					   200,
					   *phdcMem, 
					   0, 
					   0,
					   SRCCOPY);
			}
			SelectObject(hdc, hRedPen);
			MoveToEx(hdc, threshold + 20, 188, NULL);
			LineTo(hdc, threshold + 20, 10);
			EndPaint(hwnd, &ps);
			return 0;

		case WM_LBUTTONUP:
			if (LOWORD(lParam) < 20) 
				threshold = 0;
			else if (LOWORD(lParam) > 275)
				threshold = 255;
			else
				threshold = LOWORD(lParam) - 20;

			phdcMem = (HDC *)GetWindowLong(hwnd, GWL_USERDATA);
			wsprintf(str, "threshold = %.3d", threshold);
			DrawText(*phdcMem, str, lstrlen(str), &rect, DT_LEFT);
			InvalidateRect(hwnd, NULL, FALSE);
			return 0;


		case WM_DESTROY:
			PostQuitMessage(0);
			return 0;
	}
	return DefWindowProc(hwnd, msg, wParam, lParam);
}