| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include <stdio.h> |
| 23 |
|
| 24 |
#include "pm_c_util.h" |
| 25 |
#include "ppm.h" |
| 26 |
|
| 27 |
#define EOS '\0' |
| 28 |
|
| 29 |
#define MAXHIST 32767 |
| 30 |
|
| 31 |
static pixel **pixels; |
| 32 |
static colorhash_table cht; |
| 33 |
static int curcol = -1; |
| 34 |
static int polymode = FALSE; |
| 35 |
static int dxbmode = FALSE; |
| 36 |
static int bgcol = -1; |
| 37 |
static double aspect = 1.0; |
| 38 |
static int gamut = 256; |
| 39 |
|
| 40 |
#include "autocad.h" |
| 41 |
|
| 42 |
|
| 43 |
static void outrun (int const color, |
| 44 |
int const ysize, |
| 45 |
int const y, |
| 46 |
int const xstart, |
| 47 |
int const xend); |
| 48 |
|
| 49 |
static void slideout (int const xdots, |
| 50 |
int const ydots, |
| 51 |
int const ncolors, |
| 52 |
unsigned char *red, |
| 53 |
unsigned char *green, |
| 54 |
unsigned char *blue); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
static void |
| 60 |
outrun (int const color, |
| 61 |
int const ysize, |
| 62 |
int const y, |
| 63 |
int const xstart, |
| 64 |
int const xend) { |
| 65 |
|
| 66 |
if (color == 0) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
if (curcol != color) { |
| 71 |
if (dxbmode) { |
| 72 |
putchar((char)136); |
| 73 |
(void) pm_writelittleshort(stdout, color); |
| 74 |
} else { |
| 75 |
(void) pm_writelittleshort(stdout, (short) 0xFF00 | color); |
| 76 |
} |
| 77 |
curcol = color; |
| 78 |
} |
| 79 |
if (polymode) { |
| 80 |
int v, yb = (ysize - y) + 1, yspan = 1; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
for (v = y + 1; v <= ysize; v++) { |
| 89 |
int j, mismatch = FALSE; |
| 90 |
|
| 91 |
for (j = xstart; j <= xend; j++) { |
| 92 |
if (PPM_GETR(pixels[y][j]) != PPM_GETR(pixels[v][j])) { |
| 93 |
mismatch = TRUE; |
| 94 |
break; |
| 95 |
} |
| 96 |
} |
| 97 |
if (mismatch) { |
| 98 |
break; |
| 99 |
} |
| 100 |
for (j = xstart; j <= xend; j++) { |
| 101 |
PPM_ASSIGN(pixels[v][j], 0, 0, 0); |
| 102 |
} |
| 103 |
} |
| 104 |
yspan = v - y; |
| 105 |
|
| 106 |
if (dxbmode) { |
| 107 |
putchar(11); |
| 108 |
(void) pm_writelittleshort( |
| 109 |
stdout, (int) (xstart * aspect + 0.4999)); |
| 110 |
(void) pm_writelittleshort(stdout, yb); |
| 111 |
|
| 112 |
(void) pm_writelittleshort( |
| 113 |
stdout, (int) ((xend + 1) * aspect + 0.4999)); |
| 114 |
(void) pm_writelittleshort(stdout, yb); |
| 115 |
|
| 116 |
(void) pm_writelittleshort( |
| 117 |
stdout, (int) (xstart * aspect + 0.4999)); |
| 118 |
(void) pm_writelittleshort(stdout, yb - yspan); |
| 119 |
|
| 120 |
(void) pm_writelittleshort( |
| 121 |
stdout, (int) ((xend + 1) * aspect + 0.4999)); |
| 122 |
(void) pm_writelittleshort(stdout, yb - yspan); |
| 123 |
} else { |
| 124 |
(void) pm_writelittleshort(stdout, (short) 0xFD00); |
| 125 |
|
| 126 |
(void) pm_writelittleshort(stdout, 4); |
| 127 |
|
| 128 |
(void) pm_writelittleshort(stdout, -2); |
| 129 |
|
| 130 |
(void) pm_writelittleshort(stdout, (short)0xFD00); |
| 131 |
|
| 132 |
(void) pm_writelittleshort(stdout, xstart); |
| 133 |
(void) pm_writelittleshort(stdout, yb); |
| 134 |
|
| 135 |
(void) pm_writelittleshort(stdout, (short) 0xFD00); |
| 136 |
|
| 137 |
(void) pm_writelittleshort(stdout, xend + 1); |
| 138 |
(void) pm_writelittleshort(stdout, yb); |
| 139 |
|
| 140 |
(void) pm_writelittleshort(stdout, (short) 0xFD00); |
| 141 |
|
| 142 |
(void) pm_writelittleshort(stdout, xend + 1); |
| 143 |
(void) pm_writelittleshort(stdout, yb - yspan); |
| 144 |
|
| 145 |
(void) pm_writelittleshort(stdout, (short) 0xFD00); |
| 146 |
|
| 147 |
(void) pm_writelittleshort(stdout, xstart); |
| 148 |
(void) pm_writelittleshort(stdout, yb - yspan); |
| 149 |
|
| 150 |
(void) pm_writelittleshort(stdout, (short) 0xFD00); |
| 151 |
|
| 152 |
(void) pm_writelittleshort(stdout, 4); |
| 153 |
(void) pm_writelittleshort(stdout, -2); |
| 154 |
} |
| 155 |
} else { |
| 156 |
(void) pm_writelittleshort(stdout, xstart); |
| 157 |
(void) pm_writelittleshort(stdout, ysize - y); |
| 158 |
(void) pm_writelittleshort(stdout, xend); |
| 159 |
(void) pm_writelittleshort(stdout, ysize - y); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
static void |
| 168 |
slideout (int const xdots, |
| 169 |
int const ydots, |
| 170 |
int const ncolors, |
| 171 |
unsigned char *red, |
| 172 |
unsigned char *green, |
| 173 |
unsigned char *blue) { |
| 174 |
|
| 175 |
static char sldhead[18] = "AutoCAD Slide\r\n\32"; |
| 176 |
static char dxbhead[20] = "AutoCAD DXB 1.0\r\n\32"; |
| 177 |
unsigned char *acadmap; |
| 178 |
int i, xsize, ysize; |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
if (bgcol > 0) { |
| 186 |
acadcol[0][0] = acadcol[bgcol][0]; |
| 187 |
acadcol[0][1] = acadcol[bgcol][1]; |
| 188 |
acadcol[0][2] = acadcol[bgcol][2]; |
| 189 |
acadcol[bgcol][0] = acadcol[bgcol][1] = acadcol[bgcol][2] = 0; |
| 190 |
} |
| 191 |
|
| 192 |
acadmap = (unsigned char *) pm_allocrow(ncolors, sizeof(unsigned char)); |
| 193 |
xsize = polymode ? xdots : (xdots - 1); |
| 194 |
ysize = polymode ? ydots : (ydots - 1); |
| 195 |
if (dxbmode) { |
| 196 |
fwrite(dxbhead, 19, 1, stdout); |
| 197 |
putchar((char)135); |
| 198 |
(void) pm_writelittleshort(stdout, 0); |
| 199 |
} else { |
| 200 |
fwrite(sldhead, 17, 1, stdout); |
| 201 |
putchar(86); |
| 202 |
putchar(2); |
| 203 |
(void) pm_writelittleshort(stdout, xsize); |
| 204 |
(void) pm_writelittleshort(stdout, ysize); |
| 205 |
|
| 206 |
(void) pm_writelittlelong( |
| 207 |
stdout, (long int) ((((double) xsize) / ysize) * aspect * 1E7)); |
| 208 |
(void) pm_writelittleshort(stdout, 2); |
| 209 |
(void) pm_writelittleshort(stdout, 0x1234); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
for (i = 0; i < ncolors; i++) { |
| 217 |
int best, j; |
| 218 |
long int dist = 3 * 256 * 256; |
| 219 |
|
| 220 |
for (j = 0; j < gamut; j++) { |
| 221 |
long int const dr = red[i] - acadcol[j][0], |
| 222 |
dg = green[i] - acadcol[j][1], |
| 223 |
db = blue[i] - acadcol[j][2]; |
| 224 |
long int const tdist = dr * dr + dg * dg + db * db; |
| 225 |
|
| 226 |
if (tdist < dist) { |
| 227 |
dist = tdist; |
| 228 |
best = j; |
| 229 |
} |
| 230 |
} |
| 231 |
acadmap[i] = best; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
for (i = 0; i < ydots; i++) { |
| 240 |
int x; |
| 241 |
|
| 242 |
for (x = 0; x < xdots; x++) { |
| 243 |
PPM_ASSIGN(pixels[i][x], |
| 244 |
acadmap[ppm_lookupcolor(cht, &pixels[i][x])], 0 ,0); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
for (i = 0; i < ydots; i++) { |
| 252 |
int x, rx, rpix = -1, nrun = 0; |
| 253 |
|
| 254 |
for (x = 0; x < xdots; x++) { |
| 255 |
int pix = PPM_GETR(pixels[i][x]); |
| 256 |
|
| 257 |
if (pix != rpix) { |
| 258 |
if (nrun > 0) { |
| 259 |
if (rpix > 0) { |
| 260 |
outrun(rpix, ydots - 1, i, rx, x - 1); |
| 261 |
} |
| 262 |
} |
| 263 |
rpix = pix; |
| 264 |
rx = x; |
| 265 |
nrun = 1; |
| 266 |
} |
| 267 |
} |
| 268 |
if ((nrun > 0) && (rpix > 0)) { |
| 269 |
outrun(rpix, ydots - 1, i, rx, xdots - 1); |
| 270 |
} |
| 271 |
} |
| 272 |
if (dxbmode) { |
| 273 |
putchar(0); |
| 274 |
} else { |
| 275 |
(void) pm_writelittleshort(stdout, (short) 0xFC00); |
| 276 |
|
| 277 |
} |
| 278 |
pm_freerow((char *) acadmap); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
int main(int argc, char* argv[]) { |
| 286 |
|
| 287 |
FILE *ifp; |
| 288 |
int argn, rows, cols, ncolors, i; |
| 289 |
int aspectspec = FALSE; |
| 290 |
pixval maxval; |
| 291 |
colorhist_vector chv; |
| 292 |
unsigned char *Red, *Green, *Blue; |
| 293 |
const char * const usage = |
| 294 |
"[-poly] [-dxb] [-white] [-background <col>]\n\ |
| 295 |
[-aspect <f>] [-8] [ppmfile]"; |
| 296 |
|
| 297 |
ppm_init(&argc, argv); |
| 298 |
|
| 299 |
argn = 1; |
| 300 |
while (argn < argc && argv[argn][0] == '-' && argv[argn][1] != EOS) { |
| 301 |
if (pm_keymatch(argv[argn], "-dxb", 2)) { |
| 302 |
dxbmode = polymode = TRUE; |
| 303 |
} else if (pm_keymatch(argv[argn], "-poly", 2)) { |
| 304 |
polymode = TRUE; |
| 305 |
} else if (pm_keymatch(argv[argn], "-white", 2)) { |
| 306 |
if (bgcol >= 0) { |
| 307 |
pm_error("already specified a background color"); |
| 308 |
} |
| 309 |
bgcol = 7; |
| 310 |
} else if (pm_keymatch(argv[argn], "-background", 2)) { |
| 311 |
if (bgcol >= 0) { |
| 312 |
pm_error("already specified a background color"); |
| 313 |
} |
| 314 |
argn++; |
| 315 |
if ((argn == argc) || (sscanf(argv[argn], "%d", &bgcol) != 1)) |
| 316 |
pm_usage(usage); |
| 317 |
if (bgcol < 0) { |
| 318 |
pm_error("background color must be >= 0 and <= 255"); |
| 319 |
} |
| 320 |
} else if (pm_keymatch(argv[argn], "-aspect", 2)) { |
| 321 |
if (aspectspec) { |
| 322 |
pm_error("already specified an aspect ratio"); |
| 323 |
} |
| 324 |
argn++; |
| 325 |
if ((argn == argc) || (sscanf(argv[argn], "%lf", &aspect) != 1)) |
| 326 |
pm_usage(usage); |
| 327 |
if (aspect <= 0.0) { |
| 328 |
pm_error("aspect ratio must be greater than 0"); |
| 329 |
} |
| 330 |
aspectspec = TRUE; |
| 331 |
} else if (pm_keymatch(argv[argn], "-8", 2)) { |
| 332 |
gamut = 8; |
| 333 |
} else { |
| 334 |
pm_usage(usage); |
| 335 |
} |
| 336 |
argn++; |
| 337 |
} |
| 338 |
|
| 339 |
if (argn < argc) { |
| 340 |
ifp = pm_openr(argv[argn]); |
| 341 |
argn++; |
| 342 |
} else { |
| 343 |
ifp = stdin; |
| 344 |
} |
| 345 |
|
| 346 |
if (argn != argc) { |
| 347 |
pm_usage(usage); |
| 348 |
} |
| 349 |
|
| 350 |
pixels = ppm_readppm(ifp, &cols, &rows, &maxval); |
| 351 |
|
| 352 |
pm_close(ifp); |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
while (TRUE) { |
| 358 |
int row, col; |
| 359 |
pixval newmaxval; |
| 360 |
pixel *pP; |
| 361 |
|
| 362 |
pm_message("computing colormap..."); |
| 363 |
chv = ppm_computecolorhist(pixels, cols, rows, MAXHIST, &ncolors); |
| 364 |
if (chv != (colorhist_vector) 0) |
| 365 |
break; |
| 366 |
newmaxval = maxval / 2; |
| 367 |
pm_message( |
| 368 |
"scaling colors from maxval=%d to maxval=%d to improve clustering...", |
| 369 |
maxval, newmaxval ); |
| 370 |
for (row = 0; row < rows; ++row) { |
| 371 |
for (col = 0, pP = pixels[row]; col < cols; ++col, ++pP) { |
| 372 |
PPM_DEPTH(*pP, *pP, maxval, newmaxval); |
| 373 |
} |
| 374 |
} |
| 375 |
maxval = newmaxval; |
| 376 |
} |
| 377 |
pm_message("%d colors found", ncolors); |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
if (maxval != 255) { |
| 383 |
pm_message("maxval is not 255 - automatically rescaling colors"); |
| 384 |
} |
| 385 |
Red = (unsigned char *) pm_allocrow(ncolors, sizeof(unsigned char)); |
| 386 |
Green = (unsigned char *) pm_allocrow(ncolors, sizeof(unsigned char)); |
| 387 |
Blue = (unsigned char *) pm_allocrow(ncolors, sizeof(unsigned char)); |
| 388 |
|
| 389 |
for (i = 0; i < ncolors; ++i) { |
| 390 |
if ( maxval == 255 ) { |
| 391 |
Red[i] = PPM_GETR(chv[i].color); |
| 392 |
Green[i] = PPM_GETG(chv[i].color); |
| 393 |
Blue[i] = PPM_GETB( chv[i].color ); |
| 394 |
} else { |
| 395 |
Red[i] = ((int) PPM_GETR(chv[i].color) * 255) / maxval; |
| 396 |
Green[i] = ((int) PPM_GETG(chv[i].color) * 255) / maxval; |
| 397 |
Blue[i] = ((int) PPM_GETB(chv[i].color) * 255) / maxval; |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
cht = ppm_colorhisttocolorhash(chv, ncolors); |
| 404 |
ppm_freecolorhist(chv); |
| 405 |
|
| 406 |
slideout(cols, rows, ncolors, Red, Green, Blue); |
| 407 |
pm_freerow((char *) Red); |
| 408 |
pm_freerow((char *) Green); |
| 409 |
pm_freerow((char *) Blue); |
| 410 |
exit(0); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|