Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * conversions.h - Conversions 00004 * 00005 * Created: Sat Aug 12 15:19:31 2006 00006 * based on colorspaces.h from Tue Feb 23 13:49:38 2005 00007 * Copyright 2005-2006 Tim Niemueller [www.niemueller.de] 00008 * 00009 ****************************************************************************/ 00010 00011 /* This program is free software; you can redistribute it and/or modify 00012 * it under the terms of the GNU General Public License as published by 00013 * the Free Software Foundation; either version 2 of the License, or 00014 * (at your option) any later version. A runtime exception applies to 00015 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00016 * 00017 * This program is distributed in the hope that it will be useful, 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 * GNU Library General Public License for more details. 00021 * 00022 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00023 */ 00024 00025 #ifndef __FIREVISION_UTILS_COLOR_CONVERSIONS_H 00026 #define __FIREVISION_UTILS_COLOR_CONVERSIONS_H 00027 00028 #include <fvutils/color/yuv.h> 00029 #include <fvutils/color/rgb.h> 00030 #include <fvutils/color/yuvrgb.h> 00031 #include <fvutils/color/rgbyuv.h> 00032 #include <fvutils/color/bayer.h> 00033 #include <fvutils/color/colorspaces.h> 00034 00035 #include <core/exception.h> 00036 #include <cstring> 00037 00038 namespace firevision { 00039 #if 0 /* just to make Emacs auto-indent happy */ 00040 } 00041 #endif 00042 00043 /** Convert image from one colorspace to another. 00044 * This is a convenience method for unified access to all conversion routines 00045 * available in FireVision. 00046 * @param from colorspace of the src buffer 00047 * @param to colorspace to convert to 00048 * @param src source buffer 00049 * @param dst destination buffer 00050 * @param width width of image in pixels 00051 * @param height height of image in pixels 00052 * @exception Exception thrown, if the desired conversion combination is not 00053 * available. 00054 */ 00055 inline void 00056 convert(colorspace_t from, colorspace_t to, 00057 const unsigned char *src, unsigned char *dst, 00058 unsigned int width, unsigned int height) 00059 { 00060 if (from == to) { 00061 if ( src != dst ) { 00062 memcpy(dst, src, colorspace_buffer_size(from, width, height)); 00063 } 00064 } else if ( (from == YUV422_PACKED) && (to == YUV422_PLANAR) ) { 00065 yuv422packed_to_yuv422planar(src, dst, width, height); 00066 } else if ( (from == YUY2) && (to == YUV422_PLANAR_QUARTER) ) { 00067 yuy2_to_yuv422planar_quarter(src, dst, width, height); 00068 } else if ( (from == YUY2) && (to == YUV422_PLANAR) ) { 00069 yuy2_to_yuv422planar(src, dst, width, height); 00070 } else if ( (from == YVY2) && (to == YUV422_PLANAR) ) { 00071 yvy2_to_yuv422planar(src, dst, width, height); 00072 00073 #if ( \ 00074 defined __i386__ || \ 00075 defined __386__ || \ 00076 defined __X86__ || \ 00077 defined _M_IX86 || \ 00078 defined i386 \ 00079 ) 00080 } else if ( (from == YUV411_PLANAR) && (to == RGB) ) { 00081 yuv411planar_to_rgb_mmx(src, dst, width, height); 00082 #endif 00083 } else if ( (from == BGR) && (to == RGB) ) { 00084 bgr_to_rgb_plainc(src, dst, width, height); 00085 } else if ( (from == RGB) && (to == YUV411_PACKED) ) { 00086 rgb_to_yuv411packed_plainc(src, dst, width, height); 00087 } else if ( (from == RGB) && (to == YUV422_PLANAR) ) { 00088 rgb_to_yuv422planar_plainc(src, dst, width, height); 00089 } else if ( (from == RGB) && (to == YUV422_PACKED) ) { 00090 rgb_to_yuv422packed_plainc(src, dst, width, height); 00091 } else if ( (from == BGR) && (to == YUV422_PLANAR) ) { 00092 bgr_to_yuv422planar_plainc(src, dst, width, height); 00093 } else if ( (from == GRAY8) && (to == YUY2) ) { 00094 gray8_to_yuy2(src, dst, width, height); 00095 } else if ( (from == GRAY8) && (to == YUV422_PLANAR) ) { 00096 gray8_to_yuv422planar_plainc(src, dst, width, height); 00097 } else if ( (from == MONO8) && (to == YUV422_PLANAR) ) { 00098 gray8_to_yuv422planar_plainc(src, dst, width, height); 00099 } else if ( (from == YUV422_PLANAR) && (to == YUV422_PACKED) ) { 00100 yuv422planar_to_yuv422packed(src, dst, width, height); 00101 } else if ( (from == YUV422_PLANAR_QUARTER) && (to == YUV422_PACKED) ) { 00102 yuv422planar_quarter_to_yuv422packed(src, dst, width, height); 00103 } else if ( (from == YUV422_PLANAR_QUARTER) && (to == YUV422_PLANAR) ) { 00104 yuv422planar_quarter_to_yuv422planar(src, dst, width, height); 00105 } else if ( (from == YUV422_PLANAR) && (to == RGB) ) { 00106 yuv422planar_to_rgb_plainc(src, dst, width, height); 00107 } else if ( (from == YUV422_PACKED) && (to == RGB) ) { 00108 yuv422packed_to_rgb_plainc(src, dst, width, height); 00109 } else if ( (from == YUV422_PLANAR) && (to == BGR) ) { 00110 yuv422planar_to_bgr_plainc(src, dst, width, height); 00111 } else if ( (from == YUV422_PLANAR) && (to == RGB_WITH_ALPHA) ) { 00112 yuv422planar_to_rgb_with_alpha_plainc(src, dst, width, height); 00113 } else if ( (from == RGB) && (to == RGB_WITH_ALPHA) ) { 00114 rgb_to_rgb_with_alpha_plainc(src, dst, width, height); 00115 } else if ( (from == RGB) && (to == BGR_WITH_ALPHA) ) { 00116 rgb_to_bgr_with_alpha_plainc(src, dst, width, height); 00117 } else if ( (from == YUV422_PLANAR) && (to == BGR_WITH_ALPHA) ) { 00118 yuv422planar_to_bgr_with_alpha_plainc(src, dst, width, height); 00119 } else if ( (from == YUV422_PACKED) && (to == BGR_WITH_ALPHA) ) { 00120 yuv422packed_to_bgr_with_alpha_plainc(src, dst, width, height); 00121 } else if ( (from == BAYER_MOSAIC_GBRG) && (to == YUV422_PLANAR) ) { 00122 bayerGBRG_to_yuv422planar_bilinear(src, dst, width, height); 00123 } else if ( (from == BAYER_MOSAIC_GRBG) && (to == YUV422_PLANAR) ) { 00124 bayerGRBG_to_yuv422planar_nearest_neighbour(src, dst, width, height); 00125 } else if ( (from == BAYER_MOSAIC_GRBG) && (to == YUV422_PLANAR) ) { 00126 bayerGRBG_to_yuv422planar_bilinear(src, dst, width, height); 00127 } else if ( (from == YUV444_PACKED) && (to == YUV422_PLANAR) ) { 00128 yuv444packed_to_yuv422planar(src, dst, width, height); 00129 } else if ( (from == YUV444_PACKED) && (to == YUV422_PACKED) ) { 00130 yuv444packed_to_yuv422packed(src, dst, width, height); 00131 } else if ( (from == YVU444_PACKED) && (to == YUV422_PLANAR) ) { 00132 yvu444packed_to_yuv422planar(src, dst, width, height); 00133 } else if ( (from == YVU444_PACKED) && (to == YUV422_PACKED) ) { 00134 yvu444packed_to_yuv422packed(src, dst, width, height); 00135 } else { 00136 throw fawkes::Exception("Cannot convert image data from %s to %s", 00137 colorspace_to_string(from), 00138 colorspace_to_string(to)); 00139 } 00140 } 00141 00142 00143 inline void 00144 grayscale(colorspace_t cspace, 00145 unsigned char *src, unsigned char *dst, 00146 unsigned int width, unsigned int height) 00147 { 00148 switch (cspace) { 00149 case YUV422_PACKED: 00150 grayscale_yuv422packed(src, dst, width, height); 00151 break; 00152 case YUV422_PLANAR: 00153 grayscale_yuv422planar(src, dst, width, height); 00154 break; 00155 default: 00156 fawkes::Exception e("FirevisionUtils: Cannot grayscale image. " 00157 "Images from colorspace %s are not supported.", 00158 colorspace_to_string(cspace)); 00159 throw e; 00160 } 00161 } 00162 00163 } // end namespace firevision 00164 00165 #endif