jpeg image compression frequently asked questions archive name jpeg faq last modified may this faq article discusses jpeg image compression suggestions for additions and clarifications are welcome new since version of april new version of xv supports bit viewing for x windows new versions of dvpeg image alchemy for dos new versions of image archiver pmview for os new listing mgif for monochrome display ataris this article includes the following sections what is jpeg why use jpeg when should i use jpeg and when should i stick with gif how well does jpeg compress images what are good quality settings for jpeg where can i get jpeg software a canned software viewers etc b source code what s all this hoopla about color quantization how does jpeg work what about lossless jpeg why all the argument about file formats how do i recognize which file format i have and what do i do about it what about arithmetic coding does loss accumulate with repeated compression decompression what are some rules of thumb for converting gif images to jpeg sections are basic info that every jpeg user needs to know sections are advanced info for the curious this article is posted every weeks you can always find the latest version in the news answers archive at rtfm mit edu by ftp fetch pub usenet news answers jpeg faq or if you don t have ftp send e mail to mail server rtfm mit edu with body send usenet news answers jpeg faq many other faq articles are also stored in this archive for more instructions on use of the archive send e mail to the same address with the words help and index no quotes on separate lines if you don t get a reply the server may be misreading your return address add a line such as path myname mysite to specify your correct e mail address to reply to what is jpeg jpeg pronounced jay peg is a standardized image compression mechanism jpeg stands for joint photographic experts group the original name of the committee that wrote the standard jpeg is designed for compressing either full color or gray scale digital images of natural real world scenes it does not work so well on non realistic images such as cartoons or line drawings jpeg does not handle black and white bit per pixel images nor does it handle motion picture compression standards for compressing those types of images are being worked on by other committees named jbig and mpeg respectively jpeg is lossy meaning that the image you get out of decompression isn t quite identical to what you originally put in the algorithm achieves much of its compression by exploiting known limitations of the human eye notably the fact that small color details aren t perceived as well as small details of light and dark thus jpeg is intended for compressing images that will be looked at by humans if you plan to machine analyze your images the small errors introduced by jpeg may be a problem for you even if they are invisible to the eye a useful property of jpeg is that the degree of lossiness can be varied by adjusting compression parameters this means that the image maker can trade off file size against output image quality you can make extremely small files if you don t mind poor quality this is useful for indexing image archives making thumbnail views or icons etc etc conversely if you aren t happy with the output quality at the default compression setting you can jack up the quality until you are satisfied and accept lesser compression why use jpeg there are two good reasons to make your image files smaller and to store bit per pixel color data instead of bit per pixel data making image files smaller is a big win for transmitting files across networks and for archiving libraries of images being able to compress a mbyte full color file down to kbytes or so makes a big difference in disk space and transmission time if you are comparing gif and jpeg the size ratio is more like four to one more details below if your viewing software doesn t support jpeg directly you ll have to convert jpeg to some other format for viewing or manipulating images even with a jpeg capable viewer it takes longer to decode and view a jpeg image than to view an image of a simpler format gif for instance thus using jpeg is essentially a time space tradeoff you give up some time in order to store or transmit an image more cheaply it s worth noting that when network or phone transmission is involved the time savings from transferring a shorter file can be much greater than the extra time to decompress the file i ll let you do the arithmetic yourself the other reason why jpeg will gradually replace gif as a standard usenet posting format is that jpeg can store full color information bits pixel million colors instead of or less or fewer colors if you have only bit display hardware then this may not seem like much of an advantage to you within a couple of years though bit gif will look as obsolete as black and white macpaint format does today furthermore for reasons detailed in section jpeg is far more useful than gif for exchanging images among people with widely varying color display hardware hence jpeg is considerably more appropriate than gif for use as a usenet posting standard when should i use jpeg and when should i stick with gif jpeg is not going to displace gif entirely for some types of images gif is superior in image quality file size or both one of the first things to learn about jpeg is which kinds of images to apply it to as a rule of thumb jpeg is superior to gif for storing full color or gray scale images of realistic scenes that means scanned photographs and similar material jpeg is superior even if you don t have bit display hardware and it is a lot superior if you do see section for details gif does significantly better on images with only a few distinct colors such as cartoons and line drawings in particular large areas of pixels that are all exactly the same color are compressed very efficiently indeed by gif jpeg can t squeeze these files as much as gif does without introducing visible defects this sort of image is best kept in gif form in particular single color borders are quite cheap in gif files but they should be avoided in jpeg files jpeg also has a hard time with very sharp edges a row of pure black pixels adjacent to a row of pure white pixels for example sharp edges tend to come out blurred unless you use a very high quality setting again this sort of thing is not found in scanned photographs but it shows up fairly often in gif files borders overlaid text etc the blurriness is particularly objectionable with text that s only a few pixels high if you have a gif with a lot of small size overlaid text don t jpeg it computer drawn images ray traced scenes for instance usually fall between scanned images and cartoons in terms of complexity the more complex and subtly rendered the image the more likely that jpeg will do well on it the same goes for semi realistic artwork fantasy drawings and such plain black and white two level images should never be converted to jpeg you need at least about gray levels before jpeg is useful for gray scale images it should also be noted that gif is lossless for gray scale images of up to levels while jpeg is not if you have an existing library of gif images you may wonder whether you should convert them to jpeg you will lose a little image quality if you do section which argues that jpeg image quality is superior to gif only applies if both formats start from a full color original if you start from a gif you ve already irretrievably lost a great deal of information jpeg can only make things worse however the disk space savings may justify converting anyway this is a decision you ll have to make for yourself if you do convert a gif library to jpeg see section for hints be prepared to leave some images in gif format since some gifs will not convert well how well does jpeg compress images pretty darn well here are some sample file sizes for an image i have handy a x full color image of a ship in a harbor the first three files are for comparison purposes the rest were created with the free jpeg software described in section b file size in bytes comments ship ppm original file in ppm format no compression bits or bytes per pixel plus a few bytes overhead ship ppm z ppm file passed through unix compress compress doesn t accomplish a lot you ll note other text oriented compressors give similar results ship gif converted to gif with ppmquant fs ppmtogif most of the savings is the result of losing color info gif saves bits pixel not see sec ship jpg cjpeg q highest useful quality setting this is indistinguishable from the bit original at least to my nonprofessional eyeballs ship jpg cjpeg q default setting you have to look mighty darn close to distinguish this from the original even with both on screen at once ship jpg cjpeg q this has slight defects if you know what to look for you could tell it s been jpeged without seeing the original still as good image quality as many recent postings in usenet pictures groups ship jpg cjpeg q jpeg s characteristic blockiness becomes apparent at this setting djpeg blocksmooth helps some still i ve seen plenty of usenet postings that were of poorer image quality than this ship jpg o cjpeg q optimize optimize cuts table overhead blocky but perfectly satisfactory for preview or indexing purposes note that this file is tiny the compression ratio from the original is in this case jpeg can make a file that s a factor of four or five smaller than a gif of comparable quality the q file is every bit as good as the gif better if you have a full color display this seems to be a typical ratio for real world scenes what are good quality settings for jpeg most jpeg compressors let you pick a file size vs image quality tradeoff by selecting a quality setting there seems to be widespread confusion about the meaning of these settings quality does not mean keep of the information as some have claimed the quality scale is purely arbitrary it s not a percentage of anything the name of the game in using jpeg is to pick the lowest quality setting smallest file size that decompresses into an image indistinguishable from the original this setting will vary from one image to another and from one observer to another but here are some rules of thumb the default quality setting q is very often the best choice this setting is about the lowest you can go without expecting to see defects in a typical image try q first if you see defects then go up except for experimental purposes never go above q saying q will produce a file two or three times as large as q but of hardly any better quality if the image was less than perfect quality to begin with you might be able to go down to q without objectionable degradation on the other hand you might need to go to a higher quality setting to avoid further degradation the second case seems to apply much of the time when converting gifs to jpeg the default q is about right for compressing bit images but q to is usually better for converting gifs see section for more info if you want a very small file say for preview or indexing purposes and are prepared to tolerate large defects a q setting in the range of to is about right q or so may be amusing as op art note the quality settings discussed in this article apply to the free jpeg software described in section b and to many programs based on it other jpeg implementations such as image alchemy may use a completely different quality scale some programs don t even provide a numeric scale just high medium low style choices where can i get jpeg software most of the programs described in this section are available by ftp if you don t know how to use ftp see the faq article how to find sources if you don t have direct access to ftp read about ftpmail servers in the same article that article appears regularly in news answers or you can get it by sending e mail to mail server rtfm mit edu with send usenet news answers finding sources in the body the anonymous ftp list faq may also be helpful it s usenet news answers ftp list faq in the news answers archive note this list changes constantly if you have a copy more than a couple months old get the latest jpeg faq from the news answers archive a if you are looking for canned software viewers etc the first part of this list is system specific programs that only run on one kind of system if you don t see what you want for your machine check out the portable jpeg software described at the end of the list note that this list concentrates on free and shareware programs that you can obtain over internet but some commercial programs are listed too x windows xv shareware is an excellent viewer for jpeg gif and many other image formats it can also do format conversion and some simple image manipulations it s available for ftp from export lcs mit edu file contrib xv tar z version is a major upgrade with support for bit displays and many other improvements however it is brand new and still has some bugs lurking if you prefer not to be on the bleeding edge stick with version also available from export note that version is not a good choice if you have a bit display you ll get only bit color nor for converting bit images to jpeg but works fine for converting gif and other bit images to jpeg caution there is a glitch in version be sure to check the save at normal size checkbox when saving a jpeg file or the file will be blurry another good choice for x windows is john cristy s free imagemagick package also available from export lcs mit edu file contrib imagemagick tar z this package handles many image processing and conversion tasks the imagemagick viewer handles bit displays correctly for colormapped displays it does better though slower color quantization than xv or the basic free jpeg software both of the above are large complex packages if you just want a simple image viewer try xloadimage or xli xloadimage supports jpeg in its latest release xloadimage is free and available from export lcs mit edu file contrib xloadimage tar z xli is a variant version of xloadimage said by its fans to be somewhat faster and more robust than the original the current xli is indeed faster and more robust than the current xloadimage at least with respect to jpeg files because it has the ijg v decoder while xloadimage is using a hacked over v the next xloadimage release will fix this xli is also free and available from export lcs mit edu file contrib xli tar z both programs are said to do the right thing with bit displays ms dos this covers plain dos for windows or os programs see the next headings one good choice is eric praetzel s free dvpeg which views jpeg and gif files the current version is available by ftp from sunee uwaterloo ca file pub jpeg viewers dvpeg zip this is a good basic viewer that works on either or machines the user interface is not flashy but it s functional another freeware jpeg gif tga viewer is mohammad rezaei s hiview the current version is available from simtel and mirror sites see note below file msdos graphics hv zip hiview requires a or better cpu and a vcpi compatible memory manager qemm and max work windows and os do not hiview is currently the fastest viewer for images that are no bigger than your screen for larger images it scales the image down to fit on the screen rather than using panning scrolling as most viewers do you may or may not prefer this approach but there s no denying that it slows down loading of large images considerably note installation is a bit tricky read the directions carefully a shareware alternative is colorview for dos this is easier to install than either of the two freeware alternatives its user interface is also much spiffier looking although personally i find it harder to use more keystrokes inconsistent behavior it is faster than dvpeg but a little slower than hiview at least on my hardware for images larger than screen size dvpeg and colorview seem to be about the same speed and both are faster than hiview the current version is available from simtel and mirror sites see note below file msdos graphics dcview zip requires a vesa graphics driver if you don t have one look in vesadrv zip or vesa tsr zip from the same directory many recent pcs have a built in vesa driver so don t try to load a vesa driver unless colorview complains that the driver is missing a second shareware alternative is fullview which has been kicking around the net for a while but i don t know any stable archive location for it the current rather old version is inferior to the above viewers anyway the author tells me that a new version of fullview will be out shortly and it will be submitted to the simtel archives at that time the well known gif viewer compushow cshow supports jpeg in its latest revision a however cshow s jpeg implementation isn t very good it s slow about half the speed of the above viewers and image quality is poor except on hi color displays too bad it d have been nice to see a good jpeg capability in cshow shareware available from simtel and mirror sites see note below file msdos gif cshw a zip due to the remarkable variety of pc graphics hardware any one of these viewers might not work on your particular machine if you can t get any of them to work you ll need to use one of the following conversion programs to convert jpeg to gif then view with your favorite gif viewer if you have hi color hardware don t use gif as the intermediate format try to find a targa capable viewer instead vpic is reputed to do the right thing with hi color displays the independent jpeg group s free jpeg converters are ftpable from simtel and mirror sites see note below file msdos graphics jpeg zip or jpeg zip if you have a and extended memory these files are dos compilations of the free source code described in section b they will convert jpeg to and from gif targa and ppm formats handmade software offers free jpeg gif conversion tools gif jpg jpg gif these are slow and are limited to conversion to and from gif format in particular you can t get bit color output from a jpeg the major advantage of these tools is that they will read and write hsi s proprietary jpeg format as well as the usenet standard jfif format since hsi format files are rather widespread on bbses this is a useful capability version of these tools is free prior versions were shareware get it from simtel and mirror sites see note below file msdos graphics gif jpg zip note do not use hsi format for files to be posted on internet since it is not readable on non pc platforms handmade software also has a shareware image conversion and manipulation package image alchemy this will translate jpeg files both jfif and hsi formats to and from many other image formats it can also display images a demo version of image alchemy version is available from simtel and mirror sites see note below file msdos graphics alch zip note about simtel the internet s key archive site for pc related programs is simtel full name wsmr simtel army mil simtel runs a non unix system with weird directory names where this document refers to directory eg msdos graphics at simtel that really means pd msdos graphics if you are not physically on milnet you should expect rather slow ftp transfer rates from simtel there are several internet sites that maintain copies mirrors of the simtel archives most ftp users should go to one of the mirror sites instead a popular usa mirror site is oak oakland edu which keeps simtel files in eg pub msdos graphics if you have no ftp capability you can retrieve files from simtel by e mail see informational postings in comp archives msdos announce to find out how if you are outside the usa consult the same newsgroup to learn where your nearest simtel mirror is microsoft windows there are several windows programs capable of displaying jpeg images windows viewers are generally slower than dos viewers on the same hardware due to windows system overhead note that you can run the dos conversion programs described above inside a windows dos window the newest entry is winecj which is free and extremely fast version is available from ftp rahul net file pub bryanw pc jpeg wecj zip requires windows and or more colors mode this is a no frills viewer with the bad habit of hogging the machine completely while it decodes and the image quality is noticeably worse than other viewers but it s so fast you ll use it anyway at least for previewing jview is freeware fairly fast has good on line help and can write out the decompressed image in windows bmp format but it can t create new jpeg files and it doesn t view gifs jview also lacks some other useful features of the shareware viewers such as brightness adjustment but it s an excellent basic viewer the current version is available from ftp cica indiana edu file pub pc win desktop jview zip mirrors of this archive can be found at some other internet sites including wuarchive wustl edu winjpeg shareware displays jpeg gif targa tiff and bmp image files it can write all of these formats too so it can be used as a converter it has some other nifty features including color balance adjustment and slideshow the current version is available from simtel and mirror sites see note above file msdos windows winjp zip this is a slow compatible version if you register you ll get the only version which is roughly faster colorview is another shareware entry this was an early and promising contender but it has not been updated in some time and at this point it has no real advantages over winjpeg if you want to try it anyway the current version is available from ftp cica indiana edu file pub pc win desktop cview zip i understand that a new version will be appearing once the authors are finished with colorview for dos dvpeg see dos heading also works under windows but only in full screen mode not in a window os the following files are available from hobbes nmsu edu note check pub uploads for more recent versions the hobbes moderator is not very fast about moving uploads into their permanent directories pub os x graphics jpegv zip bit version of free ijg conversion programs version pub os all graphics jpeg zip bit version of same for os x pub os x graphics imgarc zip image archiver image conversion viewing with pm graphical interface strong on conversion functions viewing is a bit weaker shareware pub os x graphics pmjpeg zip pmjpeg os x port of winjpeg a popular viewer for windows see description in windows section shareware pub os x graphics pmview zip pmview jpeg gif bmp viewer gif viewing very fast jpeg viewing fast if you have huge amounts of ram otherwise about the same speed as the above programs strong bit display support shareware macintosh most mac jpeg programs rely on apple s jpeg implementation which is part of the quicktime system extension so you need to have quicktime installed to use quicktime you need a or better cpu and you need to be running system or later if you re running system you must also install the bit quickdraw extension this is built in on system you can get quicktime by ftp from ftp apple com file dts mac quicktime quicktime hqx as of this file contains quicktime which is better than qt in several ways with respect to jpeg it is marginally faster and considerably less prone to crash when fed a corrupt jpeg file however some applications seem to have compatibility problems with qt mac users should keep in mind that quicktime s jpeg format pict jpeg is not the same as the usenet standard jfif jpeg format see section for details if you post images on usenet make sure they are in jfif format most of the programs mentioned below can generate either format the first choice is probably jpegview a free program for viewing images that are in jfif format pict jpeg format or gif format it also can convert between the two jpeg formats the current version is a big improvement over prior versions get it from sumex aim stanford edu file info mac app jpeg view hqx requires system and quicktime on bit displays jpegview usually produces the best color image quality of all the currently available mac jpeg viewers jpegview can view large images in much less memory than other mac viewers in fact it s the only one that can deal with jpeg images much over x pixels on a typical mb mac given a large image jpegview automatically scales it down to fit on the screen rather than presenting scroll bars like most other viewers you can zoom in on any desired portion though some people like this behavior some don t overall jpegview s user interface is very well thought out gifconverter a shareware image viewer converter supports jfif and pict jpeg as well as gif and several other image formats the latest version is get it from sumex aim stanford edu file info mac art gif gif converter hqx requires system or later gifconverter is not better than jpegview as a plain jpeg gif viewer but it has much more extensive image manipulation and format conversion capabilities so you may find it worth its shareware fee if you do a lot of playing around with images also the newest version of gifconverter can load and save jfif images without quicktime so it is your best bet if your machine is too old to run quicktime but it s faster with quicktime note if gifconverter runs out of memory trying to load a large jpeg try converting the file to gif with jpeg convert then viewing the gif version jpeg convert a mac version of the free ijg jpeg conversion utilities is available from sumex aim stanford edu file info mac app jpeg convert hqx this will run on any mac but it only does file conversion not viewing you can use it in conjunction with any gif viewer previous versions of this faq recommended imagery jpeg v a jpeg gif converter based on an old version of the ijg code if you are using this program you definitely should replace it with jpeg convert apple s free program pictpixie can view images in jfif quicktime jpeg and gif format and can convert between these formats you can get pictpixie from ftp apple com file dts mac quicktime qt stuff pictpixie hqx requires quicktime pictpixie was intended as a developer s tool and it s really not the best choice unless you like to fool around with quicktime some of its drawbacks are that it requires lots of memory it produces relatively poor color image quality on anything less than a bit display and it has a relatively unfriendly user interface worse pictpixie is an unsupported program meaning it has some minor bugs that apple does not intend to fix there is an old version of pictpixie called pictcompressor floating around the net if you have this you should trash it as it s even buggier also the quicktime starter kit includes a much cleaned up descendant of pictpixie called picture compressor note that picture compressor is not free and may not be distributed on the net storm technology s picture decompress is a free jpeg viewer converter this rather old program is inferior to the above programs in many ways but it will run without system or quicktime so you may be forced to use it on older systems it does need bit quickdraw so really old machines can t use it you can get it from sumex aim stanford edu file info mac app picture decompress hqx you must set the file type of a downloaded image file to jpeg to allow picture decompress to open it if your machine is too old to run bit quickdraw a mac plus for instance gifconverter is your only choice for single program jpeg viewing if you don t want to pay for gifconverter use jpeg convert and a free gif viewer more and more commercial mac applications are supporting jpeg although not all can deal with the usenet standard jfif format adobe photoshop version or later can read and write jfif format jpeg files use the jpeg plug in from the acquire menu you must set the file type of a downloaded jpeg file to jpeg to allow photoshop to recognize it amiga most programs listed in this section are stored in the aminet archive at amiga physik unizh ch there are many mirror sites of this archive and you should try to use the closest one in the usa a good choice is wuarchive wustl edu look under mirrors amiga physik unizh ch hamlab plus is an excellent jpeg viewer converter as well as being a general image manipulation tool it s cheap shareware and can read several formats besides jpeg the current version is a demo version is available from amiga physik unizh ch and mirror sites file amiga gfx edit hamlab d lha the demo version will crop images larger than x but it is otherwise fully functional rend shareware is an image renderer that can display jpeg ilbm and gif images the program can be used to create animations even capturing frames on the fly from rendering packages like lightwave the current version is available from amiga physik unizh ch and mirror sites file amiga os gfx rend lha note although this directory is supposedly for amigados programs the program will also run under amigados or viewtek is a free jpeg ilbm gif anim viewer the current version is available from amiga physik unizh ch and mirror sites file amiga gfx show viewtek lha if you re willing to spend real money there are several commercial packages that support jpeg two are written by thomas krehbiel the author of rend and viewtek these are cinemorph a standalone image morphing package and imagefx an impressive bit image capture conversion editing painting effects and prepress package that also includes cinemorph both are distributed by great valley products art department professional adpro from asdg inc is the most widely used commercial image manipulation software for amigas imagemaster from black belt systems is another well regarded commercial graphics package with jpeg support the free ijg jpeg software is available compiled for amigas from amiga physik unizh ch and mirror sites in directory amiga gfx conv file amigajpegv lha these programs convert jpeg to from ppm gif targa formats the amiga world is heavily infested with quick and dirty jpeg programs many based on an ancient beta test version of the free ijg jpeg software thanks to a certain magazine that published same on its disk of the month without so much as notifying the authors among these are augjpeg newamyjpeg vjpeg and probably others i have not even heard of in my opinion anything older than ijg version march is not worth the disk space it s stored on if you have such a program trash it and get something newer atari st the free ijg jpeg software is available compiled for atari st tt etc from atari archive umich edu file atari graphics jpeg bin zoo these programs convert jpeg to from ppm gif targa formats for monochrome st monitors try mgif which manages to achieve four level grayscale effect by flickering version reads jpeg files available from atari archive umich edu file atari graphics mgif b zoo i have not heard of any other free or shareware jpeg capable viewers for ataris but surely there must be some by now pointers appreciated acorn archimedes changefsi supplied with risc os version can convert from and view jpeg jfif format provision is also made to convert images to jpeg although this must be done from the cli rather than by double clicking recent versions since of the shareware program translator can handle jpeg along with about other image formats while older versions can be found on some archimedes bboards the current version is only available by registering with the author john kortink nutterbrink wj enschede the netherlands price dutch guilders about or pounds there s also a commercial product called jpeg which provides jpeg read write functionality and direct jpeg viewing as well as a host of other image format conversion and processing options this is more expensive but not necessarily better than the above programs contact dt software freepost cambridge uk tel portable software for almost any system if none of the above fits your situation you can obtain and compile the free jpeg conversion software described in b you ll also need a viewer program if your display is bits or less any gif viewer will do fine if you have a display with more color capability try to find a viewer that can read targa or ppm bit image files there are numerous commercial jpeg offerings with more popping up every day i recommend that you not spend money on one of these unless you find the available free or shareware software vastly too slow in that case purchase a hardware assisted product ask pointed questions about whether the product complies with the final jpeg standard and about whether it can handle the jfif file format many of the earliest commercial releases are not and never will be compatible with anyone else s files b if you are looking for source code to work with free portable c code for jpeg compression is available from the independent jpeg group which i lead a package containing our source code documentation and some small test files is available from several places the official archive site for this source code is ftp uu net or look under directory graphics jpeg the current release is jpegsrc v tar z this is a compressed tar file don t forget to retrieve in binary mode you can retrieve this file by ftp or uucp if you are on a pc and don t know how to cope with tar z format you may prefer zip format which you can find at simtel and mirror sites see note above file msdos graphics jpegsrc zip this file will also be available on compuserve in the graphsupport forum go pics library as jpsrc zip if you have no ftp access you can retrieve the source from your nearest comp sources misc archive version appeared as issues of volume if you don t know how to retrieve comp sources misc postings see the faq article how to find sources referred to at the top of section the free jpeg code provides conversion between jpeg jfif format and image files in gif pbmplus ppm pgm utah rle and truevision targa file formats the core compression and decompression modules can easily be reused in other programs such as image viewers the package is highly portable we have tested it on many machines ranging from pcs to crays we have released this software for both noncommercial and commercial use companies are welcome to use it as the basis for jpeg related products we do not ask a royalty although we do ask for an acknowledgement in product literature see the readme file in the distribution for details we hope to make this software industrial quality although as with anything that s free we offer no warranty and accept no liability the independent jpeg group is a volunteer organization if you d like to contribute to improving our software you are welcome to join what s all this hoopla about color quantization most people don t have full color bit per pixel display hardware typical display hardware stores or fewer bits per pixel so it can display or fewer distinct colors at a time to display a full color image the computer must map the image into an appropriate set of representative colors this process is called color quantization this is something of a misnomer color selection would be a better term we re stuck with the standard usage though clearly color quantization is a lossy process it turns out that for most images the details of the color quantization algorithm have much more impact on the final image quality than do any errors introduced by jpeg except at the very lowest jpeg quality settings since jpeg is a full color format converting a color jpeg image for display on bit or less hardware requires color quantization this is true for all color jpegs even if you feed a or less color gif into jpeg what comes out of the decompressor is not colors but thousands of colors this happens because jpeg s lossiness affects each pixel a little differently so two pixels that started with identical colors will probably come out with slightly different colors each original color gets smeared into a group of nearby colors therefore quantization is always required to display a color jpeg on a colormapped display regardless of the image source the only way to avoid quantization is to ask for gray scale output incidentally because of this effect it s nearly meaningless to talk about the number of colors used by a jpeg image even if you attempted to count the number of distinct pixel values different jpeg decoders would give you different results because of roundoff error differences i occasionally see posted images described as color jpeg this tells me that the poster a hasn t read this faq and b probably converted the jpeg from a gif jpegs can be classified as color or gray scale just like photographs but number of colors just isn t a useful concept for jpeg on the other hand a gif image by definition has already been quantized to or fewer colors a gif does have a definite number of colors in its palette and the format doesn t allow more than palette entries for purposes of usenet picture distribution gif has the advantage that the sender precomputes the color quantization so recipients don t have to this is also the disadvantage of gif you re stuck with the sender s quantization if the sender quantized to a different number of colors than what you can display you have to re quantize resulting in much poorer image quality than if you had quantized once from a full color image furthermore if the sender didn t use a high quality color quantization algorithm you re out of luck for this reason jpeg offers the promise of significantly better image quality for all users whose machines don t match the sender s display hardware jpeg s full color image can be quantized to precisely match the user s display hardware furthermore you will be able to take advantage of future improvements in quantization algorithms there is a lot of active research in this area or purchase better display hardware to get a better view of jpeg images you already have with a gif you re stuck forevermore with what was sent it s also worth mentioning that many gif viewing programs include rather shoddy quantization routines if you view a color gif on a color ega display for example you are probably getting a much worse image than you need to this is partly an inevitable consequence of doing two color quantizations one to create the gif one to display it but often it s also due to sloppiness jpeg conversion programs will be forced to use high quality quantizers in order to get acceptable results at all and in normal use they will quantize directly to the number of colors to be displayed thus jpeg is likely to provide better results than the average gif program for low color resolution displays as well as high resolution ones finally an ever growing number of people have better than bit display hardware already bit hi color pc displays true bit displays on workstations and macintoshes etc for these people gif is already obsolete as it cannot represent an image to the full capabilities of their display jpeg images can drive these displays much more effectively thus jpeg is an all around better choice than gif for representing images in a machine independent fashion how does jpeg work the buzz words to know are chrominance subsampling discrete cosine transforms coefficient quantization and huffman or arithmetic entropy coding this article s long enough already so i m not going to say more than that here for technical information see the comp compression faq this is available from the news answers archive at rtfm mit edu in files pub usenet news answers compression faq part if you need help in using the news answers archive see the top of this article what about lossless jpeg there s a great deal of confusion on this subject the jpeg committee did define a truly lossless compression algorithm i e one that guarantees the final output is bit for bit identical to the original input however this lossless mode has almost nothing in common with the regular lossy jpeg algorithm and it offers much less compression at present very few implementations of lossless jpeg exist and all of them are commercial saying q to the free jpeg software does not get you a lossless image what it does get rid of is deliberate information loss in the coefficient quantization step there is still a good deal of information loss in the color subsampling step with the v free jpeg code you can also say sample x to turn off subsampling keep in mind that many commercial jpeg implementations cannot cope with the resulting file even with both quantization and subsampling turned off the regular jpeg algorithm is not lossless because it is subject to roundoff errors in various calculations the maximum error is a few counts in any one pixel value it s highly unlikely that this could be perceived by the human eye but it might be a concern if you are doing machine processing of an image at this minimum loss setting regular jpeg produces files that are perhaps half the size of an uncompressed bit per pixel image true lossless jpeg provides roughly the same amount of compression but it guarantees bit for bit accuracy if you have an application requiring lossless storage of images with less than bits per pixel per color component you may want to look into the jbig bilevel image compression standard this performs better than jpeg lossless on such images jpeg lossless is superior to jbig on images with or more bits per pixel furthermore jpeg is public domain at least with a huffman back end while the jbig techniques are heavily covered by patents why all the argument about file formats strictly speaking jpeg refers only to a family of compression algorithms it does not refer to a specific image file format the jpeg committee was prevented from defining a file format by turf wars within the international standards organizations since we can t actually exchange images with anyone else unless we agree on a common file format this leaves us with a problem in the absence of official standards a number of jpeg program writers have just gone off to do their own thing and as a result their programs aren t compatible with anybody else s the closest thing we have to a de facto standard jpeg format is some work that s been coordinated by people at c cube microsystems they have defined two jpeg based file formats jfif jpeg file interchange format a low end format that transports pixels and not much else tiff jpeg aka tiff an extension of the aldus tiff format tiff is a high end format that will let you record just about everything you ever wanted to know about an image and a lot more besides tiff is a lot more complex than jfif and may well prove less transportable because different vendors have historically implemented slightly different and incompatible subsets of tiff it s not likely that adding jpeg to the mix will do anything to improve this situation both of these formats were developed with input from all the major vendors of jpeg related products it s reasonably likely that future commercial products will adhere to one or both standards i believe that usenet should adopt jfif as the replacement for gif in picture postings jfif is simpler than tiff and is available now the tiff spec has only recently been officially adopted and it is still unusably vague on some crucial details even when tiff jpeg is well defined the jfif format is likely to be a widely supported lowest common denominator tiff jpeg files may never be as transportable a particular case that people may be interested in is apple s quicktime software for the macintosh quicktime uses a jfif compatible format wrapped inside the mac specific pict structure conversion between jfif and quicktime jpeg is pretty straightforward and several mac programs are available to do it see mac portion of section a if you have an editor that handles binary files you can strip a quicktime jpeg pict down to jfif by hand see section for details another particular case is handmade software s programs gif jpg jpg gif and image alchemy these programs are capable of reading and writing jfif format by default though they write a proprietary format developed by hsi this format is not readable by any non hsi programs and should not be used for usenet postings use the j switch to get jfif output this applies to old versions of these programs the current releases emit jfif format by default you still should be careful not to post hsi format files unless you want to get flamed by people on non pc platforms how do i recognize which file format i have and what do i do about it if you have an alleged jpeg file that your software won t read it s likely to be hsi format or some other proprietary jpeg based format you can tell what you have by inspecting the first few bytes of the file a jfif standard file will start with the characters hex ff d ff e followed by two variable bytes often hex followed by jfif if you see ff d at the start but not the rest of it you may have a raw jpeg file this is probably decodable as is by jfif software it s worth a try anyway hsi files start with hsi you re out of luck unless you have hsi software portions of the file may look like plain jpeg data but they won t decompress properly with non hsi programs a macintosh pict file if jpeg compressed will have a couple hundred bytes of header followed by a jfif header scan for jfif strip off everything before the ff d and you should be able to read it anything else it s a proprietary format or not jpeg at all if you are lucky the file may consist of a header and a raw jpeg data stream if you can identify the start of the jpeg data stream look for ff d try stripping off everything before that in uuencoded usenet postings the characteristic jfif pattern is begin line m c x whereas uuencoded hsi files will start with begin line m i if you learn to check for the former you can save yourself the trouble of downloading non jfif files what about arithmetic coding the jpeg spec defines two different back end modules for the final output of compressed data either huffman coding or arithmetic coding is allowed the choice has no impact on image quality but arithmetic coding usually produces a smaller compressed file on typical images arithmetic coding produces a file or percent smaller than huffman coding all the file size numbers previously cited are for huffman coding unfortunately the particular variant of arithmetic coding specified by the jpeg standard is subject to patents owned by ibm at t and mitsubishi thus you cannot legally use arithmetic coding unless you obtain licenses from these companies the fair use doctrine allows people to implement and test the algorithm but actually storing any images with it is dubious at best at least in the short run i recommend that people not worry about arithmetic coding the space savings isn t great enough to justify the potential legal hassles in particular arithmetic coding should not be used for any images to be exchanged on usenet there is some small chance that the legal situation may change in the future stay tuned for further details does loss accumulate with repeated compression decompression it would be nice if having compressed an image with jpeg you could decompress it manipulate it crop off a border say and recompress it without any further image degradation beyond what you lost initially unfortunately this is not the case in general recompressing an altered image loses more information though usually not as much as was lost the first time around the next best thing would be that if you decompress an image and recompress it without changing it then there is no further loss i e you get an identical jpeg file even this is not true at least not with the current free jpeg software it s essentially a problem of accumulation of roundoff error if you repeatedly compress and decompress the image will eventually degrade to where you can see visible changes from the first generation output it usually takes many such cycles to get visible change one of the things on our to do list is to see if accumulation of error can be avoided or limited but i am not optimistic about it in any case the most that could possibly be guaranteed would be that compressing the unmodified full color output of djpeg at the original quality setting would introduce no further loss even such simple changes as cropping off a border could cause further roundoff error degradation if you re wondering why it s because the pixel block boundaries move if you cropped off only multiples of pixels you might be safe but that s a mighty limited capability the bottom line is that jpeg is a useful format for archival storage and transmission of images but you don t want to use it as an intermediate format for sequences of image manipulation steps use a lossless format ppm rle tiff etc while working on the image then jpeg it when you are ready to file it away aside from avoiding degradation you will save a lot of compression decompression time this way what are some rules of thumb for converting gif images to jpeg as stated earlier you will lose some amount of image information if you convert an existing gif image to jpeg if you can obtain the original full color data the gif was made from it s far better to make a jpeg from that but if you need to save space and have only the gif to work from here are some suggestions for getting maximum space savings with minimum loss of quality the first rule when converting a gif library is to look at each jpeg to make sure you are happy with it before throwing away the corresponding gif that will give you a chance to re do the conversion with a higher quality setting if necessary some gifs may be better left as gifs as explained in section in particular cartoon type gifs with sixteen or fewer colors don t convert well you may find that a jpeg file of reasonable quality will be larger than the gif so check the sizes too experience to date suggests that large high visual quality gifs are the best candidates for conversion to jpeg they chew up the most storage so offer the most potential savings and they convert to jpeg with least degradation don t waste your time converting any gif much under kbytes also don t expect jpeg files converted from gifs to be as small as those created directly from full color originals to maintain image quality you may have to let the converted files be as much as twice as big as straight through jpeg files would be i e shoot for or rd the size of the gif file not th as suggested in earlier comparisons many people have developed an odd habit of putting a large constant color border around a gif image while useless this was nearly free in terms of storage cost in gif files it is not free in jpeg files and the sharp border boundary can create visible artifacts ghost edges do yourself a favor and crop off any border before jpeging if you are on an x windows system xv s manual and automatic cropping functions are a very painless way to do this cjpeg s default q setting of is appropriate for full color input but for gif inputs q settings of to often seem to be necessary to avoid image degradation if you apply smoothing as suggested below the higher q setting may not be necessary color gifs of photographs or complex artwork are usually dithered to fool your eye into seeing more than the colors that gif can actually store if you enlarge the image you will see that adjacent pixels are often of significantly different colors at normal size the eye averages these pixels together to produce the illusion of an intermediate color value the trouble with dithering is that to jpeg it looks like high spatial frequency color noise and jpeg can t compress noise very well the resulting jpeg file is both larger and of lower image quality than what you would have gotten from jpeging the original full color image if you had it to get around this you want to smooth the gif image before compression smoothing averages together nearby pixels thus approximating the color that you thought you saw anyway and in the process getting rid of the rapid color changes that give jpeg trouble appropriate use of smoothing will often let you avoid using a high q factor thus further reducing the size of the compressed file while still obtaining a better looking output image than you d get without smoothing with the v free jpeg software or products based on it a simple smoothing capability is built in try smooth or so when converting gifs values of to seem to work well for high quality gifs heavy handed dithering may require larger smoothing factors if you can see regular fine scale patterns on the gif image even without enlargement then strong smoothing is definitely called for too large a smoothing factor will blur the output image which you don t want if you are an image processing wizard you can also do smoothing with a separate filtering program such as pnmconvol from the pbmplus package however cjpeg s built in smoother is a lot faster than pnmconvol the upshot of all this is that cjpeg quality smooth is probably a good starting point for converting gifs but if you really care about the image you ll want to check the results and maybe try a few other settings for more information about jpeg in general or the free jpeg software in particular contact the independent jpeg group at jpeg info uunet uu net tom lane organizer independent jpeg group internet tgl cs cmu edu bitnet tgl cs cmu edu carnegie
