nss: upgrade to release 3.73
[LibreOffice.git] / filter / source / graphicfilter / ipsd / ipsd.cxx
blob6e9ba1aa8438eaedf6b8d34aee0c115faefb467c
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 <vcl/outdev.hxx>
24 #include <sal/log.hxx>
25 #include <tools/fract.hxx>
26 #include <tools/helpers.hxx>
27 #include <tools/stream.hxx>
28 #include <memory>
30 class FilterConfigItem;
32 //============================ PSDReader ==================================
34 #define PSD_BITMAP 0
35 #define PSD_GRAYSCALE 1
36 #define PSD_INDEXED 2
37 #define PSD_RGB 3
38 #define PSD_CMYK 4
39 #define PSD_MULTICHANNEL 7
40 #define PSD_DUOTONE 8
41 #define PSD_LAB 9
43 namespace {
45 struct PSDFileHeader
47 sal_uInt32 nSignature;
48 sal_uInt16 nVersion;
49 sal_uInt32 nPad1;
50 sal_uInt16 nPad2;
51 sal_uInt16 nChannels;
52 sal_uInt32 nRows;
53 sal_uInt32 nColumns;
54 sal_uInt16 nDepth;
55 sal_uInt16 nMode;
58 class PSDReader {
60 private:
62 SvStream& m_rPSD; // the PSD file to be read in
63 std::unique_ptr<PSDFileHeader>
64 mpFileHeader;
66 sal_uInt32 mnXResFixed;
67 sal_uInt32 mnYResFixed;
69 bool mbStatus;
70 bool mbTransparent;
72 std::unique_ptr<vcl::bitmap::RawBitmap> mpBitmap;
73 std::vector<Color> mvPalette;
74 sal_uInt16 mnDestBitDepth;
75 bool mbCompression; // RLE decoding
76 std::unique_ptr<sal_uInt8[]>
77 mpPalette;
79 bool ImplReadBody();
80 bool ImplReadHeader();
82 public:
83 explicit PSDReader(SvStream &rStream);
84 bool ReadPSD(Graphic & rGraphic);
89 //=================== Methods of PSDReader ==============================
91 PSDReader::PSDReader(SvStream &rStream)
92 : m_rPSD(rStream)
93 , mnXResFixed(0)
94 , mnYResFixed(0)
95 , mbStatus(true)
96 , mbTransparent(false)
97 , mnDestBitDepth(0)
98 , mbCompression(false)
102 bool PSDReader::ReadPSD(Graphic & rGraphic )
104 if (m_rPSD.GetError())
105 return false;
107 m_rPSD.SetEndian( SvStreamEndian::BIG );
109 // read header:
111 if ( !ImplReadHeader() )
112 return false;
114 if (mbStatus)
116 sal_uInt32 nResult;
117 if (o3tl::checked_multiply(mpFileHeader->nColumns, mpFileHeader->nRows, nResult) || nResult > SAL_MAX_INT32/2/3)
118 return false;
121 Size aBitmapSize( mpFileHeader->nColumns, mpFileHeader->nRows );
122 mpBitmap.reset( new vcl::bitmap::RawBitmap( aBitmapSize, mbTransparent ? 32 : 24 ) );
123 if ( mpPalette && mbStatus )
125 mvPalette.resize( 256 );
126 for ( sal_uInt16 i = 0; i < 256; i++ )
128 mvPalette[i] = Color( mpPalette[ i ], mpPalette[ i + 256 ], mpPalette[ i + 512 ] );
132 if ((mnDestBitDepth == 1 || mnDestBitDepth == 8) && mvPalette.empty())
134 SAL_WARN("vcl", "no palette, but bit depth is " << mnDestBitDepth);
135 mbStatus = false;
136 return mbStatus;
139 // read bitmap data
140 if ( mbStatus && ImplReadBody() )
142 rGraphic = Graphic( vcl::bitmap::CreateFromData( std::move(*mpBitmap) ) );
144 if ( mnXResFixed && mnYResFixed )
146 Fraction aFractX( 1, mnXResFixed >> 16 );
147 Fraction aFractY( 1, mnYResFixed >> 16 );
148 MapMode aMapMode( MapUnit::MapInch, Point(), aFractX, aFractY );
149 Size aPrefSize = OutputDevice::LogicToLogic(aBitmapSize, aMapMode, MapMode(MapUnit::Map100thMM));
150 rGraphic.SetPrefSize( aPrefSize );
151 rGraphic.SetPrefMapMode( MapMode( MapUnit::Map100thMM ) );
154 else
155 mbStatus = false;
156 return mbStatus;
160 bool PSDReader::ImplReadHeader()
162 mpFileHeader.reset( new PSDFileHeader );
164 m_rPSD.ReadUInt32( mpFileHeader->nSignature ).ReadUInt16( mpFileHeader->nVersion ).ReadUInt32( mpFileHeader->nPad1 ). ReadUInt16( mpFileHeader->nPad2 ).ReadUInt16( mpFileHeader->nChannels ).ReadUInt32( mpFileHeader->nRows ). ReadUInt32( mpFileHeader->nColumns ).ReadUInt16( mpFileHeader->nDepth ).ReadUInt16( mpFileHeader->nMode );
166 if ( ( mpFileHeader->nSignature != 0x38425053 ) || ( mpFileHeader->nVersion != 1 ) )
167 return false;
169 if ( mpFileHeader->nRows == 0 || mpFileHeader->nColumns == 0 )
170 return false;
172 if ( ( mpFileHeader->nRows > 30000 ) || ( mpFileHeader->nColumns > 30000 ) )
173 return false;
175 sal_uInt16 nDepth = mpFileHeader->nDepth;
176 if (!( ( nDepth == 1 ) || ( nDepth == 8 ) || ( nDepth == 16 ) ) )
177 return false;
179 mnDestBitDepth = ( nDepth == 16 ) ? 8 : nDepth;
181 sal_uInt32 nColorLength(0);
182 m_rPSD.ReadUInt32( nColorLength );
183 if ( mpFileHeader->nMode == PSD_CMYK )
185 switch ( mpFileHeader->nChannels )
187 case 5 :
188 mbTransparent = true;
189 [[fallthrough]];
190 case 4 :
191 mnDestBitDepth = 24;
192 break;
193 default :
194 return false;
197 else switch ( mpFileHeader->nChannels )
199 case 2 :
200 mbTransparent = true;
201 break;
202 case 1 :
203 break;
204 case 4 :
205 mbTransparent = true;
206 [[fallthrough]];
207 case 3 :
208 mnDestBitDepth = 24;
209 break;
210 default:
211 return false;
214 switch ( mpFileHeader->nMode )
216 case PSD_BITMAP :
218 if ( nColorLength || ( nDepth != 1 ) )
219 return false;
221 break;
223 case PSD_INDEXED :
225 if ( nColorLength != 768 ) // we need the color map
226 return false;
227 mpPalette.reset( new sal_uInt8[ 768 ] );
228 m_rPSD.ReadBytes(mpPalette.get(), 768);
230 break;
232 case PSD_DUOTONE : // we'll handle the duotone color like a normal grayscale picture
233 m_rPSD.SeekRel( nColorLength );
234 nColorLength = 0;
235 [[fallthrough]];
236 case PSD_GRAYSCALE :
238 if ( nColorLength )
239 return false;
240 mpPalette.reset( new sal_uInt8[ 768 ] );
241 for ( sal_uInt16 i = 0; i < 256; i++ )
243 mpPalette[ i ] = mpPalette[ i + 256 ] = mpPalette[ i + 512 ] = static_cast<sal_uInt8>(i);
246 break;
248 case PSD_CMYK :
249 case PSD_RGB :
250 case PSD_MULTICHANNEL :
251 case PSD_LAB :
253 if ( nColorLength ) // color table is not supported by the other graphic modes
254 return false;
256 break;
258 default:
259 return false;
261 sal_uInt32 nResourceLength(0);
262 m_rPSD.ReadUInt32(nResourceLength);
263 if (nResourceLength > m_rPSD.remainingSize())
264 return false;
265 sal_uInt32 nLayerPos = m_rPSD.Tell() + nResourceLength;
267 // this is a loop over the resource entries to get the resolution info
268 while( m_rPSD.Tell() < nLayerPos )
270 sal_uInt32 nType(0);
271 sal_uInt16 nUniqueID(0);
272 sal_uInt8 n8(0);
273 m_rPSD.ReadUInt32(nType).ReadUInt16(nUniqueID).ReadUChar(n8);
274 if (nType != 0x3842494d)
275 break;
276 sal_uInt32 nPStringLen = n8;
277 if ( ! ( nPStringLen & 1 ) )
278 nPStringLen++;
279 m_rPSD.SeekRel( nPStringLen ); // skipping the pstring
280 sal_uInt32 nResEntryLen(0);
281 m_rPSD.ReadUInt32( nResEntryLen );
282 if ( nResEntryLen & 1 )
283 nResEntryLen++; // the resource entries are padded
284 sal_uInt32 nCurrentPos = m_rPSD.Tell();
285 if (nCurrentPos > nLayerPos || nResEntryLen > (nLayerPos - nCurrentPos)) // check if size
286 break; // is possible
287 switch( nUniqueID )
289 case 0x3ed : // UID for the resolution info
291 sal_Int16 nUnit;
293 m_rPSD.ReadUInt32( mnXResFixed ).ReadInt16( nUnit ).ReadInt16( nUnit )
294 .ReadUInt32( mnYResFixed ).ReadInt16( nUnit ).ReadInt16( nUnit );
296 break;
298 m_rPSD.Seek( nCurrentPos + nResEntryLen ); // set the stream to the next
299 } // resource entry
300 m_rPSD.Seek( nLayerPos );
301 sal_uInt32 nLayerMaskLength(0);
302 m_rPSD.ReadUInt32( nLayerMaskLength );
303 m_rPSD.SeekRel( nLayerMaskLength );
305 sal_uInt16 nCompression(0);
306 m_rPSD.ReadUInt16(nCompression);
307 if ( nCompression == 0 )
309 mbCompression = false;
311 else if ( nCompression == 1 )
313 m_rPSD.SeekRel( ( mpFileHeader->nRows * mpFileHeader->nChannels ) << 1 );
314 mbCompression = true;
316 else
317 return false;
319 return true;
322 namespace
324 const Color& SanitizePaletteIndex(std::vector<Color> const & rvPalette, sal_uInt8 nIndex)
326 if (nIndex >= rvPalette.size())
328 auto nSanitizedIndex = nIndex % rvPalette.size();
329 SAL_WARN_IF(nIndex != nSanitizedIndex, "filter.tga", "invalid colormap index: "
330 << static_cast<unsigned int>(nIndex) << ", colormap len is: "
331 << rvPalette.size());
332 nIndex = nSanitizedIndex;
334 return rvPalette[nIndex];
338 bool PSDReader::ImplReadBody()
340 sal_uInt32 nX, nY;
341 signed char nRunCount = 0;
342 sal_uInt8 nDat = 0, nDummy, nRed, nGreen, nBlue;
343 BitmapColor aBitmapColor;
344 nX = nY = 0;
346 switch ( mnDestBitDepth )
348 case 1 :
350 signed char nBitCount = -1;
351 while (nY < mpFileHeader->nRows && m_rPSD.good())
353 if ( nBitCount == -1 )
355 if ( mbCompression ) // else nRunCount = 0 -> so we use only single raw packets
357 char nTmp(0);
358 m_rPSD.ReadChar(nTmp);
359 nRunCount = nTmp;
362 if ( nRunCount & 0x80 ) // a run length packet
364 const sal_uInt16 nCount = -nRunCount + 1;
365 for (sal_uInt16 i = 0; i < nCount && m_rPSD.good(); ++i)
367 if ( nBitCount == -1 ) // bits left in nDat?
369 m_rPSD.ReadUChar( nDat );
370 nDat ^= 0xff;
371 nBitCount = 7;
373 mpBitmap->SetPixel(nY, nX, SanitizePaletteIndex(mvPalette, nDat >> nBitCount--));
374 if ( ++nX == mpFileHeader->nColumns )
376 nX = 0;
377 nY++;
378 nBitCount = -1;
379 if ( nY == mpFileHeader->nRows )
380 break;
384 else // a raw packet
386 const sal_uInt16 nCount = (nRunCount & 0x7f) + 1;
387 for (sal_uInt16 i = 0; i < nCount && m_rPSD.good(); ++i)
389 if ( nBitCount == -1 ) // bits left in nDat ?
391 m_rPSD.ReadUChar( nDat );
392 nDat ^= 0xff;
393 nBitCount = 7;
395 mpBitmap->SetPixel(nY, nX, SanitizePaletteIndex(mvPalette, nDat >> nBitCount--));
396 if ( ++nX == mpFileHeader->nColumns )
398 nX = 0;
399 nY++;
400 nBitCount = -1;
401 if ( nY == mpFileHeader->nRows )
402 break;
408 break;
410 case 8 :
412 while (nY < mpFileHeader->nRows && m_rPSD.good())
414 if ( mbCompression ) // else nRunCount = 0 -> so we use only single raw packets
416 char nTmp(0);
417 m_rPSD.ReadChar(nTmp);
418 nRunCount = nTmp;
421 if ( nRunCount & 0x80 ) // a run length packet
423 m_rPSD.ReadUChar( nDat );
424 if ( mpFileHeader->nDepth == 16 ) // 16 bit depth is to be skipped
425 m_rPSD.ReadUChar( nDummy );
426 const sal_uInt16 nCount = -nRunCount + 1;
427 for (sal_uInt16 i = 0; i < nCount && m_rPSD.good(); ++i)
429 mpBitmap->SetPixel(nY, nX, SanitizePaletteIndex(mvPalette, nDat));
430 if ( ++nX == mpFileHeader->nColumns )
432 nX = 0;
433 nY++;
434 if ( nY == mpFileHeader->nRows )
435 break;
439 else // a raw packet
441 const sal_uInt16 nCount = (nRunCount & 0x7f) + 1;
442 for (sal_uInt16 i = 0; i < nCount && m_rPSD.good(); ++i)
444 m_rPSD.ReadUChar( nDat );
445 if ( mpFileHeader->nDepth == 16 ) // 16 bit depth is to be skipped
446 m_rPSD.ReadUChar( nDummy );
447 mpBitmap->SetPixel(nY, nX, SanitizePaletteIndex(mvPalette, nDat));
448 if ( ++nX == mpFileHeader->nColumns )
450 nX = 0;
451 nY++;
452 if ( nY == mpFileHeader->nRows )
453 break;
459 break;
461 case 24 :
464 // the psd format is in plain order (RRRR GGGG BBBB) so we have to set each pixel three times
465 // maybe the format is CCCC MMMM YYYY KKKK
467 while (nY < mpFileHeader->nRows && m_rPSD.good())
469 if ( mbCompression ) // else nRunCount = 0 -> so we use only single raw packets
471 char nTmp(0);
472 m_rPSD.ReadChar(nTmp);
473 nRunCount = nTmp;
476 if ( nRunCount & 0x80 ) // a run length packet
478 m_rPSD.ReadUChar( nRed );
479 if ( mpFileHeader->nDepth == 16 ) // 16 bit depth is to be skipped
480 m_rPSD.ReadUChar( nDummy );
481 const sal_uInt16 nCount = -nRunCount + 1;
482 for (sal_uInt16 i = 0; i < nCount && m_rPSD.good(); ++i)
484 mpBitmap->SetPixel( nY, nX, Color( nRed, sal_uInt8(0), sal_uInt8(0) ) );
485 if ( ++nX == mpFileHeader->nColumns )
487 nX = 0;
488 nY++;
489 if ( nY == mpFileHeader->nRows )
490 break;
494 else // a raw packet
496 const sal_uInt16 nCount = (nRunCount & 0x7f) + 1;
497 for (sal_uInt16 i = 0; i < nCount && m_rPSD.good(); ++i)
499 m_rPSD.ReadUChar( nRed );
500 if ( mpFileHeader->nDepth == 16 ) // 16 bit depth is to be skipped
501 m_rPSD.ReadUChar( nDummy );
502 mpBitmap->SetPixel( nY, nX, Color( nRed, sal_uInt8(0), sal_uInt8(0) ) );
503 if ( ++nX == mpFileHeader->nColumns )
505 nX = 0;
506 nY++;
507 if ( nY == mpFileHeader->nRows )
508 break;
513 nY = 0;
514 while (nY < mpFileHeader->nRows && m_rPSD.good())
516 if ( mbCompression )
518 char nTmp(0);
519 m_rPSD.ReadChar(nTmp);
520 nRunCount = nTmp;
523 if ( nRunCount & 0x80 ) // a run length packet
525 m_rPSD.ReadUChar( nGreen );
526 if ( mpFileHeader->nDepth == 16 ) // 16 bit depth is to be skipped
527 m_rPSD.ReadUChar( nDummy );
528 const sal_uInt16 nCount = -nRunCount + 1;
529 for (sal_uInt16 i = 0; i < nCount && m_rPSD.good(); ++i)
531 aBitmapColor = mpBitmap->GetPixel( nY, nX );
532 mpBitmap->SetPixel( nY, nX, Color( aBitmapColor.GetRed(), nGreen, aBitmapColor.GetBlue() ) );
533 if ( ++nX == mpFileHeader->nColumns )
535 nX = 0;
536 nY++;
537 if ( nY == mpFileHeader->nRows )
538 break;
542 else // a raw packet
544 const sal_uInt16 nCount = (nRunCount & 0x7f) + 1;
545 for (sal_uInt16 i = 0; i < nCount && m_rPSD.good(); ++i)
547 m_rPSD.ReadUChar( nGreen );
548 if ( mpFileHeader->nDepth == 16 ) // 16 bit depth is to be skipped
549 m_rPSD.ReadUChar( nDummy );
550 aBitmapColor = mpBitmap->GetPixel( nY, nX );
551 mpBitmap->SetPixel( nY, nX, Color( aBitmapColor.GetRed(), nGreen, aBitmapColor.GetBlue() ) );
552 if ( ++nX == mpFileHeader->nColumns )
554 nX = 0;
555 nY++;
556 if ( nY == mpFileHeader->nRows )
557 break;
562 nY = 0;
563 while (nY < mpFileHeader->nRows && m_rPSD.good())
565 if ( mbCompression )
567 char nTmp(0);
568 m_rPSD.ReadChar(nTmp);
569 nRunCount = nTmp;
572 if ( nRunCount & 0x80 ) // a run length packet
574 m_rPSD.ReadUChar( nBlue );
575 if ( mpFileHeader->nDepth == 16 ) // 16 bit depth is to be skipped
576 m_rPSD.ReadUChar( nDummy );
577 const sal_uInt16 nCount = -nRunCount + 1;
578 for (sal_uInt16 i = 0; i < nCount && m_rPSD.good(); ++i)
580 aBitmapColor = mpBitmap->GetPixel( nY, nX );
581 mpBitmap->SetPixel( nY, nX, Color( aBitmapColor.GetRed(), aBitmapColor.GetGreen(), nBlue ) );
582 if ( ++nX == mpFileHeader->nColumns )
584 nX = 0;
585 nY++;
586 if ( nY == mpFileHeader->nRows )
587 break;
591 else // a raw packet
593 const sal_uInt16 nCount = (nRunCount & 0x7f) + 1;
594 for (sal_uInt16 i = 0; i < nCount && m_rPSD.good(); ++i)
596 m_rPSD.ReadUChar( nBlue );
597 if ( mpFileHeader->nDepth == 16 ) // 16 bit depth is to be skipped
598 m_rPSD.ReadUChar( nDummy );
599 aBitmapColor = mpBitmap->GetPixel( nY, nX );
600 mpBitmap->SetPixel( nY, nX, Color( aBitmapColor.GetRed(), aBitmapColor.GetGreen(), nBlue ) );
601 if ( ++nX == mpFileHeader->nColumns )
603 nX = 0;
604 nY++;
605 if ( nY == mpFileHeader->nRows )
606 break;
611 if (mpFileHeader->nMode == PSD_CMYK && m_rPSD.good())
613 sal_uInt32 nBlack, nBlackMax = 0;
614 std::vector<sal_uInt8> aBlack(mpFileHeader->nRows * mpFileHeader->nColumns, 0);
615 nY = 0;
616 while (nY < mpFileHeader->nRows && m_rPSD.good())
618 if ( mbCompression ) // else nRunCount = 0 -> so we use only single raw packets
620 char nTmp(0);
621 m_rPSD.ReadChar(nTmp);
622 nRunCount = nTmp;
625 if ( nRunCount & 0x80 ) // a run length packet
627 m_rPSD.ReadUChar( nDat );
629 if ( mpFileHeader->nDepth == 16 ) // 16 bit depth is to be skipped
630 m_rPSD.ReadUChar( nDummy );
632 for ( sal_uInt16 i = 0; i < ( -nRunCount + 1 ); i++ )
634 nBlack = mpBitmap->GetPixel( nY, nX ).GetRed() + nDat;
635 if ( nBlack > nBlackMax )
636 nBlackMax = nBlack;
637 nBlack = mpBitmap->GetPixel( nY, nX ).GetGreen() + nDat;
638 if ( nBlack > nBlackMax )
639 nBlackMax = nBlack;
640 nBlack = mpBitmap->GetPixel( nY, nX ).GetBlue() + nDat;
641 if ( nBlack > nBlackMax )
642 nBlackMax = nBlack;
643 aBlack[ nX + nY * mpFileHeader->nColumns ] = nDat ^ 0xff;
644 if ( ++nX == mpFileHeader->nColumns )
646 nX = 0;
647 nY++;
648 if ( nY == mpFileHeader->nRows )
649 break;
653 else // a raw packet
655 for ( sal_uInt16 i = 0; i < ( ( nRunCount & 0x7f ) + 1 ); i++ )
657 m_rPSD.ReadUChar( nDat );
659 if ( mpFileHeader->nDepth == 16 ) // 16 bit depth is to be skipped
660 m_rPSD.ReadUChar( nDummy );
661 nBlack = mpBitmap->GetPixel( nY, nX ).GetRed() + nDat;
662 if ( nBlack > nBlackMax )
663 nBlackMax = nBlack;
664 nBlack = mpBitmap->GetPixel( nY, nX ).GetGreen() + nDat;
665 if ( nBlack > nBlackMax )
666 nBlackMax = nBlack;
667 nBlack = mpBitmap->GetPixel( nY, nX ).GetBlue() + nDat;
668 if ( nBlack > nBlackMax )
669 nBlackMax = nBlack;
670 aBlack[ nX + nY * mpFileHeader->nColumns ] = nDat ^ 0xff;
671 if ( ++nX == mpFileHeader->nColumns )
673 nX = 0;
674 nY++;
675 if ( nY == mpFileHeader->nRows )
676 break;
682 for ( nY = 0; nY < mpFileHeader->nRows; nY++ )
684 for ( nX = 0; nX < mpFileHeader->nColumns; nX++ )
686 sal_Int32 nDAT = aBlack[ nX + nY * mpFileHeader->nColumns ] * ( nBlackMax - 256 ) / 0x1ff;
688 aBitmapColor = mpBitmap->GetPixel( nY, nX );
689 sal_uInt8 cR = static_cast<sal_uInt8>(MinMax( aBitmapColor.GetRed() - nDAT, 0, 255L ));
690 sal_uInt8 cG = static_cast<sal_uInt8>(MinMax( aBitmapColor.GetGreen() - nDAT, 0, 255L ));
691 sal_uInt8 cB = static_cast<sal_uInt8>(MinMax( aBitmapColor.GetBlue() - nDAT, 0, 255L ));
692 mpBitmap->SetPixel( nY, nX, Color( cR, cG, cB ) );
697 break;
700 if (mbTransparent && m_rPSD.good())
702 // the psd is 24 or 8 bit grafix + alphachannel
704 nY = nX = 0;
705 while ( nY < mpFileHeader->nRows )
707 if ( mbCompression ) // else nRunCount = 0 -> so we use only single raw packets
709 char nTmp(0);
710 m_rPSD.ReadChar(nTmp);
711 nRunCount = nTmp;
714 if ( nRunCount & 0x80 ) // a run length packet
716 m_rPSD.ReadUChar( nDat );
717 if ( nDat )
718 nDat = 0;
719 else
720 nDat = 1;
721 if ( mpFileHeader->nDepth == 16 ) // 16 bit depth is to be skipped
722 m_rPSD.ReadUChar( nDummy );
723 for ( sal_uInt16 i = 0; i < ( -nRunCount + 1 ); i++ )
725 mpBitmap->SetAlpha(nY, nX, nDat ? 0 : 255);
726 if ( ++nX == mpFileHeader->nColumns )
728 nX = 0;
729 nY++;
730 if ( nY == mpFileHeader->nRows )
731 break;
735 else // a raw packet
737 for ( sal_uInt16 i = 0; i < ( ( nRunCount & 0x7f ) + 1 ); i++ )
739 m_rPSD.ReadUChar( nDat );
740 if ( nDat )
741 nDat = 0;
742 else
743 nDat = 1;
744 if ( mpFileHeader->nDepth == 16 ) // 16 bit depth is to be skipped
745 m_rPSD.ReadUChar( nDummy );
746 mpBitmap->SetAlpha(nY, nX, nDat ? 0 : 255);
747 if ( ++nX == mpFileHeader->nColumns )
749 nX = 0;
750 nY++;
751 if ( nY == mpFileHeader->nRows )
752 break;
759 return m_rPSD.good();
762 //================== GraphicImport - the exported function ================
764 extern "C" SAL_DLLPUBLIC_EXPORT bool
765 ipdGraphicImport( SvStream & rStream, Graphic & rGraphic, FilterConfigItem* )
767 PSDReader aPSDReader(rStream);
769 return aPSDReader.ReadPSD(rGraphic);
773 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */