File: pltgadec.cpp

package info (click to toggle)
paintlib 2.6.2-14
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,920 kB
  • ctags: 3,874
  • sloc: cpp: 25,209; sh: 10,605; ansic: 1,891; makefile: 120
file content (374 lines) | stat: -rw-r--r-- 9,219 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
/--------------------------------------------------------------------
|
|      $Id: pltgadec.cpp,v 1.9 2004/09/11 12:41:35 uzadow Exp $
|      Targa Decoder Class
|
|      Targa file decoder. Decodes 8, 15, 16, 24 and 32 bpp
|      targa files (compressed and uncompressed) and returns a 32
|      bpp bitmap. Preserves the alpha channel.
|
|      Copyright (c) 1996-2002 Ulrich von Zadow
|
\--------------------------------------------------------------------
*/

#include "pltgadec.h"
#include "plexcept.h"


PLTGADecoder::PLTGADecoder
    ()
    : PLPicDecoder()
    // Creates a decoder
{
}


PLTGADecoder::~PLTGADecoder
    ()
{
}

void PLTGADecoder::Open (PLDataSource * pDataSrc)
{
  Trace (2, "Decoding TGA.\n");

  readTgaHeader (&m_TgaHead, pDataSrc);

  PLPixelFormat pf = PLPixelFormat::DONTCARE;

  switch (m_TgaHead.PixelDepth)
  {
    case 8:
      if (m_TgaHead.ImageType == TGA_Mono ||
          m_TgaHead.ImageType == TGA_RLEMono)
        pf = PLPixelFormat::L8;
       else
        pf = PLPixelFormat::I8;
      break;           
    case 16:
    case 32:
      pf = PLPixelFormat::A8R8G8B8;
      break;
    case 15:
    case 24:
      pf = PLPixelFormat::X8R8G8B8;
      break;
    default:
      raiseError (PL_ERRFORMAT_UNKNOWN,
                "TGA decoder: Unknown bits per pixel in file.");

  }
  SetBmpInfo (PLPoint (m_TgaHead.ImageWidth, m_TgaHead.ImageHeight), 
              PLPoint(0, 0), pf);
}

void PLTGADecoder::GetImage (PLBmpBase & Bmp)
{
  // Read the color map data (Version 1.0 and 2.0).
  if (m_TgaHead.CmapType != 0)
    readPalette (m_TgaHead.CmapIndex,
                 m_TgaHead.CmapLength,
                 m_TgaHead.CmapEntrySize,
                 &Bmp,
                 m_pDataSrc);

  readImage (&m_TgaHead, &Bmp, m_pDataSrc);
}


void PLTGADecoder::readTgaHeader
    ( TGAHEADER * pTgaHead,       // Pointer to TGA header structure
      PLDataSource * pDataSrc
    )
{
  // Read the TGA header (Version 1.0 and 2.0).
  pTgaHead->IdLength      = ReadByte (pDataSrc);
  pTgaHead->CmapType      = ReadByte (pDataSrc);
  pTgaHead->ImageType     = ReadByte (pDataSrc);
  pTgaHead->CmapIndex     = ReadIWord (pDataSrc);
  pTgaHead->CmapLength    = ReadIWord (pDataSrc);
  pTgaHead->CmapEntrySize = ReadByte (pDataSrc);
  pTgaHead->X_Origin      = ReadIWord (pDataSrc);
  pTgaHead->Y_Origin      = ReadIWord (pDataSrc);
  pTgaHead->ImageWidth    = ReadIWord (pDataSrc);
  pTgaHead->ImageHeight   = ReadIWord (pDataSrc);
  pTgaHead->PixelDepth    = ReadByte (pDataSrc);
  pTgaHead->ImagDesc      = ReadByte (pDataSrc);

  // Skip image ID
  pDataSrc->Skip (pTgaHead->IdLength);
}


void PLTGADecoder::readPalette
    ( int StartIndex,           // Index of first palette entry.
      int Length,               // Number of palette entries stored.
      int EntrySize,            // Size of palette entries in bits.
      PLBmpBase * pBmp,
      PLDataSource * pDataSrc
    )
    // Reads the TGA palette and creates a windows palette.
{
  int   i;

  for (i=StartIndex; i<StartIndex+Length; i++)
  {
    PLPixel32 CurEntry = readPixel32 (EntrySize, pDataSrc);
    pBmp->SetPaletteEntry (i, CurEntry);
  }
}


void PLTGADecoder::readImage
    ( TGAHEADER * pTgaHead,       // Pointer to TGA header structure
      PLBmpBase * pBmp,
      PLDataSource * pDataSrc
    )
{
  bool bCompressed;

  if (pTgaHead->ImageType == TGA_Mono ||
      pTgaHead->ImageType == TGA_RLEMono)
    pBmp->SetGrayPalette ();

  switch (pTgaHead->ImageType)
  {
    case TGA_Map:
    case TGA_RGB:
    case TGA_Mono:
      bCompressed = false;
      break;
    case TGA_RLEMap:
    case TGA_RLERGB:
    case TGA_RLEMono:
      bCompressed = true;
      break;
    default:
      raiseError (PL_ERRFORMAT_UNKNOWN, "Unknown TGA image type.");
  }
  readData (pTgaHead, bCompressed, pBmp, pDataSrc);
}


void PLTGADecoder::readData
    ( TGAHEADER * pTgaHead,       // Pointer to TGA header structure
      bool bCompressed,
      PLBmpBase * pBmp,
      PLDataSource * pDataSrc
    )
{
  int Width = pTgaHead->ImageWidth;
  int Height = pTgaHead->ImageHeight;
  int bpp = pTgaHead->PixelDepth;

  // Bits 4 & 5 of the Image Descriptor byte control the ordering of
  // the pixels.
  bool bXReversed = ((pTgaHead->ImagDesc & 16) == 16);
  bool bYReversed = ((pTgaHead->ImagDesc & 32) == 32);

  PLBYTE * pDest;
  PLBYTE ** pLineArray = pBmp->GetLineArray();

  int y;

  for (y=0; y < Height; y++)
  {
    if (bYReversed)
      pDest = pLineArray[y];
      else
      pDest = pLineArray[Height-y-1];

    if (!bCompressed)
      expandUncompressedLine (pDest, Width, bXReversed, bpp, pDataSrc);
      else
      expandCompressedLine (pDest, Width, bXReversed, bpp, pDataSrc);
  }
}


void PLTGADecoder::expandUncompressedLine
    ( PLBYTE * pDest,
      int Width,
      bool bReversed,
      int bpp,
      PLDataSource * pDataSrc
    )
{
  int x;

  for (x=0; x<Width; x++)
  {
    if (bpp > 8)
    {
      *((PLPixel32 *)pDest) = readPixel32 (bpp, pDataSrc);
      pDest += 4;
    }
    else
    {
      *pDest = readPixel8 (bpp, pDataSrc);
      pDest ++;
    }
  }
}


void PLTGADecoder::expandCompressedLine
    ( PLBYTE * pDest,
      int Width,
      bool bReversed,
      int bpp,
      PLDataSource * pDataSrc
    )
{
  int  x;
  int  i;
  PLBYTE Count;

  for (x=0; x<Width; )
  {
    Count = ReadByte (pDataSrc);
    if (Count & 128)
    { // RLE-Encoded packet
      Count -= 127; // Calculate real repeat count.
      if (bpp > 8)
      {
        *((PLPixel32 *)pDest) = readPixel32 (bpp, pDataSrc);
        for (i=1; i<Count; i++)
          *((PLPixel32 *)(pDest+i*4)) = *(PLPixel32 *)pDest;
      }
      else
      {
        *pDest = readPixel8 (bpp, pDataSrc);
        for (i=1; i<Count; i++)
          *(pDest+i) = *pDest;
      }
    }
    else
    { // Raw packet
      Count += 1; // Calculate real repeat count.
      for (i=0; i<Count; i++)
      {
        if (bpp > 8)
          *((PLPixel32 *)(pDest+i*4)) = readPixel32 (bpp, pDataSrc);
        else
          *(pDest+i) = readPixel8 (bpp, pDataSrc);
      }
    }
    if (bpp > 8)
      pDest += Count*4;
    else
      pDest += Count;
    x += Count;
  }
}


PLPixel32 PLTGADecoder::readPixel32
    ( int bpp,
      PLDataSource * pDataSrc
    )
    // The decoder could be made much faster if the big switch (bpp)
    // statement was taken out of the inner loop. On the other hand,
    // doing that would cause a lot of code to be repeated half a
    // dozen times...
{
  PLPixel32 Dest;
  PLWORD Src;
  PLBYTE * pCurEntry;

  switch (bpp)
  {
    case 15:
    case 16:
      Src = ReadIWord(pDataSrc);
      if (bpp == 16)
        Dest.Set (( Src >> 7 ) & 0x0F8,     // red
                  ( Src >> 2 ) & 0x0F8,     // green
                  ( Src & 0x1F ) * 8,       // blue
                  (PLBYTE)(Src & 32786 >> 8));
       else
        Dest.Set (( Src >> 7 ) & 0x0F8,     // red
                  ( Src >> 2 ) & 0x0F8,     // green
                  ( Src & 0x1F ) * 8,       // blue
                  0xFF);
      break;
    case 24:
      pCurEntry = pDataSrc->ReadNBytes (3);
      Dest.Set (*(pCurEntry+2), *(pCurEntry+1), *(pCurEntry), 0xFF);
      break;
    case 32:
      pCurEntry = pDataSrc->ReadNBytes (4);
      Dest.Set (*(pCurEntry+2), *(pCurEntry+1), *(pCurEntry), *(pCurEntry+3));
      break;
  }
  return Dest;
}

PLBYTE PLTGADecoder::readPixel8
    ( int bpp,
      PLDataSource * pDataSrc
    )
{
  PLBYTE Dest;

  PLASSERT (bpp == 8);

  Dest = ReadByte(pDataSrc);

  return Dest;
}

/*
/--------------------------------------------------------------------
|
|      $Log: pltgadec.cpp,v $
|      Revision 1.9  2004/09/11 12:41:35  uzadow
|      removed plstdpch.h
|
|      Revision 1.8  2004/09/09 16:52:49  artcom
|      refactored PixelFormat
|
|      Revision 1.7  2004/06/19 16:49:07  uzadow
|      Changed GetImage so it works with PLBmpBase
|
|      Revision 1.6  2002/08/04 21:20:41  uzadow
|      no message
|
|      Revision 1.5  2002/08/04 20:08:01  uzadow
|      Added PLBmpInfo class, ability to extract metainformation from images without loading the whole image and proper greyscale support.
|
|      Revision 1.4  2002/03/31 13:36:42  uzadow
|      Updated copyright.
|
|      Revision 1.3  2001/10/21 17:12:40  uzadow
|      Added PSD decoder beta, removed BPPWanted from all decoders, added PLFilterPixel.
|
|      Revision 1.2  2001/10/06 22:03:26  uzadow
|      Added PL prefix to basic data types.
|
|      Revision 1.1  2001/09/16 19:03:22  uzadow
|      Added global name prefix PL, changed most filenames.
|
|      Revision 1.8  2001/02/04 14:31:52  uzadow
|      Member initialization list cleanup (Erik Hoffmann).
|
|      Revision 1.7  2000/12/18 22:42:52  uzadow
|      Replaced RGBAPIXEL with PLPixel32.
|
|      Revision 1.6  2000/10/24 23:02:28  uzadow
|      Fixed exception handling.
|
|      Revision 1.5  2000/08/13 12:11:43  Administrator
|      Added experimental DirectDraw-Support
|
|      Revision 1.4  2000/01/16 20:43:14  anonymous
|      Removed MFC dependencies
|
|      Revision 1.3  1999/10/03 18:50:52  Ulrich von Zadow
|      Added automatic logging of changes.
|
|
--------------------------------------------------------------------
*/