Code:isochromic.c: Difference between revisions

Wrapped the code in <syntaxhighlight /> (forgot to do it in the last edit)
(Created page with "<syntaxhighlight lang="c"> // Elie's isochromic image generator v0.5 // Copyright 2022, Elie Goldman Smith // License: GNU GPL V3 // To compile this code: // gcc isochromic.c -lm -lfftw3 -lreadline -lcurses -O3 -o isochromic // # takes awhile to compile, because of optimization and stb_image #include <ctype.h> #include <fftw3.h> #include <math.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <readline/readline.h> #include <...")
 
(Wrapped the code in <syntaxhighlight /> (forgot to do it in the last edit))
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
<syntaxhighlight lang="c">
<syntaxhighlight lang="c">
// Elie's isochromic image generator v0.5
// Elie's isochromic image generator v0.6
// Copyright 2022, Elie Goldman Smith
// Copyright 2022, Elie Goldman Smith
// License: GNU GPL V3
// License: GNU GPL V3
Line 64: Line 64:
  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 75:
  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 83:
  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 514:




 
 




Line 575: Line 575:
   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 713:




 
 
 
  // operator [] (array mapping)
  // operator [] (array mapping)
  if (c0=='[' && c1==']') {
  if (c0=='[' && c1==']') {
Line 757: Line 757:
   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
   rightx++;
   rightx++;
   preserveSum=1;
   preserveSum=1;
Line 789: Line 789:
   }
   }
   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 810: Line 810:
   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 853: Line 853:
   }
   }
   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 929: Line 929:




   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 956: Line 956:
     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 989: Line 989:
     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;
   }
   }