//
// Bitmap BN 11-Juni-98
//
//
//
//
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

#include "WinMain.h"
#include "GlobalData.h"
#include "File.h"
#include "System.h"
#include "Bitmap.h"


// *****************************************************************
Bitmap::Init (void)
{
int nRet=TRUE;


memset(&pBitmap, 0, sizeof(Bitmap));
nAutoWert++; // dient als eindeutiger Schlüssel
return(nRet);
}

// *****************************************************************
Bitmap::DeInit (void)
{
int nRet=TRUE;


if(pBitmap.Bitmap!=NULL){
pBitmap.nBreite = 0;
pBitmap.nHoehe = 0;
DeleteObject(pBitmap.Bitmap);
}
return(nRet);
}

// *****************************************************************
Bitmap::DoLoadBitmap (char *acBefehl_ID)
{
int nRet=FALSE;
char acFilename[512];
int nDateiHandle;
BITMAPFILEHEADER Bitmap_File_Header;
BITMAPINFOHEADER Bitmap_Info_Header;
BITMAPINFO Bitmap_Info;
int nCounter=0;
void *pPointer=FALSE;
HDC BitmapDC;


GetName(acFilename);
if(f_find(acFilename)==TRUE){
if((nDateiHandle = f_open(acFilename))!=FALSE){
if(f_read(nDateiHandle, &Bitmap_File_Header, sizeof(Bitmap_File_Header_))==TRUE){
if(Bitmap_File_Header.bfType==0x4D42){
if(f_read(nDateiHandle, &Bitmap_Info_Header, sizeof(Bitmap_Info_Header_))==TRUE){
if(Bitmap_Info_Header.biBitCount==24){
pPointer = Mem_Alloc(Bitmap_Info_Header.biWidth * Bitmap_Info_Header.biHeight * (Bitmap_Info_Header.biBitCount / 8));
if(pPointer!=NULL){
if(f_read(nDateiHandle, pPointer, Bitmap_Info_Header.biWidth * Bitmap_Info_Header.biHeight * (Bitmap_Info_Header.biBitCount / 8))==TRUE){
} else {
Mem_Free(pPointer, Bitmap_Info_Header.biWidth * Bitmap_Info_Header.biHeight * (Bitmap_Info_Header.biBitCount / 8));
pPointer = FALSE;
}
}
}
}
}
}
f_close(nDateiHandle);
}
}
if(pPointer!=FALSE){
memset(&Bitmap_Info, 0, sizeof(BITMAPINFO));
memcpy(&Bitmap_Info.bmiHeader, &Bitmap_Info_Header, sizeof(BITMAPINFOHEADER));
BitmapDC = GetDC(NULL);
pBitmap.Bitmap = CreateDIBitmap(BitmapDC, &Bitmap_Info.bmiHeader, CBM_INIT, pPointer, &Bitmap_Info, DIB_RGB_COLORS);
if(pBitmap.Bitmap!=NULL){
pBitmap.nBreite = Bitmap_Info_Header.biWidth;
pBitmap.nHoehe = Bitmap_Info_Header.biHeight;
nRet = TRUE;
}

Mem_Free(pPointer, Bitmap_Info_Header.biWidth * Bitmap_Info_Header.biHeight * (Bitmap_Info_Header.biBitCount / 8));
ReleaseDC(NULL, BitmapDC);
}
return(nRet);
}

// *****************************************************************
Bitmap::DoDisplayBitmap (char *acBefehl_ID, HDC PaintDC, int nX, int nY, HDC BitmapDC)
{
int nRet=FALSE;
HBITMAP OldBitmap;


if(pBitmap.Bitmap!=NULL){
OldBitmap = SelectObject(BitmapDC, pBitmap.Bitmap);
nRet = BitBlt(PaintDC, nX, nY, pBitmap.nBreite, pBitmap.nHoehe, BitmapDC, 0, 0, SRCCOPY);
SelectObject(BitmapDC, OldBitmap);
if(nRet!=FALSE){
nRet = TRUE;
}
}
return(nRet);
}

// *****************************************************************
Bitmap::GetName (char *GetName)
{
int nRet=TRUE;


memcpy(GetName, pBitmap.acName, sizeof(pBitmap.acName));
return(nRet);
}

// *****************************************************************
Bitmap::GetBreite (int *GetBreite)
{
int nRet=TRUE;


*GetBreite = pBitmap.nBreite;
return(nRet);
}

// *****************************************************************
Bitmap::GetHoehe (int *GetHoehe)
{
int nRet=TRUE;


*GetHoehe = pBitmap.nHoehe;
return(nRet);
}

// *****************************************************************
Bitmap::SetName (char *SetName)
{
int nRet=TRUE;


memset(pBitmap.acName, 0, sizeof(pBitmap.acName));
memcpy(pBitmap.acName, SetName, strlen(SetName));
return(nRet);
}

// *****************************************************************
Bitmap::SetBreite (int *SetBreite)
{
int nRet=TRUE;


pBitmap.nBreite = *SetBreite;
return(nRet);
}

// *****************************************************************
Bitmap::SetHoehe (int *SetHoehe)
{
int nRet=TRUE;


pBitmap.nHoehe = *SetHoehe;
return(nRet);
}

//
// BMP BN 27-Mär-98
//
//
//
//
/*
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <wingdi.h>
#include "System.h"
#include "File.h"*/
//#include <bmp.h>

/*******************************************************************/
/* */
/* GLOBALE Variablen */
/* */
/*******************************************************************/
/*******************************************************************/
/* */
/* GLOBALE FUNKTIONEN */
/* */
/*******************************************************************/
/*******************************************************************/
/* */
/* LOKALE Variablen */
/* */
/*******************************************************************/
/*******************************************************************/
/* */
/* LOKALE FUNKTIONEN */
/* */
/*******************************************************************/
/*

/*
typedef struct{
unsigned short int snID;
unsigned char lDateilaenge[4];
unsigned char snReserviert1[2];
unsigned char snReserviert2[2];
unsigned char lOffsetDatenbereich[4];
} Bitmap_File_Header_;


typedef struct{
long lGroesseBitmapInfoHeader;
long lBreiteBitmap;
long lHoeheBitmap;
short int snAnzahlFarbebenen;
short int snAnzahlBitsProPixel;
long lKompressionstyp;
long lBildgroesseInByte;
long lHorizontaleAufloesung;
long lVertikaleAufloesung;
long lAnzahlDerBenutztenFarben;
long lAnzahlDerWichtigenFarben;
} Bitmap_Info_Header_;

typedef struct{
char cRGB_BLUE;
char cRGB_GREEN;
char cRGB_RED;
char cRGB_RES;
}RGB_Eintrag_;


#define TRUE 1
#define FALSE 0
*/
HBITMAP LoadBMPFile (char *acDatei);
HBITMAP LoadBMPFileData (char *acDatei);

/*******************************************************************/
/* LoadBMPFile */
/*******************************************************************/

HBITMAP LoadBMPFile (char *acDatei)
{
return(LoadBMPFileData(acDatei));
}


HBITMAP LoadBMPFileData (char *acDatei)
{
int nDateiHandle;
BITMAPFILEHEADER Bitmap_File_Header;
BITMAPINFOHEADER Bitmap_Info_Header;
BITMAPINFO Bitmap_Info;
int nCounter=0;
void *pPointer=FALSE;
char acBMPDatei[256];
HBITMAP LoadedBitmap;
HDC DrawDC;


sprintf(acBMPDatei, "%s", acDatei);
if(f_find(acBMPDatei)==TRUE){
if((nDateiHandle = f_open(acBMPDatei))!=FALSE){
if(f_read(nDateiHandle, &Bitmap_File_Header, sizeof(Bitmap_File_Header_))==TRUE){
if(Bitmap_File_Header.bfType==0x4D42){
if(f_read(nDateiHandle, &Bitmap_Info_Header, sizeof(Bitmap_Info_Header_))==TRUE){
//if(Bitmap_Info_Header.lGroesseBitmapInfoHeader==sizeof(Bitmap_Info_Header_)){
if(Bitmap_Info_Header.biBitCount==24){
pPointer = Mem_Alloc(Bitmap_Info_Header.biWidth * Bitmap_Info_Header.biHeight * (Bitmap_Info_Header.biBitCount / 8));
if(pPointer!=NULL){
if(f_read(nDateiHandle, pPointer, Bitmap_Info_Header.biWidth * Bitmap_Info_Header.biHeight * (Bitmap_Info_Header.biBitCount / 8))==TRUE){
} else {
Mem_Free(pPointer, Bitmap_Info_Header.biWidth * Bitmap_Info_Header.biHeight * (Bitmap_Info_Header.biBitCount / 8));
pPointer = FALSE;
}

}
} else {
switch(Bitmap_Info_Header.biBitCount){
case 1:
nCounter = 2;
break;
case 4:
nCounter = 16;
break;
case 8:
nCounter = 256;
break;
}
if(nCounter>0){
}
}
//}
}
}
}

f_close(nDateiHandle);
}
}
if(pPointer!=FALSE){
memset(&Bitmap_Info, 0, sizeof(BITMAPINFO));
memcpy(&Bitmap_Info.bmiHeader, &Bitmap_Info_Header, sizeof(BITMAPINFOHEADER));
// Bitmap_Info.bmiColors = NULL;

DrawDC = GetDC(NULL);
LoadedBitmap = CreateDIBitmap(DrawDC, &Bitmap_Info.bmiHeader, CBM_INIT, pPointer, &Bitmap_Info, DIB_RGB_COLORS);
Mem_Free(pPointer, Bitmap_Info_Header.biWidth * Bitmap_Info_Header.biHeight * (Bitmap_Info_Header.biBitCount / 8));
ReleaseDC(NULL, DrawDC);
}
return(LoadedBitmap);
}