| 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"cxx . . $ `<file>/libdv/dv_types.h`/ |
| 27 | |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | |
| 32 | unsigned char* read_ppm_streamcxx . . $ read_ppm_stream(fdc46fa55c83658a).(FILEcxx . . $ FILE#* f, int * width, int * height) |
| 33 | { |
| 34 | char line[200]; |
| 35 | unsigned char* res; |
| 36 | fgetscxx . . $ fgets(efb0aaba7ac866ae).(line, sizeof(line), f); |
| 37 | do { |
| 38 | fgetscxx . . $ fgets(efb0aaba7ac866ae).(line, sizeof(line), f); /* P6 */ |
| 39 | } while ((line[0] == '#') && !feofcxx . . $ feof(89bc1efcdac0ca22).(f)); |
| 40 | if (feofcxx . . $ feof(89bc1efcdac0ca22).(f)) { |
| 41 | exitcxx . . $ exit(3ca292c338df9bb3).(0); |
| 42 | } |
| 43 | if (sscanfcxx . . $ sscanf(5e8a2c12db2ea735).(line, "%d %d\n", width, height) != 2) { |
| 44 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Bad PPM file!\n"); |
| 45 | exitcxx . . $ exit(3ca292c338df9bb3).(1); |
| 46 | } |
| 47 | fgetscxx . . $ fgets(efb0aaba7ac866ae).(line, sizeof(line), f); /* 255 */ |
| 48 | |
| 49 | res = (unsigned char*) malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(*width * *height * 3); |
| 50 | |
| 51 | freadcxx . . $ fread(738fc8ac1d711e58).(res, 1, 3 * *width * *height, f); |
| 52 | return res; |
| 53 | } |
| 54 | |
| 55 | void write_ppmcxx . . $ write_ppm(252c677dc62fa21b).(FILEcxx . . $ FILE# * f, unsigned char* outbuf, int width, int height) |
| 56 | { |
| 57 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, "P6\n# CREATOR: ppmqscale V0.0.2\n%d %d\n255\n", |
| 58 | width, height); |
| 59 | fwritecxx . . $ fwrite(ab0609fa07c47d6e).(outbuf, 3, width * height, f); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | void enlarge_picturecxx . . $ enlarge_picture(6a7d0ba6f904f1f5).(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 outpixcxx . . $ outpix# { |
| 148 | unsigned long rcxx . . $ outpix#r.; |
| 149 | unsigned long gcxx . . $ outpix#g.; |
| 150 | unsigned long bcxx . . $ outpix#b.; |
| 151 | |
| 152 | unsigned long weightcxx . . $ outpix#weight.; |
| 153 | }; |
| 154 | |
| 155 | |
| 156 | |
| 157 | void shrink_picturecxx . . $ shrink_picture(6a7d0ba6f904f1f5).(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 outpixcxx . . $ outpix# * dst_line1 = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 171 | static struct outpixcxx . . $ outpix# * dst_line2 = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 172 | |
| 173 | if (!dst_line1) { |
| 174 | dst_line1 = (struct outpixcxx . . $ outpix#*) malloccxx . . $ malloc(bfcfa4d6b7f7ef64).( |
| 175 | (dst_width + 1) * sizeof(struct outpixcxx . . $ outpix#)); |
| 176 | dst_line2 = (struct outpixcxx . . $ outpix#*) malloccxx . . $ malloc(bfcfa4d6b7f7ef64).( |
| 177 | (dst_width + 1) * sizeof(struct outpixcxx . . $ 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 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(dst_line1, 0, dst_scaled_width * sizeof(struct outpixcxx . . $ outpix#)); |
| 205 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(dst_line2, 0, dst_scaled_width * sizeof(struct outpixcxx . . $ 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].rcxx . . $ outpix#r. += (line[0] * w) >> 16; |
| 224 | dst_line1[x].gcxx . . $ outpix#g. += (line[1] * w) >> 16; |
| 225 | dst_line1[x].bcxx . . $ outpix#b. += (line[2] * w) >> 16; |
| 226 | dst_line1[x].weightcxx . . $ outpix#weight. += w; |
| 227 | |
| 228 | w = (weight2y * weight1x) >> 16; |
| 229 | |
| 230 | dst_line2[x].rcxx . . $ outpix#r. += (line[0] * w) >> 16; |
| 231 | dst_line2[x].gcxx . . $ outpix#g. += (line[1] * w) >> 16; |
| 232 | dst_line2[x].bcxx . . $ outpix#b. += (line[2] * w) >> 16; |
| 233 | dst_line2[x].weightcxx . . $ outpix#weight. += w; |
| 234 | |
| 235 | w = (weight1y * weight2x) >> 16; |
| 236 | |
| 237 | dst_line1[x+1].rcxx . . $ outpix#r. += (line[0] * w) >> 16; |
| 238 | dst_line1[x+1].gcxx . . $ outpix#g. += (line[1] * w) >> 16; |
| 239 | dst_line1[x+1].bcxx . . $ outpix#b. += (line[2] * w) >> 16; |
| 240 | dst_line1[x+1].weightcxx . . $ outpix#weight. += w; |
| 241 | |
| 242 | w = (weight2y * weight2x) >> 16; |
| 243 | |
| 244 | dst_line2[x+1].rcxx . . $ outpix#r. += (line[0] * w) >> 16; |
| 245 | dst_line2[x+1].gcxx . . $ outpix#g. += (line[1] * w) >> 16; |
| 246 | dst_line2[x+1].bcxx . . $ outpix#b. += (line[2] * w) >> 16; |
| 247 | dst_line2[x+1].weightcxx . . $ outpix#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 outpixcxx . . $ 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].weightcxx . . $ outpix#weight.; |
| 264 | *dst++ = (dst_line1[x].rcxx . . $ outpix#r. * f) >> 15; |
| 265 | *dst++ = (dst_line1[x].gcxx . . $ outpix#g. * f) >> 15; |
| 266 | *dst++ = (dst_line1[x].bcxx . . $ outpix#b. * f) >> 15; |
| 267 | } |
| 268 | dst += (dst_width - dst_scaled_width) * 3; |
| 269 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(dst_line1, 0, dst_scaled_width * |
| 270 | sizeof(struct outpixcxx . . $ 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].weightcxx . . $ outpix#weight.; |
| 280 | *dst++ = (dst_line1[x].rcxx . . $ outpix#r. * f) >> 15; |
| 281 | *dst++ = (dst_line1[x].gcxx . . $ outpix#g. * f) >> 15; |
| 282 | *dst++ = (dst_line1[x].bcxx . . $ outpix#b. * f) >> 15; |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | |
| 288 | void fast_scalecxx . . $ fast_scale(6a7d0ba6f904f1f5).(unsigned char* in, unsigned char* out, int in_width, |
| 289 | int in_height, int dst_width, int dst_height) |
| 290 | { |
| 291 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(out, 0, dst_height * dst_width * 3); |
| 292 | |
| 293 | if (dst_width > in_width && dst_height > in_height) { |
| 294 | enlarge_picturecxx . . $ enlarge_picture(6a7d0ba6f904f1f5).(in, out, in_width, in_height, |
| 295 | dst_width, dst_height); |
| 296 | } else if (dst_width == in_width && dst_height == in_height) { |
| 297 | memcpycxx . . $ memcpy(cc817cb175f282d).(out, in, dst_height * dst_width * 3); |
| 298 | } else { |
| 299 | shrink_picturecxx . . $ shrink_picture(6a7d0ba6f904f1f5).(in, out, in_width, in_height, |
| 300 | dst_width, dst_height); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | int maincxx . . $ main(d8e43b79a186046c).(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 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Usage: ppmqscale out_width out_height\n"); |
| 313 | exitcxx . . $ exit(3ca292c338df9bb3).(-1); |
| 314 | } |
| 315 | |
| 316 | out_width = atoicxx . . $ atoi(5c4306d8553b3696).(argv[1]); |
| 317 | out_height = atoicxx . . $ atoi(5c4306d8553b3696).(argv[2]); |
| 318 | |
| 319 | out_pic = (unsigned char*) malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(out_width * out_height * 3); |
| 320 | |
| 321 | for (;;) { |
| 322 | in_pic = read_ppm_streamcxx . . $ read_ppm_stream(fdc46fa55c83658a).(stdincxx . . $ `<external>/232824719582829767/stdio.h:157:9`!, &in_width, &in_height); |
| 323 | |
| 324 | fast_scalecxx . . $ fast_scale(6a7d0ba6f904f1f5).(in_pic, out_pic, in_width, in_height, |
| 325 | out_width, out_height); |
| 326 | write_ppmcxx . . $ write_ppm(252c677dc62fa21b).(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:158:9`!, out_pic, out_width, out_height); |
| 327 | freecxx . . $ free(5d22bdacc48458e8).(in_pic); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 |