2008-06-12 09:45:51 +03:00
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "SDL.h"
|
|
|
|
#include "SDL_image.h"
|
|
|
|
#include "CBitmapHandler.h"
|
|
|
|
#include "../hch/CDefHandler.h"
|
|
|
|
#include "../hch/CLodHandler.h"
|
|
|
|
#include <sstream>
|
2008-06-16 17:56:48 +03:00
|
|
|
#include <boost/thread.hpp>
|
2009-04-15 17:03:31 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* CBitmapHandler.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-05-26 07:52:34 +03:00
|
|
|
boost::mutex bitmap_handler_mx;
|
2009-05-09 19:18:27 +03:00
|
|
|
|
|
|
|
extern DLL_EXPORT CLodHandler *bitmaph;
|
|
|
|
|
2008-06-12 09:45:51 +03:00
|
|
|
void CPCXConv::openPCX(char * PCX, int len)
|
|
|
|
{
|
|
|
|
pcxs=len;
|
|
|
|
pcx=(unsigned char*)PCX;
|
|
|
|
}
|
|
|
|
void CPCXConv::fromFile(std::string path)
|
|
|
|
{
|
2008-09-26 17:16:01 +03:00
|
|
|
std::ifstream is;
|
|
|
|
is.open(path.c_str(),std::ios::binary);
|
|
|
|
is.seekg(0,std::ios::end); // to the end
|
|
|
|
pcxs = is.tellg(); // read length
|
|
|
|
is.seekg(0,std::ios::beg); // wracamy na poczatek
|
2008-06-12 09:45:51 +03:00
|
|
|
pcx = new unsigned char[pcxs]; // allocate memory
|
2008-09-26 17:16:01 +03:00
|
|
|
is.read((char*)pcx, pcxs); // read map file to buffer
|
|
|
|
is.close();
|
2008-06-12 09:45:51 +03:00
|
|
|
}
|
2009-09-27 14:37:15 +03:00
|
|
|
|
2009-10-25 16:36:11 +02:00
|
|
|
void CPCXConv::saveBMP(std::string path) const
|
2008-06-12 09:45:51 +03:00
|
|
|
{
|
|
|
|
std::ofstream os;
|
|
|
|
os.open(path.c_str(), std::ios::binary);
|
2009-10-25 16:36:11 +02:00
|
|
|
os.write(reinterpret_cast<const char*>(bmp), bmps);
|
2008-06-12 09:45:51 +03:00
|
|
|
os.close();
|
|
|
|
}
|
|
|
|
|
2009-09-27 14:37:15 +03:00
|
|
|
SDL_Surface * CPCXConv::getSurface() const
|
2008-06-12 09:45:51 +03:00
|
|
|
{
|
|
|
|
SDL_Surface * ret;
|
|
|
|
|
2009-02-14 16:37:13 +02:00
|
|
|
int width = -1, height = -1;
|
2008-06-12 09:45:51 +03:00
|
|
|
Epcxformat format;
|
2008-12-21 21:17:35 +02:00
|
|
|
int fSize,y;//,i; //TODO use me 'i'
|
2008-06-12 09:45:51 +03:00
|
|
|
bool check1, check2;
|
|
|
|
unsigned char add;
|
|
|
|
int it=0;
|
|
|
|
|
2010-02-09 15:48:14 +02:00
|
|
|
fSize = readNormalNr(pcx, it); it+=4;
|
|
|
|
width = readNormalNr(pcx, it); it+=4;
|
|
|
|
height = readNormalNr(pcx, it); it+=4;
|
2009-02-14 16:37:13 +02:00
|
|
|
if (fSize==width*height*3)
|
2008-06-12 09:45:51 +03:00
|
|
|
check1=true;
|
|
|
|
else
|
|
|
|
check1=false;
|
2009-02-14 16:37:13 +02:00
|
|
|
if (fSize==width*height)
|
2008-06-12 09:45:51 +03:00
|
|
|
check2=true;
|
|
|
|
else
|
|
|
|
check2=false;
|
|
|
|
if (check1)
|
|
|
|
format=PCX24B;
|
|
|
|
else if (check2)
|
|
|
|
format=PCX8B;
|
|
|
|
else
|
|
|
|
return NULL;
|
2009-02-14 16:37:13 +02:00
|
|
|
add = 4 - width%4;
|
2008-06-12 09:45:51 +03:00
|
|
|
if (add==4)
|
|
|
|
add=0;
|
|
|
|
if (format==PCX8B)
|
|
|
|
{
|
2009-02-14 16:37:13 +02:00
|
|
|
ret = SDL_CreateRGBSurface(SDL_SWSURFACE, width+add, height, 8, 0, 0, 0, 0);
|
2008-06-12 09:45:51 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-08-02 18:08:03 +03:00
|
|
|
#if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
|
|
|
|
int bmask = 0xff0000;
|
|
|
|
int gmask = 0x00ff00;
|
|
|
|
int rmask = 0x0000ff;
|
|
|
|
#else
|
2008-06-12 09:45:51 +03:00
|
|
|
int bmask = 0x0000ff;
|
|
|
|
int gmask = 0x00ff00;
|
|
|
|
int rmask = 0xff0000;
|
2008-08-02 18:08:03 +03:00
|
|
|
#endif
|
2009-02-14 16:37:13 +02:00
|
|
|
ret = SDL_CreateRGBSurface(SDL_SWSURFACE, width+add, height, 24, rmask, gmask, bmask, 0);
|
2008-06-12 09:45:51 +03:00
|
|
|
}
|
|
|
|
if (format==PCX8B)
|
|
|
|
{
|
|
|
|
it = pcxs-256*3;
|
|
|
|
for (int i=0;i<256;i++)
|
|
|
|
{
|
|
|
|
SDL_Color tp;
|
2008-08-02 18:08:03 +03:00
|
|
|
#if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
|
|
|
|
tp.b = pcx[it++];
|
|
|
|
tp.g = pcx[it++];
|
|
|
|
tp.r = pcx[it++];
|
|
|
|
#else
|
2008-06-12 09:45:51 +03:00
|
|
|
tp.r = pcx[it++];
|
|
|
|
tp.g = pcx[it++];
|
|
|
|
tp.b = pcx[it++];
|
2008-08-02 18:08:03 +03:00
|
|
|
#endif
|
2008-06-12 09:45:51 +03:00
|
|
|
tp.unused = 0;
|
|
|
|
*(ret->format->palette->colors+i) = tp;
|
|
|
|
}
|
2009-10-25 16:36:11 +02:00
|
|
|
for (y=height; y>0; --y)
|
2008-06-12 09:45:51 +03:00
|
|
|
{
|
2009-10-25 16:36:11 +02:00
|
|
|
it = 0xC + (y-1)*width;
|
|
|
|
memcpy((char*)ret->pixels + ret->pitch * (y-1), pcx + it, width);
|
|
|
|
|
2008-06-12 09:45:51 +03:00
|
|
|
if (add>0)
|
|
|
|
{
|
2009-10-25 16:36:11 +02:00
|
|
|
memset((char*)ret->pixels + ret->pitch * (y-1) + width, 0, add);
|
2008-06-12 09:45:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-02-14 16:37:13 +02:00
|
|
|
for (y=height; y>0; y--)
|
2008-06-12 09:45:51 +03:00
|
|
|
{
|
2009-10-25 16:36:11 +02:00
|
|
|
it = 0xC + (y-1)*width*3;
|
|
|
|
|
|
|
|
memcpy((char*)ret->pixels + ret->pitch * (y-1), pcx + it, width*3);
|
|
|
|
|
2008-06-12 09:45:51 +03:00
|
|
|
if (add>0)
|
|
|
|
{
|
2009-10-25 16:36:11 +02:00
|
|
|
memset((char*)ret->pixels + ret->pitch * (y-1) + width*3, 0, add*3);
|
2008-06-12 09:45:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-08-27 19:20:35 +03:00
|
|
|
bool isPCX(const unsigned char *header)//check whether file can be PCX according to 1st 12 bytes
|
|
|
|
{
|
|
|
|
int fSize = readNormalNr(header, 0);
|
|
|
|
int width = readNormalNr(header, 4);
|
|
|
|
int height = readNormalNr(header, 8);
|
|
|
|
return fSize == width*height || fSize == width*height*3;
|
|
|
|
}
|
|
|
|
|
2008-06-16 17:56:48 +03:00
|
|
|
SDL_Surface * BitmapHandler::loadBitmap(std::string fname, bool setKey)
|
2008-06-12 09:45:51 +03:00
|
|
|
{
|
|
|
|
if(!fname.size())
|
2009-02-14 16:37:13 +02:00
|
|
|
{
|
|
|
|
tlog2 << "Call to loadBitmap with void fname!\n";
|
2008-06-12 09:45:51 +03:00
|
|
|
return NULL;
|
2009-02-14 16:37:13 +02:00
|
|
|
}
|
2008-06-12 09:45:51 +03:00
|
|
|
unsigned char * pcx;
|
|
|
|
std::transform(fname.begin(),fname.end(),fname.begin(),toupper);
|
2010-08-28 18:10:37 +03:00
|
|
|
int dotPos = fname.find_last_of('.');
|
|
|
|
if ( dotPos != -1 )
|
|
|
|
fname.erase(dotPos);
|
|
|
|
|
2008-06-12 09:45:51 +03:00
|
|
|
Entry *e = bitmaph->entries.znajdz(fname);
|
|
|
|
if(!e)
|
|
|
|
{
|
2010-08-27 19:20:35 +03:00
|
|
|
tlog2<<"Entry for file "<<fname<<" was not found"<<std::endl;
|
2008-06-12 09:45:51 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-08-27 19:20:35 +03:00
|
|
|
if(e->offset<0)//not in LOD
|
2010-01-28 15:56:48 +02:00
|
|
|
{
|
2009-12-29 15:40:16 +02:00
|
|
|
fname = e->realName;
|
2009-10-04 05:02:45 +03:00
|
|
|
fname = DATA_DIR "/Data/" + fname;
|
2008-06-12 09:45:51 +03:00
|
|
|
FILE * f = fopen(fname.c_str(),"r");
|
2010-08-27 19:20:35 +03:00
|
|
|
unsigned char sign[12];
|
2009-12-29 15:40:16 +02:00
|
|
|
if(!f)
|
|
|
|
{
|
2010-08-27 19:20:35 +03:00
|
|
|
tlog1 << "Cannot open " << fname << " - file not found!\n";
|
2009-12-29 15:40:16 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-08-27 19:20:35 +03:00
|
|
|
fread(sign,1,12,f);
|
|
|
|
SDL_Surface * ret=NULL;
|
|
|
|
if (isPCX(sign))//H3-style PCX
|
2008-06-12 09:45:51 +03:00
|
|
|
{
|
2010-08-27 19:20:35 +03:00
|
|
|
CPCXConv cp;
|
|
|
|
pcx = new unsigned char[e->realSize];
|
|
|
|
memcpy(pcx,sign,3);
|
|
|
|
int res = fread((char*)pcx+3, 1, e->realSize-3, f); //TODO use me
|
2008-06-12 09:45:51 +03:00
|
|
|
fclose(f);
|
2010-08-27 19:20:35 +03:00
|
|
|
cp.openPCX((char*)pcx,e->realSize);
|
|
|
|
ret = cp.getSurface();
|
|
|
|
if (!ret)
|
|
|
|
tlog1<<"Failed to open "<<fname<<" as H3 PCX!\n";
|
2008-06-12 09:45:51 +03:00
|
|
|
}
|
2010-08-27 19:20:35 +03:00
|
|
|
else //try loading via SDL_Image
|
2008-06-12 09:45:51 +03:00
|
|
|
{
|
2010-08-27 19:20:35 +03:00
|
|
|
fclose(f);
|
|
|
|
ret = IMG_Load(fname.c_str());
|
|
|
|
if (!ret)
|
|
|
|
tlog1<<"Failed to open "<<fname<<" via SDL_Image\n";
|
2008-06-12 09:45:51 +03:00
|
|
|
}
|
2010-08-27 19:20:35 +03:00
|
|
|
return ret;
|
2008-06-12 09:45:51 +03:00
|
|
|
}
|
2010-08-27 19:20:35 +03:00
|
|
|
//loading from LOD
|
2009-10-11 02:46:42 +03:00
|
|
|
pcx = bitmaph->giveFile(e->nameStr, NULL);
|
|
|
|
|
2008-06-12 09:45:51 +03:00
|
|
|
CPCXConv cp;
|
2009-09-27 14:37:15 +03:00
|
|
|
cp.openPCX((char*)pcx, e->realSize);
|
2008-06-16 17:56:48 +03:00
|
|
|
SDL_Surface * ret = cp.getSurface();
|
2009-08-17 11:50:31 +03:00
|
|
|
if(ret->format->BytesPerPixel == 1 && setKey)
|
|
|
|
{
|
|
|
|
const SDL_Color &c = ret->format->palette->colors[0];
|
|
|
|
SDL_SetColorKey(ret,SDL_SRCCOLORKEY,SDL_MapRGB(ret->format, c.r, c.g, c.b));
|
|
|
|
}
|
2008-06-16 17:56:48 +03:00
|
|
|
return ret;
|
2009-10-04 05:02:45 +03:00
|
|
|
}
|