Code:isochromic.c: Difference between revisions

Started version 0.6. Changed the "preserve sums" syntax to '$' (it used to be '_').
(Added note about how I'm planning to change the syntax for "preserve sum" (i want it to be '$' instead of '_'))
(Started version 0.6. Changed the "preserve sums" syntax to '$' (it used to be '_').)
Line 1: Line 1:
<syntaxhighlight lang="c">
// Elie's isochromic image generator v0.6
// Elie's isochromic image generator v0.5
// Copyright 2022, Elie Goldman Smith
// Copyright 2022, Elie Goldman Smith
// License: GNU GPL V3
// License: GNU GPL V3
Line 64: Line 63:
  puts(" ");
  puts(" ");
  puts("Commands - quick reference:");
  puts("Commands - quick reference:");
  puts(" foo << input.png  # Load data from file");
  puts(" foo << input.png  # Load data from file (can create new variable 'foo')");
  puts(" bar := foo        # Copy data");
  puts(" bar := foo        # Copy data           (can create new variable 'bar')");
  puts(" bar += foo        # Addition");
  puts(" bar += foo        # Addition");
  puts(" bar -= foo        # Subtraction");
  puts(" bar -= foo        # Subtraction");
Line 75: Line 74:
  puts(" bar _= foo        # Fill in any blanks");
  puts(" bar _= foo        # Fill in any blanks");
  puts(" bar [] foo        # Array lookup");
  puts(" bar [] foo        # Array lookup");
  puts(" bar :: foo        # Resize image");
  puts(" bar :: foo        # Resize image (or create new blank variable 'bar')");
  puts(" bar >> output.png # Save data to file");
  puts(" bar >> output.png # Save data to file");
  puts(" -- ");
  puts(" -- ");
Line 83: Line 82:
  puts(" -- # Special functions:");
  puts(" -- # Special functions:");
  puts(" foo @@ spread    # Smooth out the overflows");
  puts(" foo @@ spread    # Smooth out the overflows");
  puts(" foo @@ stats      # Quick statistical info");  
  puts(" foo @@ stats      # Quick statistical info");
  puts(" foo @@ clear      # Delete 'foo', free up memory");
  puts(" foo @@ clear      # Delete 'foo', free up memory");
}
}
Line 514: Line 513:




 
 




Line 575: Line 574:
   return result; // done loading headerless data
   return result; // done loading headerless data
   }
   }
 
 
   // other case: image file:
   // other case: image file:
   printf("Loading image file"); fflush(stdout);
   printf("Loading image file"); fflush(stdout);
Line 713: Line 712:




 
 
 
  // operator [] (array mapping)
  // operator [] (array mapping)
  if (c0=='[' && c1==']') {
  if (c0=='[' && c1==']') {
Line 757: Line 756:
   int newWidth=0, newHeight=0, preserveSum=0;
   int newWidth=0, newHeight=0, preserveSum=0;
   char *rightx = right;
   char *rightx = right;
   if (right[0]=='_') {
   if (right[0]=='$') { // the '$' preserves pixel sums. Note that the syntax was previously '_' instead, in version 0.5
  // TODO XXX: Change the syntax. The '_' is a terrible choice for "preserve sum", because the names of variables could also contain underscores. Use '$' instead - makes far more sense. Also change this for the integer scale functions "@@ _width" and "@@ _height". Last but not least, be sure to update the wikitext of '''all''' the images generated so far!
   rightx++;
   rightx++;
   preserveSum=1;
   preserveSum=1;
Line 790: Line 788:
   }
   }
   printf("Resizing '%s'(%dx%d) to ", left, lv->width, lv->height);
   printf("Resizing '%s'(%dx%d) to ", left, lv->width, lv->height);
   if (rv) printf("fit '%s'", right);
   if (rv) printf("fit '%s'", rightx);
   printf("(%dx%d) - note: may be slow\n", newWidth, newHeight);
   printf("(%dx%d) - note: may be slow\n", newWidth, newHeight);
   if (preserveSum) printf(" + preserving the sum of pixels\n");
   if (preserveSum) printf(" + preserving the sum of pixels\n");
Line 811: Line 809:
   Var *lv[4] = {0};
   Var *lv[4] = {0};
   lv[0] = find_var(left);
   lv[0] = find_var(left);
   if (lv[0]) n=1;
   if (lv[0] && lv[0]->width && lv[0]->height) n = 1;
   else {
   else {
   // try multiple channels - for variable 'foo', look for 'foo.r', 'foo.g', 'foo.b', 'foo.a'
   // try multiple channels - for variable 'foo', look for 'foo.r', 'foo.g', 'foo.b', 'foo.a'
Line 854: Line 852:
   }
   }
   if (highs || lows) {
   if (highs || lows) {
     printf("  '%s' contains overflows (%d pixels are >1) (%d pixels are <0)\n", lv[i]->name, highs, lows);
     printf("  Note: '%s' contains overflows(%d pixels are >1)(%d pixels are <0)\n", lv[i]->name, highs, lows);
     printf("    Image will not be isochromic.\n    If you want it to be, you need to first do:\n");
     printf("    Image will not be isochromic.\n    If you want it to be, you need to first do:\n");
     printf("      %s @@ spread\n    (which would alter the contents of '%s').\n", lv[i]->name, lv[i]->name);
     printf("      %s @@ spread\n    (which would alter the contents of '%s').\n", lv[i]->name, lv[i]->name);
Line 930: Line 928:




   if (!strncasecmp(right,"width",5) || !strncasecmp(right,"_width",6)) {
   if (!strncasecmp(right,"width",5) || !strncasecmp(right,"$width",6)) {
   if (lv->width<=0 || lv->height<=0) {
   if (lv->width<=0 || lv->height<=0) {
     printf("Can't scale an empty variable\n");
     printf("Can't scale an empty variable\n");
Line 957: Line 955:
     lv->width *= scale;
     lv->width *= scale;
   }
   }
   if (right[0]=='_') printf(" + preserving the sum of pixels\n");
   if (right[0]=='$') printf(" + preserving the sum of pixels\n");
   if((right[0]=='_') != (*p=='/')) for (int i=0; i < lv->width*lv->height; i++) lv->data[i] /= scale;
   if((right[0]=='$') != (*p=='/')) for (int i=0; i < lv->width*lv->height; i++) lv->data[i] /= scale;
   return RESULT_OK;
   return RESULT_OK;
   }
   }




   if (!strncasecmp(right,"height",6) || !strncasecmp(right,"_height",7)) {
   if (!strncasecmp(right,"height",6) || !strncasecmp(right,"$height",7)) {
   if (lv->width<=0 || lv->height<=0) {
   if (lv->width<=0 || lv->height<=0) {
     printf("Can't scale an empty variable\n");
     printf("Can't scale an empty variable\n");
Line 990: Line 988:
     lv->height *= scale;
     lv->height *= scale;
   }
   }
   if (right[0]=='_') printf(" + preserving the sum of pixels\n");
   if (right[0]=='$') printf(" + preserving the sum of pixels\n");
   if((right[0]=='_') != (*p=='/')) for (int i=0; i < lv->width*lv->height; i++) lv->data[i] /= scale;
   if((right[0]=='$') != (*p=='/')) for (int i=0; i < lv->width*lv->height; i++) lv->data[i] /= scale;
   return RESULT_OK;
   return RESULT_OK;
   }
   }
Line 1,106: Line 1,104:
  return 0;
  return 0;
}
}
</syntaxhighlight>