DLBmpManager.cpp 1.75 KB
#include "stdafx.h"
#include <iostream>  
#include <windows.h> 
#include "DLBmpManager.h"  
#include "string_converter.h"
#include <fstream>

string CDLBmpManager::Encode(const char* Data, int DataByte)
{
	//编码表  
	const char EncodeTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	//返回值  
	string strEncode;
	unsigned char Tmp[4] = { 0 };
	int LineLength = 0;
	for (int i = 0; i < (int)(DataByte / 3); i++)
	{
		Tmp[1] = *Data++;
		Tmp[2] = *Data++;
		Tmp[3] = *Data++;
		strEncode += EncodeTable[Tmp[1] >> 2];
		strEncode += EncodeTable[((Tmp[1] << 4) | (Tmp[2] >> 4)) & 0x3F];
		strEncode += EncodeTable[((Tmp[2] << 2) | (Tmp[3] >> 6)) & 0x3F];
		strEncode += EncodeTable[Tmp[3] & 0x3F];
		//if (LineLength += 4, LineLength == 76) { strEncode += "\r\n"; LineLength = 0; }
	}
	//对剩余数据进行编码  
	int Mod = DataByte % 3;
	if (Mod == 1)
	{
		Tmp[1] = *Data++;
		strEncode += EncodeTable[(Tmp[1] & 0xFC) >> 2];
		strEncode += EncodeTable[((Tmp[1] & 0x03) << 4)];
		strEncode += "==";
	}
	else if (Mod == 2)
	{
		Tmp[1] = *Data++;
		Tmp[2] = *Data++;
		strEncode += EncodeTable[(Tmp[1] & 0xFC) >> 2];
		strEncode += EncodeTable[((Tmp[1] & 0x03) << 4) | ((Tmp[2] & 0xF0) >> 4)];
		strEncode += EncodeTable[((Tmp[2] & 0x0F) << 2)];
		strEncode += "=";
	}

	return strEncode;
}

string CDLBmpManager::ReadBmp(const char* pBmpName)
{
	string sBmp;

	FILE* pFile = NULL;
	int iSize = 0;
	do 
	{
		pFile = fopen(pBmpName, "r");
		if (pFile)
		{
			fseek(pFile, 0L, SEEK_END);
			iSize = ftell(pFile);
			fclose(pFile);
		}
		else
		{
			break;
		}

		std::ifstream fBmp(pBmpName, std::ios::binary);
		char* pBuff = new char[iSize];
		fBmp.read(pBuff, sizeof(char) * iSize);
		fBmp.close();

		string sBmp = Encode(pBuff, iSize);

	} while (0);
 
	return sBmp;
}