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