invert.cpp

00001 
00002 /***************************************************************************
00003  *  invert.cpp - implementation of invert filter
00004  *
00005  *  Created: Mon Jun 05 12:47:18 2006
00006  *  Copyright  2005-2007  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #include <filters/invert.h>
00025 
00026 #include <core/exceptions/software.h>
00027 #include <fvutils/color/yuv.h>
00028 #include <cstddef>
00029 
00030 namespace firevision {
00031 #if 0 /* just to make Emacs auto-indent happy */
00032 }
00033 #endif
00034 
00035 /** @class FilterInvert <filters/invert.h>
00036  * Inversion filter.
00037  * This will invert the given image.
00038  * @author Tim Niemueller
00039  */
00040 
00041 /** Constructor. */
00042 FilterInvert::FilterInvert()
00043   : Filter("FilterInvert")
00044 {
00045 }
00046 
00047 
00048 void
00049 FilterInvert::apply()
00050 {
00051   if ( src[0] == NULL )     throw fawkes::NullPointerException("FilterInvert: src buffer is NULL");
00052   if ( src_roi[0] == NULL ) throw fawkes::NullPointerException("FilterInvert: src ROI is NULL");
00053 
00054   if ( (dst == NULL) || (dst == src[0]) ) {
00055     // In-place
00056 
00057     register unsigned int h = 0;
00058     register unsigned int w = 0;
00059 
00060     // y-plane
00061     register unsigned char *yp = src[0] + (src_roi[0]->start.y * src_roi[0]->line_step) + (src_roi[0]->start.x * src_roi[0]->pixel_step);
00062     
00063     // line starts
00064     unsigned char *lyp  = yp;   // y-plane
00065 
00066     for (h = 0; h < src_roi[0]->height; ++h) {
00067       for (w = 0; w < src_roi[0]->width; ++w) {
00068         *yp = 255 - *yp;
00069         ++yp;
00070       }
00071       lyp  += src_roi[0]->line_step;
00072       yp    = lyp;
00073     }
00074 
00075   } else {
00076 
00077     register unsigned int h = 0;
00078     register unsigned int w = 0;
00079 
00080     // y-plane
00081     register unsigned char *yp   = src[0] + (src_roi[0]->start.y * src_roi[0]->line_step) + (src_roi[0]->start.x * src_roi[0]->pixel_step);
00082     // u-plane
00083     register unsigned char *up   = YUV422_PLANAR_U_PLANE(src[0], src_roi[0]->image_width, src_roi[0]->image_height)
00084       + ((src_roi[0]->start.y * src_roi[0]->line_step) / 2 + (src_roi[0]->start.x * src_roi[0]->pixel_step) / 2) ;
00085     // v-plane
00086     register unsigned char *vp   = YUV422_PLANAR_V_PLANE(src[0], src_roi[0]->image_width, src_roi[0]->image_height)
00087       + ((src_roi[0]->start.y * src_roi[0]->line_step) / 2 + (src_roi[0]->start.x * src_roi[0]->pixel_step) / 2);
00088 
00089     // destination y-plane
00090     register unsigned char *dyp  = dst + (dst_roi->start.y * dst_roi->line_step) + (dst_roi->start.x * dst_roi->pixel_step);
00091     // destination u-plane
00092     register unsigned char *dup   = YUV422_PLANAR_U_PLANE(dst, dst_roi->image_width, dst_roi->image_height)
00093       + ((dst_roi->start.y * dst_roi->line_step) / 2 + (dst_roi->start.x * dst_roi->pixel_step) / 2) ;
00094     // destination v-plane
00095     register unsigned char *dvp   = YUV422_PLANAR_V_PLANE(dst, dst_roi->image_width, dst_roi->image_height)
00096       + ((dst_roi->start.y * dst_roi->line_step) / 2 + (dst_roi->start.x * dst_roi->pixel_step) / 2);
00097 
00098     // line starts
00099     unsigned char *lyp  = yp;   // y-plane
00100     unsigned char *lup  = up;   // u-plane
00101     unsigned char *lvp  = vp;   // v-plane
00102     unsigned char *ldyp = dyp;  // destination y-plane
00103     unsigned char *ldup = dup;  // destination u-plane
00104     unsigned char *ldvp = dvp;  // destination v-plane
00105 
00106     for (h = 0; (h < src_roi[0]->height) && (h < dst_roi->height); ++h) {
00107       for (w = 0; (w < src_roi[0]->width) && (w < dst_roi->width); w += 2) {
00108         *dyp++ = 255 - *yp++;
00109         *dyp++ = 255 - *yp++;
00110         *dup++ = *up++;
00111         *dvp++ = *vp++;
00112       }
00113 
00114       lyp   += src_roi[0]->line_step;
00115       lup   += src_roi[0]->line_step / 2;
00116       lvp   += src_roi[0]->line_step / 2;
00117       ldyp  += dst_roi->line_step;
00118       ldup  += dst_roi->line_step / 2;
00119       ldvp  += dst_roi->line_step / 2;
00120       yp     = lyp;
00121       up     = lup;
00122       vp     = lvp;
00123       dyp    = ldyp;
00124       dup    = ldup;
00125       dvp    = ldvp;
00126     }
00127   }
00128 
00129 }
00130 
00131 } // end namespace firevision