Skip to content

Instantly share code, notes, and snippets.

@radiskis
Last active March 16, 2018 13:22
Show Gist options
  • Save radiskis/cac2a996f9bbb9bf31ab78b46e213cd6 to your computer and use it in GitHub Desktop.
Save radiskis/cac2a996f9bbb9bf31ab78b46e213cd6 to your computer and use it in GitHub Desktop.
Archived from Ken Turkowski Open Source Repository http://www.worldserver.com/turk/opensource/
/* Copyright (C) 1978-1995 Ken Turkowski. <turk@computer.org>
*
* All rights reserved.
*
* Warranty Information
* Even though I have reviewed this software, I make no warranty
* or representation, either express or implied, with respect to this
* software, its quality, accuracy, merchantability, or fitness for a
* particular purpose. As a result, this software is provided "as is,"
* and you, its user, are assuming the entire risk as to its quality
* and accuracy.
*
* This code may be used and freely distributed as long as it includes
* this copyright notice and the above warranty information.
*/
#include <stdio.h>
#include <stdlib.h>
#include <fp.h>
#define PI 3.1415926535898
unsigned char sineTab[256];
/*******************************************************************************
* ZonePlate
*******************************************************************************/
static void
ZonePlate(FILE *fd, long width, long height, long scale)
{
long cX, cY;
long i, j;
long x, y;
long d;
cX = width / 2;
cY = height / 2;
for (i = height, y = -cY; i--; y++) {
for (j = width, x = -cX; j--; x++) {
d = ((x * x + y * y) * scale) >> 8;
putc(sineTab[d & 0xFF], fd);
}
}
}
/*******************************************************************************
* MakeSineTab
*******************************************************************************/
static void
MakeSineTab(void) {
long i;
for (i = 0; i < 256; i++) {
sineTab[i] = 127.5 * sin(PI * (i - 127.5) / 127.5) + 127.5;
}
}
/*******************************************************************************
* main
*******************************************************************************/
int
main(int argc, char **argv)
{
long width = 0;
long height = 0;
long scale = 0;
char *outFile = NULL;
FILE *fd;
for (argc--, argv++; argc-- > 0; argv++) {
if (argv[0][0] == '-') {
switch (argv[0][1]) {
case 'w':
argc--; argv++;
width = atoi(argv[0]);
break;
case 'h':
argc--; argv++;
height = atoi(argv[0]);
break;
case 's':
argc--; argv++;
scale = atoi(argv[0]);
break;
case 'o':
argc--; argv++;
outFile = argv[0];
break;
}
}
else {
}
}
if (width == 0 || height == 0 || scale == 0 || outFile == NULL) {
printf("Usage: ZonePlate -w <width> -h <height> -s <scale> -o <outFile>\n");
exit(1);
}
if ((fd = fopen(outFile, "wb")) == NULL) {
printf("Can't open \"%s\"\n", outFile);
exit(2);
}
MakeSineTab();
ZonePlate(fd, width, height, scale);
fclose(fd);
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment