<?php
$link = "howto";
$title = "Random How-To's";
include("inc/header.php");
?>
<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>
<script language="JavaScript" type="text/JavaScript">
function show(entry_name)
{
   var span_entry_link = document.getElementById(entry_name + "_link");
   if (span_entry_link.innerHTML == "+")
   {
      span_entry_link.innerHTML = "-";
      document.getElementById(entry_name).style.display = 'block';
   }
   else
   {
      span_entry_link.innerHTML = "+";
      //document.getElementById(entry_name).visibility = 'hidden';
      document.getElementById(entry_name).style.display = 'none';
   }
}
</script>

<br><p align="center">This page is mainly for keeping track of things that I develop.  Feel free to use whatever you find helpful!<br>I doubt that any of the things can cause any damage, but if it does, I will not be held liable for it.</p>
<!------------------------------------------------------------------------------------------------>
<!------------------------------------------------------------------------------------------------>

<hr class="howtohr">
<h1>Photo / Video Tools</h1>

<h2><a href="javascript:show('gimp_scripts')"><font face="Courier New"><span id="gimp_scripts_link">+</span> </font>Gimp Photo Editing Scripts</a></h2>
<div class="box" id="gimp_scripts" style="display: none;">
The following files are a set of photo editing scripts I use in <a href="http://www.gimp.org/">GIMP</a> (a free alternative to Photoshop).<br>
<ul>
<li><a href="howto/gimp_scripts/median-usm.scm">Median and Unsharp Mask</a> - This applies the default despeckling followed by the default unsharp mask. I find this is a pretty useful combination in high ISO images.</li>
<li><a href="howto/gimp_scripts/vignette.scm">Vignette Effect</a> - This applies a vignetting effect to your picture (darkening the outsides). You can first select a region to do the vignetting on (select the objects you do not want to darken) and then run the script. If nothing is selected, an ellipse will be automatically drawn on the entire image domain for the vignetting effect to be performed on. A typical Gaussian blurring radius should be about 1/4 - 1/2 the size of the smallest image dimension.</li>
</ul>
</div>

<!------------------------------------------------------------------------------------------------>

<h2><a href="javascript:show('nef2jpg')"><font face="Courier New"><span id="nef2jpg_link">+</span> </font>Batch Conversion of NEF (Nikon RAW) Images to JPG</a></h2>
<div class="box" id="nef2jpg" style="display: none;">
<p align="center">[ <a href="howto/nef2jpg_windows.zip">Windows Download</a> ] &nbsp; [ <a href="howto/nef2jpg.sh">Linux-Bash Download</a> ]</p>
I use the following bash script to convert my Nikon RAW NEF files. The Windows code isn't included because I'm too lazy to post it. If you want it, let me know. These are both basically wrappers for <a href="http://www.sno.phy.queensu.ca/~phil/exiftool/">exiftool</a>, <a href="http://www.sentex.net/~mwandel/jhead/">jhead</a>, and <a href="http://jpegclub.org/">jpegtran</a>. These files have been included in the Windows *.zip, but should be installed separately for Linux.
</div>

<!------------------------------------------------------------------------------------------------>

<h2><a href="javascript:show('handbrakeScript')"><font face="Courier New"><span id="handbrakeScript_link">+</span> </font>Batch Conversion of Movies (HD or non-HD)</a></h2>
<div class="box" id="handbrakeScript" style="display: none;">
<p align="center">[ <a href="howto/handbrakeScript.exe">Windows Download</a> ]</p>
I use the following application to batch convert movies. This is basically a wrapper for <a href="http://handbrake.fr/downloads2.php">HandBrake</a>&rsquo;s Command Line Interface, which must be downloaded separately. handbrakeScript.exe should be put into the HandBrake Program Files directory. The default settings are used for conversion. If HD conversion is selected, the x264 codec is used with the "Regular" preset.
</div>

<h2><a href="javascript:show('batch_conversion')"><font face="Courier New"><span id="batch_conversion_link">+</span> </font>Batch Conversion of Images</a></h2>
<div class="box" id="batch_conversion" style="display: none;">
<p align="center">[ <a href="howto/convertBatch.sh">Linux-Bash Download</a> ]</p>
I use the following bash script to convert my image files:
<pre align="left"><code class="prettyprint">#!/bin/bash

# batch converts images from one type to another
shopt -s expand_aliases
alias cecho=/usr/bin/cecho.sh

echo ""
cecho "**************************************" r
echo "What kind of file would you like to convert from? (e.g. jpg, gif, png, bmp)"
read inExtension
echo ""
echo "What kind of file would you like to convert to? (e.g. jpg, gif, png, bmp)"
read outExtension

cecho "**************************************" r
for f in *.${inExtension}
do
echo $f
done
echo "Are you sure you want to convert all the files listed above? (y/n)"
read confirm

cecho "**************************************" r

if [ $confirm == "y" ]
then

echo "Starting conversion..."

for f in *.${inExtension}
do
echo "Processing $f file..."
outname=${f%.*}.${outExtension}
convert ${f} ${outname}
done
cecho "**************************************" r
echo ""
fi</code></pre>
</div>


<!------------------------------------------------------------------------------------------------>
<!------------------------------------------------------------------------------------------------>

<hr class="howtohr">
<h1>Random Things</h1>

<h2><a href="javascript:show('OpenMPReductionArray')"><font face="Courier New"><span id="OpenMPReductionArray_link">+</span> </font>OpenMP Reduction Array and Custum Reductions</a></h2>
<div class="box" id="OpenMPReductionArray" style="display: none;">
<p align="center">[ <a href="howto/OpenMP_reduction_array.zip">OpenMP_reduction_array.zip</a> ]</p>
<p>OpenMP is a great tool to parallelize code on your CPU. Reduction variables allow one to calculate things
such as the sum, product, maximum, or minimum of an array in parallel. Unfortunately, OpenMP only supports
doing reduction of an array into a scalar value. If, for example, each element in the array belongs to a
cluster, and you want to find the resulting sum of the array for each cluster, you are sore out of luck
with OpenMP in C++. Apparently, you can do it in Fortran, but how many people do you know that use Fortran?</p>

<p>To get around this problem, you typically create a separate reduction array for each thread.
Then, in your OpenMP parallel for loop, you restrict each thread to update their own copy of the reduction
array. After the for loop, you combine all the different copies of the reduction arrays into one.
This can be a pain to code up. The reduction_array class provided here makes this process easier. Also, if
for some reason you have to define your own reduction function that OpenMP does not support, this class
has the functionality to do that.</p>

<p>The interface is pretty simple. Assume we have an array of length N and a reduction array of length D
and want to do an addition (multiplication, minimization, and maximization have built-in support, but
users can also specify their own function with a simple function pointer). In sequential code, this
would look like
<pre align="left"><code class="prettyprint">// allocate the array for total reduction
double* output = new double[D];
memset(output,0,sizeof(double)*D);
for (int i=0; i&lt;N; i++)
{
   // assume "x" should be put into the array at index "d"
   output[d] += x;
}
// do some other stuff here...
delete[] output;
</code></pre>

The parallelized code using OpenMP and reduction_array looks like:
<pre align="left"><code class="prettyprint">// allocate the reduction_array variable
reduction_array&lt;double&gt; my_arr(numThreads,D,0);
#pragma omp parallel num_threads(numThreads)
{
   #pragma omp for
   for (int i=0; i&lt;N; i++)
   {
      // assume "x" should be put into the array at index "d"
      total_parallel.reduce_add(omp_get_thread_num(), d, x);
   }
}
// accumulate the reductions after the parfor loop
double* output = total_parallel.final_reduce_add();
// do some other stuff here...
</code></pre>

In contrast, the parallelized code using OpenMP *without* reduction_array looks like:
<pre align="left"><code class="prettyprint">// allocate dynamic arrays for each thread
double** my_arr = new double*[numThreads];
for (int t=0 t&lt;numThreads; t++)
{
   my_arr[t] = new double[D];
   memset(my_arr,0,sizeof(double)*D);
}
#pragma omp parallel num_threads(numThreads)
{
   #pragma omp for
   for (int i=0; i&lt;N; i++)
   {
      // assume "x" should be put into the array at index "d"
      my_arr[omp_get_thread_num()][d] += x;
   }
}

// allocate the array for total reduction
double* output = new double[D];

// accumulate the reductions after the parfor loop
for (int d=0; d&lt;D; d++)
{
   output[d] = 0;
   for (int t=0; t&lt;numThreads; t++)
      output[d] += my_arr[t][d];
}
// do some other stuff here...
delete[] output;
</code></pre>
This is clearly much more cumbersome. Can you imagine what it would look like with multiple reduction arrays of different lengths?!
Anyway, see the included demo script for more information. Good luck, and happy parallelizing!
</div>

<!------------------------------------------------------------------------------------------------>

<h2><a href="javascript:show('KateHighlight')"><font face="Courier New"><span id="KateHighlight_link">+</span> </font>Editing kate Highlighting Rules</a></h2>
<div class="box" id="KateHighlight" style="display: none;">
<ul>
   <li>Locate the file that tells us where to find the syntax highlighting files: $HOME/.kde/share/config/katesyntaxhighlightingrc</li>
   <li>Edit the syntax file (e.g. /usr/share/apps/katepart/syntax/cpp.xml). Don't forget to make a backup if you want!</li>
</ul>
</div>

<!------------------------------------------------------------------------------------------------>

<h2><a href="javascript:show('WebIcons')"><font face="Courier New"><span id="WebIcons_link">+</span> </font>Website Design Stuff</a></h2>
<div class="box" id="WebIcons" style="display: none;">
<ul>
   <li>This is a great site to get free icon images that can be used in your website designs: <a href='http://www.iconspedia.com/'>http://www.iconspedia.com/</a></li>
   <li>Good JS RegEx references: <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace">replace</a> <a href="https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions">general</a></li>
</ul>
</div>

<!------------------------------------------------------------------------------------------------>

<h2><a href="javascript:show('MEXTutorial')"><font face="Courier New"><span id="MEXTutorial_link">+</span> </font>MEX Tutorial (Programming C++ in Matlab)</a></h2>
<div class="box" id="MEXTutorial" style="display: none;">
<p align="center">[ <a href="howto/MEX/helperMEX.h">helperMEX.h</a> ] &nbsp; [ <a href="howto/MEX/addMatrices.cpp">addMatrices.cpp</a> ]</p>
Matlab is really great at vector operations, but it's painfully slow if you have big nested for loops or if you really just can't avoid looping through vectors.  These are situations when C++ can come to the rescue.  I struggled with programming C++ for Matlab back in undergrad because of the lack of a good tutorials.  I've included two files here that are no where near what a good tutorial would look like, but they should get you quickly started on programming MEX files.  Download both, and open up addMatrices.cpp for a quick demo on how to add two matrices.
</p><p>
Mathworks has a more complete reference guide here: <a href="http://www.mathworks.com/support/tech-notes/1600/1605.html" target="BLANK">http://www.mathworks.com/support/tech-notes/1600/1605.html</a>.
</p>
</div>

<!------------------------------------------------------------------------------------------------>

<h2><a href="javascript:show('RenamerProgram')"><font face="Courier New"><span id="RenamerProgram_link">+</span> </font>Batch Rename Files</a></h2>
<div class="box" id="RenamerProgram" style="display: none;">
<p align="center">[ <a href="howto/Renamer_Program.zip">Windows Install</a> ] &nbsp; [ <a href="howto/Renamer_Program_Source.zip">Source Code</a> ]</p>
I wrote this application to rename a bunch of files at once (especially useful for pictures). It can add a constant string or subtract a number of characters from the beginning or end of all files, or replace a substring within all files. Additionally, one can use "####" to include a counter.</p>
<br><br><p align="center"><img src="howto/Renamer_Program.png" width=99%>
</div>

<!------------------------------------------------------------------------------------------------>

<h2><a href="javascript:show('cecho')"><font face="Courier New"><span id="cecho_link">+</span> </font>Colored Echo in Bash</a></h2>
<div class="box" id="cecho" style="display: none;">
<p align="center">[ <a href="howto/cecho.sh">Linux-Bash Download</a> ]</p>
I use the following bash script to echo in colored text for Bash (in my /usr/bin/ directory). Some of this was taken from somewhere, but I don&rsquo;t remember where anymore :
<pre align="center"><code class="prettyprint">#!/bin/bash
# cecho.sh Echoing text messages in color.
blue='\e[01;34m'
green='\e[01;32m'
cyan='\e[01;36m'
red='\e[01;31m'
yellow='\e[01;33m'
white='\e[01;37m'
none='\e[00m'

if [ "$2" = "b" ]; then
color=$blue
fi
if [ "$2" = "g" ]; then
color=$green
fi
if [ "$2" = "c" ]; then
color=$cyan
fi
if [ "$2" = "r" ]; then
color=$red
fi
if [ "$2" = "y" ]; then
color=$yellow
fi
if [ "$2" = "w" ]; then
color=$white
fi</code></pre>
</div>

<!------------------------------------------------------------------------------------------------>

<h2><a href="javascript:show('conspirator')"><font face="Courier New"><span id="conspirator_link">+</span> </font>Fast Poster Printing on CSAIL&rsquo;s Conspirator</a></h2>
<div class="box" id="conspirator" style="display: none;">
<h3><u>Method 1 - Office 2007</u></h3>
The best "fast" method I have for printing to conspirator is the following
<ol>
   <li>Make a poster in Powerpoint 2007</li>
   <li>Using Microsoft&rsquo;s "Save as PDF" Add-in (found <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=f1fc413c-6d89-4f15-991b-63b07ba5f2e5&displaylang=en">here</a>), save the poster to a PDF</li>
   <li>Print to the correct size</li>
</ol>
Not only does the saved PDF have really small size (and more accurate graphics), it prints really fast!  For a 42"x56" poster with tons of graphics, it takes only a few minutes for the job to spool and start printing.
<br><br>

<h3><u>Method 2 - Older Microsoft Offices</u></h3>
The previous "fast" method I had for printing to conspirator was the following
<ol>
   <li>Make a poster in Powerpoint</li>
   <li>Print to PDF using CutePDFWriter (I tried Adobe&rsquo;s printer, but it has worse results</li>
   <li>Import the PDF into Photoshop, and safe as a new PDF</li>
   <li>Open the new PDF in Acrobat and print to the correct size</li>
</ol>
This method started printing (i.e. the spooling period) after about 3 minutes for a 42&rdquo;x56&rdquo; poster with tons of graphics.  There were sometimes some unwanted JPEG artifacts on edges, but nothing too drastic.
</div>













<?php include("inc/footer.php"); ?>
