home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume29 / jpeg / part02 < prev    next >
Text File  |  1992-03-24  |  55KB  |  1,552 lines

  1. Newsgroups: comp.sources.misc
  2. From: jpeg-info@uunet.uu.net (Independent JPEG Group)
  3. Subject:  v29i002:  jpeg - JPEG image compression, Part02/18
  4. Message-ID: <1992Mar24.063220.3341@sparky.imd.sterling.com>
  5. X-Md4-Signature: 47003de048d1d82ad14b82205b7a8481
  6. Date: Tue, 24 Mar 1992 06:32:20 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: jpeg-info@uunet.uu.net (Independent JPEG Group)
  10. Posting-number: Volume 29, Issue 2
  11. Archive-name: jpeg/part02
  12. Environment: UNIX, VMS, MS-DOS, Mac, Amiga, Cray
  13.  
  14. #! /bin/sh
  15. # into a shell via "sh file" or similar.  To overwrite existing files,
  16. # type "sh file -c".
  17. # The tool that generated this appeared in the comp.sources.unix newsgroup;
  18. # send mail to comp-sources-unix@uunet.uu.net if you want that tool.
  19. # Contents:  jquant2.c jrdppm.c
  20. # Wrapped by kent@sparky on Mon Mar 23 16:02:40 1992
  21. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  22. echo If this archive is complete, you will see the following message:
  23. echo '          "shar: End of archive 2 (of 18)."'
  24. if test -f 'jquant2.c' -a "${1}" != "-c" ; then 
  25.   echo shar: Will not clobber existing file \"'jquant2.c'\"
  26. else
  27.   echo shar: Extracting \"'jquant2.c'\" \(42181 characters\)
  28.   sed "s/^X//" >'jquant2.c' <<'END_OF_FILE'
  29. X/*
  30. X * jquant2.c
  31. X *
  32. X * Copyright (C) 1991, 1992, Thomas G. Lane.
  33. X * This file is part of the Independent JPEG Group's software.
  34. X * For conditions of distribution and use, see the accompanying README file.
  35. X *
  36. X * This file contains 2-pass color quantization (color mapping) routines.
  37. X * These routines are invoked via the methods color_quant_prescan,
  38. X * color_quant_doit, and color_quant_init/term.
  39. X */
  40. X
  41. X#include "jinclude.h"
  42. X
  43. X#ifdef QUANT_2PASS_SUPPORTED
  44. X
  45. X
  46. X/*
  47. X * This module implements the well-known Heckbert paradigm for color
  48. X * quantization.  Most of the ideas used here can be traced back to
  49. X * Heckbert's seminal paper
  50. X *   Heckbert, Paul.  "Color Image Quantization for Frame Buffer Display",
  51. X *   Proc. SIGGRAPH '82, Computer Graphics v.16 #3 (July 1982), pp 297-304.
  52. X *
  53. X * In the first pass over the image, we accumulate a histogram showing the
  54. X * usage count of each possible color.  (To keep the histogram to a reasonable
  55. X * size, we reduce the precision of the input; typical practice is to retain
  56. X * 5 or 6 bits per color, so that 8 or 4 different input values are counted
  57. X * in the same histogram cell.)  Next, the color-selection step begins with a
  58. X * box representing the whole color space, and repeatedly splits the "largest"
  59. X * remaining box until we have as many boxes as desired colors.  Then the mean
  60. X * color in each remaining box becomes one of the possible output colors.
  61. X * The second pass over the image maps each input pixel to the closest output
  62. X * color (optionally after applying a Floyd-Steinberg dithering correction).
  63. X * This mapping is logically trivial, but making it go fast enough requires
  64. X * considerable care.
  65. X *
  66. X * Heckbert-style quantizers vary a good deal in their policies for choosing
  67. X * the "largest" box and deciding where to cut it.  The particular policies
  68. X * used here have proved out well in experimental comparisons, but better ones
  69. X * may yet be found.
  70. X *
  71. X * The most significant difference between this quantizer and others is that
  72. X * this one is intended to operate in YCbCr colorspace, rather than RGB space
  73. X * as is usually done.  Actually we work in scaled YCbCr colorspace, where
  74. X * Y distances are inflated by a factor of 2 relative to Cb or Cr distances.
  75. X * The empirical evidence is that distances in this space correspond to
  76. X * perceptual color differences more closely than do distances in RGB space;
  77. X * and working in this space is inexpensive within a JPEG decompressor, since
  78. X * the input data is already in YCbCr form.  (We could transform to an even
  79. X * more perceptually linear space such as Lab or Luv, but that is very slow
  80. X * and doesn't yield much better results than scaled YCbCr.)
  81. X */
  82. X
  83. X#define Y_SCALE 2        /* scale Y distances up by this much */
  84. X
  85. X#define MAXNUMCOLORS  (MAXJSAMPLE+1) /* maximum size of colormap */
  86. X
  87. X
  88. X/*
  89. X * First we have the histogram data structure and routines for creating it.
  90. X *
  91. X * For work in YCbCr space, it is useful to keep more precision for Y than
  92. X * for Cb or Cr.  We recommend keeping 6 bits for Y and 5 bits each for Cb/Cr.
  93. X * If you have plenty of memory and cycles, 6 bits all around gives marginally
  94. X * better results; if you are short of memory, 5 bits all around will save
  95. X * some space but degrade the results.
  96. X * To maintain a fully accurate histogram, we'd need to allocate a "long"
  97. X * (preferably unsigned long) for each cell.  In practice this is overkill;
  98. X * we can get by with 16 bits per cell.  Few of the cell counts will overflow,
  99. X * and clamping those that do overflow to the maximum value will give close-
  100. X * enough results.  This reduces the recommended histogram size from 256Kb
  101. X * to 128Kb, which is a useful savings on PC-class machines.
  102. X * (In the second pass the histogram space is re-used for pixel mapping data;
  103. X * in that capacity, each cell must be able to store zero to the number of
  104. X * desired colors.  16 bits/cell is plenty for that too.)
  105. X * Since the JPEG code is intended to run in small memory model on 80x86
  106. X * machines, we can't just allocate the histogram in one chunk.  Instead
  107. X * of a true 3-D array, we use a row of pointers to 2-D arrays.  Each
  108. X * pointer corresponds to a Y value (typically 2^6 = 64 pointers) and
  109. X * each 2-D array has 2^5^2 = 1024 or 2^6^2 = 4096 entries.  Note that
  110. X * on 80x86 machines, the pointer row is in near memory but the actual
  111. X * arrays are in far memory (same arrangement as we use for image arrays).
  112. X */
  113. X
  114. X#ifndef HIST_Y_BITS        /* so you can override from Makefile */
  115. X#define HIST_Y_BITS  6        /* bits of precision in Y histogram */
  116. X#endif
  117. X#ifndef HIST_C_BITS        /* so you can override from Makefile */
  118. X#define HIST_C_BITS  5        /* bits of precision in Cb/Cr histogram */
  119. X#endif
  120. X
  121. X#define HIST_Y_ELEMS  (1<<HIST_Y_BITS) /* # of elements along histogram axes */
  122. X#define HIST_C_ELEMS  (1<<HIST_C_BITS)
  123. X
  124. X/* These are the amounts to shift an input value to get a histogram index.
  125. X * For a combination 8/12 bit implementation, would need variables here...
  126. X */
  127. X
  128. X#define Y_SHIFT  (BITS_IN_JSAMPLE-HIST_Y_BITS)
  129. X#define C_SHIFT  (BITS_IN_JSAMPLE-HIST_C_BITS)
  130. X
  131. X
  132. Xtypedef UINT16 histcell;    /* histogram cell; MUST be an unsigned type */
  133. X
  134. Xtypedef histcell FAR * histptr;    /* for pointers to histogram cells */
  135. X
  136. Xtypedef histcell hist1d[HIST_C_ELEMS]; /* typedefs for the array */
  137. Xtypedef hist1d FAR * hist2d;    /* type for the Y-level pointers */
  138. Xtypedef hist2d * hist3d;    /* type for top-level pointer */
  139. X
  140. Xstatic hist3d histogram;    /* pointer to the histogram */
  141. X
  142. X
  143. X/*
  144. X * Prescan some rows of pixels.
  145. X * In this module the prescan simply updates the histogram, which has been
  146. X * initialized to zeroes by color_quant_init.
  147. X * Note: workspace is probably not useful for this routine, but it is passed
  148. X * anyway to allow some code sharing within the pipeline controller.
  149. X */
  150. X
  151. XMETHODDEF void
  152. Xcolor_quant_prescan (decompress_info_ptr cinfo, int num_rows,
  153. X             JSAMPIMAGE image_data, JSAMPARRAY workspace)
  154. X{
  155. X  register JSAMPROW ptr0, ptr1, ptr2;
  156. X  register histptr histp;
  157. X  register int c0, c1, c2;
  158. X  int row;
  159. X  long col;
  160. X  long width = cinfo->image_width;
  161. X
  162. X  for (row = 0; row < num_rows; row++) {
  163. X    ptr0 = image_data[0][row];
  164. X    ptr1 = image_data[1][row];
  165. X    ptr2 = image_data[2][row];
  166. X    for (col = width; col > 0; col--) {
  167. X      /* get pixel value and index into the histogram */
  168. X      c0 = GETJSAMPLE(*ptr0++) >> Y_SHIFT;
  169. X      c1 = GETJSAMPLE(*ptr1++) >> C_SHIFT;
  170. X      c2 = GETJSAMPLE(*ptr2++) >> C_SHIFT;
  171. X      histp = & histogram[c0][c1][c2];
  172. X      /* increment, check for overflow and undo increment if so. */
  173. X      /* We assume unsigned representation here! */
  174. X      if (++(*histp) == 0)
  175. X    (*histp)--;
  176. X    }
  177. X  }
  178. X}
  179. X
  180. X
  181. X/*
  182. X * Now we have the really interesting routines: selection of a colormap
  183. X * given the completed histogram.
  184. X * These routines work with a list of "boxes", each representing a rectangular
  185. X * subset of the input color space (to histogram precision).
  186. X */
  187. X
  188. Xtypedef struct {
  189. X    /* The bounds of the box (inclusive); expressed as histogram indexes */
  190. X    int c0min, c0max;
  191. X    int c1min, c1max;
  192. X    int c2min, c2max;
  193. X    /* The number of nonzero histogram cells within this box */
  194. X    long colorcount;
  195. X      } box;
  196. Xtypedef box * boxptr;
  197. X
  198. Xstatic boxptr boxlist;        /* array with room for desired # of boxes */
  199. Xstatic int numboxes;        /* number of boxes currently in boxlist */
  200. X
  201. Xstatic JSAMPARRAY my_colormap;    /* the finished colormap (in YCbCr space) */
  202. X
  203. X
  204. XLOCAL boxptr
  205. Xfind_biggest_color_pop (void)
  206. X/* Find the splittable box with the largest color population */
  207. X/* Returns NULL if no splittable boxes remain */
  208. X{
  209. X  register boxptr boxp;
  210. X  register int i;
  211. X  register long max = 0;
  212. X  boxptr which = NULL;
  213. X  
  214. X  for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
  215. X    if (boxp->colorcount > max) {
  216. X      if (boxp->c0max > boxp->c0min || boxp->c1max > boxp->c1min ||
  217. X      boxp->c2max > boxp->c2min) {
  218. X    which = boxp;
  219. X    max = boxp->colorcount;
  220. X      }
  221. X    }
  222. X  }
  223. X  return which;
  224. X}
  225. X
  226. X
  227. XLOCAL boxptr
  228. Xfind_biggest_volume (void)
  229. X/* Find the splittable box with the largest (scaled) volume */
  230. X/* Returns NULL if no splittable boxes remain */
  231. X{
  232. X  register boxptr boxp;
  233. X  register int i;
  234. X  register INT32 max = 0;
  235. X  register INT32 norm, c0,c1,c2;
  236. X  boxptr which = NULL;
  237. X  
  238. X  /* We use 2-norm rather than real volume here.
  239. X   * Some care is needed since the differences are expressed in
  240. X   * histogram-cell units; if HIST_Y_BITS != HIST_C_BITS, we have to
  241. X   * adjust the scaling to get the proper scaled-YCbCr-space distance.
  242. X   * This code won't work right if HIST_Y_BITS < HIST_C_BITS,
  243. X   * but that shouldn't ever be true.
  244. X   * Note norm > 0 iff box is splittable, so need not check separately.
  245. X   */
  246. X  
  247. X  for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
  248. X    c0 = (boxp->c0max - boxp->c0min) * Y_SCALE;
  249. X    c1 = (boxp->c1max - boxp->c1min) << (HIST_Y_BITS-HIST_C_BITS);
  250. X    c2 = (boxp->c2max - boxp->c2min) << (HIST_Y_BITS-HIST_C_BITS);
  251. X    norm = c0*c0 + c1*c1 + c2*c2;
  252. X    if (norm > max) {
  253. X      which = boxp;
  254. X      max = norm;
  255. X    }
  256. X  }
  257. X  return which;
  258. X}
  259. X
  260. X
  261. XLOCAL void
  262. Xupdate_box (boxptr boxp)
  263. X/* Shrink the min/max bounds of a box to enclose only nonzero elements, */
  264. X/* and recompute its population */
  265. X{
  266. X  histptr histp;
  267. X  int c0,c1,c2;
  268. X  int c0min,c0max,c1min,c1max,c2min,c2max;
  269. X  long ccount;
  270. X  
  271. X  c0min = boxp->c0min;  c0max = boxp->c0max;
  272. X  c1min = boxp->c1min;  c1max = boxp->c1max;
  273. X  c2min = boxp->c2min;  c2max = boxp->c2max;
  274. X  
  275. X  if (c0max > c0min)
  276. X    for (c0 = c0min; c0 <= c0max; c0++)
  277. X      for (c1 = c1min; c1 <= c1max; c1++) {
  278. X    histp = & histogram[c0][c1][c2min];
  279. X    for (c2 = c2min; c2 <= c2max; c2++)
  280. X      if (*histp++ != 0) {
  281. X        boxp->c0min = c0min = c0;
  282. X        goto have_c0min;
  283. X      }
  284. X      }
  285. X have_c0min:
  286. X  if (c0max > c0min)
  287. X    for (c0 = c0max; c0 >= c0min; c0--)
  288. X      for (c1 = c1min; c1 <= c1max; c1++) {
  289. X    histp = & histogram[c0][c1][c2min];
  290. X    for (c2 = c2min; c2 <= c2max; c2++)
  291. X      if (*histp++ != 0) {
  292. X        boxp->c0max = c0max = c0;
  293. X        goto have_c0max;
  294. X      }
  295. X      }
  296. X have_c0max:
  297. X  if (c1max > c1min)
  298. X    for (c1 = c1min; c1 <= c1max; c1++)
  299. X      for (c0 = c0min; c0 <= c0max; c0++) {
  300. X    histp = & histogram[c0][c1][c2min];
  301. X    for (c2 = c2min; c2 <= c2max; c2++)
  302. X      if (*histp++ != 0) {
  303. X        boxp->c1min = c1min = c1;
  304. X        goto have_c1min;
  305. X      }
  306. X      }
  307. X have_c1min:
  308. X  if (c1max > c1min)
  309. X    for (c1 = c1max; c1 >= c1min; c1--)
  310. X      for (c0 = c0min; c0 <= c0max; c0++) {
  311. X    histp = & histogram[c0][c1][c2min];
  312. X    for (c2 = c2min; c2 <= c2max; c2++)
  313. X      if (*histp++ != 0) {
  314. X        boxp->c1max = c1max = c1;
  315. X        goto have_c1max;
  316. X      }
  317. X      }
  318. X have_c1max:
  319. X  if (c2max > c2min)
  320. X    for (c2 = c2min; c2 <= c2max; c2++)
  321. X      for (c0 = c0min; c0 <= c0max; c0++) {
  322. X    histp = & histogram[c0][c1min][c2];
  323. X    for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C_ELEMS)
  324. X      if (*histp != 0) {
  325. X        boxp->c2min = c2min = c2;
  326. X        goto have_c2min;
  327. X      }
  328. X      }
  329. X have_c2min:
  330. X  if (c2max > c2min)
  331. X    for (c2 = c2max; c2 >= c2min; c2--)
  332. X      for (c0 = c0min; c0 <= c0max; c0++) {
  333. X    histp = & histogram[c0][c1min][c2];
  334. X    for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C_ELEMS)
  335. X      if (*histp != 0) {
  336. X        boxp->c2max = c2max = c2;
  337. X        goto have_c2max;
  338. X      }
  339. X      }
  340. X have_c2max:
  341. X  
  342. X  /* Now scan remaining volume of box and compute population */
  343. X  ccount = 0;
  344. X  for (c0 = c0min; c0 <= c0max; c0++)
  345. X    for (c1 = c1min; c1 <= c1max; c1++) {
  346. X      histp = & histogram[c0][c1][c2min];
  347. X      for (c2 = c2min; c2 <= c2max; c2++, histp++)
  348. X    if (*histp != 0) {
  349. X      ccount++;
  350. X    }
  351. X    }
  352. X  boxp->colorcount = ccount;
  353. X}
  354. X
  355. X
  356. XLOCAL void
  357. Xmedian_cut (int desired_colors)
  358. X/* Repeatedly select and split the largest box until we have enough boxes */
  359. X{
  360. X  int n,lb;
  361. X  int c0,c1,c2,cmax;
  362. X  register boxptr b1,b2;
  363. X
  364. X  while (numboxes < desired_colors) {
  365. X    /* Select box to split */
  366. X    /* Current algorithm: by population for first half, then by volume */
  367. X    if (numboxes*2 <= desired_colors) {
  368. X      b1 = find_biggest_color_pop();
  369. X    } else {
  370. X      b1 = find_biggest_volume();
  371. X    }
  372. X    if (b1 == NULL)        /* no splittable boxes left! */
  373. X      break;
  374. X    b2 = &boxlist[numboxes];    /* where new box will go */
  375. X    /* Copy the color bounds to the new box. */
  376. X    b2->c0max = b1->c0max; b2->c1max = b1->c1max; b2->c2max = b1->c2max;
  377. X    b2->c0min = b1->c0min; b2->c1min = b1->c1min; b2->c2min = b1->c2min;
  378. X    /* Choose which axis to split the box on.
  379. X     * Current algorithm: longest scaled axis.
  380. X     * See notes in find_biggest_volume about scaling...
  381. X     */
  382. X    c0 = (b1->c0max - b1->c0min) * Y_SCALE;
  383. X    c1 = (b1->c1max - b1->c1min) << (HIST_Y_BITS-HIST_C_BITS);
  384. X    c2 = (b1->c2max - b1->c2min) << (HIST_Y_BITS-HIST_C_BITS);
  385. X    cmax = c0; n = 0;
  386. X    if (c1 > cmax) { cmax = c1; n = 1; }
  387. X    if (c2 > cmax) { n = 2; }
  388. X    /* Choose split point along selected axis, and update box bounds.
  389. X     * Current algorithm: split at halfway point.
  390. X     * (Since the box has been shrunk to minimum volume,
  391. X     * any split will produce two nonempty subboxes.)
  392. X     * Note that lb value is max for lower box, so must be < old max.
  393. X     */
  394. X    switch (n) {
  395. X    case 0:
  396. X      lb = (b1->c0max + b1->c0min) / 2;
  397. X      b1->c0max = lb;
  398. X      b2->c0min = lb+1;
  399. X      break;
  400. X    case 1:
  401. X      lb = (b1->c1max + b1->c1min) / 2;
  402. X      b1->c1max = lb;
  403. X      b2->c1min = lb+1;
  404. X      break;
  405. X    case 2:
  406. X      lb = (b1->c2max + b1->c2min) / 2;
  407. X      b1->c2max = lb;
  408. X      b2->c2min = lb+1;
  409. X      break;
  410. X    }
  411. X    /* Update stats for boxes */
  412. X    update_box(b1);
  413. X    update_box(b2);
  414. X    numboxes++;
  415. X  }
  416. X}
  417. X
  418. X
  419. XLOCAL void
  420. Xcompute_color (boxptr boxp, int icolor)
  421. X/* Compute representative color for a box, put it in my_colormap[icolor] */
  422. X{
  423. X  /* Current algorithm: mean weighted by pixels (not colors) */
  424. X  /* Note it is important to get the rounding correct! */
  425. X  histptr histp;
  426. X  int c0,c1,c2;
  427. X  int c0min,c0max,c1min,c1max,c2min,c2max;
  428. X  long count;
  429. X  long total = 0;
  430. X  long c0total = 0;
  431. X  long c1total = 0;
  432. X  long c2total = 0;
  433. X  
  434. X  c0min = boxp->c0min;  c0max = boxp->c0max;
  435. X  c1min = boxp->c1min;  c1max = boxp->c1max;
  436. X  c2min = boxp->c2min;  c2max = boxp->c2max;
  437. X  
  438. X  for (c0 = c0min; c0 <= c0max; c0++)
  439. X    for (c1 = c1min; c1 <= c1max; c1++) {
  440. X      histp = & histogram[c0][c1][c2min];
  441. X      for (c2 = c2min; c2 <= c2max; c2++) {
  442. X    if ((count = *histp++) != 0) {
  443. X      total += count;
  444. X      c0total += ((c0 << Y_SHIFT) + ((1<<Y_SHIFT)>>1)) * count;
  445. X      c1total += ((c1 << C_SHIFT) + ((1<<C_SHIFT)>>1)) * count;
  446. X      c2total += ((c2 << C_SHIFT) + ((1<<C_SHIFT)>>1)) * count;
  447. X    }
  448. X      }
  449. X    }
  450. X  
  451. X  my_colormap[0][icolor] = (JSAMPLE) ((c0total + (total>>1)) / total);
  452. X  my_colormap[1][icolor] = (JSAMPLE) ((c1total + (total>>1)) / total);
  453. X  my_colormap[2][icolor] = (JSAMPLE) ((c2total + (total>>1)) / total);
  454. X}
  455. X
  456. X
  457. XLOCAL void
  458. Xremap_colormap (decompress_info_ptr cinfo)
  459. X/* Remap the internal colormap to the output colorspace */
  460. X{
  461. X  /* This requires a little trickery since color_convert expects to
  462. X   * deal with 3-D arrays (a 2-D sample array for each component).
  463. X   * We must promote the colormaps into one-row 3-D arrays.
  464. X   */
  465. X  short ci;
  466. X  JSAMPARRAY input_hack[3];
  467. X  JSAMPARRAY output_hack[10];    /* assume no more than 10 output components */
  468. X
  469. X  for (ci = 0; ci < 3; ci++)
  470. X    input_hack[ci] = &(my_colormap[ci]);
  471. X  for (ci = 0; ci < cinfo->color_out_comps; ci++)
  472. X    output_hack[ci] = &(cinfo->colormap[ci]);
  473. X
  474. X  (*cinfo->methods->color_convert) (cinfo, 1,
  475. X                    (long) cinfo->actual_number_of_colors,
  476. X                    input_hack, output_hack);
  477. X}
  478. X
  479. X
  480. XLOCAL void
  481. Xselect_colors (decompress_info_ptr cinfo)
  482. X/* Master routine for color selection */
  483. X{
  484. X  int desired = cinfo->desired_number_of_colors;
  485. X  int i;
  486. X
  487. X  /* Allocate workspace for box list */
  488. X  boxlist = (boxptr) (*cinfo->emethods->alloc_small) (desired * SIZEOF(box));
  489. X  /* Initialize one box containing whole space */
  490. X  numboxes = 1;
  491. X  boxlist[0].c0min = 0;
  492. X  boxlist[0].c0max = MAXJSAMPLE >> Y_SHIFT;
  493. X  boxlist[0].c1min = 0;
  494. X  boxlist[0].c1max = MAXJSAMPLE >> C_SHIFT;
  495. X  boxlist[0].c2min = 0;
  496. X  boxlist[0].c2max = MAXJSAMPLE >> C_SHIFT;
  497. X  /* Shrink it to actually-used volume and set its statistics */
  498. X  update_box(& boxlist[0]);
  499. X  /* Perform median-cut to produce final box list */
  500. X  median_cut(desired);
  501. X  /* Compute the representative color for each box, fill my_colormap[] */
  502. X  for (i = 0; i < numboxes; i++)
  503. X    compute_color(& boxlist[i], i);
  504. X  cinfo->actual_number_of_colors = numboxes;
  505. X  /* Produce an output colormap in the desired output colorspace */
  506. X  remap_colormap(cinfo);
  507. X  TRACEMS1(cinfo->emethods, 1, "Selected %d colors for quantization",
  508. X       numboxes);
  509. X  /* Done with the box list */
  510. X  (*cinfo->emethods->free_small) ((void *) boxlist);
  511. X}
  512. X
  513. X
  514. X/*
  515. X * These routines are concerned with the time-critical task of mapping input
  516. X * colors to the nearest color in the selected colormap.
  517. X *
  518. X * We re-use the histogram space as an "inverse color map", essentially a
  519. X * cache for the results of nearest-color searches.  All colors within a
  520. X * histogram cell will be mapped to the same colormap entry, namely the one
  521. X * closest to the cell's center.  This may not be quite the closest entry to
  522. X * the actual input color, but it's almost as good.  A zero in the cache
  523. X * indicates we haven't found the nearest color for that cell yet; the array
  524. X * is cleared to zeroes before starting the mapping pass.  When we find the
  525. X * nearest color for a cell, its colormap index plus one is recorded in the
  526. X * cache for future use.  The pass2 scanning routines call fill_inverse_cmap
  527. X * when they need to use an unfilled entry in the cache.
  528. X *
  529. X * Our method of efficiently finding nearest colors is based on the "locally
  530. X * sorted search" idea described by Heckbert and on the incremental distance
  531. X * calculation described by Spencer W. Thomas in chapter III.1 of Graphics
  532. X * Gems II (James Arvo, ed.  Academic Press, 1991).  Thomas points out that
  533. X * the distances from a given colormap entry to each cell of the histogram can
  534. X * be computed quickly using an incremental method: the differences between
  535. X * distances to adjacent cells themselves differ by a constant.  This allows a
  536. X * fairly fast implementation of the "brute force" approach of computing the
  537. X * distance from every colormap entry to every histogram cell.  Unfortunately,
  538. X * it needs a work array to hold the best-distance-so-far for each histogram
  539. X * cell (because the inner loop has to be over cells, not colormap entries).
  540. X * The work array elements have to be INT32s, so the work array would need
  541. X * 256Kb at our recommended precision.  This is not feasible in DOS machines.
  542. X * Another disadvantage of the brute force approach is that it computes
  543. X * distances to every cell of the cubical histogram.  When working with YCbCr
  544. X * input, only about a quarter of the cube represents realizable colors, so
  545. X * many of the cells will never be used and filling them is wasted effort.
  546. X *
  547. X * To get around these problems, we apply Thomas' method to compute the
  548. X * nearest colors for only the cells within a small subbox of the histogram.
  549. X * The work array need be only as big as the subbox, so the memory usage
  550. X * problem is solved.  A subbox is processed only when some cell in it is
  551. X * referenced by the pass2 routines, so we will never bother with cells far
  552. X * outside the realizable color volume.  An additional advantage of this
  553. X * approach is that we can apply Heckbert's locality criterion to quickly
  554. X * eliminate colormap entries that are far away from the subbox; typically
  555. X * three-fourths of the colormap entries are rejected by Heckbert's criterion,
  556. X * and we need not compute their distances to individual cells in the subbox.
  557. X * The speed of this approach is heavily influenced by the subbox size: too
  558. X * small means too much overhead, too big loses because Heckbert's criterion
  559. X * can't eliminate as many colormap entries.  Empirically the best subbox
  560. X * size seems to be about 1/512th of the histogram (1/8th in each direction).
  561. X *
  562. X * Thomas' article also describes a refined method which is asymptotically
  563. X * faster than the brute-force method, but it is also far more complex and
  564. X * cannot efficiently be applied to small subboxes.  It is therefore not
  565. X * useful for programs intended to be portable to DOS machines.  On machines
  566. X * with plenty of memory, filling the whole histogram in one shot with Thomas'
  567. X * refined method might be faster than the present code --- but then again,
  568. X * it might not be any faster, and it's certainly more complicated.
  569. X */
  570. X
  571. X
  572. X#ifndef BOX_Y_LOG        /* so you can override from Makefile */
  573. X#define BOX_Y_LOG  (HIST_Y_BITS-3) /* log2(hist cells in update box, Y axis) */
  574. X#endif
  575. X#ifndef BOX_C_LOG        /* so you can override from Makefile */
  576. X#define BOX_C_LOG  (HIST_C_BITS-3) /* log2(hist cells in update box, C axes) */
  577. X#endif
  578. X
  579. X#define BOX_Y_ELEMS  (1<<BOX_Y_LOG) /* # of hist cells in update box */
  580. X#define BOX_C_ELEMS  (1<<BOX_C_LOG)
  581. X
  582. X#define BOX_Y_SHIFT  (Y_SHIFT + BOX_Y_LOG)
  583. X#define BOX_C_SHIFT  (C_SHIFT + BOX_C_LOG)
  584. X
  585. X
  586. X/*
  587. X * The next three routines implement inverse colormap filling.  They could
  588. X * all be folded into one big routine, but splitting them up this way saves
  589. X * some stack space (the mindist[] and bestdist[] arrays need not coexist)
  590. X * and may allow some compilers to produce better code by registerizing more
  591. X * inner-loop variables.
  592. X */
  593. X
  594. XLOCAL int
  595. Xfind_nearby_colors (decompress_info_ptr cinfo, int minc0, int minc1, int minc2,
  596. X            JSAMPLE colorlist[])
  597. X/* Locate the colormap entries close enough to an update box to be candidates
  598. X * for the nearest entry to some cell(s) in the update box.  The update box
  599. X * is specified by the center coordinates of its first cell.  The number of
  600. X * candidate colormap entries is returned, and their colormap indexes are
  601. X * placed in colorlist[].
  602. X * This routine uses Heckbert's "locally sorted search" criterion to select
  603. X * the colors that need further consideration.
  604. X */
  605. X{
  606. X  int numcolors = cinfo->actual_number_of_colors;
  607. X  int maxc0, maxc1, maxc2;
  608. X  int centerc0, centerc1, centerc2;
  609. X  int i, x, ncolors;
  610. X  INT32 minmaxdist, min_dist, max_dist, tdist;
  611. X  INT32 mindist[MAXNUMCOLORS];    /* min distance to colormap entry i */
  612. X
  613. X  /* Compute true coordinates of update box's upper corner and center.
  614. X   * Actually we compute the coordinates of the center of the upper-corner
  615. X   * histogram cell, which are the upper bounds of the volume we care about.
  616. X   * Note that since ">>" rounds down, the "center" values may be closer to
  617. X   * min than to max; hence comparisons to them must be "<=", not "<".
  618. X   */
  619. X  maxc0 = minc0 + ((1 << BOX_Y_SHIFT) - (1 << Y_SHIFT));
  620. X  centerc0 = (minc0 + maxc0) >> 1;
  621. X  maxc1 = minc1 + ((1 << BOX_C_SHIFT) - (1 << C_SHIFT));
  622. X  centerc1 = (minc1 + maxc1) >> 1;
  623. X  maxc2 = minc2 + ((1 << BOX_C_SHIFT) - (1 << C_SHIFT));
  624. X  centerc2 = (minc2 + maxc2) >> 1;
  625. X
  626. X  /* For each color in colormap, find:
  627. X   *  1. its minimum squared-distance to any point in the update box
  628. X   *     (zero if color is within update box);
  629. X   *  2. its maximum squared-distance to any point in the update box.
  630. X   * Both of these can be found by considering only the corners of the box.
  631. X   * We save the minimum distance for each color in mindist[];
  632. X   * only the smallest maximum distance is of interest.
  633. X   * Note we have to scale Y to get correct distance in scaled space.
  634. X   */
  635. X  minmaxdist = 0x7FFFFFFFL;
  636. X
  637. X  for (i = 0; i < numcolors; i++) {
  638. X    /* We compute the squared-c0-distance term, then add in the other two. */
  639. X    x = GETJSAMPLE(my_colormap[0][i]);
  640. X    if (x < minc0) {
  641. X      tdist = (x - minc0) * Y_SCALE;
  642. X      min_dist = tdist*tdist;
  643. X      tdist = (x - maxc0) * Y_SCALE;
  644. X      max_dist = tdist*tdist;
  645. X    } else if (x > maxc0) {
  646. X      tdist = (x - maxc0) * Y_SCALE;
  647. X      min_dist = tdist*tdist;
  648. X      tdist = (x - minc0) * Y_SCALE;
  649. X      max_dist = tdist*tdist;
  650. X    } else {
  651. X      /* within cell range so no contribution to min_dist */
  652. X      min_dist = 0;
  653. X      if (x <= centerc0) {
  654. X    tdist = (x - maxc0) * Y_SCALE;
  655. X    max_dist = tdist*tdist;
  656. X      } else {
  657. X    tdist = (x - minc0) * Y_SCALE;
  658. X    max_dist = tdist*tdist;
  659. X      }
  660. X    }
  661. X
  662. X    x = GETJSAMPLE(my_colormap[1][i]);
  663. X    if (x < minc1) {
  664. X      tdist = x - minc1;
  665. X      min_dist += tdist*tdist;
  666. X      tdist = x - maxc1;
  667. X      max_dist += tdist*tdist;
  668. X    } else if (x > maxc1) {
  669. X      tdist = x - maxc1;
  670. X      min_dist += tdist*tdist;
  671. X      tdist = x - minc1;
  672. X      max_dist += tdist*tdist;
  673. X    } else {
  674. X      /* within cell range so no contribution to min_dist */
  675. X      if (x <= centerc1) {
  676. X    tdist = x - maxc1;
  677. X    max_dist += tdist*tdist;
  678. X      } else {
  679. X    tdist = x - minc1;
  680. X    max_dist += tdist*tdist;
  681. X      }
  682. X    }
  683. X
  684. X    x = GETJSAMPLE(my_colormap[2][i]);
  685. X    if (x < minc2) {
  686. X      tdist = x - minc2;
  687. X      min_dist += tdist*tdist;
  688. X      tdist = x - maxc2;
  689. X      max_dist += tdist*tdist;
  690. X    } else if (x > maxc2) {
  691. X      tdist = x - maxc2;
  692. X      min_dist += tdist*tdist;
  693. X      tdist = x - minc2;
  694. X      max_dist += tdist*tdist;
  695. X    } else {
  696. X      /* within cell range so no contribution to min_dist */
  697. X      if (x <= centerc2) {
  698. X    tdist = x - maxc2;
  699. X    max_dist += tdist*tdist;
  700. X      } else {
  701. X    tdist = x - minc2;
  702. X    max_dist += tdist*tdist;
  703. X      }
  704. X    }
  705. X
  706. X    mindist[i] = min_dist;    /* save away the results */
  707. X    if (max_dist < minmaxdist)
  708. X      minmaxdist = max_dist;
  709. X  }
  710. X
  711. X  /* Now we know that no cell in the update box is more than minmaxdist
  712. X   * away from some colormap entry.  Therefore, only colors that are
  713. X   * within minmaxdist of some part of the box need be considered.
  714. X   */
  715. X  ncolors = 0;
  716. X  for (i = 0; i < numcolors; i++) {
  717. X    if (mindist[i] <= minmaxdist)
  718. X      colorlist[ncolors++] = (JSAMPLE) i;
  719. X  }
  720. X  return ncolors;
  721. X}
  722. X
  723. X
  724. XLOCAL void
  725. Xfind_best_colors (decompress_info_ptr cinfo, int minc0, int minc1, int minc2,
  726. X          int numcolors, JSAMPLE colorlist[], JSAMPLE bestcolor[])
  727. X/* Find the closest colormap entry for each cell in the update box,
  728. X * given the list of candidate colors prepared by find_nearby_colors.
  729. X * Return the indexes of the closest entries in the bestcolor[] array.
  730. X * This routine uses Thomas' incremental distance calculation method to
  731. X * find the distance from a colormap entry to successive cells in the box.
  732. X */
  733. X{
  734. X  int ic0, ic1, ic2;
  735. X  int i, icolor;
  736. X  register INT32 * bptr;    /* pointer into bestdist[] array */
  737. X  JSAMPLE * cptr;        /* pointer into bestcolor[] array */
  738. X  INT32 dist0, dist1;        /* initial distance values */
  739. X  register INT32 dist2;        /* current distance in inner loop */
  740. X  INT32 xx0, xx1;        /* distance increments */
  741. X  register INT32 xx2;
  742. X  INT32 inc0, inc1, inc2;    /* initial values for increments */
  743. X  /* This array holds the distance to the nearest-so-far color for each cell */
  744. X  INT32 bestdist[BOX_Y_ELEMS * BOX_C_ELEMS * BOX_C_ELEMS];
  745. X
  746. X  /* Initialize best-distance for each cell of the update box */
  747. X  bptr = bestdist;
  748. X  for (i = BOX_Y_ELEMS*BOX_C_ELEMS*BOX_C_ELEMS-1; i >= 0; i--)
  749. X    *bptr++ = 0x7FFFFFFFL;
  750. X  
  751. X  /* For each color selected by find_nearby_colors,
  752. X   * compute its distance to the center of each cell in the box.
  753. X   * If that's less than best-so-far, update best distance and color number.
  754. X   * Note we have to scale Y to get correct distance in scaled space.
  755. X   */
  756. X  
  757. X  /* Nominal steps between cell centers ("x" in Thomas article) */
  758. X#define STEP_Y  ((1 << Y_SHIFT) * Y_SCALE)
  759. X#define STEP_C  (1 << C_SHIFT)
  760. X  
  761. X  for (i = 0; i < numcolors; i++) {
  762. X    icolor = GETJSAMPLE(colorlist[i]);
  763. X    /* Compute (square of) distance from minc0/c1/c2 to this color */
  764. X    inc0 = (minc0 - (int) GETJSAMPLE(my_colormap[0][icolor])) * Y_SCALE;
  765. X    dist0 = inc0*inc0;
  766. X    inc1 = minc1 - (int) GETJSAMPLE(my_colormap[1][icolor]);
  767. X    dist0 += inc1*inc1;
  768. X    inc2 = minc2 - (int) GETJSAMPLE(my_colormap[2][icolor]);
  769. X    dist0 += inc2*inc2;
  770. X    /* Form the initial difference increments */
  771. X    inc0 = inc0 * (2 * STEP_Y) + STEP_Y * STEP_Y;
  772. X    inc1 = inc1 * (2 * STEP_C) + STEP_C * STEP_C;
  773. X    inc2 = inc2 * (2 * STEP_C) + STEP_C * STEP_C;
  774. X    /* Now loop over all cells in box, updating distance per Thomas method */
  775. X    bptr = bestdist;
  776. X    cptr = bestcolor;
  777. X    xx0 = inc0;
  778. X    for (ic0 = BOX_Y_ELEMS-1; ic0 >= 0; ic0--) {
  779. X      dist1 = dist0;
  780. X      xx1 = inc1;
  781. X      for (ic1 = BOX_C_ELEMS-1; ic1 >= 0; ic1--) {
  782. X    dist2 = dist1;
  783. X    xx2 = inc2;
  784. X    for (ic2 = BOX_C_ELEMS-1; ic2 >= 0; ic2--) {
  785. X      if (dist2 < *bptr) {
  786. X        *bptr = dist2;
  787. X        *cptr = (JSAMPLE) icolor;
  788. X      }
  789. X      dist2 += xx2;
  790. X      xx2 += 2 * STEP_C * STEP_C;
  791. X      bptr++;
  792. X      cptr++;
  793. X    }
  794. X    dist1 += xx1;
  795. X    xx1 += 2 * STEP_C * STEP_C;
  796. X      }
  797. X      dist0 += xx0;
  798. X      xx0 += 2 * STEP_Y * STEP_Y;
  799. X    }
  800. X  }
  801. X}
  802. X
  803. X
  804. XLOCAL void
  805. Xfill_inverse_cmap (decompress_info_ptr cinfo, int c0, int c1, int c2)
  806. X/* Fill the inverse-colormap entries in the update box that contains */
  807. X/* histogram cell c0/c1/c2.  (Only that one cell MUST be filled, but */
  808. X/* we can fill as many others as we wish.) */
  809. X{
  810. X  int minc0, minc1, minc2;    /* lower left corner of update box */
  811. X  int ic0, ic1, ic2;
  812. X  register JSAMPLE * cptr;    /* pointer into bestcolor[] array */
  813. X  register histptr cachep;    /* pointer into main cache array */
  814. X  /* This array lists the candidate colormap indexes. */
  815. X  JSAMPLE colorlist[MAXNUMCOLORS];
  816. X  int numcolors;        /* number of candidate colors */
  817. X  /* This array holds the actually closest colormap index for each cell. */
  818. X  JSAMPLE bestcolor[BOX_Y_ELEMS * BOX_C_ELEMS * BOX_C_ELEMS];
  819. X
  820. X  /* Convert cell coordinates to update box ID */
  821. X  c0 >>= BOX_Y_LOG;
  822. X  c1 >>= BOX_C_LOG;
  823. X  c2 >>= BOX_C_LOG;
  824. X
  825. X  /* Compute true coordinates of update box's origin corner.
  826. X   * Actually we compute the coordinates of the center of the corner
  827. X   * histogram cell, which are the lower bounds of the volume we care about.
  828. X   */
  829. X  minc0 = (c0 << BOX_Y_SHIFT) + ((1 << Y_SHIFT) >> 1);
  830. X  minc1 = (c1 << BOX_C_SHIFT) + ((1 << C_SHIFT) >> 1);
  831. X  minc2 = (c2 << BOX_C_SHIFT) + ((1 << C_SHIFT) >> 1);
  832. X  
  833. X  /* Determine which colormap entries are close enough to be candidates
  834. X   * for the nearest entry to some cell in the update box.
  835. X   */
  836. X  numcolors = find_nearby_colors(cinfo, minc0, minc1, minc2, colorlist);
  837. X
  838. X  /* Determine the actually nearest colors. */
  839. X  find_best_colors(cinfo, minc0, minc1, minc2, numcolors, colorlist,
  840. X           bestcolor);
  841. X
  842. X  /* Save the best color numbers (plus 1) in the main cache array */
  843. X  c0 <<= BOX_Y_LOG;        /* convert ID back to base cell indexes */
  844. X  c1 <<= BOX_C_LOG;
  845. X  c2 <<= BOX_C_LOG;
  846. X  cptr = bestcolor;
  847. X  for (ic0 = 0; ic0 < BOX_Y_ELEMS; ic0++) {
  848. X    for (ic1 = 0; ic1 < BOX_C_ELEMS; ic1++) {
  849. X      cachep = & histogram[c0+ic0][c1+ic1][c2];
  850. X      for (ic2 = 0; ic2 < BOX_C_ELEMS; ic2++) {
  851. X    *cachep++ = (histcell) (GETJSAMPLE(*cptr++) + 1);
  852. X      }
  853. X    }
  854. X  }
  855. X}
  856. X
  857. X
  858. X/*
  859. X * These routines perform second-pass scanning of the image: map each pixel to
  860. X * the proper colormap index, and output the indexes to the output file.
  861. X *
  862. X * output_workspace is a one-component array of pixel dimensions at least
  863. X * as large as the input image strip; it can be used to hold the converted
  864. X * pixels' colormap indexes.
  865. X */
  866. X
  867. XMETHODDEF void
  868. Xpass2_nodither (decompress_info_ptr cinfo, int num_rows,
  869. X        JSAMPIMAGE image_data, JSAMPARRAY output_workspace)
  870. X/* This version performs no dithering */
  871. X{
  872. X  register JSAMPROW ptr0, ptr1, ptr2, outptr;
  873. X  register histptr cachep;
  874. X  register int c0, c1, c2;
  875. X  int row;
  876. X  long col;
  877. X  long width = cinfo->image_width;
  878. X
  879. X  /* Convert data to colormap indexes, which we save in output_workspace */
  880. X  for (row = 0; row < num_rows; row++) {
  881. X    ptr0 = image_data[0][row];
  882. X    ptr1 = image_data[1][row];
  883. X    ptr2 = image_data[2][row];
  884. X    outptr = output_workspace[row];
  885. X    for (col = width; col > 0; col--) {
  886. X      /* get pixel value and index into the cache */
  887. X      c0 = GETJSAMPLE(*ptr0++) >> Y_SHIFT;
  888. X      c1 = GETJSAMPLE(*ptr1++) >> C_SHIFT;
  889. X      c2 = GETJSAMPLE(*ptr2++) >> C_SHIFT;
  890. X      cachep = & histogram[c0][c1][c2];
  891. X      /* If we have not seen this color before, find nearest colormap entry */
  892. X      /* and update the cache */
  893. X      if (*cachep == 0)
  894. X    fill_inverse_cmap(cinfo, c0,c1,c2);
  895. X      /* Now emit the colormap index for this cell */
  896. X      *outptr++ = (JSAMPLE) (*cachep - 1);
  897. X    }
  898. X  }
  899. X  /* Emit converted rows to the output file */
  900. X  (*cinfo->methods->put_pixel_rows) (cinfo, num_rows, &output_workspace);
  901. X}
  902. X
  903. X
  904. X/* Declarations for Floyd-Steinberg dithering.
  905. X *
  906. X * Errors are accumulated into the arrays evenrowerrs[] and oddrowerrs[].
  907. X * These have resolutions of 1/16th of a pixel count.  The error at a given
  908. X * pixel is propagated to its unprocessed neighbors using the standard F-S
  909. X * fractions,
  910. X *        ...    (here)    7/16
  911. X *        3/16    5/16    1/16
  912. X * We work left-to-right on even rows, right-to-left on odd rows.
  913. X *
  914. X * Each of the arrays has (#columns + 2) entries; the extra entry
  915. X * at each end saves us from special-casing the first and last pixels.
  916. X * Each entry is three values long.
  917. X * In evenrowerrs[], the entries for a component are stored left-to-right, but
  918. X * in oddrowerrs[] they are stored right-to-left.  This means we always
  919. X * process the current row's error entries in increasing order and the next
  920. X * row's error entries in decreasing order, regardless of whether we are
  921. X * working L-to-R or R-to-L in the pixel data!
  922. X *
  923. X * Note: on a wide image, we might not have enough room in a PC's near data
  924. X * segment to hold the error arrays; so they are allocated with alloc_medium.
  925. X */
  926. X
  927. X#ifdef EIGHT_BIT_SAMPLES
  928. Xtypedef INT16 FSERROR;        /* 16 bits should be enough */
  929. X#else
  930. Xtypedef INT32 FSERROR;        /* may need more than 16 bits? */
  931. X#endif
  932. X
  933. Xtypedef FSERROR FAR *FSERRPTR;    /* pointer to error array (in FAR storage!) */
  934. X
  935. Xstatic FSERRPTR evenrowerrs, oddrowerrs; /* current-row and next-row errors */
  936. Xstatic boolean on_odd_row;    /* flag to remember which row we are on */
  937. X
  938. X
  939. XMETHODDEF void
  940. Xpass2_dither (decompress_info_ptr cinfo, int num_rows,
  941. X          JSAMPIMAGE image_data, JSAMPARRAY output_workspace)
  942. X/* This version performs Floyd-Steinberg dithering */
  943. X{
  944. X  register FSERROR val;
  945. X  register FSERRPTR thisrowerr, nextrowerr;
  946. X  register FSERROR c0, c1, c2;
  947. X  register int pixcode;
  948. X  JSAMPROW ptr0, ptr1, ptr2, outptr;
  949. X  histptr cachep;
  950. X  int dir;
  951. X  long col;
  952. X  int row;
  953. X  long width = cinfo->image_width;
  954. X
  955. X  /* Convert data to colormap indexes, which we save in output_workspace */
  956. X  for (row = 0; row < num_rows; row++) {
  957. X    ptr0 = image_data[0][row];
  958. X    ptr1 = image_data[1][row];
  959. X    ptr2 = image_data[2][row];
  960. X    outptr = output_workspace[row];
  961. X    if (on_odd_row) {
  962. X      /* work right to left in this row */
  963. X      ptr0 += width - 1;
  964. X      ptr1 += width - 1;
  965. X      ptr2 += width - 1;
  966. X      outptr += width - 1;
  967. X      dir = -1;
  968. X      thisrowerr = oddrowerrs + 3;
  969. X      nextrowerr = evenrowerrs + width*3;
  970. X      on_odd_row = FALSE;    /* flip for next time */
  971. X    } else {
  972. X      /* work left to right in this row */
  973. X      dir = 1;
  974. X      thisrowerr = evenrowerrs + 3;
  975. X      nextrowerr = oddrowerrs + width*3;
  976. X      on_odd_row = TRUE;    /* flip for next time */
  977. X    }
  978. X    /* need only initialize this one entry in nextrowerr */
  979. X    nextrowerr[0] = nextrowerr[1] = nextrowerr[2] = 0;
  980. X    for (col = width; col > 0; col--) {
  981. X      /* Get this pixel's value and add accumulated errors */
  982. X      /* The errors are in units of 1/16th pixel value */
  983. X      val = (GETJSAMPLE(*ptr0) << 4) + thisrowerr[0];
  984. X      if (val <= 0) val = 0;    /* must watch for range overflow! */
  985. X      else {
  986. X    val += 8;        /* divide by 16 with proper rounding */
  987. X    val >>= 4;
  988. X    if (val > MAXJSAMPLE) val = MAXJSAMPLE;
  989. X      }
  990. X      c0 = val;
  991. X      val = (GETJSAMPLE(*ptr1) << 4) + thisrowerr[1];
  992. X      if (val <= 0) val = 0;    /* must watch for range overflow! */
  993. X      else {
  994. X    val += 8;        /* divide by 16 with proper rounding */
  995. X    val >>= 4;
  996. X    if (val > MAXJSAMPLE) val = MAXJSAMPLE;
  997. X      }
  998. X      c1 = val;
  999. X      val = (GETJSAMPLE(*ptr2) << 4) + thisrowerr[2];
  1000. X      if (val <= 0) val = 0;    /* must watch for range overflow! */
  1001. X      else {
  1002. X    val += 8;        /* divide by 16 with proper rounding */
  1003. X    val >>= 4;
  1004. X    if (val > MAXJSAMPLE) val = MAXJSAMPLE;
  1005. X      }
  1006. X      c2 = val;
  1007. X      /* Index into the cache with adjusted value */
  1008. X      cachep = & histogram[c0 >> Y_SHIFT][c1 >> C_SHIFT][c2 >> C_SHIFT];
  1009. X      /* If we have not seen this color before, find nearest colormap */
  1010. X      /* entry and update the cache */
  1011. X      if (*cachep == 0)
  1012. X    fill_inverse_cmap(cinfo, c0 >> Y_SHIFT, c1 >> C_SHIFT, c2 >> C_SHIFT);
  1013. X      /* Now emit the colormap index for this cell */
  1014. X      pixcode = *cachep - 1;
  1015. X      *outptr = (JSAMPLE) pixcode;
  1016. X      /* Compute representation error for this pixel */
  1017. X      c0 -= (FSERROR) GETJSAMPLE(my_colormap[0][pixcode]);
  1018. X      c1 -= (FSERROR) GETJSAMPLE(my_colormap[1][pixcode]);
  1019. X      c2 -= (FSERROR) GETJSAMPLE(my_colormap[2][pixcode]);
  1020. X      /* Propagate error to adjacent pixels */
  1021. X      /* Remember that nextrowerr entries are in reverse order! */
  1022. X      val = c0 * 2;
  1023. X      nextrowerr[0-3]  = c0;    /* not +=, since not initialized yet */
  1024. X      c0 += val;        /* form error * 3 */
  1025. X      nextrowerr[0+3] += c0;
  1026. X      c0 += val;        /* form error * 5 */
  1027. X      nextrowerr[0  ] += c0;
  1028. X      c0 += val;        /* form error * 7 */
  1029. X      thisrowerr[0+3] += c0;
  1030. X      val = c1 * 2;
  1031. X      nextrowerr[1-3]  = c1;    /* not +=, since not initialized yet */
  1032. X      c1 += val;        /* form error * 3 */
  1033. X      nextrowerr[1+3] += c1;
  1034. X      c1 += val;        /* form error * 5 */
  1035. X      nextrowerr[1  ] += c1;
  1036. X      c1 += val;        /* form error * 7 */
  1037. X      thisrowerr[1+3] += c1;
  1038. X      val = c2 * 2;
  1039. X      nextrowerr[2-3]  = c2;    /* not +=, since not initialized yet */
  1040. X      c2 += val;        /* form error * 3 */
  1041. X      nextrowerr[2+3] += c2;
  1042. X      c2 += val;        /* form error * 5 */
  1043. X      nextrowerr[2  ] += c2;
  1044. X      c2 += val;        /* form error * 7 */
  1045. X      thisrowerr[2+3] += c2;
  1046. X      /* Advance to next column */
  1047. X      ptr0 += dir;
  1048. X      ptr1 += dir;
  1049. X      ptr2 += dir;
  1050. X      outptr += dir;
  1051. X      thisrowerr += 3;        /* cur-row error ptr advances to right */
  1052. X      nextrowerr -= 3;        /* next-row error ptr advances to left */
  1053. X    }
  1054. X  }
  1055. X  /* Emit converted rows to the output file */
  1056. X  (*cinfo->methods->put_pixel_rows) (cinfo, num_rows, &output_workspace);
  1057. X}
  1058. X
  1059. X
  1060. X/*
  1061. X * Initialize for two-pass color quantization.
  1062. X */
  1063. X
  1064. XMETHODDEF void
  1065. Xcolor_quant_init (decompress_info_ptr cinfo)
  1066. X{
  1067. X  int i;
  1068. X
  1069. X  /* Lower bound on # of colors ... somewhat arbitrary as long as > 0 */
  1070. X  if (cinfo->desired_number_of_colors < 8)
  1071. X    ERREXIT(cinfo->emethods, "Cannot request less than 8 quantized colors");
  1072. X  /* Make sure colormap indexes can be represented by JSAMPLEs */
  1073. X  if (cinfo->desired_number_of_colors > MAXNUMCOLORS)
  1074. X    ERREXIT1(cinfo->emethods, "Cannot request more than %d quantized colors",
  1075. X         MAXNUMCOLORS);
  1076. X
  1077. X  /* Allocate and zero the histogram */
  1078. X  histogram = (hist3d) (*cinfo->emethods->alloc_small)
  1079. X                (HIST_Y_ELEMS * SIZEOF(hist2d));
  1080. X  for (i = 0; i < HIST_Y_ELEMS; i++) {
  1081. X    histogram[i] = (hist2d) (*cinfo->emethods->alloc_medium)
  1082. X                (HIST_C_ELEMS*HIST_C_ELEMS * SIZEOF(histcell));
  1083. X    jzero_far((void FAR *) histogram[i],
  1084. X          HIST_C_ELEMS*HIST_C_ELEMS * SIZEOF(histcell));
  1085. X  }
  1086. X
  1087. X  /* Allocate storage for the internal and external colormaps. */
  1088. X  /* We do this now since it is FAR storage and may affect the memory */
  1089. X  /* manager's space calculations. */
  1090. X  my_colormap = (*cinfo->emethods->alloc_small_sarray)
  1091. X            ((long) cinfo->desired_number_of_colors,
  1092. X             (long) 3);
  1093. X  cinfo->colormap = (*cinfo->emethods->alloc_small_sarray)
  1094. X            ((long) cinfo->desired_number_of_colors,
  1095. X             (long) cinfo->color_out_comps);
  1096. X
  1097. X  /* Allocate Floyd-Steinberg workspace if necessary */
  1098. X  /* This isn't needed until pass 2, but again it is FAR storage. */
  1099. X  if (cinfo->use_dithering) {
  1100. X    size_t arraysize = (size_t) ((cinfo->image_width + 2L) * 3L * SIZEOF(FSERROR));
  1101. X
  1102. X    evenrowerrs = (FSERRPTR) (*cinfo->emethods->alloc_medium) (arraysize);
  1103. X    oddrowerrs  = (FSERRPTR) (*cinfo->emethods->alloc_medium) (arraysize);
  1104. X    /* we only need to zero the forward contribution for current row. */
  1105. X    jzero_far((void FAR *) evenrowerrs, arraysize);
  1106. X    on_odd_row = FALSE;
  1107. X  }
  1108. X
  1109. X  /* Indicate number of passes needed, excluding the prescan pass. */
  1110. X  cinfo->total_passes++;    /* I always use one pass */
  1111. X}
  1112. X
  1113. X
  1114. X/*
  1115. X * Perform two-pass quantization: rescan the image data and output the
  1116. X * converted data via put_color_map and put_pixel_rows.
  1117. X * The source_method is a routine that can scan the image data; it can
  1118. X * be called as many times as desired.  The processing routine called by
  1119. X * source_method has the same interface as color_quantize does in the
  1120. X * one-pass case, except it must call put_pixel_rows itself.  (This allows
  1121. X * me to use multiple passes in which earlier passes don't output anything.)
  1122. X */
  1123. X
  1124. XMETHODDEF void
  1125. Xcolor_quant_doit (decompress_info_ptr cinfo, quantize_caller_ptr source_method)
  1126. X{
  1127. X  int i;
  1128. X
  1129. X  /* Select the representative colors */
  1130. X  select_colors(cinfo);
  1131. X  /* Pass the external colormap to the output module. */
  1132. X  /* NB: the output module may continue to use the colormap until shutdown. */
  1133. X  (*cinfo->methods->put_color_map) (cinfo, cinfo->actual_number_of_colors,
  1134. X                    cinfo->colormap);
  1135. X  /* Re-zero the histogram so pass 2 can use it as nearest-color cache */
  1136. X  for (i = 0; i < HIST_Y_ELEMS; i++) {
  1137. X    jzero_far((void FAR *) histogram[i],
  1138. X          HIST_C_ELEMS*HIST_C_ELEMS * SIZEOF(histcell));
  1139. X  }
  1140. X  /* Perform pass 2 */
  1141. X  if (cinfo->use_dithering)
  1142. X    (*source_method) (cinfo, pass2_dither);
  1143. X  else
  1144. X    (*source_method) (cinfo, pass2_nodither);
  1145. X}
  1146. X
  1147. X
  1148. X/*
  1149. X * Finish up at the end of the file.
  1150. X */
  1151. X
  1152. XMETHODDEF void
  1153. Xcolor_quant_term (decompress_info_ptr cinfo)
  1154. X{
  1155. X  /* no work (we let free_all release the histogram/cache and colormaps) */
  1156. X  /* Note that we *mustn't* free the external colormap before free_all, */
  1157. X  /* since output module may use it! */
  1158. X}
  1159. X
  1160. X
  1161. X/*
  1162. X * Map some rows of pixels to the output colormapped representation.
  1163. X * Not used in two-pass case.
  1164. X */
  1165. X
  1166. XMETHODDEF void
  1167. Xcolor_quantize (decompress_info_ptr cinfo, int num_rows,
  1168. X        JSAMPIMAGE input_data, JSAMPARRAY output_data)
  1169. X{
  1170. X  ERREXIT(cinfo->emethods, "Should not get here!");
  1171. X}
  1172. X
  1173. X
  1174. X/*
  1175. X * The method selection routine for 2-pass color quantization.
  1176. X */
  1177. X
  1178. XGLOBAL void
  1179. Xjsel2quantize (decompress_info_ptr cinfo)
  1180. X{
  1181. X  if (cinfo->two_pass_quantize) {
  1182. X    /* Make sure jdmaster didn't give me a case I can't handle */
  1183. X    if (cinfo->num_components != 3 || cinfo->jpeg_color_space != CS_YCbCr)
  1184. X      ERREXIT(cinfo->emethods, "2-pass quantization only handles YCbCr input");
  1185. X    cinfo->methods->color_quant_init = color_quant_init;
  1186. X    cinfo->methods->color_quant_prescan = color_quant_prescan;
  1187. X    cinfo->methods->color_quant_doit = color_quant_doit;
  1188. X    cinfo->methods->color_quant_term = color_quant_term;
  1189. X    cinfo->methods->color_quantize = color_quantize;
  1190. X  }
  1191. X}
  1192. X
  1193. X#endif /* QUANT_2PASS_SUPPORTED */
  1194. END_OF_FILE
  1195.   if test 42181 -ne `wc -c <'jquant2.c'`; then
  1196.     echo shar: \"'jquant2.c'\" unpacked with wrong size!
  1197.   fi
  1198.   # end of 'jquant2.c'
  1199. fi
  1200. if test -f 'jrdppm.c' -a "${1}" != "-c" ; then 
  1201.   echo shar: Will not clobber existing file \"'jrdppm.c'\"
  1202. else
  1203.   echo shar: Extracting \"'jrdppm.c'\" \(8944 characters\)
  1204.   sed "s/^X//" >'jrdppm.c' <<'END_OF_FILE'
  1205. X/*
  1206. X * jrdppm.c
  1207. X *
  1208. X * Copyright (C) 1991, 1992, Thomas G. Lane.
  1209. X * This file is part of the Independent JPEG Group's software.
  1210. X * For conditions of distribution and use, see the accompanying README file.
  1211. X *
  1212. X * This file contains routines to read input images in PPM format.
  1213. X * The PBMPLUS library is NOT required to compile this software,
  1214. X * but it is highly useful as a set of PPM image manipulation programs.
  1215. X *
  1216. X * These routines may need modification for non-Unix environments or
  1217. X * specialized applications.  As they stand, they assume input from
  1218. X * an ordinary stdio stream.  They further assume that reading begins
  1219. X * at the start of the file; input_init may need work if the
  1220. X * user interface has already read some data (e.g., to determine that
  1221. X * the file is indeed PPM format).
  1222. X *
  1223. X * These routines are invoked via the methods get_input_row
  1224. X * and input_init/term.
  1225. X */
  1226. X
  1227. X#include "jinclude.h"
  1228. X
  1229. X#ifdef PPM_SUPPORTED
  1230. X
  1231. X
  1232. Xstatic JSAMPLE * rescale;    /* => maxval-remapping array, or NULL */
  1233. X
  1234. X
  1235. X/* Portions of this code are based on the PBMPLUS library, which is:
  1236. X**
  1237. X** Copyright (C) 1988 by Jef Poskanzer.
  1238. X**
  1239. X** Permission to use, copy, modify, and distribute this software and its
  1240. X** documentation for any purpose and without fee is hereby granted, provided
  1241. X** that the above copyright notice appear in all copies and that both that
  1242. X** copyright notice and this permission notice appear in supporting
  1243. X** documentation.  This software is provided "as is" without express or
  1244. X** implied warranty.
  1245. X*/
  1246. X
  1247. X
  1248. XLOCAL int
  1249. Xpbm_getc (FILE * file)
  1250. X/* Read next char, skipping over any comments */
  1251. X/* A comment/newline sequence is returned as a newline */
  1252. X{
  1253. X  register int ch;
  1254. X  
  1255. X  ch = getc(file);
  1256. X  if (ch == '#') {
  1257. X    do {
  1258. X      ch = getc(file);
  1259. X    } while (ch != '\n' && ch != EOF);
  1260. X  }
  1261. X  return ch;
  1262. X}
  1263. X
  1264. X
  1265. XLOCAL unsigned int
  1266. Xread_pbm_integer (compress_info_ptr cinfo)
  1267. X/* Read an unsigned decimal integer from the PPM file */
  1268. X/* Swallows one trailing character after the integer */
  1269. X/* Note that on a 16-bit-int machine, only values up to 64k can be read. */
  1270. X/* This should not be a problem in practice. */
  1271. X{
  1272. X  register int ch;
  1273. X  register unsigned int val;
  1274. X  
  1275. X  /* Skip any leading whitespace */
  1276. X  do {
  1277. X    ch = pbm_getc(cinfo->input_file);
  1278. X    if (ch == EOF)
  1279. X      ERREXIT(cinfo->emethods, "Premature EOF in PPM file");
  1280. X  } while (ch == ' ' || ch == '\t' || ch == '\n');
  1281. X  
  1282. X  if (ch < '0' || ch > '9')
  1283. X    ERREXIT(cinfo->emethods, "Bogus data in PPM file");
  1284. X  
  1285. X  val = ch - '0';
  1286. X  while ((ch = pbm_getc(cinfo->input_file)) >= '0' && ch <= '9') {
  1287. X    val *= 10;
  1288. X    val += ch - '0';
  1289. X  }
  1290. X  return val;
  1291. X}
  1292. X
  1293. X
  1294. X/*
  1295. X * Read one row of pixels.
  1296. X *
  1297. X * We provide several different versions depending on input file format.
  1298. X * In all cases, input is scaled to the size of JSAMPLE; it's possible that
  1299. X * when JSAMPLE is 12 bits, this would not really be desirable.
  1300. X *
  1301. X * Note that a really fast path is provided for reading raw files with
  1302. X * maxval = MAXJSAMPLE, which is the normal case (at least for 8-bit JSAMPLEs).
  1303. X */
  1304. X
  1305. X
  1306. XMETHODDEF void
  1307. Xget_text_gray_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  1308. X/* This version is for reading text-format PGM files with any maxval */
  1309. X{
  1310. X  register JSAMPROW ptr0;
  1311. X  register unsigned int val;
  1312. X  register long col;
  1313. X  
  1314. X  ptr0 = pixel_row[0];
  1315. X  for (col = cinfo->image_width; col > 0; col--) {
  1316. X    val = read_pbm_integer(cinfo);
  1317. X    if (rescale != NULL)
  1318. X      val = rescale[val];
  1319. X    *ptr0++ = (JSAMPLE) val;
  1320. X  }
  1321. X}
  1322. X
  1323. X
  1324. XMETHODDEF void
  1325. Xget_text_rgb_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  1326. X/* This version is for reading text-format PPM files with any maxval */
  1327. X{
  1328. X  register JSAMPROW ptr0, ptr1, ptr2;
  1329. X  register unsigned int val;
  1330. X  register long col;
  1331. X  
  1332. X  ptr0 = pixel_row[0];
  1333. X  ptr1 = pixel_row[1];
  1334. X  ptr2 = pixel_row[2];
  1335. X  for (col = cinfo->image_width; col > 0; col--) {
  1336. X    val = read_pbm_integer(cinfo);
  1337. X    if (rescale != NULL)
  1338. X      val = rescale[val];
  1339. X    *ptr0++ = (JSAMPLE) val;
  1340. X    val = read_pbm_integer(cinfo);
  1341. X    if (rescale != NULL)
  1342. X      val = rescale[val];
  1343. X    *ptr1++ = (JSAMPLE) val;
  1344. X    val = read_pbm_integer(cinfo);
  1345. X    if (rescale != NULL)
  1346. X      val = rescale[val];
  1347. X    *ptr2++ = (JSAMPLE) val;
  1348. X  }
  1349. X}
  1350. X
  1351. X
  1352. XMETHODDEF void
  1353. Xget_scaled_gray_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  1354. X/* This version is for reading raw-format PGM files with any maxval */
  1355. X{
  1356. X  register FILE * infile = cinfo->input_file;
  1357. X  register JSAMPROW ptr0;
  1358. X  register long col;
  1359. X  
  1360. X  ptr0 = pixel_row[0];
  1361. X  for (col = cinfo->image_width; col > 0; col--) {
  1362. X    *ptr0++ = rescale[getc(infile)];
  1363. X  }
  1364. X}
  1365. X
  1366. X
  1367. XMETHODDEF void
  1368. Xget_scaled_rgb_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  1369. X/* This version is for reading raw-format PPM files with any maxval */
  1370. X{
  1371. X  register FILE * infile = cinfo->input_file;
  1372. X  register JSAMPROW ptr0, ptr1, ptr2;
  1373. X  register long col;
  1374. X  
  1375. X  ptr0 = pixel_row[0];
  1376. X  ptr1 = pixel_row[1];
  1377. X  ptr2 = pixel_row[2];
  1378. X  for (col = cinfo->image_width; col > 0; col--) {
  1379. X    *ptr0++ = rescale[getc(infile)];
  1380. X    *ptr1++ = rescale[getc(infile)];
  1381. X    *ptr2++ = rescale[getc(infile)];
  1382. X  }
  1383. X}
  1384. X
  1385. X
  1386. XMETHODDEF void
  1387. Xget_raw_gray_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  1388. X/* This version is for reading raw-format PGM files with maxval = MAXJSAMPLE */
  1389. X{
  1390. X  register FILE * infile = cinfo->input_file;
  1391. X  register JSAMPROW ptr0;
  1392. X  register long col;
  1393. X  
  1394. X  ptr0 = pixel_row[0];
  1395. X  for (col = cinfo->image_width; col > 0; col--) {
  1396. X    *ptr0++ = (JSAMPLE) getc(infile);
  1397. X  }
  1398. X}
  1399. X
  1400. X
  1401. XMETHODDEF void
  1402. Xget_raw_rgb_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  1403. X/* This version is for reading raw-format PPM files with maxval = MAXJSAMPLE */
  1404. X{
  1405. X  register FILE * infile = cinfo->input_file;
  1406. X  register JSAMPROW ptr0, ptr1, ptr2;
  1407. X  register long col;
  1408. X  
  1409. X  ptr0 = pixel_row[0];
  1410. X  ptr1 = pixel_row[1];
  1411. X  ptr2 = pixel_row[2];
  1412. X  for (col = cinfo->image_width; col > 0; col--) {
  1413. X    *ptr0++ = (JSAMPLE) getc(infile);
  1414. X    *ptr1++ = (JSAMPLE) getc(infile);
  1415. X    *ptr2++ = (JSAMPLE) getc(infile);
  1416. X  }
  1417. X}
  1418. X
  1419. X
  1420. X/*
  1421. X * Read the file header; return image size and component count.
  1422. X */
  1423. X
  1424. XMETHODDEF void
  1425. Xinput_init (compress_info_ptr cinfo)
  1426. X{
  1427. X  int c;
  1428. X  unsigned int w, h, maxval;
  1429. X
  1430. X  if (getc(cinfo->input_file) != 'P')
  1431. X    ERREXIT(cinfo->emethods, "Not a PPM file");
  1432. X
  1433. X  c = getc(cinfo->input_file);    /* save format discriminator for a sec */
  1434. X
  1435. X  w = read_pbm_integer(cinfo);    /* while we fetch the header info */
  1436. X  h = read_pbm_integer(cinfo);
  1437. X  maxval = read_pbm_integer(cinfo);
  1438. X
  1439. X  switch (c) {
  1440. X  case '2':            /* it's a text-format PGM file */
  1441. X    cinfo->methods->get_input_row = get_text_gray_row;
  1442. X    cinfo->input_components = 1;
  1443. X    cinfo->in_color_space = CS_GRAYSCALE;
  1444. X    break;
  1445. X
  1446. X  case '3':            /* it's a text-format PPM file */
  1447. X    cinfo->methods->get_input_row = get_text_rgb_row;
  1448. X    cinfo->input_components = 3;
  1449. X    cinfo->in_color_space = CS_RGB;
  1450. X    break;
  1451. X
  1452. X  case '5':            /* it's a raw-format PGM file */
  1453. X    if (maxval == MAXJSAMPLE)
  1454. X      cinfo->methods->get_input_row = get_raw_gray_row;
  1455. X    else
  1456. X      cinfo->methods->get_input_row = get_scaled_gray_row;
  1457. X    cinfo->input_components = 1;
  1458. X    cinfo->in_color_space = CS_GRAYSCALE;
  1459. X    break;
  1460. X
  1461. X  case '6':            /* it's a raw-format PPM file */
  1462. X    if (maxval == MAXJSAMPLE)
  1463. X      cinfo->methods->get_input_row = get_raw_rgb_row;
  1464. X    else
  1465. X      cinfo->methods->get_input_row = get_scaled_rgb_row;
  1466. X    cinfo->input_components = 3;
  1467. X    cinfo->in_color_space = CS_RGB;
  1468. X    break;
  1469. X
  1470. X  default:
  1471. X    ERREXIT(cinfo->emethods, "Not a PPM file");
  1472. X    break;
  1473. X  }
  1474. X
  1475. X  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
  1476. X    ERREXIT(cinfo->emethods, "Not a PPM file");
  1477. X
  1478. X  /* Compute the rescaling array if necessary */
  1479. X  /* This saves per-pixel calculation */
  1480. X  if (maxval == MAXJSAMPLE)
  1481. X    rescale = NULL;        /* no rescaling required */
  1482. X  else {
  1483. X    INT32 val, half_maxval;
  1484. X
  1485. X    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
  1486. X    rescale = (JSAMPLE *) (*cinfo->emethods->alloc_small)
  1487. X            ((size_t) (((long) maxval + 1L) * SIZEOF(JSAMPLE)));
  1488. X    half_maxval = maxval / 2;
  1489. X    for (val = 0; val <= (INT32) maxval; val++) {
  1490. X      /* The multiplication here must be done in 32 bits to avoid overflow */
  1491. X      rescale[val] = (JSAMPLE) ((val * MAXJSAMPLE + half_maxval) / maxval);
  1492. X    }
  1493. X  }
  1494. X
  1495. X  cinfo->image_width = w;
  1496. X  cinfo->image_height = h;
  1497. X  cinfo->data_precision = BITS_IN_JSAMPLE;
  1498. X}
  1499. X
  1500. X
  1501. X/*
  1502. X * Finish up at the end of the file.
  1503. X */
  1504. X
  1505. XMETHODDEF void
  1506. Xinput_term (compress_info_ptr cinfo)
  1507. X{
  1508. X  /* no work (we let free_all release the workspace) */
  1509. X}
  1510. X
  1511. X
  1512. X/*
  1513. X * The method selection routine for PPM format input.
  1514. X * Note that this must be called by the user interface before calling
  1515. X * jpeg_compress.  If multiple input formats are supported, the
  1516. X * user interface is responsible for discovering the file format and
  1517. X * calling the appropriate method selection routine.
  1518. X */
  1519. X
  1520. XGLOBAL void
  1521. Xjselrppm (compress_info_ptr cinfo)
  1522. X{
  1523. X  cinfo->methods->input_init = input_init;
  1524. X  /* cinfo->methods->get_input_row is set by input_init */
  1525. X  cinfo->methods->input_term = input_term;
  1526. X}
  1527. X
  1528. X#endif /* PPM_SUPPORTED */
  1529. END_OF_FILE
  1530.   if test 8944 -ne `wc -c <'jrdppm.c'`; then
  1531.     echo shar: \"'jrdppm.c'\" unpacked with wrong size!
  1532.   fi
  1533.   # end of 'jrdppm.c'
  1534. fi
  1535. echo shar: End of archive 2 \(of 18\).
  1536. cp /dev/null ark2isdone
  1537. MISSING=""
  1538. for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ; do
  1539.     if test ! -f ark${I}isdone ; then
  1540.     MISSING="${MISSING} ${I}"
  1541.     fi
  1542. done
  1543. if test "${MISSING}" = "" ; then
  1544.     echo You have unpacked all 18 archives.
  1545.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  1546. else
  1547.     echo You still must unpack the following archives:
  1548.     echo "        " ${MISSING}
  1549. fi
  1550. exit 0
  1551. exit 0 # Just in case...
  1552.