Debian Codegraph @

encodedv/ppmqscale.c

raw symbols SCIP
1 /*
2 * ppmqscale.c
3 *
4 * Copyright (C) Peter Schlaile - February 2001
5 *
6 * This file is part of libdv, a free DV (IEC 61834/SMPTE 314M)
7 * codec.
8 *
9 * libdv is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser Public License as published by
11 * the Free Software Foundation; either version 2.1, or (at your
12 * option) any later version.
13 *
14 * libdv is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser Public License
20 * along with libdv; see the file COPYING. If not, write to
21 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 * The libdv homepage is http://libdv.sourceforge.net/.
24 */
25
26 #include "libdv/dv_types.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 unsigned char* read_ppm_stream(FILE* f, int * width, int * height)
33 {
34 char line[200];
35 unsigned char* res;
36 fgets(line, sizeof(line), f);
37 do {
38 fgets(line, sizeof(line), f); /* P6 */
39 } while ((line[0] == '#') && !feof(f));
40 if (feof(f)) {
41 exit(0);
42 }
43 if (sscanf(line, "%d %d\n", width, height) != 2) {
44 fprintf(stderr, "Bad PPM file!\n");
45 exit(1);
46 }
47 fgets(line, sizeof(line), f); /* 255 */
48
49 res = (unsigned char*) malloc(*width * *height * 3);
50
51 fread(res, 1, 3 * *width * *height, f);
52 return res;
53 }
54
55 void write_ppm(FILE * f, unsigned char* outbuf, int width, int height)
56 {
57 fprintf(f, "P6\n# CREATOR: ppmqscale V0.0.2\n%d %d\n255\n",
58 width, height);
59 fwrite(outbuf, 3, width * height, f);
60 }
61
62
63 void enlarge_picture(unsigned char* src, unsigned char* dst, int src_width,
64 int src_height, int dst_width, int dst_height)
65 {
66 double ratiox = (double) (dst_width) / (double) (src_width);
67 double ratioy = (double) (dst_height) / (double) (src_height);
68 double ratio;
69 unsigned long x_src, dx_src, x_dst;
70 unsigned long y_src, dy_src, y_dst;
71 unsigned long dst_scaled_width;
72 unsigned long dst_scaled_height;
73
74 if (ratiox > ratioy) {
75 ratio = ratioy;
76 dst += ((int) ((dst_width - ratio * src_width) / 2)) * 3;
77 #if 0
78 fprintf(stderr, "Choosing Y ratio: %g, %d\n", ratio,
79 (int) (((dst_width - ratio * src_width) / 2) * 3));
80 #endif
81 } else {
82 ratio = ratiox;
83 dst += ((int) ((dst_height - ratio * src_height)
84 * dst_width / 2)) * 3;
85 #if 0
86 fprintf(stderr, "Choosing X ratio: %g, %d\n", ratio,
87 ((int) ((dst_height - ratio * src_height)
88 * dst_width / 2)) * 3);
89 #endif
90 }
91
92 dx_src = 65536.0 / ratio;
93 dy_src = 65536.0 / ratio;
94 dst_scaled_width = ratio * src_width;
95 dst_scaled_height = ratio * src_height;
96 y_src = 0;
97 for (y_dst = 0; y_dst < dst_scaled_height; y_dst++) {
98 unsigned char* line1 = src + (y_src >> 16) * 3 * src_width;
99 unsigned char* line2 = line1 + 3 * src_width;
100 unsigned long weight1y = 65536 - (y_src & 0xffff);
101 unsigned long weight2y = 65536 - weight1y;
102
103 x_src = 0;
104 for (x_dst = 0; x_dst < dst_scaled_width; x_dst++) {
105 unsigned long weight1x = 65536 - (x_src & 0xffff);
106 unsigned long weight2x = 65536 - weight1x;
107
108 unsigned long x = (x_src >> 16) * 3;
109
110 #if 0
111 *dst++ = line1[x];
112 *dst++ = line1[x + 1];
113 *dst++ = line1[x + 2];
114 #else
115 *dst++ = ((((line1[x] * weight1y) >> 16)
116 * weight1x) >> 16)
117 + ((((line2[x] * weight2y) >> 16)
118 * weight1x) >> 16)
119 + ((((line1[3 + x] * weight1y) >> 16)
120 * weight2x) >> 16)
121 + ((((line2[3 + x] * weight2y) >> 16)
122 * weight2x) >> 16);
123 *dst++ = ((((line1[x + 1] * weight1y) >> 16)
124 * weight1x) >> 16)
125 + ((((line2[x + 1] * weight2y) >> 16)
126 * weight1x) >> 16)
127 + ((((line1[3 + x + 1] * weight1y) >> 16)
128 * weight2x) >> 16)
129 + ((((line2[3 + x + 1] * weight2y) >> 16)
130 * weight2x) >> 16);
131 *dst++ = ((((line1[x + 2] * weight1y) >> 16)
132 * weight1x) >> 16)
133 + ((((line2[x + 2] * weight2y) >> 16)
134 * weight1x) >> 16)
135 + ((((line1[3 + x + 2] * weight1y) >> 16)
136 * weight2x) >> 16)
137 + ((((line2[3 + x + 2] * weight2y) >> 16)
138 * weight2x) >> 16);
139 #endif
140 x_src += dx_src;
141 }
142 y_src += dy_src;
143 dst += (dst_width - dst_scaled_width) * 3;
144 }
145 }
146
147 struct outpix {
148 unsigned long r;
149 unsigned long g;
150 unsigned long b;
151
152 unsigned long weight;
153 };
154
155
156
157 void shrink_picture(unsigned char* src, unsigned char* dst, int src_width,
158 int src_height, int dst_width, int dst_height)
159 {
160 double ratiox = (double) (dst_width) / (double) (src_width);
161 double ratioy = (double) (dst_height) / (double) (src_height);
162 double ratio;
163 unsigned long x_src, dx_dst, x_dst;
164 unsigned long y_src, dy_dst, y_dst;
165 long y_counter;
166
167 unsigned long dst_scaled_width;
168 unsigned long dst_scaled_height;
169
170 static struct outpix * dst_line1 = NULL;
171 static struct outpix * dst_line2 = NULL;
172
173 if (!dst_line1) {
174 dst_line1 = (struct outpix*) malloc(
175 (dst_width + 1) * sizeof(struct outpix));
176 dst_line2 = (struct outpix*) malloc(
177 (dst_width + 1) * sizeof(struct outpix));
178 }
179
180 if (ratiox > ratioy) {
181 ratio = ratioy;
182 dst += ((int) ((dst_width - ratio * src_width) / 2)) * 3;
183 #if 0
184 fprintf(stderr, "Choosing Y ratio: %g, %d\n", ratio,
185 ((int) ((dst_width - ratio * src_width) / 2)) * 3);
186 #endif
187 } else {
188 ratio = ratiox;
189 dst += ((int) ((dst_height - ratio * src_height)
190 * dst_width / 2)) * 3;
191
192 #if 0
193 fprintf(stderr, "Choosing X ratio: %g, %d\n", ratio,
194 ((int) ((dst_height - ratio * src_height)
195 * dst_width / 2)) * 3);
196 #endif
197 }
198
199 dx_dst = 65536.0 * ratio;
200 dy_dst = 65536.0 * ratio;
201 dst_scaled_width = ratio * src_width;
202 dst_scaled_height = ratio * src_height;
203
204 memset(dst_line1, 0, dst_scaled_width * sizeof(struct outpix));
205 memset(dst_line2, 0, dst_scaled_width * sizeof(struct outpix));
206 y_dst = 0;
207 y_counter = 65536;
208 for (y_src = 0; y_src < src_height; y_src++) {
209 unsigned char* line = src + y_src * 3 * src_width;
210 unsigned long weight1y = 65536 - (y_dst & 0xffff);
211 unsigned long weight2y = 65536 - weight1y;
212 x_dst = 0;
213 for (x_src = 0; x_src < src_width; x_src++) {
214 unsigned long weight1x = 65536 - (x_dst & 0xffff);
215 unsigned long weight2x = 65536 - weight1x;
216
217 unsigned long x = x_dst >> 16;
218
219 unsigned long w;
220
221 w = (weight1y * weight1x) >> 16;
222
223 dst_line1[x].r += (line[0] * w) >> 16;
224 dst_line1[x].g += (line[1] * w) >> 16;
225 dst_line1[x].b += (line[2] * w) >> 16;
226 dst_line1[x].weight += w;
227
228 w = (weight2y * weight1x) >> 16;
229
230 dst_line2[x].r += (line[0] * w) >> 16;
231 dst_line2[x].g += (line[1] * w) >> 16;
232 dst_line2[x].b += (line[2] * w) >> 16;
233 dst_line2[x].weight += w;
234
235 w = (weight1y * weight2x) >> 16;
236
237 dst_line1[x+1].r += (line[0] * w) >> 16;
238 dst_line1[x+1].g += (line[1] * w) >> 16;
239 dst_line1[x+1].b += (line[2] * w) >> 16;
240 dst_line1[x+1].weight += w;
241
242 w = (weight2y * weight2x) >> 16;
243
244 dst_line2[x+1].r += (line[0] * w) >> 16;
245 dst_line2[x+1].g += (line[1] * w) >> 16;
246 dst_line2[x+1].b += (line[2] * w) >> 16;
247 dst_line2[x+1].weight += w;
248
249 x_dst += dx_dst;
250 line += 3;
251 }
252
253 y_dst += dy_dst;
254 y_counter -= dy_dst;
255 if (y_counter < 0) {
256 unsigned long x;
257 struct outpix * temp;
258
259 y_counter += 65536;
260
261 for (x=0; x < dst_scaled_width; x++) {
262 unsigned long f = 0x80000000UL
263 / dst_line1[x].weight;
264 *dst++ = (dst_line1[x].r * f) >> 15;
265 *dst++ = (dst_line1[x].g * f) >> 15;
266 *dst++ = (dst_line1[x].b * f) >> 15;
267 }
268 dst += (dst_width - dst_scaled_width) * 3;
269 memset(dst_line1, 0, dst_scaled_width *
270 sizeof(struct outpix));
271 temp = dst_line1;
272 dst_line1 = dst_line2;
273 dst_line2 = temp;
274 }
275 }
276 {
277 unsigned long x;
278 for (x = 0; x < dst_scaled_width; x++) {
279 unsigned long f = 0x80000000UL / dst_line1[x].weight;
280 *dst++ = (dst_line1[x].r * f) >> 15;
281 *dst++ = (dst_line1[x].g * f) >> 15;
282 *dst++ = (dst_line1[x].b * f) >> 15;
283 }
284 }
285 }
286
287
288 void fast_scale(unsigned char* in, unsigned char* out, int in_width,
289 int in_height, int dst_width, int dst_height)
290 {
291 memset(out, 0, dst_height * dst_width * 3);
292
293 if (dst_width > in_width && dst_height > in_height) {
294 enlarge_picture(in, out, in_width, in_height,
295 dst_width, dst_height);
296 } else if (dst_width == in_width && dst_height == in_height) {
297 memcpy(out, in, dst_height * dst_width * 3);
298 } else {
299 shrink_picture(in, out, in_width, in_height,
300 dst_width, dst_height);
301 }
302 }
303
304 int main(int argc, const char** argv)
305 {
306 int out_width, out_height;
307 int in_width, in_height;
308 unsigned char* in_pic;
309 unsigned char* out_pic;
310
311 if (argc < 3) {
312 fprintf(stderr, "Usage: ppmqscale out_width out_height\n");
313 exit(-1);
314 }
315
316 out_width = atoi(argv[1]);
317 out_height = atoi(argv[2]);
318
319 out_pic = (unsigned char*) malloc(out_width * out_height * 3);
320
321 for (;;) {
322 in_pic = read_ppm_stream(stdin, &in_width, &in_height);
323
324 fast_scale(in_pic, out_pic, in_width, in_height,
325 out_width, out_height);
326 write_ppm(stdout, out_pic, out_width, out_height);
327 free(in_pic);
328 }
329 }
330
331