Guest User

Untitled

a guest
Jul 19th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5.  
  6. #include <gd.h>
  7.  
  8. struct basicdigit {
  9.     long digit;
  10.     long size;
  11.     basicdigit of a list
  12.     short init;
  13.     struct basicdigit* next;
  14.     struct basicdigit* last;
  15. };
  16.  
  17.  
  18.  
  19. struct basicdigit* basemaster (const unsigned long num, const int from, const int to);
  20.  
  21.  
  22. struct basicdigit* zeropad (struct basicdigit*, int l);
  23.  
  24. void nuke (struct basicdigit*);
  25.  
  26. int main (int argc, const char * argv[]) {
  27.  
  28.     int w, h, c, s, countonly;
  29.    
  30.     if (argv[1]){
  31.         w = atoi(argv[1]);
  32.     }
  33.     else {
  34.         fprintf(stderr, "Cannot do anything w/o width\n");
  35.         exit(1);
  36.     };
  37.    
  38.     if (argv[2]){
  39.         h = atoi(argv[2]);
  40.     }
  41.     else {
  42.         fprintf(stderr, "Cannot do anything w/o height\n");
  43.         exit(1);
  44.     };
  45.    
  46.     if (argv[3]){
  47.         c = atoi(argv[3]);
  48.     }
  49.     else {
  50.         fprintf(stderr, "Cannot do anything w/o colors\n");
  51.         exit(1);
  52.     };
  53.    
  54.     if (argv[4]){
  55.         s = atoi(argv[4]);
  56.     }
  57.     else {
  58.         s = 1;
  59.     };
  60.    
  61.     if (argv[5]){
  62.         countonly = atoi(argv[5]);
  63.     }
  64.     else {
  65.         countonly = 0;
  66.     };
  67.  
  68.     if (c > 7 && ! countonly){
  69.         fprintf(stderr, "Cannot support color depths greater than 7");
  70.         exit(1);
  71.     };
  72.    
  73.  
  74.     long combos = (long) pow(c, w * h);
  75.  
  76.     long unscaled_each_area = w * h;
  77.    
  78.     long each_area = unscaled_each_area * s * s;
  79.    
  80.     int perline = ceil(sqrt(combos));
  81.  
  82.     long width  = perline * (w * s + s) - s;
  83.     long height = perline * (h * s + s) - s;
  84.    
  85.     fprintf(stderr, "For a %d x %d image @ %d colors, there are %d combinations\n",
  86.                 w, h, c, combos);
  87.    
  88.     if (countonly) {
  89.         return(0);
  90.     };
  91.  
  92.     fprintf(stderr, "Your output will be %d x %d\n", width, height);
  93.  
  94.     gdImagePtr im = gdImageCreate(width, height);
  95.  
  96.     int red     = gdImageColorAllocate(im, 255,0,0);
  97.     int green   = gdImageColorAllocate(im, 0,255,0);
  98.     int blue    = gdImageColorAllocate(im, 0,0,255);
  99.     int cyan    = gdImageColorAllocate(im, 0,255,255);
  100.     int magenta = gdImageColorAllocate(im, 255,0,255);
  101.     int yellow  = gdImageColorAllocate(im, 255,255,0);
  102.     int black   = gdImageColorAllocate(im, 0,0,0);
  103.     int white   = gdImageColorAllocate(im, 255,255,255);
  104.  
  105.     int colors[] = {white, black, green, blue, cyan, magenta, yellow};
  106.    
  107.     int xpad = 0;
  108.     int ypad = 0;
  109.     int num = 0;
  110.  
  111.     for (num = 0; num < combos; num++) {
  112.    
  113.         struct basicdigit* nums = zeropad(basemaster(num, 10, c), unscaled_each_area);
  114.    
  115.         int pix = 0;
  116.         int x = xpad;
  117.         int y = ypad;
  118.        
  119.         while (1) {
  120.             int col = colors[nums->digit];
  121.            
  122.             gdImageFilledRectangle(im, x, y, x + s - 1, y + s - 1, col);
  123.            
  124.             y += s;
  125.            
  126.             if (!(++pix % h)){
  127.                 xpad += s;
  128.                 x = xpad;
  129.                 y = ypad;
  130.             };
  131.            
  132.             if (nums->next){
  133.                 nums = nums->next;
  134.             }
  135.             else {
  136.                 break;
  137.             };
  138.         };  // end while
  139.        
  140.         xpad += s;
  141.        
  142.        
  143.         if (!((num + 1) % perline)){
  144.             ypad += h * s + s;
  145.             xpad = 0;
  146.         };
  147.        
  148.         nuke(nums);
  149.        
  150.     }; 
  151.     gdImagePng(im, stdout);
  152.    
  153.     gdImageDestroy(im);
  154.    
  155.     return(0);
  156.    
  157. }
  158.  
  159.  
  160.  
  161. struct basicdigit* basemaster (const unsigned long num, const int from, const int to) {
  162.  
  163.     int power = 0;
  164.     unsigned long base10 = 0;
  165.     int digit = 1;
  166.  
  167.     if (from != 10) {
  168.         for (digit = 1; digit < num; digit *= 10){
  169.             base10 += (long) ((num % (digit * 10) - num % digit) / digit) * pow(from, power++);
  170.         };
  171.     }
  172.     else {
  173.         base10 = num;
  174.     };
  175.    
  176.     struct basicdigit* newnum = malloc(sizeof(struct basicdigit));
  177.     newnum->init = 0;
  178.     newnum->size = 0;
  179.     newnum->digit = 0;
  180.     newnum->next = NULL;
  181.     newnum->last = NULL;
  182.  
  183.     struct basicdigit* retvalue = newnum;
  184.  
  185.     power = 1;
  186.    
  187.     do {
  188.         power++;
  189.     } while (pow(to, power + 1) <= base10);
  190.  
  191.     while (power >= 0){
  192.  
  193.         if (newnum->init) {
  194.             struct basicdigit* next = malloc(sizeof(struct basicdigit));
  195.             next->init = 1;
  196.             next->digit = (long) floor(base10 / pow(to, power));
  197.             next->next = NULL;
  198.             next->last = newnum;
  199.             newnum->next = next;
  200.             newnum = next;
  201.             retvalue->size++;
  202.         }
  203.         else {
  204.             long digit = (long) floor(base10 / pow(to, power));
  205.  
  206.             if (digit) {
  207.                 retvalue->size = 1;
  208.                 newnum->init = 1;
  209.                 newnum->digit = digit;
  210.             };
  211.         };
  212.  
  213.         base10 = (int) base10 % (int) pow(to, power--);
  214.  
  215.     };
  216.  
  217.     if (! retvalue->init){
  218.         retvalue->size = 1;
  219.         retvalue->init = 1;
  220.         retvalue->digit = 0;
  221.     };
  222.    
  223.     return retvalue;
  224.  
  225. };
  226.  
  227.  
  228. struct basicdigit* zeropad (struct basicdigit* b, int l) {
  229.  
  230.     if (l && ! b->init) {
  231.         b->init = 1;
  232.         b->size = 1;
  233.         b->digit = 0;
  234.     }
  235.     else if (! l && ! b->init) {
  236.         return b;
  237.     };
  238.  
  239.     if (b->size < l) {
  240.         int diff = l - b->size;
  241.  
  242.         while (diff > 0) {
  243.             struct basicdigit* zero = malloc(sizeof(struct basicdigit));
  244.             zero->digit = 0;
  245.             zero->init = 1;
  246.             zero->last = NULL;
  247.             zero->next = b;
  248.             b->last = zero;
  249.             b = zero;
  250.             diff--;
  251.         };
  252.     };
  253.    
  254.     return b;
  255.  
  256. };
  257.  
  258. void nuke (struct basicdigit* trash) {
  259.     if (trash->next){
  260.         trash->next->last = NULL;
  261.         nuke(trash->next);
  262.         trash->next = NULL;
  263.     };
  264.    
  265.     if (trash->last){
  266.         trash->last->next = NULL;
  267.         nuke(trash->last);
  268.         trash->last = NULL;
  269.     };
  270.    
  271.     free(trash);
  272. };
Add Comment
Please, Sign In to add comment