Tuesday, April 23, 2013

Multiple binaries in a single Eclipse CDT project

Sometimes it is convenient to have a single Eclipse project that produces multiple executables. An example is a set of tools that share some base code like a set of image manipulation tools that share some common image loading / saving code.  In a situation like this, the source code has multiple source files that contain a main() function. The standard way to do this is to create a makefile eclipse project and edit the makefile so that you have a target for each executable:

binary_1: main_1.cpp shared_code.cpp
       gcc -o  $@  $^

binary_2: main_2.cpp shared_code.cpp
       gcc -o  $@  $^

If for some reason you want to use a managed eclipse project (where the makefile is automatically generated) do the following:
  1. Create a managed project (File > New  C++ Project > Executable)
  2. Add the source code containing multiple main() functions
  3. Go to Project > Properties > C/C++ Build > Manage Configurations...
  4. Make a build configuration for each executable and name it appropriately (you can clone existing configurations like Debug and Release).
  5. From the project explorer, right click on each source file that contains a main() function > Resource Configurations > Exclude from Build and exclude all build configurations except the one that builds the executable with this main() function
  6. All other code is included in all build configurations by default. You may need to change this depending on your application.
  7. You can now build an executable for each main function by going to Project > Build Configurations > Set Active , Project > Build Project
To debug each executable:
  1. Build all binaries (as explained above) 
  2. Go to Run > Debug Configurations.
  3. Create a configuration for each binary
  4. For each configuration set the correct Build Configuration and C/C++ Application (this is the binary). 
  5. Use Search Project... to locate the binaries easily 

Thursday, April 11, 2013

Pipe to clipboard


Copy the output of a command into clipboard:
echo adafasdfa | xclip

Useful for pasting command line output into text editors, spreadsheets etc.
xclip -o | grep name

Useful for pasting large selections (etc from a browser). Equivalent to middle clicking the terminal (but less messy).

Wednesday, April 10, 2013

Compile using pkg-config

pkg-config makes it easier to compile your source when you need to link libraries. For example, building a Qt application that has OpenGL support requires the following build command:

g++ source.cpp -DQT_SHARED -I/usr/include/qt4 -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtOpenGL -I/usr/include/qt4/QtGui  -lQtOpenGL -lQtGui -lQtCore  

With pkg-config that becomes:

g++ source.cpp `pkg-config --libs --cflags QtCore QtOpenGL `

Note the use of back-quotes  ``  to do in-line substitution. 

pkg-config can be integrated into eclipse through a pluggin and is also supported by qmake. Just add the following to your .pro file:

CONFIG += link_pkgconfig
PKGCONFIG +=  name_of_library

Monday, April 8, 2013

Make an image montage using ImageMagick

The following makes a 1x10 vertical montage of all .png images in the current folder. Before adding to the montage, images are cropped.

$ montage -geometry +0+0 -tile 1x10  -crop 170x70+115+125 *png montage.jpg

-geometry controls the border arround each sub-image in the montage. +0+0 means no border.

How to decode the crop string "170x70+115+125" :

170x70: new size of the cropped image
+115+125: Pixel (0,0) of the cropped image will be pixel (115,125) of the old one. Pixel (170,70) of the cropped image is pixel (170+115, 70+125) of the old one.

Convert HDR files to EXR and tonemap using pfstools

Convert all .hdr files to .exr using pfstools. This will replace the hdr extension to exr.

$ for img in *.hdr; do pfsin ${img} | pfsout ${img%%.hdr}.exr; done


Similarly, tonemap multiple HDR files using a variety of tonemapping operations:

$ for img in *.exr; do pfsin ${img} | pfstmo_mantiuk08|pfsout ${img%%.exr}.ppm; done

pfstmo_mantiuk08 is one of many available tonemap operations available in pfstools.

Mounting remote folders using ssh and SAMBA


Two ways to mount your remote folders. Use sshfs over unreliable network. Get uid, gid by running id on linux.

mount -t cifs //samba_server/folder ~/yoho/ -o user=username,domain=COMPANY,uid=xxxx'

This will mount remote /folder to local folder ~/yoho.

sshfs -o workaround=rename -o reconnect -o idmap=user -o uid=xxxx -o gid=xxxx -o allow_other username@computer_with_ssh_access.company.com: ~/yuhu/

This will mount the home folder of user username at local folder ~/yuhu.


Record webcam video

vlc v4l2:// :v4l2-dev=/dev/video0 :v4l2-width=320 :v4l2-height=240  --sout '#transcode{vcodec=mp4v,vb=3072,scale=1,acodec=mpga,ab=192,channels=2}:duplicate{dst=display,dst=std{access=file,mux=mp4,dst="/tmp/test.mpg"}}'

Webcam is typically /dev/video0 in linux

Grab video frames using VLC

The following grabs every fifth frame starting from second 0:20 and ending on 0:30 (10 seconds). Frames are saved in ./out/ . The number of frames captured depends on the number of frames per second of the video.

vlc video.avi --video-filter=scene --vout=dummy --start-time=20 --stop-time=30 --scene-ratio=5 --scene-prefix=frame --scene-path=./out vlc://quit

How to install pfstools Matlab support (HDR, EXR)

Pfstools is a set of command line programs for reading, writing and manipulating high-dynamic range (HDR) images and video frames. This is how to install for Matlab:
  1. Get pfstools http://pfstools.sourceforge.net/
  2. ../configure --prefix=$HOME/local --with-mex=/path/to/matlab/bin/mex (this installs in home folder)
  3. make
  4. make install
  5. From Matlab, File, Set Path, set it to $HOME/local//share/pfstools/pfstools_matlab/
Use --prefix=/path/ to istall to custom path. By default it installs to /usr/local/
Note: Most linux distros have a pfstools package. if you don't need Matlab support install that instead.