How to Simply Load a JPG or PNG image?

Discussion in 'Game Development (Technical)' started by bantamcitygames, Dec 14, 2005.

  1. bantamcitygames

    Administrator Original Member Indie Author Greenlit

    Joined:
    Jul 27, 2004
    Messages:
    1,731
    Likes Received:
    78
    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.
     
  2. Vorax

    Original Member

    Joined:
    Jan 21, 2005
    Messages:
    349
    Likes Received:
    0
    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 :)
     
  3. impossible

    Original Member

    Joined:
    Aug 9, 2004
    Messages:
    443
    Likes Received:
    0
    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?
     
  4. ggambett

    Moderator Original Member Indie Author

    Joined:
    Jul 26, 2004
    Messages:
    1,982
    Likes Received:
    6
    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.
     
  5. illume

    Original Member

    Joined:
    Jul 27, 2004
    Messages:
    206
    Likes Received:
    0
    if you were using python and pygame

    surface = pygame.image.load("bla.jpg")
     
  6. Matthew

    Indie Author

    Joined:
    Jul 29, 2004
    Messages:
    493
    Likes Received:
    0
  7. bantamcitygames

    Administrator Original Member Indie Author Greenlit

    Joined:
    Jul 27, 2004
    Messages:
    1,731
    Likes Received:
    78
    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.
     
  8. gosub

    Original Member

    Joined:
    Sep 10, 2005
    Messages:
    151
    Likes Received:
    0
  9. Jim Buck

    Indie Author

    Joined:
    Jul 27, 2004
    Messages:
    1,158
    Likes Received:
    0
    Also check out FreeImage. It's very simple to use and can load much more than just JPG and PNG.
     
  10. Emmanuel

    Moderator Original Member

    Joined:
    Nov 23, 2004
    Messages:
    859
    Likes Received:
    0
    Just to be complete, there is also CxImage. 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
     
  11. Alex Saveliev

    Original Member

    Joined:
    Jun 8, 2005
    Messages:
    31
    Likes Received:
    0
    I don't know a simple way to load PNG or JPG, but I know the simpliest way to load JPEG2000 :)
    See my signature :)
     
  12. bantamcitygames

    Administrator Original Member Indie Author Greenlit

    Joined:
    Jul 27, 2004
    Messages:
    1,731
    Likes Received:
    78
    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.
     
  13. Mark Sheeky

    Indie Author

    Joined:
    Aug 7, 2004
    Messages:
    448
    Likes Received:
    0
    Use Irfanview, great image viewer/converter, small in size but with lots of formats including j2k.

    Mark
     
  14. Alex Saveliev

    Original Member

    Joined:
    Jun 8, 2005
    Messages:
    31
    Likes Received:
    0
    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!
     
  15. bantamcitygames

    Administrator Original Member Indie Author Greenlit

    Joined:
    Jul 27, 2004
    Messages:
    1,731
    Likes Received:
    78
    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.
     
  16. PoV

    PoV
    Indie Author

    Joined:
    Jul 27, 2004
    Messages:
    2,132
    Likes Received:
    0
  17. Alex Saveliev

    Original Member

    Joined:
    Jun 8, 2005
    Messages:
    31
    Likes Received:
    0
    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.

    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.
     
  18. Alex Saveliev

    Original Member

    Joined:
    Jun 8, 2005
    Messages:
    31
    Likes Received:
    0
    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."

    :)
     
  19. cliffski

    Moderator Original Member

    Joined:
    Jul 27, 2004
    Messages:
    3,897
    Likes Received:
    0
    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.
     
  20. svero

    Moderator Original Member Indie Author

    Joined:
    Jul 27, 2004
    Messages:
    3,392
    Likes Received:
    6
    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.
     

Share This Page

  • About Indie Gamer

    When the original Dexterity Forums closed in 2004, Indie Gamer was born and a diverse community has grown out of a passion for creating great games. Here you will find over 10 years of in-depth discussion on game design, the business of game development, and marketing/sales. Indie Gamer also provides a friendly place to meet up with other Developers, Artists, Composers and Writers.
  • Buy us a beer!

    Indie Gamer is delicately held together by a single poor bastard who thankfully gets help from various community volunteers. If you frequent this site or have found value in something you've learned here, help keep the site running by donating a few dollars (for beer of course)!

    Sure, I'll Buy You a Beer