colorspaces.cpp

00001
00002 /***************************************************************************
00003  *  colorspaces.cpp - This header defines utility functions to deal with
00004  *                    color spaces
00005  *
00006  *  Generated: Sat Aug 12 15:26:23 2006
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 #include <fvutils/color/colorspaces.h>
00026
00027 #include <cstring>
00028 #include <cstdlib>
00029
00030 colorspace_t
00031 colorspace_by_name(const char *mode)
00032 {
00033
00034   if (strcmp(mode, "RGB") == 0) {
00035     return RGB;
00036   } else if (strcmp(mode, "YUV411_PACKED") == 0) {
00037     return YUV411_PACKED;
00038   } else if (strcmp(mode, "YUV411_PLANAR") == 0) {
00039     return YUV411_PLANAR;
00040   } else if (strcmp(mode, "YUY2") == 0) {
00041     return YUY2;
00042   } else if (strcmp(mode, "YVY2") == 0) {
00043     return YVY2;
00044   } else if (strcmp(mode, "BGR") == 0) {
00045     return BGR;
00046   } else if (strcmp(mode, "YUV422_PACKED") == 0) {
00047     return YUV422_PACKED;
00048   } else if (strcmp(mode, "YUV444_PACKED") == 0) {
00049     return YUV444_PACKED;
00050   } else if (strcmp(mode, "YVU444_PACKED") == 0) {
00051     return YVU444_PACKED;
00052   } else if (strcmp(mode, "YUV422_PLANAR") == 0) {
00053     return YUV422_PLANAR;
00054   } else if (strcmp(mode, "YUV422_PLANAR_QUARTER") == 0) {
00055     return YUV422_PLANAR_QUARTER;
00056   } else if (strcmp(mode, "GRAY8") == 0) {
00057     return GRAY8;
00058   } else if (strcmp(mode, "RGB_WITH_ALPHA") == 0) {
00059     return RGB_WITH_ALPHA;
00060   } else if (strcmp(mode, "BGR_WITH_ALPHA") == 0) {
00061     return BGR_WITH_ALPHA;
00062   } else if (strcmp(mode, "BAYER_MOSAIC_RGGB") == 0) {
00063     return BAYER_MOSAIC_RGGB;
00064   } else if (strcmp(mode, "BAYER_MOSAIC_GBRG") == 0) {
00065     return BAYER_MOSAIC_GBRG;
00066   } else if (strcmp(mode, "BAYER_MOSAIC_GRBG") == 0) {
00067     return BAYER_MOSAIC_GRBG;
00068   } else if (strcmp(mode, "BAYER_MOSAIC_BGGR") == 0) {
00069     return BAYER_MOSAIC_BGGR;
00070   } else if (strcmp(mode, "RAW16") == 0) {
00071     return RAW16;
00072   } else {
00073     return CS_UNKNOWN;
00074   }
00075
00076 }
00077
00078 const char *
00079 colorspace_to_string(colorspace_t colorspace)
00080 {
00081   switch (colorspace) {
00082   case RGB:
00083     return "RGB";
00084   case YUV411_PACKED:
00085     return "YUV411_PACKED";
00086   case YUV411_PLANAR:
00087     return "YUV411_PLANAR";
00088   case YUY2:
00089     return "YUY2";
00090   case YVY2:
00091     return "YVY2";
00092   case BGR:
00093     return "BGR";
00094   case YUV422_PACKED:
00095     return "YUV422_PACKED";
00096   case YUV444_PACKED:
00097     return "YUV444_PACKED";
00098   case YVU444_PACKED:
00099     return "YVU444_PACKED";
00100   case YUV422_PLANAR:
00101     return "YUV422_PLANAR";
00102   case YUV422_PLANAR_QUARTER:
00103     return "YUV422_PLANAR_QUARTER";
00104   case GRAY8:
00105     return "GRAY8";
00106   case RGB_WITH_ALPHA:
00107     return "RGB_WITH_ALPHA";
00108   case BGR_WITH_ALPHA:
00109     return "BGR_WITH_ALPHA";
00110   case BAYER_MOSAIC_RGGB:
00111     return "BAYER_MOSAIC_RGGB";
00112   case BAYER_MOSAIC_GBRG:
00113     return "BAYER_MOSAIC_GBRG";
00114   case BAYER_MOSAIC_GRBG:
00115     return "BAYER_MOSAIC_GRBG";
00116   case BAYER_MOSAIC_BGGR:
00117     return "BAYER_MOSAIC_BGGR";
00118   case RAW16:
00119     return "RAW16";
00120   default:
00121     return "CS_UNKNOWN";
00122   }
00123 }
00124
00125
00126 unsigned char *
00127 malloc_buffer(colorspace_t colorspace, unsigned int width, unsigned int height)
00128 {
00129   return (unsigned char *)malloc(colorspace_buffer_size(colorspace, width, height));
00130 }
00131
00132 size_t
00133 colorspace_buffer_size(colorspace_t cspace, unsigned int width, unsigned int height)
00134 {
00135   switch (cspace) {
00136   case RGB:
00137   case BGR:
00138   case YUV444_PACKED:
00139   case YVU444_PACKED:
00140     return (width * height * 3);
00141
00142   case RGB_WITH_ALPHA:
00143   case BGR_WITH_ALPHA:
00144     return (width * height * 4);
00145
00146   case YUV411_PACKED:
00147   case YUV411_PLANAR:
00148     return (width * height * 3 / 2);
00149
00150   case YUY2:
00151   case YVY2:
00152   case YUV422_PACKED:
00153   case YUV422_PLANAR:
00154     return (width * height * 2);
00155
00156   case RAW16:
00157     return (width * height * 2);
00158
00159   case GRAY8:
00160   case BAYER_MOSAIC_RGGB:
00161   case BAYER_MOSAIC_GBRG:
00162   case BAYER_MOSAIC_GRBG:
00163   case BAYER_MOSAIC_BGGR:
00164     return (width * height);
00165
00166   case YUV422_PLANAR_QUARTER:
00167     return (width * height) / 2;
00168
00169   default:
00170     return 0;
00171   }
00172 }