Presentation is loading. Please wait.

Presentation is loading. Please wait.

2311: Algorithmic Architecture Basics of Algorithms Data IO.

Similar presentations


Presentation on theme: "2311: Algorithmic Architecture Basics of Algorithms Data IO."— Presentation transcript:

1 2311: Algorithmic Architecture Basics of Algorithms Data IO

2 Data input/output

3 Is there a way to "drive" mel algorithms with data coming from outside Maya like GIS data or else? Email I received

4

5 int r = red(c); int g = green(c); int b = blue(c); int a = alpha(c); Byte (ASCII)Byte (ASCII) ( A4BitByte (Color) Byte (Color) ( Aint (Color) int (Color) ( A Alpha Red Green Blue 00000000 00000000

6 Open the JPG file For every pixel{ get RGB values gray[]= (R + G + B)/3 } Make a plane the size of the pixels For every cv cv.z = gray[]

7 Open the JPG file For every pixel{ get RGB values gray[]= (R + G + B)/3 } Make a plane the size of the pixels For every cv cv.z = gray[] Open the JPG file Open a text file For every pixel{ get RGB values write R, G, B (as text) } Close files Open text file For every line{ gray[]= (R + G + B)/3 } Make a plane the size of the pixels For every cv cv.z = gray[]

8 fopen This command creates/opens a file for write/read and returns a file identifier. "w" open file for writing (destroys prior contents of file). "a" append for writing (appends to end of file). "r" open for reading.

9 fopen This command creates/opens a file for write/read and returns a file identifier. "w" open file for writing (destroys prior contents of file). "a" append for writing (appends to end of file). "r" open for reading. Example $fileId =`fopen "filex.txt" "w" `; fclose $fileId;

10 fopen This command creates/opens a file for write/read and returns a file identifier. "w" open file for writing (destroys prior contents of file). "a" append for writing (appends to end of file). "r" open for reading. Example $fileId =`fopen "filex.txt" "w" `; //also $fileId = eval("fopen \"filex.txt\“ \”w\”"); fclose $fileId;

11 fopen This command creates/opens a file for write/read and returns a file identifier. "w" open file for writing (destroys prior contents of file). "a" append for writing (appends to end of file). "r" open for reading. Example $fileId =`fopen "filex.txt" "w" `; //also $fileId = eval("fopen \"filex.txt\“ \”w\”"); fclose $fileId; chdir "C:/Data"; $filename = "MyData.txt"; $fileId =`fopen $filename ”r”`; //or $fileId = eval("fopen " + $filename + “\”r\””); fclose $fileId;

12 fwrite Writes the next set of bytes as binary data

13 fwrite Writes the next set of bytes as binary data fread Reads the next set of bytes as binary data up to the first occurrence of a NULL character or until EOF.

14 fwrite Writes the next set of bytes as binary data fread Reads the next set of bytes as binary data up to the first occurrence of a NULL character or until EOF. fgetword Returns the next word (string of characters separated by white space characters) or nothing at end of file.

15 Open a file to write chdir "C:/Data"; $fileId =`fopen "hello.txt" "w"`; fwrite $fileId "Hello there\n"; fclose $fileId;

16 Open a file to write chdir "C:/Data"; $fileId =`fopen "hello.txt" "w"`; fwrite $fileId "Hello there\n"; fclose $fileId;

17 Open a file to write chdir "C:/Data"; $fileId =`fopen "hello.txt" “w"`; fwrite $fileId "Hello there\n"; fclose $fileId; Open a file to read from chdir "C:/Data"; $fileId=`fopen "hello.txt" "r"`; string $s; $s=`fread $fileId $s`; print $s; fclose $fileId; Hello there

18 Write and retrieve data chdir "C:/Data"; $fileId=`fopen "data.txt" "w"`; $num = 10; fprint $fileId ($num + " "); for($i=0; $i<$num; $i++) fprint $fileId (rand(10) + " "); fclose $fileId; 10 8.759808672 5.315568646 9.202609419 5.154311513 8.104294535 1.884202504 8.863144439 5.706140193 0.7677456488 8.152738823

19 Write and retrieve data chdir "C:/Data"; $fileId=`fopen "data.txt" "w"`; $num = 10; fprint $fileId ($num + " "); for($i=0; $i<$num; $i++) fprint $fileId (rand(10) + " "); fclose $fileId; chdir "C:/Data"; string $filename = "data.txt"; $fileId = `fopen $filename "r"`; string $s=`fgetword $fileId`; int $num = (int)$s; print($num + "\n"); for($i=0; $i<$num; $i++) { string $d=`fgetword $fileId`; print( $d + "\n"); } fclose $fileId; 10 8.759808672 5.315568646 9.202609419 5.154311513 8.104294535 1.884202504 8.863144439 5.706140193 0.7677456488 8.152738823 10 8.759808672 5.315568646 9.202609419 5.154311513 8.104294535 1.884202504 8.863144439 5.706140193 0.7677456488 8.152738823

20

21 5 7 87 87 87 81 81 81 92 92 92 65 65 65 60 60 60 55 55 55 17 17 17 82 82 82 66 66 66 81 81 81 82 82 82 121 121 121 164 164 164 159 159 159 92 92 92 68 68 68 88 88 88 104 104 104 155 155 155 208 208 208 201 201 201 86 86 86 75 75 75 122 122 122 145 145 145 172 172 172 198 198 198 168 168 168 67 67 67 77 77 77 158 158 158 190 190 190 195 195 195 211 211 211 188 188 188

22 5 7 87 87 87 81 81 81 92 92 92 65 65 65 60 60 60 55 55 55 17 17 17 82 82 82 66 66 66 81 81 81 82 82 82 121 121 121 164 164 164 159 159 159 92 92 92 68 68 68 88 88 88 104 104 104 155 155 155 208 208 208 201 201 201 86 86 86 75 75 75 122 122 122 145 145 145 172 172 172 198 198 198 168 168 168 67 67 67 77 77 77 158 158 158 190 190 190 195 195 195 211 211 211 188 188 188

23 chdir "C:/Data"; int $fileId = `fopen "out.txt" "r"`; Open the file 5 7 87 87 87 81 81 81 92 92 92 65 65 65 60 60 60 55 55 55 17 17 17 82 82 82 66 66 66 81 81 81 82 82 82 121 121 121 164 164 164 159 159 159 92 92 92 68 68 68 88 88 88 104 104 104 155 155 155 208 208 208 201 201 201 86 86 86 75 75 75 122 122 122 145 145 145 172 172 172 198 198 198 168 168 168 67 67 67 77 77 77 158 158 158 190 190 190 195 195 195 211 211 211 188 188 188 out.txt

24 chdir "C:/Data"; int $fileId = `fopen "out.txt" "r"`; string $s=`fgetword $fileId`; //read the width int $width = (int)$s; // convert to an integer $s=`fgetword $fileId`;//read the height int $height = (int)$s;// convert to an integer int $size = $width * $height; int $dataSet[]; // declare an array to hold the values to be read Open the file Read the xm and ym values 5 7 87 87 87 81 81 81 92 92 92 65 65 65 60 60 60 55 55 55 17 17 17 82 82 82 66 66 66 81 81 81 82 82 82 121 121 121 164 164 164 159 159 159 92 92 92 68 68 68 88 88 88 104 104 104 155 155 155 208 208 208 201 201 201 86 86 86 75 75 75 122 122 122 145 145 145 172 172 172 198 198 198 168 168 168 67 67 67 77 77 77 158 158 158 190 190 190 195 195 195 211 211 211 188 188 188 out.txt

25 chdir "C:/Data"; int $fileId = `fopen "out.txt" "r"`; string $s=`fgetword $fileId`; //read the width int $width = (int)$s; // convert to an integer $s=`fgetword $fileId`;//read the height int $height = (int)$s;// convert to an integer int $size = $width * $height; int $dataSet[]; // declare an array to hold the values to be read for($i=0; $i<$size; $i++) { string $red = `fgetword $fileId`; // read Red string $green = `fgetword $fileId`; // read Green string $blue = `fgetword $fileId`; // read Blue float $av = ((float)$red + (float)$green + (float)$blue)/3.; $dataSet[$i] = (int)$av; // save the average } fclose $fileId; // close the file Open the file Read the xm and ym values Loop in and Retrieve the RGB values. Average and put in an array data[][] Close the file 5 7 87 87 87 81 81 81 92 92 92 65 65 65 60 60 60 55 55 55 17 17 17 82 82 82 66 66 66 81 81 81 82 82 82 121 121 121 164 164 164 159 159 159 92 92 92 68 68 68 88 88 88 104 104 104 155 155 155 208 208 208 201 201 201 86 86 86 75 75 75 122 122 122 145 145 145 172 172 172 198 198 198 168 168 168 67 67 67 77 77 77 158 158 158 190 190 190 195 195 195 211 211 211 188 188 188 out.txt

26 chdir "C:/Data"; int $fileId = `fopen "out.txt" "r"`; string $s=`fgetword $fileId`; //read the width int $width = (int)$s; // convert to an integer $s=`fgetword $fileId`;//read the height int $height = (int)$s;// convert to an integer int $size = $width * $height; int $dataSet[]; // declare an array to hold the values to be read for($i=0; $i<$size; $i++) { string $red = `fgetword $fileId`; // read Red string $green = `fgetword $fileId`; // read Green string $blue = `fgetword $fileId`; // read Blue float $av = ((float)$red + (float)$green + (float)$blue)/3.; $dataSet[$i] = (int)$av; // save the average } fclose $fileId; // close the file //create a plane the size of the data eval("nurbsPlane -d 1 -ax 0 0 1 -w 10 -u "+$width+" -v "+$height); Open the file Read the xm and ym values Loop in and Retrieve the RGB values. Average and put in an array data[][] Close the file Make a plane xm by ym 5 7 87 87 87 81 81 81 92 92 92 65 65 65 60 60 60 55 55 55 17 17 17 82 82 82 66 66 66 81 81 81 82 82 82 121 121 121 164 164 164 159 159 159 92 92 92 68 68 68 88 88 88 104 104 104 155 155 155 208 208 208 201 201 201 86 86 86 75 75 75 122 122 122 145 145 145 172 172 172 198 198 198 168 168 168 67 67 67 77 77 77 158 158 158 190 190 190 195 195 195 211 211 211 188 188 188 out.txt

27 chdir "C:/Data"; int $fileId = `fopen "out.txt" "r"`; string $s=`fgetword $fileId`; //read the width int $width = (int)$s; // convert to an integer $s=`fgetword $fileId`;//read the height int $height = (int)$s;// convert to an integer int $size = $width * $height; int $dataSet[]; // declare an array to hold the values to be read for($i=0; $i<$size; $i++) { string $red = `fgetword $fileId`; // read Red string $green = `fgetword $fileId`; // read Green string $blue = `fgetword $fileId`; // read Blue float $av = ((float)$red + (float)$green + (float)$blue)/3.; $dataSet[$i] = (int)$av; // save the average } fclose $fileId; // close the file //create a plane the size of the data eval("nurbsPlane -d 1 -ax 0 0 1 -w 10 -u "+$width+" -v "+$height); int $k=0; // use a counter to read from the array for($x=0; $x<$width; $x++) for($y=0; $y<$height; $y++){ select -r nurbsPlane1.cv[$x][$y] ; //select a cv sequentially int $val = $dataSet[$k]; // get the data (average) float $z = (float)$val/100; // assign it as height move -r 0 0 $z ; $k++; // increase the array counter }; Open the file Read the xm and ym values Loop in and Retrieve the RGB values. Average and put in an array data[][] Close the file Make a plane xm by ym Make the height of every cv equal to the array value data[][] 5 7 87 87 87 81 81 81 92 92 92 65 65 65 60 60 60 55 55 55 17 17 17 82 82 82 66 66 66 81 81 81 82 82 82 121 121 121 164 164 164 159 159 159 92 92 92 68 68 68 88 88 88 104 104 104 155 155 155 208 208 208 201 201 201 86 86 86 75 75 75 122 122 122 145 145 145 172 172 172 198 198 198 168 168 168 67 67 67 77 77 77 158 158 158 190 190 190 195 195 195 211 211 211 188 188 188 out.txt

28

29

30 Processing Code to Read JPG/GIF and create a text file with RGB values //Open the image file PImage myImage = loadImage("c:/data/test.jpg"); image(myImage, 0, 0); //display the image (also loads it in memory) Open the image and

31 Processing Code to Read JPG/GIF and create a text file with RGB values //Open the image file PImage myImage = loadImage("c:/data/test.jpg"); image(myImage, 0, 0); //display the image (also loads it in memory) Open the image and

32 Processing Code to Read JPG/GIF and create a text file with RGB values //Open the image file PImage myImage = loadImage("c:/data/test.jpg"); image(myImage, 0, 0); //display the image (also loads it in memory) int size = myImage.height*myImage.width; //get the number of pixels String list[] = new String[size+2]; //allocate memory for all pixels list[0] = myImage.width+""; list[1] = myImage.height+""; Open the image and get the width and height

33 Processing Code to Read JPG/GIF and create a text file with RGB values //Open the image file PImage myImage = loadImage("c:/data/test.jpg"); image(myImage, 0, 0); //display the image (also loads it in memory) int size = myImage.height*myImage.width; //get the number of pixels String list[] = new String[size+2]; //allocate memory for all pixels list[0] = myImage.width+""; list[1] = myImage.height+""; int i=2; //initialize the counter for(int x=0; x<myImage.height; x++) for(int y=0; y<myImage.width; y++){ color c = get(x,y); //get the color of a pixel list[i] = (red(c) + " " + green(c) + " " + blue(c)); //store the color as a string i++; //increment the counter } Open the image and get the width and height Write the RGB values

34 Processing Code to Read JPG/GIF and create a text file with RGB values //Open the image file PImage myImage = loadImage("c:/data/test.jpg"); image(myImage, 0, 0); //display the image (also loads it in memory) int size = myImage.height*myImage.width; //get the number of pixels String list[] = new String[size+2]; //allocate memory for all pixels list[0] = myImage.width+""; list[1] = myImage.height+""; int i=2; //initialize the counter for(int x=0; x<myImage.height; x++) for(int y=0; y<myImage.width; y++){ color c = get(x,y); //get the color of a pixel list[i] = (red(c) + " " + green(c) + " " + blue(c)); //store the color as a string i++; //increment the counter } // now write the strings to a file, each on a separate line saveStrings("c:/data/image.txt", list); Open the image and get the width and height Write the RGB values

35


Download ppt "2311: Algorithmic Architecture Basics of Algorithms Data IO."

Similar presentations


Ads by Google