/******************************************************************
 * memBMP.h
 *
 * Written by: Shuhua Lai 
 *
 * Permission to use, copy, modify, and distribute this file for 
 * any commercial usage is prohibited without prior permission of
 * the author. 
 *
 * THE MATERIAL EMBODIED IN THIS PROGRAM IS PROVIDED TO YOU "AS-IS"
 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
 * FITNESS FOR A PARTICULAR PURPOSE. 
*******************************************************************/

#ifndef WMEMBITMAP
#define WMEMBITMAP

#include "math.h"
#include "stdio.h"
#include "stdlib.h"
#include "memory.h"
#include "string.h"
#include 

#include 
#include 

#include 
#include 
#include 


#ifdef   WIN32
#include "io.h"
#define  FILEMODE O_RDWR|O_BINARY
#else
#include 
#define  FILEMODE O_RDWR
#define CopyMemory memcpy
#define AfxMessageBox(a) 
#endif

#define MIN(a,b) (a)>(b)?(b):(a)
#define MAX(a,b) (a)>(b)?(a):(b)

#define WORD  unsigned short
#define DWORD unsigned int
#define BYTE  unsigned char
#define LONG  long
#define BOOL  char
#define TRUE  1
#define FALSE 0

#define LPCTSTR char *


#define COLORREF long
 
#define RGB(r, g ,b)  ((DWORD) (((BYTE) (r) | ((WORD) (g) << 8)) | (((DWORD) (BYTE) (b)) << 16)))
#define GetRValue(rgb)   ((BYTE) (rgb))
#define GetGValue(rgb)   ((BYTE) (((WORD) (rgb)) >> 8))
#define GetBValue(rgb)   ((BYTE) ((rgb) >> 16))
 
 
#define EXCANGE(a,b) {a = a+b; b = a-b; a = a-b;}


class memBitmap {
private:

	WORD    bfType;
	DWORD   bfSize;
	WORD    bfReserved1;
	WORD    bfReserved2;
	DWORD   bfOffBits;

	DWORD      biSize;
	LONG       biWidth;
	LONG       biHeight;
	WORD       biPlanes;
	WORD       biBitCount;
	DWORD      biCompression;
	DWORD      biSizeImage;
	LONG       biXPelsPerMeter;
	LONG       biYPelsPerMeter;
	DWORD      biClrUsed;
	DWORD      biClrImportant;


	unsigned char *bitData;

	int BytesPerLine;
	unsigned long int lenth;
	unsigned char *bData;

	int origx,origy; // the origin coordinate of the image on screen


private:
	int HugeRead(int fd,void* buf, int len);
	int HugeWrite(int fd, void* buf, int len);
	void SetInfo();
	BOOL CreateDirect(int cx,int cy);
	
	void SETWV(BYTE *v,BYTE *w) {
		#ifdef WIN32
		v[0] = w[0];
		v[1] = w[1];
		#else
/*
		v[0] = w[1];
		v[1] = w[0];
*/
		v[0] = w[0];
		v[1] = w[1];
		#endif
	}
	void SETLV(BYTE *v,BYTE *l) {
		#ifdef WIN32 
		v[0] = l[0];
		v[1] = l[1];
		v[2] = l[2];
		v[3] = l[3];
		#else
/*
		v[0] = l[3];
		v[1] = l[2];
		v[2] = l[1];
		v[3] = l[0];
*/
		v[0] = l[0];
		v[1] = l[1];
		v[2] = l[2];
		v[3] = l[3];
		#endif
	}	

public:
	memBitmap();
	memBitmap(int ImageWidth,int ImageHeight);
	~memBitmap();	
	
	BOOL Load(LPCTSTR filename);//Load a BMP file into memory
	BOOL Save(LPCTSTR filename);//Save the BMP into file
	void Display(int x,int y);  //display the image at (x,y)
	void Display(int x,int y,int cx,int cy); //display the image at (x,y) with width=cx and height=cy
	int GetWidth(); // get the width of the image
	int GetHeight();// get the height of the image

	BOOL ChangeSize(int cx,int cy);// change the size of the image to width=cx and height=cy
	int  GetOrigx(); // get the origin position of the image in the window
	int  GetOrigy();
	COLORREF GetPixel(int x,int y); //get the RGB of the pixel of (x,y)
	void SetPixel(int x,int y,COLORREF color); //set the RGB of the pixel of (x,y)
	void DoRotate(int jd1); // rotate the image with angle of jd1
	void DoScale(float sw,float sh);//scale the image with sw in  x direction and sh in y direction
	void Release(); // Release the image from memory

};

#endif