File: scale.c

package info (click to toggle)
dv4l 1.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 576 kB
  • sloc: ansic: 7,541; makefile: 382; sh: 260
file content (133 lines) | stat: -rw-r--r-- 2,588 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * Copyright (C) 2007 Free Software Foundation, Inc.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; version 2.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Author: Wolfgang Beck <bewo at users.berlios.de> 2007
 */


#include <stdlib.h>
#include <string.h>
#include "scale.h"

static void write_px_rgb(const pixel_t * const v, pixel_t * const p)
{
    // memcpy(p, v, 3);
    p[0] = v[2];
    p[1] = v[1];
    p[2] = v[0];
}

static void write_px_bgr(const pixel_t * const v, pixel_t * const p)
{
    // memcpy(p, v, 3);
    p[0] = v[0];
    p[1] = v[1];
    p[2] = v[2];
}

static inline void average(const pixel_t *x, const pixel_t *y, pixel_t *p)
{
    p[0] = (x[0] + y[0]) / 2;
    p[1] = (x[1] + y[1]) / 2;
    p[2] = (x[2] + y[2]) / 2;
}

static void (*write_px)(const pixel_t * const v, pixel_t * const p)
    = write_px_rgb;

void set_color_correction(int rgb)
{
    if(rgb) {
	write_px = write_px_bgr;
    } else {
	write_px = write_px_rgb;
    }
}

/*
 * Bresenham image scaling
 * http://www.ddj.com/showArticle.jhtml?articleID=184405045
 */
void scale_line(
	const pixel_t *src,
	pixel_t *dst, 
	int src_w,
	int dst_w
    )
{
    int num;
    int ip;
    int fp;
    int e;

    num = dst_w;
    ip = src_w / dst_w;
    fp = src_w % dst_w;
    e = 0;
    while(num > 0) {
	write_px(src, dst);
	dst += 3;
	src += 3 * ip;
	e += fp;
	if(e >= dst_w) {
	    e -= dst_w;
	    src += 3;
	}
	--num;
    }
}

void scale(
	const pixel_t *src,
	pixel_t *dst,
	int src_w,
	int src_h,
	int dst_w,
	int dst_h
    )
{
    int num;
    int ip;
    int fp;
    int e;
    const pixel_t *prev_src;

    num = dst_h;
    ip = (src_h / dst_h) * src_w;
    fp = src_h % dst_h;
    e = 0;
    prev_src = NULL;

    while(num > 0) {
	if(prev_src == src) {
	    /*
	     * re-use last line
	     */
	    memcpy(dst, dst - dst_w * 3, dst_w * 3);
	} else {
	    scale_line(src, dst, src_w, dst_w);
	    prev_src = src;
	}
	dst += dst_w * 3;
	src += ip * 3;
	e += fp;
	if(e >= dst_h) {
	    e -= dst_h;
	    src += src_w * 3;
	}
	--num;
    }
}