April 25, 2013

Creating a binary overlay image with the gimp

I saw a cool image at the Last Ninja Archives a couple of weeks ago. I got so inspired that I had to try to do something similar. First I needed a binary text file, there is an online ASCII to binary converter. So you don't have to write one on your own.

Looking at the Last Ninja Archive kinda brought me back in time. So I decided to get a Commodore 64 font. To install the font on OSX use the font book tool. In LinuxMint (or Ubuntu) run the command:

# gksudo "nautilus"
 
Then, click the install font button in the top right corner of nautilus. This should add the font to all the needed places. Here is some additional help for installing fonts in linux and some for adding fonts to the gimp windows version. (I found that I also needed to reboot the window$ machine to see the font (what a surprice!! :D))

The look I'm interested in is an overlay of transparent binary numbers allowing you to see The last Ninja through the digits. Also, the image should look like it is running on an old monitor, filled with scan lines.

To create the image you'll need to know how to use layers, the color select and fill tool. Before you start take some time to tune in on slayradio and then fire up the Gimp and paint away.

First, create the video overlay

  1. Open the source image.
  2. Press 'command' d to duplicate the image.
  3. Select: filters | distorts | video.
  4. Keep the default settings in the video dialog, perfect old scanline look.
  5. Press ok and see the result.
  6. Save the image so you dont loose it.

Second, create the binary text

  1. Goto ASCII2BinaryConverter.
  2. Paste a portion of ASCII encoded text and press convert.
  3. Copy the converted binary text.
Remember you'll need plenty of 1 and 0's as you have to cover the image widt and height. The numbers of characters in row is: image width/font size. So, row characters = 558px/10px ≈ 55 characters.

The number of character rows are: image height/font size. Which adds up to 312px/10px = 31 rows.
which is characters * rows = 55 * 31 = 1705 characters.

That means you'll need to convert at least num charecters/binary encoding, which is 8bits. Thats: 1705/8 ≈ 214 characters all in all. Then you'll need a few more just to align the binary text nicely with in the image.

Third, creating the binary text

  1. Create a new image in the same size as the source image with white background color.
  2. Select the text tool and cover the whole image area.
  3. Choose: Commodore font size 10, un-tick anti aliasing, and paste all your binary text.
  4. Color select the binary text, using the color selector tool, un-tick no feather.
  5. Copy the selection from your binary text image.
  6. Paste as new layer on the white image.

Fourth, create the binary mask

  1. Select color the pasted text using color select. (if it is not selected after paste).
  2. Merge layer down.
  3. Invert selection.
  4. Select: menu item edit and fill with foreground color.
  5. Invert selection.
  6. Cut the letters to get a black drawing with transparent characters.
  7. Select none.
  8. Save the binary mask image.

Fifth, we'll add the binary mask to the scan line image

  1. Open the scan line image.
  2. Open the binary mask image.
  3. Color select the black part of the binary mask.
  4. Cut the mask.
  5. Paste as new layer on the scan line image.
  6. Save your image.

If you like, you can now invert the selection and cut only the zeros and ones containing the parts of the original image. This can be pastes into a new image with transparent background, to create an image that will work on other than black background. Remember black ninja's are the coolest.



Here's an image based on the original C= graphics, screenshot from vice.

April 22, 2013

Its a kind of Magic ... Imagemagick

Spring is here, which means its time to dust off the old website.

First you should update your Firefox browser with some small cool web design utilities: Firefox webdeveloper addons And then, you need to install imagemagick. This is the coolest image tool I have seen so far!

# sudo aptitude install imagemagick

And then you should have close look at all the cool scripts that Fred has already done on your behalf. Use Fred's Imagemagick scripts.

I have a bunch of social network sites that I'd like my main web site to link to. The construction is already at my site. But, I have no neat icons :(

I looked at the some different ones, fund the ones I (for some reason liked) and decided to do a simple glow effect to the icon. Fred's got a script just for this called glow.

Download the glow script and place it in your ~/bin folder if you don't already have a personalised bin folder for all your crazy stuff create one.

# mkdir ~/bin

Update your .bashrc to set the path, add the line: PATH=~/bin:$PATH using your favourite editor or:

# perl -p -i -e "s|^(PATH).*$|$1=~/bin:$PATH|g" ~/.bashrc

you're ready to go, test by issuing a

# glow

This should give you the glow scripts usage text. Once that is setup you can use this simple script or create your own.

add_glow (source)
#!/bin/sh

USAGE="$0 [source_dir] [dest_dir] all images from source will be substituded to <imagename>.glow.<imageextention>"
IMG_SRC_DIR=$1
IMG_DEST_DIR=$2

if [ -n "$IMG_SRC_DIR" -a -d "$IMG_SRC_DIR" ]; then
    if [ -n "$IMG_SRC_DIR" -a -d "$IMG_DEST_DIR" ]; then
    for IMG in $(ls $IMG_SRC_DIR); do
        DEST_IMG=$(basename $IMG)
        echo "prosessing $IMG"
        glow -a 1.7 -s 16 $IMG $IMG_DEST_DIR/${DEST_IMG%%.png}.glow.png
    done
    exit 0

    else
    echo "No dest directory"
    fi
else
    echo "No source directory"
fi
echo "$USAGE"
exit 1

place that script in the ~/bin folder as well not you can create a folder for the source images and one for the destination images, and tun the add_glow script

# mkdir ~/glow_pngs

Assuming your source images are in ~/normal_pngs

# add_glow ~/normal_pngs ~/glow_pngs

Voila, now you have glow effect on all your source images.

Next off you'll have to use them on your web page. Here's a quick img tag modification that can change image when the mouse is over the image, and back when the mouse leaves the image.

<img src="icontexto-inside-xfit.png" onmouseover="this.src='icontexto-inside-xfit.glow.png'" onmouseout="this.src='icontexto-inside-xfit.png'" alt="Xfit" width="32" height="32"/>

You can check out the result @www.dideriksen.org

Counting a files

This'll be the third time I'm listing something about find. This time I'm using it to count the number of files in a directory. At work I needed to know if a specific folder would break one of our tools because it might have too many files. (Long story)

 
# find YOURDIR -type f ¦ wc -l 

Its the wc that does the actual counting by adding up each line from find.

April 16, 2013

sudo echo??? Hey isn't that an 80 band?

If you ever need to echo stuff into a file, and I'll bet you do! tee's the answer. Here's a snissle:


#echo "Acquire::http::Proxy \"$http_proxy\";" | sudo tee -a /etc/apt/apt.conf.d/70debconf

Updates the aptitude settings with the proxy server from your environment. Oh and removing it afterwards is done with:

#sudo perl -p -i -e "s/^Acquire.*$//g" /etc/apt/apt.conf.d/70debconf

Have fun ;)

April 12, 2013

C++ posix thread wrapping

Here's a short story on using posix threads and getting them into c++ objects if that's your preferred language. Tutorial link to the C version. Threads info

The following source is my two cents on C++ posix threads. There are two files: thread.h and thread.cpp. They create an inheritable thread class that you can use to implement your specialised threads.

Now if you use the code, remember you'll have to link it against the pthread library (libpthread) do this by adding option -lpthread to your compiler (or in your Makefile| Makefile.am).

Here's some implementation details on how the thread code can be used. It's  available in the incoming_connection.h & incoming_connection.cpp files. But you can't expect to build these directly as there are some files missing.

These files are just for showing the intent of the thread implementation. Have fun! And yes you should sometimes just read code, it's like reading a good book!

April 05, 2013

Setting up a debian repository with reprepro

So you're rolling your own?! Let's distribute em to the masses then, you know they want them! I'm using reprepro for distribution, installing is straight forward just check the guide in the link.

The repository I have is located remotely on a server machine, where I can only ssh and scp packages to either my home, or the repository folder. This setup has some drawbacks when scripting for repository update compared to a local database.

When working using ssh and tools like this you should setup ssh_agent as it will be really really help full. So, unless you get off on typing your password to the remote machine all the time, configure ssh agent.

The repository database is present on the repository_server in the normal www directory. /var/www/repository. This is the directory you should use when setting up the repository. The repository user have only write access to the users home folder and the repository server.

repository_user@repository_server:/home/repository_user & repository_user@repository_server:/var/www/repository


The setup for including packages to the remote repository, is that the binary packages  must first be uploaded to the repository server and secondly included in the repository. The upload part is done on your local machine and the inclusion in done remotely.

The first copy part is straight forward using scp

scp package repository_user@repository_server:/home/repository_user

The second part with the package inclusion must be run on the repository_server, you do this by directing the script to the ssh connection:

ssh repository_user@repository_server << ENDSSH
  <your script>
ENDSSH

The following listing is the script that I use to maintain my repository

#!/bin/bash

function die {
    echo $1;
    echo;
    exit 1;
}

package=$1;
repro_dir=$2
user=<repository_user>
host=<repository_server>
ext=deb;
backup_dir=old_packages;
home_dir=/home/$user;

test -z "$package" && test ! -f "$package" && die "No package!";
scp $package $user@$host:$home_dir;
ssh $user@$host << ENDSSH;
test ! -f $package && echo "No package!" && exit 1;
test ! -d $repro_dir && echo "No repository dir!" && exit 1;
test ! -d $backup_dir && mkdir -p $backup_dir;
find $repro_dir -name "${package%.$ext}*.$ext" -exec cp -vf {} $home_dir/$backup_dir \;
md5sum ~/$package > /dev/null;
cd $repro_dir;
reprepro includedeb mk3 ~/$package;
ENDSSH

April 04, 2013

Company proxy configuration

If you're running your Linux box @work you most likely have the issue of using the company proxy server. I know I do :'O With the current Ubuntu / Linuxmint there's still a couple of places you'll need to configure before you have your proxy settings up and running.

The worst part is when you have to update your windows password. Cause you'll have to update all the configuration files on your box to be able to connect to the proxy with your new password. And a password change happens quite often these days. Here's a small note on the strategy: proxy  to get your theory on the same page.

I installed cntml, a small local proxy tool that can handle the proxy connection stuff for you windows servers. cntml worked better for me, that's the only reason for not running ntml!

#sudo aptitude install cntml

Then, you'll have to configure the buggar, cntml have a great guide for this, so follow that one, and then you'll have to setup you browser and command line utilities and most likely your apt-conf as well.

As an system update can be run through pkexec and gksudo, which is what you do when you update using the GUI tools for updating. You'll have to setup the proxy server for the apt tool. This is because at writing pkexec and gksudo does not carry the cmd line proxy settings, which will cause both of these tools to fail the update.

First, You'll have to add a file to your apt.conf.d with the proxy, I just created a file called 98proxy in the /etc/apt/apt.conf.d/ directory, containing the cntlm proxy settings.


# echo 'Acquire::http::Proxy "http://localhost:3128";' | sudo tee /etc/apt/apt.conf.d/98proxy > /dev/null



Or edit the file by using you favorite editor.


Second, you'll have to setup your command line proxy, either locally via ~/.bashrc or as I did for the machine with a proxy.sh script in the /etc/profile.d/ dir You can copy the following to either you ~/.bashrc file or create a proxy script file. i.e.

# sudo touch /etc/profile.d/proxy
# sudo nano /etc/profile.d/proxy

Then copy and paste this:

#!/bin/bash
export http_proxy="http://localhost:3128"

export http_proxy="https://localhost:3128"
export http_proxy="ftp://localhost:3128"
export no_proxy="localhost,<your domain>"



You'll also have to edit the sudoers file to get your environment kept, you you ever need to run stuff as root!

# sudo nano /etc/sudoers

Paste the following to keep your settings (The Display is not for the proxy but you might like to haveroot windows displayed on your gui aswell)

Defaults     env_keep = "DISPLAY"
Defaults     env_keep += "proxy"
Defaults     env_keep += "http_proxy"
Defaults     env_keep += "https_proxy"
Defaults     env_keep += "ftp_proxy"
Defaults     env_keep += "no_proxy"



Third, you'll need to use the proxy in all you browser, spotify etc. Remember to set the no_proxy stuff aswell, as you may have internal domain look up issues. Change firefoxes settings via the gui, edit|preferences.


I rolled a small company-proxy-settings package containing the changes I mention here, just use it, on your own account. The package has an additional script that you may want. It is called cntml_config and you can run it every time you'll have to change your company password!

Script listing:

# sudo touch /usr/bin/cntlm_config
# sudo nano /usr/bin/cntlm_config 

Paste the contents:

#!/bin/bash

config=/etc/cntlm.conf

domain=<your domain> 


sudo service cntlm stop

echo -n "Enter your username for windows: ";
read user;

unset pass;
prompt="Enter password for windows: "
while IFS= read -p "$prompt" -r -s -n 1 char
do
    if [[ $char == $'\0' ]]
    then
        break
    fi
    prompt='*'
    pass+="$char"
done
echo "";

sudo perl -p -i -e "s|(Username\t).*$|Username\t$user|g" $config;
sudo perl -p -i -e "s|(Password\t).*$|Password\t$pass|g" $config;
sudo perl -p -i -e "s|(Domain\t).*$|Domain\t\t$domain|g" $config;
sudo perl -p -i -e "s/10.0.0.41:8080/<your proxy>/g" $config;
sudo perl -p -i -e "s/Proxy.*10[.]0[.]0[.]42:8080//g" $config;

sudo chmod 0600 $config;
sudo service cntlm start


replace the <your domain> and <your proxy> in the script with your company settings and you should be good to go. Modify and use at your own risk!