home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / converters / makejpeg_2 / FAQ / JPEGFAQ1 next >
Text File  |  1995-11-07  |  47KB  |  869 lines

  1. Archive-name: jpeg-faq/part1
  2. Posting-Frequency: every 14 days
  3. Last-modified: 3 September 1995
  4.  
  5. This article answers Frequently Asked Questions about JPEG image compression.
  6. This is part 1, covering general questions and answers about JPEG.  Part 2
  7. gives system-specific hints and program recommendations.  As always,
  8. suggestions for improvement of this FAQ are welcome.
  9.  
  10. New since version of 6 August 1995:
  11.   * Updated entry on progressive JPEG.
  12.  
  13.  
  14. This article includes the following sections:
  15.  
  16. Basic questions:
  17.  
  18. [1] What is JPEG?
  19. [2] Why use JPEG?
  20. [3] When should I use JPEG, and when should I stick with GIF?
  21. [4] How well does JPEG compress images?
  22. [5] What are good "quality" settings for JPEG?
  23. [6] Where can I get JPEG software?
  24. [7] How do I view JPEG images posted on Usenet?
  25.  
  26. More advanced questions:
  27.  
  28. [8] What is color quantization?
  29. [9] What are some rules of thumb for converting GIF images to JPEG?
  30. [10] Does loss accumulate with repeated compression/decompression?
  31. [11] What is progressive JPEG?
  32. [12] Isn't there a lossless JPEG?
  33. [13] Why all the argument about file formats?
  34. [14] How do I recognize which file format I have, and what do I do about it?
  35. [15] How does JPEG work?
  36. [16] What about arithmetic coding?
  37. [17] Could an FPU speed up JPEG?  How about a DSP chip?
  38. [18] Isn't there an M-JPEG standard for motion pictures?
  39. [19] What if I need more than 8-bit precision?
  40.  
  41. Miscellaneous:
  42.  
  43. [20] Where are FAQ lists archived?
  44.  
  45.  
  46. This article and its companion are posted every 2 weeks.  If you can't find
  47. part 2, you can get it from the news.answers archive at rtfm.mit.edu
  48. (see "[20] Where are FAQ lists archived?").  Part 2 changes very frequently;
  49. get a new copy if the one you are reading is more than a couple months old.
  50.  
  51. ------------------------------
  52.  
  53. Subject: [1] What is JPEG?
  54.  
  55. JPEG (pronounced "jay-peg") is a standardized image compression mechanism.
  56. JPEG stands for Joint Photographic Experts Group, the original name of the
  57. committee that wrote the standard.
  58.  
  59. JPEG is designed for compressing either full-color or gray-scale images
  60. of natural, real-world scenes.  It works well on photographs, naturalistic
  61. artwork, and similar material; not so well on lettering, simple cartoons,
  62. or line drawings.  JPEG handles only still images, but there is a related
  63. standard called MPEG for motion pictures.
  64.  
  65. JPEG is "lossy," meaning that the decompressed image isn't quite the same as
  66. the one you started with.  (There are lossless image compression algorithms,
  67. but JPEG achieves much greater compression than is possible with lossless
  68. methods.)  JPEG is designed to exploit known limitations of the human eye,
  69. notably the fact that small color changes are perceived less accurately than
  70. small changes in brightness.  Thus, JPEG is intended for compressing images
  71. that will be looked at by humans.  If you plan to machine-analyze your
  72. images, the small errors introduced by JPEG may be a problem for you, even
  73. if they are invisible to the eye.
  74.  
  75. A useful property of JPEG is that the degree of lossiness can be varied by
  76. adjusting compression parameters.  This means that the image maker can trade
  77. off file size against output image quality.  You can make *extremely* small
  78. files if you don't mind poor quality; this is useful for applications such
  79. as indexing image archives.  Conversely, if you aren't happy with the output
  80. quality at the default compression setting, you can jack up the quality
  81. until you are satisfied, and accept lesser compression.
  82.  
  83. Another important aspect of JPEG is that decoders can trade off decoding
  84. speed against image quality, by using fast but inaccurate approximations to
  85. the required calculations.  Some viewers obtain remarkable speedups in this
  86. way.
  87.  
  88. ------------------------------
  89.  
  90. Subject: [2] Why use JPEG?
  91.  
  92. There are two good reasons: to make your image files smaller, and to store
  93. 24-bit-per-pixel color data instead of 8-bit-per-pixel data.
  94.  
  95. Making image files smaller is a win for transmitting files across networks
  96. and for archiving libraries of images.  Being able to compress a 2 Mbyte
  97. full-color file down to, say, 100 Kbytes makes a big difference in disk
  98. space and transmission time!  And JPEG can easily provide 20:1 compression
  99. of full-color data.  If you are comparing GIF and JPEG, the size ratio is
  100. usually more like 4:1 (see "[4] How well does JPEG compress images?").
  101.  
  102. If your viewing software doesn't support JPEG directly, you'll have to
  103. convert JPEG to some other format to view the image.  Even with a
  104. JPEG-capable viewer, it takes longer to decode and view a JPEG image than
  105. to view an image of a simpler format such as GIF.  Thus, using JPEG is
  106. essentially a time/space tradeoff: you give up some time in order to store
  107. or transmit an image more cheaply.  But it's worth noting that when network
  108. or telephone transmission is involved, the time savings from transferring a
  109. shorter file can be greater than the time needed to decompress the file.
  110.  
  111. The second fundamental advantage of JPEG is that it stores full color
  112. information: 24 bits/pixel (16 million colors).  GIF, the other image format
  113. widely used on Usenet, can only store 8 bits/pixel (256 or fewer colors).
  114. GIF is reasonably well matched to inexpensive computer displays --- most
  115. run-of-the-mill PCs can't display more than 256 distinct colors at once.
  116. But full-color hardware is getting cheaper all the time, and JPEG images
  117. look *much* better than GIFs on such hardware.  Within a couple of years,
  118. GIF will probably seem as obsolete as black-and-white MacPaint format does
  119. today.  Furthermore, JPEG is far more useful than GIF for exchanging images
  120. among people with widely varying display hardware, because it avoids
  121. prejudging how many colors to use (see "[8] What is color quantization?").
  122. Hence JPEG is considerably more appropriate than GIF for use as a Usenet
  123. posting standard.
  124.  
  125. A lot of people are scared off by the term "lossy compression".  But when
  126. it comes to representing real-world scenes, *no* digital image format can
  127. retain all the information that impinges on your eyeball.  By comparison
  128. with the real-world scene, JPEG loses far less information than GIF.
  129. The real disadvantage of lossy compression is that if you repeatedly
  130. compress and decompress an image, you lose a little quality each time
  131. (see "[10] Does loss accumulate with repeated compression/decompression?").
  132. This is a serious objection for some applications but matters not at all
  133. for many others.
  134.  
  135. ------------------------------
  136.  
  137. Subject: [3] When should I use JPEG, and when should I stick with GIF?
  138.  
  139. JPEG is *not* going to displace GIF entirely; for some types of images,
  140. GIF is superior in image quality, file size, or both.  One of the first
  141. things to learn about JPEG is which kinds of images to apply it to.
  142.  
  143. Generally speaking, JPEG is superior to GIF for storing full-color or
  144. gray-scale images of "realistic" scenes; that means scanned photographs and
  145. similar material.  Any continuous variation in color, such as occurs in
  146. highlighted or shaded areas, will be represented more faithfully and in less
  147. space by JPEG than by GIF.
  148.  
  149. GIF does significantly better on images with only a few distinct colors,
  150. such as line drawings and simple cartoons.  Not only is GIF lossless for
  151. such images, but it often compresses them more than JPEG can.  For example,
  152. large areas of pixels that are all *exactly* the same color are compressed
  153. very efficiently indeed by GIF.  JPEG can't squeeze such data as much as GIF
  154. does without introducing visible defects.  (One implication of this is that
  155. large single-color borders are quite cheap in GIF files, while they are best
  156. avoided in JPEG files.)
  157.  
  158. Computer-drawn images, such as ray-traced scenes, usually fall between
  159. photographs and cartoons in terms of complexity.  The more complex and
  160. subtly rendered the image, the more likely that JPEG will do well on it.
  161. The same goes for semi-realistic artwork (fantasy drawings and such).
  162. But icons that use only a few colors are handled better by GIF.
  163.  
  164. JPEG has a hard time with very sharp edges: a row of pure-black pixels
  165. adjacent to a row of pure-white pixels, for example.  Sharp edges tend to
  166. come out blurred unless you use a very high quality setting.  Edges this
  167. sharp are rare in scanned photographs, but are fairly common in GIF files:
  168. consider borders, overlaid text, etc.  The blurriness is particularly
  169. objectionable with text that's only a few pixels high.  If you have a GIF
  170. with a lot of small-size overlaid text, don't JPEG it.  (If you want to
  171. attach descriptive text to a JPEG image, put it in as a comment rather than
  172. trying to overlay it on the image.  Most recent JPEG software can deal with
  173. textual comments in a JPEG file, although older viewers may just ignore the
  174. comments.)
  175.  
  176. Plain black-and-white (two level) images should never be converted to JPEG;
  177. they violate all of the conditions given above.  You need at least about
  178. 16 gray levels before JPEG is useful for gray-scale images.  It should also
  179. be noted that GIF is lossless for gray-scale images of up to 256 levels,
  180. while JPEG is not.
  181.  
  182. If you have a large library of GIF images, you may want to save space by
  183. converting the GIFs to JPEG.  This is trickier than it may seem --- even
  184. when the GIFs contain photographic images, they are actually very poor
  185. source material for JPEG, because the images have been color-reduced.
  186. Non-photographic images should generally be left in GIF form.  Good-quality
  187. photographic GIFs can often be converted with no visible quality loss, but
  188. only if you know what you are doing and you take the time to work on each
  189. image individually.  Otherwise you're likely to lose a lot of image quality
  190. or waste a lot of disk space ... quite possibly both.  Read sections 8 and 9
  191. if you want to convert GIFs to JPEG.
  192.  
  193. ------------------------------
  194.  
  195. Subject: [4] How well does JPEG compress images?
  196.  
  197. Very well indeed, when working with its intended type of image (photographs
  198. and suchlike).  For full-color images, the uncompressed data is normally 24
  199. bits/pixel.  The best known lossless compression methods can compress such
  200. data about 2:1 on average.  JPEG can typically achieve 10:1 to 20:1
  201. compression without visible loss, bringing the effective storage requirement
  202. down to 1 to 2 bits/pixel.  30:1 to 50:1 compression is possible with small
  203. to moderate defects, while for very-low-quality purposes such as previews or
  204. archive indexes, 100:1 compression is quite feasible.  An image compressed
  205. 100:1 with JPEG takes up the same space as a full-color one-tenth-scale
  206. thumbnail image, yet it retains much more detail than such a thumbnail.
  207.  
  208. For comparison, a GIF version of the same image would start out by
  209. sacrificing most of the color information to reduce the image to 256 colors
  210. (8 bits/pixel).  This provides 3:1 compression.  GIF has additional "LZW"
  211. compression built in, but LZW doesn't work very well on typical photographic
  212. data; at most you may get 5:1 compression overall, and it's not at all
  213. uncommon for LZW to be a net loss (i.e., less than 3:1 overall compression).
  214. LZW *does* work well on simpler images such as line drawings, which is why
  215. GIF handles that sort of image so well.  When a JPEG file is made from
  216. full-color photographic data, using a quality setting just high enough to
  217. prevent visible loss, the JPEG will typically be a factor of four or five
  218. smaller than a GIF file made from the same data.
  219.  
  220. Gray-scale images do not compress by such large factors.  Because the human
  221. eye is much more sensitive to brightness variations than to hue variations,
  222. JPEG can compress hue data more heavily than brightness (gray-scale) data.
  223. A gray-scale JPEG file is generally only about 10%-25% smaller than a
  224. full-color JPEG file of similar visual quality.  But the uncompressed
  225. gray-scale data is only 8 bits/pixel, or one-third the size of the color
  226. data, so the calculated compression ratio is much lower.  The threshold of
  227. visible loss is often around 5:1 compression for gray-scale images.
  228.  
  229. The exact threshold at which errors become visible depends on your viewing
  230. conditions.  The smaller an individual pixel, the harder it is to see an
  231. error; so errors are more visible on a computer screen (at maybe 70
  232. dots/inch) than on a high-quality color printout (300 or more dots/inch).
  233. Thus a higher-resolution image can tolerate more compression ... which is
  234. fortunate considering it's much bigger to start with.  The numbers quoted
  235. above are typical for screen viewing.  Also note that the threshold of
  236. visible error varies considerably across images.
  237.  
  238. ------------------------------
  239.  
  240. Subject: [5] What are good "quality" settings for JPEG?
  241.  
  242. Most JPEG compressors let you pick a file size vs. image quality tradeoff by
  243. selecting a quality setting.  There seems to be widespread confusion about
  244. the meaning of these settings.  "Quality 95" does NOT mean "keep 95% of the
  245. information", as some have claimed.  The quality scale is purely arbitrary;
  246. it's not a percentage of anything.
  247.  
  248. In fact, quality scales aren't even standardized across JPEG programs.
  249. The quality settings discussed in this article apply to the free IJG JPEG
  250. software (described in part 2), and to many programs based on it.  Other
  251. JPEG implementations, notably Apple's and HSI's, use completely different
  252. quality scales; for instance, Apple's scale runs from 0-4, not 0-100.  Some
  253. programs don't even provide a numeric scale, just "high"/"medium"/"low"
  254. style choices.  (Fortunately, this doesn't prevent different implementations
  255. from exchanging compressed files.)
  256.  
  257. In most cases the user's goal is to pick the lowest quality setting, or
  258. smallest file size, that decompresses into an image indistinguishable from
  259. the original.  This setting will vary from one image to another and from one
  260. observer to another, but here are some rules of thumb.
  261.  
  262. For good-quality, full-color source images, the default IJG quality setting
  263. (Q 75) is very often the best choice.  This setting is about the lowest you
  264. can go without expecting to see defects in a typical image.  Try Q 75 first;
  265. if you see defects, then go up.
  266.  
  267. If the image was less than perfect quality to begin with, you might be able
  268. to drop down to Q 50 without objectionable degradation.  On the other hand,
  269. you might need to go to a *higher* quality setting to avoid further loss.
  270. This is often necessary if the image contains dithering or moire patterns
  271. (see "[9] What are some rules of thumb for converting GIF images to JPEG?").
  272.  
  273. Except for experimental purposes, never go above about Q 95; using Q 100
  274. will produce a file two or three times as large as Q 95, but of hardly any
  275. better quality.  If you see a file made with Q 100, it's a pretty sure sign
  276. that the maker didn't know what he/she was doing.
  277.  
  278. If you want a very small file (say for preview or indexing purposes) and are
  279. prepared to tolerate large defects, a Q setting in the range of 5 to 10 is
  280. about right.  Q 2 or so may be amusing as "op art".  (It's worth mentioning
  281. that the current IJG software is not optimized for such low quality factors.
  282. Future versions may achieve better image quality for the same file size at
  283. low quality settings.)
  284.  
  285. ------------------------------
  286.  
  287. Subject: [6] Where can I get JPEG software?
  288.  
  289. See part 2 of this FAQ for recommendations about programs for particular
  290. systems.  Part 2 also tells where to find free C source code for
  291. implementing JPEG, in case you want to write your own programs using JPEG.
  292.  
  293. The comp.graphics.* FAQs and the alt.binaries.pictures FAQ are more general
  294. sources of information about graphics programs available on the Internet
  295. (see "[20] Where are FAQ lists archived?").
  296.  
  297. ------------------------------
  298.  
  299. Subject: [7] How do I view JPEG images posted on Usenet?
  300.  
  301. Image files posted on the alt.binaries.pictures.* newsgroups are usually
  302. "uuencoded".  Uuencoding converts binary image data into text that can
  303. safely be posted.  Most posters also divide large posts into multiple parts,
  304. since some news software can't cope with big articles.  Before your viewer
  305. will recognize the image, you must combine the parts into one file and run
  306. the text through a uudecode program.  (This is all true for GIF as well as
  307. JPEG, by the way.)  There are programs available to automate this process.
  308.  
  309. For more info see the alt.binaries.pictures FAQ, which is available from
  310. rtfm.mit.edu:/pub/usenet/news.answers/pictures-faq/part[1-3], or on WWW at
  311. http://www.cis.ohio-state.edu/hypertext/faq/usenet/pictures-faq/top.html
  312. (see also "[20] Where are FAQ lists archived?").
  313.  
  314. ------------------------------
  315.  
  316. Subject: [8] What is color quantization?
  317.  
  318. Most people don't have full-color (24 bit per pixel) display hardware.
  319. Typical display hardware stores 8 or fewer bits per pixel, so it can display
  320. 256 or fewer distinct colors at a time.  To display a full-color image, the
  321. computer must choose an appropriate set of representative colors and map the
  322. image into these colors.  This process is called "color quantization".
  323. (This is something of a misnomer; "color selection" or "color reduction"
  324. would be a better term.  But we're stuck with the standard usage.)
  325.  
  326. Clearly, color quantization is a lossy process.  It turns out that for most
  327. images, the details of the color quantization algorithm have *much* more
  328. impact on the final image quality than do any errors introduced by JPEG
  329. itself (except at the very lowest JPEG quality settings).  Making a good
  330. color quantization method is a black art, and no single algorithm is best
  331. for all images.
  332.  
  333. Since JPEG is a full-color format, displaying a color JPEG image on
  334. 8-bit-or-less hardware requires color quantization.  The speed and image
  335. quality of a JPEG viewer running on such hardware are largely determined by
  336. its quantization algorithm.  Depending on whether a quick-and-dirty or
  337. good-but-slow method is used, you'll see great variation in image quality
  338. among viewers on 8-bit displays, much more than occurs on 24-bit displays.
  339.  
  340. On the other hand, a GIF image has already been quantized to 256 or fewer
  341. colors.  (A GIF always has a specific number of colors in its palette, and
  342. the format doesn't allow more than 256 palette entries.)  GIF has the
  343. advantage that the image maker precomputes the color quantization, so
  344. viewers don't have to; this is one of the things that make GIF viewers
  345. faster than JPEG viewers.  But this is also the *disadvantage* of GIF:
  346. you're stuck with the image maker's quantization.  If the maker quantized to
  347. a different number of colors than what you can display, you'll either waste
  348. display capability or else have to requantize to reduce the number of colors
  349. (which usually results in much poorer image quality than quantizing once
  350. from a full-color image).  Furthermore, if the maker didn't use a
  351. high-quality color quantization algorithm, you're out of luck --- the image
  352. is ruined.
  353.  
  354. For this reason, JPEG promises significantly better image quality than GIF
  355. for all users whose machines don't match the image maker's display hardware.
  356. JPEG's full color image can be quantized to precisely match the viewer's
  357. display hardware.  Furthermore, you will be able to take advantage of future
  358. improvements in quantization algorithms, or purchase better display
  359. hardware, to get a better view of JPEG images you already have.  With a GIF,
  360. you're stuck forevermore with what was sent.
  361.  
  362. A growing number of people have better-than-8-bit display hardware already:
  363. 15-bit "hi-color" PC displays, true 24-bit displays on workstations and
  364. Macintoshes, etc.  For these people, GIF is already obsolete, as it cannot
  365. represent an image to the full capabilities of their display.  JPEG images
  366. can drive these displays much more effectively.
  367.  
  368. In short, JPEG is an all-around better choice than GIF for representing
  369. photographic images in a machine-independent fashion.
  370.  
  371.  
  372. It's sometimes thought that a JPEG converted from a GIF shouldn't require
  373. color quantization.  This is false: even when you feed a 256-or-less-color
  374. GIF into JPEG, what comes out of the decompressor is not 256 colors, but
  375. thousands of colors.  This happens because JPEG's lossiness affects each
  376. pixel a little differently, so two pixels that started with identical colors
  377. will usually come out with slightly different colors.  Considering the whole
  378. image, each original color gets "smeared" into a group of nearby colors.
  379. Therefore quantization is always required to display a color JPEG on a
  380. colormapped display, regardless of the image source.
  381.  
  382. The same effect makes it nearly meaningless to talk about the number of
  383. colors used by a JPEG image.  Even if you tried to count the number of
  384. distinct pixel values, different JPEG decoders would give you different
  385. results because of roundoff error differences.  I occasionally see posted
  386. images described as "256-color JPEG".  This tells me that the poster
  387. (a) hasn't read this FAQ and (b) probably converted the JPEG from a GIF.
  388. JPEGs can be classified as color or gray-scale, but number of colors just
  389. isn't a useful concept for JPEG, any more than it is for a real photograph.
  390.  
  391. ------------------------------
  392.  
  393. Subject: [9] What are some rules of thumb for converting GIF images to JPEG?
  394.  
  395. Converting GIF files to JPEG is a tricky business --- you are piling one set
  396. of limitations atop a quite different set, and the results can be awful.
  397. Certainly a JPEG made from a GIF will never be as good as a JPEG made from
  398. true 24-bit color data.  But if what you've got is GIFs, and you need to
  399. save space, here are some hints for getting the best results.
  400.  
  401. With care and a clean source image, it's often possible to make a JPEG of
  402. quality equivalent to the GIF.  This does not mean that the JPEG looks
  403. pixel-for-pixel identical to the GIF --- it won't.  Especially not on an
  404. 8-bit display, because the color quantization process used to display the
  405. JPEG probably won't quite match the quantization process used to make the
  406. GIF from the original data (see "[8] What is color quantization?").  But
  407. remember that the GIF itself is not all that faithful to the full-color
  408. original, if you look at individual pixels.  Looking at the overall image,
  409. a converted JPEG can look as good as its GIF source.  Some people claim that
  410. on 24-bit displays, a carefully converted JPEG can actually look better than
  411. the GIF source, because dither patterns have been eliminated.  (More about
  412. dithering in a moment.)
  413.  
  414. On the other hand, JPEG conversion absolutely *will* degrade an unsuitable
  415. image or one that is converted carelessly.  If you are not willing to take
  416. the amount of trouble suggested below, you're much better off leaving your
  417. GIF images alone.  Simply cranking the JPEG quality setting up to a very
  418. high value wastes space (which defeats the whole point of the exercise, no?)
  419. and some images will be degraded anyway.
  420.  
  421. The first rule is never to convert an image that's not appropriate for JPEG
  422. (see "[3] When should I use JPEG, and when should I stick with GIF?").
  423. Large, high-visual-quality photographic images are usually the best source
  424. material.  And they take up lots of space in GIF form, so they offer
  425. significant potential space savings.  (A good rule of thumb is not to bother
  426. converting any GIF that's much under 100 Kbytes; the potential savings isn't
  427. worth the hassle.)
  428.  
  429. The second rule is to look at each JPEG, to make sure you are happy with it,
  430. before throwing away the corresponding GIF.  This will give you a chance to
  431. re-do the conversion with a higher quality setting if necessary.  Also
  432. compare the file sizes --- if the image isn't suitable JPEG material, a JPEG
  433. file of reasonable quality may come out *larger* than the GIF.
  434.  
  435. The third rule is to get rid of the border.  Many people have developed
  436. an odd habit of putting a large single-color border around a GIF image.
  437. While useless, this is nearly free in terms of storage cost in GIF files.
  438. It is *not* free in JPEG files, either in storage space or in decoding time.
  439. Worse, the sharp border boundary can create visible artifacts ("ghost" edges).
  440. Furthermore, when viewing a bordered JPEG on an 8-bit display, the quantizer
  441. will think the border color is important because there's so much of it, and
  442. hence will waste color palette entries on the border, thus actually reducing
  443. the displayed quality of the main part of the image!  So do yourself a favor
  444. and crop off any border before JPEGing.
  445.  
  446. The fourth rule is to know where the image came from.  Repeated GIF<=>JPEG
  447. conversions are guaranteed to turn an image into mush, because you pay a
  448. quality-loss price on each round trip.  Don't reconvert images that have
  449. been converted before.
  450.  
  451. Gray-scale images usually convert without much problem.  When using cjpeg,
  452. be sure to use the -gray switch.  (Otherwise, cjpeg treats a GIF as color
  453. data; this works, but it wastes space and time if the image is really only
  454. gray-scale.)  Quality settings around the default (75) are usually fine.
  455.  
  456. Color images are much trickier.  Color GIFs of photographic images are
  457. usually "dithered" to fool your eye into seeing more than the 256 colors
  458. that GIF can actually store.  If you enlarge the image, you will find that
  459. adjacent pixels are often of significantly different colors; at normal size
  460. the eye averages these pixels together to produce the illusion of an
  461. intermediate color value.  The trouble with dithering is that, to JPEG, it
  462. looks like high-spatial-frequency color noise; and JPEG can't compress noise
  463. very well.  The resulting JPEG file is both larger and of lower image
  464. quality than what you would have gotten from JPEGing the original full color
  465. image (if you had it).  To get around this, you need to "smooth" the GIF
  466. image before compression.  Smoothing averages together nearby pixels, thus
  467. approximating the color that you thought you saw anyway, and in the process
  468. getting rid of the rapid color changes that give JPEG trouble.  Proper use
  469. of smoothing will both reduce the size of the compressed file and give you a
  470. better-looking output image than you'd get without smoothing.
  471.  
  472. With the IJG JPEG software (cjpeg or derived programs), a simple smoothing
  473. capability is built in.  Try "-smooth 10" or so when converting GIFs.
  474. Values of 10 to 25 seem to work well for high-quality GIFs.  GIFs with
  475. heavy-handed dithering may require larger smoothing factors.  (If you can
  476. see regular fine-scale patterns on the GIF image even without enlargement,
  477. then strong smoothing is definitely called for.)  Too large a smoothing
  478. factor will blur the output image, which you don't want.  If you are an
  479. image processing wizard, you can also do smoothing with a separate filtering
  480. program, but appropriate use of such tools is beyond the scope of this FAQ.
  481.  
  482. Quality settings around 85 (a bit higher than default) usually work well
  483. when converting color GIFs, assuming that you've picked a good smoothing
  484. factor.  You may need still higher quality settings if you can't hide the
  485. dithering pattern with a reasonable smoothing factor.  Really badly dithered
  486. GIFs are best left as GIFs.
  487.  
  488. Don't expect JPEG files converted from GIFs to be as small as those created
  489. directly from full-color originals.  The dithering noise wastes space, but
  490. you won't be able to smooth away all the noise without blurring the image.
  491. Typically, a good-quality converted JPEG will be one-half to one-third the
  492. size of the GIF file, not one-fourth as suggested in section 4.  If the JPEG
  493. comes out much more than half the size of the GIF, this is a good sign that
  494. the image shouldn't be converted at all.
  495.  
  496. The upshot of all this is that "cjpeg -quality 85 -smooth 10" is probably a
  497. good starting point for converting color GIFs.  But if you care about the
  498. image, you'll want to check the results and maybe try a few other settings.
  499. Blindly converting a large GIF library at this or any other setting is a
  500. recipe for disaster.
  501.  
  502. ------------------------------
  503.  
  504. Subject: [10] Does loss accumulate with repeated compression/decompression?
  505.  
  506. It would be nice if, having compressed an image with JPEG, you could
  507. decompress it, manipulate it (crop off a border, say), and recompress it
  508. without any further image degradation beyond what you lost initially.
  509. Unfortunately THIS IS NOT THE CASE.  In general, recompressing an altered
  510. image loses more information.  Hence it's important to minimize the number
  511. of generations of JPEG compression between initial and final versions of an
  512. image.
  513.  
  514. It turns out that if you decompress and recompress an image at the same
  515. quality setting first used, little or no further degradation occurs.  This
  516. means that you can make local modifications to a JPEG image without material
  517. degradation of other areas of the image.  The areas you change will degrade,
  518. however.  Counterintuitively, this works better the lower the quality
  519. setting.  But you must use *exactly* the same setting, or all bets are off.
  520. Also, the decompressed image must be saved in a full-color format; if you do
  521. JPEG=>GIF=>JPEG, the color quantization step loses lots of information.
  522.  
  523. Unfortunately, cropping doesn't count as a local change!  JPEG processes
  524. the image in small blocks, and cropping usually moves the block boundaries,
  525. so that the image looks completely different to JPEG.  You can take
  526. advantage of the low-degradation behavior if you are careful to crop the
  527. top and left margins only by a multiple of the block size (typically 16
  528. pixels), so that the remaining blocks start in the same places.
  529.  
  530. The bottom line is that JPEG is a useful format for archival storage and
  531. transmission of images, but you don't want to use it as an intermediate
  532. format for sequences of image manipulation steps.  Use a lossless 24-bit
  533. format (PPM, RLE, TIFF, etc) while working on the image, then JPEG it when
  534. you are ready to file it away.  Aside from avoiding degradation, you will
  535. save a lot of compression/decompression time this way :-).
  536.  
  537. ------------------------------
  538.  
  539. Subject: [11] What is progressive JPEG?
  540.  
  541. A simple or "baseline" JPEG file is stored as one top-to-bottom scan of the
  542. image.  Progressive JPEG allows the image to be stored as a series of scans
  543. of increasing quality.  The first scan shows the image at the equivalent of
  544. a very low quality setting, and therefore it takes very little space.
  545. Successive scans improve the quality.  Each scan adds to the data already
  546. provided, so that the total storage requirement is about the same as for a
  547. baseline JPEG image of the same quality as the final scan.  (Basically,
  548. progressive JPEG is just a rearrangement of the same data into a more
  549. complicated order.)
  550.  
  551. The advantage of progressive JPEG is that if an image is being viewed
  552. on-the-fly as it is transmitted, the viewer sees an approximation to the
  553. whole image very quickly, with gradual improvement of quality as one waits
  554. longer; this is much nicer than a slow top-to-bottom display of the image.
  555. The disadvantage is that each scan takes about the same amount of
  556. computation to display as a whole baseline JPEG file would.  So progressive
  557. JPEG only makes sense if one has a decoder that's much faster than the
  558. communication link.
  559.  
  560. Up until recently, there weren't many applications in which progressive JPEG
  561. looked attractive, so it hasn't been widely implemented.  But with the
  562. popularity of WWW browsers running over slow modem links, and with the
  563. ever-increasing horsepower of personal computers, progressive JPEG looks
  564. like a win for WWW.  IJG's free JPEG software (see part 2) now supports
  565. progressive JPEG, and I expect that the capability will start to appear in
  566. WWW browsers over the next few months (fall/winter 1995).
  567.  
  568. A progressive JPEG file is not readable at all by a baseline-only JPEG
  569. decoder, so existing software will have to be upgraded before progressive
  570. JPEG can be used widely.
  571.  
  572. ------------------------------
  573.  
  574. Subject: [12] Isn't there a lossless JPEG?
  575.  
  576. There's a great deal of confusion on this subject.  The JPEG standard does
  577. include a truly lossless compression algorithm, i.e., one that guarantees
  578. its decompressed output is bit-for-bit identical to the original input.
  579. However, this lossless mode has almost nothing in common with the regular
  580. lossy JPEG algorithm, and it offers much less compression.
  581.  
  582. Lossless JPEG typically compresses full-color data by around 2:1.  Lossless
  583. JPEG works well only on continuous-tone images; it does not provide useful
  584. compression of palette-color images or low-bit-depth images.  (Early results
  585. with the new PNG standard suggest that PNG outcompresses lossless JPEG on
  586. most images.  Once PNG becomes common, lossless JPEG will probably be
  587. obsolete.)
  588.  
  589. There are very few implementations of true lossless JPEG.  The PVRG code
  590. mentioned in part 2 handles lossless JPEG.  Another free implementation
  591. is available from ftp.cs.cornell.edu:/pub/multimed/ljpg.tar.Z.  This is a
  592. smaller program that handles *only* lossless JPEG.
  593.  
  594. Cranking a regular JPEG implementation up to its maximum quality setting
  595. *does not* get you lossless storage; lossless JPEG is a fundamentally
  596. different method.  Even at the maximum possible quality setting, regular
  597. JPEG cannot be lossless because it is subject to roundoff errors in various
  598. calculations.  The roundoff errors are nearly always too small to be seen,
  599. but they will accumulate if you put the image through multiple cycles of
  600. compression.
  601.  
  602. Many implementations won't even let you get to the maximum possible setting,
  603. because it's such an inefficient way to use regular JPEG.  With the IJG JPEG
  604. software, for example, you have to say not only "-quality 100" but also
  605. "-sample 1x1" to eliminate all deliberate loss of information.  The
  606. resulting files are far larger and of only fractionally better quality than
  607. files generated at more reasonable settings.  And they're still slightly
  608. lossy!  If you really need lossless storage, don't try to approximate it
  609. with regular JPEG.
  610.  
  611. ------------------------------
  612.  
  613. Subject: [13] Why all the argument about file formats?
  614.  
  615. Strictly speaking, JPEG refers only to a family of compression algorithms;
  616. it does *not* refer to a specific image file format.  The JPEG committee was
  617. prevented from defining a file format by turf wars within the international
  618. standards organizations.
  619.  
  620. Since we can't actually exchange images with anyone else unless we agree on
  621. a common file format, this leaves us with a problem.  In the absence of
  622. official standards, a number of JPEG program writers have just gone off to
  623. "do their own thing", and as a result their programs aren't compatible with
  624. anyone else's.
  625.  
  626. The closest thing we have to a standard JPEG format is some work that's been
  627. coordinated by people at C-Cube Microsystems.  They have defined two
  628. JPEG-based file formats:
  629.   * JFIF (JPEG File Interchange Format), a "low-end" format that transports
  630.     pixels and not much else.
  631.   * TIFF/JPEG, aka TIFF 6.0, an extension of the Aldus TIFF format.  TIFF is
  632.     a "high-end" format that will let you record just about everything you
  633.     ever wanted to know about an image, and a lot more besides :-).  TIFF
  634.     is far more complex than JFIF, and is generally less transportable,
  635.     because different vendors have often implemented slightly different
  636.     and incompatible subsets of TIFF.  It's not likely that adding JPEG to
  637.     the mix will do anything to improve this situation.
  638. Both of these formats were developed with input from all the major vendors
  639. of JPEG-related products; it's reasonably likely that future commercial
  640. products will adhere to one or both standards.
  641.  
  642. JFIF has emerged as the de-facto standard on Usenet.  JFIF is simpler than
  643. TIFF and is available now; the TIFF 6.0 spec for incorporating JPEG is not
  644. widely implemented, partly because it has some serious design flaws.  It is
  645. likely that the TIFF 6.0 JPEG section will be changed significantly before
  646. widespread adoption occurs.  Even when TIFF/JPEG is well defined, the JFIF
  647. format is likely to be a widely supported "lowest common denominator";
  648. TIFF/JPEG files may never be as transportable as JFIF.
  649.  
  650. A particular case of wide interest is Apple's Macintosh QuickTime software.
  651. QuickTime uses a JFIF-compatible format wrapped inside the Mac-specific PICT
  652. structure.  Conversion between JFIF and PICT/JPEG is pretty straightforward,
  653. and several Mac programs are available to do it (see Mac portion of part 2).
  654. If you have an editor that handles binary files, you can even strip a
  655. PICT/JPEG file down to JFIF by hand; see the next section for details.
  656.  
  657. Another particular case is Handmade Software's DOS programs (GIF2JPG/JPG2GIF
  658. and Image Alchemy).  These programs are capable of reading and writing JFIF
  659. format.  By default, though, they write a proprietary format developed by
  660. HSI.  This format is NOT readable by any non-HSI programs and should not be
  661. used for Usenet postings.  Use the -j switch to get JFIF output.  (This
  662. applies to old versions of these programs; the current releases emit JFIF
  663. format by default.  You still should be careful not to post HSI-format
  664. files, unless you want to get flamed by people on non-PC platforms.)
  665.  
  666. News flash: the ISO JPEG committee seems to have won their turf wars.  They
  667. will define a complete file format spec called "SPIFF" in the next version
  668. of the JPEG standard.  It's pretty late in the game though, so whether this
  669. will have much impact on real-world files remains to be seen.
  670.  
  671. ------------------------------
  672.  
  673. Subject: [14] How do I recognize which file format I have,
  674.               and what do I do about it?
  675.  
  676. If you have an alleged JPEG file that your software won't read, it's likely
  677. to be HSI format or some other proprietary JPEG-based format.  You can tell
  678. what you have by inspecting the first few bytes of the file:
  679.  
  680. 1.  A JFIF-standard file will start with the four bytes (hex) FF D8 FF E0,
  681.     followed by two variable bytes (often hex 00 10), followed by 'JFIF'.
  682.  
  683. 2.  If you see FF D8 at the start, but not the 'JFIF' marker, you may have a
  684.     "raw JPEG" file.  This is probably decodable as-is by JFIF software ---
  685.     it's worth a try, anyway.
  686.  
  687. 3.  HSI files start with 'hsi1'.  You're out of luck unless you have HSI
  688.     software.  Portions of the file may look like plain JPEG data, but they
  689.     usually won't decompress properly with non-HSI programs.
  690.  
  691. 4.  A Macintosh PICT file, if JPEG-compressed, will have several hundred
  692.     bytes of header (often 726 bytes, but not always) followed by JPEG data.
  693.     Look for the 3-byte sequence (hex) FF D8 FF.  The text 'Photo - JPEG'
  694.     will usually appear shortly before this header, and 'AppleMark' or
  695.     'JFIF' will usually appear shortly after it.  Strip off everything
  696.     before the FF D8 FF and you will usually be able to decode the file.
  697.     (This will fail if the PICT image is divided into multiple "bands",
  698.     but that is uncommon.)
  699.  
  700. 5.  If the file came from a Macintosh, it could also be a standard JFIF
  701.     file with a MacBinary header attached.  In this case, the JFIF header
  702.     will appear 128 bytes into the file.  Get rid of the first 128 bytes
  703.     and you're set.
  704.  
  705. 6.  Anything else: it's a proprietary format, or not JPEG at all.  If you
  706.     are lucky, the file may consist of a header and a raw JPEG data stream.
  707.     If you can identify the start of the JPEG data stream (look for FF D8),
  708.     try stripping off everything before that.
  709.  
  710. HSI files used to be rather common in alt.binaries.pictures.* postings,
  711. although thankfully they have gotten less so.  You can spot an HSI posting
  712. by looking at the first few characters of the uuencoded data.  The
  713. characteristic HSI pattern is
  714.     "begin" line
  715.     M:'-I ...
  716. whereas standard JFIF files begin with
  717.     "begin" line
  718.     M_]C_X ...
  719. If you learn to spot the HSI pattern, you can save yourself the trouble
  720. of downloading unusable files.
  721.  
  722. At least one release of Hijaak Pro writes JFIF files that claim to be
  723. revision 2.01.  There is no such spec; the latest JFIF revision is 1.02.
  724. It looks like HiJaak got the high and low bytes backwards.  Unfortunately,
  725. most JFIF readers will give up on encountering these files, because the JFIF
  726. spec defines a major version number change to mean an incompatible format
  727. change.  If there ever *were* a version 2.01, it would be so numbered
  728. because current software could not read it and should not try.  (One wonders
  729. if Hijaak has ever heard of cross-testing with other people's software.)
  730. If you run into one of these misnumbered files, you can fix it with a
  731. binary-file editor, by changing the twelfth byte of the file from 2 to 1.
  732.  
  733. ------------------------------
  734.  
  735. Subject: [15] How does JPEG work?
  736.  
  737. You can find an introduction and references for further reading in the
  738. comp.compression FAQ, which is available from the news.answers archive at
  739. rtfm.mit.edu, in files /pub/usenet/news.answers/compression-faq/part[1-3]
  740. (see also "[20] Where are FAQ lists archived?").
  741.  
  742. The comp.compression FAQ is also a good starting point for information on
  743. other state-of-the-art image compression methods, such as wavelets and
  744. fractals.  A quick comparison: wavelets are likely to be the basis of the
  745. next generation of image-compression standards, but they are perhaps 10
  746. years behind JPEG in the standardization pipeline.  Fractals have been
  747. terribly over-hyped by their chief commercial proponent, and it's difficult
  748. to say what their true capabilities are.
  749.  
  750. ------------------------------
  751.  
  752. Subject: [16] What about arithmetic coding?
  753.  
  754. The JPEG spec defines two different "back end" modules for the final output
  755. of compressed data: either Huffman coding or arithmetic coding is allowed.
  756. The choice has no impact on image quality, but arithmetic coding usually
  757. produces a smaller compressed file.  On typical images, arithmetic coding
  758. produces a file 5 to 10 percent smaller than Huffman coding.  (All the
  759. file-size numbers previously cited are for Huffman coding.)
  760.  
  761. Unfortunately, the particular variant of arithmetic coding specified by the
  762. JPEG standard is subject to patents owned by IBM, AT&T, and Mitsubishi.
  763. Thus *you cannot legally use JPEG arithmetic coding* unless you obtain
  764. licenses from these companies.  (Patent law's "experimental use" exception
  765. allows people to test a patented method in the context of scientific
  766. research, but any commercial or routine personal use is infringement.)
  767.  
  768. I recommend that people not use JPEG arithmetic coding; the space savings
  769. isn't great enough to justify the potential legal hassles.  In particular,
  770. arithmetic coding *should not* be used for any images to be exchanged on
  771. Usenet.  Even if you don't care about US patent law, other folks do.
  772.  
  773. ------------------------------
  774.  
  775. Subject: [17] Could an FPU speed up JPEG?  How about a DSP chip?
  776.  
  777. Since JPEG is so compute-intensive, many people suggest that using an
  778. FPU chip (a math coprocessor) should speed it up.  This is not so.
  779. Most production-quality JPEG programs use only integer arithmetic and so
  780. they are unaffected by the presence or absence of floating-point hardware.
  781.  
  782. It is possible to save a few operations by doing the DCT step in floating
  783. point.  On most PC-class machines, FP math is enough slower than integer
  784. math that the overall speed is still much worse with FP.  Some high-priced
  785. workstations and supercomputers have fast enough FP hardware to make an FP
  786. DCT method be a win.
  787.  
  788. DSP (digital signal processing) chips are ideally suited for fast repetitive
  789. integer arithmetic, so programming a DSP to do JPEG can yield significant
  790. speedups.  DSPs are starting to be found as add-ons for PCs and small
  791. workstations, so you can expect to see DSP-based JPEG programs popping up.
  792.  
  793. ------------------------------
  794.  
  795. Subject: [18] Isn't there an M-JPEG standard for motion pictures?
  796.  
  797. As was stated in section 1, JPEG is only for still images.  Nonetheless,
  798. you will frequently see references to "motion JPEG" or "M-JPEG" for video.
  799. *There is no such standard*.  Various vendors have applied JPEG to
  800. individual frames of a video sequence, and have called the result "M-JPEG".
  801. Unfortunately, in the absence of any recognized standard, they've each done
  802. it differently.  The resulting files are usually not compatible across
  803. different vendors.
  804.  
  805. MPEG is the recognized standard for motion picture compression.  It uses
  806. many of the same techniques as JPEG, but adds inter-frame compression to
  807. exploit the similarities that usually exist between successive frames.
  808. Because of this, MPEG typically compresses a video sequence by about a
  809. factor of three more than "M-JPEG" methods can.  The disadvantages of MPEG
  810. are (1) it requires much more computation to generate the compressed
  811. sequence (since detecting visual similarities is hard for a computer), and
  812. (2) it's difficult to edit an MPEG sequence on a frame-by-frame basis
  813. (since each frame is intimately tied to the ones around it).  This latter
  814. problem has made "M-JPEG" methods rather popular for video editing products.
  815.  
  816. It's a shame that there isn't a recognized M-JPEG standard.  But there
  817. isn't, so if you buy a product identified as "M-JPEG", be aware that you
  818. are probably locking yourself into that one vendor.
  819.  
  820. See the MPEG FAQ for more information about MPEG.
  821.  
  822. ------------------------------
  823.  
  824. Subject: [19] What if I need more than 8-bit precision?
  825.  
  826. Baseline JPEG stores images with 8 bits per color sample, in other words
  827. 24 bits per pixel for RGB images, 8 bits/pixel for grayscale, 32 bits/pixel
  828. for CMYK, etc.  There is an extension that stores 12 bits/sample for
  829. applications that need higher accuracy.  Medical images, for example, are
  830. often 12-bit grayscale.  The 12-bit extension is not very widely supported,
  831. however.  One package that does support it is the free IJG source code (see
  832. part 2 of this FAQ).
  833.  
  834. For lossless JPEG, the standard permits any data precision between 2 and 16
  835. bits per sample, but high-precision lossless JPEG is even less widely
  836. supported than high-precision lossy JPEG.  I don't know of any working
  837. noncommercial implementations.  (The Cornell lossless JPEG coder tries to
  838. support it, but reportedly has bugs with data of more than 8 bits.)
  839.  
  840. ------------------------------
  841.  
  842. Subject: [20] Where are FAQ lists archived?
  843.  
  844. Many FAQs are crossposted to news.answers.  Well-run netnews sites will have
  845. the latest versions available in that newsgroup.  However, there are a *lot*
  846. of postings in news.answers, and they can be hard to sort through.
  847.  
  848. The latest versions of news.answers postings are archived at rtfm.mit.edu.
  849. You can retrieve this FAQ by FTP as /pub/usenet/news.answers/jpeg-faq/part1
  850. and /pub/usenet/news.answers/jpeg-faq/part2.  If you have no FTP access,
  851. send e-mail to mail-server@rtfm.mit.edu containing the lines
  852.     send usenet/news.answers/jpeg-faq/part1
  853.     send usenet/news.answers/jpeg-faq/part2
  854. (If you don't get a reply, the server may be misreading your return address;
  855. add a line such as "path myname@mysite" to specify your correct e-mail
  856. address to reply to.)  For more info about the FAQ archive, retrieve the
  857. file rtfm.mit.edu:/pub/usenet/news.answers/news-answers/introduction.
  858.  
  859. The same FAQs are also available in the World Wide Web; see the index at
  860. http://www.cis.ohio-state.edu/hypertext/faq/usenet/FAQ-List.html.  This FAQ
  861. is http://www.cis.ohio-state.edu/hypertext/faq/usenet/jpeg-faq/top.html.
  862. Other WWW FAQ archives include http://www.cs.ruu.nl/cgi-bin/faqwais and
  863. http://www.lib.ox.ac.uk/internet/news/.
  864.  
  865. -- 
  866.             tom lane
  867.             organizer, Independent JPEG Group
  868.             tgl@netcom.com or tgl@sss.pgh.pa.us
  869.