Pimp My Code Part 1 Redux

Xhibit

Not exactly a redux, but very similar to last time:

bIsNotEmpty = false;
if (vNests.GetSize() != 0) {
  if (vNests[0]->vEggs.GetSize() != 0) bIsNotEmpty = true;
}

First of all we don’t actually care about the size, we just care that we have (Or don’t have) a nest with eggs in it. Depending on the container GetSize may or may not be a variable look up. IsEmpty is always a variable lookup:

bIsNotEmpty = false;
if (!vNests.IsEmpty()) {
  if (!vNests[0]->vEggs.IsEmpty()) bIsNotEmpty = true;
}

We can combine this into a single line:

bIsNotEmpty = (!vNests.IsEmpty() && !vNests[0]->vEggs.IsEmpty());

I would use more descriptive variable naming change my logic to use bIsEmpty/!bIsEmpty. Using bIsNot variables is often confusing and leads to harder to read code:

bIsNestWithEggs = (!vNests.IsEmpty() && !vNests[0]->vEggs.IsEmpty());
bIsEmpty = !bIsNestWithEggs;
// Use bIsEmpty and !bIsEmpty from now on

Pimp My Code Part 1

Xhibit

inline bool IsSpecial(const char* szValue)
{
 // Returns true if this is a special value
 if (stricmp(szValue, "MySpecialValue") == 0) {
   return true;
 }
 
 return false;
}

I see this sort of thing all the time. For boolean functions that call boolean functions the if and return statements are usually superflous, we can use the return value of (stricmp(szValue, “MySpecialValue”) == 0) itself:

// Returns true if this is a special value
inline bool IsSpecial(const char* szValue)
{
 return (stricmp(szValue, "MySpecialValue") == 0);
}

If it were up to me I would also use a string class and keep as much of the code as possible in the string “realm” (This makes the code a lot simpler and easier to read):

// Returns true if this is a special value
inline bool IsSpecial(const string& sValue)
{
 return (sValue == "MySpecialValue");
}

Converting Pentax PEF Files to PNG

Ufraw is a fantastic utility to convert raw camera formats. You can install it via (And you may as well get the plugin for gimp while you are at it):

sudo yum install ufraw ufraw-gimp
OR
sudo apt-get install ufraw gimp-ufraw

There is a great tutorial on using ufraw from a bash script here, my only recommendation is converting to PNG but it is entirely up to personal preference.

PNG version:
pef2png.sh

#!/bin/bash
 
if [ ! -d ./processed_images ]; then mkdir ./processed_images; fi;
 
# processes raw files
for f in *.pef;
do
  echo "Processing $f"
  ufraw-batch \
    --wb=camera \
    --exposure=auto \
    --out-type=png \
    --compression=96 \
    --out-path=./processed_images \
    $f
done
 
cd ./processed_images
 
# change the image names
for i in *.png;
do
  mv "$i" "${i/.png}"_r.png;
done
for i in *.png;
do
  mv "$i" "${i/imgp/_igp}";
done

Usage:

# Convert all pef files in the current directory to png
./pef2png.sh