graphicstools/gdi_tools/bmconv/BMCONV.H
changeset 0 5d03bc08d59c
equal deleted inserted replaced
-1:000000000000 0:5d03bc08d59c
       
     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 #ifndef __BMCONV_H__
       
    17 #define __BMCONV_H__
       
    18 
       
    19 #include <string.h>
       
    20 
       
    21 #if defined(__MSVCDOTNET__) || defined(__TOOLS2__)
       
    22 #include <fstream>
       
    23 #include <iostream>
       
    24 using namespace std;
       
    25 #else //!__MSVCDOTNET__
       
    26 #include <fstream.h>
       
    27 #include <iostream.h>
       
    28 #endif //__MSVCDOTNET__
       
    29 
       
    30 #include <stdlib.h>
       
    31 
       
    32 #ifdef __LINUX__
       
    33     #define GNUPACK __attribute__((packed))
       
    34     #define OPTCHAR '-'
       
    35     /* on Linux the '/' optchar cannot be supported as filenames can start with '/'. */
       
    36     #define ALTERNATE_OPTCHAR '-'
       
    37 #else
       
    38     #define GNUPACK
       
    39     #define OPTCHAR '-' 
       
    40     #define ALTERNATE_OPTCHAR '/'
       
    41 #endif
       
    42 
       
    43 
       
    44 /**
       
    45 @internalComponent
       
    46 */
       
    47 const long int KWriteOnceFileStoreUid=268435511;
       
    48 const long int KCBitwiseBitmapUid=268435520;
       
    49 const long int KMultiBitmapRomImageUid=268435521;
       
    50 const long int KMultiBitmapFileImageUid=268435522;
       
    51 const long int KMultiBitmapFileImageChecksum=1194943545;
       
    52 /**
       
    53 @internalComponent
       
    54 synchronized with enum SEpocBitmapHeader::TColor in \epoc32\include\bitmap.h
       
    55 */
       
    56 enum TBitmapColor
       
    57 	{
       
    58 	EMonochromeBitmap = 0,
       
    59 	EColorBitmap = 1,
       
    60 	EColorBitmapAlpha =2 ,
       
    61     EColorBitmapAlphaPM = 3,	
       
    62 	EColorBitmapUndefined = 8	
       
    63 	};
       
    64 
       
    65 /** 
       
    66 Defines the types of file compression. 
       
    67 @internalComponent
       
    68 */
       
    69 enum TBitmapfileCompression
       
    70 	{
       
    71 	/** Bitmap file is not compressed. */
       
    72 	ENoBitmapCompression=0,
       
    73 	/** File is compressed using run-length encoding compression. */
       
    74 	EByteRLECompression,
       
    75 	/** File is compressed using twelve bit run-length encoding compression. */
       
    76 	ETwelveBitRLECompression,
       
    77 	/** File is compressed using sixteen bit run-length encoding compression. */
       
    78 	ESixteenBitRLECompression,
       
    79 	/** File is compressed using twenty four bit run-length encoding compression. */
       
    80 	ETwentyFourBitRLECompression,
       
    81 	/** File is compressed using unsigned thirty two bit run-length encoding compression. */
       
    82 	EThirtyTwoUBitRLECompression,
       
    83 	/** File is compressed using unsigned thirty two bit run-length encoding compression and includes an alpha channel. */
       
    84 	EThirtyTwoABitRLECompression,
       
    85 	/** Insert new compression types here */
       
    86 	ERLECompressionLast = 255
       
    87 	};
       
    88 /**
       
    89 @internalComponent
       
    90 */
       
    91 enum TStoreType
       
    92 	{
       
    93 	ENoStore,
       
    94 	EFileStore,
       
    95 	ERomStore,
       
    96 	ECompressedRomStore
       
    97 	};
       
    98 /**
       
    99 @internalComponent
       
   100 */
       
   101 enum Errors
       
   102 	{
       
   103 	NoCompression=-1,
       
   104 	NoError=0,
       
   105 	NoMemory,
       
   106 	Arg,
       
   107 	Files,
       
   108 	SourceFile,
       
   109 	DestFile,
       
   110 	CommandFile,
       
   111 	OutOfRange,
       
   112 	TooManyArgs,
       
   113 	UnknownCompression,
       
   114 	CompressionError,
       
   115 	DecompressionError,
       
   116 	Bpp,
       
   117 	PaletteFile,
       
   118 	PaletteSupportNotImplemented,
       
   119 	AlphaFiles,
       
   120 	AlphaDimensions,
       
   121 	AlphaBpp
       
   122 	};
       
   123 /**
       
   124 @internalComponent
       
   125 */
       
   126 struct SEpocBitmapHeader
       
   127 /** Contains information about the bitmap when streaming bitmaps to stores. */
       
   128 	{
       
   129 	/** The size of the bitmap data, in bytes. */
       
   130 	long iBitmapSize;
       
   131 	/** The size of the structure in which the bitmap data is stored. */
       
   132 	long iStructSize;
       
   133 	long iWidthInPixels;
       
   134 	long iHeightInPixels;
       
   135 	long iWidthInTwips;
       
   136 	long iHeightInTwips;
       
   137 	/** The bitmaps number of bits per pixel */
       
   138 	long iBitsPerPixel;
       
   139 	/** Whether or not the bitmap is colour. A true value means that the bitmap 
       
   140 	is colour. */
       
   141 	TBitmapColor iColor;
       
   142 	/** The number of entries in the bitmap's palette. */
       
   143 	long iPaletteEntries;	// always 0 in current implementations
       
   144 	/** The type of compression used to store the bitmap. */
       
   145 	TBitmapfileCompression iCompression;
       
   146 	};
       
   147 
       
   148 #ifndef __LINUX__ 
       
   149 #include <pshpack2.h>
       
   150 #endif 
       
   151 /**
       
   152 @internalComponent
       
   153 */
       
   154 struct TRgb24bit
       
   155 	{
       
   156 	unsigned char iBlue;
       
   157 	unsigned char iGreen;
       
   158 	unsigned char iRed;
       
   159 	} GNUPACK;
       
   160 #ifndef __LINUX__ 
       
   161 #include <poppack.h>
       
   162 #endif 
       
   163 
       
   164 #ifndef __LINUX__ 
       
   165 #include <pshpack2.h>
       
   166 #endif 
       
   167 /**
       
   168 @internalComponent
       
   169 */
       
   170 struct TBitmapFileHeader
       
   171 	{
       
   172 	unsigned short bfType;
       
   173 	unsigned long bfSize;
       
   174 	unsigned short bfReserved1;
       
   175 	unsigned short bfReserved2;
       
   176 	unsigned long bfOffBits;
       
   177 	} GNUPACK;
       
   178 #ifndef __LINUX__ 
       
   179 #include <poppack.h>
       
   180 #endif 
       
   181 /**
       
   182 @internalComponent
       
   183 */
       
   184 struct TBitmapInfoHeader
       
   185 	{
       
   186 	unsigned long biSize;
       
   187 	long biWidth;
       
   188 	long biHeight;
       
   189 	unsigned short biPlanes;
       
   190 	unsigned short biBitCount;
       
   191 	unsigned long biCompression;
       
   192 	unsigned long biSizeImage;
       
   193 	long biXPelsPerMeter;
       
   194 	long biYPelsPerMeter;
       
   195 	unsigned long biClrUsed;
       
   196 	unsigned long biClrImportant;
       
   197 	};
       
   198 
       
   199 #ifndef __LINUX__ 
       
   200 #include <pshpack2.h>
       
   201 #endif 
       
   202 /**
       
   203 @internalComponent
       
   204 */
       
   205 struct TRgbTriple
       
   206 	{
       
   207 	unsigned char rgbtBlue;
       
   208 	unsigned char rgbtGreen;
       
   209 	unsigned char rgbtRed;
       
   210 	} GNUPACK;
       
   211 #ifndef __LINUX__ 
       
   212 #include <poppack.h>
       
   213 #endif 
       
   214 /**
       
   215 @internalComponent
       
   216 */
       
   217 struct TRgbQuad
       
   218 	{
       
   219 	unsigned char iBlue;
       
   220 	unsigned char iGreen;
       
   221 	unsigned char iRed;
       
   222 	unsigned char iReserved;
       
   223 	};
       
   224 
       
   225 class TRgb
       
   226 /** 
       
   227 24-bit RGB colour value with 8 bits each for red, green and blue.
       
   228 
       
   229 All Graphics drawing functions are specified in terms of a 32-bit TRgb colour 
       
   230 containing the three colour values plus 8 unused bits. For hardware which 
       
   231 does not support 24-bit colour, a mapping from TRgb to display colours is 
       
   232 performed. 
       
   233 
       
   234 The supported display modes are enumerated in the TDisplayMode type. In each 
       
   235 display mode a unique index can represent each physical colours supported, 
       
   236 and which can be mapped onto a full RGB value. The mappings are as follows: 
       
   237 
       
   238 16-colour displays use the EGA colour set: black, white, and then both light 
       
   239 and dark versions of grey, red, green, blue, cyan, magenta and yellow
       
   240 
       
   241 256-colour displays support 216 colours made up of 6x6x6 RGB values, each 
       
   242 containing all possible multiples of 51 for R,G,B values. Additionally, all 
       
   243 remaining 10 shades of pure red, green, blue and grey are represented, by 
       
   244 adding all remaining multiples of 17. This use of 256 colours is sometimes 
       
   245 known as the Netscape colour cube.
       
   246 
       
   247 4096-colour displays effectively support RGB values with 4 bits per primary 
       
   248 colour
       
   249 
       
   250 64k-colour displays effectively support RGB values with 5 bits allocated to 
       
   251 red, 6 to green and 5 to blue
       
   252 
       
   253 16 million-colour displays support true colour with 8 bits allocated to each 
       
   254 primary colour
       
   255 
       
   256 @see TDisplayMode
       
   257 @see DynamicPalette 
       
   258 @internalComponent
       
   259 */
       
   260 	{
       
   261 public:
       
   262 	TRgb();
       
   263 	TRgb(long unsigned int val);
       
   264 	TRgb(int aRed,int aGreen,int aBlue);
       
   265 	TRgb &operator=(const TRgb& aColor);
       
   266 	int operator==(const TRgb& aColor);
       
   267 	int Difference(const TRgb& col) const;
       
   268 	int Gray2() const;
       
   269 	int Gray4() const;
       
   270 	int Gray16() const;
       
   271 	int Gray256() const;
       
   272 	int Color16() const;
       
   273 	int Color256() const;
       
   274 	int Color4K() const;
       
   275 	int Color64K() const;
       
   276 	long int Color16M() const;
       
   277 	static TRgb Gray2(int aGray2);
       
   278 	static TRgb Gray4(int aGray4);
       
   279 	static TRgb Gray16(int aGray16);
       
   280 	static TRgb Gray256(int aGray256);
       
   281 	static TRgb Color16(int aColor16);
       
   282 	static TRgb Color256(int aColor256);
       
   283 	static TRgb Color4K(int aColor64K);
       
   284 	static TRgb Color64K(int aColor64K);
       
   285 	static TRgb Color16M(long int aColor16M);
       
   286 public:
       
   287 	unsigned char iRed;
       
   288 	unsigned char iGreen;
       
   289 	unsigned char iBlue;
       
   290 	unsigned char iSpare;
       
   291 	};
       
   292 
       
   293 class Bitmap
       
   294 /**
       
   295 @internalComponent
       
   296 */
       
   297 	{
       
   298 public:
       
   299 	int iUid;
       
   300 	int iDispMode;
       
   301 	void* iHeap;
       
   302 	void* iPile;
       
   303 	int iByteWidth;
       
   304 	SEpocBitmapHeader iHeader;
       
   305 	int iChunk;
       
   306 	int iDataOffset; // offset from "this"
       
   307 	};
       
   308 
       
   309 class BitmapCompiler
       
   310 /**
       
   311 Historical note - Pbm is an abbreviation for "Symbian Bitmap"
       
   312 @internalComponent
       
   313 */
       
   314 	{
       
   315 public:
       
   316 	BitmapCompiler(char* aSourcefilenames[],int aNumSources);
       
   317 	~BitmapCompiler();
       
   318 	int Compile(TStoreType aSt,int aCompress,char* aDestfilename,char* aHeaderFilename,char* aPaletteFilename);
       
   319 private:
       
   320 	int AllocatePbmSourcesArray();
       
   321 	int LoadSourcefiles();
       
   322 	int LoadPbmFile(char* aPbmFilename);
       
   323 	int RomImage(bool aCompress);
       
   324 	int FileImage(int aCompress);
       
   325 	int WriteRomheader();
       
   326 	int WriteRombitmap(SEpocBitmapHeader* aPbm);
       
   327 	int WriteFileheader();
       
   328 	int WriteFilebitmap(SEpocBitmapHeader* aPbm);
       
   329 	int WriteHeadStream();
       
   330 	int CreateHeader(char* aHeaderFilename);
       
   331 	char* UnadornedName(char* aName);	// returns pointer to static buffer
       
   332 	int LoadPalette(char* aPaletteFilename);
       
   333 	void CalculateInversePalette();
       
   334 	void WritePalette();
       
   335 	int CompressBitmap(SEpocBitmapHeader*& aPbm);
       
   336 	int CompressByteData(char*& aDest,char* aSrce,int aSize);
       
   337 	int WriteCompressedByteData(char*& aDest,char* aData,int aLength, const char* aDestEnd);
       
   338 	int WriteCompressedByteValues(char*& aDest,char aValue,int aLength, const char* aDestEnd);
       
   339 	int CompressTwelveBitData(char*& aDest,char* aSrce,int aSizeInBytes);
       
   340 	int WriteCompressedTwelveBitData(unsigned short*& aDest,unsigned short aData,int aLength);
       
   341 	int CompressSixteenBitData(char*& aDest,char* aSrce,int aSize);
       
   342 	int WriteCompressedSixteenBitData(char*& aDest,unsigned short* aData,int aLength, const char* aDestEnd);
       
   343 	int WriteCompressedSixteenBitValues(char*& aDest,unsigned short aValue,int aLength, const char* aDestEnd);
       
   344 	int CompressTwentyFourBitData(char*& aDest,char* aSrce,int aSizeInBytes);
       
   345 	int WriteCompressedTwentyFourBitData(char*& aDest,char* aData,int aLength, const char* aDestEnd);
       
   346 	int WriteCompressedTwentyFourBitValues(char*& aDest,char aComponent1,char aComponent2,char aComponent3,int aLength, const char* aDestEnd);
       
   347 	int CompressThirtyTwoUBitData(char*& aDest,char* aSrce,int aSizeInBytes);
       
   348 	int WriteCompressedThirtyTwoUBitData(char*& aDest,char* aData, int aLength, const char* aDestEnd);
       
   349 	int WriteCompressedThirtyTwoUBitValues(char*& aDest,char aComponent1, char aComponent2,char aComponent3, int aLength, const char* aDestEnd);
       
   350 	int TrueColorPointerCompare(char* aColorPointer,char aComponent1,char aComponent2,char aComponent3);
       
   351 	int CompressThirtyTwoABitData(char*& aDest,char* aSrce,int aSizeInBytes);
       
   352 	int WriteCompressedThirtyTwoABitData(char*& aDest,char* aData,int aLength,const char* aDestEnd);
       
   353 	int WriteCompressedThirtyTwoABitValues(char*& aDest,char aComponent1,char aComponent2,char aComponent3,char aComponent4,int aLength,const char* aDestEnd);
       
   354 	int ColorAlphaPointerCompare(char* aColorPointer,char aComponent1,char aComponent2,char aComponent3,char aComponent4);
       
   355 	int ReadHexString(char aHexBuf[10],char*& aDataPtr,char* aDataPtrLimit);
       
   356 	int HexToInt(char aHighNibble,char aLowNibble);
       
   357 	int HexToInt(char aNibble);
       
   358 	static void CopyTail(void* aDst, void* aSrc, int aFullSize, int aSkipped);
       
   359 private:
       
   360 	fstream iDestFile;
       
   361 	char** iSourcefilenames;
       
   362 	SEpocBitmapHeader** iPbmSources;
       
   363 	int iNumSources;
       
   364 	int iDefaultPalette;
       
   365 	TRgb iPalette[256];
       
   366 	char iInversePalette[4096];
       
   367 	};
       
   368 
       
   369 class BitmapLoader
       
   370 /**
       
   371 @internalComponent
       
   372 */
       
   373 	{
       
   374 public:
       
   375 	BitmapLoader();
       
   376 	~BitmapLoader();
       
   377 	int LoadBitmap(char* aFileName,int aBpp,TBitmapColor aColor,SEpocBitmapHeader*& aPbm);
       
   378 private:
       
   379 	int DoLoad(char* aFileName);
       
   380 	int DoLoadAlpha(char* aAlphaFileName);
       
   381 	int DoConvert(int aBpp,TBitmapColor aColor,SEpocBitmapHeader*& aPbm);
       
   382 	TRgb GetBmpPixel(long aXCoord,long aYCoord);
       
   383 	unsigned char GetAlphaPixel(long aXCoord,long aYCoord);
       
   384 private:
       
   385 	TBitmapInfoHeader iBmpHeader;
       
   386 	int iNumBmpColors;
       
   387 	TRgbQuad* iBmpColors;
       
   388 	char* iBmpBits;
       
   389 	char* iAlphaBits;
       
   390 	};
       
   391 
       
   392 class EpocLoader
       
   393 /**
       
   394 @internalComponent
       
   395 */
       
   396 	{
       
   397 public:
       
   398 	EpocLoader();
       
   399 	~EpocLoader();
       
   400 	int EpocBitmapCount(char* aFilename,int& aCount,int& isRomFormat);
       
   401 	int LoadEpocBitmap(char* aFilename,int aIndex);
       
   402 	int SaveBitmap(char* aFilename);
       
   403 	int SaveAlpha(char* aFilename);
       
   404 	int DupBitmap(SEpocBitmapHeader*& aPbm);
       
   405 	int ValidateEpocBitmap(char* aFilename);
       
   406 	SEpocBitmapHeader Header() const { return iOriginalPbmHeader;};
       
   407 private:
       
   408 	int LoadFile(char* aFilename,int aIndex);
       
   409 	int LoadRom(char* aFilename,int aIndex);
       
   410 	TRgb GetPixel(int aXCoord,int aYCoord);
       
   411 	unsigned char GetAlpha(int aXCoord,int aYCoord);
       
   412 	int DoLoadFile(fstream& aFile);
       
   413 	int DoLoadRom(fstream& aFile);
       
   414 	int ExpandByteRLEData(char* aDest,int aDestSize,char* aSrce,int aSrceSize);
       
   415 	int ExpandTwelveBitRLEData(char* aDest,int aDestSizeInBytes,char* aSrce,int aSrceSizeInBytes);
       
   416 	int ExpandSixteenBitRLEData(char* aDest,int aDestSizeInBytes,char* aSrce,int aSrceSizeInBytes);
       
   417 	int ExpandTwentyFourBitRLEData(char* aDest,int aDestSizeInBytes,char* aSrce,int aSrceSizeInBytes);
       
   418 	int ExpandThirtyTwoUBitRLEData(char* aDest,int aDestSizeInBytes,char* aSrce,int aSrceSizeInBytes);
       
   419 	int ExpandThirtyTwoABitRLEData(char* aDest,int aDestSizeInBytes,char* aSrce,int aSrceSizeInBytes);
       
   420 	int Decompress(int aSize);
       
   421 private:
       
   422 	SEpocBitmapHeader iOriginalPbmHeader;
       
   423 	SEpocBitmapHeader iPbmHeader;
       
   424 	char* iPbmBits;
       
   425 	};
       
   426 
       
   427 class BitmapUtils
       
   428 /**
       
   429 @internalComponent
       
   430 */
       
   431 	{
       
   432 public:
       
   433 	static int ByteWidth(int aPixelWidth,int aBitsPerPixel);
       
   434 	};
       
   435 
       
   436 #endif