#include #include #include #include #include "bmplib.h" int step_number_specified=0; int step_number=0; int picture_specified=0; char * picture_name=""; void read_query_string(void) { char *qs=getenv("QUERY_STRING"); if (qs==NULL) qs=""; if (strncmp(qs, "step", 4)==0) { step_number_specified=1; step_number=atol(qs+4); } else if (strncmp(qs, "picture", 7)==0) { picture_specified=1; picture_name="spirograph.bmp"; } } double big_teeth=1, little_teeth=1, num_steps=1, smoothness=1, pen_dist=1, side=1; void read_inputs(void) { int stage=0, pos=0; char name[100], value[100]; while (1) { int c=getchar(); if (c==EOF || c=='&') { if (stage==1) { if (strcmp(name, "big")==0) big_teeth=atof(value); else if (strcmp(name, "little")==0) little_teeth=atof(value); else if (strcmp(name, "steps")==0) num_steps=atof(value); else if (strcmp(name, "dist")==0) pen_dist=atof(value); else if (strcmp(name, "smooth")==0) smoothness=atof(value); else if (strcmp(name, "inout")==0) { if (strcasecmp(value, "inside")==0) side=-1; else side=1; } } stage=0; pos=0; name[pos]=0; if (c==EOF) break; } else if (c=='=') { stage=1; pos=0; value[pos]=0; } else if (stage==0 && pos<99) { name[pos]=c; pos+=1; name[pos]=0; } else if (stage==1 && pos<99) { value[pos]=c; pos+=1; value[pos]=0; } } } void main(void) { read_query_string(); read_inputs(); if (picture_specified) { int f=open(picture_name, O_RDONLY); if (f<0) { printf("Content-Type: text/html\r\n"); printf("\r\n"); printf("CGI BMP demo error\r\n"); printf("

CGI BMP demo error

\r\n"); printf("picture \"%s\" not found
\r\n", picture_name); printf("\r\n"); } else { char * s = "Content-type: image/bmp\r\n\r\n"; write(1, s, strlen(s)); char buff[512]; while (1) { int n=read(f, buff, 512); if (n<=0) break; write(1, buff, n); } close(f); } } else if (step_number==1 || !step_number_specified) { printf("Content-Type: text/html\r\n"); printf("\r\n"); printf("CGI BMP demo stage one\r\n"); printf("

CGI BMP demo stage one

\r\n"); printf("This program will draw a spirograph picture
\r\n"); printf("
\r\n"); printf("\r\n"); printf("
How many teeth on the big wheel? \r\n"); printf("
How many teeth on the little wheel? \r\n"); printf("
How many steps should the simulation run? \r\n"); printf("
Distance from center of little wheel to pen? \r\n"); printf("
How smooth should the picture be? \r\n"); printf("
Inside pattern
\r\n"); printf(" Outside pattern\r\n"); printf("
\r\n"); printf("
\r\n"); printf("Note: A small value for smoothness produces a smoother picture but takes longer to\r\n"); printf("create. Smoothness is actually an angle: The little wheel travels around the big wheel by that\r\n"); printf("angle in each of the simulation steps.
\r\n"); printf("


\r\n"); printf("\r\n"); } else if (step_number==2) { printf("Content-Type: text/html\r\n"); printf("\r\n"); printf("CGI BMP demo stage two\r\n"); printf("

CGI BMP demo stage two

\r\n"); printf("teeth on big wheel = %f
\r\n", big_teeth); printf("teeth on little wheel = %f
\r\n", little_teeth); printf("pen distance = %f
\r\n", pen_dist); printf("number of steps = %f
\r\n", num_steps); printf("inside or outside = %s
\r\n", side<0 ? "inside" : "outside"); printf("smoothness = %f
\r\n", smoothness); const int picwidth=400, picheight=400; const int midx=picwidth/2, midy=picheight/2; const double sz=big_teeth+side*little_teeth+pen_dist; const double scale=(picheight/2-1)/sz; big_teeth*=scale; little_teeth*=scale; Picture p(400, 400); int red=MakeRGB(255, 0, 0), green=MakeRGB(0, 255, 0), blue=MakeRGB(0, 0, 255), white=MakeRGB(255, 255, 255), black=MakeRGB(0, 0, 0); if (little_teeth==0.0) little_teeth=1.0; double ang1=0.0, ang2=0.0, ratio=big_teeth/little_teeth, oldx=midx, oldy=midy+big_teeth+side*little_teeth+pen_dist; for (int i=0; i1.5) { p.DrawLine((int)(oldx+0.5), (int)(oldy+0.5), (int)(newx+0.5), (int)(newy+0.5)); oldx=newx; oldy=newy; } ang1+=smoothness; ang2+=smoothness*ratio; } p.SaveAsBMP("spirograph.bmp"); printf("\r\n"); printf("\r\n"); printf("\r\n"); } else { printf("Content-Type: text/plain\r\n"); printf("\r\n"); printf("ERROR IN USAGE\r\n"); if (!step_number_specified) printf("No \"?stepN\" at end of URL\r\n"); else printf("Invalid step %d specified in URL\r\n", step_number); printf("\r\n"); } }