| 1 |
/* |
| 2 |
* cspgen(image, cycle) - calculate next "cyclic space" generation |
| 3 |
* |
| 4 |
* The image is considered a torus, with top and bottom connected directly |
| 5 |
* and with sides connected using a shift of one row. |
| 6 |
*/ |
| 7 |
|
| 8 |
/* |
| 9 |
* internal buffer layout: |
| 10 |
* |
| 11 |
* image header |
| 12 |
* copy of last row |
| 13 |
* original array |
| 14 |
* copy of first row |
| 15 |
* |
| 16 |
* new array is stored atop old array, but directly after the header. |
| 17 |
*/ |
| 18 |
|
| 19 |
|
| 20 |
#include <stdlib.h> |
| 21 |
#include <string.h> |
| 22 |
#include "icall.h" |
| 23 |
|
| 24 |
|
| 25 |
int cspgen(int argc, descriptor *argv) |
| 26 |
{ |
| 27 |
int ulength, period, i; |
| 28 |
char *ustring, *udata, *cycle; |
| 29 |
char *old, *new; |
| 30 |
char o, x; |
| 31 |
|
| 32 |
int w, h, n; /* width, height, total pixels */ |
| 33 |
|
| 34 |
char hbuf[20]; /* image header buffer */ |
| 35 |
int hlen; /* header length */ |
| 36 |
|
| 37 |
static char *ibuf; /* image buffer */ |
| 38 |
static int ilen; /* buffer length */ |
| 39 |
int ineed; /* buffer length needed */ |
| 40 |
|
| 41 |
static char map[256]; /* mapping from one char to next */ |
| 42 |
|
| 43 |
/* |
| 44 |
* Get the parameters. |
| 45 |
*/ |
| 46 |
ArgString(1); /* validate types */ |
| 47 |
ArgString(2); |
| 48 |
ustring = StringAddr(argv[1]); /* universe string and length */ |
| 49 |
ulength = StringLen(argv[1]); |
| 50 |
cycle = StringAddr(argv[2]); /* cycle and length */ |
| 51 |
period = StringLen(argv[2]); |
| 52 |
sscanf(ustring, "%d", &w); /* row width */ |
| 53 |
|
| 54 |
/* |
| 55 |
* Build the generation mapping table. |
| 56 |
*/ |
| 57 |
map[cycle[period-1] & 0xFF] = cycle[0]; /* last maps to first */ |
| 58 |
for (i = 1; i < period; i++) |
| 59 |
map[cycle[i-1] & 0xFF] = cycle[i]; |
| 60 |
|
| 61 |
/* |
| 62 |
* Copy the image header (through the second comma) to hbuf. |
| 63 |
*/ |
| 64 |
old = ustring; |
| 65 |
new = hbuf; |
| 66 |
while ((*new++ = *old++) != ',') |
| 67 |
; |
| 68 |
while ((*new++ = *old++) != ',') |
| 69 |
; |
| 70 |
udata = old; |
| 71 |
hlen = udata - ustring; /* header length */ |
| 72 |
|
| 73 |
/* |
| 74 |
* Allocate the image buffer. |
| 75 |
*/ |
| 76 |
n = ulength - hlen; /* number of pixels */ |
| 77 |
if (n % w != 0) |
| 78 |
Error(205); |
| 79 |
h = n / w; /* image height */ |
| 80 |
|
| 81 |
ineed = hlen + n + 2 * w; /* buffer size needed */ |
| 82 |
if (ilen < ineed) |
| 83 |
if (!(ibuf = realloc(ibuf, ilen = ineed))) |
| 84 |
Error(305); |
| 85 |
|
| 86 |
/* |
| 87 |
* Copy the image into the buffer. Allow for the possibility that |
| 88 |
* the image already be *in* the buffer. |
| 89 |
*/ |
| 90 |
new = ibuf + hlen; |
| 91 |
old = new + w; |
| 92 |
memmove(old, udata, n); /* main image, leaving room */ |
| 93 |
memcpy(old - w, old + n - w, w); /* dup last row first first */ |
| 94 |
memcpy(old + n, old, w); /* dup first row beyond last */ |
| 95 |
|
| 96 |
/* |
| 97 |
* Create the new image. |
| 98 |
*/ |
| 99 |
memcpy(ibuf, hbuf, hlen); |
| 100 |
for (i = 0; i < n; i++) { |
| 101 |
o = *old; |
| 102 |
x = map[o & 0xFF]; |
| 103 |
if (old[-1] == x || old[1] == x || old[-w] == x || old[w] == x) |
| 104 |
o = x; |
| 105 |
*new++ = o; |
| 106 |
old++; |
| 107 |
} |
| 108 |
|
| 109 |
/* |
| 110 |
* Return the result. |
| 111 |
*/ |
| 112 |
RetConstStringN(ibuf, ulength); |
| 113 |
} |