home *** CD-ROM | disk | FTP | other *** search
/ ftp.pasteur.org/FAQ/ / ftp-pasteur-org-FAQ.zip / FAQ / jpeg-faq / part1 next >
Text File  |  1999-03-29  |  60KB  |  1,078 lines

  1. Newsgroups: comp.graphics.misc,comp.infosystems.www.authoring.images,comp.answers,news.answers
  2. Path: senator-bedfellow.mit.edu!bloom-beacon.mit.edu!hecate.umd.edu!haven.umd.edu!purdue!mozo.cc.purdue.edu!news.iupui.edu!news.indiana.edu!vixen.cso.uiuc.edu!uwm.edu!logbridge.uoregon.edu!newsfeed.berkeley.edu!newsfeed.enteract.com!ix.netcom.com!tgl
  3. From: tgl@netcom.com (Tom Lane)
  4. Subject: JPEG image compression FAQ, part 1/2
  5. Message-ID: <jpeg-faq-p1_922674260@netcom.com>
  6. Followup-To: comp.graphics.misc
  7. Summary: General questions and answers about JPEG
  8. Keywords: JPEG, image compression, FAQ, JPG, JFIF
  9. Supersedes: <jpeg-faq-p1_921456839@netcom.com>
  10. Reply-To: jpeg-info@uunet.uu.net
  11. Organization: Independent JPEG Group
  12. Date: Mon, 29 Mar 1999 02:24:27 GMT
  13. Approved: news-answers-request@MIT.Edu
  14. Expires: Mon, 26 Apr 1999 02:24:20 GMT
  15. Lines: 1059
  16. Sender: tgl@netcom17.netcom.com
  17. Xref: senator-bedfellow.mit.edu comp.graphics.misc:39157 comp.infosystems.www.authoring.images:33421 comp.answers:35587 news.answers:154476
  18.  
  19. Archive-name: jpeg-faq/part1
  20. Posting-Frequency: every 14 days
  21. Last-modified: 28 March 1999
  22.  
  23. This article answers Frequently Asked Questions about JPEG image compression.
  24. This is part 1, covering general questions and answers about JPEG.  Part 2
  25. gives system-specific hints and program recommendations.  As always,
  26. suggestions for improvement of this FAQ are welcome.
  27.  
  28. New since version of 14 March 1999:
  29.   * Expanded item 10 to discuss lossless rotation and cropping of JPEGs.
  30.  
  31.  
  32. This article includes the following sections:
  33.  
  34. Basic questions:
  35.  
  36. [1] What is JPEG?
  37. [2] Why use JPEG?
  38. [3] When should I use JPEG, and when should I stick with GIF?
  39. [4] How well does JPEG compress images?
  40. [5] What are good "quality" settings for JPEG?
  41. [6] Where can I get JPEG software?
  42. [7] How do I view JPEG images posted on Usenet?
  43.  
  44. More advanced questions:
  45.  
  46. [8] What is color quantization?
  47. [9] What are some rules of thumb for converting GIF images to JPEG?
  48. [10] Does loss accumulate with repeated compression/decompression?
  49. [11] What is progressive JPEG?
  50. [12] Can I make a transparent JPEG?
  51. [13] Isn't there a lossless JPEG?
  52. [14] Why all the argument about file formats?
  53. [15] How do I recognize which file format I have, and what do I do about it?
  54. [16] What other common compatibility problems are there?
  55. [17] How does JPEG work?
  56. [18] What about arithmetic coding?
  57. [19] Could an FPU speed up JPEG?  How about a DSP chip?
  58. [20] Isn't there an M-JPEG standard for motion pictures?
  59. [21] What if I need more than 8-bit precision?
  60. [22] How can my program extract image dimensions from a JPEG file?
  61.  
  62. Miscellaneous:
  63.  
  64. [23] Where can I learn about using images on the World Wide Web?
  65. [24] Where are FAQ lists archived?
  66.  
  67.  
  68. This article and its companion are posted every 2 weeks.  If you can't find
  69. part 2, you can get it from the news.answers archive at rtfm.mit.edu
  70. (see "[24] Where are FAQ lists archived?").  Part 2 changes very frequently;
  71. get a new copy if the one you are reading is more than a couple months old.
  72.  
  73. ------------------------------
  74.  
  75. Subject: [1] What is JPEG?
  76.  
  77. JPEG (pronounced "jay-peg") is a standardized image compression mechanism.
  78. JPEG stands for Joint Photographic Experts Group, the original name of the
  79. committee that wrote the standard.
  80.  
  81. JPEG is designed for compressing either full-color or gray-scale images
  82. of natural, real-world scenes.  It works well on photographs, naturalistic
  83. artwork, and similar material; not so well on lettering, simple cartoons,
  84. or line drawings.  JPEG handles only still images, but there is a related
  85. standard called MPEG for motion pictures.
  86.  
  87. JPEG is "lossy," meaning that the decompressed image isn't quite the same as
  88. the one you started with.  (There are lossless image compression algorithms,
  89. but JPEG achieves much greater compression than is possible with lossless
  90. methods.)  JPEG is designed to exploit known limitations of the human eye,
  91. notably the fact that small color changes are perceived less accurately than
  92. small changes in brightness.  Thus, JPEG is intended for compressing images
  93. that will be looked at by humans.  If you plan to machine-analyze your
  94. images, the small errors introduced by JPEG may be a problem for you, even
  95. if they are invisible to the eye.
  96.  
  97. A useful property of JPEG is that the degree of lossiness can be varied by
  98. adjusting compression parameters.  This means that the image maker can trade
  99. off file size against output image quality.  You can make *extremely* small
  100. files if you don't mind poor quality; this is useful for applications such
  101. as indexing image archives.  Conversely, if you aren't happy with the output
  102. quality at the default compression setting, you can jack up the quality
  103. until you are satisfied, and accept lesser compression.
  104.  
  105. Another important aspect of JPEG is that decoders can trade off decoding
  106. speed against image quality, by using fast but inaccurate approximations to
  107. the required calculations.  Some viewers obtain remarkable speedups in this
  108. way.  (Encoders can also trade accuracy for speed, but there's usually less
  109. reason to make such a sacrifice when writing a file.)
  110.  
  111. ------------------------------
  112.  
  113. Subject: [2] Why use JPEG?
  114.  
  115. There are two good reasons: to make your image files smaller, and to store
  116. 24-bit-per-pixel color data instead of 8-bit-per-pixel data.
  117.  
  118. Making image files smaller is a win for transmitting files across networks
  119. and for archiving libraries of images.  Being able to compress a 2 Mbyte
  120. full-color file down to, say, 100 Kbytes makes a big difference in disk
  121. space and transmission time!  And JPEG can easily provide 20:1 compression
  122. of full-color data.  If you are comparing GIF and JPEG, the size ratio is
  123. usually more like 4:1 (see "[4] How well does JPEG compress images?").
  124.  
  125. Now, it takes longer to decode and view a JPEG image than to view an image
  126. of a simpler format such as GIF.  Thus using JPEG is essentially a
  127. time/space tradeoff: you give up some time in order to store or transmit an
  128. image more cheaply.  But it's worth noting that when network transmission is
  129. involved, the time savings from transferring a shorter file can be greater
  130. than the time needed to decompress the file.
  131.  
  132. The second fundamental advantage of JPEG is that it stores full color
  133. information: 24 bits/pixel (16 million colors).  GIF, the other image format
  134. widely used on the net, can only store 8 bits/pixel (256 or fewer colors).
  135. GIF is reasonably well matched to inexpensive computer displays --- most
  136. run-of-the-mill PCs can't display more than 256 distinct colors at once.
  137. But full-color hardware is getting cheaper all the time, and JPEG photos
  138. look *much* better than GIFs on such hardware.  Within a couple of years,
  139. GIF will probably seem as obsolete as black-and-white MacPaint format does
  140. today.  Furthermore, JPEG is far more useful than GIF for exchanging images
  141. among people with widely varying display hardware, because it avoids
  142. prejudging how many colors to use (see "[8] What is color quantization?").
  143. Hence JPEG is considerably more appropriate than GIF for use as a Usenet
  144. and World Wide Web standard photo format.
  145.  
  146. A lot of people are scared off by the term "lossy compression".  But when
  147. it comes to representing real-world scenes, *no* digital image format can
  148. retain all the information that impinges on your eyeball.  By comparison
  149. with the real-world scene, JPEG loses far less information than GIF.
  150. The real disadvantage of lossy compression is that if you repeatedly
  151. compress and decompress an image, you lose a little more quality each time
  152. (see "[10] Does loss accumulate with repeated compression/decompression?").
  153. This is a serious objection for some applications but matters not at all
  154. for many others.
  155.  
  156. ------------------------------
  157.  
  158. Subject: [3] When should I use JPEG, and when should I stick with GIF?
  159.  
  160. JPEG is *not* going to displace GIF entirely; for some types of images,
  161. GIF is superior in image quality, file size, or both.  One of the first
  162. things to learn about JPEG is which kinds of images to apply it to.
  163.  
  164. Generally speaking, JPEG is superior to GIF for storing full-color or
  165. gray-scale images of "realistic" scenes; that means scanned photographs,
  166. continuous-tone artwork, and similar material.  Any smooth variation in
  167. color, such as occurs in highlighted or shaded areas, will be represented
  168. more faithfully and in less space by JPEG than by GIF.
  169.  
  170. GIF does significantly better on images with only a few distinct colors,
  171. such as line drawings and simple cartoons.  Not only is GIF lossless for
  172. such images, but it often compresses them more than JPEG can.  For example,
  173. large areas of pixels that are all *exactly* the same color are compressed
  174. very efficiently indeed by GIF.  JPEG can't squeeze such data as much as GIF
  175. does without introducing visible defects.  (One implication of this is that
  176. large single-color borders are quite cheap in GIF files, while they are best
  177. avoided in JPEG files.)
  178.  
  179. Computer-drawn images, such as ray-traced scenes, usually fall between
  180. photographs and cartoons in terms of complexity.  The more complex and
  181. subtly rendered the image, the more likely that JPEG will do well on it.
  182. The same goes for semi-realistic artwork (fantasy drawings and such).
  183. But icons that use only a few colors are handled better by GIF.
  184.  
  185. JPEG has a hard time with very sharp edges: a row of pure-black pixels
  186. adjacent to a row of pure-white pixels, for example.  Sharp edges tend to
  187. come out blurred unless you use a very high quality setting.  Edges this
  188. sharp are rare in scanned photographs, but are fairly common in GIF files:
  189. consider borders, overlaid text, etc.  The blurriness is particularly
  190. objectionable with text that's only a few pixels high.  If you have a GIF
  191. with a lot of small-size overlaid text, don't JPEG it.  (If you want to
  192. attach descriptive text to a JPEG image, put it in as a comment rather than
  193. trying to overlay it on the image.  Most recent JPEG software can deal with
  194. textual comments in a JPEG file, although older viewers may just ignore the
  195. comments.)
  196.  
  197. Plain black-and-white (two level) images should never be converted to JPEG;
  198. they violate all of the conditions given above.  You need at least about
  199. 16 gray levels before JPEG is useful for gray-scale images.  It should also
  200. be noted that GIF is lossless for gray-scale images of up to 256 levels,
  201. while JPEG is not.
  202.  
  203. If you have a large library of GIF images, you may want to save space by
  204. converting the GIFs to JPEG.  This is trickier than it may seem --- even
  205. when the GIFs contain photographic images, they are actually very poor
  206. source material for JPEG, because the images have been color-reduced.
  207. Non-photographic images should generally be left in GIF form.  Good-quality
  208. photographic GIFs can often be converted with no visible quality loss, but
  209. only if you know what you are doing and you take the time to work on each
  210. image individually.  Otherwise you're likely to lose a lot of image quality
  211. or waste a lot of disk space ... quite possibly both.  Read sections 8 and 9
  212. if you want to convert GIFs to JPEG.
  213.  
  214. ------------------------------
  215.  
  216. Subject: [4] How well does JPEG compress images?
  217.  
  218. Very well indeed, when working with its intended type of image (photographs
  219. and suchlike).  For full-color images, the uncompressed data is normally 24
  220. bits/pixel.  The best known lossless compression methods can compress such
  221. data about 2:1 on average.  JPEG can typically achieve 10:1 to 20:1
  222. compression without visible loss, bringing the effective storage requirement
  223. down to 1 to 2 bits/pixel.  30:1 to 50:1 compression is possible with small
  224. to moderate defects, while for very-low-quality purposes such as previews or
  225. archive indexes, 100:1 compression is quite feasible.  An image compressed
  226. 100:1 with JPEG takes up the same space as a full-color one-tenth-scale
  227. thumbnail image, yet it retains much more detail than such a thumbnail.
  228.  
  229. For comparison, a GIF version of the same image would start out by
  230. sacrificing most of the color information to reduce the image to 256 colors
  231. (8 bits/pixel).  This provides 3:1 compression.  GIF has additional "LZW"
  232. compression built in, but LZW doesn't work very well on typical photographic
  233. data; at most you may get 5:1 compression overall, and it's not at all
  234. uncommon for LZW to be a net loss (i.e., less than 3:1 overall compression).
  235. LZW *does* work well on simpler images such as line drawings, which is why
  236. GIF handles that sort of image so well.  When a JPEG file is made from
  237. full-color photographic data, using a quality setting just high enough to
  238. prevent visible loss, the JPEG will typically be a factor of four or five
  239. smaller than a GIF file made from the same data.
  240.  
  241. Gray-scale images do not compress by such large factors.  Because the human
  242. eye is much more sensitive to brightness variations than to hue variations,
  243. JPEG can compress hue data more heavily than brightness (gray-scale) data.
  244. A gray-scale JPEG file is generally only about 10%-25% smaller than a
  245. full-color JPEG file of similar visual quality.  But the uncompressed
  246. gray-scale data is only 8 bits/pixel, or one-third the size of the color
  247. data, so the calculated compression ratio is much lower.  The threshold of
  248. visible loss is often around 5:1 compression for gray-scale images.
  249.  
  250. The exact threshold at which errors become visible depends on your viewing
  251. conditions.  The smaller an individual pixel, the harder it is to see an
  252. error; so errors are more visible on a computer screen (at 70 or so
  253. dots/inch) than on a high-quality color printout (300 or more dots/inch).
  254. Thus a higher-resolution image can tolerate more compression ... which is
  255. fortunate considering it's much bigger to start with.  The compression
  256. ratios quoted above are typical for screen viewing.  Also note that the
  257. threshold of visible error varies considerably across images.
  258.  
  259. ------------------------------
  260.  
  261. Subject: [5] What are good "quality" settings for JPEG?
  262.  
  263. Most JPEG compressors let you pick a file size vs. image quality tradeoff by
  264. selecting a quality setting.  There seems to be widespread confusion about
  265. the meaning of these settings.  "Quality 95" does NOT mean "keep 95% of the
  266. information", as some have claimed.  The quality scale is purely arbitrary;
  267. it's not a percentage of anything.
  268.  
  269. In fact, quality scales aren't even standardized across JPEG programs.
  270. The quality settings discussed in this article apply to the free IJG JPEG
  271. software (see part 2, item 15), and to many programs based on it.  Some
  272. other JPEG implementations use completely different quality scales.
  273. For example:
  274.   * Apple used to use a scale running from 0 to 4, not 0 to 100.
  275.   * Recent Apple software uses an 0-100 scale that has nothing to do with
  276.     the IJG scale (their Q 50 is about the same as Q 80 on the IJG scale).
  277.   * Paint Shop Pro's scale is the exact opposite of the IJG scale, PSP
  278.     setting N = IJG 100-N; thus lower numbers are higher quality in PSP.
  279.   * Adobe Photoshop doesn't use a numeric scale at all, it just gives you
  280.     "high"/"medium"/"low" choices.  (But I hear this is changing in 4.0.)
  281. Fortunately, this confusion doesn't prevent different implementations from
  282. exchanging JPEG files.  But you do need to keep in mind that quality scales
  283. vary considerably from one JPEG-creating program to another, and that just
  284. saying "I saved this at Q 75" doesn't mean a thing if you don't say which
  285. program you used.
  286.  
  287. In most cases the user's goal is to pick the lowest quality setting, or
  288. smallest file size, that decompresses into an image indistinguishable from
  289. the original.  This setting will vary from one image to another and from one
  290. observer to another, but here are some rules of thumb.
  291.  
  292. For good-quality, full-color source images, the default IJG quality setting
  293. (Q 75) is very often the best choice.  This setting is about the lowest you
  294. can go without expecting to see defects in a typical image.  Try Q 75 first;
  295. if you see defects, then go up.
  296.  
  297. If the image was less than perfect quality to begin with, you might be able
  298. to drop down to Q 50 without objectionable degradation.  On the other hand,
  299. you might need to go to a *higher* quality setting to avoid further loss.
  300. This is often necessary if the image contains dithering or moire patterns
  301. (see "[9] What are some rules of thumb for converting GIF images to JPEG?").
  302.  
  303. Except for experimental purposes, never go above about Q 95; using Q 100
  304. will produce a file two or three times as large as Q 95, but of hardly any
  305. better quality.  Q 100 is a mathematical limit rather than a useful setting.
  306. If you see a file made with Q 100, it's a pretty sure sign that the maker
  307. didn't know what he/she was doing.
  308.  
  309. If you want a very small file (say for preview or indexing purposes) and are
  310. prepared to tolerate large defects, a Q setting in the range of 5 to 10 is
  311. about right.  Q 2 or so may be amusing as "op art".  (It's worth mentioning
  312. that the current IJG software is not optimized for such low quality factors.
  313. Future versions may achieve better image quality for the same file size at
  314. low quality settings.)
  315.  
  316. If your image contains sharp colored edges, you may notice slight fuzziness
  317. or jagginess around such edges no matter how high you make the quality
  318. setting.  This can be suppressed, at a price in file size, by turning off
  319. chroma downsampling in the compressor.  The IJG encoder regards downsampling
  320. as a separate option which you can turn on or off independently of the Q
  321. setting.  With the "cjpeg" program, the command line switch "-sample 1x1"
  322. turns off downsampling; other programs based on the IJG library may have
  323. checkboxes or other controls for downsampling.  Other JPEG implementations
  324. may or may not provide user control of downsampling.  Adobe Photoshop, for
  325. example, automatically switches off downsampling at its higher quality
  326. settings.  On most photographic images, we recommend leaving downsampling
  327. on, because it saves a significant amount of space at little or no visual
  328. penalty.
  329.  
  330. For images being used on the World Wide Web, it's often a good idea to
  331. give up a small amount of image quality in order to reduce download time.
  332. Quality settings around 50 are often perfectly acceptable on the Web.
  333. In fact, a user viewing such an image on a browser with a 256-color display
  334. is unlikely to be able to see any difference from a higher quality setting,
  335. because the browser's color quantization artifacts will swamp any
  336. imperfections in the JPEG image itself.  It's also worth knowing that
  337. current progressive-JPEG-making programs use default progression sequences
  338. that are tuned for quality settings around 50-75: much below 50, the early
  339. scans will look really bad, while much above 75, the later scans won't
  340. contribute anything noticeable to the picture.
  341.  
  342. ------------------------------
  343.  
  344. Subject: [6] Where can I get JPEG software?
  345.  
  346. See part 2 of this FAQ for recommendations about programs for particular
  347. systems.  Part 2 also tells where to find free source code for implementing
  348. JPEG, in case you want to write your own programs using JPEG.
  349.  
  350. The comp.graphics.* FAQs and the alt.binaries.pictures FAQ are more general
  351. sources of information about graphics programs available on the Internet
  352. (see "[24] Where are FAQ lists archived?").
  353.  
  354. ------------------------------
  355.  
  356. Subject: [7] How do I view JPEG images posted on Usenet?
  357.  
  358. Image files posted on the alt.binaries.pictures.* newsgroups are usually
  359. "uuencoded".  Uuencoding converts binary image data into text that can
  360. safely be posted.  Most posters also divide large posts into multiple parts,
  361. since some news software can't cope with big articles.  Before your viewer
  362. will recognize the image, you must combine the parts into one file and run
  363. the text through a uudecode program.  (This is all true for GIF as well as
  364. JPEG, by the way.)  There are programs available to automate this process.
  365.  
  366. For more info see the alt.binaries.pictures FAQ, which is available from
  367. http://www.faqs.org/faqs/pictures-faq/
  368. (see also "[24] Where are FAQ lists archived?").
  369.  
  370. ------------------------------
  371.  
  372. Subject: [8] What is color quantization?
  373.  
  374. Many people don't have full-color (24 bit per pixel) display hardware.
  375. Inexpensive display hardware stores 8 bits per pixel, so it can display
  376. at most 256 distinct colors at a time.  To display a full-color image, the
  377. computer must choose an appropriate set of representative colors and map
  378. the image into these colors.  This process is called "color quantization".
  379. (This is something of a misnomer; "color selection" or "color reduction"
  380. would be a better term.  But we're stuck with the standard usage.)
  381.  
  382. Clearly, color quantization is a lossy process.  It turns out that for most
  383. images, the details of the color quantization algorithm have *much* more
  384. impact on the final image quality than do any errors introduced by JPEG
  385. itself (except at the very lowest JPEG quality settings).  Making a good
  386. color quantization method is a black art, and no single algorithm is best
  387. for all images.
  388.  
  389. Since JPEG is a full-color format, displaying a color JPEG image on
  390. 8-bit-or-less hardware requires color quantization.  The speed and image
  391. quality of a JPEG viewer running on such hardware are largely determined by
  392. its quantization algorithm.  Depending on whether a quick-and-dirty or
  393. good-but-slow method is used, you'll see great variation in image quality
  394. among viewers on 8-bit displays, much more than occurs on 24-bit displays.
  395.  
  396. On the other hand, a GIF image has already been quantized to 256 or fewer
  397. colors.  (A GIF always has a specific number of colors in its palette, and
  398. the format doesn't allow more than 256 palette entries.)  GIF has the
  399. advantage that the image maker precomputes the color quantization, so
  400. viewers don't have to; this is one of the things that make GIF viewers
  401. faster than JPEG viewers.  But this is also the *disadvantage* of GIF:
  402. you're stuck with the image maker's quantization.  If the maker quantized to
  403. a different number of colors than what you can display, you'll either waste
  404. display capability or else have to requantize to reduce the number of colors
  405. (which usually results in much poorer image quality than quantizing once
  406. from a full-color image).  Furthermore, if the maker didn't use a
  407. high-quality color quantization algorithm, you're out of luck --- the image
  408. is ruined.
  409.  
  410. For this reason, JPEG promises significantly better image quality than GIF
  411. for all users whose machines don't match the image maker's display hardware.
  412. JPEG's full color image can be quantized to precisely match the viewer's
  413. display hardware.  Furthermore, you will be able to take advantage of future
  414. improvements in quantization algorithms, or purchase better display
  415. hardware, to get a better view of JPEG images you already have.  With a GIF,
  416. you're stuck forevermore with what was sent.
  417.  
  418. A closely related problem is seen in many current World Wide Web browsers:
  419. when running on an 8-bit display, they force all images into a pre-chosen
  420. palette.  (They do this to avoid having to worry about how to allocate the
  421. limited number of available color slots among the various items on a Web
  422. page.)  A GIF version of a photo usually degrades very badly in this
  423. situation, because it's effectively being forced through a second
  424. quantization step.  A JPEG photo won't look wonderful either, but it will
  425. look less bad than the GIF equivalent because it's been quantized only once.
  426.  
  427. A growing number of people have better-than-8-bit display hardware already:
  428. 15- or 16-bit/pixel "high color" displays are now quite common, and true
  429. 24-bit/pixel displays are no longer rare.  For these people, GIF is already
  430. obsolete, as it cannot represent an image to the full capabilities of their
  431. display.  JPEG images can drive these displays much more effectively.
  432.  
  433. In short, JPEG is an all-around better choice than GIF for representing
  434. photographic images in a machine-independent fashion.
  435.  
  436.  
  437. It's sometimes thought that a JPEG converted from a GIF shouldn't require
  438. color quantization.  That is false; even when you feed a 256-or-less-color
  439. GIF into JPEG, what comes out of the decompressor is not 256 colors, but
  440. thousands of colors.  This happens because JPEG's lossiness affects each
  441. pixel a little differently, so two pixels that started with identical colors
  442. will usually come out with slightly different colors.  Considering the whole
  443. image, each original color gets "smeared" into a cluster of nearby colors.
  444. Therefore quantization is always required to display a color JPEG on a
  445. colormapped display, regardless of the image source.
  446.  
  447. The same effect makes it nearly meaningless to talk about the number of
  448. colors used by a JPEG image.  Even if you tried to count the number of
  449. distinct pixel values, different JPEG decoders would give you different
  450. results because of roundoff error differences.  I occasionally see posted
  451. images described as "256-color JPEG".  This tells me that the poster
  452. (a) hasn't read this FAQ and (b) probably converted the JPEG from a GIF.
  453. JPEGs can be classified as color or gray-scale, but number of colors just
  454. isn't a useful concept for JPEG, any more than it is for a real photograph.
  455.  
  456. ------------------------------
  457.  
  458. Subject: [9] What are some rules of thumb for converting GIF images to JPEG?
  459.  
  460. Converting GIF files to JPEG is a tricky business --- you are piling one set
  461. of limitations atop a quite different set, and the results can be awful.
  462. Certainly a JPEG made from a GIF will never be as good as a JPEG made from
  463. true 24-bit color data.  But if what you've got is GIFs, and you need to
  464. save space, here are some hints for getting the best results.
  465.  
  466. With care and a clean source image, it's often possible to make a JPEG of
  467. quality equivalent to the GIF.  This does not mean that the JPEG looks
  468. pixel-for-pixel identical to the GIF --- it won't.  Especially not on an
  469. 8-bit display, because the color quantization process used to display the
  470. JPEG probably won't quite match the quantization process used to make the
  471. GIF from the original data (see "[8] What is color quantization?").  But
  472. remember that the GIF itself is not all that faithful to the full-color
  473. original, if you look at individual pixels.  Looking at the overall image,
  474. a converted JPEG can look as good as its GIF source.  Some people claim that
  475. on 24-bit displays, a carefully converted JPEG can actually look better than
  476. the GIF source, because dither patterns have been eliminated.  (More about
  477. dithering in a moment.)
  478.  
  479. On the other hand, JPEG conversion absolutely *will* degrade an unsuitable
  480. image or one that is converted carelessly.  If you are not willing to take
  481. the amount of trouble suggested below, you're much better off leaving your
  482. GIF images alone.  Simply cranking the JPEG quality setting up to a very
  483. high value wastes space (which defeats the whole point of the exercise, no?)
  484. and some images will be degraded anyway.
  485.  
  486. The first rule is never to convert an image that's not appropriate for JPEG
  487. (see "[3] When should I use JPEG, and when should I stick with GIF?").
  488. Large, high-visual-quality photographic images are usually the best source
  489. material.  And they take up lots of space in GIF form, so they offer
  490. significant potential space savings.  (A good rule of thumb is not to bother
  491. converting any GIF that's much under 100 Kbytes; the potential savings isn't
  492. worth the hassle.)
  493.  
  494. The second rule is to know where the image came from.  Repeated GIF<=>JPEG
  495. conversions are guaranteed to turn an image into mush, because you pay a
  496. steep quality price on each round trip.  Don't reconvert images that have
  497. been converted before.
  498.  
  499. The third rule is to get rid of the border.  Many people have developed
  500. an odd habit of putting a large single-color border around a GIF image.
  501. While useless, this is nearly free in terms of storage cost in GIF files.
  502. It is *not* free in JPEG files, either in storage space or in decoding time.
  503. Worse, the sharp border boundary can create visible artifacts (ghost edges).
  504. Furthermore, when viewing a bordered JPEG on an 8-bit display, the quantizer
  505. will think the border color is important because there's so much of it, and
  506. hence will waste color palette entries on the border, thus actually reducing
  507. the displayed quality of the main part of the image!  So do yourself a favor
  508. and crop off any border before JPEGing.
  509.  
  510. The final rule is to look at each JPEG, to make sure you are happy with it,
  511. before throwing away the corresponding GIF.  This will give you a chance to
  512. re-do the conversion with a higher quality setting if necessary.  Also
  513. compare the file sizes --- if the image isn't suitable JPEG material, a JPEG
  514. file of reasonable quality may come out *larger* than the GIF.
  515.  
  516. Gray-scale photos usually convert without much problem.  When using cjpeg,
  517. be sure to use the -gray switch.  (Otherwise, cjpeg treats a GIF as color
  518. data; this works, but it wastes space and time if the image is really only
  519. gray-scale.)  Quality settings around the default (75) are usually fine.
  520.  
  521. Color images are much trickier.  Color GIFs of photographic images are
  522. usually "dithered" to fool your eye into seeing more than the 256 colors
  523. that GIF can actually store.  If you enlarge the image, you will find that
  524. adjacent pixels are often of significantly different colors; at normal size
  525. the eye averages these pixels together to produce the illusion of an
  526. intermediate color value.  The trouble with dithering is that, to JPEG, it
  527. looks like high-spatial-frequency color noise; and JPEG can't compress noise
  528. very well.  The resulting JPEG file is both larger and of lower image
  529. quality than what you would have gotten from JPEGing the original full color
  530. image (if you had it).  To get around this, you need to "smooth" the GIF
  531. image before compression.  Smoothing averages together nearby pixels, thus
  532. approximating the color that you thought you saw anyway, and in the process
  533. getting rid of the rapid color changes that give JPEG trouble.  Proper use
  534. of smoothing will both reduce the size of the compressed file and give you a
  535. better-looking output image than you'd get without smoothing.
  536.  
  537. With the IJG JPEG software (cjpeg or derived programs), a simple smoothing
  538. capability is built in.  Try "-smooth 10" or so when converting GIFs.
  539. Values of 10 to 25 seem to work well for high-quality GIFs.  GIFs with
  540. heavy-handed dithering may require larger smoothing factors.  (If you can
  541. see regular fine-scale patterns on the GIF image even without enlargement,
  542. then strong smoothing is definitely called for.)  Too large a smoothing
  543. factor will blur the output image, which you don't want.  If you are an
  544. image processing wizard, you can also do smoothing with a separate filtering
  545. program, but appropriate use of such tools is beyond the scope of this FAQ.
  546.  
  547. Quality settings around 85 (a bit higher than default) usually work well
  548. when converting color GIFs, assuming that you've picked a good smoothing
  549. factor.  You may need still higher quality settings if you can't hide the
  550. dithering pattern with a reasonable smoothing factor.  Really badly dithered
  551. GIFs are best left as GIFs.
  552.  
  553. Don't expect JPEG files converted from GIFs to be as small as those created
  554. directly from full-color originals.  The dithering noise wastes space, but
  555. you won't be able to smooth away all the noise without blurring the image.
  556. Typically, a good-quality converted JPEG will be one-half to one-third the
  557. size of the GIF file, not one-fourth as suggested in section 4.  If the JPEG
  558. comes out much more than half the size of the GIF, this is a good sign that
  559. the image shouldn't be converted at all.
  560.  
  561. The upshot of all this is that "cjpeg -quality 85 -smooth 10" is probably a
  562. good starting point for converting color GIFs.  But if you care about the
  563. image, you'll want to check the results and maybe try a few other settings.
  564. Blindly converting a large GIF library at this or any other setting is a
  565. recipe for disaster.
  566.  
  567. ------------------------------
  568.  
  569. Subject: [10] Does loss accumulate with repeated compression/decompression?
  570.  
  571. It would be nice if, having compressed an image with JPEG, you could
  572. decompress it, manipulate it (crop off a border, say), and recompress it
  573. without any further image degradation beyond what you lost initially.
  574. Unfortunately THIS IS NOT THE CASE.  In general, recompressing an altered
  575. image loses more information.  Hence it's important to minimize the number
  576. of generations of JPEG compression between initial and final versions of an
  577. image.
  578.  
  579. There are a few specialized operations that can be done on a JPEG file
  580. without decompressing it, and thus without incurring the generational loss
  581. that you'd normally get from loading and re-saving the image in a regular
  582. image editor.  In particular it is possible to do 90-degree rotations and
  583. flips losslessly, if the image dimensions are a multiple of the file's
  584. block size (typically 16x16, 16x8, or 8x8 pixels for color JPEGs).  This
  585. fact used to be just an academic curiosity, but it has assumed practical
  586. importance recently because many users of digital cameras would like to be
  587. able to rotate their images from landscape to portrait format without
  588. incurring loss --- and practically all digicams that produce JPEG files
  589. produce images of the right dimensions for these operations to work.  So
  590. software that can do lossless JPEG transforms has started to pop up.  But
  591. you do need special software; rotating the image in a regular image editor
  592. won't be lossless.
  593.  
  594. It turns out that if you decompress and recompress an image at the same
  595. quality setting first used, relatively little further degradation occurs.
  596. This means that you can make local modifications to a JPEG image without
  597. material degradation of other areas of the image.  (The areas you change
  598. will still degrade, however.)  Counterintuitively, this works better the
  599. lower the quality setting.  But you must use *exactly* the same setting,
  600. or all bets are off.  Also, the decompressed image must be saved in a
  601. full-color format; if you do something like JPEG=>GIF=>JPEG, the color
  602. quantization step loses lots of information.
  603.  
  604. Unfortunately, cropping doesn't count as a local change!  JPEG processes
  605. the image in small blocks, and cropping usually moves the block boundaries,
  606. so that the image looks completely different to JPEG.  You can take
  607. advantage of the low-degradation behavior if you are careful to crop the
  608. top and left margins only by a multiple of the block size (typically 16
  609. pixels), so that the remaining blocks start in the same places.  (True
  610. lossless cropping is possible under the same restrictions about where to
  611. crop, but again this requires specialized software.)
  612.  
  613. The bottom line is that JPEG is a useful format for compact storage and
  614. transmission of images, but you don't want to use it as an intermediate
  615. format for sequences of image manipulation steps.  Use a lossless 24-bit
  616. format (PNG, TIFF, PPM, etc) while working on the image, then JPEG it when
  617. you are ready to file it away or send it out on the net.  If you expect to
  618. edit your image again in the future, keep a lossless master copy to work
  619. from.  The JPEG you put up on your Web site should be a derived copy, not
  620. your editing master.
  621.  
  622. ------------------------------
  623.  
  624. Subject: [11] What is progressive JPEG?
  625.  
  626. A simple or "baseline" JPEG file is stored as one top-to-bottom scan of the
  627. image.  Progressive JPEG divides the file into a series of scans.  The first
  628. scan shows the image at the equivalent of a very low quality setting, and
  629. therefore it takes very little space.  Following scans gradually improve the
  630. quality.  Each scan adds to the data already provided, so that the total
  631. storage requirement is roughly the same as for a baseline JPEG image of the
  632. same quality as the final scan.  (Basically, progressive JPEG is just a
  633. rearrangement of the same data into a more complicated order.)
  634.  
  635. The advantage of progressive JPEG is that if an image is being viewed
  636. on-the-fly as it is transmitted, one can see an approximation to the whole
  637. image very quickly, with gradual improvement of quality as one waits longer;
  638. this is much nicer than a slow top-to-bottom display of the image.  The
  639. disadvantage is that each scan takes about the same amount of computation to
  640. display as a whole baseline JPEG file would.  So progressive JPEG only makes
  641. sense if one has a decoder that's fast compared to the communication link.
  642. (If the data arrives quickly, a progressive-JPEG decoder can adapt by
  643. skipping some display passes.  Hence, those of you fortunate enough to have
  644. T1 or faster net links may not see any difference between progressive and
  645. regular JPEG; but on a modem-speed link, progressive JPEG is great.)
  646.  
  647. Up until recently, there weren't many applications in which progressive JPEG
  648. looked attractive, so it hasn't been widely implemented.  But with the
  649. popularity of World Wide Web browsers running over slow modem links, and
  650. with the ever-increasing horsepower of personal computers, progressive JPEG
  651. has become a win for WWW use.  IJG's free JPEG software (see part 2, item
  652. 15) now supports progressive JPEG, and the capability is spreading fast in
  653. WWW browsers and other programs.
  654.  
  655. Except for the ability to provide progressive display, progressive JPEG and
  656. baseline JPEG are basically identical, and they work well on the same kinds
  657. of images.  It is possible to convert between baseline and progressive
  658. representations of an image without any quality loss.  (But specialized
  659. software is needed to do this; conversion by decompressing and recompressing
  660. is *not* lossless, due to roundoff errors.)
  661.  
  662. A progressive JPEG file is not readable at all by a baseline-only JPEG
  663. decoder, so existing software will have to be upgraded before progressive
  664. JPEG can be used widely.  See item 16 in part 2 for the latest news about
  665. which programs support it.
  666.  
  667. ------------------------------
  668.  
  669. Subject: [12] Can I make a transparent JPEG?
  670.  
  671. No.  JPEG does not support transparency and is not likely to do so any time
  672. soon.  It turns out that adding transparency to JPEG would not be a simple
  673. task; read on if you want the gory details.
  674.  
  675. The traditional approach to transparency, as found in GIF and some other
  676. file formats, is to choose one otherwise-unused color value to denote a
  677. transparent pixel.  That can't work in JPEG because JPEG is lossy: a pixel
  678. won't necessarily come out *exactly* the same color that it started as.
  679. Normally, a small error in a pixel value is OK because it affects the image
  680. only slightly.  But if it changes the pixel from transparent to normal or
  681. vice versa, the error would be highly visible and annoying, especially if
  682. the actual background were quite different from the transparent color.
  683.  
  684. A more reasonable approach is to store an alpha channel (transparency
  685. percentage) as a separate color component in a JPEG image.  That could work
  686. since a small error in alpha makes only a small difference in the result.
  687. The problem is that a typical alpha channel is exactly the sort of image
  688. that JPEG does very badly on: lots of large flat areas and sudden jumps.
  689. You'd have to use a very high quality setting for the alpha channel.  It
  690. could be done, but the penalty in file size is large.  A transparent JPEG
  691. done this way could easily be double the size of a non-transparent JPEG.
  692. That's too high a price to pay for most uses of transparency.
  693.  
  694. The only real solution is to combine lossy JPEG storage of the image
  695. with lossless storage of a transparency mask using some other algorithm.
  696. Developing, standardizing, and popularizing a file format capable of
  697. doing that is not a small task.  As far as I know, no serious work is
  698. being done on it; transparency doesn't seem worth that much effort.
  699.  
  700. ------------------------------
  701.  
  702. Subject: [13] Isn't there a lossless JPEG?
  703.  
  704. There's a great deal of confusion on this subject, which is not surprising
  705. because there are several different compression methods all known as "JPEG".
  706. The commonly used method is "baseline JPEG" (or its variant "progressive
  707. JPEG").  The same ISO standard also defines a very different method called
  708. "lossless JPEG".  And if that's not confusing enough, a new lossless
  709. standard called "JPEG-LS" is about to hit the streets.
  710.  
  711. When I say "lossless", I mean mathematically lossless: a lossless
  712. compression algorithm is one that guarantees its decompressed output is
  713. bit-for-bit identical to the original input.  This is a much stronger claim
  714. than "visually indistinguishable from the original".  Baseline JPEG can
  715. reach visual indistinguishability for most photo-like images, but it can
  716. never be truly lossless.
  717.  
  718. Lossless JPEG is a completely different method that really is lossless.
  719. However, it doesn't compress nearly as well as baseline JPEG; it typically
  720. can compress full-color data by around 2:1.  And lossless JPEG works well
  721. only on continuous-tone images.  It does not provide useful compression of
  722. palette-color images or low-bit-depth images.
  723.  
  724. Lossless JPEG has never been popular --- in fact, no common applications
  725. support it --- and it is now largely obsolete.  (For example, the new PNG
  726. standard outcompresses lossless JPEG on most images.)  Recognizing this,
  727. the ISO JPEG committee recently finished an all-new lossless compression
  728. standard called JPEG-LS (you may have also heard of it under the name LOCO).
  729. JPEG-LS gives better compression than original lossless JPEG, but still
  730. nowhere near what you can get with a lossy method.  It's anybody's guess
  731. whether this new standard will achieve any popularity.
  732.  
  733. It's worth repeating that cranking a regular JPEG implementation up to its
  734. maximum quality setting *does not* get you lossless storage; even at the
  735. highest possible quality setting, baseline JPEG is lossy because it is
  736. subject to roundoff errors in various calculations.  Roundoff errors alone
  737. are nearly always too small to be seen, but they will accumulate if you put
  738. the image through multiple cycles of compression (see section 10).
  739.  
  740. Many implementations won't even let you get to the maximum possible setting,
  741. because it's such an inefficient way to use regular JPEG.  With the IJG JPEG
  742. software, for example, you have to not only select "quality 100" but also
  743. turn off chroma downsampling to minimize loss of information.  The resulting
  744. files are far larger and of only fractionally better quality than files
  745. generated at more reasonable settings.  And they're still slightly lossy!
  746. If you really need lossless storage, don't try to approximate it with
  747. regular JPEG.
  748.  
  749. ------------------------------
  750.  
  751. Subject: [14] Why all the argument about file formats?
  752.  
  753. Strictly speaking, JPEG refers only to a family of compression algorithms;
  754. it does *not* refer to a specific image file format.  The JPEG committee was
  755. prevented from defining a file format by turf wars within the international
  756. standards organizations.
  757.  
  758. Since we can't actually exchange images with anyone else unless we agree on
  759. a common file format, this leaves us with a problem.  In the absence of
  760. official standards, a number of JPEG program writers have just gone off to
  761. "do their own thing", and as a result their programs aren't compatible with
  762. anyone else's.
  763.  
  764. The closest thing we have to a standard JPEG format is some work that's been
  765. coordinated by people at C-Cube Microsystems.  They have defined two
  766. JPEG-based file formats:
  767.   * JFIF (JPEG File Interchange Format), a "low-end" format that transports
  768.     pixels and not much else.
  769.   * TIFF/JPEG, aka TIFF 6.0, an extension of the Aldus TIFF format.  TIFF is
  770.     a "high-end" format that will let you record just about everything you
  771.     ever wanted to know about an image, and a lot more besides :-).
  772.  
  773. JFIF has emerged as the de-facto standard on Internet, and is what is most
  774. commonly meant by "a JPEG file".  Most JFIF readers are also capable of
  775. handling some not-quite-JFIF-legal variant formats.
  776.  
  777. The TIFF 6.0 spec for incorporating JPEG is not widely implemented, partly
  778. because it has some serious design flaws.  A revised TIFF/JPEG design is now
  779. described by TIFF Technical Note #2; this design will be the one used in
  780. TIFF 7.0.  New implementations of TIFF should use the Tech Note's design for
  781. embedding JPEG, not the TIFF 6.0 design.  (As far as I know, NeXTStep
  782. systems are the only ones making any significant use of TIFF 6.0 style
  783. TIFF/JPEG.)  Even when TIFF/JPEG is stable, it will never be as widely used
  784. as JFIF.  TIFF is far more complex than JFIF, and is generally less
  785. transportable because different vendors often implement slightly different,
  786. nonoverlapping subsets of TIFF.  Adding JPEG to the mix hasn't helped any.
  787.  
  788. Apple's Macintosh QuickTime software uses a JFIF-compatible datastream
  789. wrapped inside the Mac-specific PICT format.  Conversion between JFIF and
  790. PICT/JPEG is pretty straightforward, and several Mac programs are available
  791. to do it (see part 2, item 8).  If you have an editor that handles binary
  792. files, you can even strip a PICT/JPEG file down to JFIF by hand; see the
  793. next section for details.
  794.  
  795. News flash: the ISO JPEG committee seems to have won their turf wars.  They
  796. have defined a complete file format spec called SPIFF in the new "Part 3"
  797. extensions to the JPEG standard.  It's pretty late in the game though, so
  798. whether this will have much impact on real-world files remains to be seen.
  799. SPIFF is upward compatible with JFIF, so if it does get widely adopted, most
  800. users probably won't even notice.
  801.  
  802. ------------------------------
  803.  
  804. Subject: [15] How do I recognize which file format I have,
  805.               and what do I do about it?
  806.  
  807. If you have an alleged JPEG file that your software won't read, it's likely
  808. to be some proprietary JPEG-based format.  You can tell what you have by
  809. inspecting the first few bytes of the file:
  810.  
  811. 1.  A JFIF-standard file will start with the four bytes (hex) FF D8 FF E0,
  812.     followed by two variable bytes (often hex 00 10), followed by 'JFIF'.
  813.  
  814. 2.  If you see FF D8 FF at the start, but not the 'JFIF' marker, you
  815.     probably have a not-quite-JFIF JPEG file.  Most JFIF software should
  816.     read it without complaint.  If you are using something that is picky
  817.     enough to complain about the lack of a JFIF marker, try another decoder.
  818.     (Both very old JPEG files and very new ones may lack JFIF markers ---
  819.     the new SPIFF standard mentioned above doesn't use a JFIF marker.
  820.     So gripe to your software vendor if you find this to be the problem.)
  821.  
  822. 3.  A Macintosh PICT file, if JPEG-compressed, will have several hundred
  823.     bytes of header (often 726 bytes, but not always) followed by JPEG data.
  824.     Look for the 3-byte sequence (hex) FF D8 FF.  The text 'Photo - JPEG'
  825.     will usually appear shortly before this header, and 'AppleMark' or
  826.     'JFIF' will usually appear shortly after it.  Strip off everything
  827.     before the FF D8 FF and you will usually be able to decode the file.
  828.     (This will fail if the PICT image is divided into multiple "bands";
  829.     fortunately banded PICTs aren't very common.  A banded PICT contains
  830.     multiple JPEG datastreams whose heights add up to the total image
  831.     height.  These need to be stitched back together into one image.
  832.     Bailey Brown has some simple tools for this purpose on a Web page at
  833.     http://www.isomedia.com/homes/bailey/photo-jpeg/photo-jpeg.html.)
  834.  
  835. 4.  If the file came from a Macintosh, it could also be a standard JFIF
  836.     file with a MacBinary header attached.  In this case, the JFIF header
  837.     will appear 128 bytes into the file.  Get rid of the first 128 bytes
  838.     and you're set.
  839.  
  840. 5.  Anything else: it's a proprietary format, or not JPEG at all.  If you
  841.     are lucky, the file may consist of a header and a raw JPEG data stream.
  842.     If you can identify the start of the JPEG data stream (look for FF D8),
  843.     try stripping off everything before that.
  844.  
  845. At least one release of HiJaak Pro writes JFIF files that claim to be
  846. revision 2.01.  There is no such spec; the latest JFIF revision is 1.02.
  847. It looks like HiJaak got the high and low bytes backwards.  Unfortunately,
  848. most JFIF readers will give up on encountering these files, because the JFIF
  849. spec defines a major version number change to mean an incompatible format
  850. change.  If there ever *were* a version 2.01, it would be so numbered
  851. because current software could not read it and should not try.  (One wonders
  852. if HiJaak has ever heard of cross-testing with other people's software.)
  853. If you run into one of these misnumbered files, you can fix it with a
  854. binary-file editor, by changing the twelfth byte of the file from 2 to 1.
  855.  
  856. ------------------------------
  857.  
  858. Subject: [16] What other common compatibility problems are there?
  859.  
  860. Aside from the file format difficulties mentioned in the previous section,
  861. there are a few other common causes of trouble with transferring JPEGs.
  862.  
  863. Old decoders that don't handle progressive JPEG will often give rather
  864. cryptic error messages when fed a progressive JPEG.  If you get a complaint
  865. like "Unsupported marker type 0xC2", then you definitely have a progressive
  866. JPEG file and a non-progressive-capable decoder.  (See part 2 of this FAQ
  867. for information about more up-to-date programs.)  Or you may get a generic
  868. error message that claims the file is corrupted or isn't JPEG at all.
  869.  
  870. Adobe Photoshop and some other prepress-oriented applications will produce
  871. four-channel CMYK JPEG files when asked to save a JPEG from CMYK image mode.
  872. Hardly anything that's not prepress-savvy will cope with CMYK JPEGs (or any
  873. other CMYK format for that matter).  When making JPEGs for Web use, be sure
  874. to save from RGB or grayscale mode.
  875.  
  876. Photoshop also has a habit of stuffing a rather large thumbnail/preview
  877. image into an application-private segment of JPEG files.  Some other
  878. applications (notably early releases of Sun's Java library) are known to
  879. choke on this data.  This is definitely a bug in those other applications,
  880. but the best available workaround is still to tell Photoshop not to save
  881. a thumbnail.  If you're putting up an image on the Web, having a thumbnail
  882. embedded in it is just a waste of download time anyway.
  883.  
  884. When transferring images between machines running different operating
  885. systems, be very careful to get a straight "binary" transfer --- any
  886. sort of text format conversion will corrupt a JPEG file.  Actually that's
  887. true for all image formats not just JPEG.
  888.  
  889. ------------------------------
  890.  
  891. Subject: [17] How does JPEG work?
  892.  
  893. Technical details are outside the scope of this FAQ, but you can find
  894. an introduction and references for further reading in the comp.compression
  895. FAQ, which is available from http://www.faqs.org/faqs/compression-faq/
  896. (see also "[24] Where are FAQ lists archived?").
  897.  
  898. The comp.compression FAQ is also a good starting point for information on
  899. other state-of-the-art image compression methods, such as wavelets and
  900. fractals.  A quick comparison: wavelets are likely to be the basis of the
  901. next generation of lossy image-compression standards, but they are perhaps
  902. 10 years behind JPEG in the standardization pipeline.  Fractals have been
  903. terribly over-hyped by their chief commercial proponent, and seem to be
  904. losing favor as people learn more about their true capabilities and
  905. limitations.
  906.  
  907. ------------------------------
  908.  
  909. Subject: [18] What about arithmetic coding?
  910.  
  911. The JPEG spec defines two different "back end" modules for the final output
  912. of compressed data: either Huffman coding or arithmetic coding is allowed.
  913. The choice has no impact on image quality, but arithmetic coding usually
  914. produces a smaller compressed file.  On typical images, arithmetic coding
  915. produces a file 5 to 10 percent smaller than Huffman coding.  (All the
  916. file-size numbers previously cited are for Huffman coding.)
  917.  
  918. Unfortunately, the particular variant of arithmetic coding specified by the
  919. JPEG standard is subject to patents owned by IBM, AT&T, and Mitsubishi.
  920. Thus *you cannot legally use JPEG arithmetic coding* unless you obtain
  921. licenses from these companies.  (Patent law's "experimental use" exception
  922. allows people to test a patented method in the context of scientific
  923. research, but any commercial or routine personal use is infringement.)
  924.  
  925. I recommend that people not use JPEG arithmetic coding; the space savings
  926. isn't great enough to justify the potential legal hassles.  In particular,
  927. arithmetic coding *should not* be used for any images to be exchanged on
  928. the Internet.  Even if you don't care about US patent law, other folks do.
  929.  
  930. ------------------------------
  931.  
  932. Subject: [19] Could an FPU speed up JPEG?  How about a DSP chip?
  933.  
  934. Since JPEG is so compute-intensive, many people suggest that using an
  935. FPU chip (a math coprocessor) should speed it up.  This is not so.
  936. Most production-quality JPEG programs use only integer arithmetic and so
  937. they are unaffected by the presence or absence of floating-point hardware.
  938.  
  939. It is possible to save a few math operations by doing the DCT step in
  940. floating point.  On most PC-class machines, FP operations are enough slower
  941. than integer operations that the overall speed is still much worse with FP.
  942. Some high-priced workstations and supercomputers have fast enough FP
  943. hardware to make an FP DCT method be a win.
  944.  
  945. DSP (digital signal processing) chips are ideally suited for fast repetitive
  946. integer arithmetic, so programming a DSP to do JPEG can yield significant
  947. speedups.  DSPs are available as add-ons for some PCs and workstations;
  948. if you have such hardware, look for a JPEG program that can exploit it.
  949.  
  950. ------------------------------
  951.  
  952. Subject: [20] Isn't there an M-JPEG standard for motion pictures?
  953.  
  954. As was stated in section 1, JPEG is only for still images.  Nonetheless,
  955. you will frequently see references to "motion JPEG" or "M-JPEG" for video.
  956. *There is no such standard*.  Various vendors have applied JPEG to
  957. individual frames of a video sequence, and have called the result "M-JPEG".
  958. Unfortunately, in the absence of any recognized standard, they've each done
  959. it differently.  The resulting files are usually not compatible across
  960. different vendors.
  961.  
  962. MPEG is the recognized standard for motion picture compression.  It uses
  963. many of the same techniques as JPEG, but adds inter-frame compression to
  964. exploit the similarities that usually exist between successive frames.
  965. Because of this, MPEG typically compresses a video sequence by about a
  966. factor of three more than "M-JPEG" methods can for similar quality.
  967. The disadvantages of MPEG are (1) it requires far more computation to
  968. generate the compressed sequence (since detecting visual similarities is
  969. hard for a computer), and (2) it's difficult to edit an MPEG sequence on a
  970. frame-by-frame basis (since each frame is intimately tied to the ones around
  971. it).  This latter problem has made "M-JPEG" methods rather popular for video
  972. editing products.
  973.  
  974. It's a shame that there isn't a recognized M-JPEG standard.  But there
  975. isn't, so if you buy a product identified as "M-JPEG", be aware that you
  976. are probably locking yourself into that one vendor.
  977.  
  978. Recently, both Microsoft and Apple have started pushing (different :-()
  979. "standard" M-JPEG formats.  It remains to be seen whether either of these
  980. efforts will have much impact on the current chaos.  Both companies were
  981. spectacularly unsuccessful in getting anyone else to adopt their ideas about
  982. still-image JPEG file formats, so I wouldn't assume that anything good will
  983. happen this time either...
  984.  
  985. See the MPEG FAQ for more information about MPEG.
  986.  
  987. ------------------------------
  988.  
  989. Subject: [21] What if I need more than 8-bit precision?
  990.  
  991. Baseline JPEG stores images with 8 bits per color sample, in other words
  992. 24 bits per pixel for RGB images, 8 bits/pixel for grayscale, 32 bits/pixel
  993. for CMYK, etc.  There is an extension that stores 12 bits/sample for
  994. applications that need higher accuracy.  Medical images, for example, are
  995. often 12-bit grayscale.  The 12-bit extension is not very widely supported,
  996. however.  One package that does support it is the free IJG source code (see
  997. part 2, item 15).
  998.  
  999. For lossless JPEG, the standard permits any data precision between 2 and 16
  1000. bits per sample, but high-precision lossless JPEG is even less widely
  1001. supported than high-precision lossy JPEG.  The Stanford PVRG codec (see
  1002. part 2, item 15) reportedly supports up to 16 bits/sample for lossless JPEG.
  1003.  
  1004. ------------------------------
  1005.  
  1006. Subject: [22] How can my program extract image dimensions from a JPEG file?
  1007.  
  1008. The header of a JPEG file consists of a series of blocks, called "markers".
  1009. The image height and width are stored in a marker of type SOFn (Start Of
  1010. Frame, type N).  To find the SOFn you must skip over the preceding markers;
  1011. you don't have to know what's in the other types of markers, just use their
  1012. length words to skip over them.  The minimum logic needed is perhaps a page
  1013. of C code.  (Some people have recommended just searching for the byte pair
  1014. representing SOFn, without paying attention to the marker block structure.
  1015. This is unsafe because a prior marker might contain the SOFn pattern, either
  1016. by chance or because it contains a JPEG-compressed thumbnail image.  If you
  1017. don't follow the marker structure you will retrieve the thumbnail's size
  1018. instead of the main image size.)  A profusely commented example in C can be
  1019. found in rdjpgcom.c in the IJG distribution (see part 2, item 15).  Perl
  1020. code can be found in wwwis, from http://www.tardis.ed.ac.uk/~ark/wwwis/.
  1021.  
  1022. ------------------------------
  1023.  
  1024. Subject: [23] Where can I learn about using images on the World Wide Web?
  1025.  
  1026. If you want to display still images on the World Wide Web, you have a choice
  1027. of using JPEG or GIF; those two formats are by far the most widely supported
  1028. by WWW browsers.  (We can hope that PNG will soon become popular enough to
  1029. replace GIF on the Web; see http://www.cdrom.com/pub/png/ for PNG info.)
  1030. For most images it's pretty obvious which format to choose
  1031. (see "[3] When should I use JPEG, and when should I stick with GIF?").
  1032. JPEG's ability to trade off file size against image quality is especially
  1033. helpful for trimming download times of Web photos.
  1034.  
  1035. But there's a good many things to know that are specific to Web design,
  1036. and even specific to the currently-most-popular browsers.  This FAQ doesn't
  1037. try to cover Web graphics design.  Good basic information can be found at:
  1038.     http://www.boutell.com/faq/
  1039.     http://www.servtech.com/public/dougg/graphics/index.html
  1040.     http://www.webreference.com/dev/graphics/
  1041.     http://www.adobe.com/studio/tipstechniques/GIFJPGchart/main.html
  1042.     http://ppewww.ph.gla.ac.uk/~flavell/www/palette.html
  1043.     http://the-light.com/netcol.html
  1044. and here are some sites with more advanced info:
  1045.     http://www.inforamp.net/~poynton/Poynton-colour.html
  1046.     http://www.photo.net/philg/how-to-scan-photos.html
  1047.     http://www.scantips.com/
  1048.  
  1049. ------------------------------
  1050.  
  1051. Subject: [24] Where are FAQ lists archived?
  1052.  
  1053. Many FAQs are crossposted to news.answers.  Well-run netnews sites will have
  1054. the latest versions available in that newsgroup.  However, there are a *lot*
  1055. of postings in news.answers, and they can be hard to sort through.
  1056.  
  1057. The latest versions of news.answers postings are archived at rtfm.mit.edu.
  1058. You can retrieve this FAQ by FTP as rtfm.mit.edu:/pub/faqs/jpeg-faq/part1
  1059. and rtfm.mit.edu:/pub/faqs/jpeg-faq/part2.  If you have no FTP access,
  1060. send e-mail to mail-server@rtfm.mit.edu containing the lines
  1061.     send faqs/jpeg-faq/part1
  1062.     send faqs/jpeg-faq/part2
  1063. (If you don't get a reply, the server may be misreading your return address;
  1064. add a line such as "path myname@mysite" to specify your correct e-mail
  1065. address to reply to.)  For more info about the FAQ archive, retrieve the
  1066. file rtfm.mit.edu:/pub/faqs/news-answers/introduction.
  1067.  
  1068. The same FAQs are also available from several places on the World Wide Web,
  1069. of which my favorite is http://www.faqs.org/faqs/.
  1070. This FAQ is http://www.faqs.org/faqs/jpeg-faq/.
  1071. Other popular WWW FAQ archives include http://www.cs.ruu.nl/cgi-bin/faqwais
  1072. and http://www.lib.ox.ac.uk/internet/news/.
  1073.  
  1074. -- 
  1075.             tom lane
  1076.             organizer, Independent JPEG Group
  1077.             tgl@netcom.com or tgl@sss.pgh.pa.us
  1078.