os/graphics/graphicstools/gdi_tools/bmconv/BMTOPBM.CPP
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #include "BMCONV.H"
    17 
    18 BitmapLoader::BitmapLoader():
    19 	iNumBmpColors(0),
    20 	iBmpColors(NULL),
    21 	iBmpBits(NULL),
    22 	iAlphaBits(NULL)
    23 	{}
    24 
    25 BitmapLoader::~BitmapLoader()
    26 	{
    27 	delete [] iBmpColors;
    28 	delete [] iBmpBits;
    29 	delete [] iAlphaBits;
    30 	}
    31 
    32 int BitmapLoader::LoadBitmap(char* aFileName,int aBpp,TBitmapColor aColor,SEpocBitmapHeader*& aPbm)
    33 	{
    34 	char sig[2];
    35 
    36 #if defined(__MSVCDOTNET__) || defined(__LINUX__) || defined(__TOOLS2__)
    37 	fstream file(aFileName, ios::in | ios::binary);
    38 #else //!__MSVCDOTNET__
    39 	fstream file(aFileName, ios::in | ios::binary | ios::nocreate);
    40 #endif //__MSVCDOTNET__
    41 
    42 	if (file.is_open()==0)
    43 		return Files;
    44 	file.read(sig,2);
    45 	file.close();
    46 	if (file.gcount()!=2)
    47 		return SourceFile ;
    48 	if (sig[0]!='B'||sig[1]!='M')
    49 		return SourceFile;
    50 
    51 	int ret = DoLoad(aFileName);
    52 	if (!ret && aColor==EColorBitmapAlpha)
    53 		{
    54 		int fileNameLen = strlen(aFileName);
    55 		char* alphaFileName = new char[fileNameLen + 7];// -alpha suffix is 6 chars, plus NUL termination
    56 		if (alphaFileName == NULL)
    57 			return NoMemory;
    58 		int dotPos = -1;
    59 		for (int i = 0; i < fileNameLen; ++i)
    60 			if (aFileName[i]=='.')
    61 				dotPos=i;
    62 		int prefixLen = (dotPos>=0?dotPos:fileNameLen);
    63 		memcpy(alphaFileName,aFileName,prefixLen);
    64 		const char* suffix = "-alpha";
    65 		memcpy(alphaFileName+prefixLen,suffix,6);
    66 		if (dotPos>=0)
    67 			memcpy(alphaFileName+prefixLen+6,aFileName+dotPos,fileNameLen-dotPos);
    68 		*(alphaFileName + fileNameLen + 6) = '\0';
    69 		ret = DoLoadAlpha(alphaFileName); // overlay alpha data from separate file
    70 		delete [] alphaFileName;
    71 		}
    72 	if (!ret)
    73 		ret = DoConvert(aBpp,aColor,aPbm);
    74 	return ret;
    75 	}
    76 
    77 int BitmapLoader::DoLoad(char* aFileName)
    78 	{
    79 #if defined(__MSVCDOTNET__) || defined(__TOOLS2__)
    80 	fstream file(aFileName, ios::in | ios::binary);
    81 #else //!__MSVCDOTNET__
    82 	fstream file(aFileName, ios::in | ios::binary | ios::nocreate);
    83 #endif //__MSVCDOTNET__
    84 	if (file.is_open()==0)
    85 		return Files;
    86 	TBitmapFileHeader bmpfile;
    87 	long size=sizeof(TBitmapFileHeader);
    88 	file.read((char *)&bmpfile,size);
    89 	if (file.gcount()!=size)
    90 		return SourceFile;
    91 	size=sizeof(TBitmapInfoHeader);
    92 	file.read((char *)&iBmpHeader,size);
    93 	if (file.gcount()!=size)
    94 		return SourceFile;
    95 	if (iBmpHeader.biCompression != 0)
    96 		return UnknownCompression;
    97 	size=bmpfile.bfSize-sizeof(TBitmapInfoHeader)-sizeof(TBitmapFileHeader);
    98 	long bitcount=iBmpHeader.biBitCount;
    99 	long colors=iBmpHeader.biClrUsed;
   100 	if (colors==0)
   101 		{
   102 		if (bitcount==24)
   103 			iNumBmpColors=0;
   104 		else if (bitcount==32)
   105 			iNumBmpColors=0;//See MSDN - BITMAPFILEHEADER and BITMAPINFOHEADER structures.
   106                             //If biCompression is 0 - we don't have TRgbQuad array! 
   107 		else
   108 			iNumBmpColors=1<<bitcount;
   109 		}
   110 	else
   111 		iNumBmpColors=colors;
   112 	if (iNumBmpColors > 256)
   113 		return SourceFile;
   114 	if (iNumBmpColors>0)
   115 		{
   116 		iBmpColors = new TRgbQuad[iNumBmpColors];
   117 		if (iBmpColors == NULL)
   118 			return NoMemory;
   119 		memset(iBmpColors,0,iNumBmpColors*sizeof(TRgbQuad));
   120 		}
   121 	size-=iNumBmpColors*sizeof(TRgbQuad);
   122 	iBmpBits = new char[size];
   123 	if (iBmpBits == NULL)
   124 		return NoMemory;
   125 	memset(iBmpBits,0xff,size);
   126 	// DEF102183: Graphics tools fail to run after building with MS VC8.
   127 	if(iBmpColors != NULL)
   128 	    {
   129 		file.read((char *)iBmpColors,iNumBmpColors*sizeof(TRgbQuad));
   130 		if (file.gcount()!=(int)(iNumBmpColors*sizeof(TRgbQuad)))
   131 			return SourceFile;
   132 	    }
   133 	file.read(iBmpBits,size);
   134 	file.close();
   135 	if (file.gcount()!=size)
   136 		return SourceFile;
   137 	return NoError;
   138 	}
   139 
   140 int BitmapLoader::DoLoadAlpha(char* aAlphaFileName)
   141 	{
   142 #if defined(__MSVCDOTNET__) || defined(__TOOLS2__)
   143 	fstream file(aAlphaFileName, ios::in | ios::binary);
   144 #else //!__MSVCDOTNET__
   145 	fstream file(aAlphaFileName, ios::in | ios::binary | ios::nocreate);
   146 #endif //__MSVCDOTNET__
   147 	if (file.is_open()==0)
   148 		return AlphaFiles;
   149 	TBitmapFileHeader alphaBmpfile;
   150 	long size=sizeof(TBitmapFileHeader);
   151 	file.read((char *)&alphaBmpfile,size);
   152 	if (file.gcount()!=size)
   153 		return SourceFile;
   154 	size=sizeof(TBitmapInfoHeader);
   155 	TBitmapInfoHeader alphaBmpInfo;
   156 	file.read((char *)&alphaBmpInfo,size);
   157 	if (file.gcount()!=size)
   158 		return SourceFile;
   159 	if (alphaBmpInfo.biCompression != 0)
   160 		return UnknownCompression;
   161 	if (alphaBmpInfo.biWidth != iBmpHeader.biWidth || alphaBmpInfo.biHeight != iBmpHeader.biHeight)
   162 		return AlphaDimensions;
   163 	if (alphaBmpInfo.biBitCount != 8)
   164 		return AlphaBpp;
   165 	size=alphaBmpfile.bfSize-sizeof(TBitmapInfoHeader)-sizeof(TBitmapFileHeader);
   166 	long numBmpColors=alphaBmpInfo.biClrUsed;
   167 	if (numBmpColors == 0)
   168 		numBmpColors = 256;
   169 	if (numBmpColors != 256)
   170 		return SourceFile;
   171 	size-=numBmpColors*sizeof(TRgbQuad);
   172 	iAlphaBits = new char[size];
   173 	if (iAlphaBits == NULL)
   174 		{
   175 		return NoMemory;
   176 		}
   177 	memset(iAlphaBits,0xff,size);
   178 	char* bmpColors = new char[numBmpColors*sizeof(TRgbQuad)];
   179 	file.read((char *)bmpColors,numBmpColors*sizeof(TRgbQuad));
   180 	delete [] bmpColors; // we aren't interested in the palette data for the 8bpp grayscale alpha bmp
   181 	if (file.gcount()!=(int)(numBmpColors*sizeof(TRgbQuad)))
   182 		return SourceFile;
   183 	file.read(iAlphaBits,size);
   184 	file.close();
   185 	if (file.gcount()!=size)
   186 		return SourceFile;
   187 	return NoError;
   188 	}
   189 
   190 TRgb BitmapLoader::GetBmpPixel(long aXCoord,long aYCoord)
   191 	{
   192 	TRgb darkgray(128,128,128);
   193 	TRgb darkgrayex(127,127,127);
   194 	TRgb lightgray(192,192,192);
   195 	TRgb lightgrayex(187,187,187);
   196 	unsigned char col;
   197 	TRgb color;
   198 
   199 	switch(iBmpHeader.biBitCount)
   200 		{
   201 		case 1:
   202 			col=iBmpBits[(iBmpHeader.biHeight-aYCoord-1)*(((iBmpHeader.biWidth+31)>>5)<<2)+(aXCoord>>3)];
   203 			col&=(0x80>>(aXCoord%8));
   204 			if (iBmpColors)
   205 				{
   206 				TRgbQuad rgbq;
   207 				if (col)
   208 					rgbq = iBmpColors[1];
   209 				else
   210 					rgbq = iBmpColors[0];
   211 				color = TRgb(rgbq.iRed,rgbq.iGreen,rgbq.iBlue);
   212 				}
   213 			else
   214 				{
   215 				if (col)
   216 					color = TRgb(0x00ffffff);
   217 				else
   218 					color = TRgb(0);
   219 				}
   220 			break;
   221 		case 4:
   222 			col=iBmpBits[(iBmpHeader.biHeight-aYCoord-1)*(((iBmpHeader.biWidth+7)>>3)<<2)+(aXCoord>>1)];
   223 			if (aXCoord%2==0)
   224 				col=(unsigned char)(col>>4);
   225 			col&=0x0f;
   226 			if (iBmpColors)
   227 				{
   228 				TRgbQuad rgbq = iBmpColors[col];
   229 				color = TRgb(rgbq.iRed,rgbq.iGreen,rgbq.iBlue);
   230 				}
   231 			else
   232 				{
   233 				col *= 17;
   234 				color = TRgb(col,col,col);
   235 				}
   236 			break;
   237 		case 8:
   238 			col=iBmpBits[(iBmpHeader.biHeight-aYCoord-1)*((iBmpHeader.biWidth+3)&~3)+aXCoord];
   239 			if (iBmpColors)
   240 				{
   241 				TRgbQuad rgbq = iBmpColors[col];
   242 				color = TRgb(rgbq.iRed,rgbq.iGreen,rgbq.iBlue);
   243 				}
   244 			else
   245 				color = TRgb(col,col,col);
   246 			break;
   247 		case 16:
   248 			{
   249 			unsigned short int* wordptr = (unsigned short int*)&iBmpBits[(iBmpHeader.biHeight-aYCoord-1)*(((iBmpHeader.biWidth+1)&~1)<<1)+(aXCoord<<1)];
   250 			color = TRgb((*wordptr&0x7c)>>10,(*wordptr&0x3e)>>5,(*wordptr&0x1f));
   251 			}
   252 			break;
   253 		case 24:
   254 			{
   255 			TRgbTriple rgbcol = *((TRgbTriple *)&(iBmpBits[(iBmpHeader.biHeight-aYCoord-1)*((3*iBmpHeader.biWidth+3)&~3)+aXCoord+(aXCoord<<1)]));
   256 			color = TRgb(rgbcol.rgbtRed,rgbcol.rgbtGreen,rgbcol.rgbtBlue);
   257 			}
   258 			break;
   259 		case 32:
   260 			{
   261 			unsigned long int* dwordptr = (unsigned long int*)&iBmpBits[(iBmpHeader.biHeight-aYCoord-1)*((iBmpHeader.biWidth)<<2)+(aXCoord<<2)];
   262 			color = TRgb((*dwordptr&0xff0000)>>16,(*dwordptr&0xff00)>>8,*dwordptr&0xff);
   263 			}
   264 			break;
   265 		default:
   266 			break;
   267 		}
   268 	if (color == darkgray)
   269 		color = darkgrayex;
   270 	else if (color == lightgray)
   271 		color = lightgrayex;
   272 	return color;
   273 	}
   274 
   275 unsigned char BitmapLoader::GetAlphaPixel(long aXCoord,long aYCoord)
   276 	{
   277 	return iAlphaBits[(iBmpHeader.biHeight-aYCoord-1)*((iBmpHeader.biWidth+3)&~3)+aXCoord];
   278 	}
   279 
   280 int BitmapLoader::DoConvert(int aBpp,TBitmapColor aColor,SEpocBitmapHeader*& aPbm)
   281 	{
   282 	bool useAlpha = (aColor==EColorBitmapAlpha || aColor==EColorBitmapAlphaPM);
   283 	long desttwipswidth = 0;
   284 	long desttwipsheight = 0;
   285 
   286 	long bytewidth = BitmapUtils::ByteWidth(iBmpHeader.biWidth,aBpp);
   287 	long destlength = iBmpHeader.biHeight * bytewidth;
   288 
   289 	if (iBmpHeader.biXPelsPerMeter>0)
   290 		desttwipswidth = iBmpHeader.biWidth*1440000/254/iBmpHeader.biXPelsPerMeter;
   291 	if (iBmpHeader.biYPelsPerMeter>0)
   292 		desttwipsheight = iBmpHeader.biHeight*1440000/254/iBmpHeader.biYPelsPerMeter;
   293 
   294 	aPbm = (SEpocBitmapHeader*)new char[sizeof(SEpocBitmapHeader) + destlength];
   295 	if (aPbm == NULL)
   296 		return NoMemory;
   297 	memset(aPbm,0,sizeof(SEpocBitmapHeader));
   298 
   299 	// aBitmap->iByteWidth = bytewidth;
   300 	// aBitmap->iDataOffset = sizeof(Bitmap);
   301 
   302 	aPbm->iBitmapSize = sizeof(SEpocBitmapHeader) + destlength;
   303 	aPbm->iStructSize = sizeof(SEpocBitmapHeader);
   304 	aPbm->iWidthInPixels = iBmpHeader.biWidth;
   305 	aPbm->iHeightInPixels = iBmpHeader.biHeight;
   306 	aPbm->iWidthInTwips = desttwipswidth;
   307 	aPbm->iHeightInTwips = desttwipsheight;
   308 	aPbm->iBitsPerPixel = aBpp;
   309 	aPbm->iColor = aColor;
   310 	aPbm->iPaletteEntries = 0;
   311 	aPbm->iCompression = ENoBitmapCompression;
   312 
   313 	char* pbmBits = ((char*)aPbm) + sizeof(SEpocBitmapHeader);
   314 	memset(pbmBits,0xff,destlength);
   315 
   316 	long col = 0;
   317 	char* pixadd = 0;
   318 
   319 	switch(aBpp)
   320 		{
   321 	case 1:
   322 		{
   323 		for(long ycrd=0;ycrd<iBmpHeader.biHeight;ycrd++)
   324 			for(long xcrd=0;xcrd<iBmpHeader.biWidth;xcrd++)
   325 				{
   326 				TRgb color=GetBmpPixel(xcrd,ycrd);
   327 				col=color.Gray2();
   328 				pixadd=&(pbmBits[ycrd*bytewidth+(xcrd>>3)]);
   329 				(*pixadd)&=~(1<<((xcrd&7)));
   330 				(*pixadd)|=(unsigned char)(col<<(xcrd&7));
   331 				}
   332 		}
   333 		break;
   334 	case 2:
   335 		{
   336 		for(long ycrd=0;ycrd<iBmpHeader.biHeight;ycrd++)
   337 			for(long xcrd=0;xcrd<iBmpHeader.biWidth;xcrd++)
   338 				{
   339 				TRgb color=GetBmpPixel(xcrd,ycrd);
   340 				col=color.Gray4();
   341 				pixadd=&(pbmBits[ycrd*bytewidth+(xcrd>>2)]);
   342 				(*pixadd)&=~(0x3<<(2*(xcrd%4)));
   343 				(*pixadd)|=(unsigned char)(col<<(2*(xcrd%4)));
   344 				}
   345 		}
   346 		break;
   347 	case 4:
   348 		{
   349 		for(long ycrd=0;ycrd<iBmpHeader.biHeight;ycrd++)
   350 			for(long xcrd=0;xcrd<iBmpHeader.biWidth;xcrd++)
   351 				{
   352 				TRgb color=GetBmpPixel(xcrd,ycrd);
   353 				if (aColor == EMonochromeBitmap)
   354 					col = color.Gray16();
   355 				else
   356 					col = color.Color16();
   357 				pixadd=&(pbmBits[ycrd*bytewidth+(xcrd>>1)]);
   358 				if (xcrd%2!=0)
   359 					*pixadd=(unsigned char)((unsigned char)((col<<4)|(*pixadd&0x0f)));
   360 				else
   361 					*pixadd=(unsigned char)((unsigned char)(col|(*pixadd&0xf0)));
   362 				}
   363 		}
   364 		break;
   365 	case 8:
   366 		{
   367 		for(long ycrd=0;ycrd<iBmpHeader.biHeight;ycrd++)
   368 			for(long xcrd=0;xcrd<iBmpHeader.biWidth;xcrd++)
   369 				{
   370 				TRgb color=GetBmpPixel(xcrd,ycrd);
   371 				if (aColor == EMonochromeBitmap)
   372 					col = color.Gray256();
   373 				else
   374 					col = color.Color256();
   375 				pixadd=&(pbmBits[ycrd*((iBmpHeader.biWidth+3)&~3)+xcrd]);
   376 				*pixadd=(unsigned char)col;
   377 				}
   378 		}
   379 		break;
   380 	case 12:
   381 		{
   382 		for(long ycrd=0;ycrd<iBmpHeader.biHeight;ycrd++)
   383 			for(long xcrd=0;xcrd<iBmpHeader.biWidth;xcrd++)
   384 				{
   385 				TRgb color=GetBmpPixel(xcrd,ycrd);
   386 				col=color.Color4K();
   387 				pixadd=&(pbmBits[ycrd*bytewidth+(xcrd<<1)]);
   388 				unsigned short* wordadd=(unsigned short*)pixadd;
   389 				*wordadd=(unsigned short)col;
   390 				}
   391 		}
   392 		break;
   393 	case 16:
   394 		{
   395 		for(long ycrd=0;ycrd<iBmpHeader.biHeight;ycrd++)
   396 			for(long xcrd=0;xcrd<iBmpHeader.biWidth;xcrd++)
   397 				{
   398 				TRgb color=GetBmpPixel(xcrd,ycrd);
   399 				col=color.Color64K();
   400 				pixadd=&(pbmBits[ycrd*bytewidth+(xcrd<<1)]);
   401 				unsigned short* wordadd=(unsigned short*)pixadd;
   402 				*wordadd=(unsigned short)col;
   403 				}
   404 		}
   405 		break;
   406 	case 24:
   407 		{
   408 		for(long ycrd=0;ycrd<iBmpHeader.biHeight;ycrd++)
   409 			{
   410 			unsigned char* bytePtr = (unsigned char*)&pbmBits[ycrd*bytewidth];
   411 			for(long xcrd=0;xcrd<iBmpHeader.biWidth;xcrd++)
   412 				{
   413 				TRgb col = GetBmpPixel(xcrd,ycrd);
   414 				*bytePtr++ = (unsigned char) col.iBlue;
   415 				*bytePtr++ = (unsigned char) col.iGreen;
   416 				*bytePtr++ = (unsigned char) col.iRed;
   417 				}
   418 			}
   419 		}
   420 		break;
   421 	case 32:
   422 		{
   423 		for(long ycrd=0;ycrd<iBmpHeader.biHeight;ycrd++)
   424 			{
   425 			unsigned char* bytePtr = (unsigned char*)&pbmBits[ycrd*bytewidth];
   426 			for(long xcrd=0;xcrd<iBmpHeader.biWidth;xcrd++)
   427 				{
   428 				TRgb col = GetBmpPixel(xcrd,ycrd);
   429 				unsigned char alpha = useAlpha?GetAlphaPixel(xcrd,ycrd):(unsigned char)0;
   430 				*bytePtr++ = (unsigned char) col.iBlue;
   431 				*bytePtr++ = (unsigned char) col.iGreen;
   432 				*bytePtr++ = (unsigned char) col.iRed;
   433 				*bytePtr++ = alpha;	
   434 				}
   435 			}
   436 		}
   437 		break;
   438 		};
   439 
   440 	return NoError;
   441 	}
   442