1 /***************************************************************************
2  *   Copyright (C) 2007 by Dominik Seichter                                *
3  *   domseichter@web.de                                                    *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU Library General Public License as       *
7  *   published by the Free Software Foundation; either version 2 of the    *
8  *   License, or (at your option) any later version.                       *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU Library General Public     *
16  *   License along with this program; if not, write to the                 *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  *                                                                         *
20  *   In addition, as a special exception, the copyright holders give       *
21  *   permission to link the code of portions of this program with the      *
22  *   OpenSSL library under certain conditions as described in each         *
23  *   individual source file, and distribute linked combinations            *
24  *   including the two.                                                    *
25  *   You must obey the GNU General Public License in all respects          *
26  *   for all of the code used other than OpenSSL.  If you modify           *
27  *   file(s) with this exception, you may extend this exception to your    *
28  *   version of the file(s), but you are not obligated to do so.  If you   *
29  *   do not wish to do so, delete this exception statement from your       *
30  *   version.  If you delete this exception statement from all source      *
31  *   files in the program, then also delete it here.                       *
32  ***************************************************************************/
33 
34 #ifndef _PDF_FILTERS_PRIVATE_H_
35 #define _PDF_FILTERS_PRIVATE_H_
36 
37 /**
38  * \file PdfFiltersPrivate.h
39  *
40  * Provides implementations of various PDF stream filters.
41  *
42  * This is an internal header. It should not be included in podofo.h, and
43  * should not be included directly by client applications. These filters should
44  * only be accessed through the factory interface in PdfFilters.h .
45  */
46 
47 #include "PdfDefines.h"
48 #include "PdfDefinesPrivate.h"
49 #include "PdfFilter.h"
50 #include "PdfRefCountedBuffer.h"
51 
52 #include <zlib.h>
53 
54 #ifdef PODOFO_HAVE_JPEG_LIB
55 extern "C" {
56 #ifdef _WIN32		// Collision between Win32 and libjpeg headers
57 #define XMD_H
58 #undef FAR
59 
60 #ifndef HAVE_BOOLEAN
61 #define HAVE_BOOLEAN
62 #define PODOFO_JPEG_HAVE_BOOLEAN // not to be defined in the build system
63 #endif
64 
65 #endif
66 #include "jpeglib.h"
67 
68 #ifdef PODOFO_JPEG_HAVE_BOOLEAN
69 #undef HAVE_BOOLEAN
70 #endif
71 }
72 #endif // PODOFO_HAVE_JPEG_LIB
73 
74 #ifdef PODOFO_HAVE_TIFF_LIB
75 extern "C" {
76 #include "tiffio.h"
77 #ifdef _WIN32		// Collision between tiff and jpeg-headers
78 #define XMD_H
79 #undef FAR
80 #endif
81 }
82 #endif // PODOFO_HAVE_TIFF_LIB
83 
84 
85 namespace PoDoFo {
86 
87 #define PODOFO_FILTER_INTERNAL_BUFFER_SIZE 4096
88 
89 class PdfPredictorDecoder;
90 class PdfOutputDevice;
91 
92 /** The ascii hex filter.
93  */
94 class PdfHexFilter : public PdfFilter {
95  public:
96     PdfHexFilter();
97 
~PdfHexFilter()98     virtual ~PdfHexFilter() { }
99 
100     /** Check wether the encoding is implemented for this filter.
101      *
102      *  \returns true if the filter is able to encode data
103      */
104     inline virtual bool CanEncode() const;
105 
106     /** Encode a block of data and write it to the PdfOutputStream
107      *  specified by BeginEncodeImpl.
108      *
109      *  BeginEncodeImpl() has to be called before this function.
110      *
111      *  \param pBuffer pointer to a buffer with data to encode
112      *  \param lLen length of data to encode.
113      *
114      *  Call EndEncodeImpl() after all data has been encoded
115      *
116      *
117      *  \see BeginEncodeImpl
118      *  \see EndEncodeImpl
119      */
120     virtual void EncodeBlockImpl( const char* pBuffer, pdf_long lLen );
121 
122     /** Check wether the decoding is implemented for this filter.
123      *
124      *  \returns true if the filter is able to decode data
125      */
126     inline virtual bool CanDecode() const;
127 
128     /** Real implementation of `BeginDecode()'. NEVER call this method directly.
129      *
130      *  By default this function does nothing. If your filter needs to do setup for decoding,
131      *  you should override this method.
132      *
133      *  PdfFilter ensures that a valid stream is available when this method is called, and
134      *  that EndDecode() was called since the last BeginDecode()/DecodeBlock().
135      *
136      * \see BeginDecode */
137     virtual void BeginDecodeImpl( const PdfDictionary* );
138 
139     /** Real implementation of `DecodeBlock()'. NEVER call this method directly.
140      *
141      *  You must override this method to decode the buffer passed by the caller.
142      *
143      *  You are not obliged to immediately process any or all of the data in
144      *  the passed buffer, but you must ensure that you have processed it and
145      *  written it out by the end of EndDecodeImpl(). You must copy the buffer
146      *  if you're going to store it, as ownership is not transferred to the
147      *  filter and the caller may free the buffer at any time.
148      *
149      *  PdfFilter ensures that a valid stream is available when this method is
150      *  called, ensures that BeginDecode() has been called, and ensures that
151      *  EndDecode() has not been called since the last BeginDecode().
152      *
153      * \see DecodeBlock */
154     virtual void DecodeBlockImpl( const char* pBuffer, pdf_long lLen );
155 
156     /** Real implementation of `EndDecode()'. NEVER call this method directly.
157      *
158      * By the time this method returns, all filtered data must be written to the stream
159      * and the filter must be in a state where BeginDecode() can be safely called.
160      *
161      *  PdfFilter ensures that a valid stream is available when this method is
162      *  called, and ensures that BeginDecodeImpl() has been called.
163      *
164      * \see EndDecode */
165     virtual void EndDecodeImpl();
166 
167     /** GetType of this filter.
168      *  \returns the GetType of this filter
169      */
170     inline virtual EPdfFilter GetType() const;
171 
172  private:
173     char m_cDecodedByte;
174     bool m_bLow;
175 };
176 
177 // -----------------------------------------------------
178 //
179 // -----------------------------------------------------
CanEncode()180 bool PdfHexFilter::CanEncode() const
181 {
182     return true;
183 }
184 
185 // -----------------------------------------------------
186 //
187 // -----------------------------------------------------
CanDecode()188 bool PdfHexFilter::CanDecode() const
189 {
190     return true;
191 }
192 
193 // -----------------------------------------------------
194 //
195 // -----------------------------------------------------
GetType()196 EPdfFilter PdfHexFilter::GetType() const
197 {
198     return ePdfFilter_ASCIIHexDecode;
199 }
200 
201 /** The Ascii85 filter.
202  */
203 class PdfAscii85Filter : public PdfFilter {
204  public:
205     PdfAscii85Filter();
~PdfAscii85Filter()206     virtual ~PdfAscii85Filter() { }
207 
208     /** Check wether the encoding is implemented for this filter.
209      *
210      *  \returns true if the filter is able to encode data
211      */
212     inline virtual bool CanEncode() const;
213 
214     /** Begin encoding data using this filter. Called by PdfFilter::BeginEncode.
215      *
216      *  \see EncodeBlockImpl
217      *  \see EndEncodeImpl
218      *  \see PdfFilter::BeginEncode
219      */
220     virtual void BeginEncodeImpl();
221 
222     /** Encode a block of data and write it to the PdfOutputStream
223      *  specified by BeginEncodeImpl.
224      *
225      *  BeginEncodeImpl() has to be called before this function.
226      *
227      *  \param pBuffer pointer to a buffer with data to encode
228      *  \param lLen length of data to encode.
229      *
230      *  Call EndEncodeImpl() after all data has been encoded
231      *
232      *
233      *  \see BeginEncodeImpl
234      *  \see EndEncodeImpl
235      */
236     virtual void EncodeBlockImpl( const char* pBuffer, pdf_long lLen );
237 
238     /**
239      *  Finish encoding of data.
240      *
241      *  \see BeginEncodeImpl
242      *  \see EncodeBlockImpl
243      */
244     virtual void EndEncodeImpl();
245 
246     /** Check wether the decoding is implemented for this filter.
247      *
248      *  \returns true if the filter is able to decode data
249      */
250     inline virtual bool CanDecode() const;
251 
252     /** Real implementation of `BeginDecode()'. NEVER call this method directly.
253      *
254      *  By default this function does nothing. If your filter needs to do setup for decoding,
255      *  you should override this method.
256      *
257      *  PdfFilter ensures that a valid stream is available when this method is called, and
258      *  that EndDecode() was called since the last BeginDecode()/DecodeBlock().
259      *
260      * \see BeginDecode */
261     virtual void BeginDecodeImpl( const PdfDictionary* );
262 
263     /** Real implementation of `DecodeBlock()'. NEVER call this method directly.
264      *
265      *  You must override this method to decode the buffer passed by the caller.
266      *
267      *  You are not obliged to immediately process any or all of the data in
268      *  the passed buffer, but you must ensure that you have processed it and
269      *  written it out by the end of EndDecodeImpl(). You must copy the buffer
270      *  if you're going to store it, as ownership is not transferred to the
271      *  filter and the caller may free the buffer at any time.
272      *
273      *  PdfFilter ensures that a valid stream is available when this method is
274      *  called, ensures that BeginDecode() has been called, and ensures that
275      *  EndDecode() has not been called since the last BeginDecode().
276      *
277      * \see DecodeBlock */
278     virtual void DecodeBlockImpl( const char* pBuffer, pdf_long lLen );
279 
280     /** Real implementation of `EndDecode()'. NEVER call this method directly.
281      *
282      * By the time this method returns, all filtered data must be written to the stream
283      * and the filter must be in a state where BeginDecode() can be safely called.
284      *
285      *  PdfFilter ensures that a valid stream is available when this method is
286      *  called, and ensures that BeginDecodeImpl() has been called.
287      *
288      * \see EndDecode */
289     virtual void EndDecodeImpl();
290 
291     /** GetType of this filter.
292      *  \returns the GetType of this filter
293      */
294     inline virtual EPdfFilter GetType() const;
295 
296  private:
297     void EncodeTuple ( unsigned long tuple, int bytes );
298     void WidePut( unsigned long tuple, int bytes ) const;
299 
300  private:
301     int           m_count;
302     unsigned long m_tuple;
303 };
304 
305 // -----------------------------------------------------
306 //
307 // -----------------------------------------------------
CanEncode()308 bool PdfAscii85Filter::CanEncode() const
309 {
310     return true;
311 }
312 
313 // -----------------------------------------------------
314 //
315 // -----------------------------------------------------
CanDecode()316 bool PdfAscii85Filter::CanDecode() const
317 {
318     return true;
319 }
320 
321 // -----------------------------------------------------
322 //
323 // -----------------------------------------------------
GetType()324 EPdfFilter PdfAscii85Filter::GetType() const
325 {
326     return ePdfFilter_ASCII85Decode;
327 }
328 
329 /** The flate filter.
330  */
331 class PdfFlateFilter : public PdfFilter {
332  public:
333     PdfFlateFilter();
334     virtual ~PdfFlateFilter();
335 
336     /** Check wether the encoding is implemented for this filter.
337      *
338      *  \returns true if the filter is able to encode data
339      */
340     inline virtual bool CanEncode() const;
341 
342     /** Begin encoding data using this filter. Called by PdfFilter::BeginEncode.
343      *
344      *  \see EncodeBlockImpl
345      *  \see EndEncodeImpl
346      *  \see PdfFilter::BeginEncode
347      */
348     virtual void BeginEncodeImpl();
349 
350     /** Encode a block of data and write it to the PdfOutputStream
351      *  specified by BeginEncodeImpl.
352      *
353      *  BeginEncodeImpl() has to be called before this function.
354      *
355      *  \param pBuffer pointer to a buffer with data to encode
356      *  \param lLen length of data to encode.
357      *
358      *  Call EndEncodeImpl() after all data has been encoded
359      *
360      *
361      *  \see BeginEncodeImpl
362      *  \see EndEncodeImpl
363      */
364     virtual void EncodeBlockImpl( const char* pBuffer, pdf_long lLen );
365 
366     /**
367      *  Finish encoding of data.
368      *
369      *  \see BeginEncodeImpl
370      *  \see EncodeBlockImpl
371      */
372     virtual void EndEncodeImpl();
373 
374     /** Check wether the decoding is implemented for this filter.
375      *
376      *  \returns true if the filter is able to decode data
377      */
378     inline virtual bool CanDecode() const;
379 
380     /** Real implementation of `BeginDecode()'. NEVER call this method directly.
381      *
382      *  By default this function does nothing. If your filter needs to do setup for decoding,
383      *  you should override this method.
384      *
385      *  PdfFilter ensures that a valid stream is available when this method is called, and
386      *  that EndDecode() was called since the last BeginDecode()/DecodeBlock().
387      *
388      *  \param pDecodeParms additional parameters for decoding data
389      *
390      * \see BeginDecode
391      */
392     virtual void BeginDecodeImpl( const PdfDictionary* pDecodeParms );
393 
394     /** Real implementation of `DecodeBlock()'. NEVER call this method directly.
395      *
396      *  You must override this method to decode the buffer passed by the caller.
397      *
398      *  You are not obliged to immediately process any or all of the data in
399      *  the passed buffer, but you must ensure that you have processed it and
400      *  written it out by the end of EndDecodeImpl(). You must copy the buffer
401      *  if you're going to store it, as ownership is not transferred to the
402      *  filter and the caller may free the buffer at any time.
403      *
404      *  PdfFilter ensures that a valid stream is available when this method is
405      *  called, ensures that BeginDecode() has been called, and ensures that
406      *  EndDecode() has not been called since the last BeginDecode().
407      *
408      * \see DecodeBlock */
409     virtual void DecodeBlockImpl( const char* pBuffer, pdf_long lLen );
410 
411     /** Real implementation of `EndDecode()'. NEVER call this method directly.
412      *
413      * By the time this method returns, all filtered data must be written to the stream
414      * and the filter must be in a state where BeginDecode() can be safely called.
415      *
416      *  PdfFilter ensures that a valid stream is available when this method is
417      *  called, and ensures that BeginDecodeImpl() has been called.
418      *
419      * \see EndDecode */
420     virtual void EndDecodeImpl();
421 
422     /** GetType of this filter.
423      *  \returns the GetType of this filter
424      */
425     inline virtual EPdfFilter GetType() const;
426 
427  private:
428     void EncodeBlockInternal( const char* pBuffer, pdf_long lLen, int nMode );
429 
430  private:
431     unsigned char        m_buffer[PODOFO_FILTER_INTERNAL_BUFFER_SIZE];
432 
433     z_stream             m_stream;
434     PdfPredictorDecoder* m_pPredictor;
435 };
436 
437 // -----------------------------------------------------
438 //
439 // -----------------------------------------------------
CanEncode()440 bool PdfFlateFilter::CanEncode() const
441 {
442     return true;
443 }
444 
445 // -----------------------------------------------------
446 //
447 // -----------------------------------------------------
CanDecode()448 bool PdfFlateFilter::CanDecode() const
449 {
450     return true;
451 }
452 
453 // -----------------------------------------------------
454 //
455 // -----------------------------------------------------
GetType()456 EPdfFilter PdfFlateFilter::GetType() const
457 {
458     return ePdfFilter_FlateDecode;
459 }
460 
461 
462 /** The RLE filter.
463  */
464 class PdfRLEFilter : public PdfFilter {
465  public:
466     PdfRLEFilter();
~PdfRLEFilter()467     virtual ~PdfRLEFilter() {}
468 
469     /** Check wether the encoding is implemented for this filter.
470      *
471      *  \returns true if the filter is able to encode data
472      */
473     inline virtual bool CanEncode() const;
474 
475     virtual void BeginEncodeImpl();
476 
477     /** Encode a block of data and write it to the PdfOutputStream
478      *  specified by BeginEncodeImpl.
479      *
480      *  BeginEncodeImpl() has to be called before this function.
481      *
482      *  \param pBuffer pointer to a buffer with data to encode
483      *  \param lLen length of data to encode.
484      *
485      *  Call EndEncodeImpl() after all data has been encoded
486      *
487      *
488      *  \see BeginEncodeImpl
489      *  \see EndEncodeImpl
490      */
491     virtual void EncodeBlockImpl( const char* pBuffer, pdf_long lLen );
492 
493     /**
494      *  Finish encoding of data.
495      *
496      *  \see BeginEncodeImpl
497      *  \see EncodeBlockImpl
498      */
499     virtual void EndEncodeImpl();
500 
501     /** Check wether the decoding is implemented for this filter.
502      *
503      *  \returns true if the filter is able to decode data
504      */
505     inline virtual bool CanDecode() const;
506 
507     /** Real implementation of `BeginDecode()'. NEVER call this method directly.
508      *
509      *  By default this function does nothing. If your filter needs to do setup for decoding,
510      *  you should override this method.
511      *
512      *  PdfFilter ensures that a valid stream is available when this method is called, and
513      *  that EndDecode() was called since the last BeginDecode()/DecodeBlock().
514      *
515      * \see BeginDecode */
516     virtual void BeginDecodeImpl( const PdfDictionary* );
517 
518     /** Real implementation of `DecodeBlock()'. NEVER call this method directly.
519      *
520      *  You must override this method to decode the buffer passed by the caller.
521      *
522      *  You are not obliged to immediately process any or all of the data in
523      *  the passed buffer, but you must ensure that you have processed it and
524      *  written it out by the end of EndDecodeImpl(). You must copy the buffer
525      *  if you're going to store it, as ownership is not transferred to the
526      *  filter and the caller may free the buffer at any time.
527      *
528      *  PdfFilter ensures that a valid stream is available when this method is
529      *  called, ensures that BeginDecode() has been called, and ensures that
530      *  EndDecode() has not been called since the last BeginDecode().
531      *
532      * \see DecodeBlock */
533     virtual void DecodeBlockImpl( const char* pBuffer, pdf_long lLen );
534 
535     /** GetType of this filter.
536      *  \returns the GetType of this filter
537      */
538     inline virtual EPdfFilter GetType() const;
539 
540  private:
541     int m_nCodeLen;
542 };
543 
544 // -----------------------------------------------------
545 //
546 // -----------------------------------------------------
CanEncode()547 bool PdfRLEFilter::CanEncode() const
548 {
549     return false;
550 }
551 
552 // -----------------------------------------------------
553 //
554 // -----------------------------------------------------
CanDecode()555 bool PdfRLEFilter::CanDecode() const
556 {
557     return true;
558 }
559 
560 // -----------------------------------------------------
561 //
562 // -----------------------------------------------------
GetType()563 EPdfFilter PdfRLEFilter::GetType() const
564 {
565     return ePdfFilter_RunLengthDecode;
566 }
567 
568 /** The LZW filter.
569  */
570 class PdfLZWFilter : public PdfFilter {
571     struct TLzwItem {
572         std::vector<unsigned char> value;
573     };
574 
575     typedef std::vector<TLzwItem>     TLzwTable;
576     typedef TLzwTable::iterator       TILzwTable;
577     typedef TLzwTable::const_iterator TCILzwTable;
578 
579  public:
580     PdfLZWFilter();
581 
582     virtual ~PdfLZWFilter();
583 
584     /** Check wether the encoding is implemented for this filter.
585      *
586      *  \returns true if the filter is able to encode data
587      */
588     inline virtual bool CanEncode() const;
589 
590     /** Begin encoding data using this filter. Called by PdfFilter::BeginEncode.
591      *
592      *  \see EncodeBlockImpl
593      *  \see EndEncodeImpl
594      *  \see PdfFilter::BeginEncode
595      */
596     virtual void BeginEncodeImpl();
597 
598     /** Encode a block of data and write it to the PdfOutputStream
599      *  specified by BeginEncodeImpl.
600      *
601      *  BeginEncodeImpl() has to be called before this function.
602      *
603      *  \param pBuffer pointer to a buffer with data to encode
604      *  \param lLen length of data to encode.
605      *
606      *  Call EndEncodeImpl() after all data has been encoded
607      *
608      *
609      *  \see BeginEncodeImpl
610      *  \see EndEncodeImpl
611      */
612     virtual void EncodeBlockImpl( const char* pBuffer, pdf_long lLen );
613 
614     /**
615      *  Finish encoding of data.
616      *
617      *  \see BeginEncodeImpl
618      *  \see EncodeBlockImpl
619      */
620     virtual void EndEncodeImpl();
621 
622     /** Check wether the decoding is implemented for this filter.
623      *
624      *  \returns true if the filter is able to decode data
625      */
626     inline virtual bool CanDecode() const;
627 
628     /** Real implementation of `BeginDecode()'. NEVER call this method directly.
629      *
630      *  By default this function does nothing. If your filter needs to do setup for decoding,
631      *  you should override this method.
632      *
633      *  PdfFilter ensures that a valid stream is available when this method is called, and
634      *  that EndDecode() was called since the last BeginDecode()/DecodeBlock().
635      *
636      * \see BeginDecode */
637     virtual void BeginDecodeImpl( const PdfDictionary* );
638 
639     /** Real implementation of `DecodeBlock()'. NEVER call this method directly.
640      *
641      *  You must override this method to decode the buffer passed by the caller.
642      *
643      *  You are not obliged to immediately process any or all of the data in
644      *  the passed buffer, but you must ensure that you have processed it and
645      *  written it out by the end of EndDecodeImpl(). You must copy the buffer
646      *  if you're going to store it, as ownership is not transferred to the
647      *  filter and the caller may free the buffer at any time.
648      *
649      *  PdfFilter ensures that a valid stream is available when this method is
650      *  called, ensures that BeginDecode() has been called, and ensures that
651      *  EndDecode() has not been called since the last BeginDecode().
652      *
653      * \see DecodeBlock */
654     virtual void DecodeBlockImpl( const char* pBuffer, pdf_long lLen );
655 
656     /** Real implementation of `EndDecode()'. NEVER call this method directly.
657      *
658      * By the time this method returns, all filtered data must be written to the stream
659      * and the filter must be in a state where BeginDecode() can be safely called.
660      *
661      *  PdfFilter ensures that a valid stream is available when this method is
662      *  called, and ensures that BeginDecodeImpl() has been called.
663      *
664      * \see EndDecode */
665     virtual void EndDecodeImpl();
666 
667     /** GetType of this filter.
668      *  \returns the GetType of this filter
669      */
670     inline virtual EPdfFilter GetType() const;
671 
672  private:
673     /** Initialize an lzw table.
674      */
675     void InitTable();
676 
677  private:
678     static const unsigned short s_masks[4];
679     static const unsigned short s_clear;
680     static const unsigned short s_eod;
681 
682     TLzwTable     m_table;
683 
684     unsigned int  m_mask;
685     unsigned int  m_code_len;
686     unsigned char m_character;
687 
688     bool          m_bFirst;
689 
690     PdfPredictorDecoder* m_pPredictor;
691 };
692 
693 // -----------------------------------------------------
694 //
695 // -----------------------------------------------------
CanEncode()696 bool PdfLZWFilter::CanEncode() const
697 {
698     return false;
699 }
700 
701 // -----------------------------------------------------
702 //
703 // -----------------------------------------------------
CanDecode()704 bool PdfLZWFilter::CanDecode() const
705 {
706     return true;
707 }
708 
709 // -----------------------------------------------------
710 //
711 // -----------------------------------------------------
GetType()712 EPdfFilter PdfLZWFilter::GetType() const
713 {
714     return ePdfFilter_LZWDecode;
715 }
716 
717 
718 #ifdef PODOFO_HAVE_JPEG_LIB
719 
720 void PODOFO_API jpeg_memory_src (j_decompress_ptr cinfo, const JOCTET * buffer, size_t bufsize);
721 
722 extern "C" {
723 void JPegErrorExit(j_common_ptr cinfo);
724 
725 void JPegErrorOutput(j_common_ptr, int);
726 };
727 
728 /** The DCT filter can decoded JPEG compressed data.
729  *
730  *  This filter requires JPEG lib to be available
731  */
732 class PdfDCTFilter : public PdfFilter {
733  public:
734     PdfDCTFilter();
735 
736     virtual ~PdfDCTFilter();
737 
738     /** Check wether the encoding is implemented for this filter.
739      *
740      *  \returns true if the filter is able to encode data
741      */
742     inline virtual bool CanEncode() const;
743 
744     /** Begin encoding data using this filter. Called by PdfFilter::BeginEncode.
745      *
746      *  \see EncodeBlockImpl
747      *  \see EndEncodeImpl
748      *  \see PdfFilter::BeginEncode
749      */
750     virtual void BeginEncodeImpl();
751 
752     /** Encode a block of data and write it to the PdfOutputStream
753      *  specified by BeginEncodeImpl.
754      *
755      *  BeginEncodeImpl() has to be called before this function.
756      *
757      *  \param pBuffer pointer to a buffer with data to encode
758      *  \param lLen length of data to encode.
759      *
760      *  Call EndEncodeImpl() after all data has been encoded
761      *
762      *
763      *  \see BeginEncodeImpl
764      *  \see EndEncodeImpl
765      */
766     virtual void EncodeBlockImpl( const char* pBuffer, pdf_long lLen );
767 
768     /**
769      *  Finish encoding of data.
770      *
771      *  \see BeginEncodeImpl
772      *  \see EncodeBlockImpl
773      */
774     virtual void EndEncodeImpl();
775 
776     /** Check wether the decoding is implemented for this filter.
777      *
778      *  \returns true if the filter is able to decode data
779      */
780     inline virtual bool CanDecode() const;
781 
782     /** Real implementation of `BeginDecode()'. NEVER call this method directly.
783      *
784      *  By default this function does nothing. If your filter needs to do setup for decoding,
785      *  you should override this method.
786      *
787      *  PdfFilter ensures that a valid stream is available when this method is called, and
788      *  that EndDecode() was called since the last BeginDecode()/DecodeBlock().
789      *
790      * \see BeginDecode */
791     virtual void BeginDecodeImpl( const PdfDictionary* );
792 
793     /** Real implementation of `DecodeBlock()'. NEVER call this method directly.
794      *
795      *  You must override this method to decode the buffer passed by the caller.
796      *
797      *  You are not obliged to immediately process any or all of the data in
798      *  the passed buffer, but you must ensure that you have processed it and
799      *  written it out by the end of EndDecodeImpl(). You must copy the buffer
800      *  if you're going to store it, as ownership is not transferred to the
801      *  filter and the caller may free the buffer at any time.
802      *
803      *  PdfFilter ensures that a valid stream is available when this method is
804      *  called, ensures that BeginDecode() has been called, and ensures that
805      *  EndDecode() has not been called since the last BeginDecode().
806      *
807      * \see DecodeBlock */
808     virtual void DecodeBlockImpl( const char* pBuffer, pdf_long lLen );
809 
810     /** Real implementation of `EndDecode()'. NEVER call this method directly.
811      *
812      * By the time this method returns, all filtered data must be written to the stream
813      * and the filter must be in a state where BeginDecode() can be safely called.
814      *
815      *  PdfFilter ensures that a valid stream is available when this method is
816      *  called, and ensures that BeginDecodeImpl() has been called.
817      *
818      * \see EndDecode */
819     virtual void EndDecodeImpl();
820 
821     /** GetType of this filter.
822      *  \returns the GetType of this filter
823      */
824     inline virtual EPdfFilter GetType() const;
825 
826  private:
827     struct jpeg_decompress_struct m_cinfo;
828     struct jpeg_error_mgr         m_jerr;
829 
830     PdfRefCountedBuffer           m_buffer;
831     PdfOutputDevice*              m_pDevice;
832 };
833 
834 // -----------------------------------------------------
835 //
836 // -----------------------------------------------------
CanEncode()837 bool PdfDCTFilter::CanEncode() const
838 {
839     return false;
840 }
841 
842 // -----------------------------------------------------
843 //
844 // -----------------------------------------------------
CanDecode()845 bool PdfDCTFilter::CanDecode() const
846 {
847     return true;
848 }
849 
850 // -----------------------------------------------------
851 //
852 // -----------------------------------------------------
GetType()853 EPdfFilter PdfDCTFilter::GetType() const
854 {
855     return ePdfFilter_DCTDecode;
856 }
857 #endif // PODOFO_HAVE_JPEG_LIB
858 
859 #ifdef PODOFO_HAVE_TIFF_LIB
860 
861 /** The CCITT filter can decoded CCITTFaxDecode compressed data.
862  *
863  *  This filter requires TIFFlib to be available
864  */
865 class PdfCCITTFilter : public PdfFilter {
866  public:
867     PdfCCITTFilter();
868 
869     virtual ~PdfCCITTFilter();
870 
871     /** Check wether the encoding is implemented for this filter.
872      *
873      *  \returns true if the filter is able to encode data
874      */
875     inline virtual bool CanEncode() const;
876 
877     /** Begin encoding data using this filter. Called by PdfFilter::BeginEncode.
878      *
879      *  \see EncodeBlockImpl
880      *  \see EndEncodeImpl
881      *  \see PdfFilter::BeginEncode
882      */
883     virtual void BeginEncodeImpl();
884 
885     /** Encode a block of data and write it to the PdfOutputStream
886      *  specified by BeginEncodeImpl.
887      *
888      *  BeginEncodeImpl() has to be called before this function.
889      *
890      *  \param pBuffer pointer to a buffer with data to encode
891      *  \param lLen length of data to encode.
892      *
893      *  Call EndEncodeImpl() after all data has been encoded
894      *
895      *
896      *  \see BeginEncodeImpl
897      *  \see EndEncodeImpl
898      */
899     virtual void EncodeBlockImpl( const char* pBuffer, pdf_long lLen );
900 
901     /**
902      *  Finish encoding of data.
903      *
904      *  \see BeginEncodeImpl
905      *  \see EncodeBlockImpl
906      */
907     virtual void EndEncodeImpl();
908 
909     /** Check wether the decoding is implemented for this filter.
910      *
911      *  \returns true if the filter is able to decode data
912      */
913     inline virtual bool CanDecode() const;
914 
915     /** Real implementation of `BeginDecode()'. NEVER call this method directly.
916      *
917      *  By default this function does nothing. If your filter needs to do setup for decoding,
918      *  you should override this method.
919      *
920      *  PdfFilter ensures that a valid stream is available when this method is called, and
921      *  that EndDecode() was called since the last BeginDecode()/DecodeBlock().
922      *
923      * \see BeginDecode */
924     virtual void BeginDecodeImpl( const PdfDictionary* );
925 
926     /** Real implementation of `DecodeBlock()'. NEVER call this method directly.
927      *
928      *  You must override this method to decode the buffer passed by the caller.
929      *
930      *  You are not obliged to immediately process any or all of the data in
931      *  the passed buffer, but you must ensure that you have processed it and
932      *  written it out by the end of EndDecodeImpl(). You must copy the buffer
933      *  if you're going to store it, as ownership is not transferred to the
934      *  filter and the caller may free the buffer at any time.
935      *
936      *  PdfFilter ensures that a valid stream is available when this method is
937      *  called, ensures that BeginDecode() has been called, and ensures that
938      *  EndDecode() has not been called since the last BeginDecode().
939      *
940      * \see DecodeBlock */
941     virtual void DecodeBlockImpl( const char* pBuffer, pdf_long lLen );
942 
943     /** Real implementation of `EndDecode()'. NEVER call this method directly.
944      *
945      * By the time this method returns, all filtered data must be written to the stream
946      * and the filter must be in a state where BeginDecode() can be safely called.
947      *
948      *  PdfFilter ensures that a valid stream is available when this method is
949      *  called, and ensures that BeginDecodeImpl() has been called.
950      *
951      * \see EndDecode */
952     virtual void EndDecodeImpl();
953 
954     /** GetType of this filter.
955      *  \returns the GetType of this filter
956      */
957     inline virtual EPdfFilter GetType() const;
958 
959  private:
960     TIFF* m_tiff;
961 };
962 
963 // -----------------------------------------------------
964 //
965 // -----------------------------------------------------
CanEncode()966 bool PdfCCITTFilter::CanEncode() const
967 {
968     return false;
969 }
970 
971 // -----------------------------------------------------
972 //
973 // -----------------------------------------------------
CanDecode()974 bool PdfCCITTFilter::CanDecode() const
975 {
976     return true;
977 }
978 
979 // -----------------------------------------------------
980 //
981 // -----------------------------------------------------
GetType()982 EPdfFilter PdfCCITTFilter::GetType() const
983 {
984     return ePdfFilter_CCITTFaxDecode;
985 }
986 #endif // PODOFO_HAVE_TIFF_LIB
987 
988 };
989 
990 
991 #endif /* _PDF_FILTERS_PRIVATE_H_ */
992