<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>
<chapter name="Tutorial 29: Using the Alpha Channel">
<setdocpatch name="29jUsingTheAlphaChannel" patch="29jUsingTheAlphaChannel.maxpat"/>

<previous name="jitterchapter28">Audio Control of Video</previous>
<next name="jitterchapter30">Drawing 3D text</next>
<parent name="jitindex">Jitter Tutorials</parent>



<h1>Tutorial 29: Using the Alpha Channel</h1>
<p>In this tutorial we'll look at how to composite two images using the <i>alpha channel</i> of a 4-plane <i>char</i> Jitter matrix as a transparency mask. We'll explore this concept as a way to superimpose subtitles generated by the <o>jit.lcd</o> object over a movie image.</p>


<p>The upper-lefthand region of the tutorial patch contains a <o>jit.movie</o> object that reads the file <i>ozone.mov</i> when the patch opens. The <o>metro</o> object outputs a new matrix from the <o>jit.movie</o> object every <m>33</m> milliseconds and polls the <m>time</m> attribute of the object (the current playback position of the movie, in QuickTime time units) by using a <o>trigger</o> object:</p>

<illustration><img src="images/jitterchapter29a.png"/>Play back the movie, getting the current time position with each new matrix</illustration>

<bullet>Start viewing the movie by clicking the <o>toggle</o> box attached to the <o>metro</o>. The <o>jit.movie</o> object will start to output matrices as well as the current playback position within the movie. You should see the movie (with subtitles!) appear in the <o>jit.pwindow</o> at the bottom of the patch.</bullet>
<illustration><img src="images/jitterchapter29b.png"/>Nice kitty</illustration>
<p>First, we'll look at how the subtitles are being generated. Then we'll investigate how we composite them with the image from the movie using the alpha channel.</p>

<h2>The jit.lcd object</h2>
<p>The subtitles in our patch are generated by sending messages to the <o>jit.lcd</o> object (at the top of the patch). The arguments to <o>jit.lcd</o> specify the <m>planecount</m>, <m>type</m>, and <m>dim</m> of the matrix generated by the object (<o>jit.lcd</o> only supports 4-plane <i>char</i> matrices). The <o>jit.lcd</o> object takes messages in the form of QuickDraw commands, and draws them into an output matrix when it receives a <m>bang</m>. We initialize our <o>jit.lcd</o> object by giving it commands to set its <m>font</m> and <m>textface</m> for drawing text and its foreground (<m>frgb</m>) and background (<m>brgb</m>) color (in lists of RGB values). We then <m>clear</m> the <o>jit.lcd</o> object's internal image and send out an empty matrix with a <m>bang</m>:</p>

<illustration><img src="images/jitterchapter29c.png"/>An initialized <o>jit.lcd</o> object</illustration>
<div>
<techdetail>Note: The <o>jit.lcd</o> object has the same complete set of QuickDraw commands and functions that are available in the Max <o>lcd </o> object. Though we'll be using it in this patch for the purpose of generating text images, <o>jit.lcd</o> can be used to generate all manner of 2-dimensional vector graphics as well. The Max tutorial <link type="tutorial" module="max" name="basicchapter09">Mouse Drawing</link> — Introduction to Drawing, demonstrates some of the features of the <o>lcd</o> object, all of which can be applied just as easily to <o>jit.lcd</o>.</techdetail>
</div><br/>
<p>The <o>jit.lcd</o> object outputs its matrix into a <o>jit.rgb2luma</o> object, which converts the 4-plane image output by <o>jit.lcd</o> into a 1-plane grayscale image. The <o>jit.rgb2luma</o> object generates a matrix containing the luminosity value of each cell in the input matrix. This 1-plane matrix is then sent to a <o>send</o> object with the name <m>alphamask</m> and to a <o>jit.pwindow</o> object so we can view it. Note that the <o>jit.pwindow</o> object has its <m>border</m> attribute set to <m>1</m>. As a result, we can see a 1-pixel black border around the white image inside.</p>

<p>Our <o>jit.lcd</o> object also receives messages from elsewhere in the patch (via the <o>receive</o> object named <m>lcd</m> attached to it). The subtitles are generated automatically by looking for certain times in the movie playback:</p>

<illustration><img src="images/jitterchapter29d.png"/>Parsing the time values from the <o>jit.movie</o> object</illustration>
<p>The <o>jit.movie</o> object outputs its current playback position with every tick of the <o>metro</o> object, thanks to the <link name="trigger" type="refpage">t</link> <m>gettime b</m> we have between the two. The <m>time</m> attribute is sent out the right outlet of the <o>jit.movie</o> object, where we can use a <o>route</o> object to strip it of its message selector (<m>time</m>). We divide the value by <m>100</m> so that we can search for a specific time more accurately. Since the <o>metro</o> only queries the time every <m>33</m> milliseconds, it's entirely possible that we'll completely skip over a specific time -- dividing the time value by 100 makes it easier to find the point in the movie we want.</p>

<p>The time values are sent through a <o>gate</o> object where you can disable the subtitles if you so choose:</p>

<illustration><img src="images/jitterchapter29e.png"/>Control the flow of the time values with a <o>gate</o> object</illustration>

<bullet>Click the <o>toggle</o> box attached to the <o>gate</o>. The subtitles should disappear. You can resume the subtitles by clicking the <o>toggle</o> box again.</bullet><br/>
<p>The subtitles are finally generated when the time values <m>21</m> and <m>40</m> make it past the <o>gate</o> object. The select object sends out a <m>bang</m> when those values arrive. This triggers commands from the <o>message</o> boxes to <o>jit.lcd</o>:</p>

<illustration><img src="images/jitterchapter29f.png"/>Performing the subtitling based on the time values</illustration>
<p>The <m>clear</m> message to <o>jit.lcd</o> erases the drawing canvas, filling all the pixels with white (our chosen background color). The <m>moveto</m> message moves the cursor of the <o>jit.lcd</o> object to a specific coordinate from which it will draw subsequent commands. The <m>write</m> message draws text into the matrix using the currently selected <m>font</m> and <m>textface</m>. Once we've written in our subtitles, we send the object a <m>bang</m> to make it output a new matrix. With every subtitle, we also send a <m>bang</m> to a <o>delay</o> object, which clears and resends the matrix <m>1000</m> milliseconds later, erasing the title.</p>

<h2>Make Your Own Titles</h2>
<p>The region of the tutorial patch to the right (with the pink background) lets you use the <o>textedit</o> object to generate your own subtitles. The <o>number</o> box labelled <i>Offset</i> determines the horizontal offset for the text. The <o>trigger</o> object allows you to send all the necessary QuickDraw commands to the <o>jit.lcd</o> object in the correct order.</p>

<illustration><img src="images/jitterchapter29g.png"/>Make your own subtitles</illustration>
<bullet>Turn off the automatic subtitling with the <o>toggle</o> box above the <o>gate</o>. Type some text into the <o>textedit</o> box and hit the return key. The text will appear superimposed over the image.</bullet>
<illustration><img src="images/jitterchapter29h.png"/>The new subtitle over the image</illustration>
<p>Now that we understand how the titles are generated, lets take a look at how they get composited over the movie.</p>

<h2>The Alpha Channel</h2>
<p>The <i>alpha channel</i> of an ARGB image defines its transparency when it is composited with a second image. If a pixel has an alpha channel of <m>0</m> it is considered completely transparent when composited onto another image. If a pixel's alpha channel is set to <m>255</m> it is considered completely opaque, and will show at full opacity when composited. Intermediate values will cause the pixel to fade smoothly between the first and second image. In 4-plane <i>char</i> Jitter matrices, data stored in plane <m>0</m> of the matrix is considered to be the alpha channel.</p>

<div>
<techdetail>Technical Detail: Color systems and software environments differ on whether the alpha channel describes the <i>transparency</i> or the <i>opacity</i> of an image. In movies (and hence in Jitter) an alpha value of <m>255</m> means that the pixel is fully <i>opaque</i>. You may encounter programs where the opposite is true (i.e. an alpha value of <m>255</m> denotes full transparency). The <m>mode</m> attribute of the <o>jit.alphablend</o> object lets you treat the alpha channel in either way. The default <m>mode</m> of <m>0</m> treats increasing alpha values as more opaque, while setting the <m>mode</m> attribute to <m>1</m> causes the object to treat increasing alpha values as more transparent.</techdetail>
</div>
<p>The <o>jit.alphablend</o> object uses the values stored in the alpha channel (plane 0) of the matrix arriving in the left inlet to perform a crossfade (on a cell-by-cell basis) between the matrices arriving in its two inlets. Our patch replaces plane <m>0</m> of the <o>jit.movie</o> object's output matrix with the output of the <o>jit.lcd</o> object. We then use this new alpha channel with the <o>jit.alphablend</o> object to crossfade between the movie and an inverted copy of itself:</p>

<illustration><img src="images/jitterchapter29i.png"/>Inserting a new alpha channel with the <o>jit.pack</o> object</illustration>
<p>We use the <o>jit.unpack</o> and <o>jit.pack</o> objects to strip the original alpha channel from our movie. The 1-plane matrix containing the subtitle arrives at the <o>jit.pack</o> object from the <o>receive</o> object above it. Notice how the <o>trigger</o> object is used to force <o>jit.pack</o> to output new matrices even when no new matrix has arrived from the <o>receive</o> (<o>jit.pack</o>, like the Max <o>pack</o> object, will only output a matrix when it has received a new matrix or a <m>bang</m> in its leftmost inlet). The <o>jit.op</o> object creates a negative of the original matrix from the movie (by subtracting the matrix values from <m>255</m> using the <m>!-</m> operator). The <o>jit.alphablend</o> object then uses our new alpha channel -- white values in the subtitle matrix cause the original image to be retained, while black values bring in the inverted image from the righthand matrix.</p>

<p>Different techniques are often used for subtitling. The technique of superimposing white text over an image (sometimes with a black border around it) is far more common than the technique used here of filling an alpha mask with an inverted image. However, doing our subtitling this way gives us a perfect excuse to use the <o>jit.alphablend</o> object, and may give you more legible subtitles in situations where the background image has areas of high contrast.</p>

<p>The image below shows the compositing process with <o>jit.pwindow</o> objects showing intermediate steps:</p>

<illustration><img src="images/jitterchapter29j.png"/>The compositing process, showing intermediate steps</illustration>

<h2>Summary</h2>
<p>The <o>jit.lcd</o> object offers a complete set of QuickDraw commands to draw text and 2-dimensional graphics into a Jitter matrix. The <o>jit.rgb2luma</o> object converts a 4-plane ARGB matrix to a 1-plane grayscale matrix containing luminance data. You can replace the alpha channel (plane <m>0</m>) of an image with a 1-plane matrix using the <o>jit.pack</o> object. The <o>jit.alphablend</o> object crossfades two images on a cell-by-cell basis based on the alpha channel of the lefthand matrix.</p>

	<seealsolist>
		<seealso display="Video and Graphics Tutorial 10: Composing the Screen" module="Video and Graphics" name="jitterchapter00l_Composing the screen" type="tutorial" />
		<seealso display="Depth Testing vs Layering" module="core" name="jitter_layering" type="vignette" />
		<seealso name="gate">Pass the input out a specific outlet</seealso>
		<seealso name="jit.alphablend">Use the alpha channel of one image to blend two images together</seealso>
		<seealso name="jit.lcd">QuickDraw wrapper</seealso>
		<seealso name="jit.op">Apply binary or unary operators</seealso>
		<seealso name="jit.pack">Make a multiplane matrix out of single plane matrices</seealso>
		<seealso name="jit.pwindow">In-Patcher Window</seealso>
		<seealso name="jit.movie">Play or edit a movie</seealso>
		<seealso name="jit.rgb2luma">Converts RGB to monochrome (luminance)</seealso>
		<seealso name="jit.unpack">Make multiple single plane matrices out of a multiplane matrix</seealso>
		<seealso name="text">Format messages as a text file</seealso>
	</seealsolist>
	</chapter>
