/* greywin.cpp:  functions for drawing greyscale images */

#include <owl.h>
#include <stdlib.h>
#include <windows.h>
#include <stdio.h>
#include <inputdia.h>
#include <time.h>
#include <alloc.h>
#include "greywin.h"

TGreyWindow::TGreyWindow(PTWindowsObject AParent, LPSTR ATitle,
	unsigned char far** New_Data)
  : TWindow(AParent, ATitle)
{
	int i, j;

	Attr.W = WINWIDTH+10;
	Attr.H = WINHEIGHT+40;
	Attr.Style = WS_POPUP | WS_OVERLAPPEDWINDOW | WS_VISIBLE;
	Dptr = Data = New_Data;

	mapinfo = (BITMAPINFO*) malloc(sizeof(BITMAPINFOHEADER) +
			(MAXGREY+1) * sizeof(RGBQUAD));
	mapinfo->bmiHeader.biSize = (long) sizeof(BITMAPINFOHEADER);
	mapinfo->bmiHeader.biWidth = (long) WINWIDTH;
	mapinfo->bmiHeader.biHeight = (long) WINHEIGHT;
	mapinfo->bmiHeader.biPlanes = 1;
	mapinfo->bmiHeader.biBitCount = 8;
	mapinfo->bmiHeader.biCompression = BI_RGB;
	mapinfo->bmiHeader.biSizeImage = (long) (WINWIDTH * WINHEIGHT);
	mapinfo->bmiHeader.biClrUsed = 0;
	for (i=0 ; i<=MAXGREY ; i++)
  {
  	mapinfo->bmiColors[i].rgbBlue = (BYTE) i;
		mapinfo->bmiColors[i].rgbRed = (BYTE) i;
		mapinfo->bmiColors[i].rgbGreen = (BYTE) i;
		mapinfo->bmiColors[i].rgbReserved = (BYTE) 0;
	}
}

TGreyWindow::~TGreyWindow()
{
	DeleteObject(hpal);
	DeleteObject(Hbitmap);
  free(mapinfo);
}

void TGreyWindow::Paint(HDC DC, PAINTSTRUCT&)
{
	HPALETTE temppal;

	SelectPalette(DC, hpal, FALSE);
  RealizePalette(DC);
	MemDC = CreateCompatibleDC(DC);
	SelectPalette(MemDC, hpal, FALSE);
  RealizePalette(MemDC);
	SelectObject(MemDC, Hbitmap);
  BitBlt(DC, 0, 0, WINWIDTH, WINHEIGHT, MemDC, 0, 0, SRCCOPY);
  DeleteDC(MemDC);

  //SelectPalette(DC, temppal, FALSE);
}

void TGreyWindow::MakeMyPalette()
{
	int i, j, cColors=GREYLEVELS, c_inc, red, green, blue;
	LOGPALETTE far *plgpl;

    /* set up color palette for grayscale */
	plgpl = (LOGPALETTE far*) malloc(
    	sizeof(LOGPALETTE) + cColors * sizeof(PALETTEENTRY));

	plgpl->palNumEntries = cColors;
	plgpl->palVersion = 0x300;
  c_inc = (int) 256/cColors;

	for (i = 0, red = 0, green = 0, blue = 0; i < cColors;
		i++, red += c_inc, green += c_inc, blue += c_inc)
	{
		 plgpl->palPalEntry[i].peRed = LOBYTE(red);
		 plgpl->palPalEntry[i].peGreen = LOBYTE(green);
		 plgpl->palPalEntry[i].peBlue = LOBYTE(blue);
		 plgpl->palPalEntry[i].peFlags = PC_RESERVED;
	}
	hpal = CreatePalette(plgpl);
	free(/* (HLOCAL)*/ plgpl);
}

void TGreyWindow::SetupWindow()
{
	TWindow::SetupWindow();

	/* setup for greylevel image */
	MakeMyPalette();
	PixelDC =GetDC(HWindow);
	Hbitmap = CreateCompatibleBitmap(PixelDC, WINWIDTH, WINHEIGHT);
	//Hbitmap = CreateDIBitmap(PixelDC, (BITMAPINFOHEADER *) mapinfo,
	//		CBM_INIT, Dptr, mapinfo, DIB_RGB_COLORS);
	ReleaseDC(HWindow, PixelDC);
	SetData(Dptr);
}

void TGreyWindow::SetData(unsigned char far** Data_Pointer)
{
	int numlevels, i, j, pos;
	char tempstr[60];
	unsigned char value;
	
	PixelDC =GetDC(HWindow);
	oldpal = SelectPalette(PixelDC, hpal, FALSE);
	numlevels = RealizePalette(PixelDC);
	if (numlevels < GREYLEVELS)
	{
		sprintf(tempstr, "numlevels = %d", numlevels);
		//MessageBox(HWindow, "Realize Palette", tempstr, MB_OK);
  }
  
	MemDC = CreateCompatibleDC(PixelDC);
	oldpal2 = SelectPalette(MemDC, hpal, FALSE);
	RealizePalette(MemDC);
	for (i=0 ; i<WINHEIGHT ; i+=LINES_PER_BLOCK)
		SetDIBits(MemDC, Hbitmap, i, LINES_PER_BLOCK, Data_Pointer[i],
							mapinfo, DIB_RGB_COLORS);
	oldbitmap = SelectObject(MemDC, Hbitmap);
	BitBlt(PixelDC, 0, 0, WINWIDTH, WINHEIGHT, MemDC, 0, 0, SRCCOPY);
	SelectPalette(MemDC, oldpal2, FALSE);
	SelectObject(MemDC, oldbitmap);
	DeleteDC(MemDC);
	//SelectPalette(PixelDC, oldpal, FALSE);
	ReleaseDC(HWindow, PixelDC);
}
