📄 vconvert.cxx
字号:
/* * vconvert.cxx * * Classes to support streaming video input (grabbing) and output. * * Portable Windows Library * * Copyright (c) 1993-2000 Equivalence Pty. Ltd. * * The contents of this file are subject to the Mozilla Public License * Version 1.0 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. * * The Original Code is Portable Windows Library. * * The Initial Developer of the Original Code is Equivalence Pty. Ltd. * * Contributor(s): Derek Smithies (derek@indranet.co.nz) * Thorsten Westheider (thorsten.westheider@teleos-web.de) * Mark Cooke (mpc@star.sr.bham.ac.uk) * * $Log: vconvert.cxx,v $ * Revision 1.41 2005/08/09 09:08:11 rjongbloed * Merged new video code from branch back to the trunk. * * Revision 1.40.6.2 2005/07/17 12:58:15 rjongbloed * Sorted out the ordering or Red. Blue, Cr and Cb in RGB/BGR/YUV420 formats * * Revision 1.40.6.1 2005/07/17 09:27:08 rjongbloed * Major revisions of the PWLib video subsystem including: * removal of F suffix on colour formats for vertical flipping, all done with existing bool * working through use of RGB and BGR formats so now consistent * cleaning up the plug in system to use virtuals instead of pointers to functions. * rewrite of SDL to be a plug in compatible video output device. * extensive enhancement of video test program * * Revision 1.40 2005/01/25 06:35:28 csoutheren * Removed warnings under MSVC * * Revision 1.39 2005/01/04 07:44:03 csoutheren * More changes to implement the new configuration methodology, and also to * attack the global static problem * * Revision 1.38 2004/11/07 06:28:00 rjongbloed * Removed warnings about unused parameters in SBGGR8 conversion functions. Left one * in (flip vertical) as it should be implemented but wasn't. * * Revision 1.37 2004/10/27 09:24:18 dsandras * Added patch from Nicola Orru' to convert from SBGGR8 to YUV420P. Thanks! * * Revision 1.36 2004/09/21 13:01:08 dsandras * Added conversion from sbggr to rgb thanks to an anonymous patcher. * * Revision 1.35 2003/11/23 22:17:35 dsandras * Added YUV420P to BGR24 and BGR32 conversion. * * Revision 1.34 2003/06/14 02:57:36 rjongbloed * REmoved redundent parameter, grey scale does not have rgb increment! * * Revision 1.33 2003/06/09 22:37:24 dereksmithies * Fix from Clive Nicolson to make b/w colour conversions work (i.e. grey palette). * many thanks! * * Revision 1.32 2003/04/03 09:28:37 robertj * Added reversed RGB byte order versions (BGR24), thanks Damien Sandras * * Revision 1.31 2003/03/31 11:30:14 rogerh * make 'cb' and 'cr' contain the values that their name implies. * * Revision 1.30 2002/09/01 23:00:05 dereks * Fix noise in flipped RGB image. Thanks Alex Phtahov. * * Revision 1.29 2002/02/26 02:23:21 dereks * Reduced verbosity in PTRACE output for when video is enabled. * * Revision 1.28 2002/02/20 02:37:26 dereks * Initial release of Firewire camera support for linux. * Many thanks to Ryutaroh Matsumoto <ryutaroh@rmatsumoto.org>. * * Revision 1.27 2002/02/03 19:55:57 dereks * *** empty log message *** * * Revision 1.26 2002/01/08 01:32:50 robertj * Tidied up some PTRACE debug output. * * Revision 1.25 2002/01/04 04:11:45 dereks * Add video flip code from Walter Whitlock, which flips code at the grabber. * * Revision 1.24 2001/12/08 00:33:11 robertj * Changed some (unsigned int *) to (DWORD *) as the latter is assured to be a * pointer to a 32 bit integer and the former is not. * * Revision 1.23 2001/12/06 22:14:45 dereks * Improve YUV 422 resize routine so it now subsamples as required. * * Revision 1.22 2001/12/03 02:21:50 dereks * Add YUV420P to RGB24F, RGB32F converters. * * Revision 1.21 2001/12/02 21:53:56 dereks * Additional debug information * * Revision 1.20 2001/11/28 04:43:10 robertj * Added synonym colour class for equivalent colour format strings. * Allowed for setting ancestor classes in PCOLOUR_CONVERTER() macro. * Moved static functions into internal class to avoid pasing lots of parameters. * Added conversions for flipped RGB colour formats. * * Revision 1.19 2001/09/06 02:06:36 robertj * Fixed bug in detecting size mismatch, thanks Vjacheslav Andrejev * * Revision 1.18 2001/08/22 02:14:08 robertj * Fixed MSVC compatibility. * * Revision 1.17 2001/08/22 02:06:17 robertj * Resolved confusion with YUV411P and YUV420P video formats, thanks Mark Cooke. * * Revision 1.16 2001/08/20 07:01:26 robertj * Fixed wierd problems with YUV411P and YUV420P formats, thanks Mark Cooke. * * Revision 1.15 2001/08/16 23:17:29 robertj * Added 420P to 411P converter, thanks Mark Cooke. * * Revision 1.14 2001/08/03 10:13:56 robertj * Changes to previous check in to support MSVC. * * Revision 1.13 2001/08/03 04:21:51 dereks * Add colour/size conversion for YUV422->YUV411P * Add Get/Set Brightness,Contrast,Hue,Colour for PVideoDevice, and * Linux PVideoInputDevice. * Add lots of PTRACE statement for debugging colour conversion. * Add support for Sony Vaio laptop under linux. Requires 2.4.7 kernel. * * Revision 1.12 2001/07/20 05:23:51 robertj * Added YUV411P to RGB24 converter. * * Revision 1.11 2001/05/14 05:10:38 robertj * Fixed problems with video colour converters registration, could not rely * on static PList being initialised before all registration instances. * * Revision 1.10 2001/03/20 02:21:57 robertj * More enhancements from Mark Cooke * * Revision 1.9 2001/03/08 23:36:03 robertj * Added backward compatibility SetFrameSize() function. * Added internal SimpleConvert() function for same type converters. * Fixed some documentation. * * Revision 1.8 2001/03/08 08:31:34 robertj * Numerous enhancements to the video grabbing code including resizing * infrastructure to converters. Thanks a LOT, Mark Cooke. * * Revision 1.7 2001/03/07 01:39:56 dereks * Fix image flip (top to bottom) in YUV411P to RGB24 conversion * * Revision 1.6 2001/03/06 23:48:32 robertj * Fixed naming convention on video converter classes. * * Revision 1.5 2001/03/03 23:25:07 robertj * Fixed use of video conversion function, returning bytes in destination frame. * * Revision 1.4 2001/03/03 06:13:01 robertj * Major upgrade of video conversion and grabbing classes. * * Revision 1.3 2001/03/03 05:06:31 robertj * Major upgrade of video conversion and grabbing classes. * * Revision 1.2 2000/12/19 23:58:14 robertj * Fixed MSVC compatibility issues. * * Revision 1.1 2000/12/19 22:20:26 dereks * Add video channel classes to connect to the PwLib PVideoInputDevice class. * Add PFakeVideoInput class to generate test images for video. */#include <ptlib.h>#ifdef __GNUC__#pragma implementation "vconvert.h"#endif#include <ptlib/vconvert.h>static PColourConverterRegistration * RegisteredColourConvertersListHead = NULL;PSYNONYM_COLOUR_CONVERTER(SBGGR8, SBGGR8);PSYNONYM_COLOUR_CONVERTER(Grey, Grey);PSYNONYM_COLOUR_CONVERTER(RGB24, RGB24);PSYNONYM_COLOUR_CONVERTER(BGR24, BGR24);PSYNONYM_COLOUR_CONVERTER(RGB32, RGB32);PSYNONYM_COLOUR_CONVERTER(BGR32, BGR32);PSYNONYM_COLOUR_CONVERTER(YUV411P,YUV411P);PSYNONYM_COLOUR_CONVERTER(YUV420P,YUV420P);PSYNONYM_COLOUR_CONVERTER(YUV420P,IYUV);PSYNONYM_COLOUR_CONVERTER(IYUV, YUV420P);PSYNONYM_COLOUR_CONVERTER(YUV420P,I420);PSYNONYM_COLOUR_CONVERTER(I420, YUV420P);class PStandardColourConverter : public PColourConverter{ PCLASSINFO(PStandardColourConverter, PColourConverter); protected: PStandardColourConverter( const PString & srcFmt, const PString & dstFmt, unsigned w, unsigned h ) : PColourConverter(srcFmt, dstFmt, w, h) { } BOOL SBGGR8toYUV420P( const BYTE * srgb, BYTE * rgb, PINDEX * bytesReturned ) const; BOOL SBGGR8toRGB( const BYTE * srgb, BYTE * rgb, PINDEX * bytesReturned ) const; void GreytoYUV420PSameSize( const BYTE * rgb, BYTE * yuv ) const; void GreytoYUV420PWithResize( const BYTE * rgb, BYTE * yuv ) const; BOOL GreytoYUV420P( const BYTE * rgb, BYTE * yuv, PINDEX * bytesReturned ) const; void RGBtoYUV420PSameSize( const BYTE * rgb, BYTE * yuv, unsigned rgbIncrement, unsigned redOffset, unsigned blueOffset ) const; void RGBtoYUV420PWithResize( const BYTE * rgb, BYTE * yuv, unsigned rgbIncrement, unsigned redOffset, unsigned blueOffset ) const; BOOL RGBtoYUV420P( const BYTE * rgb, BYTE * yuv, PINDEX * bytesReturned, unsigned rgbIncrement, unsigned redOffset, unsigned blueOffset ) const; BOOL YUV420PtoRGB( const BYTE * yuv, BYTE * rgb, PINDEX * bytesReturned, unsigned rgbIncrement, unsigned redOffset, unsigned blueOffset ) const; BOOL SwapRedAndBlue( const BYTE * src, BYTE * dst, PINDEX * bytesReturned, unsigned srcIncrement, unsigned dstIncrement ) const; void ResizeYUV422( const BYTE * src, BYTE * dest ) const;};#define PSTANDARD_COLOUR_CONVERTER(from,to) \ PCOLOUR_CONVERTER2(P_##from##_##to,PStandardColourConverter,#from,#to)///////////////////////////////////////////////////////////////////////////////// PColourConverterPColourConverterRegistration::PColourConverterRegistration(const PString & srcColourFormat, const PString & destColourFormat) : PCaselessString(srcColourFormat+'\t'+destColourFormat){ PColourConverterRegistration * test = RegisteredColourConvertersListHead; while (test != NULL) { if (*test == *this) return; test = test->link; } link = RegisteredColourConvertersListHead; RegisteredColourConvertersListHead = this;}PColourConverter * PColourConverter::Create(const PString & srcColourFormat, const PString & destColourFormat, unsigned width, unsigned height){ PString converterName = srcColourFormat + '\t' + destColourFormat; PColourConverterRegistration * find = RegisteredColourConvertersListHead; while (find != NULL) { if (*find == converterName) { return find->Create(width, height); } find = find->link; } PTRACE(2,"PColCnv\tCreate error. Did not find " << srcColourFormat << "->" << destColourFormat); return NULL;}PColourConverter::PColourConverter(const PString & src, const PString & dst, unsigned width, unsigned height) : srcColourFormat(src), dstColourFormat(dst){ PTRACE(6,"PColCnv\tPColourConverter constructed: " << src << "->" << dst << ' ' << width << 'x'<< height); verticalFlip = FALSE; SetFrameSize(width,height);}BOOL PColourConverter::SetFrameSize(unsigned width, unsigned height){ BOOL ok1 = SetSrcFrameSize(width, height); BOOL ok2 = SetDstFrameSize(width, height, FALSE); PTRACE(2,"PColCnv\tSetFrameSize: " << width << 'x' << height << (ok1 && ok2 ? " OK" : " Failed")); return ok1 && ok2;}BOOL PColourConverter::SetSrcFrameSize(unsigned width, unsigned height){ srcFrameWidth = width; srcFrameHeight = height; srcFrameBytes = PVideoDevice::CalculateFrameBytes(srcFrameWidth, srcFrameHeight, srcColourFormat); PTRACE(srcFrameBytes != 0 ? 6 : 2, "PColCnv\tSetSrcFrameSize " << ((srcFrameBytes != 0) ? "Succeed": "Fail") << "ed, " << srcColourFormat << ' ' << srcFrameWidth << 'x' << srcFrameHeight << ", " << srcFrameBytes << " bytes."); return srcFrameBytes != 0;}BOOL PColourConverter::SetDstFrameSize(unsigned width, unsigned height, BOOL bScale){ dstFrameWidth = width; dstFrameHeight = height; scaleNotCrop = bScale; dstFrameBytes = PVideoDevice::CalculateFrameBytes(dstFrameWidth, dstFrameHeight, dstColourFormat); PTRACE(dstFrameBytes != 0 ? 6 : 2, "PColCnv\tSetDstFrameSize " << ((dstFrameBytes != 0) ? "Succeed": "Fail") << "ed, " << dstColourFormat << ' ' << dstFrameWidth << 'x' << dstFrameHeight << ", " << dstFrameBytes << " bytes."); return dstFrameBytes != 0;}BOOL PColourConverter::GetSrcFrameSize(unsigned &width, unsigned &height) const{ width = srcFrameWidth; height = srcFrameHeight; return TRUE;}BOOL PColourConverter::GetDstFrameSize(unsigned &width, unsigned &height) const{ width = dstFrameWidth; height = dstFrameHeight; return TRUE;}BOOL PColourConverter::ConvertInPlace(BYTE * frameBuffer, PINDEX * bytesReturned, BOOL noIntermediateFrame){ if (Convert(frameBuffer, frameBuffer, bytesReturned)) return TRUE; if (noIntermediateFrame) { PTRACE(2,"PColCnv\tError in ConvertInPlace, no intermediate frame available."); return FALSE; } BYTE * intermediate = intermediateFrameStore.GetPointer(dstFrameBytes); PINDEX bytes; if (!Convert(frameBuffer, intermediate, &bytes)) return FALSE; memcpy(frameBuffer, intermediate, bytes); if (bytesReturned != NULL) *bytesReturned = bytes; return TRUE;}///////////////////////////////////////////////////////////////////////////////PSynonymColourRegistration::PSynonymColourRegistration(const char * srcFmt, const char * dstFmt)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -