Lesson 04
Simple Image Processing
A crash course on adding images to your applications.
Now that we have everything set up (you should after the first part of Lesson04) we can start. This is the second page
of Lesson 04. If you haven't read the first page, you need to go back.
So, to start our program, download this file and extract it into a new folder in your ~/ directory. What's that you say? You don't know what the ~/ directory is? That's alright; unless you have experience with *nix, you wouldn't have any reason to. Basically, ~/ is synonymous with a user's home directory on a *nix system. So ~/ is equivalent to C:/cygwin/home/yourUserName. So, create a directory there and extract the files you just downloaded into that folder.
Now open up your editor and create a new C source file. Name it "main.c" and save it in the folder you just created. From now on I will refer to this
folder as your project folder, just for redundancy's sake. Insert the standard comment into the top of your new file, stating the purpose of the
program, your name, and the date. This step is (again) optional, but it is a good programming practice to include it.
Simple enough so far, right? Next we'll add our include files. If you recall from Lesson 02, included files allow us to use prewritten and external functions and datatypes without having to include the behind the scenes code that makes them work. Programming with images ups the ante a bit, and we will have to include quite a few files:
Now we'll put in the #define statements, you should already be familiar with the first, but the second will probably throw you for a loop:
That concludes part two of Lesson 04. Continue on to part three.
So, to start our program, download this file and extract it into a new folder in your ~/ directory. What's that you say? You don't know what the ~/ directory is? That's alright; unless you have experience with *nix, you wouldn't have any reason to. Basically, ~/ is synonymous with a user's home directory on a *nix system. So ~/ is equivalent to C:/cygwin/home/yourUserName. So, create a directory there and extract the files you just downloaded into that folder.
/*
My Image Display Program
Author: Brad Dwyer
Date: 12/28/2005
Thanks to Psilocybeing for the base code.
*/
My Image Display Program
Author: Brad Dwyer
Date: 12/28/2005
Thanks to Psilocybeing for the base code.
*/
Simple enough so far, right? Next we'll add our include files. If you recall from Lesson 02, included files allow us to use prewritten and external functions and datatypes without having to include the behind the scenes code that makes them work. Programming with images ups the ante a bit, and we will have to include quite a few files:
#include <pspdisplay.h>
#include <pspctrl.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspgu.h>
#include <png.h>
#include <stdio.h>
#include "graphics.h"
So we have the standard pspdisplay.h, pspkernel.h, and pspdebug.h. And then we have the pspctrl.h file which allows us to read
input from the PSP's keys (as you should remember from Lesson 03). And then we have three files that probably look foreign to you.
The first, pspgu.h gives us access to hardware acceleration, we won't be using any of the functions from this file, but it is used behind the scenes
by graphics.h (which we will discuss shortly). The second, png.h, allows us to manipulate PNG image files. And the third, stdio.h gives
us access to some standard C functions that we will take advantage of. Specifically, we will be using sprintf() to parse strings. And then finally
we have an include that looks a bit different. Rather than using the less-than and greater-than characters, it uses quotes. These quotes signify that it is
included not from the compiler's include directory, but from the project folder. This file, graphics.h, is the one that we downloaded, and it contains
several functions that will make our job a lot easier, including the actual functions that we will use to load and display our image.#include <pspctrl.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspgu.h>
#include <png.h>
#include <stdio.h>
#include "graphics.h"
Now we'll put in the #define statements, you should already be familiar with the first, but the second will probably throw you for a loop:
#define printf pspDebugScreenPrintf
#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
This second #define is actually defining a function; but it is doing it in a shorthand form. This type of definition is called a Macro Function. It
accepts the parameters X and Y, and then will run them through the function, which basically amounts to an if statement. Basically, what this function
will do is return the value of X if it is greater than Y, or the value of Y if X is less than Y. The syntax of a Macro Function is "NAME (TEST ? TRUE : FALSE)"
where NAME is the identifier, TEST is the equivalent of what would go in the parenthesis of an if statement, TRUE is what to return if the TEST result is
true, and FALSE is what to return if the TEST result is false. It is the equivalent of the following function:
#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
//THIS IS AN EXAMPLE; DO NOT ADD IT TO YOUR CODE
int MAX(int X, int Y) {
if(X > Y) {
return X;
} else {
return Y;
}
}
You will likely encounter the abbreviated syntax if you delve into other people's source codes, so it is important to understand how it works. Or at the very
least understand what it does. The advantage of using this syntax over an if/else structure is that the compiler can often optimize this code better, resulting
in slightly faster execution. It's probably insignificant except for those programs that push the hardware to its limits and need that little extra "umph."int MAX(int X, int Y) {
if(X > Y) {
return X;
} else {
return Y;
}
}
That concludes part two of Lesson 04. Continue on to part three.
