forked from schani/metapixel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric.c
140 lines (119 loc) · 3.61 KB
/
metric.c
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
134
135
136
137
138
139
140
/*
* metric.c
*
* metapixel
*
* Copyright (C) 1997-2009 Mark Probst
*
* 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; either version 2
* of the License, or (at your option) any later version.
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include "api.h"
metric_t*
metric_init (metric_t *metric, int kind, int color_space, float weights[])
{
assert(kind == METRIC_SUBPIXEL);
metric->kind = kind;
metric->color_space = color_space;
memcpy(metric->weights, weights, sizeof(float) * NUM_CHANNELS);
return metric;
}
void
metric_generate_coeffs_for_subimage (coeffs_union_t *coeffs, bitmap_t *bitmap,
int x, int y, int width, int height, metric_t *metric)
{
if (metric->kind == METRIC_SUBPIXEL)
{
bitmap_t *sub_bitmap, *scaled_bitmap;
sub_bitmap = bitmap_sub(bitmap, x, y, width, height);
assert(sub_bitmap != 0);
//bitmap_write(sub_bitmap, "/tmp/sub.png");
scaled_bitmap = bitmap_scale(sub_bitmap, NUM_SUBPIXEL_ROWS_COLS, NUM_SUBPIXEL_ROWS_COLS, FILTER_MITCHELL);
assert(scaled_bitmap != 0);
bitmap_free(sub_bitmap);
//bitmap_write(scaled_bitmap, "/tmp/scaled.png");
assert(scaled_bitmap->color == COLOR_RGB_8);
assert(scaled_bitmap->pixel_stride == NUM_CHANNELS);
assert(scaled_bitmap->row_stride == NUM_SUBPIXEL_ROWS_COLS * NUM_CHANNELS);
color_convert_rgb_pixels(coeffs->subpixel.subpixels, scaled_bitmap->data,
NUM_SUBPIXELS, metric->color_space);
bitmap_free(scaled_bitmap);
}
else
assert(0);
}
static unsigned char*
subpixels_for_color_space (metapixel_t *pixel, int color_space)
{
switch (color_space)
{
case COLOR_SPACE_RGB :
return pixel->subpixels_rgb;
case COLOR_SPACE_HSV :
return pixel->subpixels_hsv;
case COLOR_SPACE_YIQ :
return pixel->subpixels_yiq;
default :
assert(0);
}
}
#define COMPARE_FUNC_NAME subpixel_compare_no_flip
#define FLIP_X(x) ((x))
#define FLIP_Y(y) ((y))
#include "subpixel_compare.h"
#undef COMPARE_FUNC_NAME
#undef FLIP_X
#undef FLIP_Y
#define COMPARE_FUNC_NAME subpixel_compare_hor_flip
#define FLIP_X(x) (NUM_SUBPIXEL_ROWS_COLS - 1 - (x))
#define FLIP_Y(y) ((y))
#include "subpixel_compare.h"
#undef COMPARE_FUNC_NAME
#undef FLIP_X
#undef FLIP_Y
#define COMPARE_FUNC_NAME subpixel_compare_ver_flip
#define FLIP_X(x) ((x))
#define FLIP_Y(y) (NUM_SUBPIXEL_ROWS_COLS - 1 - (y))
#include "subpixel_compare.h"
#undef COMPARE_FUNC_NAME
#undef FLIP_X
#undef FLIP_Y
#define COMPARE_FUNC_NAME subpixel_compare_hor_ver_flip
#define FLIP_X(x) (NUM_SUBPIXEL_ROWS_COLS - 1 - (x))
#define FLIP_Y(y) (NUM_SUBPIXEL_ROWS_COLS - 1 - (y))
#include "subpixel_compare.h"
#undef COMPARE_FUNC_NAME
#undef FLIP_X
#undef FLIP_Y
compare_func_set_t*
metric_compare_func_set_for_metric (metric_t *metric)
{
if (metric->kind == METRIC_SUBPIXEL)
{
static compare_func_set_t set =
{
subpixel_compare_no_flip,
subpixel_compare_hor_flip,
subpixel_compare_ver_flip,
subpixel_compare_hor_ver_flip
};
return &set;
}
else
assert(0);
return 0;
}