So one of my assignments for my C class involves creating a struct and populating it. Of course, I did this over a month ago and needed a challenge. One thing that I was trying to do is create a CSV of 802 Pokemon whose name, attack, special attack, defense, special defense, speed, HP, primary type, and secondary type are all then loaded into an array of structs. I tried using fscanf for this, but couldn't figure out how to tokenize it. I couldn't figure out how to use strtok either, so I decided today was the day I was going to figure out strtok.
Now I didn't quite write my Pokemon code just yet. That particular code I want to be able to run averages based on type, classify a Pokemon as pseudolegendary based on the sum of its base stats, etc. But this struct project acted as a proof of concept of the reading a CSV into a struct array.
The CSV is the following set of values.
Name,Developer,1,Genre1,Genre2,Perspective,1,1,DevEngine,
Tetris,Alexey Pajitnov,1984,Puzzle,Strategy,Static,1,1,Various,
Beat Saber,Beat Games,2018,Rhythm,Null,VR,0,0,Unity,
Portal,Valve,2011,Puzzle,Platformer,FPV,0,0,Source,
Portal 2,Valve,2007,Puzzle,Platformer,FPV,0,0,Source,
Fallout: New Vegas,Obsidian,2010,FPS,RPG,FPV,0,0,Gamebryo,
140,Carlsen Games,2013,Platformer,Rhythm,Sidescroller,0,0,Unity,
Antichamber,Demruth,2013,Puzzle,Platformer,FPV,0,0,Unreal,
The Stanley Parable,Galactic Cafe,2013,Interactive,Null,FPV,0,0,Source,
Transistor,Supergiant Games,2014,RPG,Turn-based,TPV,0,0,Custom,
VVVVVV,Terry Cavanagh,2010,Platformer,Null,Sidescroller,0,0,Flash,
Paper Mario: The Thousand Year Door,Nintendo,2004,RPG,Turn-based,TPV,0,0,custom,
Luigi's Mansion,Nintendo,2001,RPG,Null,TPV,0,0,custom,
The first line has the names of the categories except where there were integers, just because I didn't want to risk there being an error thrown if a string were attempted to be read into an integer (although I'd imagine that'd just read its ASCII value).
The game list isn't ordered (except for the first 4, where they're my top favorite games of all time), but they're all my faves list.
So I explain in the following video how I implement strtok to tokenize my CSV and populate an array of structs of type Game.
My actual code is as follows. It requires that you have the CSV saved in the same directory with the title "favegames.csv" by pasting it into Notepad, and when you Save As, set it to "All Files" instead of just .txt, and just save it as favegames.csv.
Save the struct as structdef.h, or remove the include statement and paste it straight into the main C file.
Here's my tokenizer into an array of structs!
#include <stdio.h>#include <stdlib.h>#include <string.h>#include "structdef.h"intj=1;intmain(){FILE*myFile;myFile=fopen("favegames.csv","r");if(myFile==NULL){printf("File did not successfully open. \n");}rewind(myFile);charmyLine[200];fgets(myLine,1024,myFile);char*delimiter=",";inti=0;for(charc=getc(myFile);c!=EOF;c=getc(myFile)){if(c=='\n'){j++;}}Gamegames[j+1];rewind(myFile);while(fgets(myLine,200,(FILE*)myFile)!=NULL){strcpy(games[i].name,strtok(myLine,delimiter));strcpy(games[i].developer,strtok(NULL,delimiter));games[i].release=atoi(strtok(NULL,delimiter));strcpy(games[i].genre1,strtok(NULL,delimiter));strcpy(games[i].genre2,strtok(NULL,delimiter));strcpy(games[i].perspective,strtok(NULL,delimiter));games[i].multiplayer=atoi(strtok(NULL,delimiter));games[i].online=atoi(strtok(NULL,delimiter));strcpy(games[i].devEngine,strtok(NULL,delimiter));i++;}fclose(myFile);FILE*binStruct=NULL;binStruct=fopen("binStruct.bin","wb");if(binStruct==NULL){printf("Couldn't open binStruct.\n");}fwrite(games,sizeof(Game),j,binStruct);fclose(binStruct);searchStruct(games);}voidsearchStruct(Gamegames[j]){chargameName[40];printf("Enter a video game: ");fgets(gameName,40,stdin);intlen=strlen(gameName);if(gameName[len-1]=='\n'){gameName[len-1]=0;}inti=0;for(inta=0;a<=j;a++){if(strcmp(gameName,games[a].name)==0){break;}else{if(a==j){printf("Invalid entry.");return;}}i++;}printf("%s was made in %d by %s.\n",games[i].name,games[i].release,games[i].developer);if(strcmp(games[i].genre2,"Null")==0){printf("It is a %s game.\n",games[i].genre1);}else{printf("It is a %s and %s game.\n",games[i].genre1,games[i].genre2);}if(strcmp(games[i].devEngine,"custom")==0){printf("It was made using a custom-built game engine.\n");}else{printf("It was made using the %s game engine.\n",games[i].devEngine);}printf("It has a %s type of perspective.\n",games[i].perspective);if(games[i].multiplayer==1){printf("It has multiplayer capabilities and ");}else{printf("It does not have multiplayer capabilities and ");}if(games[i].online==1){printf("has online capabilities.\n\n");}else{printf("does not have online capabilities.\n\n");}}
About a week ago I decided to toy around with Unity. I didn't upload anything to my blog because I did it on the downstairs computer. This is the Unity Roll-A-Ball project. Now that PC doesn't have a microphone so I couldn't record a code explanation but I did record a quick video demonstrating my project.
Here's just a quick project I did on my phone when I was bored. The video contains the source code, but it's a bit of a pain to get Processing code off of my phone, so I won't be pasting it here.
So my teacher wrote code that essentially read a .pgm graymap file and would write it back to a file. We had to define the matrix that held it, and wrote code that processed it. Me being me I went through all the options and did all of the options because I thought it'd be an interesting project that'd help my coding skills, and more than that, understanding what the actual code behind certain filters and processes in Photoshop are.
I wrote code to invert, threshold, change luminosity, rotate, reflect, frame, box blur, Gaussian blur, sharpen, and edge detect an image.
I tried to write code to change the contrast but I couldn't manage to do it in a reliable manner.
The first section of the code is all my teacher's work, except where I define the arrays, put a loop on the theImageArrayDup to duplicate the array, and call my imageProcessing code. Aside from that the rest of it is my code.
Here's Default Irv (my professor's kitty), for comparison
Convolution UI
Convolution is essentially the idea of taking one pixel, and manipulating it based on the surrounding pixels. You use a matrix to manipulate the pixel based on its surrounding pixels. Because accounting for the edge is too much work, I simply let my loops start at 1 instead of 0 to ignore the outermost pixels.
Box Blur
It's hard to notice on the small image but it's there. If you do this in Photoshop with a 1px box blur, you'll get the exact same effect.
Gaussian Blur
Again, hard to notice on the small image but it's there. Oddly enough doing this in Photoshop with a 1px Gaussian blur does not give the same effect.
Edge Detection
Works a little differently than that in Photoshop. But still picks up on edges fairly well. Does not threshold despite looking like it did.
Sharpen
This one got a little screwy and ended up being washed out and otherwise looking like the edge detection algorithm.
Framing UI
Framing
Invert UI
This one is literally 3 lines of code, if you're feeling lazy.
Invert
Luminosity UI
Lighten
Darken
Transform UI
Reflect Horizontal
Reflect Vertical
Rotate 90 Degrees Clockwise
Rotate 180 Degrees Clockwise
Rotate 270 Degrees Clockwise
Threshold UI
I tried to make this flexible but couldn't manage to do it with flexible if statements so I just hard coded these cases, as after 6 levels, it doesn't make much of a difference anyway.
Threshold Lv 2
Threshold Lv 3
Threshold Lv 4
Threshold Lv 5
Threshold Lv 6
// Read a PGM-format file into an array// PGM file format is documented here:// http://en.wikipedia.org/wiki/Netpbm_format//// Here's a short example://// P2// # a comment line// 24 7// 3// 0 0 0 0 1 1 1 0 0 0 2 2 2 2 0 0 0 0 3 3 3 3 3 0// ... 6 more rows of pixel data//// The P2 means a gray-scale image// Comment lines start with # and can be ignored// 24 means 24 wide// 7 means 7 high// 3 means the maximum pixel value is 3 in this example#include <stdio.h>#include <stdlib.h>#include <stdbool.h>#include <string.h>//typedef unsigned short int word ;//typedef unsigned char byte ;// Maximum size image we will process (larger ones// get truncated to the upper left corner), or may "wrap"// in interesting ways...#define MAX_HEIGHT 1025#define MAX_WIDTH 1025// allows writing multiple pixels per line to the// output PGM file. Looks like many programs that// use the PGM format just keep this at one. It// should not be more than 70 in any event.#define MAX_PIXELS_PER_LINE 25// Useful macro def#define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) )// This is the array that will contain the image// within your program.//*** ????????????????????????????????// You will need an identical array to use to hold// the output of your image transformation//*** ????????????????????????????????// ME DEFINING THEIMAGEARRAYinttheImageArray[1025][MAX_WIDTH];inttheImageArrayDup[1025][MAX_WIDTH];// Internal function to read the header information// stored at the beginning of a PGM format file.//// Used by read_pgm_file_into_array to see set the// height and width of the imageFILE*read_pgm_file_info(int*height,// outputint*width,// outputint*maxPixel,// outputchar*pgmFname// input){staticFILE*fp=0L;charpgmFormatFlag[3]={'\0'};chartrash[80]={'\0'};memset((void*)trash,'\0',80);fp=fopen(pgmFname,"r");if(fp){// Check the signaturefscanf(fp,"%2c\n",pgmFormatFlag);//printf ( "%s\n", pgmFormatFlag ) ;if(!strcmp(pgmFormatFlag,"P2")){// Skip the comment line//fscanf ( fp, "\n" ) ;fgets(trash,70,fp);//printf ( "%s", trash ) ;// Read the widthfscanf(fp,"%i",width);//printf ( "%i,", *width ) ;// Read the heightfscanf(fp,"%i",height);//printf ( "%i\n", *height ) ;// Read the maximum pixel valuefscanf(fp,"%i",maxPixel);}//fclose ( fp ) ;}returnfp;}// Write out an array as a PGM file (ascii).// writes imageArray to the file named by pgmOutFileName.// commentLine, height, width, and maxPixel must be// set and passed to this function so they can// be written correctly in the PGM file header and// so this function will know how many rows (image height)// and how many columns (image width) to write.voidwrite_pgm_file_from_array(char*pgmOutFileName,// inputintimageArray[][MAX_WIDTH],// inputchar*commentLine,// inputintheight,// inputintwidth,// inputintmaxPixel// input){introw=0;intcol=0;FILE*fp=fopen(pgmOutFileName,"w");if(fp){// Generate the header infofprintf(fp,"P2\n");fprintf(fp,"%s\n",commentLine);fprintf(fp,"%u %u\n",width,height);fprintf(fp,"%u\n",maxPixel);// Now write out the data, ensuring that at most 70// values appear on each line, even if width is > 70for(row=0;row<height;row++){for(col=0;col<width;col++){fprintf(fp,"%u",imageArray[row][col]);// break up long rows into multiple lines as neededif(MAX_PIXELS_PER_LINE>1){fprintf(fp," ");}if((col%MAX_PIXELS_PER_LINE)==0){fprintf(fp,"\n");}}// Watch out of special case of width == 70if(col%MAX_PIXELS_PER_LINE){fprintf(fp,"\n");}}fclose(fp);}return;}// Read file named by pgmInFileName argument into// the array imageArray. This function respects the// MAX_HEIGHT and MAX_WIDTH values, so if you declare// your target array using these bounds, even reading// a large image file should not blow up your program.// (but you will only get the upper left-hand corner// of the image). Checks that the file read is a PGM// (gray scale, ascii) file.//// Sets height, width, and maxPixel according to the// header read.voidread_pgm_file_into_array(intimageArray[][MAX_WIDTH],// outputint*height,// outputint*width,// outputint*maxPixel,// outputchar*pgmInFileName// input){introw=0;intcol=0;FILE*fp=read_pgm_file_info(height,width,maxPixel,pgmInFileName);chartrash=' ';charyesThreshold=' ';if(fp){printf("reading height=%d, width=%d\n",*height,*width);for(row=0;row<MIN(MAX_HEIGHT-1,*height);row++){for(col=0;col<MIN(MAX_WIDTH-1,*width);col++){fscanf(fp,"%i",&imageArray[row][col]);}}fclose(fp);}return;}intmain(void){intheight=0;intwidth=0;intmaxPixel=0;//char* pgmInFileName = "lynx_central.pgm" ;//char* pgmOutFileName = "lynx_central_out.pgm" ;char*pgmInFileName="irv.pgm";char*pgmOutFileName="irv_out.pgm";// Read input file into array, then write it back out,// just to prove this all works. Input file and output// file should look just the same. (sizes on disk might// vary slightly)read_pgm_file_into_array(theImageArray,&height,&width,&maxPixel,pgmInFileName);//****//**** Your code to transform the image goes here ...for(inti=0;i<1025;i++){for(intj=0;j<MAX_WIDTH;j++){theImageArrayDup[i][j]=theImageArray[i][j];}}imageProcessing(theImageArray,theImageArrayDup,height,width);//Transfer the current values in theImageArray back to diskwrite_pgm_file_from_array(pgmOutFileName,theImageArray,"# JR test file",height,width,maxPixel);printf("Copying %s to %s, height=%u, width=%u, maxPixel=%d\n",pgmInFileName,pgmOutFileName,height,width,maxPixel);//system("pause");return0;}
#defineMAX_HEIGHT1025#defineMAX_WIDTH1025#include<stdio.h>#include<math.h>#include<string.h>voidconvolve(inttheImageArray[1025][1025],inttheImageArrayDup[1025][1025],intheight,intwidth){intboxBlur[3][3]={{1,1,1},{1,1,1},{1,1,1}};intgaussian[3][3]={{1,2,1},{2,4,2},{1,2,1}};intedgeDetect[3][3]={{-1,-1,-1},{-1,5,-1},{-1,-1,-1}};intsharpen[3][3]={{0,-1,0},{-1,5,-1},{0,-1,0}};charconvolution[30];printf("Would you like to run box blur, Gaussian blur, edge detection, or sharpen?\n(Box, Gaussian, Edge, Sharpen) \n");scanf("%s",convolution);lowerletter(convolution);if(strcmp(convolution,"box")==0){for(inti=1;i<MAX_HEIGHT;i++){for(intj=1;j<MAX_WIDTH;j++){conMatrixMult(theImageArray,theImageArrayDup,boxBlur,i,j,9,0);}}}if(strcmp(convolution,"gaussian")==0){for(inti=1;i<MAX_HEIGHT;i++){for(intj=1;j<MAX_WIDTH;j++){conMatrixMult(theImageArray,theImageArrayDup,gaussian,i,j,16,0);}}}if(strcmp(convolution,"edge")==0){for(inti=1;i<MAX_HEIGHT;i++){for(intj=1;j<MAX_WIDTH;j++){conMatrixMult(theImageArray,theImageArrayDup,edgeDetect,i,j,128,0);}}}if(strcmp(convolution,"sharpen")==0){for(inti=1;i<MAX_HEIGHT;i++){for(intj=1;j<MAX_WIDTH;j++){conMatrixMult(theImageArray,theImageArrayDup,sharpen,i,j,8,-50);/*if((2*theImageArray[i][j])<= 255){ theImageArray[i][j] = (2*theImageArray[i][j]); } else{ theImageArray[i][j] = 255; }*/theImageArray[i][j]=2*(theImageArray[i][j]-50)+100;if(theImageArray[i][j]>255){theImageArray[i][j]=255;}}}printf("Image is washed out, sorry. Couldn't figure out a way around that. It is sharpened though.");}}voidconMatrixMult(inttheImageArray[1025][1025],inttheImageArrayDup[1025][1025],intmyMatrix[3][3],inti,intj,intdivisor,intsubtractor){theImageArray[i][j]=(theImageArray[i+1][j+1]*myMatrix[0][0]+theImageArray[i][j+1]*myMatrix[0][1]+theImageArray[i-1][j+1]*myMatrix[0][2]+theImageArray[i+1][j]*myMatrix[1][0]+theImageArray[i][j]*myMatrix[1][1]+theImageArray[i-1][j]*myMatrix[1][2]+theImageArray[i+1][j-1]*myMatrix[2][0]+theImageArray[i][j+1]*myMatrix[2][1]+theImageArray[i-1][j-1]*myMatrix[2][2])/divisor-subtractor;}voidinvert(inttheImageArray[1025][1025]){for(inti=1;i<MAX_HEIGHT;i++){for(intj=1;j<MAX_WIDTH;j++){theImageArray[i][j]=255-theImageArray[i][j];}}}voidthreshold(inttheImageArray[1025][1025]){intn;printf("Enter the number of levels you wish to threshold by. \n(Integer between 2-6 inclusive). \n");scanf("%d",&n);for(inti=0;i<MAX_HEIGHT;i++){for(intj=0;j<MAX_WIDTH;j++){switch(n){case1:
printf("Invalid response. Please start over. /n");break;case2:
if(theImageArray[i][j]>(n-1)*255/n){theImageArray[i][j]=255;}else{theImageArray[i][j]=0;}break;case3:
if(theImageArray[i][j]>(n-1)*255/n){theImageArray[i][j]=255;}elseif(theImageArray[i][j]>(n-2)*(255/n)){theImageArray[i][j]=(n-2)*(255/n);}else{theImageArray[i][j]=0;}break;case4:
if(theImageArray[i][j]>(n-1)*255/n){theImageArray[i][j]=255;}elseif(theImageArray[i][j]>(n-2)*(255/n)){theImageArray[i][j]=(n-2)*(255/n);}elseif(theImageArray[i][j]>(n-3)*(255/n)){theImageArray[i][j]=(n-3)*(255/n);}else{theImageArray[i][j]=0;}break;case5:
if(theImageArray[i][j]>(n-1)*255/n){theImageArray[i][j]=255;}elseif(theImageArray[i][j]>(n-2)*(255/n)){theImageArray[i][j]=(n-2)*(255/n);}elseif(theImageArray[i][j]>(n-3)*(255/n)){theImageArray[i][j]=(n-3)*(255/n);}elseif(theImageArray[i][j]>(n-4)*(255/n)){theImageArray[i][j]=(n-4)*(255/n);}else{theImageArray[i][j]=0;}break;case6:
if(theImageArray[i][j]>(n-1)*255/n){theImageArray[i][j]=255;}elseif(theImageArray[i][j]>(n-2)*(255/n)){theImageArray[i][j]=(n-2)*(255/n);}elseif(theImageArray[i][j]>(n-3)*(255/n)){theImageArray[i][j]=(n-3)*(255/n);}elseif(theImageArray[i][j]>(n-4)*(255/n)){theImageArray[i][j]=(n-4)*(255/n);}elseif(theImageArray[i][j]>(n-5)*(255/n)){theImageArray[i][j]=(n-5)*(255/n);}else{theImageArray[i][j]=0;}break;default:printf("Invalid response. Please start over. /n");break;}}}}voidluminosity(inttheImageArray[1025][1025]){charlum[10];doubleperc;printf("Do you want to lighten or darken the image? (Lighten, Darken) \n");scanf("%s",lum);lowerletter(lum);printf("Enter a percentage between 0 and 100 (without the %%) of how much you want\nthe luminosity to change by. \n");scanf("%lf",&perc);perc=0.01*perc;if(strcmp(lum,"lighten")==0){perc=1.0+perc;for(inti=0;i<MAX_HEIGHT;i++){for(intj=0;j<MAX_WIDTH;j++){if(perc*theImageArray[i][j]<=255){theImageArray[i][j]=perc*theImageArray[i][j];}else{theImageArray[i][j]=255;}}}}if(strcmp(lum,"darken")==0){perc=1.0-perc;for(inti=0;i<MAX_HEIGHT;i++){for(intj=0;j<MAX_WIDTH;j++){theImageArray[i][j]=perc*theImageArray[i][j];}}}}voidframe(inttheImageArray[1025][1025],intwidth,intheight){intstartPosX;intendPosX;intstartPosY;intendPosY;intlum;printf("Enter leftmost X position (0-%d): ",width);scanf("%d",&startPosX);printf("\nEnter rightmost X position (0-%d): ",width);scanf("%d",&endPosX);printf("\nEnter topmost Y position (0-%d): ",height);scanf("%d",&startPosY);printf("\nEnter bottommost Y position (0-%d): ",height);scanf("%d",&endPosY);printf("\n");printf("Enter frame luminosity (0-255): ");scanf("%d",&lum);printf("\n");for(inti=0;i<MAX_WIDTH;i++){for(intj=0;j<startPosY;j++){theImageArray[i][j]=lum;}}for(inti=0;i<startPosX;i++){for(intj=startPosY;j<MAX_HEIGHT;j++){theImageArray[i][j]=lum;}}for(inti=startPosX;i<endPosX;i++){for(intj=endPosY;j<MAX_HEIGHT;j++){theImageArray[i][j]=lum;}}for(inti=endPosX;i<MAX_WIDTH;i++){for(intj=startPosY;j<MAX_HEIGHT;j++){theImageArray[i][j]=lum;}}}voidtransformation(inttheImageArray[1025][1025],inttheImageArrayDup[1025][1025],intwidth,intheight){chartfm[10];introtation;printf("Do you want to rotate the image or reflect the image? (Rotate, Reflect) \n");scanf("%s",tfm);lowerletter(tfm);if(strcmp(tfm,"reflect")==0){printf("Do you want the image to be reflected vertically or horizontally?\n(Vertical, Horizontal)\n");chardirection[15];scanf("%s",direction);lowerletter(direction);if(strcmp(direction,"vertical")==0){for(inti=0;i<width;i++){for(intj=0;j<height;j++){theImageArrayDup[i][j]=theImageArray[width-i][j];}}for(inti=0;i<width;i++){for(intj=0;j<height;j++){theImageArray[i][j]=theImageArrayDup[i][j];}}}if(strcmp(direction,"horizontal")==0){for(inti=0;i<width;i++){for(intj=0;j<height;j++){theImageArrayDup[i][j]=theImageArray[i][height-j];}}for(inti=0;i<width;i++){for(intj=0;j<height;j++){theImageArray[i][j]=theImageArrayDup[i][j];}}}}if(strcmp(tfm,"rotate")==0){printf("Do you want the image rotated 90, 180, or 270 degrees clockwise? ");scanf("%d",&rotation);if(rotation==90){for(inti=0;i<MAX_WIDTH;i++){for(intj=0;j<MAX_HEIGHT;j++){theImageArrayDup[i][j]=theImageArray[j][i];}}for(inti=0;i<MAX_WIDTH;i++){for(intj=0;j<MAX_HEIGHT;j++){theImageArray[i][j]=theImageArrayDup[i][j];}}}if(rotation==180){for(inti=0;i<width;i++){for(intj=0;j<height;j++){theImageArrayDup[i][j]=theImageArray[width-i][height-j];}}for(inti=0;i<width;i++){for(intj=0;j<height;j++){theImageArray[i][j]=theImageArrayDup[i][j];}}}if(rotation==270){for(inti=0;i<width;i++){for(intj=0;j<height;j++){theImageArrayDup[i][j]=theImageArray[height-j][i];}}for(inti=0;i<width;i++){for(intj=0;j<height;j++){theImageArray[i][j]=theImageArrayDup[i][j];}}}}}
#include <ctype.h>>#include <stdio.h>#include <stdlib.h>voidimageProcessing(inttheImageArray[1025][1025],inttheImageArrayDup[1025][1025],intheight,intwidth){charinputFunc[30];printf("Enter the function you wish to apply to your image. (Threshold, Luminosity,\nFraming, Transform, Convolve, Invert) \n");scanf("%s",inputFunc);lowerletter(inputFunc);if(strcmp(inputFunc,"invert")==0){invert(theImageArray);}if(strcmp(inputFunc,"convolve")==0){convolve(theImageArray,height,width);}if(strcmp(inputFunc,"threshold")==0){threshold(theImageArray);}if(strcmp(inputFunc,"luminosity")==0){luminosity(theImageArray);}if(strcmp(inputFunc,"framing")==0){frame(theImageArray,height,width);}if(strcmp(inputFunc,"transform")==0){transformation(theImageArray,theImageArrayDup,height,width);}printf("\nDone\n");}voidlowerletter(charmyString[30]){for(inti=0;i<strlen(myString);i++){myString[i]=tolower(myString[i]);}}