jpeg files

Does anybody know where I can find info on loading jpeg file and using them as textures? Thanks.

DevIL

[This message has been edited by Zeno (edited 08-11-2002).]

OpenIL/DevIL
Nehe IPicture loading
google
google
google

what you want to do is build upon two parts:
loading a jpg file
loading up data onto a texture

the first one, don’t ask it here
the second one is simple and if you don’t know it, ask in the beginners forum.

there is some free source to load jpg files, but i don’t remember the name… well, you’ll find it by browsing and searching and googling and searching the forums (gamedev,here,flipcode,there where ever, the topic is discussed and the solutions are out there…)

good luck

In addition to what’s already been mentioned, be sure that JPEG really is an appropriate format. JPEG files are good for photographic images (which many textures are), but can be extremely poor for hand-drawn or computer-drawn images. (Many–if not most–webmasters are completely clueless of this fact). In addition, JPEG files don’t support an alpha channel, so if you want 32-bit textures with alpha, you’ll have to use a different format (such as PNG).

I personally use tga files. They can be compressed or uncompressed if you want and they can support an alpha channel. So maybe check those out too.

-SirKnight

JPEG’s are Evil too :
http://www.sjbaker.org/steve/omniv/jpegs_are_evil_too.html

jpegs aren’t evil. jpegs rock!

Use TGA’s for alpha channels and normal maps. Use jpegs for everything else.

But dont crank the compression too high, or they’ll look crap.

Oh, and according to Adobe Photoshop 6, PNG’s dont support alpha channels.

Nutty

Originally posted by Nutty:
jpegs aren’t evil. jpegs rock!
Use TGA’s for alpha channels and normal maps. Use jpegs for everything else.

…Or simply use PNGs for every thing down to multilayered heightmaps

Oh, and according to Adobe Photoshop 6, PNG’s dont support alpha channels.

They must mean Adobe’s Non-Compliant PNG’s ™ don’t.

Use The Gimp.

Julien.

Well I think JPEGs are still good. For somethings you may not want them, I can think of a few things I would never use a jpg for. But anyway, look at Quake 3. If you take a peek into the pak files, you will notice that a lot of the textures are JPGs! And the graphics in quake 3 look pretty good to me.

-SirKnight

i dont know about photoshop 6 but
photoshop 5.5 has embarassing? (for the programmeurs) support for png’s. eg if u save a png with alpha channel + then reopen in the alpha channel has corrupted some of the RGB channels.

check out the Intel JPEG Library Developer’s Guide in pdf format available at : http://developer.intel.com

this url might also be useful: http://www.cfxweb.net/~delphigl/projects/glbmp.htm for a C++ example

Hope that could help

[b]They must mean Adobe’s Non-Compliant PNG’s ™ don’t.

Use The Gimp.

[/b]

Or Paint Shop Pro 7. It too can easily support alpha in png files.

Originally posted by Fredz:
JPEG’s are Evil too :
http://www.sjbaker.org/steve/omniv/jpegs_are_evil_too.html

That web page has mis-informed you. JPEG actually consists of several phases of compression. The first phase is a run-length (lossless) encoding and it is followed by a huffman encoding (also lossless) and then by a discrete cosine transform (lossy) and quantization process (lossy). There is such a thing as lossless JPEG and it equates to using the run-length/huffman passes only. This lossless compression rivals the best algorithms used in the targa images. In Photoshop 7, simply select a quality of 10, 11, or 12 to access this lossless compression.
True, JPEG does not support an alpha channel but it does support greyscale luminance images. If I need an alpha mask for a JPEG image, all I have to do is to save it as a greyscale JPEG and load it at the same time I load the RGB image.
There are many tradeoffs you must consider when selecting an image format. I choose JPEG simply because it consistently yields the best compression ratios for the types of textures I use. That in turn cuts down on the loading time and the total amount of disk space I need for storage.

Jpeg/lossless jpeg/png/targa is all a matter of perspective and intent of use. Jpeg can take a little more time to load/save due to the multi-pass compression. Targa/raw or Targa/compressed can make a decent screen save minimal memory overhead and reasonably fast. etc. etc. You can pretty much use almost anythign for anything else, from saving all colors into their own greyscale map and then use a greyscale mapped GIF shudder. If someone wants to use jpeg, I tell them go for it. I do one step worse, I do almost everything now with wavelet compression. but then the size of images I use are just plain HUGE anymore and parsing a targa/tiff/jpeg/png all took way too long.

My point, if there is one, and even I am not sure anymore, is that he has a right to use jpeg without arguing over it. but if you have a better suggestion, say, “try this, you might like it better, i do.”

Enjoy!
Jeff
P.S. Try parsing Nasa’s Blue Marble 1Km for the world in almost any format. a programmer’s nightmare. If anyone has anything faster than wavelet compression for that large of an image, I am definatly up for a change!

Originally posted by Nutty:
Oh, and according to Adobe Photoshop 6, PNG’s dont support alpha channels.

That’s partially because they finally woke up and smelled the burning coffee and implemented transparency support correctly.

If you create a Photoshop image with a transparent background, and save it using the PNG format, it automatically generates an appropriate alpha channel that represents the transparency of each pixel (at a full 8 bits, NOT on/off). Seeing as how that’s almost certainly what you really wanted in the alpha channel anyway, this is a huge improvement over forcing you to draw into the alpha channel manually.

PNGs do support alpha channels. I’ve created PNG files with alpha using Adobe photoshop and it works just fine, it also seems to work in various browsers (although IE sucks at alpha blending).

This image (DevIL logo) is a PNG and was created using photoshop.
http://openil.sourceforge.net/logos/normal/angus/Lshadtran.png

[This message has been edited by dorbie (edited 08-16-2002).]

Here’s the code for loading jpg/jpeg files (but it’s not my work):


#include “jpeglib.h”
#pragma comment(lib, “jpeg.lib”)

//***************** Prototypes ************************//

// function for loading .jpg
tImage *LoadJPG(const char *strFileName);

// for .jpg decoding and decompression
void DecodeJPG(jpeg_decompress_struct* cinfo, tImage*pImageData);

///////////////////////////////// DECODE JPG ///////////
/* here we decode .jpg */

void DecodeJPG(jpeg_decompress_struct* cinfo, tImage *pImageData)
{
// reads JPG’s header
jpeg_read_header(cinfo, TRUE);

// decompression
jpeg_start_decompress(cinfo);

pImageData->channels = cinfo->num_components;
pImageData->sizeX = cinfo->image_width;
pImageData->sizeY = cinfo->image_height;

int rowSpan = cinfo->image_width * cinfo->num_components;

pImageData->data = ((unsigned char*)malloc(sizeof(unsigned char)rowSpanpImageData->sizeY));

unsigned char** rowPtr = new unsigned char*[pImageData->sizeY];

for (int i = 0; i < pImageData->sizeY; i++)
rowPtr = &(pImageData->data[i * rowSpan]);

int rowsRead = 0;
while (cinfo->output_scanline < cinfo->output_height)
{
rowsRead += jpeg_read_scanlines(cinfo,
&rowPtr[rowsRead], cinfo->output_height - rowsRead);
}

// finish decompression
jpeg_finish_decompress(cinfo);
}

///////////////////////////////// LOAD JPG /////////////
/* here we load .jpg */

tImage *LoadJPG(const char *strFileName)
{
struct jpeg_decompress_struct cinfo;
tImage *pImageData = NULL;
FILE *pFile;

if((pFile = fopen(strFileName, “rb”)) == NULL)
{
MessageBox(g_hWnd, “Loading Failed!”, “Error”, MB_OK);
return NULL;
}

jpeg_error_mgr jerr;

// Have our compression info object point to the error handler address
cinfo.err = jpeg_std_error(&jerr);

jpeg_create_decompress(&cinfo);

jpeg_stdio_src(&cinfo, pFile);

pImageData = (tImage*)malloc(sizeof(tImage));

DecodeJPG(&cinfo, pImageData);

jpeg_destroy_decompress(&cinfo);

fclose(pFile);

return pImageData;
}

I kind-of like .dds files. They come pre-compressed, so you don’t have to spend time in the driver when uploading. The PhotoShop tools for saving .dds are pretty good (download from nVIDIAs site), and generate much better compressed images than the quick-and-dirty job the driver is doing.

Now, if you’re trying to minimize on-disk size, then something like JPEG2000 might be worth it. Else, just go with .dds. Except for normal maps – s3tc doesn’t do so well on those :slight_smile:

Originally posted by pleopard:
That web page has mis-informed you. JPEG actually consists of several phases of compression. The first phase is a run-length (lossless) encoding and it is followed by a huffman encoding (also lossless) and then by a discrete cosine transform (lossy) and quantization process (lossy). There is such a thing as lossless JPEG and it equates to using the run-length/huffman passes only. This lossless compression rivals the best algorithms used in the targa images. In Photoshop 7, simply select a quality of 10, 11, or 12 to access this lossless compression.

Huffman and RLE encodings are applied to the coefficients of the DCT (after the lossy encoding). I think that although offtopic, this info would be useful:

The commonly used method is “baseline JPEG” (or its variant “progressive
JPEG”). The same ISO standard also defines a very different method called
“lossless JPEG”. And if that’s not confusing enough, a new lossless
standard called “JPEG-LS” is about to hit the streets.
Lossless JPEG has never been popular — in fact, no common applications
support it — and it is now largely obsolete. (For example, the new PNG
standard outcompresses lossless JPEG on most images.) Recognizing this,
the ISO JPEG committee recently finished an all-new lossless compression
standard called JPEG-LS (you may have also heard of it under the name LOCO).
JPEG-LS gives better compression than original lossless JPEG, but still
nowhere near what you can get with a lossy method. It’s anybody’s guess
whether this new standard will achieve any popularity.

It’s worth repeating that cranking a regular JPEG implementation up to its
maximum quality setting does not get you lossless storage; even at the
highest possible quality setting, baseline JPEG is lossy because it is
subject to roundoff errors in various calculations. Roundoff errors alone
are nearly always too small to be seen, but they will accumulate if you put
the image through multiple cycles of compression (see section 10).

http://www.faqs.org/faqs/jpeg-faq/part1/section-13.html

The FAQ is dated 28 of March 1999, so maybe JPEG-LS has indeed become widespread since then :?

[EDIT] Added date info.

[This message has been edited by evanGLizr (edited 08-17-2002).]