Editing Lesson 04 - Simple Image Processing

Jump to navigation Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 8: Line 8:


Before we do anything, we will need to install some new libraries from SVN. What's SVN you ask? Well, SVN is a version management system (it is shorthand for "subversion"). What we will be doing is grabbing some code from online to add more functionality to our compiler. The packages that we will need are zlib and libpng. zlib is a compression library and libpng allows us to work with PNG files (a type of image). To install these libraries, we will need to type the following things into a CYGWIN Bash Shell.
Before we do anything, we will need to install some new libraries from SVN. What's SVN you ask? Well, SVN is a version management system (it is shorthand for "subversion"). What we will be doing is grabbing some code from online to add more functionality to our compiler. The packages that we will need are zlib and libpng. zlib is a compression library and libpng allows us to work with PNG files (a type of image). To install these libraries, we will need to type the following things into a CYGWIN Bash Shell.
<pre>svn checkout svn://svn.pspdev.org/psp/trunk/zlib</pre>
svn checkout svn://svn.pspdev.org/psp/trunk/zlib
"Checkout" is basically SVN's way of saying "download." This will download the zlib source into a folder called "zlib." It will take a minute, a bunch of stuff should scroll down the screen and you should be left back at the "$" for input.
"Checkout" is basically SVN's way of saying "download." This will download the zlib source into a folder called "zlib." It will take a minute, a bunch of stuff should scroll down the screen and you should be left back at the "$" for input.


So now we need to compile this new library, so we'll "cd" into the new folder.
So now we need to compile this new library, so we'll "cd" into the new folder.
<pre>cd zlib</pre>
cd zlib
We are now in the zlib folder, and are ready to compile. So, like any other thing that we want to compile, we just need to type
We are now in the zlib folder, and are ready to compile. So, like any other thing that we want to compile, we just need to type
make
make
Line 18: Line 18:


Now we need to put the resulting files into a place where the compiler can access them. So, we'll install them:
Now we need to put the resulting files into a place where the compiler can access them. So, we'll install them:
<pre>make install</pre>
make install
And BAM! We've installed zlib. Now wasn't that simple?
And BAM! We've installed zlib. Now wasn't that simple?


Now for libpng. We just need to do the same thing, except we'll substitute "libpng" for "zlib."
Now for libpng. We just need to do the same thing, except we'll substitute "libpng" for "zlib."
<pre>cd ..
cd ..
svn checkout svn://svn.pspdev.org/psp/trunk/libpng
svn checkout svn://svn.pspdev.org/psp/trunk/libpng
cd libpng
cd libpng
make
make
make install</pre>
make install
And there we are, ready to use PNG's in our program.
And there we are, ready to use PNG's in our program.


Now we'll just clean up those install files by deleting them. To delete things via a Linux shell (which is what CYGWIN emulates), you use the "rm" command. We don't only want to delete one file, though, we want to delete a whole folder and all of its contents, so we'll add the "-R" modifier to it to signify a recursive removal. Basically this just means we want to delete a folder and everything inside of that folder. We'll also use the "-f" modifier to force the deletion of write-protected files so that we don't have to hit "y" for each file we want to delete. So we'll "cd" back to the parent directory and remove both the "zlib" and "libpng" temporary folders that we just created.
Now we'll just clean up those install files by deleting them. To delete things via a Linux shell (which is what CYGWIN emulates), you use the "rm" command. We don't only want to delete one file, though, we want to delete a whole folder and all of its contents, so we'll add the "-R" modifier to it to signify a recursive removal. Basically this just means we want to delete a folder and everything inside of that folder. We'll also use the "-f" modifier to force the deletion of write-protected files so that we don't have to hit "y" for each file we want to delete. So we'll "cd" back to the parent directory and remove both the "zlib" and "libpng" temporary folders that we just created.
<pre>rm -Rf zlib
rm -Rf zlib
rm -Rf libpng</pre>
rm -Rf libpng
Now that we have that out of the way, we are ready to start programming.
Now that we have that out of the way, we are ready to start programming.


So, to start our program, download this [https://web.archive.org/web/20110705120835/https://www.psp-programming.com/tutorials/c/lesson04.zip 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.
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.
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.


<pre>/*
/*
           My Image Display Program
           My Image Display Program
           Author: Brad Dwyer
           Author: Brad Dwyer
Line 44: Line 44:


           Thanks to Psilocybeing for the base code.
           Thanks to Psilocybeing for the base code.
*/</pre>
*/


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:
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:
<pre>#include <pspdisplay.h>
#include <pspdisplay.h>
#include <pspctrl.h>
#include <pspctrl.h>
#include <pspkernel.h>
#include <pspkernel.h>
Line 54: Line 54:
#include <png.h>
#include <png.h>
#include <stdio.h>
#include <stdio.h>
#include "graphics.h"</pre>
#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.
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.


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:
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:
<pre>#define printf pspDebugScreenPrintf
#define printf pspDebugScreenPrintf
#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))</pre>
#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:
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:
<pre>//THIS IS AN EXAMPLE; DO NOT ADD IT TO YOUR CODE
//THIS IS AN EXAMPLE; DO NOT ADD IT TO YOUR CODE
int MAX(int X, int Y) {
int MAX(int X, int Y) {
           if(X > Y) {
           if(X > Y) {
Line 68: Line 68:
                     return Y;
                     return Y;
           }
           }
}</pre>
}
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."
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."


After that bit of a headache at the end of the last section, the next line should be a nice refresher. It's just the simple module definition that we have used in all of our other programs:
After that bit of a headache at the end of the last section, the next line should be a nice refresher. It's just the simple module definition that we have used in all of our other programs:
<pre>PSP_MODULE_INFO("Image Display Program", 0, 1, 1);</pre>
PSP_MODULE_INFO("Image Display Program", 0, 1, 1);
Again we will add some familiar code, our standard callbacks.
Again we will add some familiar code, our standard callbacks.


<pre>/* Exit callback */
/* Exit callback */
int exit_callback(int arg1, int arg2, void *common) {
int exit_callback(int arg1, int arg2, void *common) {
           sceKernelExitGame();
           sceKernelExitGame();
Line 103: Line 103:


           return thid;
           return thid;
}</pre>
}
Look familiar? Good.
Look familiar? Good.


Now we get to the good stuff; it's the start of our main() function.
Now we get to the good stuff; it's the start of our main() function.
<pre>int main() {
int main() {
           char buffer[200];
           char buffer[200];
           Image* ourImage;</pre>
           Image* ourImage;
Up to this point, all we have done with strings is display them on the screen through the use of printf(). That's fine and dandy, but it's about time we get a little more in depth about what a string actually is. Basically, in C a string is an array of characters. There isn't such a thing in C as "myString," it is really just 8 character variables placed next to each other in memory. So to allow us to create a string, we have to first allocate that memory, because as soon as we do anything else (define a variable or whatnot), the memory space next to that allocated space next to the string will be reserved and therefore our string won't be allow to extend into it. Well, it will, it's called a buffer overflow, but we're not concerned about that right now; just know that it's generally a bad thing and will freeze your PSP. So to define an array in C, you simply add on how large you want the array (of a given type) to be in brackets after the variable name. So, when we use the line char buffer[200], we are allocating a block in memory of 200 character variables. This is the maximum length that our string can be, 200 characters. There is a way to change the size, but that's beyond the scope of this tutorial.
Up to this point, all we have done with strings is display them on the screen through the use of printf(). That's fine and dandy, but it's about time we get a little more in depth about what a string actually is. Basically, in C a string is an array of characters. There isn't such a thing in C as "myString," it is really just 8 character variables placed next to each other in memory. So to allow us to create a string, we have to first allocate that memory, because as soon as we do anything else (define a variable or whatnot), the memory space next to that allocated space next to the string will be reserved and therefore our string won't be allow to extend into it. Well, it will, it's called a buffer overflow, but we're not concerned about that right now; just know that it's generally a bad thing and will freeze your PSP. So to define an array in C, you simply add on how large you want the array (of a given type) to be in brackets after the variable name. So, when we use the line char buffer[200], we are allocating a block in memory of 200 character variables. This is the maximum length that our string can be, 200 characters. There is a way to change the size, but that's beyond the scope of this tutorial.


Line 115: Line 115:


Now we have a few lines of setup code.
Now we have a few lines of setup code.
<pre>          pspDebugScreenInit();
          pspDebugScreenInit();
           SetupCallbacks();
           SetupCallbacks();
           initGraphics();</pre>
           initGraphics();
The first two lines of this code are the same as what we have been using in other programs to set up the screen. The third line calls another function in graphics.h that gets the PSP ready to display graphics; it does all of the initial setup.
The first two lines of this code are the same as what we have been using in other programs to set up the screen. The third line calls another function in graphics.h that gets the PSP ready to display graphics; it does all of the initial setup.


That's all of the setup that we needed to do before actually working with our images. Now we'll move on to loading our PNG file.
That's all of the setup that we needed to do before actually working with our images. Now we'll move on to loading our PNG file.
<pre>          sprintf(buffer, "ourImage.png");
          sprintf(buffer, "ourImage.png");
           ourImage = loadImage(buffer);</pre>
           ourImage = loadImage(buffer);
The first line of this code is similar to a printf() call. But instead of printing to the screen, this line prints into a string. It's very useful, trust me, before I knew about this function, I tried to write my own. Man was that a mistake. The first paramater that sprintf() takes is the character array (AKA string) that you want to output to. We're using the buffer array that we defined above. And then the rest is like a printf(). We only wanted to store the path of our image into the buffer, but if we had wanted to, we could have used this to insert variables into a string as well, by parsing them the same way as with printf() (if you don't remember how to do this, maybe a revisit to Lesson 03 is in order.
The first line of this code is similar to a printf() call. But instead of printing to the screen, this line prints into a string. It's very useful, trust me, before I knew about this function, I tried to write my own. Man was that a mistake. The first paramater that sprintf() takes is the character array (AKA string) that you want to output to. We're using the buffer array that we defined above. And then the rest is like a printf(). We only wanted to store the path of our image into the buffer, but if we had wanted to, we could have used this to insert variables into a string as well, by parsing them the same way as with printf() (if you don't remember how to do this, maybe a revisit to Lesson 03 is in order.


Line 128: Line 128:


Now we'll do a quick check to see whether our loading of the image was successful:
Now we'll do a quick check to see whether our loading of the image was successful:
<pre>          if (!ourImage) {
          if (!ourImage) {
                     //Image load failed
                     //Image load failed
                     printf("Image load failed!\n");
                     printf("Image load failed!\n");
           } else {</pre>
           } else {
If the loadImage function encounters a problem, it will set the value of our variable to 0 (or false), alerting us of the error. So we check this with a simple if statement, and if there was a problem, we print out a short error message.
If the loadImage function encounters a problem, it will set the value of our variable to 0 (or false), alerting us of the error. So we check this with a simple if statement, and if there was a problem, we print out a short error message.


Hopefully we won't encounter a problem loading the image, and if not, then the following code will execute:
Hopefully we won't encounter a problem loading the image, and if not, then the following code will execute:
<pre>                    int x = 0;
                    int x = 0;
                     int y = 0;
                     int y = 0;
                     sceDisplayWaitVblankStart();</pre>
                     sceDisplayWaitVblankStart();
This is just setup for our image display, it shouldn't look too foreign to you, it just declares and initializes a couple of integers and then calls a sceDisplayWaitVblankStart (if you'll remember, this is the same function we used when we were reading from the control pad). This allows the "Home" button to work.
This is just setup for our image display, it shouldn't look too foreign to you, it just declares and initializes a couple of integers and then calls a sceDisplayWaitVblankStart (if you'll remember, this is the same function we used when we were reading from the control pad). This allows the "Home" button to work.


Now we will loop through the entire screen. The screen is 480 pixels wide and 272 pixels high:
Now we will loop through the entire screen. The screen is 480 pixels wide and 272 pixels high:
<pre>                    while (x < 480) {
                    while (x < 480) {
                               while (y < 272) {</pre>
                               while (y < 272) {
These are simple while loops, you should remember them from Lesson 03. The outer loop will continue while x is less than 480 (the screen's width), and the inner loop will continue while y is less than 272, the screen's height.
These are simple while loops, you should remember them from Lesson 03. The outer loop will continue while x is less than 480 (the screen's width), and the inner loop will continue while y is less than 272, the screen's height.


Now inside there, we are going to put the code to blit (or display) our image.
Now inside there, we are going to put the code to blit (or display) our image.
<pre>                                        blitAlphaImageToScreen(0 ,0 ,32 , 32, ourImage, x, y);
                                        blitAlphaImageToScreen(0 ,0 ,32 , 32, ourImage, x, y);
                                         y += 32;
                                         y += 32;
                               }</pre>
                               }
This first line is your key. It will display a 32 bit image on the screen. That includes an alpha channel! Alpha is another word for transparency. The blitAlphaImageToScreen function takes seven parameters. The first two are offsets. If you are using seperate PNGs for each of your images, these should be zero. What these two parameters allow for, though, is for you to have one master image file containing all of your sprites. Basically, if you have two images in the same image file (next to each other for example), you can use the offsets to tell your program which part of the master image you want to display. The second two parameters are for width and height respectively. Since our image is 32 pixels by 32 pixels, we are using 32 for both of these values. The fifth parameter is to tell the function which image variable you want to use (obviously we're using our ourImage variable). And finally, the last two parameters are for setting the x and y positions of the image on the screen. In programming, this is like an inverted coordinate plane. The x axis is the same as it would be in math (that is, it increases as you move from left to right). The y axis, on the other hand, is upside down; as you increase the y, you move DOWN on the coordinate plane. This can be confusing at first, if you're used to math coordinates, but you'll get the hang of it.
This first line is your key. It will display a 32 bit image on the screen. That includes an alpha channel! Alpha is another word for transparency. The blitAlphaImageToScreen function takes seven parameters. The first two are offsets. If you are using seperate PNGs for each of your images, these should be zero. What these two parameters allow for, though, is for you to have one master image file containing all of your sprites. Basically, if you have two images in the same image file (next to each other for example), you can use the offsets to tell your program which part of the master image you want to display. The second two parameters are for width and height respectively. Since our image is 32 pixels by 32 pixels, we are using 32 for both of these values. The fifth parameter is to tell the function which image variable you want to use (obviously we're using our ourImage variable). And finally, the last two parameters are for setting the x and y positions of the image on the screen. In programming, this is like an inverted coordinate plane. The x axis is the same as it would be in math (that is, it increases as you move from left to right). The y axis, on the other hand, is upside down; as you increase the y, you move DOWN on the coordinate plane. This can be confusing at first, if you're used to math coordinates, but you'll get the hang of it.


Line 154: Line 154:


Next, we add the incrementation for the x, like so:
Next, we add the incrementation for the x, like so:
<pre>                              x += 32;
                              x += 32;
                               y = 0;
                               y = 0;
                     }</pre>
                     }
This moves the horizontal placeholder over 32 pixels, the width of our image. So essentially we're moving over one column. Then we set the y placeholder back to zero so it can start from the top of the column again and work its way down.
This moves the horizontal placeholder over 32 pixels, the width of our image. So essentially we're moving over one column. Then we set the y placeholder back to zero so it can start from the top of the column again and work its way down.


Now our image is drawn. But wait, there's one more step! Right now the "screen" is only stored in memory. This is because it is much much quicker to write to memory than to write to the screen. So since we're done with our manipulation of the screen, we now need to actually put those changes into effect; this is called flipping the screen.
Now our image is drawn. But wait, there's one more step! Right now the "screen" is only stored in memory. This is because it is much much quicker to write to memory than to write to the screen. So since we're done with our manipulation of the screen, we now need to actually put those changes into effect; this is called flipping the screen.
<pre>                    flipScreen();
                    flipScreen();
           }</pre>
           }
The flipScreen() function is also defined in graphics.h. Calling it is what actually does the updating of the screen.
The flipScreen() function is also defined in graphics.h. Calling it is what actually does the updating of the screen.


And finally, we shut down the program, allowing us to look at our beautiful creation and admire our grid of images.
And finally, we shut down the program, allowing us to look at our beautiful creation and admire our grid of images.
<pre>          sceKernelSleepThread();
          sceKernelSleepThread();
           return 0;
           return 0;
}</pre>
}


So now you have your completed main.c, now we need to compile this baby. The Makefile for this program needs a few slight modifications.
So now you have your completed main.c, now we need to compile this baby. The Makefile for this program needs a few slight modifications.
<pre>TARGET = hello
TARGET = hello
OBJS = main.o graphics.o framebuffer.o
OBJS = main.o graphics.o framebuffer.o


Line 185: Line 185:


PSPSDK=$(shell psp-config --pspsdk-path)
PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak</pre>
include $(PSPSDK)/lib/build.mak
It's the standard Makefile, but with two differences. The first is that we have added graphics.o and framebuffer.o to the OBJS line. This is simply because we are using these source files (graphics.c and it uses framebuffer.c). This simply tells the compiler that we need these compiled into our project too. The second thing that we have added are some LIBS, we put in zlib with "-lz" and we added libpng with "-lpng." We also added access to the graphics hardware with "-lpspgu" and then put in the math library (which graphics.h uses) using "-lm." Compile, and there you have it, your first program with images. Now go out and create some great applications and games using your new knowledge!
It's the standard Makefile, but with two differences. The first is that we have added graphics.o and framebuffer.o to the OBJS line. This is simply because we are using these source files (graphics.c and it uses framebuffer.c). This simply tells the compiler that we need these compiled into our project too. The second thing that we have added are some LIBS, we put in zlib with "-lz" and we added libpng with "-lpng." We also added access to the graphics hardware with "-lpspgu" and then put in the math library (which graphics.h uses) using "-lm." Compile, and there you have it, your first program with images. Now go out and create some great applications and games using your new knowledge!


Note: Be sure that when you put the EBOOT on your PSP you also copy the PNG image file and place it in the same folder as the EBOOT.
Note: Be sure that when you put the EBOOT on your PSP you also copy the PNG image file and place it in the same folder as the EBOOT.
Please note that all contributions to PSP Developer wiki are considered to be released under the GNU Free Documentation License 1.2 (see PSP Developer wiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

To protect the wiki against automated edit spam, we kindly ask you to solve the following hCaptcha:

Cancel Editing help (opens in new window)