View Full Version : How to Simply Load a JPG or PNG image?
BantamCityGames
12-13-2005, 05:43 PM
Hi there, after a bit of googling turned up alot of convoluted code, I wanted to ask if there was a simple way to load PNG's or JPG's (not worried about alpha right now)?? I am not looking for a 3rd party library, but more of a snippet that I can modify and drop into my code. Currently I am loading all my images as BMP's and this really limits the amount of images I can have in a ~5 MB download.
Is this a fairly simple thing or am I asking too much?
Thanks.
Vorax
12-13-2005, 06:52 PM
I assume you mean code for C or C++? I use Java which comes with loaders for PNG, JPEG, BMP, etc. out of the box, but there are lots of C++ API's out there for the same thing.
I can tell you that neither of these formats is simple. I did write a JPEG loader for C++ a long time ago - the algorithm is pretty heft due to the lossy compression. If using a third party API is an option, I strongly recommend you go that route. Writing image loaders beyond TGA and BMP is pretty difficult and time consuming. There's a reason those files are so small :)
impossible
12-13-2005, 06:52 PM
For PNG, libpng, www.libpng.org. For JPG, libjpeg http://www.ijg.org
These are basically the standard libraries that everyone uses. Of course depending on your platform or engine you might already have support to load jpegs and pngs (D3DX, SDL_Image, etc.), what API or engine are you using?
ggambett
12-13-2005, 07:08 PM
Impossible's answer is right on target. There are no "easy ways" to load PNG or JPEG using just "code snippets" because PNG and JPEG are very complex formats (as opposed to BMP for example - you don't want to write your own Lempel-Ziv code or Discrete Cosine Transform or Huffman code, right?), so you will end up using libjpeg and libpng. Or a library that wraps them in an unified and easier to use interface. I use SDL_Image (I'm not sure if you can use SDL_Image without using SDL itself - but I think you could), you can also try DevIL which is like SDL_Image but not tied to another library such as SDL.
illume
12-13-2005, 07:49 PM
surface = pygame.image.load("bla.jpg")
Matthew
12-13-2005, 07:54 PM
Another option to look at:
http://corona.sourceforge.net/
BantamCityGames
12-13-2005, 08:30 PM
Thanks guys, looking into libpng now... the documentation has my head spinning at the moment... I'll look at it some more tommorow.
Thanks again.
gosub
12-13-2005, 09:47 PM
A windows only solution is to use gdiplus.dll, which has functions to load/save jpg and png.
http://www.microsoft.com/downloads/details.aspx?FamilyID=6a63ab9c-df12-4d41-933c-be590feaa05a&DisplayLang=en
-Jeremy
Jim Buck
12-13-2005, 10:57 PM
Also check out FreeImage (http://freeimage.sourceforge.net/). It's very simple to use and can load much more than just JPG and PNG.
Emmanuel
12-13-2005, 11:37 PM
Just to be complete, there is also CxImage (http://www.xdp.it/cximage.htm). This is what PTK uses to load jpeg, png, tga, bmp, etc. using a clean C++ interface. It's also possible to load from memory or from your own resource pack.
Best regards,
Emmanuel
Alex Saveliev
01-26-2006, 11:54 PM
I don't know a simple way to load PNG or JPG, but I know the simpliest way to load JPEG2000 :)
See my signature :)
BantamCityGames
01-27-2006, 05:56 AM
I've been looking into the packages mentioned above and I think I'm going to go with cximage. It looks the easiest to use out of them all and thats pretty much what I'm looking for at this point with the limited amount of time I have. The j2k codec looks interesting, but I don't even know how to get my images into a j2k format at this point.
Mark Sheeky
01-27-2006, 06:21 AM
The j2k codec looks interesting, but I don't even know how to get my images into a j2k format at this point.
Use Irfanview, great image viewer/converter, small in size but with lots of formats including j2k.
Mark
Alex Saveliev
01-27-2006, 07:11 AM
I would recommend the Fnord Photoshop plugin (its latest beta) from here:
http://fnordware.com/j2k/
ACDSee 7.0 and higher supports JPEG2000. Kakadu (http://www.kakadusoftware.com/) has demo tools, which allow encoding (good for batch processing).
And of course - do not hesitate to contact me! I love to be contacted :)
My email: alex (at) j2k-codec.com.
Anyone interested - just send me your typical image and I'll compress it for you into JPEG and JPEG2000 so you can see the difference for yourself!
BantamCityGames
01-28-2006, 08:19 AM
Hi Alex, I was going to email you these questions, but I thought the rest of the group may be able to benefit from the answer as well... How is the j2k-codec normally used in games? Is it used to decode an image and then copy to your normal graphics buffer (say for instance a directx surface)? Also, the web page says the codec has alpha capabilities, but I couldn't find any function calls to retrieve the alpha information.
Thanks.
Anyone tried Jasper (http://www.ece.uvic.ca/~mdadams/jasper/) as a J2K loader lib (http://svn.ghostscript.com:8080/jasper/releases/)?
Alex Saveliev
01-28-2006, 09:21 AM
How is the j2k-codec normally used in games? Is it used to decode an image and then copy to your normal graphics buffer (say for instance a directx surface)?
Well, J2K-Codec is used as an ordinary image loader - it returns uncompressed image in a memory buffer (or puts the image into a user's buffer).
Look at the example:
J2K_Image j2k;
j2k.easyDecode("file.jp2");
printf("\n J2K-Codec image info: %ux%u, %u component(s).\n", j2k.width, j2k.height, j2k.components);
j2k.buffer now contains the whole uncompressed image with 1, 3 or 4 bytes per pixel. You can do with it whatever you want :)
Here's a quote from one of J2K-Codec's samples, "f_Alpha":
J2K_Image j2k_house;
j2k_house.easyDecode("house.jp2", "mba");
gfx.drawSpriteAlpha(450, 300, j2k_house.width, j2k_house.height, j2k_house.buffer);
You can also do animations with J2K_Frames class:
J2K_Frames j2k_sun;
j2k_sun.open("sun.jp2", "mba");
gfx.drawFrame(20, 20, j2k_sun(sun_frame_no));
I guess it's best to just download the package and take a look at the samples (there are plenty of them :)
You can also decode images directly into DirectX surfaces.
Also, the web page says the codec has alpha capabilities, but I couldn't find any function calls to retrieve the alpha information.
You do not usually need to extract alpha from the image - it comes as 4-th byte (i.e. BGRA or RGBA format) and is used while drawing.
Alex Saveliev
01-28-2006, 09:24 AM
Anyone tried Jasper (http://www.ece.uvic.ca/~mdadams/jasper/) as a J2K loader lib (http://svn.ghostscript.com:8080/jasper/releases/)?
Yep, I tried :)
Out of my frustration the J2K-Codec was born :)
Take a look also at this thread:
http://forums.indiegamer.com/showthread.php?t=5411
Here's a recent quote from Mark Featherstone (moonpod.com):
"Loading tga's straight out of the pak file gave me a game start up time of 9 seconds, with jp2's it was 12 seconds. I can certainly live with that! Using jasper it was about 3 minutes - hehe."
:)
cliffski
01-28-2006, 09:30 AM
I use the D3DX texture helper functions to load in DDS files. Very good comrpession, and its a one-call deal to laod in a bmp or a DDS or tga. not sure about jpg.
D3DX was built into directx7, and I think 8. not sure about newer versions, I use DX7.
svero
01-28-2006, 09:42 AM
What I really dont understand is why you're struggling with these basic things when there's the popcap engine and ptk etc.. available. Sounds to me like you should be using one of these standard good engines and not try writing everything from scratch.
Alex Saveliev
01-28-2006, 10:25 AM
Yeah, I wonder why people are developing Linux when there’s already a very good operating system?.. ;)
soniCron
01-28-2006, 10:31 AM
Why use Linux when you could write your own operating system from scratch?
Vorax
01-28-2006, 11:02 AM
What I really dont understand is why you're struggling with these basic things when there's the popcap engine and ptk etc.. available. Sounds to me like you should be using one of these standard good engines and not try writing everything from scratch.
If your only objective is to create a game, then I agree.
However, there are many reasons to create a game. For those who want to build their own engine, the final goal is secondary to the challenge itself.
Alex Saveliev
01-28-2006, 11:15 AM
Why use Linux when you could write your own operating system from scratch?
That's exactly what Linus did, right? :)
Wrote his own OS completely from scratch...
BantamCityGames
01-29-2006, 07:55 AM
What I really dont understand is why you're struggling with these basic things when there's the popcap engine and ptk etc.. available. Sounds to me like you should be using one of these standard good engines and not try writing everything from scratch.
Steve, I know what you are saying... but the thing is, I already have Invadazoid written in C++/DirectX, so do I spend all of my 5 hours per week porting it over to the popcap framework (which at the moment looks very foreign to me, which would mean learning how everything works again) or can I just drop in some png/jpg2000 code in a couple hours and be done with it? I'd love to have a better framework than I have, but I just don't have the time.
Savant
01-29-2006, 08:43 AM
I'd love to have a better framework than I have, but I just don't have the time.
That's sort of a weird statement. If you feel/believe that PopCap would be better, how would it not be worth your time to convert to it? The time/effort saved in the long run should vastly outweigh whatever initial pain there is for conversion.
It's like that "sharpening the saw" story.
"I've been sawing like crazy on this tree! It's taking me hours. I'm exhausted."
"So why don't you sharpen the saw?"
"I'm too busy sawing!"
svero
01-29-2006, 08:47 AM
Steve, I know what you are saying... but the thing is, I already have Invadazoid written in C++/DirectX, so do I spend all of my 5 hours per week porting it over to the popcap framework (which at the moment looks very foreign to me, which would mean learning how everything works again) or can I just drop in some png/jpg2000 code in a couple hours and be done with it? I'd love to have a better framework than I have, but I just don't have the time.
Well I don't know about Invadazoid. If its just to improve the work on a game already completed then I suppose it would be easier to just drop the extra code in and rebuild it without porting. Given that Invadazoid was released some time ago I assumed this was for work on a new title, where you'd likely save time by moving to a new framework in the long run if you're still struggling with png file loading and other basic stuff that the other gamelibs do.
BantamCityGames
01-29-2006, 07:57 PM
You're right... it seems like a much easier way of making games that I have been doing, and maybe I will move to popcap if I get up the initiative to "just do it". I guess we'll see!! :D
MrGoldfish
02-13-2006, 02:35 PM
You can use the D3DXCreateTextureFromFileEx function to load in graphic files under Direct3D. It supports .bmp, .dds, .dib, .hdr, .jpg, .pfm, .png, .ppm, and .tga formats. It really is a lifesaver.
cliffski
08-14-2006, 05:00 AM
You can use the D3DXCreateTextureFromFileEx function to load in graphic files under Direct3D. It supports .bmp, .dds, .dib, .hdr, .jpg, .pfm, .png, .ppm, and .tga formats. It really is a lifesaver.
not in directx7, it's just dds bitmap and simple tga then.
BantamCityGames
08-15-2006, 08:22 AM
Since this original discussion, I'm now struggling to decide between torque 2d and 3d for my next game. I would have preferred TGB (2d), but they cut back the real time networking to be turn based and my next game will be a real time multiplayer game. Does anyone know why they did this? I mean the engine already supported real-time networking...
The biggest reason I've heard was that there wasn't a simple, generic way to handle real-time networking and that's really why it isn't "in the box". If you purchase TGE + TGB (with source), you have the full networking code... there's a tutorial for how to take the real time network code from TGE and use it in a TGB game. The "turn based" network support that's in the box is a bit of a misnomer as it can handle more than just turn based games but it's not real-time either. Anyway, with a little effort, TGB is definitely capable of doing real-time networking.
I hope that helps.
-Andrew Douglas
http://theoreticalgames.com
vBulletin v3.6.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.