home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / compress / 4805 < prev    next >
Encoding:
Internet Message Format  |  1993-01-25  |  15.8 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!usc!randvax!edhall
  2. From: edhall@rand.org (Ed Hall)
  3. Newsgroups: comp.compression
  4. Subject: Re: best way to describe jpeg
  5. Message-ID: <4258@randvax.rand.org>
  6. Date: 25 Jan 93 23:49:45 GMT
  7. References: <1993Jan17.2090.5297@dosgate> <dak.727313698@hathi> <C12LuC.C8.2@cs.cmu.edu>
  8. Sender: news@randvax.rand.org
  9. Organization: RAND
  10. Lines: 288
  11. Nntp-Posting-Host: ives.rand.org
  12.  
  13. I (and the net) might regret this, but here is my attempt to give a an
  14. intelligent layman's description of JPEG.  My target audiences are image
  15. archivists and collectors who are sceptical of JPEG and perhaps misled
  16. by the term "lossy compression" and by misinformed articles on BBSes,
  17. on-line services, and the net.
  18.  
  19. Anyone can take this article and do what they want with it--I abandon
  20. any copyright to it, although I'd appreciate receiving any improvements
  21. or other suggestions.  Keep in mind that I have tried hard to minimize
  22. the amount of technical background assumed of the reader, so don't
  23. criticize me for not explaining Floyd-Steinberg dithering or whatever.
  24.  
  25.         -Ed Hall
  26.         edhall@rand.org
  27.  
  28.  
  29.  
  30.     JPEG: What Is It?  Why Use It?
  31.  
  32.         by Ed Hall
  33.  
  34.  
  35. This document is intended to be a simple description of the JPEG image
  36. compression technique, providing you with a general "feel" for the
  37. process and its strengths and failings.  It is not a technical
  38. description of JPEG; there are several articles and papers, and even a
  39. book, which describe JPEG in depth.  Also, I'm confining my discription
  40. to the use of JPEG for "lossy" compression of color images, although
  41. the JPEG standard itself addresses other uses.
  42.  
  43. In order to understand JPEG you need to know a bit about digital images,
  44. and about some characteristics of how people perceive images, so we'll
  45. start with a few brief paragraphs on these.  We need this background to
  46. understand JPEG because JPEG is /lossy/ compression, which is to say
  47. that part of the information in the original image is thrown away during
  48. compression.  JPEG can't be evaluated unless we understand just what is
  49. lost, and how that loss affects the final displayed image.
  50.  
  51. The fact that JPEG is "lossy" is seen as a bad thing by many people who
  52. are first learning about it.  This impression hasn't been helped by
  53. some defective JPEG programs that appeared soon after its introduction.
  54. However, I think you will see by the end of this document that this
  55. sort of "loss" can often be a good thing--in most cases, the loss is in
  56. the size of the image file, and not the quality of the viewed image.
  57. And the reduction is size can be considerable: usually 3 to 8 times
  58. smaller than a GIF image of similar quality (and later we'll see how,
  59. with appropriate hardware, JPEG images can appear better than the best
  60. GIFs).
  61.  
  62. Color Digital Images
  63.  
  64. Computers handle sounds and images by dividing them up into small
  65. pieces and assigning an intensity value to each piece.  For images,
  66. this usually means dividing the picture up into many small squares,
  67. arranged like very fine graph paper, and assigning a number to each
  68. square according to the brightness of the image within that square.
  69. (These squares are called /pixels/, which is short for "picture
  70. elements.")  If it is a color picture three numbers are used per square,
  71. one each for the intensity of red, green, and blue (often called "RGB"
  72. for short).  Since almost any color can be represented by a mixture of
  73. these three "primary colors," this means that the color of a square can
  74. be represented as well as its brightness.
  75.  
  76. Digital images typically start with a TV camera or a device called a
  77. /scanner/ (or are generated by a computer program).  If we are dealing
  78. with color images, the result is the grid of red, green, and blue
  79. values we just described.  However, there are actually many ways of
  80. representing a color image beside using the raw red, green, and blue
  81. values; in fact, it is often more convenient to translate each square
  82. to a value representing its overall brightness, called /luminance/, and
  83. two other values representing its color or /chroma/.  The ability to do
  84. this is important for JPEG, as we shall soon see.  In any case, the
  85. image is translated back into red, green, and blue values prior to
  86. display.
  87.  
  88. A Few Facts About The Human Visual System
  89.  
  90. There is much that is still unknown about how people see, but through
  91. study and experiment we have learned a number of facts which are quite
  92. useful in designing systems for storing and displaying computer images.
  93. Some of these facts are quite intuitive since we encounter them every
  94. day: for example, a field of black and white dots appears grey when
  95. viewed at a distance, "fusing" into a sort of average of their
  96. brightnesses.  Other facts are just as common but probably not quite as
  97. intuitive, such as the eye's ability to see changes in brightness in
  98. much finer detail than changes in color.  Color television relies upon
  99. this fact, transmitting much more luminance detail than chroma detail.
  100. In fact, JPEG does the same thing (although with a great deal more
  101. finesse than color TV).
  102.  
  103. The tendency for fine detail to fuse into a single impression is a bit
  104. more complicated than the example with black and white dots suggests.  For
  105. example, if the dots are instead two different shades of grey, the dots
  106. can be quite a bit larger before they are seen as separate dots.  In
  107. Scientific termonology, this phenomenon is described by saying that the
  108. eye is less sensitive to higher frequencies.  And in large part it is this
  109. reduced sensitivity that makes JPEG possible.
  110.  
  111. Remember this concept of "high frequencies," meaning image details
  112. which are very close together, since we will use the term quite heavily
  113. later in this document.  Note:  Be careful not to confuse this concept
  114. of "frequency" with that of the electromagnetic spectrum, where blue
  115. light is said to have a higher frequency than red light.  Our use of
  116. the term "frequency" has nothing to do with color, and is more properly
  117. described as "spatial frequency."
  118.  
  119. A Few Words About Color GIFs
  120.  
  121. The same visual phenomena we've just described are also very important
  122. in producing high-quality color GIFs.  This is because color GIF images
  123. also involve lossy compression, a result of the GIF's restriction to 256
  124. colors.
  125.  
  126. Many color scanners and digital cameras produce 8 bits--that is, 256
  127. possible values--for each of the three primary colors, for a total of
  128. 24 bits--that is, 16777216 possible values--for each pixel.  Squeezing
  129. this much information into a GIF means picking 256 colors out of the
  130. 16777216 possible colors and assigning a color from this limited
  131. /palette/ of colors to each pixel.  Since we can rely on the human
  132. visual system to blend adjacent pixels (the "dots" we talked about in
  133. the previous section), we can, if we're smart about how we chose our
  134. palette, use what is effectively many more than 256 colors to produce
  135. our image.  The common technique for doing this, called "dithering,"
  136. makes precisely this compromise: the accuracy of individual pixels is
  137. adjusted to make the average of /groups/ of pixels closer to the color
  138. in the original image.  The eye is relatively insensitive to the
  139. pixel-to-pixel (that is, high-frequency) noise which results from this
  140. process.
  141.  
  142. Be very wary when you hear someone claim that color GIFs use "lossless"
  143. compression.  In fact, as we shall see, the lossy nature of GIFs can
  144. lead to problems when GIFs and JPEG are combined.
  145.  
  146. How JPEG works
  147.  
  148. Some magazine articles on JPEG have claimed that "JPEG throws out the
  149. high frequencies which the eye can't see." This is misleading--it would
  150. be more accurate to say that JPEG represents high frequencies with /less
  151. accuracy/ in proportion to the eye's lower sensitivity to such
  152. frequencies.  If a high-frequency detail is prominent enough for the
  153. eye to notice, a reasonable quality JPEG image will represent it.
  154.  
  155. JPEG does its magic through a series of steps.
  156.  
  157. First, JPEG separates the image into luminance (brightness) and chroma
  158. (color).  Since details in the latter are much less noticible to the
  159. eye, it combines adjacent pixels in forming the chroma "image," which
  160. results in a considerable savings in space.  A full-sized luminance
  161. image and two half-sized chroma images are then each compressed
  162. separately, according to the methods given in the following paragraphs.
  163. Unless we magnify the uncompressed image so that individual pixels
  164. become visible, the reduction in color resolution is invisible.
  165.  
  166. The next step divides the images into blocks of 8 by 8 pixels, and
  167. compresses each block separately.  Using blocks both simplifies the
  168. compression step and takes advantage of the fact that some parts of the
  169. image will be much more compressible than others.  In addition,
  170. artifacts (false detail) of the compression process which result from
  171. processing high frequencies will be confined to small areas, where they
  172. will be (usually) completely obscured by the real details, a phenomenon
  173. called /masking/.  (Such artifacts are usually quite a bit more subtle
  174. than those produced in GIF color reduction, by the way.  You'll generally
  175. not see them unless you choose a very high compression level, or attempt
  176. to compress an inappropriate image such as a line-drawing.)
  177.  
  178. Image blocks are compressed by performing a mathematical operation on
  179. them called a "discrete cosine transform" (abbreviated "DCT").  This
  180. separates the frequencies in the image block into a set of numbers
  181. called /coefficients/.  One of these coefficients, which corresponds
  182. to the average brightness of the entire block, is separated out for
  183. special treatment.  This is because the eye is very sensitive to
  184. inaccuracies in the overall brightness of adjacent blocks; errors
  185. here make the image look "blocky" by making the boundries of the
  186. blocks noticible.  Also, this particular coefficient tends to change
  187. slowly from block to block; this allows an improvement in compression
  188. by saving just the difference from one block to the next.
  189.  
  190. The other coefficients are then /quantized/--that is, reduced to a
  191. limited number of possible values--with the higher frequencies
  192. quantized more coarsely (with fewer possible values) than the lower
  193. frequencies.  Most of the quantized coefficients will be zero, since in
  194. most cases only a few frequencies will exist to a significant (i.e.
  195. visible) degree within any given block.  Of the remaining few
  196. coeffcients, most will have small values.
  197.  
  198. Together, the presence of many successive zeros and of small values in
  199. the coefficients allows for very efficient /coding/; that is, few bits
  200. are generally needed to represent all the quantized coefficents in the
  201. block.  A single count of successive zeros represents several
  202. coefficents by using a technique similar to run-length compression.
  203. Common values of run lengths and of small coefficient values are
  204. assigned short sequences of bits, while less frequent values are
  205. assigned longer sequences; this is known as "huffman coding," and is
  206. quite common in many types of data compression.  These bit sequences
  207. form the compressed block.
  208.  
  209. JPEG has a compression parameter called the "quality" level which
  210. determines the lossiness of the compression.  It varies from 0 to 100,
  211. and is used to determine the courseness of the quantizations used for
  212. the coefficients (see above).  Lower quality levels result in many zero
  213. coefficents and in small values for the nonzero coefficients, allowing
  214. for high degrees of compression at the expense of image bluring and
  215. visible artifacts.  Somewhere between 75 and 90 (depending upon the
  216. image) the resulting image will be visually indistinguishable from
  217. the original under normal viewing conditions, while still allowing
  218. compression of ten to thirty times over the original RGB image--
  219. equivalent to three to eight times over a GIF of the same image.
  220.  
  221. At the other end of the scale, quality factors of 20 or so result
  222. in images which are compressed several more times and are of good
  223. enough quality for previewing or indexing, even though they are
  224. obviously degraded versions of the original.
  225.  
  226. JPEG And GIF
  227.  
  228. JPEG was designed to work on full-color images where red, green, and
  229. blue values are individually kept for each pixel.  GIFs only support a
  230. single value per pixel, and use a palette of up to 256 colors to
  231. approximate the colors in the image.  It is easy to convert a GIF into
  232. these separate color values so processing like JPEG can be applied, but
  233. we have to remember that the GIF has already placed "noise" into the
  234. higher frequencies in order to represent colors more faithfully.  The
  235. eye isn't sensitive to this false detail, but unfortunately JPEG isn't
  236. quite as able to ignore it.  This means a GIF generally can't be
  237. compressed as much as the original RGB image that came from the scanner
  238. or camera could without a visible degradation in quality.  Certain
  239. dithering techniques can actually result in images which cannot be
  240. compressed at all without visible artifacts (fortunately, these images
  241. seem to be getting less common).
  242.  
  243. Another factor comes into play when JPEGs are turned into GIFs.  Since
  244. JPEG produces a full-color image, color reduction, as previously
  245. described, has to take place before a GIF is produced.  There are
  246. several things which can result in less-than-optimal results at this
  247. point.  One is that high-quality dithering can actually take more CPU
  248. time than JPEG processing, so JPEG software sometimes cuts corners in
  249. this step.  Another problem is that the artifacts of color reduction
  250. sometimes combine with the artifacts of the JPEG process in such a way
  251. as to render them visible.  In addition, if the JPEG file was created
  252. from a GIF, chances are good that the palette and dithering will differ
  253. from the original, leading to a difference in appearance that can
  254. easily be interpreted as a degradation.
  255.  
  256. Nonetheless, satisfactory results in GIF-to-JPEG-to-GIF processing are
  257. often possible, and some people routinely use JPEG on many of their
  258. GIFs.  It is probably a good idea to only try for compressions of two
  259. to four times when handling GIFs, though, due to the problems just
  260. discussed.
  261.  
  262. JPEG And True-color Displays
  263.  
  264. JPEG is at its best advantage when no color reductions occur between
  265. the image source and the device (usually a computer's monitor) finally
  266. used for display.  Unfortunately, most computers are unable to display
  267. 24-bit/pixel images, so for these computers color reduction must be
  268. done prior to viewing the image.  Still, if care is taken at this step
  269. the result will be virtually the same as when the color reduction was
  270. done to the original 24-bit image in turning it into a GIF.  And on a
  271. true-color display--rapidly becoming more common as prices for such
  272. video cards fall into the under-$200 range--the superior quality of a
  273. non-color-reduced image can be seen.
  274.  
  275. However, even if you don't have a true-color video card in your
  276. computer now, you'll probably have one at some point in the future.
  277. At that point, JPEG-compressed full-color images will look much
  278. better than GIF's.  So if you're an image collector, it makes sense
  279. to get JPEG images whenever you have a choice.
  280.  
  281. A Final Word On Lossy Compression
  282.  
  283. In a world where data storage was free, and data transmission was
  284. instantaneous, lossy compression would be useless.  But we don't
  285. live in a perfect world.  Disk space costs money, modems take time
  286. and, perhaps, long-distance charges, computer networks bog down
  287. as exchanging images becomes more and more common--in short, it
  288. makes a lot of sense to use data compression wherever it can reduce
  289. such costs.  But why use "lossy" compression?  Isn't that a bit like
  290. accepting damaged goods just because they are cheaper?
  291.  
  292. In a word, no.  For example, which is more worthwhile, a 512x384 GIF or
  293. a 1024x768 high-quality-level JPEG of the same image, if both cost the
  294. same to acquire and store?  I don't know about you, but I'd much rather
  295. have the JPEG.  A quadrupling in transmission and storage costs is much
  296. too high a price to pay for a near-invisible increase in image
  297. quality--but if I have a 24-bit true-color display, the 4X-larger GIF
  298. will actually look inferior!  So the real comparison is between the
  299. original 24-bit image and JPEG, perhaps an 18:1 difference in size.
  300. In brief, not using JPEG can cause "lossy compression" of your wallet...
  301.