cid#1640468 Dereference after null check
[LibreOffice.git] / vcl / source / filter / itga / itga.cxx
blob6b3b3037fe240beb27efbf8b14b9d6aa1f1bd0dc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <vcl/graph.hxx>
22 #include <vcl/BitmapTools.hxx>
23 #include <tools/stream.hxx>
24 #include <memory>
25 #include <filter/TgaReader.hxx>
27 class FilterConfigItem;
29 //============================ TGAReader ==================================
31 namespace {
33 struct TGAFileHeader
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;
49 #define SizeOfTGAFileFooter 26
51 struct TGAFileFooter
53 sal_uInt32 nExtensionFileOffset;
54 sal_uInt32 nDeveloperDirectoryOffset;
55 sal_uInt32 nSignature[4];
56 sal_uInt8 nPadByte;
57 sal_uInt8 nStringTerminator;
60 #define SizeOfTGAExtension 495
62 struct TGAExtension
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;
83 class TGAReader {
85 private:
87 SvStream& m_rTGA;
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;
100 bool mbStatus;
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
107 bool ImplReadHeader();
108 bool ImplReadPalette();
109 bool ImplReadBody();
111 public:
112 explicit TGAReader(SvStream &rTGA);
113 bool ReadTGA(Graphic &rGraphic);
118 //=================== Methods of TGAReader ==============================
120 TGAReader::TGAReader(SvStream &rTGA)
121 : m_rTGA(rTGA)
122 , mbStatus(true)
123 , mnTGAVersion(1)
124 , mnDestBitDepth(8)
125 , mbIndexing(false)
126 , mbEncoding(false)
130 bool TGAReader::ReadTGA(Graphic & rGraphic)
132 if ( m_rTGA.GetError() )
133 return false;
135 m_rTGA.SetEndian( SvStreamEndian::LITTLE );
137 // Kopf einlesen:
139 if ( !m_rTGA.GetError() )
141 mbStatus = ImplReadHeader();
142 if (mbStatus)
143 mbStatus = mpFileHeader->nImageWidth && mpFileHeader->nImageHeight;
144 if (mbStatus)
146 sal_Size nSize = mpFileHeader->nImageWidth;
147 nSize *= mpFileHeader->nImageHeight;
148 if (nSize > SAL_MAX_INT32/2/3)
149 return false;
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();
157 if ( mbStatus )
158 rGraphic = vcl::bitmap::CreateFromData(std::move(*mpBitmap));
161 return mbStatus;
165 bool TGAReader::ImplReadHeader()
167 mpFileHeader.reset( new TGAFileHeader );
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 );
171 if ( !m_rTGA.good())
172 return false;
174 if ( mpFileHeader->nColorMapType > 1 )
175 return false;
176 if ( mpFileHeader->nColorMapType == 1 )
177 mbIndexing = true;
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
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 );
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 );
191 if ( !m_rTGA.good())
192 return false;
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') )
200 mpExtension.reset( new TGAExtension );
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 )
208 mnTGAVersion = 2;
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 );
223 if ( !m_rTGA.good())
224 return false;
227 m_rTGA.Seek( nCurStreamPos );
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;
234 if ( mnDestBitDepth == 8 ) // this is a patch for grayscale pictures not including a palette
235 mbIndexing = true;
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;
246 if ( !mbIndexing && ( mnDestBitDepth < 15 ) )
247 return false;
249 switch ( mpFileHeader->nImageType )
251 case 9 : // encoding for colortype 9, 10, 11
252 case 10 :
253 case 11 :
254 mbEncoding = true;
255 break;
258 if ( mpFileHeader->nImageIDLength ) // skip the Image ID
259 m_rTGA.SeekRel( mpFileHeader->nImageIDLength );
261 return mbStatus;
265 bool TGAReader::ImplReadBody()
268 sal_uInt16 nXCount, nYCount, nRGB16;
269 sal_uInt8 nRed, nGreen, nBlue, nRunCount, nDummy, nDepth;
271 // this four variables match the image direction
272 tools::Long nY, nYAdd, nX, nXAdd, nXStart;
274 nX = nXStart = nY = 0;
275 nXCount = nYCount = 0;
276 nYAdd = nXAdd = 1;
278 if ( mpFileHeader->nImageDescriptor & 0x10 )
280 nX = nXStart = mpFileHeader->nImageWidth - 1;
281 nXAdd -= 2;
284 if ( !(mpFileHeader->nImageDescriptor & 0x20 ) )
286 nY = mpFileHeader->nImageHeight - 1;
287 nYAdd -=2;
290 nDepth = mpFileHeader->nPixelDepth;
292 if ( mbEncoding )
294 if ( mbIndexing )
296 switch( nDepth )
298 // 16 bit encoding + indexing
299 case 16 :
300 while ( nYCount < mpFileHeader->nImageHeight )
302 m_rTGA.ReadUChar( nRunCount );
303 if ( !m_rTGA.good())
304 return false;
305 if ( nRunCount & 0x80 ) // a run length packet
307 m_rTGA.ReadUInt16( nRGB16 );
308 if (!m_rTGA.good())
309 return false;
310 if ( nRGB16 >= mpFileHeader->nColorMapLength )
311 return false;
312 nRed = static_cast<sal_uInt8>( mpColorMap[ nRGB16 ] >> 16 );
313 nGreen = static_cast<sal_uInt8>( mpColorMap[ nRGB16 ] >> 8 );
314 nBlue = static_cast<sal_uInt8>( mpColorMap[ nRGB16 ] );
315 for ( sal_uInt16 i = 0; i < ( ( nRunCount & 0x7f ) + 1 ); i++ )
317 mpBitmap->SetPixel( nY, nX, Color( nRed, nGreen, nBlue ) );
318 nX += nXAdd;
319 nXCount++;
320 if ( nXCount == mpFileHeader->nImageWidth )
322 nX = nXStart;
323 nXCount = 0;
324 nY += nYAdd;
325 nYCount++;
327 if( nYCount >= mpFileHeader->nImageHeight )
328 break;
332 else // a raw packet
334 for ( sal_uInt16 i = 0; i < ( ( nRunCount & 0x7f ) + 1 ); i++ )
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 )
351 nX = nXStart;
352 nXCount = 0;
353 nY += nYAdd;
354 nYCount++;
356 if( nYCount >= mpFileHeader->nImageHeight )
357 break;
362 break;
364 // 8 bit encoding + indexing
365 case 8 :
366 while ( nYCount < mpFileHeader->nImageHeight )
368 m_rTGA.ReadUChar( nRunCount );
369 if ( !m_rTGA.good())
370 return false;
371 if ( nRunCount & 0x80 ) // a run length packet
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++ )
380 mpBitmap->SetPixel( nY, nX, mvPalette[nDummy] );
381 nX += nXAdd;
382 nXCount++;
383 if ( nXCount == mpFileHeader->nImageWidth )
385 nX = nXStart;
386 nXCount = 0;
387 nY += nYAdd;
388 nYCount++;
390 if( nYCount >= mpFileHeader->nImageHeight )
391 break;
395 else // a raw packet
397 for ( sal_uInt16 i = 0; i < ( ( nRunCount & 0x7f ) + 1 ); i++ )
399 m_rTGA.ReadUChar( nDummy );
400 if ( !m_rTGA.good())
401 return false;
402 if ( nDummy >= mpFileHeader->nColorMapLength )
403 return false;
404 mpBitmap->SetPixel( nY, nX, mvPalette[nDummy] );
405 nX += nXAdd;
406 nXCount++;
407 if ( nXCount == mpFileHeader->nImageWidth )
409 nX = nXStart;
410 nXCount = 0;
411 nY += nYAdd;
412 nYCount++;
414 if( nYCount >= mpFileHeader->nImageHeight )
415 break;
420 break;
421 default:
422 return false;
425 else
427 switch( nDepth )
429 // 32 bit transparent true color encoding
430 case 32 :
432 while ( nYCount < mpFileHeader->nImageHeight )
434 m_rTGA.ReadUChar( nRunCount );
435 if ( !m_rTGA.good())
436 return false;
437 if ( nRunCount & 0x80 ) // a run length packet
439 m_rTGA.ReadUChar( nBlue ).ReadUChar( nGreen ).ReadUChar( nRed ).ReadUChar( nDummy );
440 if ( !m_rTGA.good())
441 return false;
442 for ( sal_uInt16 i = 0; i < ( ( nRunCount & 0x7f ) + 1 ); i++ )
444 mpBitmap->SetPixel( nY, nX, Color( nRed, nGreen, nBlue ) );
445 nX += nXAdd;
446 nXCount++;
447 if ( nXCount == mpFileHeader->nImageWidth )
449 nX = nXStart;
450 nXCount = 0;
451 nY += nYAdd;
452 nYCount++;
454 if( nYCount >= mpFileHeader->nImageHeight )
455 break;
459 else // a raw packet
461 for ( sal_uInt16 i = 0; i < ( ( nRunCount & 0x7f ) + 1 ); i++ )
463 m_rTGA.ReadUChar( nBlue ).ReadUChar( nGreen ).ReadUChar( nRed ).ReadUChar( nDummy );
464 if ( !m_rTGA.good())
465 return false;
466 mpBitmap->SetPixel( nY, nX, Color( nRed, nGreen, nBlue ) );
467 nX += nXAdd;
468 nXCount++;
469 if ( nXCount == mpFileHeader->nImageWidth )
471 nX = nXStart;
472 nXCount = 0;
473 nY += nYAdd;
474 nYCount++;
476 if( nYCount >= mpFileHeader->nImageHeight )
477 break;
483 break;
485 // 24 bit true color encoding
486 case 24 :
487 while ( nYCount < mpFileHeader->nImageHeight )
489 m_rTGA.ReadUChar( nRunCount );
490 if ( !m_rTGA.good())
491 return false;
492 if ( nRunCount & 0x80 ) // a run length packet
494 m_rTGA.ReadUChar( nBlue ).ReadUChar( nGreen ).ReadUChar( nRed );
495 if ( !m_rTGA.good())
496 return false;
497 for ( sal_uInt16 i = 0; i < ( ( nRunCount & 0x7f ) + 1 ); i++ )
499 mpBitmap->SetPixel( nY, nX, Color( nRed, nGreen, nBlue ) );
500 nX += nXAdd;
501 nXCount++;
502 if ( nXCount == mpFileHeader->nImageWidth )
504 nX = nXStart;
505 nXCount = 0;
506 nY += nYAdd;
507 nYCount++;
509 if( nYCount >= mpFileHeader->nImageHeight )
510 break;
514 else // a raw packet
516 for ( sal_uInt16 i = 0; i < ( ( nRunCount & 0x7f ) + 1 ); i++ )
518 m_rTGA.ReadUChar( nBlue ).ReadUChar( nGreen ).ReadUChar( nRed );
519 if ( !m_rTGA.good())
520 return false;
521 mpBitmap->SetPixel( nY, nX, Color( nRed, nGreen, nBlue ) );
522 nX += nXAdd;
523 nXCount++;
524 if ( nXCount == mpFileHeader->nImageWidth )
526 nX = nXStart;
527 nXCount = 0;
528 nY += nYAdd;
529 nYCount++;
531 if( nYCount >= mpFileHeader->nImageHeight )
532 break;
537 break;
539 // 16 bit true color encoding
540 case 16 :
541 while ( nYCount < mpFileHeader->nImageHeight )
543 m_rTGA.ReadUChar( nRunCount );
544 if ( !m_rTGA.good())
545 return false;
546 if ( nRunCount & 0x80 ) // a run length packet
548 m_rTGA.ReadUInt16( nRGB16 );
549 if ( !m_rTGA.good())
550 return false;
551 nRed = static_cast<sal_uInt8>( nRGB16 >> 7 ) & 0xf8;
552 nGreen = static_cast<sal_uInt8>( nRGB16 >> 2 ) & 0xf8;
553 nBlue = static_cast<sal_uInt8>( nRGB16 << 3 ) & 0xf8;
554 for ( sal_uInt16 i = 0; i < ( ( nRunCount & 0x7f ) + 1 ); i++ )
556 mpBitmap->SetPixel( nY, nX, Color( nRed, nGreen, nBlue ) );
557 nX += nXAdd;
558 nXCount++;
559 if ( nXCount == mpFileHeader->nImageWidth )
561 nX = nXStart;
562 nXCount = 0;
563 nY += nYAdd;
564 nYCount++;
566 if( nYCount >= mpFileHeader->nImageHeight )
567 break;
571 else // a raw packet
573 for ( sal_uInt16 i = 0; i < ( ( nRunCount & 0x7f ) + 1 ); i++ )
575 m_rTGA.ReadUInt16( nRGB16 );
576 if ( !m_rTGA.good())
577 return false;
578 nRed = static_cast<sal_uInt8>( nRGB16 >> 7 ) & 0xf8;
579 nGreen = static_cast<sal_uInt8>( nRGB16 >> 2 ) & 0xf8;
580 nBlue = static_cast<sal_uInt8>( nRGB16 << 3 ) & 0xf8;
581 mpBitmap->SetPixel( nY, nX, Color( nRed, nGreen, nBlue ) );
582 nX += nXAdd;
583 nXCount++;
584 if ( nXCount == mpFileHeader->nImageWidth )
586 nX = nXStart;
587 nXCount = 0;
588 nY += nYAdd;
589 nYCount++;
591 if( nYCount >= mpFileHeader->nImageHeight )
592 break;
597 break;
599 default:
600 return false;
604 else
606 for ( nYCount = 0; nYCount < mpFileHeader->nImageHeight; nYCount++, nY += nYAdd )
608 nX = nXStart;
609 nXCount = 0;
611 if ( mbIndexing )
613 switch( nDepth )
615 // 16 bit indexing
616 case 16 :
617 for (;nXCount < mpFileHeader->nImageWidth; nXCount++, nX += nXAdd )
619 m_rTGA.ReadUInt16( nRGB16 );
620 if ( !m_rTGA.good())
621 return false;
622 if ( nRGB16 >= mpFileHeader->nColorMapLength )
623 return false;
624 nRed = static_cast<sal_uInt8>( mpColorMap[ nRGB16 ] >> 16 );
625 nGreen = static_cast<sal_uInt8>( mpColorMap[ nRGB16 ] >> 8 );
626 nBlue = static_cast<sal_uInt8>( mpColorMap[ nRGB16 ] );
627 mpBitmap->SetPixel( nY, nX, Color( nRed, nGreen, nBlue ) );
629 break;
631 // 8 bit indexing
632 case 8 :
633 for (;nXCount < mpFileHeader->nImageWidth; nXCount++, nX += nXAdd )
635 m_rTGA.ReadUChar( nDummy );
636 if ( !m_rTGA.good())
637 return false;
638 if ( nDummy >= mpFileHeader->nColorMapLength )
639 return false;
640 mpBitmap->SetPixel( nY, nX, Color(ColorTransparency, nDummy) );
642 break;
643 default:
644 return false;
647 else
649 switch( nDepth )
651 // 32 bit true color
652 case 32 :
654 for (;nXCount < mpFileHeader->nImageWidth; nXCount++, nX += nXAdd )
656 m_rTGA.ReadUChar( nBlue ).ReadUChar( nGreen ).ReadUChar( nRed ).ReadUChar( nDummy );
657 if ( !m_rTGA.good())
658 return false;
659 mpBitmap->SetPixel( nY, nX, Color( nRed, nGreen, nBlue ) );
662 break;
664 // 24 bit true color
665 case 24 :
666 for (;nXCount < mpFileHeader->nImageWidth; nXCount++, nX += nXAdd )
668 m_rTGA.ReadUChar( nBlue ).ReadUChar( nGreen ).ReadUChar( nRed );
669 if ( !m_rTGA.good())
670 return false;
671 mpBitmap->SetPixel( nY, nX, Color( nRed, nGreen, nBlue ) );
673 break;
675 // 16 bit true color
676 case 16 :
677 for (;nXCount < mpFileHeader->nImageWidth; nXCount++, nX += nXAdd )
679 m_rTGA.ReadUInt16( nRGB16 );
680 if ( !m_rTGA.good())
681 return false;
682 nRed = static_cast<sal_uInt8>( nRGB16 >> 7 ) & 0xf8;
683 nGreen = static_cast<sal_uInt8>( nRGB16 >> 2 ) & 0xf8;
684 nBlue = static_cast<sal_uInt8>( nRGB16 << 3 ) & 0xf8;
685 mpBitmap->SetPixel( nY, nX, Color( nRed, nGreen, nBlue ) );
687 break;
688 default:
689 return false;
694 return mbStatus;
698 bool TGAReader::ImplReadPalette()
700 if ( mbIndexing ) // read the colormap
702 sal_uInt16 nColors = mpFileHeader->nColorMapLength;
704 if ( !nColors ) // colors == 0 ? -> we will build a grayscale palette
706 if ( mpFileHeader->nPixelDepth != 8 )
707 return false;
708 nColors = 256;
709 mpFileHeader->nColorMapLength = 256;
710 mpFileHeader->nColorMapEntrySize = 0x3f; // patch for the following switch routine
712 mpColorMap.reset( new sal_uInt32[ nColors ] ); // we will always index dwords
714 switch( mpFileHeader->nColorMapEntrySize )
716 case 0x3f :
718 for (sal_uInt32 i = 0; i < nColors; ++i)
720 mpColorMap[ i ] = ( i << 16 ) + ( i << 8 ) + i;
723 break;
725 case 32 :
726 for (sal_uInt16 i = 0; i < nColors; i++)
728 m_rTGA.ReadUInt32(mpColorMap[i]);
730 break;
732 case 24 :
734 for ( sal_uInt16 i = 0; i < nColors; i++ )
736 sal_uInt8 nBlue;
737 sal_uInt8 nGreen;
738 sal_uInt8 nRed;
739 m_rTGA.ReadUChar(nBlue).ReadUChar(nGreen).ReadUChar(nRed);
740 mpColorMap[i] = (nRed << 16) | (nGreen << 8) | nBlue;
743 break;
745 case 15 :
746 case 16 :
748 for ( sal_uInt16 i = 0; i < nColors; i++ )
750 sal_uInt16 nTemp;
751 m_rTGA.ReadUInt16( nTemp );
752 if ( !m_rTGA.good() )
753 return false;
754 mpColorMap[ i ] = ( ( nTemp & 0x7c00 ) << 9 ) + ( ( nTemp & 0x01e0 ) << 6 ) +
755 ( ( nTemp & 0x1f ) << 3 );
758 break;
760 default :
761 return false;
763 if ( mnDestBitDepth <= 8 )
765 sal_uInt16 nDestColors = ( 1 << mnDestBitDepth );
766 if ( nColors > nDestColors )
767 return false;
769 mvPalette.resize( nColors );
770 for ( sal_uInt16 i = 0; i < nColors; i++ )
772 mvPalette[i] = Color( static_cast<sal_uInt8>( mpColorMap[ i ] >> 16 ),
773 static_cast<sal_uInt8>( mpColorMap[ i ] >> 8 ), static_cast<sal_uInt8>(mpColorMap[ i ] ) );
778 return mbStatus;
781 //================== GraphicImport - the exported function ================
783 bool ImportTgaGraphic(SvStream & rStream, Graphic & rGraphic)
785 TGAReader aTGAReader(rStream);
787 return aTGAReader.ReadTGA(rGraphic);
790 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */