monte

monte

Monday, 05 January 2015 15:36

In the light box 2

It has been quite a while since I wrote about how I created a simple light box (Old article). I have recently been using it again and tried to improve the sharpness of the pictures. This was mainly triggered through my all new Canon EOS 60D that I wanted to explore. I came up with the following two pictures:

IMG 1212 making of

IMG 1223 making of

 

This is what I did:

I used the same light box as before without modification. I do not have professional light equipment. I used the ceiling lamp, a stand lamp and a desk lamp. As you can see in the second picture „The little drummer boy.“ the desk lamp created to hard light. I therefore used some plastic for diffusion. I shot the pictures in raw mode. Because of the difficult light setup, I had to adjust the white balance by hand. I used aperture value 11 and a zoom lens to gain a nice depth of field. I aligned the camera vertically for a nice navel perspective. To improve the sharpness of the image I used a tripod,  mirror lock-up and a remote trigger. I have been using the tripod and the remote before but I think using the mirror lock-up is what made the picture tack sharp. Even though my old EOS 400D also had this function, the EOS 60D makes it a lot easier to apply. One just has to turn on the live view. I normally don’t use the live view because it drains to much battery, though.

 

 In short:

  • Try to create soft light with diffusion and reflected light.
  • Aperture 11 (for good dept of field)
  • Use raw mode, especially when working with different light types
  • Use a zoom lens with the maximum zoom
  • Align the camera in the navel perspective
  • Use a tripod (for sharpness)
  • Use mirror lock-up (for sharpness)
  • Use a remote trigger (for sharpness)

 

I have to work a lot with 32bit word aligned data. Unfortunately the hexdump tool included with linux uses byte or short alignment by default. I’ve wondered how to make hexdump to print 32bit words instead including an offset indicator as well as the hexdump typical ascii interpretation.

What I came up with was:

monte@mibix:~$ dd if=/dev/urandom bs=4 count=32 2>/dev/null | hexdump -v -e '"%08_ax: "' -e ' 4/4 "%08x " " |"' -e '16/1 "%_p" "|\n"'
00000000: 4d6ec4eb d5b8921d c3bd306a c0d20d1a |..nM....j0......|
00000010: 228d9a4c 4fd68ea7 6a837903 aca46e9a |L.."...O.y.j.n..|
00000020: 513e32f6 2cbc69db ca18bda0 d74bbe71 |.2>Q.i.,....q.K.|
00000030: b27cfd65 8859a08a db92a090 f81e301d |e.|...Y......0..|
00000040: 029a5e6c 23a3d621 ce35a06d e4b48cbd |l^..!..#m.5.....|
00000050: 2bba6da0 2e0c7f84 536eb6ad 94a4827b |.m.+......nS{...|
00000060: 17699fc1 2231d9d1 43210088 37f97a4c |..i...1"..!CLz.7|
00000070: e474aef3 23483fa7 b7f6ed41 cb01d18a |..t..?H#A.......|monte@mibix:~$ 

The dd command reads the data from a binary file. The block size (bs) is 4 so we read 32bit words. The count is 32 which means we read 128 bytes in total. We pipe stderr to null because we do not want to see the in/out statistic. The stdout output is forwarded to the hexdump command.

We specify the -v option to output all data as is. Otherwise hexdump would shorten equal data. The first -e format parameter tells hexdump to print the offset. The second -e format parameter tells hexump to print 4 times 4 bytes (which is 32bits) in the format with 8 nibbles and trailing zeros. The last -e format tells hexdump to print 16 times one byte as character interpretation. The %_p format will print a „.“ for each non printable character. Note that the last two -e formats print the same data. If you specify all formats with one -e you will get the wrong result.

Thursday, 10 July 2014 10:26

Windows Kernel Mode Wait

Sometimes you have to wait, even when writing a Windows driver. Well, waiting in kernel mode is a bad idea in general but sometimes it is necessary.

Today I stumbled over a code fragment which I know from my experience does not do what the programmer might have expected. The problem is the function KeDelayExecutionThread. KeDelayExecutionThread was used in a wait function which was designed to wait for any time in micro seconds. The value of micro seconds was passed to KeDelayExecutionThread multiplied by ten to generate a wait time in steps of 100 nano seconds.

 

There are two problems in this approach. First is that KeDelayExecutionThread waits for an absolute time event in case of a positive number. Thus to get the requested delay in micro seconds one has to specify a negative value. The second problem here is that KeDelayExecutionThread yields the process which leads to a very inaccurate delay especially for times smaller than about 50 milli seconds.

 

A better kernel wait function would call KeDelayExecutionThread or KeDelayExecutionThread depending on the situaton.

 

Maybe like this :  

wait( ULONG usec )
{
    ULONG ulReturn = 0;
    if( u_sec < 50000 )
    {
        /* KeDelayExecutionThread yields the process */
        /* Waiting times lower than 50ms are not accurate */
        /* Because of this we need to busy wait here */
        KeStallExecutionProcessor( usec );
    }
    else
    {
        LARGE_INTEGER waittime;
        /* specifies the wait time in steps of 100ns */
        /* Negative values specify a relative time offset */
        waittime.QuadPart = -1 * usec * 10;
        if( STATUS_SUCCESS == KeDelayExecutionThread( KernelMode, TRUE, &waittime ) )
            ulReturn = 0;
        else
            ulReturn = 1;
    }
    return ulReturn;
}

 

Monday, 13 January 2014 14:24

Dealing with Ubuntu Unity freezes

Ubuntu is not my preferred Linux distribution but somehow this is what we currently use at work. For some reason the Unity GUI keeps on freezing several times a day. The Linux base system seems not to be affected by this because I can log into the Ctrl-Alt-1 console to restart the X server. I have no 3rd party graphic drivers installed which I could blame for this.

The hangup seems to be related to the OpenGL-Composition and Windowmanagement system (compiz) upon which Unity is based. If I kill compiz from the command line the hangup is resolved but I experienced crashes of programs like gedit. This is very bad especially if this happens during coding.Depending on the Ubuntu version you might be able to resolve the freeze with either „unity –reset“ or „dconf reset -f /org/compiz/

However for work I do not need a fancy 3d desktop and I noticed that you can use the Unity 2d desktop without compiz. Therefore from the log in screen click on the Ubuntu icon next to your name and select Ubuntu 2d.


This might prevent your system from hanging.

I have a sub directory data with multiple jpg picture files. The files are named in a way that they are sorted automatically by the os. In my case this is YYYYMM-xy.jpg where YYYY is the year and MM is the month. This is because for this project I made one picture every month over one year. The sub string -xy is extracted by the Python program and is rendered as text onto each image.

 

files with numbers

 With the following Python script I can combine these eight images into one.

import os
import Image
import ImageFont
import ImageDraw


img_list = []
img_text = []
img_path = "./data/"
width = 176 #684
height = 187 #1024
text_pos_x = 3 #10
text_pos_y = 3 #10
text_size = 10 #50
rows = 2
columns = 0


for file in os.listdir( img_path ):
# Search images
if file.endswith(".jpg"):
# open images
img_list.append( Image.open( img_path + file ) )
# Extract text to render on the image from the file name
img_text.append( file.split("-")[1].split(".")[0] )
# Print info
print 'File:', file, ' => ', img_text[-1]


for image in img_list:
# resize images
image.thumbnail( (width, height) )

columns = len(img_list)/rows

#creates a new empty image, RGB mode
mosaic = Image.new( 'RGB', ( columns * width, rows * height ) )

# Prepare draw and font objects to render text
draw = ImageDraw.Draw(mosaic)
font = ImageFont.truetype("C:/Windows/Fonts/arial.ttf",text_size)

k=0
for j in xrange( 0, rows * height, height ):
for i in xrange( 0, columns * width, width ):
# paste the image at location i,j:
mosaic.paste( img_list[k], (i,j) )
# render text at location i,j:
draw.text((i + text_pos_x, j + text_pos_y), img_text[k], font=font)
# Select next image and text
k = k + 1

# Save image to file
mosaic.save('mosaic.jpg')

 

The Result is written to the file mosaic.jpg. 

mosaic

If you install a Ubuntu Linux it tends to install a big chuck of packages that you probably never need. I think that a useful command to find this big guys is dpigs from the debian-goodies package. This command lists the ten biggest packages installed. Make sure the packages displayed are really necessary or throw them out.

I tried to replace the US keyboard of my wifes Compaq Presario CQ40 with a german keyboard. First I was amazed at how easy the keyboard can be removed. They even printed a keyboard symbol next to the correct screws.

After disassembling everything however I noticed that the screws on the DE keyboard are in different positions :-(

Wy would they do such a thing ?

 

P1040537

P1040542

python utf8

Many programmers including me like short variable names. We consider programming as an art where the source code is the paint and the screen is the canvas. However source code should be written as easy to understand as possible. This is because source code is almost never documented well enough. I believe that well written source code explains itself to the reader through variable, class, object and method names. Of course tricky parts should still be commented.

When looking for descriptive variable names you often end up with very long variable names, which when passed to long function names look rather ugly on the canvas. It came to my mind some years ago how interesting it would be to write software by using languages that can describe words with short characters like Japanese or Chinese. Some days ago I checked out the changes in Python 3 and surprisingly they do now support full Unicode variable names. I instantly upgraded my rasberry pi with python3 which was quite a hassle because python 2.x is still the standard many programs depend on. Once installed I wrote my first Chinese program, a Person class. The class has a constructor as well as the methods walk, run, sit and drink. When drinking you can pass a variable of what kind the person is drinking.

The screen shot above shows the source code in my favorite editor jed. The screen shot below shows the output of the program when executed.

Happy scripting.

I was facing the problem today that I wanted to read the command line arguments for a program from a configuration file. I did also need to check the return value of the command executed.

Consider you have the following configuration file :

monte@raspberrypi ~ $ cat toolconfig
parameter,1,"FIRST",0x100
parameter,2,"SECOND",0x200
parameter,3,"THIRD",0x300

data,x,y
data,a,b

You could then use grep to extract all lines that start with „parameter“

monte@raspberrypi ~ $ grep ^parameter toolconfig
parameter,1,"FIRST",0x100
parameter,2,"SECOND",0x200
parameter,3,"THIRD",0x300

If you pipe the output of grep to awk and tell awk to split the lines by the comma you are half way there.

monte@raspberrypi ~ $ grep ^parameter toolconfig | awk -F',' '{ print " "$1 " " $2 " " $3 " " $4 }'
parameter 1 "FIRST" 0x100
parameter 2 "SECOND" 0x200
parameter 3 "THIRD" 0x300

The next step is to format the call that needs to be executed. In this example we simple execute the echo command. Since the first entry „parameter“ is only for identification we skip it with the next call. For now we do not execute anything but do only display the command.

monte@raspberrypi ~ $ grep ^parameter toolconfig | awk -F',' '{ print "echo -a " $2 " -b " $3 " -c " $4 }'
echo -a 1 -b "FIRST" -c 0x100
echo -a 2 -b "SECOND" -c 0x200
echo -a 3 -b "THIRD" -c 0x300

Many implementations of awk support the system command to execute other commands. The following line will execute the echo command with the options specified in the config file.

monte@raspberrypi ~ $ grep ^parameter toolconfig | awk -F',' '{ system( "echo -a " $2 " -b " $3 " -c " $4 ) }'
-a 1 -b FIRST -c 0x100
-a 2 -b SECOND -c 0x200
-a 3 -b THIRD -c 0x300

Awk works on a line by line basis. This means that the command surrounded by {} is executed for each line in our file (or each line provided by grep here). If we call a complex program with system it might report an error and we need to stop processing the other entries. This can be done by checking the return value of system.

monte@raspberrypi ~ $ grep ^parameter toolconfig | awk -F',' '{ if( system( "echo -a " $2 " -b " $3 " -c " $4 ) != 0 ) exit 1 }'
-a 1 -b FIRST -c 0x100
-a 2 -b SECOND -c 0x200
-a 3 -b THIRD -c 0x300
monte@raspberrypi ~ $ echo $?
0

If the echo command would fail for any of the lines the processing of the left over lines would stop. The return value which can be obtained with $? would be not equal to zero.

Happy scripting.

Friday, 11 January 2013 13:48

In the light box

lightbox

It has been a long time that I wanted to build myself a light box. Today I finally made it ! It didn’t require that much effort and I didn’t need to by anything yet.

 
Basically my light box is an old carton where I cut of the top and one side. On the side I left a border of 3 cm for stability. On the sides I have cut out two windows, again with a border of about 3 cm. The rear wall and the base I covered with the back side of an old poster. The color should be white though. Then I covered the side windows with white backing paper for light diffusion. 
 
Basically you should turn of the room light and install two equal light sources at both of the side windows. Take the picture with a tripod, no flash and a long exposure time. The exposure time in my case was about half a second. I used a remote control to avoid shaking the camera but you can also use the timer function. I didn’t have two equal light sources available this is why the picture is too dark on the right. I have to improve the light but for now I am satisfied.
 
Page 1 of 2