Fawkes API  Fawkes Development Version
invert.cpp
1 
2 /***************************************************************************
3  * invert.cpp - implementation of invert filter
4  *
5  * Created: Mon Jun 05 12:47:18 2006
6  * Copyright 2005-2007 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <core/exceptions/software.h>
25 #include <fvfilters/invert.h>
26 #include <fvutils/color/yuv.h>
27 
28 #include <cstddef>
29 
30 namespace firevision {
31 
32 /** @class FilterInvert <fvfilters/invert.h>
33  * Inversion filter.
34  * This will invert the given image.
35  * @author Tim Niemueller
36  */
37 
38 /** Constructor. */
39 FilterInvert::FilterInvert() : Filter("FilterInvert")
40 {
41 }
42 
43 void
45 {
46  if (src[0] == NULL)
47  throw fawkes::NullPointerException("FilterInvert: src buffer is NULL");
48  if (src_roi[0] == NULL)
49  throw fawkes::NullPointerException("FilterInvert: src ROI is NULL");
50 
51  if ((dst == NULL) || (dst == src[0])) {
52  // In-place
53 
54  unsigned int h = 0;
55  unsigned int w = 0;
56 
57  // y-plane
58  unsigned char *yp = src[0] + (src_roi[0]->start.y * src_roi[0]->line_step)
59  + (src_roi[0]->start.x * src_roi[0]->pixel_step);
60 
61  // line starts
62  unsigned char *lyp = yp; // y-plane
63 
64  for (h = 0; h < src_roi[0]->height; ++h) {
65  for (w = 0; w < src_roi[0]->width; ++w) {
66  *yp = 255 - *yp;
67  ++yp;
68  }
69  lyp += src_roi[0]->line_step;
70  yp = lyp;
71  }
72 
73  } else {
74  unsigned int h = 0;
75  unsigned int w = 0;
76 
77  // y-plane
78  unsigned char *yp = src[0] + (src_roi[0]->start.y * src_roi[0]->line_step)
79  + (src_roi[0]->start.x * src_roi[0]->pixel_step);
80  // u-plane
81  unsigned char *up =
82  YUV422_PLANAR_U_PLANE(src[0], src_roi[0]->image_width, src_roi[0]->image_height)
83  + ((src_roi[0]->start.y * src_roi[0]->line_step) / 2
84  + (src_roi[0]->start.x * src_roi[0]->pixel_step) / 2);
85  // v-plane
86  unsigned char *vp =
87  YUV422_PLANAR_V_PLANE(src[0], src_roi[0]->image_width, src_roi[0]->image_height)
88  + ((src_roi[0]->start.y * src_roi[0]->line_step) / 2
89  + (src_roi[0]->start.x * src_roi[0]->pixel_step) / 2);
90 
91  // destination y-plane
92  unsigned char *dyp =
94  // destination u-plane
95  unsigned char *dup = YUV422_PLANAR_U_PLANE(dst, dst_roi->image_width, dst_roi->image_height)
96  + ((dst_roi->start.y * dst_roi->line_step) / 2
97  + (dst_roi->start.x * dst_roi->pixel_step) / 2);
98  // destination v-plane
99  unsigned char *dvp = YUV422_PLANAR_V_PLANE(dst, dst_roi->image_width, dst_roi->image_height)
100  + ((dst_roi->start.y * dst_roi->line_step) / 2
101  + (dst_roi->start.x * dst_roi->pixel_step) / 2);
102 
103  // line starts
104  unsigned char *lyp = yp; // y-plane
105  unsigned char *lup = up; // u-plane
106  unsigned char *lvp = vp; // v-plane
107  unsigned char *ldyp = dyp; // destination y-plane
108  unsigned char *ldup = dup; // destination u-plane
109  unsigned char *ldvp = dvp; // destination v-plane
110 
111  for (h = 0; (h < src_roi[0]->height) && (h < dst_roi->height); ++h) {
112  for (w = 0; (w < src_roi[0]->width) && (w < dst_roi->width); w += 2) {
113  *dyp++ = 255 - *yp++;
114  *dyp++ = 255 - *yp++;
115  *dup++ = *up++;
116  *dvp++ = *vp++;
117  }
118 
119  lyp += src_roi[0]->line_step;
120  lup += src_roi[0]->line_step / 2;
121  lvp += src_roi[0]->line_step / 2;
122  ldyp += dst_roi->line_step;
123  ldup += dst_roi->line_step / 2;
124  ldvp += dst_roi->line_step / 2;
125  yp = lyp;
126  up = lup;
127  vp = lvp;
128  dyp = ldyp;
129  dup = ldup;
130  dvp = ldvp;
131  }
132  }
133 }
134 
135 } // end namespace firevision
firevision::ROI::width
unsigned int width
ROI width.
Definition: roi.h:121
firevision::Filter::src
unsigned char ** src
Source buffers, dynamically allocated by Filter ctor.
Definition: filter.h:70
firevision::ROI::image_width
unsigned int image_width
width of image that contains this ROI
Definition: roi.h:125
firevision::ROI::image_height
unsigned int image_height
height of image that contains this ROI
Definition: roi.h:127
firevision::ROI::height
unsigned int height
ROI height.
Definition: roi.h:123
firevision::Filter::src_roi
ROI ** src_roi
Source ROIs, dynamically allocated by Filter ctor.
Definition: filter.h:75
fawkes::upoint_t::y
unsigned int y
y coordinate
Definition: types.h:36
firevision::ROI::pixel_step
unsigned int pixel_step
pixel step
Definition: roi.h:131
firevision::Filter::dst_roi
ROI * dst_roi
Destination ROI.
Definition: filter.h:77
firevision::FilterInvert::FilterInvert
FilterInvert()
Constructor.
Definition: invert.cpp:43
firevision::ROI::start
fawkes::upoint_t start
ROI start.
Definition: roi.h:119
fawkes::NullPointerException
Definition: software.h:35
firevision::ROI::line_step
unsigned int line_step
line step
Definition: roi.h:129
fawkes::upoint_t::x
unsigned int x
x coordinate
Definition: types.h:35
firevision::Filter::dst
unsigned char * dst
Destination buffer.
Definition: filter.h:72
firevision::FilterInvert::apply
virtual void apply()
Definition: invert.cpp:48