Solarian Programmer

My programming ramblings

C++ reading and writing BMP images

Posted on November 19, 2018 by Sol

In this article, I will show you how to implement a BMP image loader from scratch in C++. BMP is one of the oldest image formats on the Windows platform and it is supported on most other operating systems. BMP can store two-dimensional raster images with optional compression and transparency. In this article, we will implement a simplified version of the BMP format specification that will support only 24 and 32 bits depth images in the BGR and BGRA color spaces. To make our life simpler we can also ignore the, optional, compression component.

Even if you don’t plan to use BMP images, it is still a useful programming exercise to write a BMP reader/writer in C++.

From a programming point of view, a BMP file is a binary file in the little-endian format. For our purposes, we can divide a BMP image in four regions:

  • file header - all BMP images starts with a five elements file header. This has information about the file type, file size and location of the pixel data.
  • bitmap header - also named the info header. This has information about the width/height of the image, bits depth and so on.
  • color header - contains informations about the color space and bit masks
  • pixel data.

►  Continue reading

Installing OpenJDK 11 on macOS Mojave

Posted on September 28, 2018 by Sol

This is a short note about getting started with Java 11 on macOS. As you probably know, starting with Java 11 there was a big change in the license under which the official Oracle JDK is provided. In short, you need to buy a license from Oracle if you want to use the official JDK in a commercial setting. As far as I know, using Oracle’s JDK on your private computer for testing and learning purposes is allowed.

That being said, for most users OpenJDK is the new JDK of choice, it is provided under an open source license and you don’t need to pay for using it.

►  Continue reading

C++17 constexpr generation of a FizzBuzz solution

Posted on September 23, 2018 by Sol

The famous FizzBuzz interview question asks to print all numbers from 1 to 100. If a number is divisible with 3 print fizz, if the number is divisible with 5 print buzz and if the number is divisible with both 3 and 5 print fizzbuzz. Writing a program for the above problem is trivial. A more interesting problem is to generate the solution for the FizzBuzz problem at compile time.

In C++ we use the constexpr specifier for a variable or a function that needs to be evaluated at compile time.

►  Continue reading

Install Ruby 2.5 on macOS, Windows 10 and Ubuntu 18.04

Posted on September 22, 2018 by Sol

In this article I will show you how to install the latest stable version of Ruby, which is 2.5.1 at the time of this writing, on macOS, Windows 10 and Ubuntu 18.04.

►  Continue reading

How to create a .NET Core F# application and generate executables for multiple operating systems

Posted on August 13, 2018 by Sol

In this article I will show you how to create a simple F# console application that runs on .NET Core and how to generate executables for various operating systems.

Start by downloading the .NET Core SDK for your operating system from Run the installer and accept the default settings.

►  Continue reading

Compiling Boost with GCC or Clang on macOS

Posted on August 7, 2018 by Sol

In this article I will show you how to build the Boost libraries under macOS with GCC 8 or Clang. Once the libraries are installed, we’ll test the build with a short demo of using Boost Filesystem.

First, you will need to download the latest stable version of Boost, I will use version 1.68.0. Extract the archive and open a Terminal in the Boost folder.

►  Continue reading

Building GCC as a cross compiler for Raspberry Pi

Posted on May 6, 2018 by Sol

In this article, I will show you how to build GCC 8 as a cross compiler for Raspberry Pi. A cross compiler is a compiler that runs on an operating system and produces executables for another. This is really useful when you want to use your beefy computer to build a library or other large piece of code for Raspberry Pi. As a practical example, at the end of the article, I will show you how to use the cross compiler to build GCC itself as a native Raspberry Pi application.

Part of this article is a compilation of what I’ve learned reading other people posts. Here is a list of the sources I’ve used:

From the above list, the first article is the one that is the most complete and, if you follow it, you end up with a cross compiler that partially works. To be fair, the article wasn’t written for Raspberry Pi. I recommend that you read it if you want to see a more in depth explanation of certain steps of the process.

►  Continue reading

Install NumPy, SciPy, Matplotlib and OpenCV for Python 3 on Ubuntu 18.04

Posted on April 25, 2018 by Sol

This is a short article about installing NumPy, SciPy, Matplotlib and OpenCV on the latest Ubuntu LTS, which at the time of this writing is 18.04. Ubuntu 18.04 comes with Python 3.6.5.

Let’s start by making sure we have an updated system:

1 sudo apt update
2 sudo apt upgrade

►  Continue reading

Raspberry Pi - Install Clang 6 and compile C++17 programs

Posted on April 22, 2018 by Sol

In this article I will show you how to install Clang 6 on your Raspberry Pi system and how to compile C++17 programs. At the time of this writing Raspbian is based on Debian Stretch, which comes with the stable but slightly outdated GCC 6.3 as the default C and C++ compiler. If you prefer to use GCC 7 I wrote an article about installing GCC 7 on Raspberry Pi.

Let’s start the installation process. Open a Terminal and download the official binary of Clang 6 for Raspberry Pi:

1 cd ~
2 wget

►  Continue reading

Python OpenCV - show a video in a Tkinter window

Posted on April 21, 2018 by Sol

In my last tutorial I’ve shown you how to create a minimal Tkinter application: load an image with OpenCV, plot the image on a Tkinter window and apply a blur filter when the user presses a button. Another interesting application is to show a camera feed or an exiting video on a Tkinter window.

Simple Tkinter window

►  Continue reading

Python OpenCV - show an image in a Tkinter window

Posted on April 20, 2018 by Sol

This is a short tutorial about using Tkinter, the default Python GUI library, with OpenCV. On Windows, Tkinter is bundled with the official Python installer. On Linux, you can install Tkinter using your distribution package manager. The situation is a bit more complex on macOS, that comes with Python 2.7 and an old version of Tkinter, at the time of this writing, the easiest path is to install Miniconda Python 3 that comes with the latest Tkinter.

OpenCV includes some rudimentary GUI capabilities, useful if you need to show a video or an image, get the mouse or the keyboard input. But, if you need something more complicated like buttons, drop down lists, menus, labels, text boxes and so on, you need to use a dedicated GUI library like Qt or Tkinter.

In the remaining of this article, I’ll assume that you have Python 3.6, Tkinter 8.6 and OpenCV 3.3 or newer installed on your machine. If you need help to install the above on Windows, macOS or Linux check my previous articles.

►  Continue reading

Writing a minimal x86-64 JIT compiler in C++ - Part 2

Posted on January 12, 2018 by Sol

In my last article, I’ve shown you how to generate the machine code for a function at runtime, copy this code in a part of the memory, marked as executable, and call it from C++. Now, we’ll go the other way around, we’ll call a C++ function from a function generated at runtime. Like before, I assume that you try the code on Linux or macOS.

If you remember from part 1, we’ve started by adding machine code instructions in an std::vector and copying this code to an executable memory page. While this was a fine approach from a didactic point of view, in practice, you will probably want to write the code directly to the executable memory. Here is an example of how I propose to do it:

1     MemoryPages mp;
2     mp.push(0x48); mp.push(0xb8);

The object mp, from the above piece of code, will ask the OS for memory, release this memory when it is not needed and will have some helper member functions that will let us push pieces of machine code to the executable memory. We can also add safety features, e.g. a mechanism to check if we can push more data on the executable memory or if we’ve reached the bounds of the allocated memory pages.

►  Continue reading

Writing a minimal x86-64 JIT compiler in C++ - Part 1

Posted on January 10, 2018 by Sol

In this article, I will show you how to write a minimal, bare-bones, x86-64 JIT compiler in C++ that runs on macOS, Linux and could potentially run on Windows through WSL.

For our purposes, JIT compilation is a technique through which a program generates machine code at runtime, based on the user input. A C++ program is AOT (ahead of time) compiled, which typically means that once the original code was compiled for a particular machine it can’t be changed at runtime (and from a security point of view this is a desirable feature). A simple, useful application, of a C++ JIT compiler is on the fly compilation of a new function that is based on other functions already defined in the original code.

Let’s start with an even simpler example. Write a C++ program that asks the user for his name and generates, at runtime, a function that simply prints a greeting. While not a very practical program (you really don’t need to compile this to a separate function), this example will exemplify how to create and execute code at runtime.

►  Continue reading

C++17 constexpr everything (or as much as the compiler can)

Posted on December 27, 2017 by Sol

During the holidays I did some catch up with CppCon 2017. One of the titles that I had on my to watch list for a few months now was constexpr ALL the Things! by Bean Deane and Jason Turner. Please note that I wrote most of this article before actually watching the presentation.

The title of the presentation made me curious if I can optimize an old piece of code that used a huge 2D array of coefficients as the initial condition for a long calculation. In order to avoid recalculating the big array of coefficients, I used to keep them in a file and simply load the data in memory every time the code was executed. The promise of using a constexpr was that I could avoid keeping two executables (the code that generated the coefficients and the code that did the actual work) and a data file. Replacing everything with a single binary was interesting and could potentially be faster.

In order to test the above, I devised a simpler model - fill an array with data generated at compile time.

►  Continue reading

Clang 7 in a Docker container for C++17 development

Posted on December 14, 2017 by Sol

Updated 25 September 2018

If you want to try the new C++17, using Clang in a Docker container, you are in the right place. Running Clang in a container has the advantage that it is light on resources and won’t mess with your underlying OS. The last point is especially important if your host operating system is macOS, on which it is a really bad idea to directly install a binary Clang other than the one that comes with Xcode. I’ve tested the approach presented in this article on Windows 10, macOS Mojave and Ubuntu Linux.

I assume that you have Docker installed on your machine, if not go to the Docker website and install it. After the installation, open a Terminal or, if you are on Windows, a PowerShell window and check if Docker was properly installed with:

1 docker version

►  Continue reading