PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vconvert.h
Go to the documentation of this file.
1 /*
2  * vconvert.h
3  *
4  * Classes to support streaming video input (grabbing) and output.
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 1993-2000 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Contributor(s): Derek Smithies (derek@indranet.co.nz)
25  * Thorsten Westheider (thorsten.westheider@teleos-web.de)
26  * Mark Cooke (mpc@star.sr.bham.ac.uk)
27  */
28 
29 #ifndef PTLIB_CONVERT_H
30 #define PTLIB_CONVERT_H
31 
32 #ifdef P_USE_PRAGMA
33 #pragma interface
34 #endif
35 
36 #include <ptlib.h>
37 
38 #if P_VIDEO
39 
40 #include <ptlib/videoio.h>
41 
42 struct jdec_private;
43 
44 
47 class PColourPair : public PObject
48 {
50  public:
51  PColourPair(const PString & src, const PString & dst)
52  : m_srcColourFormat(src)
53  , m_dstColourFormat(dst)
54  { }
55 
56  Comparison Compare(const PObject & other) const;
57 
60  const PString & GetSrcColourFormat() const { return m_srcColourFormat; }
61 
64  const PString & GetDstColourFormat() const { return m_dstColourFormat; }
65 
66  protected:
69 };
70 
75 {
76  PCLASSINFO(PColourConverter, PColourPair);
77  protected:
81  const PColourPair & colours
82  );
83 
84  public:
86  virtual void PrintOn(
87  ostream & strm
88  ) const;
89 
92  bool GetVFlipState() const
93  { return m_verticalFlip; }
94 
98  bool vFlipState
99  ) { m_verticalFlip = vFlipState; }
100 
105  virtual PBoolean SetFrameSize(
106  unsigned width,
107  unsigned height
108  );
109 
118  virtual PBoolean SetSrcFrameInfo(
119  const PVideoFrameInfo & info
120  );
121 
130  virtual PBoolean SetDstFrameInfo(
131  const PVideoFrameInfo & info
132  );
133 
136  virtual void GetSrcFrameInfo(
137  PVideoFrameInfo & info
138  );
139 
142  virtual void GetDstFrameInfo(
143  PVideoFrameInfo & info
144  );
145 
152  virtual PBoolean SetSrcFrameSize(
153  unsigned width,
154  unsigned height
155  );
156 
163  virtual PBoolean SetDstFrameSize(
164  unsigned width,
165  unsigned height
166  );
167  virtual PBoolean SetDstFrameSize(
168  unsigned width,
169  unsigned height,
170  PBoolean bScale
171  );
172 
178  PINDEX GetMaxSrcFrameBytes() const { return m_srcFrameBytes; }
179 
185  void SetSrcFrameBytes(PINDEX frameBytes) { m_srcFrameBytes = frameBytes; }
186 
192  PINDEX GetMaxDstFrameBytes() const { return m_dstFrameBytes; }
193 
194 
204  virtual PBoolean Convert(
205  const BYTE * srcFrameBuffer,
206  BYTE * dstFrameBuffer,
207  PINDEX * bytesReturned = NULL
208  ) = 0;
209 
226  virtual PBoolean ConvertInPlace(
227  BYTE * frameBuffer,
228  PINDEX * bytesReturned = NULL,
229  PBoolean noIntermediateFrame = false
230  );
231 
232 
237  static PColourConverter * Create(
238  const PVideoFrameInfo & src,
239  const PVideoFrameInfo & dst
240  );
241  static PColourConverter * Create(
242  const PString & srcColourFormat,
243  const PString & destColourFormat,
244  unsigned width,
245  unsigned height
246  );
247 
251  unsigned & width,
252  unsigned & height
253  ) const;
254 
258  unsigned & width,
259  unsigned & height
260  ) const;
261 
262  unsigned GetSrcFrameWidth() const { return m_srcFrameWidth; }
263  unsigned GetSrcFrameHeight() const { return m_srcFrameHeight; }
264  unsigned GetDstFrameWidth() const { return m_dstFrameWidth; }
265  unsigned GetDstFrameHeight() const { return m_dstFrameHeight; }
266 
270  PVideoFrameInfo::ResizeMode mode
271  ) { if (mode < PVideoFrameInfo::eMaxResizeMode) m_resizeMode = mode; }
272 
275  PVideoFrameInfo::ResizeMode GetResizeMode() const { return m_resizeMode; }
276 
279  static void RGBtoYUV(
280  unsigned r, unsigned g, unsigned b,
281  BYTE & y, BYTE & u, BYTE & v
282  );
283 
286  static void YUVtoRGB(
287  unsigned y, unsigned u, unsigned v,
288  BYTE & r, BYTE & g, BYTE & b
289  );
290 
294  static bool CopyYUV420P(
295  unsigned srcX, unsigned srcY, unsigned srcWidth, unsigned srcHeight,
296  unsigned srcFrameWidth, unsigned srcFrameHeight, const BYTE * srcYUV,
297  unsigned dstX, unsigned dstY, unsigned dstWidth, unsigned dstHeight,
298  unsigned dstFrameWidth, unsigned dstFrameHeight, BYTE * dstYUV,
299  PVideoFrameInfo::ResizeMode resizeMode = PVideoFrameInfo::eScale,
300  bool verticalFlip = false, std::ostream * error = NULL
301  );
302 
309  static bool RotateYUV420P(
310  int angle, unsigned width, unsigned height, BYTE * srcYUV, BYTE * dstYUV = NULL
311  );
312 
315  static bool FillYUV420P(
316  unsigned x, unsigned y, unsigned width, unsigned height,
317  unsigned frameWidth, unsigned frameHeight, BYTE * yuv,
318  unsigned r_or_y, unsigned g_or_u, unsigned b_or_v,
319  bool rgb = true
320  );
321 
322  protected:
323  unsigned m_srcFrameWidth;
326 
327  // Needed for resizing
328  unsigned m_dstFrameWidth;
331 
332  PVideoFrameInfo::ResizeMode m_resizeMode;
334 
336 
337  P_REMOVE_VIRTUAL(PBoolean,Convert(const BYTE*,BYTE*,unsigned,PINDEX*),false);
338 };
339 
341 
342 
348 #define PCOLOUR_CONVERTER2(cls,ancestor,srcFmt,dstFmt) \
349 class cls : public ancestor { \
350  public: \
351  cls() : ancestor(PColourPair(srcFmt, dstFmt)) { } \
352  virtual PBoolean Convert(const BYTE *, BYTE *, PINDEX * = NULL); \
353 }; \
354 PFACTORY_CREATE(PColourConverterFactory, cls, PColourPair(srcFmt, dstFmt)); \
355 PBoolean cls::Convert(const BYTE *srcFrameBuffer, BYTE *dstFrameBuffer, PINDEX * bytesReturned) \
356 
357 
363 #define PCOLOUR_CONVERTER(cls,src,dst) \
364  PCOLOUR_CONVERTER2(cls,PColourConverter,src,dst)
365 
366 
367 
368 #if P_JPEG_DECODER
369 
378 class PJPEGConverter : public PColourConverter
379 {
380  protected:
381  struct Context;
382  Context * m_context;
383 
384  public:
387  PJPEGConverter();
390  PJPEGConverter(
391  unsigned width,
392  unsigned height,
393  PVideoFrameInfo::ResizeMode resizeMode = PVideoFrameInfo::eScaleKeepAspect,
394  const PString & colourFormat = PVideoFrameInfo::YUV420P()
395  );
399  PJPEGConverter(
400  const PColourPair & colours
401  );
402 
405  PJPEGConverter(
406  const PVideoFrameInfo & src,
407  const PVideoFrameInfo & dst
408  );
409 
411  ~PJPEGConverter();
412 
416  virtual PBoolean Convert(
417  const BYTE * srcFrameBuffer,
418  BYTE * dstFrameBuffer,
419  PINDEX * bytesReturned = NULL
420  );
421 
424  bool Load(
425  const PFilePath & filename,
426  PBYTEArray & dstFrameBuffer
427  );
428  bool Load(
429  PFile & file,
430  PBYTEArray & dstFrameBuffer
431  );
432 };
433 
434 #endif // P_JPEG_DECODER
435 
436 #endif // P_VIDEO
437 
438 #endif // PTLIB_CONVERT_H
439 
440 
441 // End of file ///////////////////////////////////////////////////////////////
virtual void GetSrcFrameInfo(PVideoFrameInfo &info)
Get the source frame info to be used.
P_REMOVE_VIRTUAL(PBoolean, Convert(const BYTE *, BYTE *, unsigned, PINDEX *), false)
static void RGBtoYUV(unsigned r, unsigned g, unsigned b, BYTE &y, BYTE &u, BYTE &v)
Convert RGB to YUV.
virtual void PrintOn(ostream &strm) const
Print description of converter.
virtual PBoolean SetDstFrameSize(unsigned width, unsigned height)
Set the destination frame size to be used.
This class represents a disk file.
Definition: file.h:57
PBYTEArray m_intermediateFrameStore
Definition: vconvert.h:335
virtual PBoolean SetFrameSize(unsigned width, unsigned height)
Set the frame size to be used.
virtual PBoolean SetSrcFrameSize(unsigned width, unsigned height)
Set the source frame size to be used.
virtual void GetDstFrameInfo(PVideoFrameInfo &info)
Get the destination frame info to be used.
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:2164
unsigned GetDstFrameHeight() const
Definition: vconvert.h:265
virtual PBoolean ConvertInPlace(BYTE *frameBuffer, PINDEX *bytesReturned=NULL, PBoolean noIntermediateFrame=false)
Convert from one colour format to another.
PVideoFrameInfo::ResizeMode m_resizeMode
Definition: vconvert.h:332
This class describes a full description for a file on the particular platform.
Definition: filepath.h:61
Comparison
Result of the comparison operation performed by the Compare() function.
Definition: object.h:2251
void SetSrcFrameBytes(PINDEX frameBytes)
Set the actual frame size in bytes for source frames.
Definition: vconvert.h:185
PColourConverter(const PColourPair &colours)
Create a new converter.
unsigned GetDstFrameWidth() const
Definition: vconvert.h:264
This class contains a pair of colour formats for conversion.
Definition: vconvert.h:47
PColourPair(const PString &src, const PString &dst)
Definition: vconvert.h:51
static const PString & YUV420P()
virtual PBoolean Convert(const BYTE *srcFrameBuffer, BYTE *dstFrameBuffer, PINDEX *bytesReturned=NULL)=0
Convert from one colour format to another.
unsigned m_dstFrameWidth
Definition: vconvert.h:328
PINDEX m_srcFrameBytes
Definition: vconvert.h:325
Comparison Compare(const PObject &other) const
Compare the two objects and return their relative rank.
unsigned GetSrcFrameWidth() const
Definition: vconvert.h:262
Definition: videoio.h:49
Array of unsigned characters.
Definition: array.h:605
static void YUVtoRGB(unsigned y, unsigned u, unsigned v, BYTE &r, BYTE &g, BYTE &b)
Convert YUV to RGB.
const PString & GetDstColourFormat() const
Get the destination colour format.
Definition: vconvert.h:64
PBoolean GetSrcFrameSize(unsigned &width, unsigned &height) const
Get the input frame size.
void SetResizeMode(PVideoFrameInfo::ResizeMode mode)
Set the resize mode to be used.
Definition: vconvert.h:269
unsigned GetSrcFrameHeight() const
Definition: vconvert.h:263
bool PBoolean
Definition: object.h:174
bool GetVFlipState() const
Get the video conversion vertical flip state.
Definition: vconvert.h:92
virtual PBoolean SetDstFrameInfo(const PVideoFrameInfo &info)
Set the destination frame info to be used.
const PString m_srcColourFormat
Definition: vconvert.h:67
The character string class.
Definition: pstring.h:108
static PColourConverter * Create(const PVideoFrameInfo &src, const PVideoFrameInfo &dst)
Create an instance of a colour conversion function.
PBoolean GetDstFrameSize(unsigned &width, unsigned &height) const
Get the output frame size.
unsigned m_dstFrameHeight
Definition: vconvert.h:329
Class for a factory to create concrete class instances without parameters during construction.
Definition: pfactory.h:396
This class defines a means to convert an image from one colour format to another. ...
Definition: vconvert.h:74
PINDEX GetMaxSrcFrameBytes() const
Get the maximum frame size in bytes for source frames.
Definition: vconvert.h:178
bool m_verticalFlip
Definition: vconvert.h:333
unsigned m_srcFrameWidth
Definition: vconvert.h:323
static bool CopyYUV420P(unsigned srcX, unsigned srcY, unsigned srcWidth, unsigned srcHeight, unsigned srcFrameWidth, unsigned srcFrameHeight, const BYTE *srcYUV, unsigned dstX, unsigned dstY, unsigned dstWidth, unsigned dstHeight, unsigned dstFrameWidth, unsigned dstFrameHeight, BYTE *dstYUV, PVideoFrameInfo::ResizeMode resizeMode=PVideoFrameInfo::eScale, bool verticalFlip=false, std::ostream *error=NULL)
Copy a section of the source frame to a section of the destination frame with scaling/cropping as req...
PINDEX m_dstFrameBytes
Definition: vconvert.h:330
unsigned m_srcFrameHeight
Definition: vconvert.h:324
const PString & GetSrcColourFormat() const
Get the source colour format.
Definition: vconvert.h:60
PFactory< PColourConverter, PColourPair > PColourConverterFactory
Definition: vconvert.h:340
PINDEX GetMaxDstFrameBytes() const
Get the maximum frame size in bytes for destination frames.
Definition: vconvert.h:192
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
const PString m_dstColourFormat
Definition: vconvert.h:68
PVideoFrameInfo::ResizeMode GetResizeMode() const
Get the resize mode to be used.
Definition: vconvert.h:275
void SetVFlipState(bool vFlipState)
Set the video conversion vertical flip state.
Definition: vconvert.h:97
virtual PBoolean SetSrcFrameInfo(const PVideoFrameInfo &info)
Set the source frame info to be used.
static bool RotateYUV420P(int angle, unsigned width, unsigned height, BYTE *srcYUV, BYTE *dstYUV=NULL)
Rotate the video buffer image.
static bool FillYUV420P(unsigned x, unsigned y, unsigned width, unsigned height, unsigned frameWidth, unsigned frameHeight, BYTE *yuv, unsigned r_or_y, unsigned g_or_u, unsigned b_or_v, bool rgb=true)
Fill a rectangle of the video buffer with the specified colour.