The Android NDK
Let's begin with a look at the Android NDK and how it can be used for enhancing the Android platform. While the Android SDK provides a very rich programming environment, the Android NDK broadens the horizons and can speed up the delivery of desired functionality by bringing in existing source code, some of which may be proprietary and some of which may be open source code.
The NDK is a software release available as a free download from the Android website. The NDK includes all the components necessary to incorporate functionality written in C into an Android application. The initial release of the NDK offered only the most primitive of functionality with significant constraints. With each successive release, the NDK has expanded its capabilities. As of r5 of the NDK application, authors can write a significant portion of an application directly in C, including user interface and event-handling capability. The features enabling the image handling functionality demonstrated here were introduced with the r4b version of the NDK.
Two common uses of the NDK are to increase application performance and to leverage existing C code by porting it to Android. Let's look first at performance improvement. Writing code in C does not guarantee a significant increase in performance. In fact, poorly written native code can actually slow down an application when compared to a well-written Java application. Application performance improvements are available when carefully crafted functions written in C are leveraged to perform memory-based or computationally intensive operations like those demonstrated in this tutorial. In particular, algorithms that leverage pointer arithmetic are particularly ripe for use with the NDK. The second common use case for the NDK is to port an existing body of C code written for another platform, such as Linux®. This tutorial demonstrates the NDK in a manner that highlights the performance and the re-use cases.
The NDK contains a compiler and build scripts, allowing you to focus on the C source files and leave the build magic to the NDK installation. The NDK build process is easily incorporated into the Eclipse development environment, which is demonstrated in the section on Customizing Eclipse.
Before jumping into the application itself, let's take a brief detour to discuss some fundamentals of digital image processing.
An enjoyable aspect of modern computer technology is the advent and ubiquity of digital photography. There is more to digital photography than simply catching your kid doing something cute. Digital images are found everywhere from candid cellphone shots to high-end wedding albums, deep-space images, and numerous other applications. Digital images are easy to capture, exchange, and even alter. Modifying a digital image is of interest to us here and represents the core functionality of the tutorial's sample application.
Digital image manipulation occurs in myriad ways, including but not limited to the following operations:
- Cropping — Extracting a portion of an image
- Scaling — Changing the size of an image
- Rotating — Changing the orientation of an image
- Conversion — Converting from one format to another
- Sampling — Changing the density of an image
- Mixing/Morphing — Changing the appearance of an image
- Filtering — Extracting elements of an image, such as colors or frequencies
- Edge detection — Used for machine vision applications to identify objects within an image
- Compressing — Reducing the storage size of an image
- Enhancing an image through pixel operations:
- Histogram equalization
Some of these operations are performed on a pixel-by-pixel basis, while others involve matrix math to work on small sections of the image at a time. Regardless of the operations, all image processing algorithms involve working with raw image data. This tutorial demonstrates the use of pixel and matrix operations in the C programming language, running on an Android device.