Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <math.h>
- #include <gd.h>
- struct basicdigit {
- long digit;
- long size;
- basicdigit of a list
- short init;
- struct basicdigit* next;
- struct basicdigit* last;
- };
- struct basicdigit* basemaster (const unsigned long num, const int from, const int to);
- struct basicdigit* zeropad (struct basicdigit*, int l);
- void nuke (struct basicdigit*);
- int main (int argc, const char * argv[]) {
- int w, h, c, s, countonly;
- if (argv[1]){
- w = atoi(argv[1]);
- }
- else {
- fprintf(stderr, "Cannot do anything w/o width\n");
- exit(1);
- };
- if (argv[2]){
- h = atoi(argv[2]);
- }
- else {
- fprintf(stderr, "Cannot do anything w/o height\n");
- exit(1);
- };
- if (argv[3]){
- c = atoi(argv[3]);
- }
- else {
- fprintf(stderr, "Cannot do anything w/o colors\n");
- exit(1);
- };
- if (argv[4]){
- s = atoi(argv[4]);
- }
- else {
- s = 1;
- };
- if (argv[5]){
- countonly = atoi(argv[5]);
- }
- else {
- countonly = 0;
- };
- if (c > 7 && ! countonly){
- fprintf(stderr, "Cannot support color depths greater than 7");
- exit(1);
- };
- long combos = (long) pow(c, w * h);
- long unscaled_each_area = w * h;
- long each_area = unscaled_each_area * s * s;
- int perline = ceil(sqrt(combos));
- long width = perline * (w * s + s) - s;
- long height = perline * (h * s + s) - s;
- fprintf(stderr, "For a %d x %d image @ %d colors, there are %d combinations\n",
- w, h, c, combos);
- if (countonly) {
- return(0);
- };
- fprintf(stderr, "Your output will be %d x %d\n", width, height);
- gdImagePtr im = gdImageCreate(width, height);
- int red = gdImageColorAllocate(im, 255,0,0);
- int green = gdImageColorAllocate(im, 0,255,0);
- int blue = gdImageColorAllocate(im, 0,0,255);
- int cyan = gdImageColorAllocate(im, 0,255,255);
- int magenta = gdImageColorAllocate(im, 255,0,255);
- int yellow = gdImageColorAllocate(im, 255,255,0);
- int black = gdImageColorAllocate(im, 0,0,0);
- int white = gdImageColorAllocate(im, 255,255,255);
- int colors[] = {white, black, green, blue, cyan, magenta, yellow};
- int xpad = 0;
- int ypad = 0;
- int num = 0;
- for (num = 0; num < combos; num++) {
- struct basicdigit* nums = zeropad(basemaster(num, 10, c), unscaled_each_area);
- int pix = 0;
- int x = xpad;
- int y = ypad;
- while (1) {
- int col = colors[nums->digit];
- gdImageFilledRectangle(im, x, y, x + s - 1, y + s - 1, col);
- y += s;
- if (!(++pix % h)){
- xpad += s;
- x = xpad;
- y = ypad;
- };
- if (nums->next){
- nums = nums->next;
- }
- else {
- break;
- };
- }; // end while
- xpad += s;
- if (!((num + 1) % perline)){
- ypad += h * s + s;
- xpad = 0;
- };
- nuke(nums);
- };
- gdImagePng(im, stdout);
- gdImageDestroy(im);
- return(0);
- }
- struct basicdigit* basemaster (const unsigned long num, const int from, const int to) {
- int power = 0;
- unsigned long base10 = 0;
- int digit = 1;
- if (from != 10) {
- for (digit = 1; digit < num; digit *= 10){
- base10 += (long) ((num % (digit * 10) - num % digit) / digit) * pow(from, power++);
- };
- }
- else {
- base10 = num;
- };
- struct basicdigit* newnum = malloc(sizeof(struct basicdigit));
- newnum->init = 0;
- newnum->size = 0;
- newnum->digit = 0;
- newnum->next = NULL;
- newnum->last = NULL;
- struct basicdigit* retvalue = newnum;
- power = 1;
- do {
- power++;
- } while (pow(to, power + 1) <= base10);
- while (power >= 0){
- if (newnum->init) {
- struct basicdigit* next = malloc(sizeof(struct basicdigit));
- next->init = 1;
- next->digit = (long) floor(base10 / pow(to, power));
- next->next = NULL;
- next->last = newnum;
- newnum->next = next;
- newnum = next;
- retvalue->size++;
- }
- else {
- long digit = (long) floor(base10 / pow(to, power));
- if (digit) {
- retvalue->size = 1;
- newnum->init = 1;
- newnum->digit = digit;
- };
- };
- base10 = (int) base10 % (int) pow(to, power--);
- };
- if (! retvalue->init){
- retvalue->size = 1;
- retvalue->init = 1;
- retvalue->digit = 0;
- };
- return retvalue;
- };
- struct basicdigit* zeropad (struct basicdigit* b, int l) {
- if (l && ! b->init) {
- b->init = 1;
- b->size = 1;
- b->digit = 0;
- }
- else if (! l && ! b->init) {
- return b;
- };
- if (b->size < l) {
- int diff = l - b->size;
- while (diff > 0) {
- struct basicdigit* zero = malloc(sizeof(struct basicdigit));
- zero->digit = 0;
- zero->init = 1;
- zero->last = NULL;
- zero->next = b;
- b->last = zero;
- b = zero;
- diff--;
- };
- };
- return b;
- };
- void nuke (struct basicdigit* trash) {
- if (trash->next){
- trash->next->last = NULL;
- nuke(trash->next);
- trash->next = NULL;
- };
- if (trash->last){
- trash->last->next = NULL;
- nuke(trash->last);
- trash->last = NULL;
- };
- free(trash);
- };
Add Comment
Please, Sign In to add comment