difference.cpp

00001
00002 /***************************************************************************
00003  *  difference.cpp - implementation of difference intensity filter
00004  *
00005  *  Created: Sun Jun 25 23:07:35 2006 (on train to Ac, father in hospital)
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/difference.h>
00025
00026 #include <fvutils/color/yuv.h>
00027 #include <cstddef>
00028 
00029 /** @class FilterDifference <filters/difference.h>
00030  * Calculates the difference of two images.
00031  */
00032 
00033 /** Constructor. */
00034 FilterDifference::FilterDifference()
00035   : Filter("FilterDifference", 2)
00036 {
00037 }
00038
00039
00040 void
00041 FilterDifference::apply()
00042 {
00043   if ( src[0] == NULL ) return;
00044   if ( src[1] == NULL ) return;
00045   if ( src_roi[0] == NULL ) return;
00046   if ( src_roi[1] == NULL ) return;
00047
00048   register unsigned int h = 0;
00049   register unsigned int w = 0;
00050
00051
00052   // y-plane
00053   register unsigned char *byp   = src[0] + (src_roi[0]->start.y * src_roi[0]->line_step) + (src_roi[0]->start.x * src_roi[0]->pixel_step);
00054   // u-plane
00055   register unsigned char *bup   = YUV422_PLANAR_U_PLANE(src[0], src_roi[0]->image_width, src_roi[0]->image_height)
00056     + ((src_roi[0]->start.y * src_roi[0]->line_step) / 2 + (src_roi[0]->start.x * src_roi[0]->pixel_step) / 2) ;
00057   // v-plane
00058   register unsigned char *bvp   = YUV422_PLANAR_V_PLANE(src[0], src_roi[0]->image_width, src_roi[0]->image_height)
00059     + ((src_roi[0]->start.y * src_roi[0]->line_step) / 2 + (src_roi[0]->start.x * src_roi[0]->pixel_step) / 2);
00060
00061
00062   // y-plane
00063   register unsigned char *fyp   = src[1] + (src_roi[1]->start.y * src_roi[1]->line_step) + (src_roi[1]->start.x * src_roi[1]->pixel_step);
00064   // u-plane
00065   register unsigned char *fup   = YUV422_PLANAR_U_PLANE(src[1], src_roi[1]->image_width, src_roi[1]->image_height)
00066     + ((src_roi[1]->start.y * src_roi[1]->line_step) / 2 + (src_roi[1]->start.x * src_roi[1]->pixel_step) / 2) ;
00067   // v-plane
00068   register unsigned char *fvp   = YUV422_PLANAR_V_PLANE(src[1], src_roi[1]->image_width, src_roi[1]->image_height)
00069     + ((src_roi[1]->start.y * src_roi[1]->line_step) / 2 + (src_roi[1]->start.x * src_roi[1]->pixel_step) / 2);
00070
00071
00072   // destination y-plane
00073   register unsigned char *dyp  = dst + (dst_roi->start.y * dst_roi->line_step) + (dst_roi->start.x * dst_roi->pixel_step);
00074   // destination u-plane
00075   register unsigned char *dup   = YUV422_PLANAR_U_PLANE(dst, dst_roi->image_width, dst_roi->image_height)
00076     + ((dst_roi->start.y * dst_roi->line_step) / 2 + (dst_roi->start.x * dst_roi->pixel_step) / 2) ;
00077   // destination v-plane
00078   register unsigned char *dvp   = YUV422_PLANAR_V_PLANE(dst, dst_roi->image_width, dst_roi->image_height)
00079     + ((dst_roi->start.y * dst_roi->line_step) / 2 + (dst_roi->start.x * dst_roi->pixel_step) / 2);
00080
00081   // line starts
00082   unsigned char *lbyp = byp;   // y-plane
00083   unsigned char *lbup = fup;   // u-plane
00084   unsigned char *lbvp = fvp;   // v-plane
00085   unsigned char *lfyp = fyp;   // y-plane
00086   unsigned char *lfup = fup;   // u-plane
00087   unsigned char *lfvp = fvp;   // v-plane
00088   unsigned char *ldyp = dyp;  // destination y-plane
00089   unsigned char *ldup = dup;  // destination u-plane
00090   unsigned char *ldvp = dvp;  // destination v-plane
00091
00092   for (h = 0; (h < src_roi[1]->height) && (h < dst_roi->height); ++h) {
00093     for (w = 0; (w < src_roi[1]->width) && (w < dst_roi->width); w += 2) {
00094       *dyp++ = ((*byp - *fyp) < 0) ? 0 : (*byp - *fyp);
00095       ++byp; ++fyp;
00096       *dyp++ = ((*byp - *fyp) < 0) ? 0 : (*byp - *fyp);
00097       ++byp; ++fyp;
00098
00099       *dup++ = (*fup++ + *bup++) / 2;
00100       *dvp++ = (*fvp++ + *bvp++) / 2;
00101     }
00102
00103     lbyp   += src_roi[0]->line_step;
00104     lbup   += src_roi[0]->line_step / 2;
00105     lbvp   += src_roi[0]->line_step / 2;
00106     lfyp   += src_roi[1]->line_step;
00107     lfup   += src_roi[1]->line_step / 2;
00108     lfvp   += src_roi[1]->line_step / 2;
00109     ldyp  += dst_roi->line_step;
00110     ldup  += dst_roi->line_step / 2;
00111     ldvp  += dst_roi->line_step / 2;
00112     byp    = lbyp;
00113     bup    = lbup;
00114     bvp    = lbvp;
00115     fyp    = lfyp;
00116     fup    = lfup;
00117     fvp    = lfvp;
00118     dyp    = ldyp;
00119     dup    = ldup;
00120     dvp    = ldvp;
00121   }
00122
00123 }