forked from schani/metapixel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metapixel.c
211 lines (163 loc) · 5.36 KB
/
metapixel.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* metapixel.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 <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "api.h"
void
metapixel_complete_subpixel (metapixel_t *pixel)
{
color_convert_rgb_pixels(pixel->subpixels_hsv, pixel->subpixels_rgb, NUM_SUBPIXELS, COLOR_SPACE_HSV);
color_convert_rgb_pixels(pixel->subpixels_yiq, pixel->subpixels_rgb, NUM_SUBPIXELS, COLOR_SPACE_YIQ);
}
static void
generate_coefficients (metapixel_t *pixel)
{
bitmap_t *scaled_bitmap;
/* generate subpixel coefficients */
if (pixel->width != NUM_SUBPIXEL_ROWS_COLS || pixel->height != NUM_SUBPIXEL_ROWS_COLS)
{
scaled_bitmap = bitmap_scale(pixel->bitmap, NUM_SUBPIXEL_ROWS_COLS, NUM_SUBPIXEL_ROWS_COLS, FILTER_MITCHELL);
assert(scaled_bitmap != 0);
}
else
{
scaled_bitmap = bitmap_copy(pixel->bitmap);
assert(scaled_bitmap != 0);
}
//bitmap_write(scaled_bitmap, "/tmp/debug.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);
memcpy(pixel->subpixels_rgb, scaled_bitmap->data, NUM_SUBPIXELS * NUM_CHANNELS);
metapixel_complete_subpixel(pixel);
bitmap_free(scaled_bitmap);
}
metapixel_t*
metapixel_new (const char *name, unsigned int scaled_width, unsigned int scaled_height,
float aspect_ratio)
{
metapixel_t *metapixel = (metapixel_t*)malloc(sizeof(metapixel_t));
assert(metapixel != 0);
memset(metapixel, 0, sizeof(metapixel_t));
metapixel->name = strdup(name);
assert(metapixel->name != 0);
metapixel->width = scaled_width;
metapixel->height = scaled_height;
metapixel->aspect_ratio = aspect_ratio;
metapixel->enabled = 1;
metapixel->anti_x = metapixel->anti_y = -1;
metapixel->flip = 0;
return metapixel;
}
metapixel_t*
metapixel_new_from_bitmap (bitmap_t *bitmap, const char *name,
unsigned int scaled_width, unsigned int scaled_height)
{
metapixel_t *metapixel = metapixel_new(name, scaled_width, scaled_height,
(float)bitmap->width / (float)bitmap->height);
assert(metapixel != 0);
metapixel->bitmap = bitmap_scale(bitmap, scaled_width, scaled_height, FILTER_MITCHELL);
assert(metapixel->bitmap != 0);
generate_coefficients(metapixel);
return metapixel;
}
void
metapixel_free (metapixel_t *metapixel)
{
free(metapixel->name);
if (metapixel->filename != 0)
free(metapixel->filename);
if (metapixel->bitmap != 0)
bitmap_free(metapixel->bitmap);
}
bitmap_t*
metapixel_get_bitmap (metapixel_t *metapixel)
{
if (metapixel->bitmap != 0)
return bitmap_copy(metapixel->bitmap);
else
{
char *filename;
bitmap_t *bitmap;
assert(metapixel->library != 0 && metapixel->filename != 0);
filename = (char*)malloc(strlen(metapixel->library->path) + 1 + strlen(metapixel->filename) + 1);
assert(filename != 0);
strcpy(filename, metapixel->library->path);
strcat(filename, "/");
strcat(filename, metapixel->filename);
bitmap = bitmap_read(filename);
if (bitmap == 0)
{
error_info_t info = error_make_string_info(filename);
free(filename);
error_report(ERROR_CANNOT_READ_METAPIXEL_IMAGE, info);
return 0;
}
free(filename);
return bitmap;
}
}
int
metapixel_paste (metapixel_t *pixel, bitmap_t *image, unsigned int x, unsigned int y,
unsigned int small_width, unsigned int small_height, unsigned int orientation)
{
bitmap_t *bitmap, *flipped;
bitmap = metapixel_get_bitmap(pixel);
if (bitmap == 0)
return 0;
if (bitmap->width != small_width || bitmap->height != small_height)
{
bitmap_t *scaled_bitmap = bitmap_scale(bitmap, small_width, small_height, FILTER_MITCHELL);
assert(scaled_bitmap != 0);
bitmap_free(bitmap);
bitmap = scaled_bitmap;
}
flipped = bitmap_flip(bitmap, orientation);
bitmap_free(bitmap);
bitmap_paste(image, flipped, x, y);
bitmap_free(flipped);
return 1;
}
void
metapixel_set_enabled (metapixel_t *metapixel, int enabled)
{
/* FIXME: implement */
}
metapixel_t*
metapixel_find_in_libraries (int num_libraries, library_t **libraries,
const char *library_path, const char *filename,
int *num_new_libraries, library_t ***new_libraries)
{
library_t *library = library_find_or_open(num_libraries, libraries,
library_path,
num_new_libraries, new_libraries);
metapixel_t *pixel;
if (library == 0)
return 0;
for (pixel = library->metapixels; pixel != 0; pixel = pixel->next)
if (strcmp(pixel->filename, filename) == 0)
return pixel;
error_report(ERROR_METAPIXEL_NOT_FOUND, error_make_string_info(filename));
return 0;
}