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