/*** Brian Shucker 11-24-2000 This program takes an image file as input and produces out.html. When displayed, the html text looks like the image. The content of the text is the source code for a program, sans white space. The source code with white space included is left in the temporary file _temp_source.c. You can recompile that file (with gcc flags -lm -lgd) to get a program that produces the same image. Other temporary files prefixed by _temp are also produced. This program makes a call on gcc with -lm -lgd during execution, so gcc and the math and gd libraries must be available and in the proper paths. The program has been tested with RedHat Linux 6.2 only. 11-26-2000: version2 Modified to make much more compact output. 11-26-2000: version3 Equivalent to version2, but made code more compact at cost of very bad style. 12-2-2000: version4 Modified to collapse whitespace instead of removing it. Now you can cut and paste directly from a browser and get almost compilable code. The one problem is that you get newlines where the line breaks are in the html, and no newlines after preprocessor directives. The newline problem is not avoidable, but can be repaired with a text editor. 8-30-2001: version5 Modified to use JPEG instead of GIF, for compatibility with new versions of GD library ***/ #include #include void makeDupProg(FILE *out); int main(int argc, char *argv[]) { FILE *in, *out, *final; unsigned char buf; int count = 1; if (argc != 2) { printf("Usage: poster \n"); exit(1); } if((in = fopen(argv[1], "rb")) == NULL) { fprintf(stderr, "Cannot open source file %s\n", argv[1]); exit(0); } if((out = fopen("_temp_prog.c", "w")) == NULL) { fprintf(stderr, "Cannot open temp prog file\n"); exit(0); } if((final = fopen("_temp_gen.c", "w")) == NULL) { fprintf(stderr, "Cannot open output file\n"); exit(0); } fread(&buf, sizeof(char), 1, in); fprintf(out, "char data[] = \"%02X", buf); while (fread(&buf, sizeof(char), 1, in)) { fprintf(out, "%02X", buf); count++; } fprintf(out, "\";\n"); fprintf(out, " #include void convert(FILE *imageFile, FILE *textFile, FILE *outFile); int myMain(int argc, char *argv[]) { FILE *myout; FILE *image, *text, *html; int i; if((myout = fopen(\"_temp_image.jpeg\", \"wb\")) == NULL) exit(0); if((text = fopen(\"_temp_source.c\", \"r\")) == NULL) exit(0); if((html = fopen(\"out.html\", \"w\")) == NULL) exit(0); "); fprintf(out, " for(i=0; i<%d; i++) { unsigned char tmp; char tmp2[] = {data[2*i], data[2*i+1], 0}; tmp = strtol(tmp2, 0, 16); fwrite(&tmp, 1, 1, myout); } ", count); fprintf(out, " fclose(myout); if((image = fopen(\"_temp_image.jpeg\", \"rb\")) == NULL) exit(0); convert(image, text, html); } "); fprintf(out, " #include #include #define AF 1.8 void convert(FILE *imageFile, FILE *textFile, FILE *outFile) { gdImagePtr sourceIm, im; int sourceW, sourceH, w, h; int i,j; int charCount = 0; char chr; int inSpace = 0; int prevR, prevG, prevB; char pch = EOF; sourceIm = gdImageCreateFromJpeg(imageFile); fclose(imageFile); sourceW = gdImageSX(sourceIm); sourceH = gdImageSY(sourceIm); while((chr = getc(textFile)) != EOF) { if (chr != ' ' && chr != '\\t' && chr != '\\n') { charCount++; inSpace = 0; } else { if(!inSpace) charCount++; inSpace = 1; } } rewind(textFile); w = ceil(sqrt(AF*charCount*((double)sourceW/sourceH))); h = ceil((double)charCount / w); im = gdImageCreate(w, h); gdImageCopyResized(im, sourceIm, 0, 0, 0, 0, w, h, sourceW, sourceH); fprintf(outFile, \"\\n\\n\"); fprintf(outFile, \"\\n\"); fprintf(outFile, \"\\n\"); fprintf(outFile, \"\\n\"); prevR = 0; prevG = 0; prevB = 0; for(j=0; jred[c] == prevR && im->green[c] == prevG && im->blue[c] == prevB && ch != '<' && ch != '>' && pch != '<' && pch != '>') fprintf(outFile, \"%%c\", ch); else fprintf(outFile, \"%%c\", im->red[c], im->green[c], im->blue[c], ch); prevR = im->red[c]; prevG = im->green[c]; prevB = im->blue[c]; pch = ch; } fprintf(outFile, \"
\\n\"); } fprintf(outFile, \"
\\n\\n\"); fclose(textFile); fclose(outFile); gdImageDestroy(sourceIm); gdImageDestroy(im); } "); fclose(in); fclose(out); makeDupProg(final); system("gcc -o _temp_exe _temp_gen.c -lm -lgd\n"); system("_temp_exe\n"); } #define TRUE 1 #define FALSE 0 #define HEADER_SIZE 1000 char header[HEADER_SIZE]; void makeDupProg(FILE *out) { int i; char ch; FILE *in; if((in = fopen("_temp_prog.c", "r")) == NULL) exit(0); for(i=0; i int myMain(int argc, char *argv[]); int main(int argc, char *argv[]){ int i; FILE *out; if((out = fopen(\"_temp_source.c\", \"w\")) == NULL) exit(0); fprintf(out, \"char __s[] = \\\"\"); for(i=0; __s[i]; i++) { if(__s[i] == '\\\\') fprintf(out, \"\\\\\\\\\"); else if (__s[i] == '\\\"') fprintf(out, \"\\\\\\\"\"); else fprintf(out, \"%c\", __s[i]); } fprintf(out, \"%s\", __s); argv[1] = argv[0]; fclose(out); myMain(argc - 1, &argv[1]); } "); fprintf(out, "char __s[] = \""); fprintf(out, "\\\";\\n\\n"); for(i=0; i