From d3c796386f000d994eca777953210d2545868815 Mon Sep 17 00:00:00 2001 From: Cem Date: Thu, 18 Jun 2026 18:07:03 +0200 Subject: [PATCH] added parsing, isn't functional yet. there are some tests to be deleted later --- Implementierung/main.c | 138 +++++++++++++++++++++++++++++++++++++- Implementierung/tricorn.c | 3 +- 2 files changed, 138 insertions(+), 3 deletions(-) diff --git a/Implementierung/main.c b/Implementierung/main.c index 98c0e34..dd0eee1 100644 --- a/Implementierung/main.c +++ b/Implementierung/main.c @@ -2,11 +2,147 @@ #include #include #include +#include // for getopt +#include // for getopt_long +#include // for strcpy +#include "tricorn.h" void save_bmp(const char *filename, unsigned char *img, size_t width, ssize_t height); -int main(int argc, char const *argv[]) +int main(int argc, char** argv) { + int opt_long; + // will be the returned optional character, + // example: ./main -c -> opt_long here will be the optional 'c' + + const char* optstring = "V:B:s:d:n:r:o:Cht" ; + // valid options that can be given to the programme. options with ":" after them expect an argument, which we'll later store in + // example: B: -> optional B expects an argument after it, and is valid -> ./main -B 67 (optarg is now "67") + // example: h -> optional h doesn't expect an argument -> ./main -h + + // optind -> keeps track of the next index. initialised as 1 in the beginning. calling opt_long updates it every time + + // optarg -> is a charptr. stores the arguments given to the optionals + + static struct option long_options[] = { + {"color",0,0,'C'}, + {"help",0,0,'h'}, + {"test",0,0,'t'}, + {0,0,0,0} + }; + // these are the long optionals, which we can call with double "--". "--help" or "--test". they are converted to the optionals "h" and "t" respectively + // first argument is "name" -> name of the long. "--" + // second is "has_arg" -> 0 if no arg, 1 if arg, 2 if optional arg + // third is "*flag" -> sets a flag so that "--help" simply returns "h" and does nothing else + // fourth is "val" -> what to return when "--help" is given, returns "h" + + + + int version_to_use = 0; + unsigned amount_function_called; + unsigned n; + float starting_point_real; + float starting_point_imaginary; + size_t width; + ssize_t height; + float step_amount; + char* output_data; + _Bool colored = 0; + + + opt_long = getopt_long(argc,argv,optstring, long_options, NULL); + // first four arguments are self-explanatory, fifth argument is the index of which long_option we return in the struct "long_options", but it's irrelevant so we set it to NULL + while (opt_long != -1) + { + switch (opt_long) + { + case 'V': // done, function needed + version_to_use = strtol(optarg,NULL,10); + printf("version is %d\n", version_to_use); + // Die Implementierung, die verwendet werden soll. Hierbei soll mit + // -V 0 Ihre Hauptimplementierung verwendet werden. Wenn diese Option nicht + // gesetzt wird, soll ebenfalls die Hauptimplementierung ausgeführt werden + break; + + case 'B': // done, function needed + // wird die Laufzeit der angegebenen Implementierung gemessen und ausgegeben (do this later) + amount_function_called = strtoul(optarg,NULL,10); + printf("amount function called is %d\n", amount_function_called); + // Das Argument dieser Option gibt die Anzahl an Wiederholungen des Funktionsaufrufs an, z. B.-B 5 . + break; + + case 's': + char* imaginary_string; + starting_point_real = strtof(optarg,&imaginary_string); // split into two + starting_point_imaginary = strtof(imaginary_string + 1,NULL); // this is horrible + printf("real %d, img %d\n", starting_point_real,starting_point_imaginary); + // s , — Startpunkt der Berechnung + break; + + case 'd': + char* height_string; + width = strtol(optarg,&height_string,10); + height = strtol(height_string + 1,NULL,10); + printf("width %d, height %d\n", width,height); + // -d , —Breite und Höhe des Bildausschnitts + break; + + case 'n': + n = strtol(optarg,NULL,10); + printf("n:%d\n",n); + // -n —MaximaleAnzahlanIterationen pro Bildpixel + break; + + case 'r': + step_amount = strtof(optarg,NULL); + printf("r:%d\n", step_amount); + // r —Schrittweite pro Bildpixel + break; + + case 'o': + output_data = malloc(strlen(optarg) + 1); + strcpy(output_data,optarg); + printf("%s\n", output_data); // test + // -o —Ausgabedatei + break; + + case 'C': + colored = 1; + printf("colored:%d\n",colored); + // C|--color —Farbausgabe + break; + + case 'h': + // print help message + // Eine Beschreibung aller Optionen des Programms und Verwendungsbeispiele werden ausgegeben und das Programm danach beendet. + // Die Beschreibung soll mit "Help Message" anfangen. + break; + + case 't': + + // do testing + // Führt eine vordefinierte Testsuite aus, welche sämtliche von Ihnen + // verwendete Benchmarking-Szenarien gesammelt durchläuft. Das Programm gibt + // die jeweiligen Parameter, eine kurze Beschreibungen sowie die Ergebnisse in einer + // übersichtlichen und gut lesbaren Form aus und terminiert anschließend. + break; + + default: // case '?' -> when the argument is not valid + // print error msg + break; + } + + opt_long = getopt_long(argc,argv,optstring, long_options, NULL); + } + + float complex complexA = (starting_point_real + starting_point_imaginary*I); + char tempArr[width*height*3]; + printf("yashar\n"); + tricorn(complexA,width,height,step_amount,n,colored,tempArr); + printf("yashar\n"); + save_bmp(output_data,tempArr,width,height); + + free(optarg); // return 0; } diff --git a/Implementierung/tricorn.c b/Implementierung/tricorn.c index 9ead146..cdef16a 100644 --- a/Implementierung/tricorn.c +++ b/Implementierung/tricorn.c @@ -3,7 +3,7 @@ static const float convergenceLimit = 2.0f; unsigned converge(float complex position,unsigned n); -void tricorn(float complex start, size_t width, ssize_t height, float res, unsigned n, _Bool color, unsigned char* img){ +void tricorn(float complex start, size_t width, ssize_t height, float res, unsigned n, _Bool color, unsigned char* img){ height = height < 0 ? height*-1 : height; @@ -12,7 +12,6 @@ void tricorn(float complex start, size_t width, ssize_t height, float res, unsi unsigned value = converge(start + (i*res + I*j*res),n); size_t position = (i*width +j) *3; - if(!color){ img[position] = value; img[position + 1] = value;