<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>
<chapter name="Tutorial 20: Importing and Exporting Single Matrices">
<setdocpatch name="20jImportExport" patch="20jImportExport.maxpat"/>

<previous name="jitterchapter19">Recording QuickTime movies</previous>
<next name="jitterchapter21">Working with Live Video and Audio Input</next>
<parent name="jitindex">Jitter Tutorials</parent>



<h1>Tutorial 20: Importing and Exporting Single Matrices</h1>
<p>This tutorial shows how you can export a single matrix to disk from Jitter. We'll demonstrate the variety of options available, including QuickTime still picture formats and Jitter's own .jxf file format.</p>

<p>In the previous Tutorial, we learned how to save a sequence of matrices as a QuickTime movie&#x2014;and we can save a single matrix using the same techniques. Since the data that Jitter works with can describe much more than series of images, it makes sense that there should be several additional options for exporting individual matrices.</p>

<h2>Import and Export from the jit.matrix object</h2>
<p>The <o>jit.matrix</o> object offers two types of single-matrix import/export: QuickTime movie and Jitter binary (.jxf). Both formats permit import and export of a single matrix. We'll discuss both, starting with the movie format.</p>

<h3>QuickTime export and import</h3>
<p>The QuickTime movie format is identical to the format we learned about in the last tutorial&#x2014;Recording QuickTime Movies. The only difference is that, in this case, an exported output movie will be exactly one frame long. We can also import a single frame of a movie, regardless of the movie's length. The messages <m>importmovie</m> and <m>exportmovie</m> are used to import and export a single matrix from the <o>jit.matrix</o> object. The <m>exportmovie</m> message conveniently uses the same format as the <o>jit.record</o> object's <m>write</m> message.</p>

<div>
<techdetail>In fact, when you use the <o>jit.matrix</o> object's <m>exportmovie</m> message, the <o>jit.matrix</o> object is briefly creating an internal instance of a <o>jit.record</o> object, and sending a <m>write</m> message to it with the arguments you've specified for <m>exportmovie</m>. Although that shouldn't change the way you think about it, it's pretty nifty.</techdetail>
</div>

<illustration><img src="images/jitterchapter20a.png"/>the <o>jit.matrix</o> object&#x2019;s <m>importmovie</m> and <m>exportmovie</m> messages</illustration>

<bullet>Click the <o>message</o> box that says <m>read</m> to load a movie into the <o>jit.movie</o> object. Since we're only interested in a single frame, there's no <o>metro</o> in the patch. You'll also notice that there is a new attribute set for the <o>jit.movie</o> object&#x2014;<m> @autostart 0.</m> Setting this attribute means that our movie will not begin playing automatically. We can choose a frame of the loaded movie we want to export, using the <o>number</o> box connected to the <o>message</o> box containing the message <m>frame $1, bang</m>.</bullet>
<bullet>Why are the colors all messed up?  They're messed up because we're using the <m>planemap</m> attribute of <o>jit.matrix</o> to remap the planes of the incoming movie from 0 1 2 3 (alpha, red, green, blue) to 0 3 2 1 (alpha, blue, red, green). We'll see why in a few moments.</bullet>
<bullet>Click the <o>message</o> box <m>exportmovie myframe.mov 30. jpeg</m> to export the frame as a QuickTime movie. As with <o>jit.record</o>, we're specifying the frame rate and codec (<m>30.</m> and <m>jpeg</m>). We're also specifying a file name, <m>myframe.mov</m>. The <m>exportmovie</m> message automatically closes the file after writing the frame to it.</bullet>
<bullet>To load the single frame we've just exported , we'll use the <m>importmovie</m> message. Since we want to be certain that we're really importing the frame we just exported, we'll clear the <o>jit.matrix</o> object first. Click on the <o>message</o> box that says <m>clear, bang</m>. This message clears the matrix and then outputs it to the <o>jit.pwindow</o>, which should now appear entirely black.</bullet>
<bullet>Now, click the <o>message</o> box that says <m>importmovie, bang</m>. A file Dialog Box should appear, and you should locate and read <i>myframe.mov</i>. The movie should have been saved in the same folder as the Tutorial patch. If you can't find it there, use the Finder's Find... command to locate the file on your disk drive. The frame you just exported should now be back in the <o>jit.pwindow</o>. The <m>importmovie</m> message takes an optional first argument which specifies a file name.</bullet>
<bullet>You're probably wondering what that second <m>importmovie</m> message is for. If you try to import a multi-frame movie into <o>jit.matrix</o>, the object assumes that you want the first frame, unless you specify a time value that you'd prefer. In this example, we've asked for time value 600 whichis one second in from the beginning of a normal  QuickTime movie&#x2014;remember from Tutorial 4 that most QuickTime movies use a timescale of 600, which means they have 600 time values per second.</bullet>
<bullet>Try reading another QuickTime movie by clicking on the <o>message</o> box <m>importmovie 600, bang</m>. The <o>jit.pwindow</o> should now be showing a frame from one second into that movie. You'll notice that the colors in the frame are not switched around this time. This is because we are importing an image matrix <i>directly into the object</i>. Since the <o>jit.matrix</o> object's <m>planemap</m> attribute only affects matrices that arrive via its inlet, no plane remapping occurred.</bullet>
<h3>Jitter binary export and import</h3>
<p>Jitter offers its own binary format for single-matrix export, called .jxf. The Jitter binary format is simpler to use than the QuickTime format, and .jxf stores your data without any of the data losses associated with the various codecs you need to use with the QuickTime format. Additionally, the .jxf format supports matrices of all types, dimensions and planecounts, whereas the QuickTime format is only capable of storing 4-plane char matrices (images).</p>

<illustration><img src="images/jitterchapter20b.png"/>using .jxf format with the <o>jit.matrix</o> object</illustration>

<p>The patch shown above uses the Jitter binary format, which is selected using the <m>write</m> and <m>read</m> messages. While it is very similar to the previous example patch, there are some important differences:</p>

<bullet>The <m>write</m> message only takes a single argument that specifies the name of the output file. Since the .jxf format is always a single matrix of uncompressed data, we don't need any other arguments.</bullet>
<bullet>The <m>read</m> message doesn't need a time argument (since a .jxf file will only have a single matrix in it). Like the <m>importmovie</m> message, <m>read</m> will take an optional argument for the file name.</bullet>
<bullet>The <m>read</m> and <m>write</m> messages cause the <o>jit.matrix</o> object to send confirmation messages out the object's right outlet. We've connected that outlet to a <o>print</o> object, so you can see those messages in the Max Console.</bullet>
<div>
<techdetail>Both techniques described in this section are available to the <o>jit.matrixset</o> object as well, and function in similar ways.</techdetail>
</div>



<h2>Summary</h2>
<p>Jitter offers several methods for importing and exporting single matrices from and to disk. The <o>jit.matrix</o> object (and the <o>jit.matrixset</o> object, not demonstrated in this chapter) allow single matrices to be saved as single-frame movies, using the same parameters as the <o>jit.record</o> object's <m>write</m> message. The <o>jit.matrix</o> and <o>jit.matrixset</o> objects also support the Jitter binary (.jxf) format&#x2014; an uncompressed format specially suited to Jitter matrices. </p>



	<seealsolist>
		<seealso name="jit.matrix">The Jitter Matrix!</seealso>
		<seealso name="jit.op">Apply binary or unary operators</seealso>
		<seealso name="jit.pwindow">In-Patcher Window</seealso>
		<seealso name="jit.movie">Play or edit a movie</seealso>
		<seealso name="jit.record">Record a movie</seealso>
		<seealso name="jit.str.fromsymbol">Convert Max symbol to Jitter string matrix</seealso>
		<seealso name="jit.str.tosymbol">Convert Jitter string matrix to Max symbol</seealso>
		<seealso name="jit.textfile">Read and write a matrix as an ASCII text file</seealso>
	</seealsolist>
	</chapter>
