Qt 4.8
Enumerations | Functions | Variables
qprintengine_ps.cpp File Reference
#include "qplatformdefs.h"
#include <private/qprintengine_ps_p.h>
#include <private/qpainter_p.h>
#include <private/qfontengine_p.h>
#include <private/qpaintengine_p.h>
#include <private/qpdf_p.h>
#include "qprinter.h"
#include "qpainter.h"
#include "qapplication.h"
#include "qpixmap.h"
#include "qimage.h"
#include "qdatetime.h"
#include "qstring.h"
#include "qbytearray.h"
#include "qhash.h"
#include "qbuffer.h"
#include "qsettings.h"
#include "qmap.h"
#include "qbitmap.h"
#include "qregion.h"
#include "qimagewriter.h"
#include <private/qpainterpath_p.h>
#include <qdebug.h>
#include <private/qdrawhelper_p.h>
#include <private/qmutexpool_p.h>
#include <stdlib.h>
#include <limits.h>

Go to the source code of this file.

Enumerations

enum  format { Raw, Runlength, DCT }
 

Functions

static QByteArray compressHelper (const QImage &image, bool gray, int *format)
 
static void ignoreSigPipe (bool b)
 
static void ps_r7 (QPdf::ByteStream &stream, const char *s, int l)
 
void qt_generate_epsf (bool b)
 
static QByteArray runlengthEncode (const QByteArray &input)
 
static QByteArray wrapDSC (const QByteArray &str)
 

Variables

static const char *const filters [3]
 
static const int max_in_memory_size = 2000000
 
static const char *const ps_header
 
static bool qt_gen_epsf = false
 

Enumeration Type Documentation

◆ format

enum format
Enumerator
Raw 
Runlength 
DCT 

Definition at line 286 of file qprintengine_ps.cpp.

286  {
287  Raw,
288  Runlength,
289  DCT
290 };

Function Documentation

◆ compressHelper()

static QByteArray compressHelper ( const QImage image,
bool  gray,
int *  format 
)
static

Definition at line 297 of file qprintengine_ps.cpp.

Referenced by QPSPrintEnginePrivate::drawImageHelper().

298 {
299  // we can't use premultiplied here
300  QByteArray pixelData;
301  int depth = image.depth();
302 
304 
305  if (depth != 1 && !gray && QImageWriter::supportedImageFormats().contains("jpeg")) {
306  QBuffer buffer(&pixelData);
307  QImageWriter writer(&buffer, "jpeg");
308  writer.setQuality(94);
309  writer.write(image);
310  *format = DCT;
311  } else {
312  int width = image.width();
313  int height = image.height();
314  int size = width*height;
315 
316  if (depth == 1)
317  size = (width+7)/8*height;
318  else if (!gray)
319  size = size*3;
320 
321  pixelData.resize(size);
322  uchar *pixel = (uchar *)pixelData.data();
323  int i = 0;
324  if (depth == 1) {
325  QImage::Format format = image.format();
326  memset(pixel, 0xff, size);
327  for(int y=0; y < height; y++) {
328  const uchar * s = image.scanLine(y);
329  for(int x=0; x < width; x++) {
330  // need to copy bit for bit...
331  bool b = (format == QImage::Format_MonoLSB) ?
332  (*(s + (x >> 3)) >> (x & 7)) & 1 :
333  (*(s + (x >> 3)) << (x & 7)) & 0x80 ;
334  if (b)
335  pixel[i >> 3] ^= (0x80 >> (i & 7));
336  i++;
337  }
338  // we need to align to 8 bit here
339  i = (i+7) & 0xffffff8;
340  }
341  } else if (depth == 8) {
342  for(int y=0; y < height; y++) {
343  const uchar * s = image.scanLine(y);
344  for(int x=0; x < width; x++) {
345  QRgb rgb = image.color(s[x]);
346  if (gray) {
347  pixel[i] = (unsigned char) qGray(rgb);
348  i++;
349  } else {
350  pixel[i] = (unsigned char) qRed(rgb);
351  pixel[i+1] = (unsigned char) qGreen(rgb);
352  pixel[i+2] = (unsigned char) qBlue(rgb);
353  i += 3;
354  }
355  }
356  }
357  } else {
358  for(int y=0; y < height; y++) {
359  QRgb * s = (QRgb*)(image.scanLine(y));
360  for(int x=0; x < width; x++) {
361  QRgb rgb = (*s++);
362  if (gray) {
363  pixel[i] = (unsigned char) qGray(rgb);
364  i++;
365  } else {
366  pixel[i] = (unsigned char) qRed(rgb);
367  pixel[i+1] = (unsigned char) qGreen(rgb);
368  pixel[i+2] = (unsigned char) qBlue(rgb);
369  i += 3;
370  }
371  }
372  }
373  }
374  *format = Raw;
375  if (depth == 1) {
376  pixelData = runlengthEncode(pixelData);
377  *format = Runlength;
378  }
379  }
380  QByteArray outarr = QPdf::ascii85Encode(pixelData);
381  return outarr;
382 }
Format
The following image formats are available in Qt.
Definition: qimage.h:91
unsigned int QRgb
Definition: qrgb.h:53
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
int qRed(QRgb rgb)
Returns the red component of the ARGB quadruplet rgb.
Definition: qrgb.h:57
Format format() const
Returns the format of the image.
Definition: qimage.cpp:2305
unsigned char uchar
Definition: qglobal.h:994
QBool contains(const T &t) const
Returns true if the list contains an occurrence of value; otherwise returns false.
Definition: qlist.h:880
The QImageWriter class provides a format independent interface for writing images to files or other d...
Definition: qimagewriter.h:59
int qGray(int r, int g, int b)
Definition: qrgb.h:75
int depth() const
Returns the depth of the image.
Definition: qimage.cpp:1620
#define rgb(r, g, b)
Definition: qcolor_p.cpp:130
int width() const
Returns the width of the image.
Definition: qimage.cpp:1557
void resize(int size)
Sets the size of the byte array to size bytes.
int height() const
Returns the height of the image.
Definition: qimage.cpp:1572
static QList< QByteArray > supportedImageFormats()
Returns the list of image formats supported by QImageWriter.
int qGreen(QRgb rgb)
Returns the green component of the ARGB quadruplet rgb.
Definition: qrgb.h:60
static QByteArray runlengthEncode(const QByteArray &input)
int qBlue(QRgb rgb)
Returns the blue component of the ARGB quadruplet rgb.
Definition: qrgb.h:63
QRgb color(int i) const
Returns the color in the color table at index i.
Definition: qimage.cpp:1829
uchar * scanLine(int)
Returns a pointer to the pixel data at the scanline with index i.
Definition: qimage.cpp:1886
QByteArray ascii85Encode(const QByteArray &input)
Definition: qpdf.cpp:806

◆ ignoreSigPipe()

static void ignoreSigPipe ( bool  b)
static

Definition at line 671 of file qprintengine_ps.cpp.

Referenced by QPSPrintEngine::end(), and QPSPrintEngine::newPage().

672 {
673 #ifndef QT_NO_LPR
674  static struct sigaction *users_sigpipe_handler = 0;
675  static int lockCount = 0;
676 
677 #ifndef QT_NO_THREAD
678  QMutexLocker locker(QMutexPool::globalInstanceGet(&users_sigpipe_handler));
679 #endif
680 
681  if (b) {
682  if (lockCount++ > 0)
683  return;
684 
685  if (users_sigpipe_handler != 0)
686  return; // already ignoring sigpipe
687 
688  users_sigpipe_handler = new struct sigaction;
689  struct sigaction tmp_sigpipe_handler;
690  tmp_sigpipe_handler.sa_handler = SIG_IGN;
691  sigemptyset(&tmp_sigpipe_handler.sa_mask);
692  tmp_sigpipe_handler.sa_flags = 0;
693 
694  if (sigaction(SIGPIPE, &tmp_sigpipe_handler, users_sigpipe_handler) == -1) {
695  delete users_sigpipe_handler;
696  users_sigpipe_handler = 0;
697  }
698  }
699  else {
700  if (--lockCount > 0)
701  return;
702 
703  if (users_sigpipe_handler == 0)
704  return; // not ignoring sigpipe
705 
706  if (sigaction(SIGPIPE, users_sigpipe_handler, 0) == -1)
707  qWarning("QPSPrintEngine: Could not restore SIGPIPE handler");
708 
709  delete users_sigpipe_handler;
710  users_sigpipe_handler = 0;
711  }
712 #else
713  Q_UNUSED(b);
714 #endif
715 }
Q_CORE_EXPORT void qWarning(const char *,...)
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
int sigaction(int, const struct sigaction *, struct sigaction *)
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
static QMutex * globalInstanceGet(const void *address)
Returns a QMutex from the global mutex pool.
Definition: qmutexpool.cpp:150

◆ ps_r7()

static void ps_r7 ( QPdf::ByteStream stream,
const char *  s,
int  l 
)
static

Definition at line 200 of file qprintengine_ps.cpp.

Referenced by QPSPrintEnginePrivate::drawImageHelper().

201 {
202  int i = 0;
203  uchar line[84];
204  int col = 0;
205 
206  while(i < l) {
207  line[col++] = s[i++];
208  if (i < l - 1 && col >= 76) {
209  line[col++] = '\n';
210  line[col++] = '\0';
211  stream << (const char *)line;
212  col = 0;
213  }
214  }
215  if (col > 0) {
216  while((col&3) != 0)
217  line[col++] = '%'; // use a comment as padding
218  line[col++] = '\n';
219  line[col++] = '\0';
220  stream << (const char *)line;
221  }
222 }
unsigned char uchar
Definition: qglobal.h:994
QFactoryLoader * l

◆ qt_generate_epsf()

void qt_generate_epsf ( bool  b)

Definition at line 82 of file qprintengine_ps.cpp.

83 {
84  qt_gen_epsf = b;
85 }
static bool qt_gen_epsf

◆ runlengthEncode()

static QByteArray runlengthEncode ( const QByteArray input)
static

Definition at line 224 of file qprintengine_ps.cpp.

Referenced by compressHelper().

225 {
226  if (!input.length())
227  return input;
228 
229  const char *data = input.constData();
230 
231  QByteArray out;
232  int start = 0;
233  char last = *data;
234 
235  enum State {
236  Undef,
237  Equal,
238  Diff
239  };
240  State state = Undef;
241 
242  int i = 1;
243  int written = 0;
244  while (1) {
245  bool flush = (i == input.size());
246  if (!flush) {
247  switch(state) {
248  case Undef:
249  state = (last == data[i]) ? Equal : Diff;
250  break;
251  case Equal:
252  if (data[i] != last)
253  flush = true;
254  break;
255  case Diff:
256  if (data[i] == last) {
257  --i;
258  flush = true;
259  }
260  }
261  }
262  if (flush || i - start == 128) {
263  int size = i - start;
264  if (state == Equal) {
265  out.append((char)(uchar)(257-size));
266  out.append(last);
267  written += size;
268  } else {
269  out.append((char)(uchar)size-1);
270  while (start < i)
271  out.append(data[start++]);
272  written += size;
273  }
274  state = Undef;
275  start = i;
276  if (i == input.size())
277  break;
278  }
279  last = data[i];
280  ++i;
281  };
282  out.append((char)(uchar)128);
283  return out;
284 }
QByteArray & append(char c)
Appends the character ch to this byte array.
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
unsigned char uchar
Definition: qglobal.h:994
static const char * data(const QByteArray &arr)
The State element defines configurations of objects and properties.
int length() const
Same as size().
Definition: qbytearray.h:356
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
QTextStream & flush(QTextStream &stream)
Calls QTextStream::flush() on stream and returns stream.

◆ wrapDSC()

static QByteArray wrapDSC ( const QByteArray str)
static

Definition at line 153 of file qprintengine_ps.cpp.

Referenced by QPSPrintEnginePrivate::emitHeader(), and QPSPrintEngine::end().

154 {
155  QByteArray dsc = str.simplified();
156  const int wrapAt = 254;
157  QByteArray wrapped;
158  if (dsc.length() < wrapAt)
159  wrapped = dsc;
160  else {
161  wrapped = dsc.left(wrapAt);
162  QByteArray tmp = dsc.mid(wrapAt);
163  while (tmp.length() > wrapAt-3) {
164  wrapped += "\n%%+" + tmp.left(wrapAt-3);
165  tmp = tmp.mid(wrapAt-3);
166  }
167  wrapped += "\n%%+" + tmp;
168  }
169  return wrapped + '\n';
170 }
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QByteArray simplified() const
Returns a byte array that has whitespace removed from the start and the end, and which has each seque...
QByteArray left(int len) const
Returns a byte array that contains the leftmost len bytes of this byte array.
QByteArray mid(int index, int len=-1) const
Returns a byte array containing len bytes from this byte array, starting at position pos...
int length() const
Same as size().
Definition: qbytearray.h:356

Variable Documentation

◆ filters

const char* const filters[3]
static

◆ max_in_memory_size

const int max_in_memory_size = 2000000
static

Definition at line 611 of file qprintengine_ps.cpp.

Referenced by QPSPrintEnginePrivate::flushPage().

◆ ps_header

const char* const ps_header
static

Definition at line 87 of file qprintengine_ps.cpp.

Referenced by QPSPrintEnginePrivate::emitHeader().

◆ qt_gen_epsf

bool qt_gen_epsf = false
static

Definition at line 80 of file qprintengine_ps.cpp.

Referenced by QPSPrintEnginePrivate::emitHeader(), and qt_generate_epsf().