LibreOffice Module vcl (master)  1
ipict.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 #include <filter/PictReader.hxx>
21 #include <string.h>
22 #include <osl/thread.h>
23 #include <sal/log.hxx>
24 #include <vcl/BitmapTools.hxx>
25 #include <vcl/graph.hxx>
26 #include <vcl/gdimtf.hxx>
27 #include <tools/poly.hxx>
28 #include <tools/fract.hxx>
29 #include <tools/stream.hxx>
30 #include <vcl/virdev.hxx>
31 #include <math.h>
32 #include "shape.hxx"
33 #include <memory>
34 
35 #include <vcl/FilterConfigItem.hxx>
36  // complete FilterConfigItem for GraphicImport under -fsanitize=function
37 
38 namespace PictReaderInternal {
39  namespace {
40 
42  class Pattern {
43  public:
45  Pattern() : penStyle(PEN_SOLID),
46  brushStyle(BRUSH_SOLID),
47  nBitCount(64),
48  isColor(false),
49  isRead(false)
50  {}
51 
53  sal_uInt8 read(SvStream &stream);
55  void setColor(Color col) { isColor = true; color = col; }
61  Color getColor(Color bkColor, Color fgColor) const {
62  if (isColor) return color;
63  // we create a gray pattern from nBitCount
64  double alpha = nBitCount / 64.0;
65  return Color(sal_uInt8(alpha*fgColor.GetRed()+(1.0-alpha)*bkColor.GetRed()),
66  sal_uInt8(alpha*fgColor.GetGreen()+(1.0-alpha)*bkColor.GetGreen()),
67  sal_uInt8(alpha*fgColor.GetBlue()+(1.0-alpha)*bkColor.GetBlue()));
68  }
69 
71  bool isDefault() const { return !isRead; }
72 
73  enum PenStyle { PEN_NULL, PEN_SOLID, PEN_DOT, PEN_DASH, PEN_DASHDOT };
74  enum BrushStyle { BRUSH_SOLID, BRUSH_HORZ, BRUSH_VERT,
75  BRUSH_CROSS, BRUSH_DIAGCROSS, BRUSH_UPDIAG, BRUSH_DOWNDIAG,
76  BRUSH_25, BRUSH_50, BRUSH_75 };
77  // Data
79  enum BrushStyle brushStyle;
80  short nBitCount;
81 
82  bool isColor; // true if it is a color pattern
84 
85  protected:
86  // flag to know if the pattern came from reading the picture, or if it is the default pattern
87  bool isRead;
88  };
89 
90  }
91 
92  sal_uInt8 Pattern::read(SvStream &stream) {
93  unsigned char nbyte[8] = {0};
94  isColor = false;
95 
96  // count the no of bits in pattern which are set to 1:
97  nBitCount=0;
98  for (unsigned char & ny : nbyte) {
99  stream.ReadChar( reinterpret_cast<char&>(ny) );
100  for (short nx=0; nx<8; nx++) {
101  if ( (ny & (1<<nx)) != 0 ) nBitCount++;
102  }
103  }
104 
105  // store pattern in 2 long words:
106  sal_uInt32 nHiBytes = (((((static_cast<sal_uInt32>(nbyte[0])<<8)|
107  static_cast<sal_uInt32>(nbyte[1]))<<8)|
108  static_cast<sal_uInt32>(nbyte[2]))<<8)|
109  static_cast<sal_uInt32>(nbyte[3]);
110  sal_uInt32 nLoBytes = (((((static_cast<sal_uInt32>(nbyte[4])<<8)|
111  static_cast<sal_uInt32>(nbyte[5]))<<8)|
112  static_cast<sal_uInt32>(nbyte[6]))<<8)|
113  static_cast<sal_uInt32>(nbyte[7]);
114 
115  // create a PenStyle:
116  if (nBitCount<=0) penStyle=PEN_NULL;
117  else if (nBitCount<=16) penStyle=PEN_DOT;
118  else if (nBitCount<=32) penStyle=PEN_DASHDOT;
119  else if (nBitCount<=48) penStyle=PEN_DASH;
120  else penStyle=PEN_SOLID;
121 
122  // create a BrushStyle:
123  if (nHiBytes==0xffffffff && nLoBytes==0xffffffff) brushStyle=BRUSH_SOLID;
124  else if (nHiBytes==0xff000000 && nLoBytes==0x00000000) brushStyle=BRUSH_HORZ;
125  else if (nHiBytes==0x80808080 && nLoBytes==0x80808080) brushStyle=BRUSH_VERT;
126  else if (nHiBytes==0xff808080 && nLoBytes==0x80808080) brushStyle=BRUSH_CROSS;
127  else if (nHiBytes==0x01824428 && nLoBytes==0x10284482) brushStyle=BRUSH_DIAGCROSS;
128  else if (nHiBytes==0x80402010 && nLoBytes==0x08040201) brushStyle=BRUSH_UPDIAG;
129  else if (nHiBytes==0x01020408 && nLoBytes==0x10204080) brushStyle=BRUSH_DOWNDIAG;
130  else if (nBitCount<=24) brushStyle=BRUSH_25;
131  else if (nBitCount<=40) brushStyle=BRUSH_50;
132  else if (nBitCount<=56) brushStyle=BRUSH_75;
133  else brushStyle=BRUSH_SOLID;
134 
135  isRead = true;
136 
137  return 8;
138  }
139 }
140 
141 //============================ PictReader ==================================
142 
143 namespace {
144 
145 enum class PictDrawingMethod {
146  FRAME, PAINT, ERASE, INVERT, FILL,
147  TEXT, UNDEFINED
148 };
149 
150 class PictReader {
151  typedef class PictReaderInternal::Pattern Pattern;
152 private:
153 
154  SvStream * pPict; // The Pict file to read.
155  VclPtr<VirtualDevice> pVirDev; // Here the drawing method will be called.
156  // A recording into the GDIMetaFile will take place.
157 
158  sal_uInt64 nOrigPos; // Initial position in pPict.
159  bool IsVersion2; // If it is a version 2 Pictfile.
160  tools::Rectangle aBoundingRect; // Min/Max-Rectangle for the whole drawing.
161 
162  Point aPenPosition;
163  Point aTextPosition;
164  Color aActForeColor;
165  Color aActBackColor;
166  Pattern eActPenPattern;
167  Pattern eActFillPattern;
168  Pattern eActBackPattern;
169  Size nActPenSize;
170  // Note: Postscript mode is stored by setting eActRop to RasterOp::N1
171  RasterOp eActROP;
172  PictDrawingMethod eActMethod;
173  Size aActOvalSize;
174  vcl::Font aActFont;
175 
176  Fraction aHRes;
177  Fraction aVRes;
178 
179  Point ReadPoint();
180 
181  Point ReadDeltaH(Point aBase);
182  Point ReadDeltaV(Point aBase);
183 
184  Point ReadUnsignedDeltaH(Point aBase);
185  Point ReadUnsignedDeltaV(Point aBase);
186 
187  Size ReadSize();
188 
189  Color ReadColor();
190 
191  Color ReadRGBColor();
192 
193  void ReadRectangle(tools::Rectangle & rRect);
194 
195  sal_uInt64 ReadPolygon(tools::Polygon & rPoly);
196 
197  sal_uInt64 ReadPixPattern(Pattern &pattern);
198 
199  tools::Rectangle aLastRect;
200  sal_uInt8 ReadAndDrawRect(PictDrawingMethod eMethod);
201  sal_uInt8 ReadAndDrawSameRect(PictDrawingMethod eMethod);
202 
203  tools::Rectangle aLastRoundRect;
204  sal_uInt8 ReadAndDrawRoundRect(PictDrawingMethod eMethod);
205  sal_uInt8 ReadAndDrawSameRoundRect(PictDrawingMethod eMethod);
206 
207  tools::Rectangle aLastOval;
208  sal_uInt8 ReadAndDrawOval(PictDrawingMethod eMethod);
209  sal_uInt8 ReadAndDrawSameOval(PictDrawingMethod eMethod);
210 
211  tools::Polygon aLastPolygon;
212  sal_uInt64 ReadAndDrawPolygon(PictDrawingMethod eMethod);
213  sal_uInt8 ReadAndDrawSamePolygon(PictDrawingMethod eMethod);
214 
215  tools::Rectangle aLastArcRect;
216  sal_uInt8 ReadAndDrawArc(PictDrawingMethod eMethod);
217  sal_uInt8 ReadAndDrawSameArc(PictDrawingMethod eMethod);
218 
219  sal_uInt64 ReadAndDrawRgn(PictDrawingMethod eMethod);
220  sal_uInt8 ReadAndDrawSameRgn(PictDrawingMethod eMethod);
221 
222  // returns true if there's no need to print the shape/text/frame
223  bool IsInvisible( PictDrawingMethod eMethod ) const {
224  if ( eActROP == RasterOp::N1 ) return true;
225  if ( eMethod == PictDrawingMethod::FRAME && nActPenSize.IsEmpty() ) return true;
226  return false;
227  }
228 
229  void DrawingMethod(PictDrawingMethod eMethod);
230 
231  sal_uInt64 ReadAndDrawText();
232 
233  sal_uInt64 ReadPixMapEtc(BitmapEx & rBitmap, bool bBaseAddr, bool bColorTable,
234  tools::Rectangle * pSrcRect, tools::Rectangle * pDestRect,
235  bool bMode, bool bMaskRgn);
236 
237  void ReadHeader();
238  // Reads the header of the Pict file, set IsVersion and aBoundingRect
239 
240  sal_uInt64 ReadData(sal_uInt16 nOpcode);
241  // Reads the date of anOopcode and executes the operation.
242  // The number of data bytes belonging to the opcode will be returned
243  // in any case.
244 
245  void SetLineColor( const Color& rColor );
246  void SetFillColor( const Color& rColor );
247 
248  // OSNOLA: returns the text encoding which must be used for system id
249  static rtl_TextEncoding GetTextEncoding (sal_uInt16 fId = 0xFFFF);
250 
251 public:
252 
253  PictReader()
254  : pPict(nullptr)
255  , pVirDev(nullptr)
256  , nOrigPos(0)
257  , IsVersion2(false)
258  , eActROP(RasterOp::OverPaint)
259  , eActMethod(PictDrawingMethod::UNDEFINED)
260  {
261  aActFont.SetCharSet(GetTextEncoding());
262  }
263 
264  void ReadPict( SvStream & rStreamPict, GDIMetaFile & rGDIMetaFile );
265  // reads a pict file from the stream and fills the GDIMetaFile
266 
267 };
268 
269 }
270 
271 static void SetByte(sal_uInt16& nx, sal_uInt16 ny, vcl::bitmap::RawBitmap& rBitmap, sal_uInt16 nPixelSize, sal_uInt8 nDat, sal_uInt16 nWidth, std::vector<Color> const & rvPalette)
272 {
273  switch (nPixelSize)
274  {
275  case 1:
276  rBitmap.SetPixel(ny, nx++, rvPalette[(nDat >> 7) & 1]);
277  if ( nx == nWidth ) break;
278  rBitmap.SetPixel(ny, nx++, rvPalette[(nDat >> 6) & 1]);
279  if ( nx == nWidth ) break;
280  rBitmap.SetPixel(ny, nx++, rvPalette[(nDat >> 5) & 1]);
281  if ( nx == nWidth ) break;
282  rBitmap.SetPixel(ny, nx++, rvPalette[(nDat >> 4) & 1]);
283  if ( nx == nWidth ) break;
284  rBitmap.SetPixel(ny, nx++, rvPalette[(nDat >> 3) & 1]);
285  if ( nx == nWidth ) break;
286  rBitmap.SetPixel(ny, nx++, rvPalette[(nDat >> 2) & 1]);
287  if ( nx == nWidth ) break;
288  rBitmap.SetPixel(ny, nx++, rvPalette[(nDat >> 1) & 1]);
289  if ( nx == nWidth ) break;
290  rBitmap.SetPixel(ny, nx++, rvPalette[nDat & 1]);
291  break;
292  case 2:
293  rBitmap.SetPixel(ny, nx++, rvPalette[nDat >> 6]);
294  if ( nx == nWidth ) break;
295  rBitmap.SetPixel(ny, nx++, rvPalette[(nDat>>4)&3]);
296  if ( nx == nWidth ) break;
297  rBitmap.SetPixel(ny, nx++, rvPalette[(nDat>>2)&3]);
298  if ( nx == nWidth ) break;
299  rBitmap.SetPixel(ny, nx++, rvPalette[nDat & 3]);
300  break;
301  case 4:
302  rBitmap.SetPixel(ny, nx++, rvPalette[nDat >> 4]);
303  if ( nx == nWidth ) break;
304  rBitmap.SetPixel(ny, nx++, rvPalette[nDat & 0x0f]);
305  break;
306  case 8:
307  rBitmap.SetPixel(ny, nx++, rvPalette[nDat]);
308  break;
309  }
310 }
311 
312 //=================== methods of PictReader ==============================
313 rtl_TextEncoding PictReader::GetTextEncoding (sal_uInt16 fId) {
314  static rtl_TextEncoding enc = []()
315  {
316  rtl_TextEncoding def = osl_getThreadTextEncoding();
317  // we keep osl_getThreadTextEncoding only if it is a mac encoding
318  switch(def) {
319  case RTL_TEXTENCODING_APPLE_ROMAN:
320  case RTL_TEXTENCODING_APPLE_ARABIC:
321  case RTL_TEXTENCODING_APPLE_CENTEURO:
322  case RTL_TEXTENCODING_APPLE_CROATIAN:
323  case RTL_TEXTENCODING_APPLE_CYRILLIC:
324  case RTL_TEXTENCODING_APPLE_DEVANAGARI:
325  case RTL_TEXTENCODING_APPLE_FARSI:
326  case RTL_TEXTENCODING_APPLE_GREEK:
327  case RTL_TEXTENCODING_APPLE_GUJARATI:
328  case RTL_TEXTENCODING_APPLE_GURMUKHI:
329  case RTL_TEXTENCODING_APPLE_HEBREW:
330  case RTL_TEXTENCODING_APPLE_ICELAND:
331  case RTL_TEXTENCODING_APPLE_ROMANIAN:
332  case RTL_TEXTENCODING_APPLE_THAI:
333  case RTL_TEXTENCODING_APPLE_TURKISH:
334  case RTL_TEXTENCODING_APPLE_UKRAINIAN:
335  case RTL_TEXTENCODING_APPLE_CHINSIMP:
336  case RTL_TEXTENCODING_APPLE_CHINTRAD:
337  case RTL_TEXTENCODING_APPLE_JAPANESE:
338  case RTL_TEXTENCODING_APPLE_KOREAN:
339  return def; break;
340  default:
341  break;
342  }
343  return RTL_TEXTENCODING_APPLE_ROMAN;
344  }();
345  if (fId == 13) return RTL_TEXTENCODING_ADOBE_DINGBATS; // CHECKME
346  if (fId == 23) return RTL_TEXTENCODING_ADOBE_SYMBOL;
347  return enc;
348 }
349 
350 void PictReader::SetLineColor( const Color& rColor )
351 {
352  pVirDev->SetLineColor( rColor );
353 }
354 
355 void PictReader::SetFillColor( const Color& rColor )
356 {
357  pVirDev->SetFillColor( rColor );
358 }
359 
360 Point PictReader::ReadPoint()
361 {
362  short nx,ny;
363 
364  pPict->ReadInt16( ny ).ReadInt16( nx );
365 
366  Point aPoint( static_cast<tools::Long>(nx) - aBoundingRect.Left(),
367  static_cast<tools::Long>(ny) - aBoundingRect.Top() );
368 
369  SAL_INFO("filter.pict", "ReadPoint: " << aPoint);
370  return aPoint;
371 }
372 
373 Point PictReader::ReadDeltaH(Point aBase)
374 {
375  signed char ndh;
376 
377  pPict->ReadChar( reinterpret_cast<char&>(ndh) );
378 
379  return Point( aBase.X() + static_cast<tools::Long>(ndh), aBase.Y() );
380 }
381 
382 Point PictReader::ReadDeltaV(Point aBase)
383 {
384  signed char ndv;
385 
386  pPict->ReadChar( reinterpret_cast<char&>(ndv) );
387 
388  return Point( aBase.X(), aBase.Y() + static_cast<tools::Long>(ndv) );
389 }
390 
391 Point PictReader::ReadUnsignedDeltaH(Point aBase)
392 {
393  sal_uInt8 ndh;
394 
395  pPict->ReadUChar( ndh );
396 
397  return Point( aBase.X() + static_cast<tools::Long>(ndh), aBase.Y() );
398 }
399 
400 Point PictReader::ReadUnsignedDeltaV(Point aBase)
401 {
402  sal_uInt8 ndv;
403 
404  pPict->ReadUChar( ndv );
405 
406  return Point( aBase.X(), aBase.Y() + static_cast<tools::Long>(ndv) );
407 }
408 
409 Size PictReader::ReadSize()
410 {
411  short nx,ny;
412 
413  pPict->ReadInt16( ny ).ReadInt16( nx );
414 
415  return Size( static_cast<tools::Long>(nx), static_cast<tools::Long>(ny) );
416 }
417 
418 Color PictReader::ReadColor()
419 {
420  Color aCol;
421 
422  sal_uInt32 nCol(0);
423  pPict->ReadUInt32( nCol );
424  switch (nCol)
425  {
426  case 33: aCol=COL_BLACK; break;
427  case 30: aCol=COL_WHITE; break;
428  case 205: aCol=COL_LIGHTRED; break;
429  case 341: aCol=COL_LIGHTGREEN; break;
430  case 409: aCol=COL_LIGHTBLUE; break;
431  case 273: aCol=COL_LIGHTCYAN; break;
432  case 137: aCol=COL_LIGHTMAGENTA; break;
433  case 69: aCol=COL_YELLOW; break;
434  default: aCol=COL_LIGHTGRAY;
435  }
436  return aCol;
437 }
438 
439 Color PictReader::ReadRGBColor()
440 {
441  sal_uInt16 nR, nG, nB;
442 
443  pPict->ReadUInt16( nR ).ReadUInt16( nG ).ReadUInt16( nB );
444  return Color( static_cast<sal_uInt8>( nR >> 8 ), static_cast<sal_uInt8>( nG >> 8 ), static_cast<sal_uInt8>( nB >> 8 ) );
445 }
446 
447 void PictReader::ReadRectangle(tools::Rectangle & rRect)
448 {
449  Point aTopLeft = ReadPoint();
450  Point aBottomRight = ReadPoint();
451  if (!pPict->good() || aTopLeft.X() > aBottomRight.X() || aTopLeft.Y() > aBottomRight.Y())
452  {
453  SAL_WARN("filter.pict", "broken rectangle");
455  rRect = tools::Rectangle();
456  return;
457  }
458  rRect=tools::Rectangle(aTopLeft,aBottomRight);
459 
460  SAL_INFO("filter.pict", "ReadRectangle: " << rRect);
461 }
462 
463 sal_uInt64 PictReader::ReadPolygon(tools::Polygon & rPoly)
464 {
465  sal_uInt16 nSize(0);
466  pPict->ReadUInt16(nSize);
467  pPict->SeekRel(8);
468  sal_uInt64 nDataSize = static_cast<sal_uInt64>(nSize);
469  nSize=(nSize-10)/4;
470  const size_t nMaxPossiblePoints = pPict->remainingSize() / 2 * sizeof(sal_uInt16);
471  if (nSize > nMaxPossiblePoints)
472  {
473  SAL_WARN("filter.pict", "pict record claims to have: " << nSize << " points, but only " << nMaxPossiblePoints << " possible, clamping");
474  nSize = nMaxPossiblePoints;
475  }
476  rPoly.SetSize(nSize);
477  for (sal_uInt16 i = 0; i < nSize; ++i)
478  {
479  rPoly.SetPoint(ReadPoint(), i);
480  if (!pPict->good())
481  {
482  rPoly.SetSize(i);
483  break;
484  }
485  }
486  return nDataSize;
487 }
488 
489 sal_uInt64 PictReader::ReadPixPattern(PictReader::Pattern &pattern)
490 {
491  // Don't know if this is correct because no picture which contains PixPatterns found.
492  // Here again the attempt to calculate the size of the date to create simple StarView-Styles
493  // from them. Luckily a PixPattern always contains a normal pattern.
494 
495 
496  sal_uInt64 nDataSize;
497  sal_uInt16 nPatType;
498  BitmapEx aBMP;
499 
500  pPict->ReadUInt16( nPatType );
501  if (nPatType==1) {
502  pattern.read(*pPict);
503  nDataSize=ReadPixMapEtc(aBMP,false,true,nullptr,nullptr,false,false);
504  // CHANGEME: use average pixmap colors to update the pattern, ...
505  if (nDataSize!=0xffffffff) nDataSize+=10;
506  }
507  else if (nPatType==2) {
508  pattern.read(*pPict);
509  // RGBColor
510  sal_uInt16 nR, nG, nB;
511  pPict->ReadUInt16( nR ).ReadUInt16( nG ).ReadUInt16( nB );
512  Color col(static_cast<sal_uInt8>( nR >> 8 ), static_cast<sal_uInt8>( nG >> 8 ), static_cast<sal_uInt8>( nB >> 8 ) );
513  pattern.setColor(col);
514  nDataSize=16;
515  }
516  else nDataSize=0xffffffff;
517 
518  return nDataSize;
519 }
520 
521 sal_uInt8 PictReader::ReadAndDrawRect(PictDrawingMethod eMethod)
522 {
523  ReadRectangle(aLastRect);
524  ReadAndDrawSameRect(eMethod);
525  return 8;
526 }
527 
528 sal_uInt8 PictReader::ReadAndDrawSameRect(PictDrawingMethod eMethod)
529 {
530  if (IsInvisible(eMethod)) return 0;
531  DrawingMethod(eMethod);
532  PictReaderShape::drawRectangle( pVirDev, eMethod == PictDrawingMethod::FRAME, aLastRect, nActPenSize );
533  return 0;
534 }
535 
536 sal_uInt8 PictReader::ReadAndDrawRoundRect(PictDrawingMethod eMethod)
537 {
538  ReadRectangle(aLastRoundRect);
539  ReadAndDrawSameRoundRect(eMethod);
540  return 8;
541 }
542 
543 sal_uInt8 PictReader::ReadAndDrawSameRoundRect(PictDrawingMethod eMethod)
544 {
545  if (IsInvisible(eMethod)) return 0;
546  DrawingMethod(eMethod);
547  PictReaderShape::drawRoundRectangle( pVirDev, eMethod == PictDrawingMethod::FRAME, aLastRoundRect, aActOvalSize, nActPenSize );
548  return 0;
549 }
550 
551 sal_uInt8 PictReader::ReadAndDrawOval(PictDrawingMethod eMethod)
552 {
553  ReadRectangle(aLastOval);
554  ReadAndDrawSameOval(eMethod);
555  return 8;
556 }
557 
558 sal_uInt8 PictReader::ReadAndDrawSameOval(PictDrawingMethod eMethod)
559 {
560  if (IsInvisible(eMethod)) return 0;
561  DrawingMethod(eMethod);
562  PictReaderShape::drawEllipse( pVirDev, eMethod == PictDrawingMethod::FRAME, aLastOval, nActPenSize );
563  return 0;
564 }
565 
566 sal_uInt64 PictReader::ReadAndDrawPolygon(PictDrawingMethod eMethod)
567 {
568  sal_uInt64 nDataSize;
569  nDataSize=ReadPolygon(aLastPolygon);
570  ReadAndDrawSamePolygon(eMethod);
571  return nDataSize;
572 }
573 
574 sal_uInt8 PictReader::ReadAndDrawSamePolygon(PictDrawingMethod eMethod)
575 {
576  if (IsInvisible(eMethod)) return 0;
577  DrawingMethod(eMethod);
578  PictReaderShape::drawPolygon( pVirDev, eMethod == PictDrawingMethod::FRAME, aLastPolygon, nActPenSize );
579  return 0;
580 }
581 
582 
583 sal_uInt8 PictReader::ReadAndDrawArc(PictDrawingMethod eMethod)
584 {
585  ReadRectangle(aLastArcRect);
586  ReadAndDrawSameArc(eMethod);
587  return 12;
588 }
589 
590 sal_uInt8 PictReader::ReadAndDrawSameArc(PictDrawingMethod eMethod)
591 {
592  short nstartAngle, narcAngle;
593 
594  pPict->ReadInt16( nstartAngle ).ReadInt16( narcAngle );
595  if (!pPict->good() || IsInvisible(eMethod)) return 4;
596  DrawingMethod(eMethod);
597 
598  if (narcAngle<0) {
599  nstartAngle = nstartAngle + narcAngle;
600  narcAngle=-narcAngle;
601  }
602  const double pi = 2 * acos(0.0);
603  double fAng1 = static_cast<double>(nstartAngle) * pi / 180.0;
604  double fAng2 = static_cast<double>(nstartAngle + narcAngle) * pi / 180.0;
605  PictReaderShape::drawArc( pVirDev, eMethod == PictDrawingMethod::FRAME, aLastArcRect, fAng1, fAng2, nActPenSize );
606  return 4;
607 }
608 
609 sal_uInt64 PictReader::ReadAndDrawRgn(PictDrawingMethod eMethod)
610 {
611  sal_uInt16 nSize;
612 
613  pPict->ReadUInt16( nSize );
614  // read the DATA
615  //
616  // a region data is a mask and is probably coded as
617  // - the first 8 bytes: bdbox ( which can be read by ReadRectangle )
618  // - then a list of line modifiers: y_i, a_0, b_0, a_1, b_1, ..., a_{n_i}, b_{n_i}, 0x7fff
619  // - 0x7fff
620  // where y_i is the increasing sequences of line coordinates
621  // and on each line: a0 < b0 < a1 < b1 < ... < a_{n_i} < b_{n_i}
622 
623  // it can be probably decoded as :
624  // M=an empty mask: ie. (0, 0, ... ) with (left_box-right_box+1) zeroes
625  // then for each line (y_i):
626  // - takes M and inverts all values in [a_0,b_0-1], in [a_1,b_1-1] ...
627  // - sets M = new y_i line mask
628  ReadAndDrawSameRgn(eMethod);
629  return static_cast<sal_uInt64>(nSize);
630 }
631 
632 sal_uInt8 PictReader::ReadAndDrawSameRgn(PictDrawingMethod eMethod)
633 {
634  if (IsInvisible(eMethod)) return 0;
635  DrawingMethod(eMethod);
636  // DISPLAY: ...???...
637  return 0;
638 }
639 
640 void PictReader::DrawingMethod(PictDrawingMethod eMethod)
641 {
642  if( eActMethod==eMethod ) return;
643  switch (eMethod) {
644  case PictDrawingMethod::FRAME:
645  if (eActPenPattern.isDefault())
646  SetLineColor( aActForeColor );
647  else
648  SetLineColor(eActPenPattern.getColor(aActBackColor, aActForeColor));
649  SetFillColor( COL_TRANSPARENT );
650  pVirDev->SetRasterOp(eActROP);
651  break;
652  case PictDrawingMethod::PAINT:
653  SetLineColor( COL_TRANSPARENT );
654  if (eActPenPattern.isDefault())
655  SetFillColor( aActForeColor );
656  else
657  SetFillColor(eActPenPattern.getColor(aActBackColor, aActForeColor));
658  pVirDev->SetRasterOp(eActROP);
659  break;
660  case PictDrawingMethod::ERASE:
661  SetLineColor( COL_TRANSPARENT );
662  if (eActBackPattern.isDefault())
663  SetFillColor( aActBackColor );// Osnola: previously aActForeColor
664  else // checkMe
665  SetFillColor(eActBackPattern.getColor(COL_BLACK, aActBackColor));
667  break;
668  case PictDrawingMethod::INVERT: // checkme
669  SetLineColor( COL_TRANSPARENT);
670  SetFillColor( COL_BLACK );
671  pVirDev->SetRasterOp(RasterOp::Invert);
672  break;
673  case PictDrawingMethod::FILL:
674  SetLineColor( COL_TRANSPARENT );
675  if (eActFillPattern.isDefault())
676  SetFillColor( aActForeColor );
677  else
678  SetFillColor(eActFillPattern.getColor(aActBackColor, aActForeColor));
680  break;
681  case PictDrawingMethod::TEXT:
682  aActFont.SetColor(aActForeColor);
683  aActFont.SetFillColor(aActBackColor);
684  aActFont.SetTransparent(true);
685  pVirDev->SetFont(aActFont);
687  break;
688  default:
689  break; // -Wall undefined not handled...
690  }
691  eActMethod=eMethod;
692 }
693 
694 sal_uInt64 PictReader::ReadAndDrawText()
695 {
696  char sText[256];
697 
698  char nByteLen(0);
699  pPict->ReadChar(nByteLen);
700  sal_uInt32 nLen = static_cast<sal_uInt32>(nByteLen)&0x000000ff;
701  sal_uInt32 nDataLen = nLen + 1;
702  nLen = pPict->ReadBytes(&sText, nLen);
703 
704  if (IsInvisible( PictDrawingMethod::TEXT )) return nDataLen;
705  DrawingMethod( PictDrawingMethod::TEXT );
706 
707  // remove annoying control characters:
708  while ( nLen > 0 && static_cast<unsigned char>(sText[ nLen - 1 ]) < 32 )
709  nLen--;
710  sText[ nLen ] = 0;
711  OUString aString( sText, strlen(sText), aActFont.GetCharSet());
712  pVirDev->DrawText( Point( aTextPosition.X(), aTextPosition.Y() ), aString );
713  return nDataLen;
714 }
715 
716 sal_uInt64 PictReader::ReadPixMapEtc( BitmapEx &rBitmap, bool bBaseAddr, bool bColorTable, tools::Rectangle* pSrcRect,
717  tools::Rectangle* pDestRect, bool bMode, bool bMaskRgn )
718 {
719  std::unique_ptr<vcl::bitmap::RawBitmap> pBitmap;
720  sal_uInt16 nPackType(0), nPixelSize(0), nCmpCount(0), nCmpSize(0);
721  sal_uInt8 nDat(0), nRed(0), nGreen(0), nBlue(0);
722 
723  // The calculation of nDataSize is considering the size of the whole data.
724  size_t nDataSize = 0;
725 
726  // conditionally skip BaseAddr
727  if ( bBaseAddr )
728  {
729  pPict->SeekRel( 4 );
730  nDataSize += 4;
731  }
732 
733  // Read PixMap or Bitmap structure;
734  sal_uInt16 nRowBytes(0), nBndX(0), nBndY(0), nWidth(0), nHeight(0);
735  pPict->ReadUInt16(nRowBytes).ReadUInt16(nBndY).ReadUInt16(nBndX).ReadUInt16(nHeight).ReadUInt16(nWidth);
736  if (nBndY > nHeight)
737  return 0xffffffff;
738  nHeight = nHeight - nBndY;
739  if (nHeight == 0)
740  return 0xffffffff;
741  if (nBndX > nWidth)
742  return 0xffffffff;
743  nWidth = nWidth - nBndX;
744  if (nWidth == 0)
745  return 0xffffffff;
746 
747  std::vector<Color> aPalette;
748  const bool bNotMonoChrome = (nRowBytes & 0x8000) != 0;
749  if (bNotMonoChrome)
750  { // it is a PixMap
751  nRowBytes &= 0x3fff;
752  sal_uInt16 nVersion;
753  sal_uInt32 nPackSize;
754  sal_uInt16 nPixelType;
755  sal_uInt32 nPlaneBytes;
756  sal_uInt32 nHRes, nVRes;
757  pPict->ReadUInt16( nVersion ).ReadUInt16( nPackType ).ReadUInt32( nPackSize ).ReadUInt32( nHRes ).ReadUInt32( nVRes ).ReadUInt16( nPixelType ).ReadUInt16( nPixelSize ).ReadUInt16( nCmpCount ).ReadUInt16( nCmpSize ).ReadUInt32( nPlaneBytes );
758 
759  pPict->SeekRel( 8 );
760  nDataSize += 46;
761 
762  if ( bColorTable )
763  {
764  pPict->SeekRel( 6 );
765  sal_uInt16 nColTabSize(0);
766  pPict->ReadUInt16(nColTabSize);
767 
768  if (nColTabSize > 255)
769  return 0xffffffff;
770 
771  ++nColTabSize;
772 
773  aPalette.resize(nColTabSize);
774 
775  for (size_t i = 0; i < nColTabSize; ++i)
776  {
777  pPict->SeekRel(2);
778  sal_uInt8 nDummy;
779  pPict->ReadUChar( nRed ).ReadUChar( nDummy ).ReadUChar( nGreen ).ReadUChar( nDummy ).ReadUChar( nBlue ).ReadUChar( nDummy );
780  aPalette[i] = Color(nRed, nGreen, nBlue);
781  }
782 
783  nDataSize += 8 + nColTabSize * 8;
784  }
785  }
786  else
787  {
788  nRowBytes &= 0x3fff;
789  nPixelSize = nCmpCount = nCmpSize = 1;
790  nDataSize += 10;
791  aPalette.resize(2);
792  aPalette[0] = Color(0xff, 0xff, 0xff);
793  aPalette[1] = Color(0, 0, 0);
794  }
795 
796  // conditionally read source rectangle:
797  if ( pSrcRect != nullptr)
798  {
799  sal_uInt16 nTop, nLeft, nBottom, nRight;
800  pPict->ReadUInt16( nTop ).ReadUInt16( nLeft ).ReadUInt16( nBottom ).ReadUInt16( nRight );
801  *pSrcRect = tools::Rectangle(nLeft, nTop, nRight, nBottom);
802  nDataSize += 8;
803  }
804 
805  // conditionally read destination rectangle:
806  if ( pDestRect != nullptr )
807  {
808  Point aTL, aBR;
809  aTL = ReadPoint();
810  aBR = ReadPoint();
811  *pDestRect = tools::Rectangle( aTL, aBR );
812  nDataSize += 8;
813  }
814 
815  // conditionally read mode (or skip it):
816  if ( bMode )
817  {
818  pPict->SeekRel(2);
819  nDataSize += 2;
820  }
821 
822  // conditionally read region (or skip it):
823  if ( bMaskRgn )
824  {
825  sal_uInt16 nSize;
826  pPict->ReadUInt16( nSize );
827  pPict->SeekRel( nSize - 2 );
828  nDataSize += nSize;
829  }
830 
831  // read and write Bitmap bits:
832  if ( nPixelSize == 1 || nPixelSize == 2 || nPixelSize == 4 || nPixelSize == 8 )
833  {
834  sal_uInt16 nSrcBPL, nDestBPL;
835  size_t nCount;
836 
837  if ( nPixelSize == 1 ) nSrcBPL = ( nWidth + 7 ) >> 3;
838  else if ( nPixelSize == 2 ) nSrcBPL = ( nWidth + 3 ) >> 2;
839  else if ( nPixelSize == 4 ) nSrcBPL = ( nWidth + 1 ) >> 1;
840  else nSrcBPL = nWidth;
841  nDestBPL = ( nSrcBPL + 3 ) & 0xfffc;
842  if (!nRowBytes || nRowBytes < nSrcBPL || nRowBytes > nDestBPL)
843  return 0xffffffff;
844 
845  if (nRowBytes < 8 || nPackType == 1)
846  {
847  if (nHeight > pPict->remainingSize() / (sizeof(sal_uInt8) * nRowBytes))
848  return 0xffffffff;
849  }
850  else
851  {
852  size_t nByteCountSize = nRowBytes > 250 ? sizeof(sal_uInt16) : sizeof(sal_uInt8);
853  if (nHeight > pPict->remainingSize() / nByteCountSize)
854  return 0xffffffff;
855  }
856 
857  pBitmap.reset(new vcl::bitmap::RawBitmap( Size(nWidth, nHeight), 24 ));
858 
859  for (sal_uInt16 ny = 0; ny < nHeight; ++ny)
860  {
861  sal_uInt16 nx = 0;
862  if ( nRowBytes < 8 || nPackType == 1 )
863  {
864  for (size_t i = 0; i < nRowBytes; ++i)
865  {
866  pPict->ReadUChar( nDat );
867  if ( nx < nWidth )
868  SetByte(nx, ny, *pBitmap, nPixelSize, nDat, nWidth, aPalette);
869  }
870  nDataSize += nRowBytes;
871  }
872  else
873  {
874  sal_uInt16 nByteCount(0);
875  if ( nRowBytes > 250 )
876  {
877  pPict->ReadUInt16( nByteCount );
878  nDataSize += 2 + static_cast<sal_uInt32>(nByteCount);
879  }
880  else
881  {
882  sal_uInt8 nByteCountAsByte(0);
883  pPict->ReadUChar( nByteCountAsByte );
884  nByteCount = static_cast<sal_uInt16>(nByteCountAsByte) & 0x00ff;
885  nDataSize += 1 + nByteCount;
886  }
887 
888  while (pPict->good() && nByteCount)
889  {
890  sal_uInt8 nFlagCounterByte(0);
891  pPict->ReadUChar(nFlagCounterByte);
892  if ( ( nFlagCounterByte & 0x80 ) == 0 )
893  {
894  nCount = static_cast<sal_uInt16>(nFlagCounterByte) + 1;
895  for (size_t i = 0; i < nCount; ++i)
896  {
897  pPict->ReadUChar( nDat );
898  if ( nx < nWidth )
899  SetByte(nx, ny, *pBitmap, nPixelSize, nDat, nWidth, aPalette);
900  }
901  nByteCount -= 1 + nCount;
902  }
903  else
904  {
905  nCount = static_cast<sal_uInt16>( 1 - sal_Int16( static_cast<sal_uInt16>(nFlagCounterByte) | 0xff00 ) );
906  pPict->ReadUChar( nDat );
907  for (size_t i = 0; i < nCount; ++i)
908  {
909  if ( nx < nWidth )
910  SetByte(nx, ny, *pBitmap, nPixelSize, nDat, nWidth, aPalette);
911  }
912  nByteCount -= 2;
913  }
914  }
915  }
916  }
917  }
918  else if ( nPixelSize == 16 )
919  {
920  sal_uInt8 nByteCountAsByte, nFlagCounterByte;
921  sal_uInt16 nByteCount, nCount, nD;
922  sal_uInt64 nSrcBitsPos;
923 
924  if (nWidth > nRowBytes / 2)
925  return 0xffffffff;
926 
927  if (nRowBytes < 8 || nPackType == 1)
928  {
929  if (nHeight > pPict->remainingSize() / (sizeof(sal_uInt8) * nRowBytes))
930  return 0xffffffff;
931  }
932  else
933  {
934  size_t nByteCountSize = nRowBytes > 250 ? sizeof(sal_uInt16) : sizeof(sal_uInt8);
935  if (nHeight > pPict->remainingSize() / nByteCountSize)
936  return 0xffffffff;
937  }
938 
939  pBitmap.reset(new vcl::bitmap::RawBitmap( Size(nWidth, nHeight), 24 ));
940 
941  for (sal_uInt16 ny = 0; ny < nHeight; ++ny)
942  {
943  sal_uInt16 nx = 0;
944  if ( nRowBytes < 8 || nPackType == 1 )
945  {
946  for (size_t i = 0; i < nWidth; ++i)
947  {
948  pPict->ReadUInt16( nD );
949  nRed = static_cast<sal_uInt8>( nD >> 7 );
950  nGreen = static_cast<sal_uInt8>( nD >> 2 );
951  nBlue = static_cast<sal_uInt8>( nD << 3 );
952  pBitmap->SetPixel(ny, nx++, Color(nRed, nGreen, nBlue));
953  }
954  nDataSize += static_cast<sal_uInt32>(nWidth) * 2;
955  }
956  else
957  {
958  nSrcBitsPos = pPict->Tell();
959  if ( nRowBytes > 250 )
960  {
961  pPict->ReadUInt16( nByteCount );
962  nByteCount += 2;
963  }
964  else
965  {
966  pPict->ReadUChar( nByteCountAsByte );
967  nByteCount = static_cast<sal_uInt16>(nByteCountAsByte) & 0x00ff;
968  nByteCount++;
969  }
970  while ( nx != nWidth )
971  {
972  pPict->ReadUChar( nFlagCounterByte );
973  if ( (nFlagCounterByte & 0x80) == 0)
974  {
975  nCount=static_cast<sal_uInt16>(nFlagCounterByte)+1;
976  if ( nCount + nx > nWidth)
977  nCount = nWidth - nx;
978  if (pPict->remainingSize() < sizeof(sal_uInt16) * nCount)
979  return 0xffffffff;
980  /* SJ: the RLE decoding seems not to be correct here,
981  I don't want to change this until I have a bugdoc for
982  this case. Have a look at 32bit, there I changed the
983  encoding, so that it is used a straight forward array
984  */
985  for (size_t i = 0; i < nCount; ++i)
986  {
987  pPict->ReadUInt16( nD );
988  nRed = static_cast<sal_uInt8>( nD >> 7 );
989  nGreen = static_cast<sal_uInt8>( nD >> 2 );
990  nBlue = static_cast<sal_uInt8>( nD << 3 );
991  pBitmap->SetPixel(ny, nx++, Color(nRed, nGreen, nBlue));
992  }
993  }
994  else
995  {
996  if (pPict->remainingSize() < sizeof(sal_uInt16))
997  return 0xffffffff;
998  nCount=(1-sal_Int16(static_cast<sal_uInt16>(nFlagCounterByte)|0xff00));
999  if ( nCount + nx > nWidth )
1000  nCount = nWidth - nx;
1001  pPict->ReadUInt16( nD );
1002  nRed = static_cast<sal_uInt8>( nD >> 7 );
1003  nGreen = static_cast<sal_uInt8>( nD >> 2 );
1004  nBlue = static_cast<sal_uInt8>( nD << 3 );
1005  for (size_t i = 0; i < nCount; ++i)
1006  {
1007  pBitmap->SetPixel(ny, nx++, Color(nRed, nGreen, nBlue));
1008  }
1009  }
1010  }
1011  nDataSize += nByteCount;
1012  pPict->Seek(nSrcBitsPos+nByteCount);
1013  }
1014  }
1015  }
1016  else if ( nPixelSize == 32 )
1017  {
1018  sal_uInt16 nByteCount;
1019  size_t nCount;
1020  sal_uInt64 nSrcBitsPos;
1021  if ( nRowBytes != 4*nWidth )
1022  return 0xffffffff;
1023 
1024  if ( nRowBytes < 8 || nPackType == 1 )
1025  {
1026  const size_t nMaxPixels = pPict->remainingSize() / 4;
1027  const size_t nMaxRows = nMaxPixels / nWidth;
1028  if (nHeight > nMaxRows)
1029  return 0xffffffff;
1030  const size_t nMaxCols = nMaxPixels / nHeight;
1031  if (nWidth > nMaxCols)
1032  return 0xffffffff;
1033 
1034  pBitmap.reset(new vcl::bitmap::RawBitmap( Size(nWidth, nHeight), 24 ));
1035 
1036  for (sal_uInt16 ny = 0; ny < nHeight; ++ny)
1037  {
1038  for (sal_uInt16 nx = 0; nx < nWidth; ++nx)
1039  {
1040  sal_uInt8 nDummy;
1041  pPict->ReadUChar( nDummy ).ReadUChar( nRed ).ReadUChar( nGreen ).ReadUChar( nBlue );
1042  pBitmap->SetPixel(ny, nx, Color(nRed, nGreen, nBlue));
1043  }
1044  nDataSize += static_cast<sal_uInt32>(nWidth) * 4;
1045  }
1046  }
1047  else if ( nPackType == 2 )
1048  {
1049  const size_t nMaxPixels = pPict->remainingSize() / 3;
1050  const size_t nMaxRows = nMaxPixels / nWidth;
1051  if (nHeight > nMaxRows)
1052  return 0xffffffff;
1053  const size_t nMaxCols = nMaxPixels / nHeight;
1054  if (nWidth > nMaxCols)
1055  return 0xffffffff;
1056 
1057  pBitmap.reset(new vcl::bitmap::RawBitmap( Size(nWidth, nHeight), 24 ));
1058 
1059  for (sal_uInt16 ny = 0; ny < nHeight; ++ny)
1060  {
1061  for (sal_uInt16 nx = 0; nx < nWidth; ++nx)
1062  {
1063  pPict->ReadUChar( nRed ).ReadUChar( nGreen ).ReadUChar( nBlue );
1064  pBitmap->SetPixel(ny, nx, Color(nRed, nGreen, nBlue));
1065  }
1066  nDataSize += static_cast<sal_uInt32>(nWidth) * 3;
1067  }
1068  }
1069  else
1070  {
1071  sal_uInt8 nByteCountAsByte;
1072  sal_uInt8 nFlagCounterByte;
1073  if ( ( nCmpCount == 3 ) || ( nCmpCount == 4 ) )
1074  {
1075  size_t nByteCountSize = nRowBytes > 250 ? sizeof(sal_uInt16) : sizeof(sal_uInt8);
1076  if (nHeight > pPict->remainingSize() / nByteCountSize)
1077  return 0xffffffff;
1078 
1079  pBitmap.reset(new vcl::bitmap::RawBitmap( Size(nWidth, nHeight), 24 ));
1080 
1081  // cid#1458434 to sanitize Untrusted loop bound
1082  nWidth = pBitmap->Width();
1083 
1084  size_t nByteWidth = static_cast<size_t>(nWidth) * nCmpCount;
1085  std::vector<sal_uInt8> aScanline(nByteWidth);
1086  for (sal_uInt16 ny = 0; ny < nHeight; ++ny)
1087  {
1088  nSrcBitsPos = pPict->Tell();
1089  if ( nRowBytes > 250 )
1090  {
1091  pPict->ReadUInt16( nByteCount );
1092  nByteCount += 2;
1093  }
1094  else
1095  {
1096  pPict->ReadUChar( nByteCountAsByte );
1097  nByteCount = nByteCountAsByte;
1098  nByteCount++;
1099  }
1100  size_t i = 0;
1101  while (i < aScanline.size())
1102  {
1103  pPict->ReadUChar( nFlagCounterByte );
1104  if ( ( nFlagCounterByte & 0x80 ) == 0)
1105  {
1106  nCount = static_cast<sal_uInt16>(nFlagCounterByte) + 1;
1107  if ((i + nCount) > aScanline.size())
1108  nCount = aScanline.size() - i;
1109  if (pPict->remainingSize() < nCount)
1110  return 0xffffffff;
1111  while( nCount-- )
1112  {
1113  pPict->ReadUChar( nDat );
1114  aScanline[ i++ ] = nDat;
1115  }
1116  }
1117  else
1118  {
1119  if (pPict->remainingSize() < 1)
1120  return 0xffffffff;
1121  nCount = ( 1 - sal_Int16( static_cast<sal_uInt16>(nFlagCounterByte) | 0xff00 ) );
1122  if (( i + nCount) > aScanline.size())
1123  nCount = aScanline.size() - i;
1124  pPict->ReadUChar( nDat );
1125  while( nCount-- )
1126  aScanline[ i++ ] = nDat;
1127  }
1128  }
1129  sal_uInt8* pTmp = aScanline.data();
1130  if ( nCmpCount == 4 )
1131  pTmp += nWidth;
1132  for (sal_uInt16 nx = 0; nx < nWidth; pTmp++)
1133  pBitmap->SetPixel(ny, nx++, Color(*pTmp, pTmp[ nWidth ], pTmp[ 2 * nWidth ]));
1134  nDataSize += nByteCount;
1135  pPict->Seek( nSrcBitsPos + nByteCount );
1136  }
1137  }
1138  }
1139  }
1140  else
1141  return 0xffffffff;
1142  rBitmap = vcl::bitmap::CreateFromData(std::move(*pBitmap));
1143  return nDataSize;
1144 }
1145 
1146 void PictReader::ReadHeader()
1147 {
1148  short y1,x1,y2,x2;
1149 
1150  char sBuf[ 2 ];
1151  // previous code considers pPict->Tell() as the normal starting position,
1152  // can we have nStartPos != 0 ?
1153  sal_uInt64 nStartPos = pPict->Tell();
1154  // Standard:
1155  // a picture file begins by 512 bytes (reserved to the application) followed by the picture data
1156  // while clipboard, pictures stored in a document often contain only the picture data.
1157 
1158  // Special cases:
1159  // - some Pict v.1 use 0x00 0x11 0x01 ( instead of 0x11 0x01) to store the version op
1160  // (we consider here this as another standard for Pict. v.1 )
1161  // - some files seem to contain extra garbage data at the beginning
1162  // - some picture data seem to contain extra NOP opcode(0x00) between the bounding box and the version opcode
1163 
1164  // This code looks hard to find a picture header, ie. it looks at positions
1165  // - nStartPos+0, nStartPos+512 with potential extra NOP codes between bdbox and version (at most 9 extra NOP)
1166  // - 512..1024 with more strict bdbox checking and no extra NOP codes
1167 
1168  // Notes:
1169  // - if the header can begin at nStartPos+0 and at nStartPos+512, we try to choose the more
1170  // <<probable>> ( using the variable confidence)
1171  // - svtools/source/filter.vcl/filter/{filter.cxx,filter2.cxx} only check for standard Pict,
1172  // this may cause future problems
1173  int st;
1174  sal_uInt32 nOffset;
1175  int confidence[2] = { 0, 0};
1176  for ( st = 0; st < 3 + 513; st++ )
1177  {
1178  int actualConfid = 20; // the actual confidence
1179  pPict->ResetError();
1180  if (st < 2) nOffset = nStartPos+st*512;
1181  else if (st == 2) {
1182  // choose nStartPos+0 or nStartPos+512 even if there are a little dubious
1183  int actPos = -1, actConf=0;
1184  if (confidence[0] > 0) { actPos = 0; actConf = confidence[0]; }
1185  if (confidence[1] > 0 && confidence[1] >= actConf) actPos = 1;
1186  if (actPos < 0) continue;
1187  nOffset = nStartPos+actPos*512;
1188  }
1189  else {
1190  nOffset = 509+st; // illogical : more logical will be nStartPos+509+st or to consider that nStartPos=0
1191  // a small test to check if versionOp code exists after the bdbox ( with no extra NOP codes)
1192  pPict->Seek(nOffset+10);
1193  pPict->ReadBytes(sBuf, 2);
1194  if (!pPict->good()) break;
1195  if (sBuf[0] == 0x11 || (sBuf[0] == 0x00 && sBuf[1] == 0x11)) ; // maybe ok
1196  else continue;
1197  }
1198  pPict->Seek(nOffset);
1199 
1200  // 2 bytes to store size ( version 1 ) ignored
1201  pPict->SeekRel( 2 );
1202  pPict->ReadInt16( y1 ).ReadInt16( x1 ).ReadInt16( y2 ).ReadInt16( x2 ); // frame rectangle of the picture
1203  if (x1 > x2 || y1 > y2) continue; // bad bdbox
1204  if (x1 < -2048 || x2 > 2048 || y1 < -2048 || y2 > 2048 || // origin|dest is very small|large
1205  (x1 == x2 && y1 == y2) ) // 1 pixel pict is dubious
1206  actualConfid-=3;
1207  else if (x2 < x1+8 || y2 < y1+8) // a little dubious
1208  actualConfid-=1;
1209  if (st >= 3 && actualConfid != 20) continue;
1210  aBoundingRect=tools::Rectangle( x1,y1, x2, y2 );
1211 
1212  if (!pPict->good()) continue;
1213  // read version
1214  pPict->ReadBytes(sBuf, 2);
1215  // version 1 file
1216  if ( sBuf[ 0 ] == 0x11 && sBuf[ 1 ] == 0x01 ) {
1217  // pict v1 must be rare and we do only few tests
1218  if (st < 2) { confidence[st] = --actualConfid; continue; }
1219  IsVersion2 = false; return;
1220  }
1221  if (sBuf[0] != 0x00) continue; // unrecoverable error
1222  int numZero = 0;
1223  do
1224  {
1225  numZero++;
1226  pPict->SeekRel(-1);
1227  pPict->ReadBytes(sBuf, 2);
1228  }
1229  while ( sBuf[0] == 0x00 && numZero < 10);
1230  actualConfid -= (numZero-1); // extra nop are dubious
1231  if (!pPict->good()) continue;
1232  if (sBuf[0] != 0x11) continue; // not a version opcode
1233  // abnormal version 1 file
1234  if (sBuf[1] == 0x01 ) {
1235  // pict v1 must be rare and we do only few tests
1236  if (st < 2) { confidence[st] = --actualConfid; continue; }
1237  IsVersion2 = false; return;
1238  }
1239  if (sBuf[1] != 0x02 ) continue; // not a version 2 file
1240 
1241  IsVersion2=true;
1242  short nExtVer, nReserved;
1243  // 3 Bytes ignored : end of version arg 0x02FF (ie: 0xFF), HeaderOp : 0x0C00
1244  pPict->SeekRel( 3 );
1245  pPict->ReadInt16( nExtVer ).ReadInt16( nReserved );
1246  if (!pPict->good()) continue;
1247 
1248  if ( nExtVer == -2 ) // extended version 2 picture
1249  {
1250  sal_Int32 nHResFixed, nVResFixed;
1251  pPict->ReadInt32( nHResFixed ).ReadInt32( nVResFixed );
1252  pPict->ReadInt16( y1 ).ReadInt16( x1 ).ReadInt16( y2 ).ReadInt16( x2 ); // reading the optimal bounding rect
1253  if (x1 > x2 || y1 > y2) continue; // bad bdbox
1254  if (st < 2 && actualConfid != 20) { confidence[st] = actualConfid; continue; }
1255 
1256  double fHRes = nHResFixed;
1257  fHRes /= 65536;
1258  double fVRes = nVResFixed;
1259  fVRes /= 65536;
1260  aHRes /= fHRes;
1261  aVRes /= fVRes;
1262  aBoundingRect=tools::Rectangle( x1,y1, x2, y2 );
1263  pPict->SeekRel( 4 ); // 4 bytes reserved
1264  return;
1265  }
1266  else if (nExtVer == -1 ) { // basic version 2 picture
1267  if (st < 2 && actualConfid != 20) { confidence[st] = actualConfid; continue; }
1268  pPict->SeekRel( 16); // bdbox(4 fixed number)
1269  pPict->SeekRel(4); // 4 bytes reserved
1270  return;
1271  }
1272  }
1274 }
1275 
1276 #if OSL_DEBUG_LEVEL > 0
1277 static const char* operationName(sal_uInt16 nOpcode)
1278 {
1279  // add here whatever makes the debugging easier for you, otherwise you'll
1280  // see only the operation's opcode
1281  switch (nOpcode)
1282  {
1283  case 0x0001: return "Clip";
1284  case 0x0003: return "TxFont";
1285  case 0x0004: return "TxFace";
1286  case 0x0008: return "PnMode";
1287  case 0x0009: return "PnPat";
1288  case 0x000d: return "TxSize";
1289  case 0x001a: return "RGBFgCol";
1290  case 0x001d: return "HiliteColor";
1291  case 0x0020: return "Line";
1292  case 0x0022: return "ShortLine";
1293  case 0x0028: return "LongText";
1294  case 0x0029: return "DHText";
1295  case 0x002a: return "DVText";
1296  case 0x002c: return "fontName";
1297  case 0x002e: return "glyphState";
1298  case 0x0031: return "paintRect";
1299  case 0x0038: return "frameSameRect";
1300  case 0x0070: return "framePoly";
1301  case 0x0071: return "paintPoly";
1302  case 0x00a1: return "LongComment";
1303  default: return "?";
1304  }
1305 }
1306 #endif
1307 
1308 sal_uInt64 PictReader::ReadData(sal_uInt16 nOpcode)
1309 {
1310  sal_uInt16 nUSHORT;
1311  Point aPoint;
1312  sal_uInt64 nDataSize=0;
1313  PictDrawingMethod shapeDMethod = PictDrawingMethod::UNDEFINED;
1314  switch (nOpcode & 7) {
1315  case 0: shapeDMethod = PictDrawingMethod::FRAME; break;
1316  case 1: shapeDMethod = PictDrawingMethod::PAINT; break;
1317  case 2: shapeDMethod = PictDrawingMethod::ERASE; break;
1318  case 3: shapeDMethod = PictDrawingMethod::INVERT; break;
1319  case 4: shapeDMethod = PictDrawingMethod::FILL; break;
1320  default: break;
1321  }
1322 
1323 #if OSL_DEBUG_LEVEL > 0
1324  SAL_INFO("filter.pict", "Operation: 0x" << OUString::number(nOpcode, 16) << " [" << operationName(nOpcode) << "]");
1325 #endif
1326 
1327  switch(nOpcode) {
1328 
1329  case 0x0000: // NOP
1330  nDataSize=0;
1331  break;
1332 
1333  case 0x0001: { // Clip
1334  tools::Rectangle aRect;
1335  pPict->ReadUInt16( nUSHORT );
1336  nDataSize=nUSHORT;
1337  ReadRectangle(aRect);
1338  // checkme: do we really want to extend the rectangle here ?
1339  // I do that because the clipping is often used to clean a region,
1340  // before drawing some text and also to draw this text.
1341  // So using a too small region can lead to clip the end of the text ;
1342  // but this can be discussable...
1343  aRect.setWidth(aRect.getWidth()+1);
1344  aRect.setHeight(aRect.getHeight()+1);
1345  pVirDev->SetClipRegion( vcl::Region( aRect ) );
1346  break;
1347  }
1348  case 0x0002: // BkPat
1349  nDataSize = eActBackPattern.read(*pPict);
1350  eActMethod = PictDrawingMethod::UNDEFINED;
1351  break;
1352 
1353  case 0x0003: // TxFont
1354  pPict->ReadUInt16( nUSHORT );
1355  if (nUSHORT <= 1) aActFont.SetFamily(FAMILY_SWISS);
1356  else if (nUSHORT <= 12) aActFont.SetFamily(FAMILY_DECORATIVE);
1357  else if (nUSHORT <= 20) aActFont.SetFamily(FAMILY_ROMAN);
1358  else if (nUSHORT == 21) aActFont.SetFamily(FAMILY_SWISS);
1359  else if (nUSHORT == 22) aActFont.SetFamily(FAMILY_MODERN);
1360  else if (nUSHORT <= 1023) aActFont.SetFamily(FAMILY_SWISS);
1361  else aActFont.SetFamily(FAMILY_ROMAN);
1362  aActFont.SetCharSet(GetTextEncoding(nUSHORT));
1363  eActMethod = PictDrawingMethod::UNDEFINED;
1364  nDataSize=2;
1365  break;
1366 
1367  case 0x0004: { // TxFace
1368  char nFace;
1369  pPict->ReadChar( nFace );
1370  if ( (nFace & 0x01)!=0 ) aActFont.SetWeight(WEIGHT_BOLD);
1371  else aActFont.SetWeight(WEIGHT_NORMAL);
1372  if ( (nFace & 0x02)!=0 ) aActFont.SetItalic(ITALIC_NORMAL);
1373  else aActFont.SetItalic(ITALIC_NONE);
1374  if ( (nFace & 0x04)!=0 ) aActFont.SetUnderline(LINESTYLE_SINGLE);
1375  else aActFont.SetUnderline(LINESTYLE_NONE);
1376  if ( (nFace & 0x08)!=0 ) aActFont.SetOutline(true);
1377  else aActFont.SetOutline(false);
1378  if ( (nFace & 0x10)!=0 ) aActFont.SetShadow(true);
1379  else aActFont.SetShadow(false);
1380  eActMethod = PictDrawingMethod::UNDEFINED;
1381  nDataSize=1;
1382  break;
1383  }
1384  case 0x0005: // TxMode
1385  nDataSize=2;
1386  break;
1387 
1388  case 0x0006: // SpExtra
1389  nDataSize=4;
1390  break;
1391 
1392  case 0x0007: { // PnSize
1393  nActPenSize=ReadSize();
1394  eActMethod = PictDrawingMethod::UNDEFINED;
1395  nDataSize=4;
1396  break;
1397  }
1398  case 0x0008: // PnMode
1399  pPict->ReadUInt16( nUSHORT );
1400  // internal code for postscript command (Quickdraw Reference Drawing B-30,B-34)
1401  if (nUSHORT==23) eActROP = RasterOp::N1;
1402  else {
1403  switch (nUSHORT & 0x0007) {
1404  case 0: eActROP=RasterOp::OverPaint; break; // Copy
1405  case 1: eActROP=RasterOp::OverPaint; break; // Or
1406  case 2: eActROP=RasterOp::Xor; break; // Xor
1407  case 3: eActROP=RasterOp::OverPaint; break; // Bic
1408  case 4: eActROP=RasterOp::Invert; break; // notCopy
1409  case 5: eActROP=RasterOp::OverPaint; break; // notOr
1410  case 6: eActROP=RasterOp::Xor; break; // notXor
1411  case 7: eActROP=RasterOp::OverPaint; break; // notBic
1412  }
1413  }
1414  eActMethod = PictDrawingMethod::UNDEFINED;
1415  nDataSize=2;
1416  break;
1417 
1418  case 0x0009: // PnPat
1419  nDataSize=eActPenPattern.read(*pPict);
1420  eActMethod = PictDrawingMethod::UNDEFINED;
1421  break;
1422 
1423  case 0x000a: // FillPat
1424  nDataSize=eActFillPattern.read(*pPict);
1425  eActMethod = PictDrawingMethod::UNDEFINED;
1426  break;
1427 
1428  case 0x000b: // OvSize
1429  aActOvalSize=ReadSize();
1430  nDataSize=4;
1431  break;
1432 
1433  case 0x000c: // Origin
1434  nDataSize=4;
1435  break;
1436 
1437  case 0x000d: // TxSize
1438  {
1439  pPict->ReadUInt16( nUSHORT );
1440  aActFont.SetFontSize( Size( 0, static_cast<tools::Long>(nUSHORT) ) );
1441  eActMethod = PictDrawingMethod::UNDEFINED;
1442  nDataSize=2;
1443  }
1444  break;
1445 
1446  case 0x000e: // FgColor
1447  aActForeColor=ReadColor();
1448  eActMethod = PictDrawingMethod::UNDEFINED;
1449  nDataSize=4;
1450  break;
1451 
1452  case 0x000f: // BkColor
1453  aActBackColor=ReadColor();
1454  nDataSize=4;
1455  break;
1456 
1457  case 0x0010: // TxRatio
1458  nDataSize=8;
1459  break;
1460 
1461  case 0x0011: // VersionOp
1462  nDataSize=1;
1463  break;
1464 
1465  case 0x0012: // BkPixPat
1466  nDataSize=ReadPixPattern(eActBackPattern);
1467  eActMethod = PictDrawingMethod::UNDEFINED;
1468  break;
1469 
1470  case 0x0013: // PnPixPat
1471  nDataSize=ReadPixPattern(eActPenPattern);
1472  eActMethod = PictDrawingMethod::UNDEFINED;
1473  break;
1474 
1475  case 0x0014: // FillPixPat
1476  nDataSize=ReadPixPattern(eActFillPattern);
1477  eActMethod = PictDrawingMethod::UNDEFINED;
1478  break;
1479 
1480  case 0x0015: // PnLocHFrac
1481  nDataSize=2;
1482  break;
1483 
1484  case 0x0016: // ChExtra
1485  nDataSize=2;
1486  break;
1487 
1488  case 0x0017: // Reserved (0 Bytes)
1489  case 0x0018: // Reserved (0 Bytes)
1490  case 0x0019: // Reserved (0 Bytes)
1491  nDataSize=0;
1492  break;
1493 
1494  case 0x001a: // RGBFgCol
1495  aActForeColor=ReadRGBColor();
1496  eActMethod = PictDrawingMethod::UNDEFINED;
1497  nDataSize=6;
1498  break;
1499 
1500  case 0x001b: // RGBBkCol
1501  aActBackColor=ReadRGBColor();
1502  eActMethod = PictDrawingMethod::UNDEFINED;
1503  nDataSize=6;
1504  break;
1505 
1506  case 0x001c: // HiliteMode
1507  nDataSize=0;
1508  break;
1509 
1510  case 0x001d: // HiliteColor
1511  nDataSize=6;
1512  break;
1513 
1514  case 0x001e: // DefHilite
1515  nDataSize=0;
1516  break;
1517 
1518  case 0x001f: // OpColor
1519  nDataSize=6;
1520  break;
1521 
1522  case 0x0020: // Line
1523  aPoint=ReadPoint(); aPenPosition=ReadPoint();
1524  nDataSize=8;
1525 
1526  if (!pPict->good())
1527  break;
1528 
1529  if (IsInvisible( PictDrawingMethod::FRAME )) break;
1530  DrawingMethod( PictDrawingMethod::FRAME );
1531  PictReaderShape::drawLine(pVirDev, aPoint,aPenPosition, nActPenSize);
1532  break;
1533 
1534  case 0x0021: // LineFrom
1535  aPoint=aPenPosition; aPenPosition=ReadPoint();
1536  nDataSize=4;
1537 
1538  if (!pPict->good())
1539  break;
1540 
1541  if (IsInvisible( PictDrawingMethod::FRAME )) break;
1542  DrawingMethod( PictDrawingMethod::FRAME );
1543  PictReaderShape::drawLine(pVirDev, aPoint,aPenPosition, nActPenSize);
1544  break;
1545 
1546  case 0x0022: // ShortLine
1547  aPoint=ReadPoint();
1548  aPenPosition=ReadDeltaH(aPoint);
1549  aPenPosition=ReadDeltaV(aPenPosition);
1550  nDataSize=6;
1551 
1552  if (!pPict->good())
1553  break;
1554 
1555  if ( IsInvisible(PictDrawingMethod::FRAME) ) break;
1556  DrawingMethod( PictDrawingMethod::FRAME );
1557  PictReaderShape::drawLine(pVirDev, aPoint,aPenPosition, nActPenSize);
1558  break;
1559 
1560  case 0x0023: // ShortLineFrom
1561  aPoint=aPenPosition;
1562  aPenPosition=ReadDeltaH(aPoint);
1563  aPenPosition=ReadDeltaV(aPenPosition);
1564  nDataSize=2;
1565 
1566  if (!pPict->good())
1567  break;
1568 
1569  if (IsInvisible( PictDrawingMethod::FRAME )) break;
1570  DrawingMethod( PictDrawingMethod::FRAME );
1571  PictReaderShape::drawLine(pVirDev, aPoint,aPenPosition, nActPenSize);
1572  break;
1573 
1574  case 0x0024: // Reserved (n Bytes)
1575  case 0x0025: // Reserved (n Bytes)
1576  case 0x0026: // Reserved (n Bytes)
1577  case 0x0027: // Reserved (n Bytes)
1578  pPict->ReadUInt16( nUSHORT );
1579  nDataSize=2+nUSHORT;
1580  break;
1581 
1582  case 0x0028: // LongText
1583  aTextPosition=ReadPoint();
1584  nDataSize=4+ReadAndDrawText();
1585  break;
1586 
1587  case 0x0029: // DHText
1588  aTextPosition=ReadUnsignedDeltaH(aTextPosition);
1589  nDataSize=1+ReadAndDrawText();
1590  break;
1591 
1592  case 0x002a: // DVText
1593  aTextPosition=ReadUnsignedDeltaV(aTextPosition);
1594  nDataSize=1+ReadAndDrawText();
1595  break;
1596 
1597  case 0x002b: // DHDVText
1598  aTextPosition=ReadUnsignedDeltaH(aTextPosition);
1599  aTextPosition=ReadUnsignedDeltaV(aTextPosition);
1600  nDataSize=2+ReadAndDrawText();
1601  break;
1602 
1603  case 0x002c: { // fontName
1604  pPict->ReadUInt16( nUSHORT ); nDataSize=nUSHORT+2;
1605  pPict->ReadUInt16( nUSHORT );
1606  if (nUSHORT <= 1) aActFont.SetFamily(FAMILY_SWISS);
1607  else if (nUSHORT <= 12) aActFont.SetFamily(FAMILY_DECORATIVE);
1608  else if (nUSHORT <= 20) aActFont.SetFamily(FAMILY_ROMAN);
1609  else if (nUSHORT == 21) aActFont.SetFamily(FAMILY_SWISS);
1610  else if (nUSHORT == 22) aActFont.SetFamily(FAMILY_MODERN);
1611  else if (nUSHORT <= 1023) aActFont.SetFamily(FAMILY_SWISS);
1612  else aActFont.SetFamily(FAMILY_ROMAN);
1613  aActFont.SetCharSet(GetTextEncoding(nUSHORT));
1614  char nByteLen(0);
1615  pPict->ReadChar( nByteLen );
1616  sal_uInt16 nLen = static_cast<sal_uInt16>(nByteLen)&0x00ff;
1617  char sFName[ 256 ];
1618  sFName[pPict->ReadBytes(sFName, nLen)] = 0;
1619  OUString aString( sFName, strlen(sFName), osl_getThreadTextEncoding() );
1620  aActFont.SetFamilyName( aString );
1621  eActMethod = PictDrawingMethod::UNDEFINED;
1622  break;
1623  }
1624  case 0x002d: // lineJustify
1625  nDataSize=10;
1626  break;
1627 
1628  case 0x002e: // glyphState
1629  pPict->ReadUInt16( nUSHORT );
1630  nDataSize=2+nUSHORT;
1631  break;
1632 
1633  case 0x002f: // Reserved (n Bytes)
1634  pPict->ReadUInt16( nUSHORT );
1635  nDataSize=2+nUSHORT;
1636  break;
1637 
1638  case 0x0030: // frameRect
1639  case 0x0031: // paintRect
1640  case 0x0032: // eraseRect
1641  case 0x0033: // invertRect
1642  case 0x0034: // fillRect
1643  nDataSize=ReadAndDrawRect(shapeDMethod);
1644  break;
1645 
1646  case 0x0035: // Reserved (8 Bytes)
1647  case 0x0036: // Reserved (8 Bytes)
1648  case 0x0037: // Reserved (8 Bytes)
1649  nDataSize=8;
1650  break;
1651 
1652  case 0x0038: // frameSameRect
1653  case 0x0039: // paintSameRect
1654  case 0x003a: // eraseSameRect
1655  case 0x003b: // invertSameRect
1656  case 0x003c: // fillSameRect
1657  nDataSize=ReadAndDrawSameRect(shapeDMethod);
1658  break;
1659 
1660  case 0x003d: // Reserved (0 Bytes)
1661  case 0x003e: // Reserved (0 Bytes)
1662  case 0x003f: // Reserved (0 Bytes)
1663  nDataSize=0;
1664  break;
1665 
1666  case 0x0040: // frameRRect
1667  case 0x0041: // paintRRect
1668  case 0x0042: // eraseRRect
1669  case 0x0043: // invertRRect
1670  case 0x0044: // fillRRect
1671  nDataSize=ReadAndDrawRoundRect(shapeDMethod);
1672  break;
1673 
1674  case 0x0045: // Reserved (8 Bytes)
1675  case 0x0046: // Reserved (8 Bytes)
1676  case 0x0047: // Reserved (8 Bytes)
1677  nDataSize=8;
1678  break;
1679 
1680  case 0x0048: // frameSameRRect
1681  case 0x0049: // paintSameRRect
1682  case 0x004a: // eraseSameRRect
1683  case 0x004b: // invertSameRRect
1684  case 0x004c: // fillSameRRect
1685  nDataSize=ReadAndDrawSameRoundRect(shapeDMethod);
1686  break;
1687 
1688  case 0x004d: // Reserved (0 Bytes)
1689  case 0x004e: // Reserved (0 Bytes)
1690  case 0x004f: // Reserved (0 Bytes)
1691  nDataSize=0;
1692  break;
1693 
1694  case 0x0050: // frameOval
1695  case 0x0051: // paintOval
1696  case 0x0052: // eraseOval
1697  case 0x0053: // invertOval
1698  case 0x0054: // fillOval
1699  nDataSize=ReadAndDrawOval(shapeDMethod);
1700  break;
1701 
1702  case 0x0055: // Reserved (8 Bytes)
1703  case 0x0056: // Reserved (8 Bytes)
1704  case 0x0057: // Reserved (8 Bytes)
1705  nDataSize=8;
1706  break;
1707 
1708  case 0x0058: // frameSameOval
1709  case 0x0059: // paintSameOval
1710  case 0x005a: // eraseSameOval
1711  case 0x005b: // invertSameOval
1712  case 0x005c: // fillSameOval
1713  nDataSize=ReadAndDrawSameOval(shapeDMethod);
1714  break;
1715 
1716  case 0x005d: // Reserved (0 Bytes)
1717  case 0x005e: // Reserved (0 Bytes)
1718  case 0x005f: // Reserved (0 Bytes)
1719  nDataSize=0;
1720  break;
1721 
1722  case 0x0060: // frameArc
1723  case 0x0061: // paintArc
1724  case 0x0062: // eraseArc
1725  case 0x0063: // invertArc
1726  case 0x0064: // fillArc
1727  nDataSize=ReadAndDrawArc(shapeDMethod);
1728  break;
1729 
1730  case 0x0065: // Reserved (12 Bytes)
1731  case 0x0066: // Reserved (12 Bytes)
1732  case 0x0067: // Reserved (12 Bytes)
1733  nDataSize=12;
1734  break;
1735 
1736  case 0x0068: // frameSameArc
1737  case 0x0069: // paintSameArc
1738  case 0x006a: // eraseSameArc
1739  case 0x006b: // invertSameArc
1740  case 0x006c: // fillSameArc
1741  nDataSize=ReadAndDrawSameArc(shapeDMethod);
1742  break;
1743 
1744  case 0x006d: // Reserved (4 Bytes)
1745  case 0x006e: // Reserved (4 Bytes)
1746  case 0x006f: // Reserved (4 Bytes)
1747  nDataSize=4;
1748  break;
1749 
1750  case 0x0070: // framePoly
1751  case 0x0071: // paintPoly
1752  case 0x0072: // erasePoly
1753  case 0x0073: // invertPoly
1754  case 0x0074: // fillPoly
1755  nDataSize=ReadAndDrawPolygon(shapeDMethod);
1756  break;
1757 
1758  case 0x0075: // Reserved (Polygon-Size)
1759  case 0x0076: // Reserved (Polygon-Size)
1760  case 0x0077: // Reserved (Polygon-Size)
1761  pPict->ReadUInt16( nUSHORT ); nDataSize=nUSHORT;
1762  break;
1763 
1764  case 0x0078: // frameSamePoly
1765  case 0x0079: // paintSamePoly
1766  case 0x007a: // eraseSamePoly
1767  case 0x007b: // invertSamePoly
1768  case 0x007c: // fillSamePoly
1769  nDataSize=ReadAndDrawSamePolygon(shapeDMethod);
1770  break;
1771 
1772  case 0x007d: // Reserved (0 Bytes)
1773  case 0x007e: // Reserved (0 Bytes)
1774  case 0x007f: // Reserved (0 Bytes)
1775  nDataSize=0;
1776  break;
1777 
1778  case 0x0080: // frameRgn
1779  case 0x0081: // paintRgn
1780  case 0x0082: // eraseRgn
1781  case 0x0083: // invertRgn
1782  case 0x0084: // fillRgn
1783  nDataSize=ReadAndDrawRgn(shapeDMethod);
1784  break;
1785 
1786  case 0x0085: // Reserved (Region-Size)
1787  case 0x0086: // Reserved (Region-Size)
1788  case 0x0087: // Reserved (Region-Size)
1789  pPict->ReadUInt16( nUSHORT ); nDataSize=nUSHORT;
1790  break;
1791 
1792  case 0x0088: // frameSameRgn
1793  case 0x0089: // paintSameRgn
1794  case 0x008a: // eraseSameRgn
1795  case 0x008b: // invertSameRgn
1796  case 0x008c: // fillSameRgn
1797  nDataSize=ReadAndDrawSameRgn(shapeDMethod);
1798  break;
1799 
1800  case 0x008d: // Reserved (0 Bytes)
1801  case 0x008e: // Reserved (0 Bytes)
1802  case 0x008f: // Reserved (0 Bytes)
1803  nDataSize=0;
1804  break;
1805 
1806  case 0x0090: { // BitsRect
1807  BitmapEx aBmp;
1808  tools::Rectangle aSrcRect, aDestRect;
1809  nDataSize=ReadPixMapEtc(aBmp, false, true, &aSrcRect, &aDestRect, true, false);
1810  DrawingMethod( PictDrawingMethod::PAINT );
1811  pVirDev->DrawBitmapEx(aDestRect.TopLeft(),aDestRect.GetSize(),aBmp);
1812  break;
1813  }
1814  case 0x0091: { // BitsRgn
1815  BitmapEx aBmp;
1816  tools::Rectangle aSrcRect, aDestRect;
1817  nDataSize=ReadPixMapEtc(aBmp, false, true, &aSrcRect, &aDestRect, true, true);
1818  DrawingMethod( PictDrawingMethod::PAINT );
1819  pVirDev->DrawBitmapEx(aDestRect.TopLeft(),aDestRect.GetSize(),aBmp);
1820  break;
1821  }
1822  case 0x0092: // Reserved (n Bytes)
1823  case 0x0093: // Reserved (n Bytes)
1824  case 0x0094: // Reserved (n Bytes)
1825  case 0x0095: // Reserved (n Bytes)
1826  case 0x0096: // Reserved (n Bytes)
1827  case 0x0097: // Reserved (n Bytes)
1828  pPict->ReadUInt16( nUSHORT ); nDataSize=2+nUSHORT;
1829  break;
1830 
1831  case 0x0098: { // PackBitsRect
1832  BitmapEx aBmp;
1833  tools::Rectangle aSrcRect, aDestRect;
1834  nDataSize=ReadPixMapEtc(aBmp, false, true, &aSrcRect, &aDestRect, true, false);
1835  DrawingMethod( PictDrawingMethod::PAINT );
1836  pVirDev->DrawBitmapEx(aDestRect.TopLeft(),aDestRect.GetSize(),aBmp);
1837  break;
1838  }
1839  case 0x0099: { // PackBitsRgn
1840  BitmapEx aBmp;
1841  tools::Rectangle aSrcRect, aDestRect;
1842  nDataSize=ReadPixMapEtc(aBmp, false, true, &aSrcRect, &aDestRect, true, true);
1843  DrawingMethod( PictDrawingMethod::PAINT );
1844  pVirDev->DrawBitmapEx(aDestRect.TopLeft(),aDestRect.GetSize(),aBmp);
1845  break;
1846  }
1847  case 0x009a: { // DirectBitsRect
1848  BitmapEx aBmp;
1849  tools::Rectangle aSrcRect, aDestRect;
1850  nDataSize=ReadPixMapEtc(aBmp, true, false, &aSrcRect, &aDestRect, true, false);
1851  DrawingMethod( PictDrawingMethod::PAINT );
1852  pVirDev->DrawBitmapEx(aDestRect.TopLeft(),aDestRect.GetSize(),aBmp);
1853  break;
1854  }
1855  case 0x009b: { // DirectBitsRgn
1856  BitmapEx aBmp;
1857  tools::Rectangle aSrcRect, aDestRect;
1858  nDataSize=ReadPixMapEtc(aBmp, true, false, &aSrcRect, &aDestRect, true, true);
1859  DrawingMethod( PictDrawingMethod::PAINT );
1860  pVirDev->DrawBitmapEx(aDestRect.TopLeft(),aDestRect.GetSize(),aBmp);
1861  break;
1862  }
1863  case 0x009c: // Reserved (n Bytes)
1864  case 0x009d: // Reserved (n Bytes)
1865  case 0x009e: // Reserved (n Bytes)
1866  case 0x009f: // Reserved (n Bytes)
1867  pPict->ReadUInt16( nUSHORT ); nDataSize=2+nUSHORT;
1868  break;
1869 
1870  case 0x00a0: // ShortComment
1871  nDataSize=2;
1872  break;
1873 
1874  case 0x00a1: // LongComment
1875  pPict->SeekRel(2); pPict->ReadUInt16( nUSHORT ); nDataSize=4+nUSHORT;
1876  break;
1877 
1878  default: // 0x00a2 bis 0xffff (most times reserved)
1879  if (nOpcode<=0x00af) { pPict->ReadUInt16( nUSHORT ); nDataSize=2+nUSHORT; }
1880  else if (nOpcode<=0x00cf) { nDataSize=0; }
1881  else if (nOpcode<=0x00fe) { sal_uInt32 nTemp(0); pPict->ReadUInt32(nTemp) ; nDataSize = nTemp; nDataSize+=4; }
1882  // Osnola: checkme: in the Quickdraw Ref examples ( for pict v2)
1883  // 0x00ff(EndOfPict) is also not followed by any data...
1884  else if (nOpcode==0x00ff) { nDataSize=IsVersion2 ? 2 : 0; } // OpEndPic
1885  else if (nOpcode<=0x01ff) { nDataSize=2; }
1886  else if (nOpcode<=0x0bfe) { nDataSize=4; }
1887  else if (nOpcode<=0x0bff) { nDataSize=22; }
1888  else if (nOpcode==0x0c00) { nDataSize=24; } // HeaderOp
1889  else if (nOpcode<=0x7eff) { nDataSize=24; }
1890  else if (nOpcode<=0x7fff) { nDataSize=254; }
1891  else if (nOpcode<=0x80ff) { nDataSize=0; }
1892  else { sal_uInt32 nTemp(0); pPict->ReadUInt32(nTemp) ; nDataSize = nTemp; nDataSize+=4; }
1893  }
1894 
1895  if (nDataSize==0xffffffff) {
1897  return 0;
1898  }
1899  return nDataSize;
1900 }
1901 
1902 void PictReader::ReadPict( SvStream & rStreamPict, GDIMetaFile & rGDIMetaFile )
1903 {
1904  try {
1905  sal_uInt16 nOpcode;
1906  sal_uInt8 nOneByteOpcode;
1907  sal_uInt64 nSize;
1908 
1909  pPict = &rStreamPict;
1910  nOrigPos = pPict->Tell();
1911  SvStreamEndian nOrigNumberFormat = pPict->GetEndian();
1912 
1913  aActForeColor = COL_BLACK;
1914  aActBackColor = COL_WHITE;
1915  nActPenSize = Size(1,1);
1916  eActROP = RasterOp::OverPaint;
1917  eActMethod = PictDrawingMethod::UNDEFINED;
1918  aActOvalSize = Size(1,1);
1919 
1920  aActFont.SetCharSet( GetTextEncoding());
1921  aActFont.SetFamily(FAMILY_SWISS);
1922  aActFont.SetFontSize(Size(0,12));
1923  aActFont.SetAlignment(ALIGN_BASELINE);
1924 
1925  aHRes = aVRes = Fraction( 1, 1 );
1926 
1927  pVirDev = VclPtr<VirtualDevice>::Create();
1928  pVirDev->EnableOutput(false);
1929  rGDIMetaFile.Record(pVirDev);
1930 
1931  pPict->SetEndian(SvStreamEndian::BIG);
1932 
1933  ReadHeader();
1934 
1935  aPenPosition=Point(-aBoundingRect.Left(),-aBoundingRect.Top());
1936  aTextPosition=aPenPosition;
1937 
1938  sal_uInt64 nPos=pPict->Tell();
1939 
1940  for (;;) {
1941 
1942  if (IsVersion2 )
1943  pPict->ReadUInt16( nOpcode );
1944  else
1945  {
1946  pPict->ReadUChar( nOneByteOpcode );
1947  nOpcode=static_cast<sal_uInt16>(nOneByteOpcode);
1948  }
1949 
1950  if (pPict->GetError())
1951  break;
1952 
1953  if (pPict->eof())
1954  {
1956  break;
1957  }
1958 
1959  if (nOpcode==0x00ff)
1960  break;
1961 
1962  nSize=ReadData(nOpcode);
1963 
1964  if ( IsVersion2 )
1965  {
1966  if ( nSize & 1 )
1967  nSize++;
1968 
1969  nPos+=2+nSize;
1970  }
1971  else
1972  nPos+=1+nSize;
1973 
1974  if (!checkSeek(*pPict, nPos))
1975  {
1977  break;
1978  }
1979  }
1980 
1981  pVirDev->SetClipRegion();
1982  rGDIMetaFile.Stop();
1983  pVirDev.disposeAndClear();
1984 
1985  rGDIMetaFile.SetPrefMapMode( MapMode( MapUnit::MapInch, Point(), aHRes, aVRes ) );
1986  rGDIMetaFile.SetPrefSize( aBoundingRect.GetSize() );
1987 
1988  pPict->SetEndian(nOrigNumberFormat);
1989 
1990  if (pPict->GetError()) pPict->Seek(nOrigPos);
1991  } catch (...)
1992  {
1993  rStreamPict.SetError(SVSTREAM_FILEFORMAT_ERROR);
1994  }
1995 }
1996 
1997 void ReadPictFile(SvStream &rStreamPict, GDIMetaFile& rGDIMetaFile)
1998 {
1999  PictReader aPictReader;
2000  aPictReader.ReadPict(rStreamPict, rGDIMetaFile);
2001 }
2002 
2003 //================== GraphicImport - the exported function ================
2004 
2005 bool ImportPictGraphic( SvStream& rIStm, Graphic & rGraphic)
2006 {
2007  GDIMetaFile aMTF;
2008  bool bRet = false;
2009 
2010  ReadPictFile(rIStm, aMTF);
2011 
2012  if ( !rIStm.GetError() )
2013  {
2014  rGraphic = Graphic( aMTF );
2015  bRet = true;
2016  }
2017 
2018  return bRet;
2019 }
2020 
2021 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SetFamily(FontFamily)
Definition: font/font.cxx:133
void EnableOutput(bool bEnable=true)
Definition: outdev.cxx:334
SvStream & ReadInt16(sal_Int16 &rInt16)
UNDEFINED
void SetFillColor(const Color &)
Definition: font/font.cxx:98
void SetFontSize(const Size &)
Definition: font/font.cxx:127
sal_uInt8 GetRed() const
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_LIGHTMAGENTA
void ReadHeader(SvStream &rStream, ODbaseIndex::NDXHeader &rHeader)
SvStream & ReadUInt16(sal_uInt16 &rUInt16)
tools::Long getWidth() const
constexpr tools::Long Left() const
void DrawBitmapEx(const Point &rDestPt, const BitmapEx &rBitmapEx)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: bitmapex.cxx:33
double ny
SvStream & ReadPolygon(SvStream &rIStream, tools::Polygon &rPoly)
long Long
void disposeAndClear()
Definition: vclptr.hxx:200
void SetPrefSize(const Size &rSize)
Definition: gdimtf.hxx:173
void SetWeight(FontWeight)
Definition: font/font.cxx:225
void drawPolygon(VirtualDevice *dev, bool drawFrame, tools::Polygon const &orig, Size const &pSize)
draws a polygon knowing penSize
Definition: shape.cxx:218
void SetOutline(bool bOutline)
Definition: font/font.cxx:243
sal_uInt64 Seek(sal_uInt64 nPos)
ERASE
void SetSize(sal_uInt16 nNewSize)
void drawRectangle(VirtualDevice *dev, bool drawFrame, tools::Rectangle const &orig, Size const &pSize)
draws a rectangle knowing penSize
Definition: shape.cxx:128
void SetCharSet(rtl_TextEncoding)
Definition: font/font.cxx:139
constexpr::Color COL_LIGHTGRAY(0xC0, 0xC0, 0xC0)
void drawArc(VirtualDevice *dev, bool drawFrame, tools::Rectangle const &orig, const double &angle1, const double &angle2, Size const &pSize)
draws an arc in a b2dpolygon knowing penSize
Definition: shape.cxx:162
void SetPoint(const Point &rPt, sal_uInt16 nPos)
rtl_TextEncoding GetTextEncoding()
enum PenStyle penStyle
Definition: ipict.cxx:78
sal_uInt64 SeekRel(sal_Int64 nPos)
static BitmapColor getColor(const BitmapReadAccess *pReadAcc, tools::Long nZ)
tools::Long getHeight() const
ErrCode GetError() const
void Record(OutputDevice *pOutDev)
Definition: gdimtf.cxx:311
bool eof() const
void ReadPictFile(SvStream &rStreamPict, GDIMetaFile &rGDIMetaFile)
Function to access PictReader::ReadPict for unit testing.
Definition: ipict.cxx:1997
int nCount
void SetUnderline(FontLineStyle)
Definition: font/font.cxx:255
sal_uInt64 remainingSize()
enum BrushStyle brushStyle
Definition: ipict.cxx:79
sal_uInt8 GetBlue() const
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_LIGHTRED
Color color
Definition: ipict.cxx:83
void SetShadow(bool bShadow)
Definition: font/font.cxx:249
SvStream & ReadUInt32(sal_uInt32 &rUInt32)
void SetLineColor()
Definition: line.cxx:36
bool checkSeek(SvStream &rSt, sal_uInt64 nOffset)
void SetFamilyName(const OUString &rFamilyName)
Definition: font/font.cxx:117
bool IsEmpty() const
int i
bool isRead
Definition: ipict.cxx:87
void drawLine(VirtualDevice *dev, Point const &orig, Point const &dest, Size const &pSize)
draws a line from orig to dest knowing penSize
Definition: shape.cxx:111
void SetFillColor()
Definition: fill.cxx:29
PenStyle
Definition: ios2met.cxx:40
constexpr tools::Long Top() const
void SetError(ErrCode nErrorCode)
SvStream & ReadUChar(unsigned char &rChar)
sal_Int16 nVersion
RasterOp
Definition: RasterOp.hxx:22
Intended to be used to feed into CreateFromData to create a BitmapEx.
Definition: RawBitmap.hxx:21
short nBitCount
Definition: ipict.cxx:80
void SetColor(const Color &)
Definition: font/font.cxx:90
Reference< XOutputStream > stream
constexpr Point TopLeft() const
SvStream & ReadInt32(sal_Int32 &rInt32)
void SetAlignment(TextAlign)
Definition: font/font.cxx:111
std::size_t ReadBytes(void *pData, std::size_t nSize)
PictDrawingMethod
Definition: ipict.cxx:145
sal_uInt8 GetGreen() const
SvStreamEndian GetEndian() const
void Stop()
Definition: gdimtf.cxx:536
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_LIGHTCYAN
constexpr Size GetSize() const
SvStream & ReadChar(char &rChar)
double nx
const int nMaxCols
void drawEllipse(VirtualDevice *dev, bool drawFrame, tools::Rectangle const &orig, Size const &pSize)
draws an ellipse knowing penSize
Definition: shape.cxx:147
unsigned char sal_uInt8
void setWidth(tools::Long n)
void SetFont(const vcl::Font &rNewFont)
Definition: outdev/font.cxx:49
static const char * operationName(sal_uInt16 nOpcode)
Definition: ipict.cxx:1277
void SetTransparent(bool bTransparent)
Definition: font/font.cxx:105
void SetEndian(SvStreamEndian SvStreamEndian)
FRAME
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_WHITE
#define SAL_INFO(area, stream)
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_LIGHTGREEN
void SetRasterOp(RasterOp eRasterOp)
Definition: outdev.cxx:313
static VclPtr< reference_type > Create(Arg &&...arg)
A construction helper for VclPtr.
Definition: vclptr.hxx:127
#define SVSTREAM_FILEFORMAT_ERROR
Definition: errcode.hxx:260
sal_uInt64 Tell() const
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_BLACK
BitmapEx CreateFromData(sal_uInt8 const *pData, sal_Int32 nWidth, sal_Int32 nHeight, sal_Int32 nStride, vcl::PixelFormat ePixelFormat, bool bReversColors, bool bReverseAlpha)
Copy block of image data into the bitmap.
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_YELLOW
bool good() const
void SetItalic(FontItalic)
Definition: font/font.cxx:237
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_LIGHTBLUE
#define SAL_WARN(area, stream)
SvStreamEndian
void SetLineColor(const css::uno::Reference< css::beans::XPropertySet > &xGridProperties, sal_Int32 nColor)
virtual void ResetError()
bool ImportPictGraphic(SvStream &rIStm, Graphic &rGraphic)
Definition: ipict.cxx:2005
static void SetByte(sal_uInt16 &nx, sal_uInt16 ny, vcl::bitmap::RawBitmap &rBitmap, sal_uInt16 nPixelSize, sal_uInt8 nDat, sal_uInt16 nWidth, std::vector< Color > const &rvPalette)
Definition: ipict.cxx:271
void drawRoundRectangle(VirtualDevice *dev, bool drawFrame, tools::Rectangle const &orig, Size const &ovalSize, Size const &pSize)
draws a rounded rectangle knowing penSize
Definition: shape.cxx:196
void SetPixel(tools::Long nY, tools::Long nX, Color nColor)
Definition: RawBitmap.hxx:47
void DrawText(const Point &rStartPt, const OUString &rStr, sal_Int32 nIndex=0, sal_Int32 nLen=-1, std::vector< tools::Rectangle > *pVector=nullptr, OUString *pDisplayText=nullptr, const SalLayoutGlyphs *pLayoutCache=nullptr)
Definition: text.cxx:772
TEXT
rtl_TextEncoding GetCharSet() const
Definition: font/font.cxx:838
bool isColor
Definition: ipict.cxx:82
void setHeight(tools::Long n)
sal_uInt16 nPos
void SetPrefMapMode(const MapMode &rMapMode)
Definition: gdimtf.hxx:176