LibreOffice Module vcl (master)  1
itga.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 
21 #include <vcl/graph.hxx>
22 #include <vcl/BitmapTools.hxx>
23 #include <tools/stream.hxx>
24 #include <memory>
25 #include <filter/TgaReader.hxx>
26 
27 class FilterConfigItem;
28 
29 //============================ TGAReader ==================================
30 
31 namespace {
32 
33 struct TGAFileHeader
34 {
35  sal_uInt8 nImageIDLength;
36  sal_uInt8 nColorMapType;
37  sal_uInt8 nImageType;
38  sal_uInt16 nColorMapFirstEntryIndex;
39  sal_uInt16 nColorMapLength;
40  sal_uInt8 nColorMapEntrySize;
41  sal_uInt16 nColorMapXOrigin;
42  sal_uInt16 nColorMapYOrigin;
43  sal_uInt16 nImageWidth;
44  sal_uInt16 nImageHeight;
45  sal_uInt8 nPixelDepth;
46  sal_uInt8 nImageDescriptor;
47 };
48 
49 #define SizeOfTGAFileFooter 26
50 
51 struct TGAFileFooter
52 {
53  sal_uInt32 nExtensionFileOffset;
54  sal_uInt32 nDeveloperDirectoryOffset;
55  sal_uInt32 nSignature[4];
56  sal_uInt8 nPadByte;
57  sal_uInt8 nStringTerminator;
58 };
59 
60 #define SizeOfTGAExtension 495
61 
62 struct TGAExtension
63 {
64  sal_uInt16 nExtensionSize;
65  char sAuthorName[41];
66  char sAuthorComment[324];
67  char sDateTimeStamp[12];
68  char sJobNameID[41];
69  char sSoftwareID[41];
70  sal_uInt16 nSoftwareVersionNumber;
71  sal_uInt8 nSoftwareVersionLetter;
72  sal_uInt32 nKeyColor;
73  sal_uInt16 nPixelAspectRatioNumerator;
74  sal_uInt16 nPixelAspectRatioDeNumerator;
75  sal_uInt16 nGammaValueNumerator;
76  sal_uInt16 nGammaValueDeNumerator;
77  sal_uInt32 nColorCorrectionOffset;
78  sal_uInt32 nPostageStampOffset;
79  sal_uInt32 nScanLineOffset;
80  sal_uInt8 nAttributesType;
81 };
82 
83 class TGAReader {
84 
85 private:
86 
87  SvStream& m_rTGA;
88 
89  std::unique_ptr<vcl::bitmap::RawBitmap> mpBitmap;
90  std::vector<Color> mvPalette;
91  std::unique_ptr<TGAFileHeader>
92  mpFileHeader;
93  std::unique_ptr<TGAFileFooter>
94  mpFileFooter;
95  std::unique_ptr<TGAExtension>
96  mpExtension;
97  std::unique_ptr<sal_uInt32[]>
98  mpColorMap;
99 
100  bool mbStatus;
101 
102  sal_uInt8 mnTGAVersion; // Enhanced TGA is defined as Version 2.0
103  sal_uInt16 mnDestBitDepth;
104  bool mbIndexing; // sal_True if source contains indexing color values
105  bool mbEncoding; // sal_True if source is compressed
106 
107  bool ImplReadHeader();
108  bool ImplReadPalette();
109  bool ImplReadBody();
110 
111 public:
112  explicit TGAReader(SvStream &rTGA);
113  bool ReadTGA(Graphic &rGraphic);
114 };
115 
116 }
117 
118 //=================== Methods of TGAReader ==============================
119 
120 TGAReader::TGAReader(SvStream &rTGA)
121  : m_rTGA(rTGA)
122  , mbStatus(true)
123  , mnTGAVersion(1)
124  , mnDestBitDepth(8)
125  , mbIndexing(false)
126  , mbEncoding(false)
127 {
128 }
129 
130 bool TGAReader::ReadTGA(Graphic & rGraphic)
131 {
132  if ( m_rTGA.GetError() )
133  return false;
134 
135  m_rTGA.SetEndian( SvStreamEndian::LITTLE );
136 
137  // Kopf einlesen:
138 
139  if ( !m_rTGA.GetError() )
140  {
141  mbStatus = ImplReadHeader();
142  if (mbStatus)
143  mbStatus = mpFileHeader->nImageWidth && mpFileHeader->nImageHeight;
144  if (mbStatus)
145  {
146  sal_Size nSize = mpFileHeader->nImageWidth;
147  nSize *= mpFileHeader->nImageHeight;
148  if (nSize > SAL_MAX_INT32/2/3)
149  return false;
150 
151  mpBitmap.reset( new vcl::bitmap::RawBitmap( Size( mpFileHeader->nImageWidth, mpFileHeader->nImageHeight ), 24 ) );
152  if ( mbIndexing )
153  mbStatus = ImplReadPalette();
154  if ( mbStatus )
155  mbStatus = ImplReadBody();
156 
157  if ( mbStatus )
158  rGraphic = vcl::bitmap::CreateFromData(std::move(*mpBitmap));
159  }
160  }
161  return mbStatus;
162 }
163 
164 
165 bool TGAReader::ImplReadHeader()
166 {
167  mpFileHeader.reset( new TGAFileHeader );
168 
169  m_rTGA.ReadUChar( mpFileHeader->nImageIDLength ).ReadUChar( mpFileHeader->nColorMapType ).ReadUChar( mpFileHeader->nImageType ). ReadUInt16( mpFileHeader->nColorMapFirstEntryIndex ).ReadUInt16( mpFileHeader->nColorMapLength ).ReadUChar( mpFileHeader->nColorMapEntrySize ). ReadUInt16( mpFileHeader->nColorMapXOrigin ).ReadUInt16( mpFileHeader->nColorMapYOrigin ).ReadUInt16( mpFileHeader->nImageWidth ). ReadUInt16( mpFileHeader->nImageHeight ).ReadUChar( mpFileHeader->nPixelDepth ).ReadUChar( mpFileHeader->nImageDescriptor );
170 
171  if ( !m_rTGA.good())
172  return false;
173 
174  if ( mpFileHeader->nColorMapType > 1 )
175  return false;
176  if ( mpFileHeader->nColorMapType == 1 )
177  mbIndexing = true;
178 
179  // first we want to get the version
180  mpFileFooter.reset( new TGAFileFooter ); // read the TGA-File-Footer to determine whether
181  // we got an old TGA format or the new one
182 
183  sal_uInt64 nCurStreamPos = m_rTGA.Tell();
184  m_rTGA.Seek( STREAM_SEEK_TO_END );
185  sal_uInt64 nTemp = m_rTGA.Tell();
186  m_rTGA.Seek( nTemp - SizeOfTGAFileFooter );
187 
188  m_rTGA.ReadUInt32( mpFileFooter->nExtensionFileOffset ).ReadUInt32( mpFileFooter->nDeveloperDirectoryOffset ). ReadUInt32( mpFileFooter->nSignature[0] ).ReadUInt32( mpFileFooter->nSignature[1] ).ReadUInt32( mpFileFooter->nSignature[2] ). ReadUInt32( mpFileFooter->nSignature[3] ).ReadUChar( mpFileFooter->nPadByte ).ReadUChar( mpFileFooter->nStringTerminator );
189 
190 
191  if ( !m_rTGA.good())
192  return false;
193 
194  // check for sal_True, VISI, ON-X, FILE in the signatures
195  if ( mpFileFooter->nSignature[ 0 ] == (('T'<<24)|('R'<<16)|('U'<<8)|'E') &&
196  mpFileFooter->nSignature[ 1 ] == (('V'<<24)|('I'<<16)|('S'<<8)|'I') &&
197  mpFileFooter->nSignature[ 2 ] == (('O'<<24)|('N'<<16)|('-'<<8)|'X') &&
198  mpFileFooter->nSignature[ 3 ] == (('F'<<24)|('I'<<16)|('L'<<8)|'E') )
199  {
200  mpExtension.reset( new TGAExtension );
201 
202  m_rTGA.Seek( mpFileFooter->nExtensionFileOffset );
203  m_rTGA.ReadUInt16( mpExtension->nExtensionSize );
204  if ( !m_rTGA.good())
205  return false;
206  if ( mpExtension->nExtensionSize >= SizeOfTGAExtension )
207  {
208  mnTGAVersion = 2;
209 
210  m_rTGA.ReadBytes(mpExtension->sAuthorName, 41);
211  m_rTGA.ReadBytes(mpExtension->sAuthorComment, 324);
212  m_rTGA.ReadBytes(mpExtension->sDateTimeStamp, 12);
213  m_rTGA.ReadBytes(mpExtension->sJobNameID, 12);
214  m_rTGA.ReadChar( mpExtension->sJobNameID[ 0 ] ).ReadChar( mpExtension->sJobNameID[ 1 ] ).ReadChar( mpExtension->sJobNameID[ 2 ] );
215  m_rTGA.ReadBytes(mpExtension->sSoftwareID, 41);
216  m_rTGA.ReadUInt16( mpExtension->nSoftwareVersionNumber ).ReadUChar( mpExtension->nSoftwareVersionLetter )
217  .ReadUInt32( mpExtension->nKeyColor ).ReadUInt16( mpExtension->nPixelAspectRatioNumerator )
218  .ReadUInt16( mpExtension->nPixelAspectRatioDeNumerator ).ReadUInt16( mpExtension->nGammaValueNumerator )
219  .ReadUInt16( mpExtension->nGammaValueDeNumerator ).ReadUInt32( mpExtension->nColorCorrectionOffset )
220  .ReadUInt32( mpExtension->nPostageStampOffset ).ReadUInt32( mpExtension->nScanLineOffset )
221  .ReadUChar( mpExtension->nAttributesType );
222 
223  if ( !m_rTGA.good())
224  return false;
225  }
226  }
227  m_rTGA.Seek( nCurStreamPos );
228 
229  // using the TGA file specification this was the correct form but adobe photoshop sets nImageDescriptor
230  // equal to nPixelDepth
231  // mnDestBitDepth = mpFileHeader->nPixelDepth - ( mpFileHeader->nImageDescriptor & 0xf );
232  mnDestBitDepth = mpFileHeader->nPixelDepth;
233 
234  if ( mnDestBitDepth == 8 ) // this is a patch for grayscale pictures not including a palette
235  mbIndexing = true;
236 
237  if ( mnDestBitDepth > 32 ) // maybe the pixeldepth is invalid
238  return false;
239  else if ( mnDestBitDepth > 8 )
240  mnDestBitDepth = 24;
241  else if ( mnDestBitDepth > 4 )
242  mnDestBitDepth = 8;
243  else if ( mnDestBitDepth > 2 )
244  mnDestBitDepth = 4;
245 
246  if ( !mbIndexing && ( mnDestBitDepth < 15 ) )
247  return false;
248 
249  switch ( mpFileHeader->nImageType )
250  {
251  case 9 : // encoding for colortype 9, 10, 11
252  case 10 :
253  case 11 :
254  mbEncoding = true;
255  break;
256  }
257 
258  if ( mpFileHeader->nImageIDLength ) // skip the Image ID
259  m_rTGA.SeekRel( mpFileHeader->nImageIDLength );
260 
261  return mbStatus;
262 }
263 
264 
265 bool TGAReader::ImplReadBody()
266 {
267 
268  sal_uInt16 nXCount, nYCount, nRGB16;
269  sal_uInt8 nRed, nGreen, nBlue, nRunCount, nDummy, nDepth;
270 
271  // this four variables match the image direction
272  tools::Long nY, nYAdd, nX, nXAdd, nXStart;
273 
274  nX = nXStart = nY = 0;
275  nXCount = nYCount = 0;
276  nYAdd = nXAdd = 1;
277 
278  if ( mpFileHeader->nImageDescriptor & 0x10 )
279  {
280  nX = nXStart = mpFileHeader->nImageWidth - 1;
281  nXAdd -= 2;
282  }
283 
284  if ( !(mpFileHeader->nImageDescriptor & 0x20 ) )
285  {
286  nY = mpFileHeader->nImageHeight - 1;
287  nYAdd -=2;
288  }
289 
290  nDepth = mpFileHeader->nPixelDepth;
291 
292  if ( mbEncoding )
293  {
294  if ( mbIndexing )
295  {
296  switch( nDepth )
297  {
298  // 16 bit encoding + indexing
299  case 16 :
300  while ( nYCount < mpFileHeader->nImageHeight )
301  {
302  m_rTGA.ReadUChar( nRunCount );
303  if ( !m_rTGA.good())
304  return false;
305  if ( nRunCount & 0x80 ) // a run length packet
306  {
307  m_rTGA.ReadUInt16( nRGB16 );
308  if ( nRGB16 >= mpFileHeader->nColorMapLength )
309  return false;
310  nRed = static_cast<sal_uInt8>( mpColorMap[ nRGB16 ] >> 16 );
311  nGreen = static_cast<sal_uInt8>( mpColorMap[ nRGB16 ] >> 8 );
312  nBlue = static_cast<sal_uInt8>( mpColorMap[ nRGB16 ] );
313  if ( !m_rTGA.good())
314  return false;
315  for ( sal_uInt16 i = 0; i < ( ( nRunCount & 0x7f ) + 1 ); i++ )
316  {
317  mpBitmap->SetPixel( nY, nX, Color( nRed, nGreen, nBlue ) );
318  nX += nXAdd;
319  nXCount++;
320  if ( nXCount == mpFileHeader->nImageWidth )
321  {
322  nX = nXStart;
323  nXCount = 0;
324  nY += nYAdd;
325  nYCount++;
326 
327  if( nYCount >= mpFileHeader->nImageHeight )
328  break;
329  }
330  }
331  }
332  else // a raw packet
333  {
334  for ( sal_uInt16 i = 0; i < ( ( nRunCount & 0x7f ) + 1 ); i++ )
335  {
336  m_rTGA.ReadUInt16( nRGB16 );
337  if ( !m_rTGA.good())
338  return false;
339  if ( nRGB16 >= mpFileHeader->nColorMapLength )
340  return false;
341  nRed = static_cast<sal_uInt8>( mpColorMap[ nRGB16 ] >> 16 );
342  nGreen = static_cast<sal_uInt8>( mpColorMap[ nRGB16 ] >> 8 );
343  nBlue = static_cast<sal_uInt8>( mpColorMap[ nRGB16 ] );
344  if ( !m_rTGA.good())
345  return false;
346  mpBitmap->SetPixel( nY, nX, Color( nRed, nGreen, nBlue ) );
347  nX += nXAdd;
348  nXCount++;
349  if ( nXCount == mpFileHeader->nImageWidth )
350  {
351  nX = nXStart;
352  nXCount = 0;
353  nY += nYAdd;
354  nYCount++;
355 
356  if( nYCount >= mpFileHeader->nImageHeight )
357  break;
358  }
359  }
360  }
361  }
362  break;
363 
364  // 8 bit encoding + indexing
365  case 8 :
366  while ( nYCount < mpFileHeader->nImageHeight )
367  {
368  m_rTGA.ReadUChar( nRunCount );
369  if ( !m_rTGA.good())
370  return false;
371  if ( nRunCount & 0x80 ) // a run length packet
372  {
373  m_rTGA.ReadUChar( nDummy );
374  if ( !m_rTGA.good())
375  return false;
376  if ( nDummy >= mpFileHeader->nColorMapLength )
377  return false;
378  for ( sal_uInt16 i = 0; i < ( ( nRunCount & 0x7f ) + 1 ); i++ )
379  {
380  mpBitmap->SetPixel( nY, nX, mvPalette[nDummy] );
381  nX += nXAdd;
382  nXCount++;
383  if ( nXCount == mpFileHeader->nImageWidth )
384  {
385  nX = nXStart;
386  nXCount = 0;
387  nY += nYAdd;
388  nYCount++;
389 
390  if( nYCount >= mpFileHeader->nImageHeight )
391  break;
392  }
393  }
394  }
395  else // a raw packet
396  {
397  for ( sal_uInt16 i = 0; i < ( ( nRunCount & 0x7f ) + 1 ); i++ )
398  {
399 
400  m_rTGA.ReadUChar( nDummy );
401  if ( !m_rTGA.good())
402  return false;
403  if ( nDummy >= mpFileHeader->nColorMapLength )
404  return false;
405  mpBitmap->SetPixel( nY, nX, mvPalette[nDummy] );
406  nX += nXAdd;
407  nXCount++;
408  if ( nXCount == mpFileHeader->nImageWidth )
409  {
410  nX = nXStart;
411  nXCount = 0;
412  nY += nYAdd;
413  nYCount++;
414 
415  if( nYCount >= mpFileHeader->nImageHeight )
416  break;
417  }
418  }
419  }
420  }
421  break;
422  default:
423  return false;
424  }
425  }
426  else
427  {
428  switch( nDepth )
429  {
430  // 32 bit transparent true color encoding
431  case 32 :
432  {
433  while ( nYCount < mpFileHeader->nImageHeight )
434  {
435  m_rTGA.ReadUChar( nRunCount );
436  if ( !m_rTGA.good())
437  return false;
438  if ( nRunCount & 0x80 ) // a run length packet
439  {
440  m_rTGA.ReadUChar( nBlue ).ReadUChar( nGreen ).ReadUChar( nRed ).ReadUChar( nDummy );
441  if ( !m_rTGA.good())
442  return false;
443  for ( sal_uInt16 i = 0; i < ( ( nRunCount & 0x7f ) + 1 ); i++ )
444  {
445  mpBitmap->SetPixel( nY, nX, Color( nRed, nGreen, nBlue ) );
446  nX += nXAdd;
447  nXCount++;
448  if ( nXCount == mpFileHeader->nImageWidth )
449  {
450  nX = nXStart;
451  nXCount = 0;
452  nY += nYAdd;
453  nYCount++;
454 
455  if( nYCount >= mpFileHeader->nImageHeight )
456  break;
457  }
458  }
459  }
460  else // a raw packet
461  {
462  for ( sal_uInt16 i = 0; i < ( ( nRunCount & 0x7f ) + 1 ); i++ )
463  {
464  m_rTGA.ReadUChar( nBlue ).ReadUChar( nGreen ).ReadUChar( nRed ).ReadUChar( nDummy );
465  if ( !m_rTGA.good())
466  return false;
467  mpBitmap->SetPixel( nY, nX, Color( nRed, nGreen, nBlue ) );
468  nX += nXAdd;
469  nXCount++;
470  if ( nXCount == mpFileHeader->nImageWidth )
471  {
472  nX = nXStart;
473  nXCount = 0;
474  nY += nYAdd;
475  nYCount++;
476 
477  if( nYCount >= mpFileHeader->nImageHeight )
478  break;
479  }
480  }
481  }
482  }
483  }
484  break;
485 
486  // 24 bit true color encoding
487  case 24 :
488  while ( nYCount < mpFileHeader->nImageHeight )
489  {
490  m_rTGA.ReadUChar( nRunCount );
491  if ( !m_rTGA.good())
492  return false;
493  if ( nRunCount & 0x80 ) // a run length packet
494  {
495  m_rTGA.ReadUChar( nBlue ).ReadUChar( nGreen ).ReadUChar( nRed );
496  if ( !m_rTGA.good())
497  return false;
498  for ( sal_uInt16 i = 0; i < ( ( nRunCount & 0x7f ) + 1 ); i++ )
499  {
500  mpBitmap->SetPixel( nY, nX, Color( nRed, nGreen, nBlue ) );
501  nX += nXAdd;
502  nXCount++;
503  if ( nXCount == mpFileHeader->nImageWidth )
504  {
505  nX = nXStart;
506  nXCount = 0;
507  nY += nYAdd;
508  nYCount++;
509 
510  if( nYCount >= mpFileHeader->nImageHeight )
511  break;
512  }
513  }
514  }
515  else // a raw packet
516  {
517  for ( sal_uInt16 i = 0; i < ( ( nRunCount & 0x7f ) + 1 ); i++ )
518  {
519  m_rTGA.ReadUChar( nBlue ).ReadUChar( nGreen ).ReadUChar( nRed );
520  if ( !m_rTGA.good())
521  return false;
522  mpBitmap->SetPixel( nY, nX, Color( nRed, nGreen, nBlue ) );
523  nX += nXAdd;
524  nXCount++;
525  if ( nXCount == mpFileHeader->nImageWidth )
526  {
527  nX = nXStart;
528  nXCount = 0;
529  nY += nYAdd;
530  nYCount++;
531 
532  if( nYCount >= mpFileHeader->nImageHeight )
533  break;
534  }
535  }
536  }
537  }
538  break;
539 
540  // 16 bit true color encoding
541  case 16 :
542  while ( nYCount < mpFileHeader->nImageHeight )
543  {
544  m_rTGA.ReadUChar( nRunCount );
545  if ( !m_rTGA.good())
546  return false;
547  if ( nRunCount & 0x80 ) // a run length packet
548  {
549  m_rTGA.ReadUInt16( nRGB16 );
550  if ( !m_rTGA.good())
551  return false;
552  nRed = static_cast<sal_uInt8>( nRGB16 >> 7 ) & 0xf8;
553  nGreen = static_cast<sal_uInt8>( nRGB16 >> 2 ) & 0xf8;
554  nBlue = static_cast<sal_uInt8>( nRGB16 << 3 ) & 0xf8;
555  for ( sal_uInt16 i = 0; i < ( ( nRunCount & 0x7f ) + 1 ); i++ )
556  {
557  mpBitmap->SetPixel( nY, nX, Color( nRed, nGreen, nBlue ) );
558  nX += nXAdd;
559  nXCount++;
560  if ( nXCount == mpFileHeader->nImageWidth )
561  {
562  nX = nXStart;
563  nXCount = 0;
564  nY += nYAdd;
565  nYCount++;
566 
567  if( nYCount >= mpFileHeader->nImageHeight )
568  break;
569  }
570  }
571  }
572  else // a raw packet
573  {
574  for ( sal_uInt16 i = 0; i < ( ( nRunCount & 0x7f ) + 1 ); i++ )
575  {
576  m_rTGA.ReadUInt16( nRGB16 );
577  if ( !m_rTGA.good())
578  return false;
579  nRed = static_cast<sal_uInt8>( nRGB16 >> 7 ) & 0xf8;
580  nGreen = static_cast<sal_uInt8>( nRGB16 >> 2 ) & 0xf8;
581  nBlue = static_cast<sal_uInt8>( nRGB16 << 3 ) & 0xf8;
582  mpBitmap->SetPixel( nY, nX, Color( nRed, nGreen, nBlue ) );
583  nX += nXAdd;
584  nXCount++;
585  if ( nXCount == mpFileHeader->nImageWidth )
586  {
587  nX = nXStart;
588  nXCount = 0;
589  nY += nYAdd;
590  nYCount++;
591 
592  if( nYCount >= mpFileHeader->nImageHeight )
593  break;
594  }
595  }
596  }
597  }
598  break;
599 
600  default:
601  return false;
602  }
603  }
604  }
605  else
606  {
607  for ( nYCount = 0; nYCount < mpFileHeader->nImageHeight; nYCount++, nY += nYAdd )
608  {
609  nX = nXStart;
610  nXCount = 0;
611 
612  if ( mbIndexing )
613  {
614  switch( nDepth )
615  {
616  // 16 bit indexing
617  case 16 :
618  for (;nXCount < mpFileHeader->nImageWidth; nXCount++, nX += nXAdd )
619  {
620  m_rTGA.ReadUInt16( nRGB16 );
621  if ( !m_rTGA.good())
622  return false;
623  if ( nRGB16 >= mpFileHeader->nColorMapLength )
624  return false;
625  nRed = static_cast<sal_uInt8>( mpColorMap[ nRGB16 ] >> 16 );
626  nGreen = static_cast<sal_uInt8>( mpColorMap[ nRGB16 ] >> 8 );
627  nBlue = static_cast<sal_uInt8>( mpColorMap[ nRGB16 ] );
628  mpBitmap->SetPixel( nY, nX, Color( nRed, nGreen, nBlue ) );
629  }
630  break;
631 
632  // 8 bit indexing
633  case 8 :
634  for (;nXCount < mpFileHeader->nImageWidth; nXCount++, nX += nXAdd )
635  {
636  m_rTGA.ReadUChar( nDummy );
637  if ( !m_rTGA.good())
638  return false;
639  if ( nDummy >= mpFileHeader->nColorMapLength )
640  return false;
641  mpBitmap->SetPixel( nY, nX, Color(ColorTransparency, nDummy) );
642  }
643  break;
644  default:
645  return false;
646  }
647  }
648  else
649  {
650  switch( nDepth )
651  {
652  // 32 bit true color
653  case 32 :
654  {
655  for (;nXCount < mpFileHeader->nImageWidth; nXCount++, nX += nXAdd )
656  {
657  m_rTGA.ReadUChar( nBlue ).ReadUChar( nGreen ).ReadUChar( nRed ).ReadUChar( nDummy );
658  if ( !m_rTGA.good())
659  return false;
660  mpBitmap->SetPixel( nY, nX, Color( nRed, nGreen, nBlue ) );
661  }
662  }
663  break;
664 
665  // 24 bit true color
666  case 24 :
667  for (;nXCount < mpFileHeader->nImageWidth; nXCount++, nX += nXAdd )
668  {
669  m_rTGA.ReadUChar( nBlue ).ReadUChar( nGreen ).ReadUChar( nRed );
670  if ( !m_rTGA.good())
671  return false;
672  mpBitmap->SetPixel( nY, nX, Color( nRed, nGreen, nBlue ) );
673  }
674  break;
675 
676  // 16 bit true color
677  case 16 :
678  for (;nXCount < mpFileHeader->nImageWidth; nXCount++, nX += nXAdd )
679  {
680  m_rTGA.ReadUInt16( nRGB16 );
681  if ( !m_rTGA.good())
682  return false;
683  nRed = static_cast<sal_uInt8>( nRGB16 >> 7 ) & 0xf8;
684  nGreen = static_cast<sal_uInt8>( nRGB16 >> 2 ) & 0xf8;
685  nBlue = static_cast<sal_uInt8>( nRGB16 << 3 ) & 0xf8;
686  mpBitmap->SetPixel( nY, nX, Color( nRed, nGreen, nBlue ) );
687  }
688  break;
689  default:
690  return false;
691  }
692  }
693  }
694  }
695  return mbStatus;
696 }
697 
698 
699 bool TGAReader::ImplReadPalette()
700 {
701  if ( mbIndexing ) // read the colormap
702  {
703  sal_uInt16 nColors = mpFileHeader->nColorMapLength;
704 
705  if ( !nColors ) // colors == 0 ? -> we will build a grayscale palette
706  {
707  if ( mpFileHeader->nPixelDepth != 8 )
708  return false;
709  nColors = 256;
710  mpFileHeader->nColorMapLength = 256;
711  mpFileHeader->nColorMapEntrySize = 0x3f; // patch for the following switch routine
712  }
713  mpColorMap.reset( new sal_uInt32[ nColors ] ); // we will always index dwords
714 
715  switch( mpFileHeader->nColorMapEntrySize )
716  {
717  case 0x3f :
718  {
719  for (sal_uInt32 i = 0; i < nColors; ++i)
720  {
721  mpColorMap[ i ] = ( i << 16 ) + ( i << 8 ) + i;
722  }
723  }
724  break;
725 
726  case 32 :
727  for (sal_uInt16 i = 0; i < nColors; i++)
728  {
729  m_rTGA.ReadUInt32(mpColorMap[i]);
730  }
731  break;
732 
733  case 24 :
734  {
735  for ( sal_uInt16 i = 0; i < nColors; i++ )
736  {
737  sal_uInt8 nBlue;
738  sal_uInt8 nGreen;
739  sal_uInt8 nRed;
740  m_rTGA.ReadUChar(nBlue).ReadUChar(nGreen).ReadUChar(nRed);
741  mpColorMap[i] = (nRed << 16) | (nGreen << 8) | nBlue;
742  }
743  }
744  break;
745 
746  case 15 :
747  case 16 :
748  {
749  for ( sal_uInt16 i = 0; i < nColors; i++ )
750  {
751  sal_uInt16 nTemp;
752  m_rTGA.ReadUInt16( nTemp );
753  if ( !m_rTGA.good() )
754  return false;
755  mpColorMap[ i ] = ( ( nTemp & 0x7c00 ) << 9 ) + ( ( nTemp & 0x01e0 ) << 6 ) +
756  ( ( nTemp & 0x1f ) << 3 );
757  }
758  }
759  break;
760 
761  default :
762  return false;
763  }
764  if ( mnDestBitDepth <= 8 )
765  {
766  sal_uInt16 nDestColors = ( 1 << mnDestBitDepth );
767  if ( nColors > nDestColors )
768  return false;
769 
770  mvPalette.resize( nColors );
771  for ( sal_uInt16 i = 0; i < nColors; i++ )
772  {
773  mvPalette[i] = Color( static_cast<sal_uInt8>( mpColorMap[ i ] >> 16 ),
774  static_cast<sal_uInt8>( mpColorMap[ i ] >> 8 ), static_cast<sal_uInt8>(mpColorMap[ i ] ) );
775  }
776  }
777  }
778 
779  return mbStatus;
780 }
781 
782 //================== GraphicImport - the exported function ================
783 
784 bool ImportTgaGraphic(SvStream & rStream, Graphic & rGraphic)
785 {
786  TGAReader aTGAReader(rStream);
787 
788  return aTGAReader.ReadTGA(rGraphic);
789 }
790 
791 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long Long
BitmapEx CreateFromData(sal_uInt8 const *pData, sal_Int32 nWidth, sal_Int32 nHeight, sal_Int32 nStride, vcl::PixelFormat ePixelFormat)
Copy block of image data into the bitmap.
FPDF_BITMAP mpBitmap
#define SizeOfTGAFileFooter
Definition: itga.cxx:49
int i
#define SizeOfTGAExtension
Definition: itga.cxx:60
Intended to be used to feed into CreateFromData to create a BitmapEx.
Definition: RawBitmap.hxx:21
unsigned char sal_uInt8
bool ImportTgaGraphic(SvStream &rStream, Graphic &rGraphic)
Definition: itga.cxx:784