| 1 |
|
| /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
|
| ppm-lite.c --- simple, self-contained code for manipulating |
| 3 |
|
| PBM, PGM, and PPM data. |
| 4 |
|
| |
| 5 |
|
| Copyright © 2001 Jamie Zawinski <jwz@jwz.org> |
| 6 |
|
| |
| 7 |
|
| Permission to use, copy, modify, distribute, and sell this software and its |
| 8 |
|
| documentation for any purpose is hereby granted without fee, provided that |
| 9 |
|
| the above copyright notice appear in all copies and that both that |
| 10 |
|
| copyright notice and this permission notice appear in supporting |
| 11 |
|
| documentation. No representations are made about the suitability of this |
| 12 |
|
| software for any purpose. It is provided "as is" without express or |
| 13 |
|
| implied warranty. |
| 14 |
|
| */ |
| 15 |
|
| |
| 16 |
|
| #include "config.h" |
| 17 |
|
| #include "ppm-lite.h" |
| 18 |
|
| |
| 19 |
|
| #include "config.h" |
| 20 |
|
| #include "ppm-lite.h" |
| 21 |
|
| #include "font-bdf.h" |
| 22 |
|
| |
| 23 |
|
| #include <stdlib.h> |
| 24 |
|
| #include <stdio.h> |
| 25 |
|
| #include <string.h> |
| 26 |
|
| |
| 27 |
|
| #undef countof |
| 28 |
|
| #define countof(x) (sizeof((x))/sizeof(*(x))) |
| 29 |
|
| |
| 30 |
|
| |
| 31 |
|
| /* Reads a non-blank line, stripping comments and blank lines. |
| 32 |
|
| At EOF, either exits, or returns 0, depending on exit_p. |
| 33 |
|
| */ |
| 34 |
|
| static char * |
| 35 |
0 |
| read_ppm_line (FILE *in, char *buf, size_t size, |
| 36 |
|
| const char *file, int *lineP, int exit_p) |
| 37 |
|
| { |
| 38 |
|
| char *s; |
| 39 |
|
| |
| 40 |
0 |
▶ | if (file[0] == '-' && file[1] == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (file[0] == '-' && file[1] == 0) |
0 |
0 |
0 |
| if (file[0] == '-' && file[1] == 0) |
0 |
0 |
0 |
|
| 41 |
0 |
| file = "<stdin>"; |
| 42 |
|
| |
| 43 |
|
| while (1) |
| 44 |
|
| { |
| 45 |
0 |
▶ | if (!fgets (buf, size-1, in)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!fgets (buf, size-1, in)) |
0 |
0 |
0 |
|
| 46 |
|
| { |
| 47 |
0 |
▶ | if (!exit_p) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!exit_p) return 0; |
0 |
0 |
0 |
|
| 48 |
0 |
| fprintf (stderr, "%s: %s: %d: premature EOF\n", |
| 49 |
|
| "progname", file, *lineP); |
| 50 |
0 |
| exit (1); |
| 51 |
|
| } |
| 52 |
|
| |
| 53 |
0 |
| (*lineP)++; |
| 54 |
|
| |
| 55 |
0 |
| s = strchr(buf, '#'); /* delete comments to end of line */ |
| 56 |
0 |
▶ | if (s) |
Branch: 1 branch
| Branch | True | False | Total |
| if (s) |
0 |
0 |
0 |
|
| 57 |
|
| { |
| 58 |
0 |
| s[0] = '\n'; |
| 59 |
0 |
| s[1] = 0; |
| 60 |
|
| } |
| 61 |
|
| |
| 62 |
0 |
| s = buf; |
| 63 |
0 |
▶ | while (*s == ' ' || *s == '\t' || /* skip white */ |
Branch: 1 branch
| Branch | True | False | Total |
| while (*s == ' ' || *s == '\t' || /* skip white */ |
0 |
0 |
0 |
|
| 64 |
0 |
▶ | *s == '\n' || *s == '\r') |
Branch: 3 branches
| Branch | True | False | Total |
| *s == '\n' || *s == '\r') |
0 |
0 |
0 |
| *s == '\n' || *s == '\r') |
0 |
0 |
0 |
| *s == '\n' || *s == '\r') |
0 |
0 |
0 |
|
| 65 |
0 |
| s++; |
| 66 |
|
| |
| 67 |
0 |
▶ | if (*s != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s != 0) |
0 |
0 |
0 |
|
| 68 |
0 |
| break; /* non-blank line: return it. Else read another. */ |
| 69 |
|
| } |
| 70 |
0 |
| return buf; |
| 71 |
|
| } |
| 72 |
|
| |
| 73 |
|
| |
| 74 |
|
| /* Reads a PBM, PGM, or PPM file, binary or ASCII, and returns a `struct ppm' |
| 75 |
|
| that contains RGBA data. File name of "-" means stdin. |
| 76 |
|
| */ |
| 77 |
|
| struct ppm * |
| 78 |
0 |
| read_ppm (const char *file) |
| 79 |
|
| { |
| 80 |
0 |
| int stdin_p = !strcmp(file, "-"); |
| 81 |
|
| FILE *in; |
| 82 |
|
| char buf [10240]; |
| 83 |
|
| int max; |
| 84 |
|
| char dummy; |
| 85 |
0 |
| struct ppm *ppm = (struct ppm *) calloc (1, sizeof(*ppm)); |
| 86 |
0 |
| int line = 1; |
| 87 |
|
| |
| 88 |
0 |
▶ | if (stdin_p) |
Branch: 1 branch
| Branch | True | False | Total |
| if (stdin_p) |
0 |
0 |
0 |
|
| 89 |
0 |
| in = stdin; |
| 90 |
|
| else |
| 91 |
|
| { |
| 92 |
0 |
| in = fopen (file, "r"); |
| 93 |
0 |
▶ | if (!in) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!in) |
0 |
0 |
0 |
|
| 94 |
|
| { |
| 95 |
0 |
| sprintf(buf, "%.255s: %.255s", "progname", file); |
| 96 |
0 |
| perror(buf); |
| 97 |
0 |
| exit (1); |
| 98 |
|
| } |
| 99 |
|
| } |
| 100 |
|
| |
| 101 |
0 |
| read_ppm_line (in, buf, sizeof(buf), file, &line, 1); /* header line */ |
| 102 |
0 |
▶ | if (buf[0] != 'P' || |
Branch: 1 branch
| Branch | True | False | Total |
| if (buf[0] != 'P' || |
0 |
0 |
0 |
|
| 103 |
0 |
▶ | buf[1] < '1' || |
Branch: 1 branch
| Branch | True | False | Total |
| buf[1] < '1' || |
0 |
0 |
0 |
|
| 104 |
0 |
▶ | buf[1] > '6' || |
Branch: 1 branch
| Branch | True | False | Total |
| buf[1] > '6' || |
0 |
0 |
0 |
|
| 105 |
0 |
▶ | buf[2] != '\n') |
Branch: 1 branch
| Branch | True | False | Total |
| buf[2] != '\n') |
0 |
0 |
0 |
|
| 106 |
|
| { |
| 107 |
0 |
| fprintf (stderr, "%s: %s: not a PBM, PGM, or PPM file\n", |
| 108 |
|
| "progname", file); |
| 109 |
0 |
| exit (1); |
| 110 |
|
| } |
| 111 |
|
| |
| 112 |
0 |
| ppm->type = buf[1] - '0'; |
| 113 |
0 |
▶ | if (ppm->type < 1 || ppm->type > 6) |
Branch: 2 branches
| Branch | True | False | Total |
| if (ppm->type < 1 || ppm->type > 6) |
0 |
0 |
0 |
| if (ppm->type < 1 || ppm->type > 6) |
0 |
0 |
0 |
|
| 114 |
0 |
| abort(); |
| 115 |
|
| |
| 116 |
|
| |
| 117 |
0 |
| read_ppm_line (in, buf, sizeof(buf), file, &line, 1); /* width/height line */ |
| 118 |
|
| |
| 119 |
0 |
▶ | if (2 != sscanf (buf, "%d %d %c", &ppm->width, &ppm->height, &dummy) || |
Branch: 1 branch
| Branch | True | False | Total |
| if (2 != sscanf (buf, "%d %d %c", &ppm->width, &ppm->height, &dummy) || |
0 |
0 |
0 |
|
| 120 |
0 |
▶ | ppm->width < 1 || ppm->height < 1) |
Branch: 2 branches
| Branch | True | False | Total |
| ppm->width < 1 || ppm->height < 1) |
0 |
0 |
0 |
| ppm->width < 1 || ppm->height < 1) |
0 |
0 |
0 |
|
| 121 |
|
| { |
| 122 |
0 |
| fprintf (stderr, "%s: %s: %d: bogus PNM file: %s\n", |
| 123 |
|
| "progname", file, line, buf); |
| 124 |
0 |
| exit (1); |
| 125 |
|
| } |
| 126 |
|
| |
| 127 |
0 |
| ppm->rgba = (unsigned char *) |
| 128 |
0 |
| calloc (1, (ppm->width + 2) * (ppm->height + 2) * 4); |
| 129 |
|
| |
| 130 |
0 |
▶ | if (!ppm->rgba) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ppm->rgba) |
0 |
0 |
0 |
|
| 131 |
|
| { |
| 132 |
0 |
| fprintf (stderr, "%s: %s: out of memory (%d x %d)\n", |
| 133 |
|
| "progname", file, ppm->width, ppm->height); |
| 134 |
0 |
| exit (1); |
| 135 |
|
| } |
| 136 |
|
| |
| 137 |
0 |
▶ | if (ppm->type == 1 || ppm->type == 4) |
Branch: 2 branches
| Branch | True | False | Total |
| if (ppm->type == 1 || ppm->type == 4) |
0 |
0 |
0 |
| if (ppm->type == 1 || ppm->type == 4) |
0 |
0 |
0 |
|
| 138 |
0 |
| max = 1; |
| 139 |
|
| else |
| 140 |
|
| { |
| 141 |
0 |
| read_ppm_line (in, buf, sizeof(buf), file, &line, 1); /* maxval line */ |
| 142 |
0 |
▶ | if (1 != sscanf (buf, "%d %c", &max, &dummy) || max < 1) |
Branch: 2 branches
| Branch | True | False | Total |
| if (1 != sscanf (buf, "%d %c", &max, &dummy) || max < 1) |
0 |
0 |
0 |
| if (1 != sscanf (buf, "%d %c", &max, &dummy) || max < 1) |
0 |
0 |
0 |
|
| 143 |
|
| { |
| 144 |
0 |
| fprintf (stderr, "%s: %s: %d: bogus PNM file: %s\n", |
| 145 |
|
| "progname", file, line, buf); |
| 146 |
0 |
| exit (1); |
| 147 |
|
| } |
| 148 |
|
| |
| 149 |
0 |
▶ | if (max > 255) |
Branch: 1 branch
| Branch | True | False | Total |
| if (max > 255) |
0 |
0 |
0 |
|
| 150 |
|
| { |
| 151 |
0 |
| fprintf (stderr, "%s: %s: %d: maxval must be <= 255\n", |
| 152 |
|
| "progname", file, line); |
| 153 |
0 |
| exit (1); |
| 154 |
|
| } |
| 155 |
|
| } |
| 156 |
|
| |
| 157 |
0 |
▶ | if (ppm->type == 1 || ppm->type == 2 || ppm->type == 3) |
Branch: 3 branches
| Branch | True | False | Total |
| if (ppm->type == 1 || ppm->type == 2 || ppm->type == 3) |
0 |
0 |
0 |
| if (ppm->type == 1 || ppm->type == 2 || ppm->type == 3) |
0 |
0 |
0 |
| if (ppm->type == 1 || ppm->type == 2 || ppm->type == 3) |
0 |
0 |
0 |
|
| 158 |
0 |
| { |
| 159 |
0 |
| unsigned char *out = ppm->rgba; |
| 160 |
0 |
| unsigned char *end = out + (ppm->width * ppm->height * 4); |
| 161 |
0 |
| int tick = 0; |
| 162 |
|
| |
| 163 |
0 |
▶ | while (read_ppm_line (in, buf, sizeof(buf), file, &line, 0)) |
Branch: 1 branch
| Branch | True | False | Total |
| while (read_ppm_line (in, buf, sizeof(buf), file, &line, 0)) |
0 |
0 |
0 |
|
| 164 |
|
| { |
| 165 |
0 |
| char *s = buf; |
| 166 |
0 |
▶ | while (*s && (*s != '\r' && *s != '\n')) |
Branch: 3 branches
| Branch | True | False | Total |
| while (*s && (*s != '\r' && *s != '\n')) |
0 |
0 |
0 |
| while (*s && (*s != '\r' && *s != '\n')) |
0 |
0 |
0 |
| while (*s && (*s != '\r' && *s != '\n')) |
0 |
0 |
0 |
|
| 167 |
|
| { |
| 168 |
0 |
| int any = 0; |
| 169 |
0 |
| int n = 0; |
| 170 |
0 |
▶ | while (*s == ' ' || *s == '\t') s++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (*s == ' ' || *s == '\t') s++; |
0 |
0 |
0 |
| while (*s == ' ' || *s == '\t') s++; |
0 |
0 |
0 |
|
| 171 |
|
| |
| 172 |
0 |
▶ | if (*s >= '0' && *s <= '9') |
Branch: 2 branches
| Branch | True | False | Total |
| if (*s >= '0' && *s <= '9') |
0 |
0 |
0 |
| if (*s >= '0' && *s <= '9') |
0 |
0 |
0 |
|
| 173 |
|
| { |
| 174 |
0 |
| any = 1; |
| 175 |
0 |
▶ | while (*s >= '0' && *s <= '9') |
Branch: 2 branches
| Branch | True | False | Total |
| while (*s >= '0' && *s <= '9') |
0 |
0 |
0 |
| while (*s >= '0' && *s <= '9') |
0 |
0 |
0 |
|
| 176 |
0 |
| n = (n * 10) + (*s++ - '0'); |
| 177 |
|
| } |
| 178 |
|
| |
| 179 |
0 |
▶ | if (n > max) |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > max) |
0 |
0 |
0 |
|
| 180 |
|
| { |
| 181 |
0 |
| fprintf (stderr, "%s: %s: %d: value (%d) exceeds max (%d)\n", |
| 182 |
|
| "progname", file, line, n, max); |
| 183 |
0 |
| exit (1); |
| 184 |
|
| } |
| 185 |
|
| |
| 186 |
0 |
▶ | if (*s && |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s && |
0 |
0 |
0 |
|
| 187 |
0 |
▶ | *s != ' ' && *s != '\t' && |
Branch: 2 branches
| Branch | True | False | Total |
| *s != ' ' && *s != '\t' && |
0 |
0 |
0 |
| *s != ' ' && *s != '\t' && |
0 |
0 |
0 |
|
| 188 |
0 |
▶ | *s != '\r' && *s != '\n') |
Branch: 2 branches
| Branch | True | False | Total |
| *s != '\r' && *s != '\n') |
0 |
0 |
0 |
| *s != '\r' && *s != '\n') |
0 |
0 |
0 |
|
| 189 |
|
| { |
| 190 |
0 |
| fprintf (stderr, "%s: %s: %d: bogus PNM file: '%c'\n", |
| 191 |
0 |
| "progname", file, line, *s); |
| 192 |
0 |
| exit (1); |
| 193 |
|
| } |
| 194 |
|
| |
| 195 |
0 |
▶ | if (!any) /* got nothing */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!any) /* got nothing */ |
0 |
0 |
0 |
|
| 196 |
|
| ; |
| 197 |
0 |
▶ | else if (ppm->type == 1) /* PBM */ |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ppm->type == 1) /* PBM */ |
0 |
0 |
0 |
|
| 198 |
|
| { |
| 199 |
0 |
▶ | *out++ = (n ? 255 : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| *out++ = (n ? 255 : 0); |
0 |
0 |
0 |
|
| 200 |
0 |
▶ | *out++ = (n ? 255 : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| *out++ = (n ? 255 : 0); |
0 |
0 |
0 |
|
| 201 |
0 |
▶ | *out++ = (n ? 255 : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| *out++ = (n ? 255 : 0); |
0 |
0 |
0 |
|
| 202 |
0 |
| *out++ = 255; |
| 203 |
|
| } |
| 204 |
0 |
▶ | else if (ppm->type == 2) /* PGM */ |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ppm->type == 2) /* PGM */ |
0 |
0 |
0 |
|
| 205 |
|
| { |
| 206 |
0 |
▶ | n = (max == 255 ? n : ((255 * n) / max)); |
Branch: 1 branch
| Branch | True | False | Total |
| n = (max == 255 ? n : ((255 * n) / max)); |
0 |
0 |
0 |
|
| 207 |
0 |
| *out++ = n; |
| 208 |
0 |
| *out++ = n; |
| 209 |
0 |
| *out++ = n; |
| 210 |
0 |
| *out++ = 255; |
| 211 |
|
| } |
| 212 |
0 |
▶ | else if (ppm->type == 3) /* PPM */ |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ppm->type == 3) /* PPM */ |
0 |
0 |
0 |
|
| 213 |
|
| { |
| 214 |
0 |
▶ | n = (max == 255 ? n : ((255 * n) / max)); |
Branch: 1 branch
| Branch | True | False | Total |
| n = (max == 255 ? n : ((255 * n) / max)); |
0 |
0 |
0 |
|
| 215 |
0 |
| *out++ = n; |
| 216 |
|
| |
| 217 |
0 |
▶ | if (++tick == 3) /* fill in alpha */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (++tick == 3) /* fill in alpha */ |
0 |
0 |
0 |
|
| 218 |
|
| { |
| 219 |
0 |
| tick = 0; |
| 220 |
0 |
| *out++ = 255; |
| 221 |
|
| } |
| 222 |
|
| } |
| 223 |
|
| else |
| 224 |
0 |
| abort(); |
| 225 |
|
| } |
| 226 |
|
| |
| 227 |
0 |
▶ | if (out > end) |
Branch: 1 branch
| Branch | True | False | Total |
| if (out > end) |
0 |
0 |
0 |
|
| 228 |
0 |
| break; |
| 229 |
|
| } |
| 230 |
|
| |
| 231 |
0 |
▶ | if (out < end) |
Branch: 1 branch
| Branch | True | False | Total |
| if (out < end) |
0 |
0 |
0 |
|
| 232 |
0 |
| fprintf (stderr, "%s: %s: %d: premature end of data\n", |
| 233 |
|
| "progname", file, line); |
| 234 |
|
| } |
| 235 |
0 |
▶ | else if (ppm->type == 4 || ppm->type == 5 || ppm->type == 6) |
Branch: 3 branches
| Branch | True | False | Total |
| else if (ppm->type == 4 || ppm->type == 5 || ppm->type == 6) |
0 |
0 |
0 |
| else if (ppm->type == 4 || ppm->type == 5 || ppm->type == 6) |
0 |
0 |
0 |
| else if (ppm->type == 4 || ppm->type == 5 || ppm->type == 6) |
0 |
0 |
0 |
|
| 236 |
0 |
| { |
| 237 |
|
| int L; |
| 238 |
0 |
| unsigned char *out = ppm->rgba; |
| 239 |
0 |
| unsigned char *end = out + (ppm->width * ppm->height * 4); |
| 240 |
0 |
| int tick = 0; |
| 241 |
0 |
| int x = 0; |
| 242 |
|
| |
| 243 |
0 |
▶ | while ((L = fread (buf, 1, sizeof(buf)-1, in)) > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| while ((L = fread (buf, 1, sizeof(buf)-1, in)) > 0) |
0 |
0 |
0 |
|
| 244 |
|
| { |
| 245 |
|
| int i; |
| 246 |
0 |
▶ | if (ppm->type == 4) /* PBM */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (ppm->type == 4) /* PBM */ |
0 |
0 |
0 |
|
| 247 |
|
| { |
| 248 |
0 |
▶ | for (i = 0; i < L; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < L; i++) |
0 |
0 |
0 |
|
| 249 |
|
| { |
| 250 |
|
| int j; |
| 251 |
0 |
▶ | for (j = 7; j >= 0; j--) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 7; j >= 0; j--) |
0 |
0 |
0 |
|
| 252 |
|
| { |
| 253 |
0 |
▶ | int n = ((buf[i] >> j) & 1) ? 255 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| int n = ((buf[i] >> j) & 1) ? 255 : 0; |
0 |
0 |
0 |
|
| 254 |
0 |
| out[0] = out[1] = out[2] = n; |
| 255 |
0 |
| out[3] = 255; |
| 256 |
|
| |
| 257 |
0 |
▶ | if (x++ >= ppm->width) |
Branch: 1 branch
| Branch | True | False | Total |
| if (x++ >= ppm->width) |
0 |
0 |
0 |
|
| 258 |
|
| { |
| 259 |
|
| /* eol pads to byte boundary */ |
| 260 |
0 |
| x = 0; |
| 261 |
0 |
| break; |
| 262 |
|
| } |
| 263 |
0 |
| out += 4; |
| 264 |
|
| } |
| 265 |
|
| } |
| 266 |
0 |
▶ | if (out > end) |
Branch: 1 branch
| Branch | True | False | Total |
| if (out > end) |
0 |
0 |
0 |
|
| 267 |
0 |
| break; |
| 268 |
|
| } |
| 269 |
0 |
▶ | else if (ppm->type == 5) /* PGM */ |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ppm->type == 5) /* PGM */ |
0 |
0 |
0 |
|
| 270 |
|
| { |
| 271 |
0 |
▶ | for (i = 0; i < L; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < L; i++) |
0 |
0 |
0 |
|
| 272 |
|
| { |
| 273 |
0 |
▶ | int n = (max == 255 ? buf[i] : ((255 * buf[i]) / max)); |
Branch: 1 branch
| Branch | True | False | Total |
| int n = (max == 255 ? buf[i] : ((255 * buf[i]) / max)); |
0 |
0 |
0 |
|
| 274 |
0 |
| out[0] = out[1] = out[2] = n; |
| 275 |
0 |
| out[3] = 255; |
| 276 |
0 |
| out += 4; |
| 277 |
|
| } |
| 278 |
0 |
▶ | if (out > end) |
Branch: 1 branch
| Branch | True | False | Total |
| if (out > end) |
0 |
0 |
0 |
|
| 279 |
0 |
| break; |
| 280 |
|
| } |
| 281 |
0 |
▶ | else if (ppm->type == 6) /* PPM */ |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ppm->type == 6) /* PPM */ |
0 |
0 |
0 |
|
| 282 |
|
| { |
| 283 |
0 |
▶ | for (i = 0; i < L; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < L; i++) |
0 |
0 |
0 |
|
| 284 |
|
| { |
| 285 |
0 |
▶ | int n = (max == 255 ? buf[i] : ((255 * buf[i]) / max)); |
Branch: 1 branch
| Branch | True | False | Total |
| int n = (max == 255 ? buf[i] : ((255 * buf[i]) / max)); |
0 |
0 |
0 |
|
| 286 |
0 |
| *out++ = n; |
| 287 |
|
| |
| 288 |
0 |
▶ | if (++tick == 3) /* fill in alpha */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (++tick == 3) /* fill in alpha */ |
0 |
0 |
0 |
|
| 289 |
|
| { |
| 290 |
0 |
| tick = 0; |
| 291 |
0 |
| *out++ = 255; |
| 292 |
|
| } |
| 293 |
|
| } |
| 294 |
0 |
▶ | if (out > end) |
Branch: 1 branch
| Branch | True | False | Total |
| if (out > end) |
0 |
0 |
0 |
|
| 295 |
0 |
| break; |
| 296 |
|
| } |
| 297 |
|
| else |
| 298 |
0 |
| abort(); |
| 299 |
|
| } |
| 300 |
|
| } |
| 301 |
|
| else |
| 302 |
0 |
| abort(); |
| 303 |
|
| |
| 304 |
|
| |
| 305 |
0 |
▶ | if (!stdin_p) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!stdin_p) |
0 |
0 |
0 |
|
| 306 |
0 |
| fclose (in); |
| 307 |
|
| |
| 308 |
0 |
| return ppm; |
| 309 |
|
| } |
| 310 |
|
| |
| 311 |
|
| |
| 312 |
|
| |
| 313 |
|
| /* Write a PBM, PGM, or PPM file, binary or ASCII, depending on the value |
| 314 |
|
| of the `type' slot. File name of "-" means stdout. |
| 315 |
|
| */ |
| 316 |
|
| void |
| 317 |
0 |
| write_ppm (struct ppm *ppm, const char *file) |
| 318 |
|
| { |
| 319 |
0 |
| int stdout_p = !strcmp(file, "-"); |
| 320 |
|
| FILE *out; |
| 321 |
|
| unsigned char *s, *end; |
| 322 |
|
| char buf[10240]; |
| 323 |
|
| |
| 324 |
0 |
▶ | if (stdout_p) |
Branch: 1 branch
| Branch | True | False | Total |
| if (stdout_p) |
0 |
0 |
0 |
|
| 325 |
0 |
| out = stdout; |
| 326 |
|
| else |
| 327 |
|
| { |
| 328 |
0 |
| out = fopen (file, "w"); |
| 329 |
0 |
▶ | if (!out) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!out) |
0 |
0 |
0 |
|
| 330 |
|
| { |
| 331 |
0 |
| sprintf(buf, "%.255s: %.255s", "progname", file); |
| 332 |
0 |
| perror(buf); |
| 333 |
0 |
| exit (1); |
| 334 |
|
| } |
| 335 |
|
| } |
| 336 |
|
| |
| 337 |
0 |
| fprintf (out, "P%d\n", ppm->type); |
| 338 |
0 |
| fprintf (out, "%d %d\n", ppm->width, ppm->height); |
| 339 |
|
| |
| 340 |
0 |
▶ | if (ppm->type != 1 && ppm->type != 4) |
Branch: 2 branches
| Branch | True | False | Total |
| if (ppm->type != 1 && ppm->type != 4) |
0 |
0 |
0 |
| if (ppm->type != 1 && ppm->type != 4) |
0 |
0 |
0 |
|
| 341 |
0 |
| fprintf (out, "%d\n", 255); |
| 342 |
|
| |
| 343 |
0 |
| s = ppm->rgba; |
| 344 |
0 |
| end = s + (ppm->width * ppm->height * 4); |
| 345 |
|
| |
| 346 |
0 |
▶ | if (ppm->type == 1) /* PBM */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (ppm->type == 1) /* PBM */ |
0 |
0 |
0 |
|
| 347 |
|
| { |
| 348 |
0 |
| int col = 0; |
| 349 |
0 |
| char *o = buf; |
| 350 |
0 |
▶ | while (s < end) |
Branch: 1 branch
| Branch | True | False | Total |
| while (s < end) |
0 |
0 |
0 |
|
| 351 |
|
| { |
| 352 |
0 |
| int p = (s[0] + s[1] + s[2]) / 3; |
| 353 |
0 |
▶ | *o++ = (p > 127 ? '1' : '0'); |
Branch: 1 branch
| Branch | True | False | Total |
| *o++ = (p > 127 ? '1' : '0'); |
0 |
0 |
0 |
|
| 354 |
0 |
| *o++ = ' '; |
| 355 |
0 |
| s += 4; |
| 356 |
0 |
| col += 2; |
| 357 |
0 |
▶ | if (col >= 68) |
Branch: 1 branch
| Branch | True | False | Total |
| if (col >= 68) |
0 |
0 |
0 |
|
| 358 |
|
| { |
| 359 |
0 |
| o--; |
| 360 |
0 |
| *o++ = '\n'; |
| 361 |
0 |
| *o = 0; |
| 362 |
0 |
| fputs (buf, out); |
| 363 |
0 |
| o = buf; |
| 364 |
0 |
| *o = 0; |
| 365 |
0 |
| col = 0; |
| 366 |
|
| } |
| 367 |
|
| } |
| 368 |
|
| |
| 369 |
0 |
▶ | if (o > buf) |
Branch: 1 branch
| Branch | True | False | Total |
| if (o > buf) |
0 |
0 |
0 |
|
| 370 |
|
| { |
| 371 |
0 |
| o--; |
| 372 |
0 |
| *o++ = '\n'; |
| 373 |
0 |
| *o = 0; |
| 374 |
0 |
| fputs (buf, out); |
| 375 |
|
| } |
| 376 |
|
| } |
| 377 |
0 |
▶ | else if (ppm->type == 2) /* PGM */ |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ppm->type == 2) /* PGM */ |
0 |
0 |
0 |
|
| 378 |
|
| { |
| 379 |
0 |
| int col = 0; |
| 380 |
0 |
▶ | while (s < end) |
Branch: 1 branch
| Branch | True | False | Total |
| while (s < end) |
0 |
0 |
0 |
|
| 381 |
|
| { |
| 382 |
0 |
| int p = (s[0] + s[1] + s[2]) / 3; |
| 383 |
0 |
| fprintf (out, "%3d ", p); |
| 384 |
0 |
| s += 4; |
| 385 |
0 |
| col += 4; /* close enough */ |
| 386 |
0 |
▶ | if (col >= 66) |
Branch: 1 branch
| Branch | True | False | Total |
| if (col >= 66) |
0 |
0 |
0 |
|
| 387 |
|
| { |
| 388 |
0 |
| fprintf (out, "\n"); |
| 389 |
0 |
| col = 0; |
| 390 |
|
| } |
| 391 |
|
| } |
| 392 |
0 |
▶ | if (col != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (col != 0) |
0 |
0 |
0 |
|
| 393 |
0 |
| fprintf (out, "\n"); |
| 394 |
|
| } |
| 395 |
0 |
▶ | else if (ppm->type == 3) /* PPM */ |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ppm->type == 3) /* PPM */ |
0 |
0 |
0 |
|
| 396 |
|
| { |
| 397 |
0 |
| int col = 0; |
| 398 |
0 |
▶ | while (s < end) |
Branch: 1 branch
| Branch | True | False | Total |
| while (s < end) |
0 |
0 |
0 |
|
| 399 |
|
| { |
| 400 |
0 |
| fprintf (out, "%3d %3d %3d ", s[0], s[1], s[2]); |
| 401 |
0 |
| s += 4; |
| 402 |
0 |
| col += 12; /* close enough */ |
| 403 |
0 |
▶ | if (col >= 60) |
Branch: 1 branch
| Branch | True | False | Total |
| if (col >= 60) |
0 |
0 |
0 |
|
| 404 |
|
| { |
| 405 |
0 |
| fprintf (out, "\n"); |
| 406 |
0 |
| col = 0; |
| 407 |
|
| } |
| 408 |
|
| } |
| 409 |
0 |
▶ | if (col != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (col != 0) |
0 |
0 |
0 |
|
| 410 |
0 |
| fprintf (out, "\n"); |
| 411 |
|
| } |
| 412 |
|
| |
| 413 |
0 |
▶ | else if (ppm->type == 4) /* PBM */ |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ppm->type == 4) /* PBM */ |
0 |
0 |
0 |
|
| 414 |
|
| { |
| 415 |
0 |
| char *obuf = buf; |
| 416 |
0 |
| char *oend = buf + sizeof(buf) - 10; |
| 417 |
0 |
| int x = 0; |
| 418 |
0 |
▶ | while (s < end) |
Branch: 1 branch
| Branch | True | False | Total |
| while (s < end) |
0 |
0 |
0 |
|
| 419 |
|
| { |
| 420 |
0 |
| int byte = 0; |
| 421 |
|
| int i; |
| 422 |
0 |
▶ | for (i = 7; i >= 0; i--) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 7; i >= 0; i--) |
0 |
0 |
0 |
|
| 423 |
|
| { |
| 424 |
0 |
| int p = (s[0] + s[1] + s[2]) / 3; |
| 425 |
0 |
▶ | if (p > 127) |
Branch: 1 branch
| Branch | True | False | Total |
| if (p > 127) |
0 |
0 |
0 |
|
| 426 |
0 |
| byte |= (1 << i); |
| 427 |
0 |
| s += 4; |
| 428 |
|
| |
| 429 |
0 |
▶ | if (++x >= ppm->width) |
Branch: 1 branch
| Branch | True | False | Total |
| if (++x >= ppm->width) |
0 |
0 |
0 |
|
| 430 |
|
| { |
| 431 |
|
| /* eol pads to byte boundary */ |
| 432 |
0 |
| x = 0; |
| 433 |
0 |
| break; |
| 434 |
|
| } |
| 435 |
|
| } |
| 436 |
0 |
| *obuf++ = byte; |
| 437 |
|
| |
| 438 |
0 |
▶ | if (obuf >= oend) |
Branch: 1 branch
| Branch | True | False | Total |
| if (obuf >= oend) |
0 |
0 |
0 |
|
| 439 |
|
| { |
| 440 |
0 |
| fwrite (buf, 1, obuf-buf, out); |
| 441 |
0 |
| obuf = buf; |
| 442 |
|
| } |
| 443 |
|
| } |
| 444 |
0 |
▶ | if (obuf != buf) |
Branch: 1 branch
| Branch | True | False | Total |
| if (obuf != buf) |
0 |
0 |
0 |
|
| 445 |
0 |
| fwrite (buf, 1, obuf-buf, out); |
| 446 |
|
| } |
| 447 |
0 |
▶ | else if (ppm->type == 5) /* PGM */ |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ppm->type == 5) /* PGM */ |
0 |
0 |
0 |
|
| 448 |
|
| { |
| 449 |
0 |
| char *obuf = buf; |
| 450 |
0 |
| char *oend = buf + sizeof(buf) - 10; |
| 451 |
0 |
▶ | while (s < end) |
Branch: 1 branch
| Branch | True | False | Total |
| while (s < end) |
0 |
0 |
0 |
|
| 452 |
|
| { |
| 453 |
0 |
| *obuf++ = ((s[0] + s[1] + s[2]) / 3); |
| 454 |
0 |
| s += 4; |
| 455 |
0 |
▶ | if (obuf >= oend) |
Branch: 1 branch
| Branch | True | False | Total |
| if (obuf >= oend) |
0 |
0 |
0 |
|
| 456 |
|
| { |
| 457 |
0 |
| fwrite (buf, 1, obuf-buf, out); |
| 458 |
0 |
| obuf = buf; |
| 459 |
|
| } |
| 460 |
|
| } |
| 461 |
0 |
▶ | if (obuf != buf) |
Branch: 1 branch
| Branch | True | False | Total |
| if (obuf != buf) |
0 |
0 |
0 |
|
| 462 |
0 |
| fwrite (buf, 1, obuf-buf, out); |
| 463 |
|
| } |
| 464 |
0 |
▶ | else if (ppm->type == 6) /* PPM */ |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ppm->type == 6) /* PPM */ |
0 |
0 |
0 |
|
| 465 |
|
| { |
| 466 |
0 |
| char *obuf = buf; |
| 467 |
0 |
| char *oend = buf + sizeof(buf) - 10; |
| 468 |
0 |
▶ | while (s < end) |
Branch: 1 branch
| Branch | True | False | Total |
| while (s < end) |
0 |
0 |
0 |
|
| 469 |
|
| { |
| 470 |
0 |
| *obuf++ = *s++; |
| 471 |
0 |
| *obuf++ = *s++; |
| 472 |
0 |
| *obuf++ = *s++; |
| 473 |
0 |
| s++; |
| 474 |
0 |
▶ | if (obuf >= oend) |
Branch: 1 branch
| Branch | True | False | Total |
| if (obuf >= oend) |
0 |
0 |
0 |
|
| 475 |
|
| { |
| 476 |
0 |
| fwrite (buf, 1, obuf-buf, out); |
| 477 |
0 |
| obuf = buf; |
| 478 |
|
| } |
| 479 |
|
| } |
| 480 |
0 |
▶ | if (obuf != buf) |
Branch: 1 branch
| Branch | True | False | Total |
| if (obuf != buf) |
0 |
0 |
0 |
|
| 481 |
0 |
| fwrite (buf, 1, obuf-buf, out); |
| 482 |
|
| } |
| 483 |
|
| else |
| 484 |
0 |
| abort(); |
| 485 |
|
| |
| 486 |
|
| |
| 487 |
|
| |
| 488 |
0 |
▶ | if (!stdout_p) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!stdout_p) |
0 |
0 |
0 |
|
| 489 |
0 |
| fclose (out); |
| 490 |
|
| else |
| 491 |
0 |
| fflush (out); |
| 492 |
0 |
| } |
| 493 |
|
| |
| 494 |
|
| |
| 495 |
|
| |
| 496 |
|
| /* Manipulating image data |
| 497 |
|
| */ |
| 498 |
|
| |
| 499 |
|
| struct ppm * |
| 500 |
191 |
| copy_ppm (struct ppm *ppm) |
| 501 |
|
| { |
| 502 |
191 |
| struct ppm *ppm2 = (struct ppm *) calloc (1, sizeof(*ppm)); |
| 503 |
|
| int n; |
| 504 |
191 |
| memcpy (ppm2, ppm, sizeof(*ppm2)); |
| 505 |
191 |
| n = ppm->width * ppm->height * 4; |
| 506 |
191 |
| ppm2->rgba = (unsigned char *) calloc (1, n); |
| 507 |
191 |
▶ | if (!ppm2->rgba) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ppm2->rgba) |
0 |
191 |
191 |
|
| 508 |
|
| { |
| 509 |
0 |
| fprintf (stderr, "%s: out of memory (%d x %d)\n", |
| 510 |
|
| "progname", ppm->width, ppm->height); |
| 511 |
0 |
| exit (1); |
| 512 |
|
| } |
| 513 |
191 |
| memcpy (ppm2->rgba, ppm->rgba, n); |
| 514 |
191 |
| return ppm2; |
| 515 |
|
| } |
| 516 |
|
| |
| 517 |
|
| |
| 518 |
|
| void |
| 519 |
290 |
| free_ppm (struct ppm *ppm) |
| 520 |
|
| { |
| 521 |
290 |
| memset (ppm->rgba, 0xDE, ppm->width * ppm->height * 4); |
| 522 |
290 |
| free (ppm->rgba); |
| 523 |
290 |
| memset (ppm, 0xDE, sizeof(*ppm)); |
| 524 |
290 |
| free (ppm); |
| 525 |
290 |
| } |
| 526 |
|
| |
| 527 |
|
| |
| 528 |
|
| void |
| 529 |
380405 |
| get_pixel (struct ppm *ppm, |
| 530 |
|
| int x, int y, |
| 531 |
|
| unsigned char *rP, |
| 532 |
|
| unsigned char *gP, |
| 533 |
|
| unsigned char *bP, |
| 534 |
|
| unsigned char *aP) |
| 535 |
|
| { |
| 536 |
380405 |
▶ | if (x < 0 || x >= ppm->width || |
Branch: 3 branches
| Branch | True | False | Total |
| if (x < 0 || x >= ppm->width || |
380405 |
0 |
380405 |
| if (x < 0 || x >= ppm->width || |
380405 |
0 |
380405 |
| if (x < 0 || x >= ppm->width || |
380405 |
0 |
380405 |
|
| 537 |
380405 |
▶ | y < 0 || y >= ppm->height) |
Branch: 1 branch
| Branch | True | False | Total |
| y < 0 || y >= ppm->height) |
0 |
380405 |
380405 |
|
| 538 |
|
| { |
| 539 |
0 |
| *rP = *gP = *bP = *aP = 0; |
| 540 |
|
| } |
| 541 |
|
| else |
| 542 |
|
| { |
| 543 |
380405 |
| unsigned char *p = ppm->rgba + (((y * ppm->width) + x) * 4); |
| 544 |
380405 |
| *rP = *p++; |
| 545 |
380405 |
| *gP = *p++; |
| 546 |
380405 |
| *bP = *p++; |
| 547 |
380405 |
| *aP = *p; |
| 548 |
|
| } |
| 549 |
380405 |
| } |
| 550 |
|
| |
| 551 |
|
| void |
| 552 |
238095 |
| put_pixel (struct ppm *ppm, |
| 553 |
|
| int x, int y, |
| 554 |
|
| unsigned char r, |
| 555 |
|
| unsigned char g, |
| 556 |
|
| unsigned char b, |
| 557 |
|
| unsigned char a) |
| 558 |
|
| { |
| 559 |
238095 |
▶ | if (x < 0 || x >= ppm->width || |
Branch: 3 branches
| Branch | True | False | Total |
| if (x < 0 || x >= ppm->width || |
145249 |
92846 |
238095 |
| if (x < 0 || x >= ppm->width || |
145249 |
0 |
145249 |
| if (x < 0 || x >= ppm->width || |
142402 |
2847 |
145249 |
|
| 560 |
142402 |
▶ | y < 0 || y >= ppm->height || |
Branch: 2 branches
| Branch | True | False | Total |
| y < 0 || y >= ppm->height || |
142217 |
185 |
142402 |
| y < 0 || y >= ppm->height || |
59034 |
83183 |
142217 |
|
| 561 |
|
| a <= 0) |
| 562 |
|
| { |
| 563 |
|
| /* no-op */ |
| 564 |
|
| } |
| 565 |
|
| else |
| 566 |
|
| { |
| 567 |
59034 |
| unsigned char *p = ppm->rgba + (((y * ppm->width) + x) * 4); |
| 568 |
59034 |
▶ | if (a == 255) |
Branch: 1 branch
| Branch | True | False | Total |
| if (a == 255) |
15219 |
43815 |
59034 |
|
| 569 |
|
| { |
| 570 |
15219 |
| p[0] = r; |
| 571 |
15219 |
| p[1] = g; |
| 572 |
15219 |
| p[2] = b; |
| 573 |
15219 |
| p[3] = a; |
| 574 |
|
| } |
| 575 |
|
| else |
| 576 |
|
| { |
| 577 |
|
| /* dest[xyz] = (old[xyz] * (1-new[a])) + (new[xyz] * new[a]) |
| 578 |
|
| dest[a] = old[a] + (new[a] * (1-old[a])) |
| 579 |
|
| */ |
| 580 |
43815 |
| unsigned char a2 = 255 - a; |
| 581 |
43815 |
| p[0] = ((p[0] * a2) + (r * a)) / 255; |
| 582 |
43815 |
| p[1] = ((p[1] * a2) + (g * a)) / 255; |
| 583 |
43815 |
| p[2] = ((p[2] * a2) + (b * a)) / 255; |
| 584 |
43815 |
| p[3] = p[3] + (a * (255 - p[3]) / 255); |
| 585 |
|
| } |
| 586 |
|
| } |
| 587 |
238095 |
| } |
| 588 |
|
| |
| 589 |
|
| |
| 590 |
|
| /* Paste part of one image into another, with all necessary clipping. |
| 591 |
|
| Alpha controls the blending of the new image into the old image |
| 592 |
|
| (and any alpha already in the new image is also taken into account.) |
| 593 |
|
| If override_fg/bg is an RGB value (0xNNNNNN) then any dark/light values |
| 594 |
|
| in the source image are assumed to be that, and only the source image's |
| 595 |
|
| intensity and alpha are used. (Note that 0 is a color: black. To not |
| 596 |
|
| specify override_color at all, pass in -1 (or ~0, which is the same |
| 597 |
|
| thing.) |
| 598 |
|
| */ |
| 599 |
|
| void |
| 600 |
2772 |
| paste_ppm (struct ppm *into, int to_x, int to_y, |
| 601 |
|
| struct ppm *from, int from_x, int from_y, int w, int h, |
| 602 |
|
| unsigned int override_fg, unsigned int override_bg, |
| 603 |
|
| int alpha) |
| 604 |
|
| { |
| 605 |
|
| int i, j; |
| 606 |
|
| unsigned char ofr, ofg, ofb; |
| 607 |
|
| unsigned char obr, obg, obb; |
| 608 |
|
| |
| 609 |
2772 |
▶ | if (alpha <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (alpha <= 0) |
0 |
2772 |
2772 |
|
| 610 |
0 |
| return; |
| 611 |
|
| |
| 612 |
2772 |
| ofr = (override_fg >> 16) & 0xFF; |
| 613 |
2772 |
| ofg = (override_fg >> 8) & 0xFF; |
| 614 |
2772 |
| ofb = (override_fg ) & 0xFF; |
| 615 |
2772 |
| obr = (override_bg >> 16) & 0xFF; |
| 616 |
2772 |
| obg = (override_bg >> 8) & 0xFF; |
| 617 |
2772 |
| obb = (override_bg ) & 0xFF; |
| 618 |
|
| |
| 619 |
2772 |
| override_fg = (override_fg <= 0xFFFFFF); |
| 620 |
2772 |
| override_bg = (override_bg <= 0xFFFFFF); |
| 621 |
|
| |
| 622 |
|
| # define MIDPOINT (3 * 160) /* around 62% intensity */ |
| 623 |
|
| |
| 624 |
38808 |
▶ | for (j = 0; j < h; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < h; j++) |
36036 |
2772 |
38808 |
|
| 625 |
274131 |
▶ | for (i = 0; i < w; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < w; i++) |
238095 |
36036 |
274131 |
|
| 626 |
|
| { |
| 627 |
|
| unsigned char r, g, b, a; |
| 628 |
238095 |
| get_pixel (from, from_x + i, from_y + j, &r, &g, &b, &a); |
| 629 |
238095 |
▶ | if (alpha < 255) a = a * alpha / 255; |
Branch: 1 branch
| Branch | True | False | Total |
| if (alpha < 255) a = a * alpha / 255; |
0 |
238095 |
238095 |
|
| 630 |
|
| |
| 631 |
238095 |
▶ | if (override_fg || override_bg) |
Branch: 2 branches
| Branch | True | False | Total |
| if (override_fg || override_bg) |
0 |
238095 |
238095 |
| if (override_fg || override_bg) |
0 |
0 |
0 |
|
| 632 |
|
| { |
| 633 |
238095 |
▶ | if (r + g + b < MIDPOINT) /* image's foreground color */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (r + g + b < MIDPOINT) /* image's foreground color */ |
238095 |
0 |
238095 |
|
| 634 |
|
| { |
| 635 |
238095 |
▶ | if (override_fg) |
Branch: 1 branch
| Branch | True | False | Total |
| if (override_fg) |
238095 |
0 |
238095 |
|
| 636 |
|
| { |
| 637 |
238095 |
| r = 255-((255-r) * (255-ofr) / 255); |
| 638 |
238095 |
| g = 255-((255-g) * (255-ofg) / 255); |
| 639 |
238095 |
| b = 255-((255-b) * (255-ofb) / 255); |
| 640 |
|
| } |
| 641 |
|
| } |
| 642 |
|
| else /* image's background color */ |
| 643 |
|
| { |
| 644 |
0 |
▶ | if (override_bg) |
Branch: 1 branch
| Branch | True | False | Total |
| if (override_bg) |
0 |
0 |
0 |
|
| 645 |
|
| { |
| 646 |
0 |
| r = r * obr / 255; |
| 647 |
0 |
| g = g * obg / 255; |
| 648 |
0 |
| b = b * obb / 255; |
| 649 |
|
| } |
| 650 |
|
| } |
| 651 |
|
| } |
| 652 |
|
| |
| 653 |
238095 |
| put_pixel (into, to_x + i, to_y + j, r, g, b, a); |
| 654 |
|
| } |
| 655 |
|
| # undef MIDPOINT |
| 656 |
|
| } |
| 657 |
|
| |
| 658 |
|
| |
| 659 |
|
| /* Returns a copy of the PPM, scaled larger or smaller. |
| 660 |
|
| When scaling down, it dithers; when scaling up, it does not. |
| 661 |
|
| */ |
| 662 |
|
| struct ppm * |
| 663 |
191 |
| scale_ppm (struct ppm *ppm, double scale) |
| 664 |
|
| { |
| 665 |
|
| int x, y; |
| 666 |
|
| struct ppm *ppm2; |
| 667 |
|
| |
| 668 |
191 |
▶ | if (scale == 1.0) return copy_ppm (ppm); |
Branch: 1 branch
| Branch | True | False | Total |
| if (scale == 1.0) return copy_ppm (ppm); |
0 |
191 |
191 |
|
| 669 |
191 |
▶ | if (scale <= 0.0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (scale <= 0.0) return 0; |
0 |
191 |
191 |
|
| 670 |
|
| |
| 671 |
191 |
| ppm2 = (struct ppm *) malloc (sizeof(*ppm2)); |
| 672 |
191 |
| memcpy (ppm2, ppm, sizeof(*ppm2)); |
| 673 |
191 |
| ppm2->width = (ppm->width + 0.5) * scale; |
| 674 |
191 |
| ppm2->height = (ppm->height + 0.5) * scale; |
| 675 |
|
| |
| 676 |
191 |
| ppm2->rgba = (unsigned char *) |
| 677 |
191 |
| calloc (1, (ppm2->width) * (ppm2->height) * 4); |
| 678 |
191 |
▶ | if (!ppm2->rgba) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ppm2->rgba) |
0 |
191 |
191 |
|
| 679 |
|
| { |
| 680 |
0 |
| fprintf (stderr, "%s: out of memory (%d x %d)\n", |
| 681 |
|
| "progname", ppm2->width, ppm2->height); |
| 682 |
0 |
| exit (1); |
| 683 |
|
| } |
| 684 |
|
| |
| 685 |
191 |
▶ | if (scale < 1.0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (scale < 1.0) |
191 |
0 |
191 |
|
| 686 |
2674 |
▶ | for (y = 0; y < ppm2->height; y++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (y = 0; y < ppm2->height; y++) |
2483 |
191 |
2674 |
|
| 687 |
19539 |
▶ | for (x = 0; x < ppm2->width; x++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (x = 0; x < ppm2->width; x++) |
17056 |
2483 |
19539 |
|
| 688 |
|
| { |
| 689 |
17056 |
| int x1 = (x - 0) / scale; |
| 690 |
17056 |
| int x2 = (x + 1) / scale; |
| 691 |
17056 |
| int y1 = (y - 0) / scale; |
| 692 |
17056 |
| int y2 = (y + 1) / scale; |
| 693 |
|
| int xx, yy; |
| 694 |
17056 |
| int tr = 0, tg = 0, tb = 0, ta = 0; |
| 695 |
17056 |
| int total_pixels = (x2-x1) * (y2-y1); |
| 696 |
|
| |
| 697 |
17056 |
| unsigned char *p = ppm2->rgba + (((y * ppm2->width) + x) * 4); |
| 698 |
|
| |
| 699 |
17056 |
▶ | if (total_pixels <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (total_pixels <= 0) |
0 |
17056 |
17056 |
|
| 700 |
0 |
| continue; |
| 701 |
|
| |
| 702 |
66912 |
▶ | for (yy = y1; yy < y2; yy++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (yy = y1; yy < y2; yy++) |
49856 |
17056 |
66912 |
|
| 703 |
192166 |
▶ | for (xx = x1; xx < x2; xx++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (xx = x1; xx < x2; xx++) |
142310 |
49856 |
192166 |
|
| 704 |
|
| { |
| 705 |
|
| unsigned char r, g, b, a; |
| 706 |
142310 |
| get_pixel (ppm, |
| 707 |
|
| (xx < 0 ? 0 : xx), |
| 708 |
|
| (yy < 0 ? 0 : yy), |
| 709 |
|
| &r, &g, &b, &a); |
| 710 |
142310 |
| tr += r; |
| 711 |
142310 |
| tg += g; |
| 712 |
142310 |
| tb += b; |
| 713 |
142310 |
| ta += a; |
| 714 |
|
| } |
| 715 |
|
| |
| 716 |
17056 |
| tr /= total_pixels; |
| 717 |
17056 |
| tg /= total_pixels; |
| 718 |
17056 |
| tb /= total_pixels; |
| 719 |
17056 |
| ta /= total_pixels; |
| 720 |
|
| |
| 721 |
|
| /* put_pixel would do the wrong thing with alpha here */ |
| 722 |
17056 |
| p[0] = tr; |
| 723 |
17056 |
| p[1] = tg; |
| 724 |
17056 |
| p[2] = tb; |
| 725 |
17056 |
| p[3] = ta; |
| 726 |
|
| } |
| 727 |
|
| |
| 728 |
|
| else /* scale > 1.0 */ |
| 729 |
|
| |
| 730 |
0 |
▶ | for (y = 0; y < ppm2->height; y++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (y = 0; y < ppm2->height; y++) |
0 |
0 |
0 |
|
| 731 |
0 |
▶ | for (x = 0; x < ppm2->width; x++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (x = 0; x < ppm2->width; x++) |
0 |
0 |
0 |
|
| 732 |
|
| { |
| 733 |
|
| unsigned char r, g, b, a; |
| 734 |
0 |
| unsigned char *p = ppm2->rgba + (((y * ppm2->width) + x) * 4); |
| 735 |
0 |
| get_pixel (ppm, |
| 736 |
0 |
| (x + 0.5) / scale, |
| 737 |
0 |
| (y + 0.5) / scale, |
| 738 |
|
| &r, &g, &b, &a); |
| 739 |
|
| /* put_pixel would do the wrong thing with alpha here */ |
| 740 |
0 |
| p[0] = r; |
| 741 |
0 |
| p[1] = g; |
| 742 |
0 |
| p[2] = b; |
| 743 |
0 |
| p[3] = a; |
| 744 |
|
| } |
| 745 |
|
| |
| 746 |
191 |
| return ppm2; |
| 747 |
|
| } |
| 748 |
|
| |
| 749 |
|
| |
| 750 |
|
| /* This is just the curve N^2.4 from 0.0-1.0, scaled to 0-256. |
| 751 |
|
| We use this curve to emphasize the blur: it doesn't drop off linearly, |
| 752 |
|
| but decays more sharply at the edges. |
| 753 |
|
| */ |
| 754 |
|
| static unsigned char blur_alpha_curve [256] = { |
| 755 |
|
| 0, 3, 5, 8, 10, 12, 15, 17, 19, 21, 24, 26, 28, 30, |
| 756 |
|
| 33, 35, 37, 39, 41, 44, 46, 48, 50, 52, 54, 56, 58, 60, |
| 757 |
|
| 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, |
| 758 |
|
| 90, 91, 93, 95, 97, 99, 101, 102, 104, 106, 108, 109, 111, 113, |
| 759 |
|
| 114, 116, 118, 120, 121, 123, 124, 126, 128, 129, 131, 132, 134, 135, |
| 760 |
|
| 137, 139, 140, 142, 143, 145, 146, 147, 149, 150, 152, 153, 155, 156, |
| 761 |
|
| 157, 159, 160, 161, 163, 164, 165, 167, 168, 169, 170, 172, 173, 174, |
| 762 |
|
| 175, 177, 178, 179, 180, 181, 183, 184, 185, 186, 187, 188, 189, 190, |
| 763 |
|
| 191, 192, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 203, 204, |
| 764 |
|
| 205, 206, 207, 208, 209, 210, 211, 212, 212, 213, 214, 215, 216, 217, |
| 765 |
|
| 217, 218, 219, 220, 220, 221, 222, 223, 223, 224, 225, 225, 226, 227, |
| 766 |
|
| 227, 228, 229, 229, 230, 231, 231, 232, 232, 233, 234, 234, 235, 235, |
| 767 |
|
| 236, 236, 237, 237, 238, 238, 239, 239, 240, 240, 241, 241, 242, 242, |
| 768 |
|
| 243, 243, 243, 244, 244, 245, 245, 245, 246, 246, 246, 247, 247, 247, |
| 769 |
|
| 248, 248, 248, 249, 249, 249, 249, 250, 250, 250, 250, 251, 251, 251, |
| 770 |
|
| 251, 252, 252, 252, 252, 252, 253, 253, 253, 253, 253, 253, 253, 254, |
| 771 |
|
| 254, 254, 254, 254, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, |
| 772 |
|
| 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 773 |
|
| 255, 255, 255, 255 |
| 774 |
|
| }; |
| 775 |
|
| |
| 776 |
|
| |
| 777 |
|
| /* Returns a copy of the PPM, blurred out. |
| 778 |
|
| The PPM will be enlarged in both directions by 2*radius. |
| 779 |
|
| */ |
| 780 |
|
| struct ppm * |
| 781 |
0 |
| blur_ppm (struct ppm *ppm, int radius) |
| 782 |
|
| { |
| 783 |
|
| int x, y; |
| 784 |
|
| struct ppm *ppm2; |
| 785 |
|
| int x1, x2, y1, y2; |
| 786 |
|
| int total_pixels; |
| 787 |
|
| int maxx, maxy; |
| 788 |
|
| |
| 789 |
0 |
▶ | if (radius == 0) return copy_ppm (ppm); |
Branch: 1 branch
| Branch | True | False | Total |
| if (radius == 0) return copy_ppm (ppm); |
0 |
0 |
0 |
|
| 790 |
0 |
▶ | if (radius < 0) radius = -radius; |
Branch: 1 branch
| Branch | True | False | Total |
| if (radius < 0) radius = -radius; |
0 |
0 |
0 |
|
| 791 |
|
| |
| 792 |
0 |
| ppm2 = (struct ppm *) malloc (sizeof(*ppm2)); |
| 793 |
0 |
| memcpy (ppm2, ppm, sizeof(*ppm2)); |
| 794 |
0 |
| ppm2->width = (ppm->width + (radius * 2)); |
| 795 |
0 |
| ppm2->height = (ppm->height + (radius * 2)); |
| 796 |
|
| |
| 797 |
0 |
| ppm2->rgba = (unsigned char *) |
| 798 |
0 |
| calloc (1, (ppm2->width) * (ppm2->height) * 4); |
| 799 |
|
| |
| 800 |
0 |
▶ | if (!ppm2->rgba) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ppm2->rgba) |
0 |
0 |
0 |
|
| 801 |
|
| { |
| 802 |
0 |
| fprintf (stderr, "%s: out of memory (%d x %d)\n", |
| 803 |
|
| "progname", ppm2->width, ppm2->height); |
| 804 |
0 |
| exit (1); |
| 805 |
|
| } |
| 806 |
|
| |
| 807 |
|
| |
| 808 |
0 |
| total_pixels = (radius * 2) * (radius * 2); |
| 809 |
0 |
| maxx = ppm->width - 1; |
| 810 |
0 |
| maxy = ppm->height - 1; |
| 811 |
|
| |
| 812 |
0 |
| y1 = -radius * 2; |
| 813 |
0 |
| y2 = 0; |
| 814 |
0 |
▶ | for (y = 0; y < ppm2->height; y++, y1++, y2++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (y = 0; y < ppm2->height; y++, y1++, y2++) |
0 |
0 |
0 |
|
| 815 |
|
| { |
| 816 |
0 |
| unsigned char *p = ppm2->rgba + ((y * ppm2->width) * 4); |
| 817 |
0 |
| x1 = -radius * 2; |
| 818 |
0 |
| x2 = 0; |
| 819 |
|
| |
| 820 |
0 |
▶ | for (x = 0; x < ppm2->width; x++, x1++, x2++, p += 4) |
Branch: 1 branch
| Branch | True | False | Total |
| for (x = 0; x < ppm2->width; x++, x1++, x2++, p += 4) |
0 |
0 |
0 |
|
| 821 |
|
| { |
| 822 |
|
| int xx, yy; |
| 823 |
0 |
| unsigned int tr = 0, tg = 0, tb = 0, ta = 0; |
| 824 |
|
| |
| 825 |
0 |
▶ | for (yy = y1; yy < y2; yy++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (yy = y1; yy < y2; yy++) |
0 |
0 |
0 |
|
| 826 |
0 |
▶ | for (xx = x1; xx < x2; xx++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (xx = x1; xx < x2; xx++) |
0 |
0 |
0 |
|
| 827 |
|
| { |
| 828 |
|
| unsigned char r, g, b, a; |
| 829 |
0 |
▶ | if (xx >= 0 && xx <= maxx && |
Branch: 3 branches
| Branch | True | False | Total |
| if (xx >= 0 && xx <= maxx && |
0 |
0 |
0 |
| if (xx >= 0 && xx <= maxx && |
0 |
0 |
0 |
| if (xx >= 0 && xx <= maxx && |
0 |
0 |
0 |
|
| 830 |
0 |
▶ | yy >= 0 && yy <= maxy) |
Branch: 1 branch
| Branch | True | False | Total |
| yy >= 0 && yy <= maxy) |
0 |
0 |
0 |
|
| 831 |
0 |
| get_pixel (ppm, xx, yy, &r, &g, &b, &a); |
| 832 |
|
| else |
| 833 |
0 |
| r = g = b = a = 0; |
| 834 |
0 |
| tr += r; |
| 835 |
0 |
| tg += g; |
| 836 |
0 |
| tb += b; |
| 837 |
0 |
| ta += a; |
| 838 |
|
| } |
| 839 |
|
| |
| 840 |
0 |
| tr /= total_pixels; |
| 841 |
0 |
| tg /= total_pixels; |
| 842 |
0 |
| tb /= total_pixels; |
| 843 |
0 |
| ta /= total_pixels; |
| 844 |
|
| |
| 845 |
0 |
▶ | if (ta > 255) abort(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ta > 255) abort(); |
0 |
0 |
0 |
|
| 846 |
0 |
| ta = blur_alpha_curve[ta]; |
| 847 |
|
| |
| 848 |
|
| /* put_pixel would do the wrong thing with alpha here */ |
| 849 |
0 |
| p[0] = tr; |
| 850 |
0 |
| p[1] = tg; |
| 851 |
0 |
| p[2] = tb; |
| 852 |
0 |
| p[3] = ta; |
| 853 |
|
| } |
| 854 |
|
| } |
| 855 |
|
| |
| 856 |
0 |
| return ppm2; |
| 857 |
|
| } |