Skip to content
Merged
1 change: 1 addition & 0 deletions ReferenceTests/Retro68/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ add_application(PolygonCapture PolygonCapture.c)
add_application(CopyBitsCapture CopyBitsCapture.c)
add_application(TextCapture TextCapture.c TextCapture.r)
add_application(GWorldCapture GWorldCapture.c)
add_application(UtilitiesCapture UtilitiesCapture.c)
28 changes: 28 additions & 0 deletions ReferenceTests/Retro68/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -741,3 +741,31 @@ what depths 0 and 24 make, as the screen here is 1 bit deep; the pixels
`UpdateGWorld` leaves with no flags or stretches with `stretchPix`; and the
flags it returns for smaller bounds.

## Utilities

`UtilitiesCapture.c` records `Random` from a range of seeds, `StuffHex`,
`PtToAngle` over rectangles of several shapes, `SlopeFromAngle` and where
`AngleFromSlope` changes its answer, and the Color Picker's conversions. It ran
from the Finder on Mac OS 9.2.2 in UTM. It writes `Utilities.txt`, kept here as
`Utilities-macos-9.2.2-ppc.txt` (CR line endings).

- `Random` stepped `randSeed`, taken as unsigned, by 16807 modulo 2^31 - 1, and
gave 0 for a low word of `0x8000`.
- `StuffHex` left out a last character without a pair and refused nothing: a
character up to `'9'`, or from `0x80`, gave its low four bits, any other those
of its code plus 9.
- `AngleFromSlope` stayed with a degree until the slope was 501 past
`SlopeFromAngle`'s, which from 73 degrees on was not quite the tangent in
fixed point. `PtToAngle`'s answers all follow from that.
- `PtToAngle`'s center was the rectangle's sides added and shifted down. A
point straight above or below it gave 0 or 180; then a rectangle without
width gave 90 or 270, and one without height 180 or 0.
- `Fix2SmallFract` gave `0xFFFF` for 1 and above, and a negative number's low
word.
- `RGB2HSL` and `RGB2HSV` rounded saturation to the nearest and hue down;
converting back rounded each product to the nearest, so colors did not always
survive the round trip.

`UtilitiesCaptureTests` makes the probe's calls through libQuickDraw and
compares every line but the slope calls', which are not QuickDraw's. All match.

1 change: 1 addition & 0 deletions ReferenceTests/Retro68/Utilities-macos-9.2.2-ppc.txt

Large diffs are not rendered by default.

294 changes: 294 additions & 0 deletions ReferenceTests/Retro68/UtilitiesCapture.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
/*
* Captures QuickDraw's small utilities: Random and randSeed, StuffHex,
* PtToAngle and the slope calls behind it, and the Color
* Picker's color conversions. It writes
* "Utilities.txt" next to itself and refuses to run if that file is already
* there. The file is flushed after every line, so a call that crashes leaves
* the lines before it; the color conversions, which are the likeliest to be
* missing, come last.
*/
#include <Quickdraw.h>
#include <Files.h>
#include <Fonts.h>
#include <ToolUtils.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

static short out;

static char text[6000];
static int used;

static void add(const char *format, ...)
{
va_list arguments;
va_start(arguments, format);
used += vsnprintf(text + used, sizeof text - used, format, arguments);
va_end(arguments);
}

static void start(const char *format, ...)
{
va_list arguments;
va_start(arguments, format);
used = vsnprintf(text, sizeof text, format, arguments);
va_end(arguments);
add(" ->");
}

static void end(void)
{
long count = strlen(text);
FSWrite(out, &count, text);
count = 1;
FSWrite(out, &count, "\r");
FlushVol(0, 0);
}

static Rect rect(short top, short left, short bottom, short right)
{
Rect r;
r.top = top; r.left = left; r.bottom = bottom; r.right = right;
return r;
}

/* Random */

static void sequence(long seed, int count)
{
int i;
qd.randSeed = seed;
start("Random from %08lX", (unsigned long)seed);
for (i = 0; i < count; i++) {
short value = Random();
add(" %d:%08lX", value, (unsigned long)qd.randSeed);
}
end();
}

static void randoms(void)
{
static const long seeds[] = {
1, 0, -1, 2, 12345, 0x7FFFFFFE, 0x7FFFFFFF, (long)0x80000000, (long)0x80000001,
(long)0xFFFFFFFE, 0x392453E7, 0x2B6CFBB6, 0x653CE19E, 0x1E613586
};
int i;
start("randSeed after InitGraf");
add(" %ld", (long)qd.randSeed);
end();
for (i = 0; i < sizeof seeds / sizeof seeds[0]; i++)
sequence(seeds[i], 8);
}

/* StuffHex */

static void stuff(const char *label, const unsigned char *hex)
{
unsigned char bytes[12];
int i;
memset(bytes, 0xA5, sizeof bytes);
StuffHex(bytes, hex);
start("StuffHex %s", label);
for (i = 0; i < sizeof bytes; i++)
add(" %02X", bytes[i]);
end();
}

static void stuffs(void)
{
stuff("digits", "\p0123456789ABCDEF");
stuff("lowercase", "\pabcdefABCDEF");
stuff("odd length", "\pFFF");
stuff("one character", "\pF");
stuff("empty", "\p");
stuff("beyond F", "\pGHIJKLMNOPQRSTUV");
stuff("lowercase beyond f", "\pghijklmnopqrstuv");
stuff("punctuation", "\p !#$%&()*+,-./:");
stuff("more punctuation", "\p;<=>?@[]^_`{|}~");
stuff("high characters", "\p\x80\x81\x9F\xA0\xC0\xF0\xFE\xFF");
}

/* PtToAngle */

static void angles(const char *label, Rect r, short step)
{
short midV = (r.top + r.bottom) / 2, midH = (r.left + r.right) / 2;
short dv, dh;
for (dv = -6; dv <= 6; dv++) {
start("PtToAngle %s (%d,%d,%d,%d) step %d row %d", label, r.top, r.left, r.bottom, r.right, step, dv);
for (dh = -6; dh <= 6; dh++) {
Point p;
short angle = -1;
p.v = midV + dv * step;
p.h = midH + dh * step;
PtToAngle(&r, p, &angle);
add(" %d", angle);
}
end();
}
}

/* Points around a quarter turn, finely enough to see the rounding. */
static void sweep(const char *label, Rect r)
{
short h;
start("PtToAngle sweep %s (%d,%d,%d,%d)", label, r.top, r.left, r.bottom, r.right);
for (h = -200; h <= 200; h += 3) {
Point p;
short angle = -1;
p.v = (r.top + r.bottom) / 2 - 100;
p.h = (r.left + r.right) / 2 + h;
PtToAngle(&r, p, &angle);
add(" %d", angle);
}
end();
}

static void ptAngles(void)
{
angles("square", rect(0, 0, 100, 100), 10);
angles("wide", rect(0, 0, 100, 200), 10);
angles("tall", rect(0, 0, 200, 100), 10);
angles("odd", rect(0, 0, 7, 13), 1);
angles("away", rect(-30, 40, 11, 63), 3);
angles("empty", rect(10, 10, 10, 10), 5);
angles("no width", rect(0, 10, 40, 10), 5);
angles("no height", rect(10, 0, 10, 40), 5);
angles("inverted", rect(100, 200, 0, 0), 10);
angles("one pixel", rect(0, 0, 1, 1), 1);
sweep("square", rect(0, 0, 100, 100));
sweep("wide", rect(0, 0, 100, 300));
sweep("tall", rect(0, 0, 300, 100));
sweep("odd", rect(0, 0, 41, 23));
sweep("huge", rect(-16000, -16000, 16000, 16000));
}

/* The slopes PtToAngle's angles come from */

/* The least magnitude of slope, with the sign given, at which AngleFromSlope
* leaves the answer it gives for the smallest one for each further answer. */
static void thresholds(const char *label, long sign)
{
long low = 1;
short angle = AngleFromSlope(sign);
int count = 0;
start("AngleFromSlope %s", label);
add(" 00000001:%d", angle);
while (count < 100) {
long under = low, over = 0x7FFFFFFF;
if (AngleFromSlope(sign * over) == angle) break;
while (over - under > 1) {
long middle = under + (over - under) / 2;
if (AngleFromSlope(sign * middle) == angle) under = middle; else over = middle;
}
low = over;
angle = AngleFromSlope(sign * low);
add(" %08lX:%d", (unsigned long)low, angle);
count++;
}
end();
}

static void slopes(void)
{
int angle;
start("SlopeFromAngle");
for (angle = 0; angle <= 180; angle++)
add(" %08lX", (unsigned long)SlopeFromAngle(angle));
end();
start("AngleFromSlope of zero");
add(" %d", AngleFromSlope(0));
end();
thresholds("positive", 1);
thresholds("negative", -1);
}

/* The Color Picker's conversions */

static const unsigned short colors[][3] = {
{0, 0, 0}, {0xFFFF, 0xFFFF, 0xFFFF}, {0x8000, 0x8000, 0x8000},
{0xFFFF, 0, 0}, {0, 0xFFFF, 0}, {0, 0, 0xFFFF},
{0xFFFF, 0xFFFF, 0}, {0, 0xFFFF, 0xFFFF}, {0xFFFF, 0, 0xFFFF},
{0x8000, 0x4000, 0xC000}, {0x1234, 0x5678, 0x9ABC}, {0xFEDC, 0xBA98, 0x7654},
{1, 2, 3}, {0xFFFE, 0xFFFF, 0xFFFD}, {0x2000, 0xC000, 0x6000},
{0xC000, 0x2000, 0x2001}, {0x00FF, 0xFF00, 0x0F0F}, {0x5555, 0xAAAA, 0xAAAA},
{0x7FFF, 0x8000, 0x8001}, {0, 1, 0},
};

static void fixes(void)
{
static const long values[] = {
0, 1, 0x7FFF, 0x8000, 0xFFFF, 0x10000, 0x10001, 0x18000, 0x7FFFFFFF, -1, -0x8000, -0x10000,
(long)0x80000000
};
static const unsigned short fracts[] = {0, 1, 0x7FFF, 0x8000, 0xFFFE, 0xFFFF};
int i;
start("Fix2SmallFract");
for (i = 0; i < sizeof values / sizeof values[0]; i++)
add(" %08lX:%04X", (unsigned long)values[i], (unsigned short)Fix2SmallFract(values[i]));
end();
start("SmallFract2Fix");
for (i = 0; i < sizeof fracts / sizeof fracts[0]; i++)
add(" %04X:%08lX", fracts[i], (unsigned long)SmallFract2Fix(fracts[i]));
end();
}

/* Each color to the other space and back, and its three numbers taken as a
* color of that space. */
#define CONVERSION(Other, Space) \
static void convert##Other(int i) \
{ \
RGBColor from, back; \
Space there; \
const unsigned short *c = colors[i]; \
unsigned short *n = (unsigned short *)&there; \
from.red = c[0]; from.green = c[1]; from.blue = c[2]; \
memset(&there, 0, sizeof there); \
memset(&back, 0, sizeof back); \
RGB2##Other(&from, &there); \
Other##2RGB(&there, &back); \
start("RGB2" #Other " %04X,%04X,%04X", c[0], c[1], c[2]); \
add(" %04X,%04X,%04X back %04X,%04X,%04X", n[0], n[1], n[2], back.red, back.green, back.blue); \
end(); \
n[0] = c[0]; n[1] = c[1]; n[2] = c[2]; \
memset(&back, 0, sizeof back); \
Other##2RGB(&there, &back); \
start(#Other "2RGB %04X,%04X,%04X", c[0], c[1], c[2]); \
add(" %04X,%04X,%04X", back.red, back.green, back.blue); \
end(); \
}

CONVERSION(CMY, CMYColor)
CONVERSION(HSL, HSLColor)
CONVERSION(HSV, HSVColor)

static void conversions(void)
{
int i, count = sizeof colors / sizeof colors[0];
fixes();
for (i = 0; i < count; i++) convertCMY(i);
for (i = 0; i < count; i++) convertHSL(i);
for (i = 0; i < count; i++) convertHSV(i);
}

int main(void)
{
static const unsigned char name[] = "\pUtilities.txt";

InitGraf(&qd.thePort);
InitFonts();
if (Create(name, 0, 'Grph', 'TEXT') != noErr) return 1;
if (FSOpen(name, 0, &out) != noErr) return 2;

randoms();
stuffs();
ptAngles();
slopes();
conversions();

start("done"); end();
FSClose(out);
FlushVol(0, 0);
return 0;
}
Loading
Loading