25 #include <fvutils/scalers/lossy.h> 26 #include <fvutils/color/yuv.h> 47 orig_width = orig_height = 0;
48 scal_width = scal_height = 0;
65 if ( (factor <= 0) || (factor > 1) ) {
68 scale_factor = factor;
71 if (orig_width != 0) {
72 scal_width = (
unsigned int) ceil(orig_width * scale_factor);
73 scal_width += (scal_width % 2);
75 if (orig_height != 0) {
76 scal_height = (
unsigned int) ceil(orig_height * scale_factor);
77 scal_height += (scal_width % 2);
98 float scale_factor_width = 1.0;
99 float scale_factor_height = 1.0;
101 if (orig_width != 0) {
102 scale_factor_width = scal_width / float(orig_width);
104 if (orig_height != 0) {
105 scale_factor_height = scal_height / float(orig_height);
108 scale_factor = (scale_factor_width < scale_factor_height) ? scale_factor_width : scale_factor_height;
110 scal_width = (
unsigned int) floor(orig_width * scale_factor);
111 scal_height = (
unsigned int) floor(orig_height * scale_factor);
113 scal_width += (scal_width % 2);
114 scal_height += (scal_height % 2);
121 orig_buffer = buffer;
128 scal_buffer = buffer;
155 if ( orig_width == 0 )
return;
156 if ( orig_height == 0 )
return;
157 if ( scal_width == 0 )
return;
158 if ( scal_height == 0 )
return;
159 if ( orig_buffer == NULL )
return;
160 if ( scal_buffer == NULL )
return;
164 float skip = 1 / scale_factor;
165 unsigned char *oyp = orig_buffer;
166 unsigned char *oup = YUV422_PLANAR_U_PLANE( orig_buffer, orig_width, orig_height );
167 unsigned char *ovp = YUV422_PLANAR_V_PLANE( orig_buffer, orig_width, orig_height );
169 unsigned char *syp = scal_buffer;
170 unsigned char *sup = YUV422_PLANAR_U_PLANE( scal_buffer, scal_width, scal_height );
171 unsigned char *svp = YUV422_PLANAR_V_PLANE( scal_buffer, scal_width, scal_height );
173 memset( syp, 0, scal_width * scal_height );
174 memset( sup, 128, scal_width * scal_height );
176 float oh_float = 0.0;
177 float ow_float = 0.0;
179 unsigned int oh_pixel;
180 unsigned int ow_pixel;
181 unsigned int ow_pixel_next;
183 for (
unsigned int h = 0; h < scal_height; ++h) {
184 oh_pixel = (
unsigned int) rint(oh_float);
187 if (oh_pixel >= orig_height) {
188 oh_pixel = orig_height - 1;
190 for (
unsigned int w = 0; w < scal_width; w += 2) {
191 ow_pixel = (
unsigned int) rint(ow_float);
192 ow_pixel_next = (
unsigned int) rint( ow_float + skip);
194 if (ow_pixel >= orig_width) {
195 ow_pixel = orig_width - 1;
198 if (ow_pixel_next >= orig_width) {
199 ow_pixel_next = orig_width - 1;
202 syp[ h * scal_width + w ] = oyp[ oh_pixel * orig_width + ow_pixel ];
203 syp[ h * scal_width + w + 1 ] = oyp[ oh_pixel * orig_width + ow_pixel_next ];
204 sup[ (h * scal_width + w) / 2 ] = (oup[ (oh_pixel * orig_width + ow_pixel) / 2 ] + oup[ (oh_pixel * orig_width + ow_pixel_next) / 2 ]) / 2;
205 svp[ (h * scal_width + w) / 2 ] = (ovp[ (oh_pixel * orig_width + ow_pixel) / 2 ] + ovp[ (oh_pixel * orig_width + ow_pixel_next) / 2 ]) / 2;
207 ow_float += 2 * skip;
virtual float get_scale_factor()
Returns the scale factor.
virtual unsigned int needed_scaled_height()
Minimum needed height of scaled image depending on factor and original image height.
virtual void set_scaled_buffer(unsigned char *buffer)
Set scaled image buffer.
virtual void set_original_buffer(unsigned char *buffer)
Set original image buffer.
virtual ~LossyScaler()
Destructor.
virtual void set_original_dimensions(unsigned int width, unsigned int height)
Set original image dimensions.
virtual void set_scaled_dimensions(unsigned int width, unsigned int height)
Set dimenins of scaled image buffer.
LossyScaler()
Constructor.
virtual void set_scale_factor(float factor)
Set scale factor.
virtual unsigned int needed_scaled_width()
Minimum needed width of scaled image depending on factor and original image width.
virtual void scale()
Scale image.