os/graphics/graphicstools/gdi_tools/bmconv/BMTOPBM.CPP
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/graphics/graphicstools/gdi_tools/bmconv/BMTOPBM.CPP	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,442 @@
     1.4 +// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include "BMCONV.H"
    1.20 +
    1.21 +BitmapLoader::BitmapLoader():
    1.22 +	iNumBmpColors(0),
    1.23 +	iBmpColors(NULL),
    1.24 +	iBmpBits(NULL),
    1.25 +	iAlphaBits(NULL)
    1.26 +	{}
    1.27 +
    1.28 +BitmapLoader::~BitmapLoader()
    1.29 +	{
    1.30 +	delete [] iBmpColors;
    1.31 +	delete [] iBmpBits;
    1.32 +	delete [] iAlphaBits;
    1.33 +	}
    1.34 +
    1.35 +int BitmapLoader::LoadBitmap(char* aFileName,int aBpp,TBitmapColor aColor,SEpocBitmapHeader*& aPbm)
    1.36 +	{
    1.37 +	char sig[2];
    1.38 +
    1.39 +#if defined(__MSVCDOTNET__) || defined(__LINUX__) || defined(__TOOLS2__)
    1.40 +	fstream file(aFileName, ios::in | ios::binary);
    1.41 +#else //!__MSVCDOTNET__
    1.42 +	fstream file(aFileName, ios::in | ios::binary | ios::nocreate);
    1.43 +#endif //__MSVCDOTNET__
    1.44 +
    1.45 +	if (file.is_open()==0)
    1.46 +		return Files;
    1.47 +	file.read(sig,2);
    1.48 +	file.close();
    1.49 +	if (file.gcount()!=2)
    1.50 +		return SourceFile ;
    1.51 +	if (sig[0]!='B'||sig[1]!='M')
    1.52 +		return SourceFile;
    1.53 +
    1.54 +	int ret = DoLoad(aFileName);
    1.55 +	if (!ret && aColor==EColorBitmapAlpha)
    1.56 +		{
    1.57 +		int fileNameLen = strlen(aFileName);
    1.58 +		char* alphaFileName = new char[fileNameLen + 7];// -alpha suffix is 6 chars, plus NUL termination
    1.59 +		if (alphaFileName == NULL)
    1.60 +			return NoMemory;
    1.61 +		int dotPos = -1;
    1.62 +		for (int i = 0; i < fileNameLen; ++i)
    1.63 +			if (aFileName[i]=='.')
    1.64 +				dotPos=i;
    1.65 +		int prefixLen = (dotPos>=0?dotPos:fileNameLen);
    1.66 +		memcpy(alphaFileName,aFileName,prefixLen);
    1.67 +		const char* suffix = "-alpha";
    1.68 +		memcpy(alphaFileName+prefixLen,suffix,6);
    1.69 +		if (dotPos>=0)
    1.70 +			memcpy(alphaFileName+prefixLen+6,aFileName+dotPos,fileNameLen-dotPos);
    1.71 +		*(alphaFileName + fileNameLen + 6) = '\0';
    1.72 +		ret = DoLoadAlpha(alphaFileName); // overlay alpha data from separate file
    1.73 +		delete [] alphaFileName;
    1.74 +		}
    1.75 +	if (!ret)
    1.76 +		ret = DoConvert(aBpp,aColor,aPbm);
    1.77 +	return ret;
    1.78 +	}
    1.79 +
    1.80 +int BitmapLoader::DoLoad(char* aFileName)
    1.81 +	{
    1.82 +#if defined(__MSVCDOTNET__) || defined(__TOOLS2__)
    1.83 +	fstream file(aFileName, ios::in | ios::binary);
    1.84 +#else //!__MSVCDOTNET__
    1.85 +	fstream file(aFileName, ios::in | ios::binary | ios::nocreate);
    1.86 +#endif //__MSVCDOTNET__
    1.87 +	if (file.is_open()==0)
    1.88 +		return Files;
    1.89 +	TBitmapFileHeader bmpfile;
    1.90 +	long size=sizeof(TBitmapFileHeader);
    1.91 +	file.read((char *)&bmpfile,size);
    1.92 +	if (file.gcount()!=size)
    1.93 +		return SourceFile;
    1.94 +	size=sizeof(TBitmapInfoHeader);
    1.95 +	file.read((char *)&iBmpHeader,size);
    1.96 +	if (file.gcount()!=size)
    1.97 +		return SourceFile;
    1.98 +	if (iBmpHeader.biCompression != 0)
    1.99 +		return UnknownCompression;
   1.100 +	size=bmpfile.bfSize-sizeof(TBitmapInfoHeader)-sizeof(TBitmapFileHeader);
   1.101 +	long bitcount=iBmpHeader.biBitCount;
   1.102 +	long colors=iBmpHeader.biClrUsed;
   1.103 +	if (colors==0)
   1.104 +		{
   1.105 +		if (bitcount==24)
   1.106 +			iNumBmpColors=0;
   1.107 +		else if (bitcount==32)
   1.108 +			iNumBmpColors=0;//See MSDN - BITMAPFILEHEADER and BITMAPINFOHEADER structures.
   1.109 +                            //If biCompression is 0 - we don't have TRgbQuad array! 
   1.110 +		else
   1.111 +			iNumBmpColors=1<<bitcount;
   1.112 +		}
   1.113 +	else
   1.114 +		iNumBmpColors=colors;
   1.115 +	if (iNumBmpColors > 256)
   1.116 +		return SourceFile;
   1.117 +	if (iNumBmpColors>0)
   1.118 +		{
   1.119 +		iBmpColors = new TRgbQuad[iNumBmpColors];
   1.120 +		if (iBmpColors == NULL)
   1.121 +			return NoMemory;
   1.122 +		memset(iBmpColors,0,iNumBmpColors*sizeof(TRgbQuad));
   1.123 +		}
   1.124 +	size-=iNumBmpColors*sizeof(TRgbQuad);
   1.125 +	iBmpBits = new char[size];
   1.126 +	if (iBmpBits == NULL)
   1.127 +		return NoMemory;
   1.128 +	memset(iBmpBits,0xff,size);
   1.129 +	// DEF102183: Graphics tools fail to run after building with MS VC8.
   1.130 +	if(iBmpColors != NULL)
   1.131 +	    {
   1.132 +		file.read((char *)iBmpColors,iNumBmpColors*sizeof(TRgbQuad));
   1.133 +		if (file.gcount()!=(int)(iNumBmpColors*sizeof(TRgbQuad)))
   1.134 +			return SourceFile;
   1.135 +	    }
   1.136 +	file.read(iBmpBits,size);
   1.137 +	file.close();
   1.138 +	if (file.gcount()!=size)
   1.139 +		return SourceFile;
   1.140 +	return NoError;
   1.141 +	}
   1.142 +
   1.143 +int BitmapLoader::DoLoadAlpha(char* aAlphaFileName)
   1.144 +	{
   1.145 +#if defined(__MSVCDOTNET__) || defined(__TOOLS2__)
   1.146 +	fstream file(aAlphaFileName, ios::in | ios::binary);
   1.147 +#else //!__MSVCDOTNET__
   1.148 +	fstream file(aAlphaFileName, ios::in | ios::binary | ios::nocreate);
   1.149 +#endif //__MSVCDOTNET__
   1.150 +	if (file.is_open()==0)
   1.151 +		return AlphaFiles;
   1.152 +	TBitmapFileHeader alphaBmpfile;
   1.153 +	long size=sizeof(TBitmapFileHeader);
   1.154 +	file.read((char *)&alphaBmpfile,size);
   1.155 +	if (file.gcount()!=size)
   1.156 +		return SourceFile;
   1.157 +	size=sizeof(TBitmapInfoHeader);
   1.158 +	TBitmapInfoHeader alphaBmpInfo;
   1.159 +	file.read((char *)&alphaBmpInfo,size);
   1.160 +	if (file.gcount()!=size)
   1.161 +		return SourceFile;
   1.162 +	if (alphaBmpInfo.biCompression != 0)
   1.163 +		return UnknownCompression;
   1.164 +	if (alphaBmpInfo.biWidth != iBmpHeader.biWidth || alphaBmpInfo.biHeight != iBmpHeader.biHeight)
   1.165 +		return AlphaDimensions;
   1.166 +	if (alphaBmpInfo.biBitCount != 8)
   1.167 +		return AlphaBpp;
   1.168 +	size=alphaBmpfile.bfSize-sizeof(TBitmapInfoHeader)-sizeof(TBitmapFileHeader);
   1.169 +	long numBmpColors=alphaBmpInfo.biClrUsed;
   1.170 +	if (numBmpColors == 0)
   1.171 +		numBmpColors = 256;
   1.172 +	if (numBmpColors != 256)
   1.173 +		return SourceFile;
   1.174 +	size-=numBmpColors*sizeof(TRgbQuad);
   1.175 +	iAlphaBits = new char[size];
   1.176 +	if (iAlphaBits == NULL)
   1.177 +		{
   1.178 +		return NoMemory;
   1.179 +		}
   1.180 +	memset(iAlphaBits,0xff,size);
   1.181 +	char* bmpColors = new char[numBmpColors*sizeof(TRgbQuad)];
   1.182 +	file.read((char *)bmpColors,numBmpColors*sizeof(TRgbQuad));
   1.183 +	delete [] bmpColors; // we aren't interested in the palette data for the 8bpp grayscale alpha bmp
   1.184 +	if (file.gcount()!=(int)(numBmpColors*sizeof(TRgbQuad)))
   1.185 +		return SourceFile;
   1.186 +	file.read(iAlphaBits,size);
   1.187 +	file.close();
   1.188 +	if (file.gcount()!=size)
   1.189 +		return SourceFile;
   1.190 +	return NoError;
   1.191 +	}
   1.192 +
   1.193 +TRgb BitmapLoader::GetBmpPixel(long aXCoord,long aYCoord)
   1.194 +	{
   1.195 +	TRgb darkgray(128,128,128);
   1.196 +	TRgb darkgrayex(127,127,127);
   1.197 +	TRgb lightgray(192,192,192);
   1.198 +	TRgb lightgrayex(187,187,187);
   1.199 +	unsigned char col;
   1.200 +	TRgb color;
   1.201 +
   1.202 +	switch(iBmpHeader.biBitCount)
   1.203 +		{
   1.204 +		case 1:
   1.205 +			col=iBmpBits[(iBmpHeader.biHeight-aYCoord-1)*(((iBmpHeader.biWidth+31)>>5)<<2)+(aXCoord>>3)];
   1.206 +			col&=(0x80>>(aXCoord%8));
   1.207 +			if (iBmpColors)
   1.208 +				{
   1.209 +				TRgbQuad rgbq;
   1.210 +				if (col)
   1.211 +					rgbq = iBmpColors[1];
   1.212 +				else
   1.213 +					rgbq = iBmpColors[0];
   1.214 +				color = TRgb(rgbq.iRed,rgbq.iGreen,rgbq.iBlue);
   1.215 +				}
   1.216 +			else
   1.217 +				{
   1.218 +				if (col)
   1.219 +					color = TRgb(0x00ffffff);
   1.220 +				else
   1.221 +					color = TRgb(0);
   1.222 +				}
   1.223 +			break;
   1.224 +		case 4:
   1.225 +			col=iBmpBits[(iBmpHeader.biHeight-aYCoord-1)*(((iBmpHeader.biWidth+7)>>3)<<2)+(aXCoord>>1)];
   1.226 +			if (aXCoord%2==0)
   1.227 +				col=(unsigned char)(col>>4);
   1.228 +			col&=0x0f;
   1.229 +			if (iBmpColors)
   1.230 +				{
   1.231 +				TRgbQuad rgbq = iBmpColors[col];
   1.232 +				color = TRgb(rgbq.iRed,rgbq.iGreen,rgbq.iBlue);
   1.233 +				}
   1.234 +			else
   1.235 +				{
   1.236 +				col *= 17;
   1.237 +				color = TRgb(col,col,col);
   1.238 +				}
   1.239 +			break;
   1.240 +		case 8:
   1.241 +			col=iBmpBits[(iBmpHeader.biHeight-aYCoord-1)*((iBmpHeader.biWidth+3)&~3)+aXCoord];
   1.242 +			if (iBmpColors)
   1.243 +				{
   1.244 +				TRgbQuad rgbq = iBmpColors[col];
   1.245 +				color = TRgb(rgbq.iRed,rgbq.iGreen,rgbq.iBlue);
   1.246 +				}
   1.247 +			else
   1.248 +				color = TRgb(col,col,col);
   1.249 +			break;
   1.250 +		case 16:
   1.251 +			{
   1.252 +			unsigned short int* wordptr = (unsigned short int*)&iBmpBits[(iBmpHeader.biHeight-aYCoord-1)*(((iBmpHeader.biWidth+1)&~1)<<1)+(aXCoord<<1)];
   1.253 +			color = TRgb((*wordptr&0x7c)>>10,(*wordptr&0x3e)>>5,(*wordptr&0x1f));
   1.254 +			}
   1.255 +			break;
   1.256 +		case 24:
   1.257 +			{
   1.258 +			TRgbTriple rgbcol = *((TRgbTriple *)&(iBmpBits[(iBmpHeader.biHeight-aYCoord-1)*((3*iBmpHeader.biWidth+3)&~3)+aXCoord+(aXCoord<<1)]));
   1.259 +			color = TRgb(rgbcol.rgbtRed,rgbcol.rgbtGreen,rgbcol.rgbtBlue);
   1.260 +			}
   1.261 +			break;
   1.262 +		case 32:
   1.263 +			{
   1.264 +			unsigned long int* dwordptr = (unsigned long int*)&iBmpBits[(iBmpHeader.biHeight-aYCoord-1)*((iBmpHeader.biWidth)<<2)+(aXCoord<<2)];
   1.265 +			color = TRgb((*dwordptr&0xff0000)>>16,(*dwordptr&0xff00)>>8,*dwordptr&0xff);
   1.266 +			}
   1.267 +			break;
   1.268 +		default:
   1.269 +			break;
   1.270 +		}
   1.271 +	if (color == darkgray)
   1.272 +		color = darkgrayex;
   1.273 +	else if (color == lightgray)
   1.274 +		color = lightgrayex;
   1.275 +	return color;
   1.276 +	}
   1.277 +
   1.278 +unsigned char BitmapLoader::GetAlphaPixel(long aXCoord,long aYCoord)
   1.279 +	{
   1.280 +	return iAlphaBits[(iBmpHeader.biHeight-aYCoord-1)*((iBmpHeader.biWidth+3)&~3)+aXCoord];
   1.281 +	}
   1.282 +
   1.283 +int BitmapLoader::DoConvert(int aBpp,TBitmapColor aColor,SEpocBitmapHeader*& aPbm)
   1.284 +	{
   1.285 +	bool useAlpha = (aColor==EColorBitmapAlpha || aColor==EColorBitmapAlphaPM);
   1.286 +	long desttwipswidth = 0;
   1.287 +	long desttwipsheight = 0;
   1.288 +
   1.289 +	long bytewidth = BitmapUtils::ByteWidth(iBmpHeader.biWidth,aBpp);
   1.290 +	long destlength = iBmpHeader.biHeight * bytewidth;
   1.291 +
   1.292 +	if (iBmpHeader.biXPelsPerMeter>0)
   1.293 +		desttwipswidth = iBmpHeader.biWidth*1440000/254/iBmpHeader.biXPelsPerMeter;
   1.294 +	if (iBmpHeader.biYPelsPerMeter>0)
   1.295 +		desttwipsheight = iBmpHeader.biHeight*1440000/254/iBmpHeader.biYPelsPerMeter;
   1.296 +
   1.297 +	aPbm = (SEpocBitmapHeader*)new char[sizeof(SEpocBitmapHeader) + destlength];
   1.298 +	if (aPbm == NULL)
   1.299 +		return NoMemory;
   1.300 +	memset(aPbm,0,sizeof(SEpocBitmapHeader));
   1.301 +
   1.302 +	// aBitmap->iByteWidth = bytewidth;
   1.303 +	// aBitmap->iDataOffset = sizeof(Bitmap);
   1.304 +
   1.305 +	aPbm->iBitmapSize = sizeof(SEpocBitmapHeader) + destlength;
   1.306 +	aPbm->iStructSize = sizeof(SEpocBitmapHeader);
   1.307 +	aPbm->iWidthInPixels = iBmpHeader.biWidth;
   1.308 +	aPbm->iHeightInPixels = iBmpHeader.biHeight;
   1.309 +	aPbm->iWidthInTwips = desttwipswidth;
   1.310 +	aPbm->iHeightInTwips = desttwipsheight;
   1.311 +	aPbm->iBitsPerPixel = aBpp;
   1.312 +	aPbm->iColor = aColor;
   1.313 +	aPbm->iPaletteEntries = 0;
   1.314 +	aPbm->iCompression = ENoBitmapCompression;
   1.315 +
   1.316 +	char* pbmBits = ((char*)aPbm) + sizeof(SEpocBitmapHeader);
   1.317 +	memset(pbmBits,0xff,destlength);
   1.318 +
   1.319 +	long col = 0;
   1.320 +	char* pixadd = 0;
   1.321 +
   1.322 +	switch(aBpp)
   1.323 +		{
   1.324 +	case 1:
   1.325 +		{
   1.326 +		for(long ycrd=0;ycrd<iBmpHeader.biHeight;ycrd++)
   1.327 +			for(long xcrd=0;xcrd<iBmpHeader.biWidth;xcrd++)
   1.328 +				{
   1.329 +				TRgb color=GetBmpPixel(xcrd,ycrd);
   1.330 +				col=color.Gray2();
   1.331 +				pixadd=&(pbmBits[ycrd*bytewidth+(xcrd>>3)]);
   1.332 +				(*pixadd)&=~(1<<((xcrd&7)));
   1.333 +				(*pixadd)|=(unsigned char)(col<<(xcrd&7));
   1.334 +				}
   1.335 +		}
   1.336 +		break;
   1.337 +	case 2:
   1.338 +		{
   1.339 +		for(long ycrd=0;ycrd<iBmpHeader.biHeight;ycrd++)
   1.340 +			for(long xcrd=0;xcrd<iBmpHeader.biWidth;xcrd++)
   1.341 +				{
   1.342 +				TRgb color=GetBmpPixel(xcrd,ycrd);
   1.343 +				col=color.Gray4();
   1.344 +				pixadd=&(pbmBits[ycrd*bytewidth+(xcrd>>2)]);
   1.345 +				(*pixadd)&=~(0x3<<(2*(xcrd%4)));
   1.346 +				(*pixadd)|=(unsigned char)(col<<(2*(xcrd%4)));
   1.347 +				}
   1.348 +		}
   1.349 +		break;
   1.350 +	case 4:
   1.351 +		{
   1.352 +		for(long ycrd=0;ycrd<iBmpHeader.biHeight;ycrd++)
   1.353 +			for(long xcrd=0;xcrd<iBmpHeader.biWidth;xcrd++)
   1.354 +				{
   1.355 +				TRgb color=GetBmpPixel(xcrd,ycrd);
   1.356 +				if (aColor == EMonochromeBitmap)
   1.357 +					col = color.Gray16();
   1.358 +				else
   1.359 +					col = color.Color16();
   1.360 +				pixadd=&(pbmBits[ycrd*bytewidth+(xcrd>>1)]);
   1.361 +				if (xcrd%2!=0)
   1.362 +					*pixadd=(unsigned char)((unsigned char)((col<<4)|(*pixadd&0x0f)));
   1.363 +				else
   1.364 +					*pixadd=(unsigned char)((unsigned char)(col|(*pixadd&0xf0)));
   1.365 +				}
   1.366 +		}
   1.367 +		break;
   1.368 +	case 8:
   1.369 +		{
   1.370 +		for(long ycrd=0;ycrd<iBmpHeader.biHeight;ycrd++)
   1.371 +			for(long xcrd=0;xcrd<iBmpHeader.biWidth;xcrd++)
   1.372 +				{
   1.373 +				TRgb color=GetBmpPixel(xcrd,ycrd);
   1.374 +				if (aColor == EMonochromeBitmap)
   1.375 +					col = color.Gray256();
   1.376 +				else
   1.377 +					col = color.Color256();
   1.378 +				pixadd=&(pbmBits[ycrd*((iBmpHeader.biWidth+3)&~3)+xcrd]);
   1.379 +				*pixadd=(unsigned char)col;
   1.380 +				}
   1.381 +		}
   1.382 +		break;
   1.383 +	case 12:
   1.384 +		{
   1.385 +		for(long ycrd=0;ycrd<iBmpHeader.biHeight;ycrd++)
   1.386 +			for(long xcrd=0;xcrd<iBmpHeader.biWidth;xcrd++)
   1.387 +				{
   1.388 +				TRgb color=GetBmpPixel(xcrd,ycrd);
   1.389 +				col=color.Color4K();
   1.390 +				pixadd=&(pbmBits[ycrd*bytewidth+(xcrd<<1)]);
   1.391 +				unsigned short* wordadd=(unsigned short*)pixadd;
   1.392 +				*wordadd=(unsigned short)col;
   1.393 +				}
   1.394 +		}
   1.395 +		break;
   1.396 +	case 16:
   1.397 +		{
   1.398 +		for(long ycrd=0;ycrd<iBmpHeader.biHeight;ycrd++)
   1.399 +			for(long xcrd=0;xcrd<iBmpHeader.biWidth;xcrd++)
   1.400 +				{
   1.401 +				TRgb color=GetBmpPixel(xcrd,ycrd);
   1.402 +				col=color.Color64K();
   1.403 +				pixadd=&(pbmBits[ycrd*bytewidth+(xcrd<<1)]);
   1.404 +				unsigned short* wordadd=(unsigned short*)pixadd;
   1.405 +				*wordadd=(unsigned short)col;
   1.406 +				}
   1.407 +		}
   1.408 +		break;
   1.409 +	case 24:
   1.410 +		{
   1.411 +		for(long ycrd=0;ycrd<iBmpHeader.biHeight;ycrd++)
   1.412 +			{
   1.413 +			unsigned char* bytePtr = (unsigned char*)&pbmBits[ycrd*bytewidth];
   1.414 +			for(long xcrd=0;xcrd<iBmpHeader.biWidth;xcrd++)
   1.415 +				{
   1.416 +				TRgb col = GetBmpPixel(xcrd,ycrd);
   1.417 +				*bytePtr++ = (unsigned char) col.iBlue;
   1.418 +				*bytePtr++ = (unsigned char) col.iGreen;
   1.419 +				*bytePtr++ = (unsigned char) col.iRed;
   1.420 +				}
   1.421 +			}
   1.422 +		}
   1.423 +		break;
   1.424 +	case 32:
   1.425 +		{
   1.426 +		for(long ycrd=0;ycrd<iBmpHeader.biHeight;ycrd++)
   1.427 +			{
   1.428 +			unsigned char* bytePtr = (unsigned char*)&pbmBits[ycrd*bytewidth];
   1.429 +			for(long xcrd=0;xcrd<iBmpHeader.biWidth;xcrd++)
   1.430 +				{
   1.431 +				TRgb col = GetBmpPixel(xcrd,ycrd);
   1.432 +				unsigned char alpha = useAlpha?GetAlphaPixel(xcrd,ycrd):(unsigned char)0;
   1.433 +				*bytePtr++ = (unsigned char) col.iBlue;
   1.434 +				*bytePtr++ = (unsigned char) col.iGreen;
   1.435 +				*bytePtr++ = (unsigned char) col.iRed;
   1.436 +				*bytePtr++ = alpha;	
   1.437 +				}
   1.438 +			}
   1.439 +		}
   1.440 +		break;
   1.441 +		};
   1.442 +
   1.443 +	return NoError;
   1.444 +	}
   1.445 +