home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume34 / jpeg / part15 < prev    next >
Encoding:
Text File  |  1992-12-16  |  58.3 KB  |  2,054 lines

  1. Newsgroups: comp.sources.misc
  2. From: jpeg-info@uunet.uu.net (Independent JPEG Group)
  3. Subject:  v34i069:  jpeg - JPEG image compression, Part15/18
  4. Message-ID: <1992Dec17.165052.6804@sparky.imd.sterling.com>
  5. X-Md4-Signature: 12f254513e9764b5ba0ef59b4fa98b03
  6. Date: Thu, 17 Dec 1992 16:50:52 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: jpeg-info@uunet.uu.net (Independent JPEG Group)
  10. Posting-number: Volume 34, Issue 69
  11. Archive-name: jpeg/part15
  12. Environment: UNIX, VMS, MS-DOS, Mac, Amiga, Atari, Cray
  13. Supersedes: jpeg: Volume 29, Issue 1-18
  14.  
  15. #! /bin/sh
  16. # This is a shell archive.  Remove anything before this line, then feed it
  17. # into a shell via "sh file" or similar.  To overwrite existing files,
  18. # type "sh file -c".
  19. # Contents:  jccolor.c jdcolor.c jmemdosa.asm jwrppm.c jwrrle.c
  20. #   jwrtarga.c
  21. # Wrapped by kent@sparky on Wed Dec 16 20:52:30 1992
  22. PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/lbin ; export PATH
  23. echo If this archive is complete, you will see the following message:
  24. echo '          "shar: End of archive 15 (of 18)."'
  25. if test -f 'jccolor.c' -a "${1}" != "-c" ; then 
  26.   echo shar: Will not clobber existing file \"'jccolor.c'\"
  27. else
  28.   echo shar: Extracting \"'jccolor.c'\" \(11236 characters\)
  29.   sed "s/^X//" >'jccolor.c' <<'END_OF_FILE'
  30. X/*
  31. X * jccolor.c
  32. X *
  33. X * Copyright (C) 1991, 1992, Thomas G. Lane.
  34. X * This file is part of the Independent JPEG Group's software.
  35. X * For conditions of distribution and use, see the accompanying README file.
  36. X *
  37. X * This file contains input colorspace conversion routines.
  38. X * These routines are invoked via the methods get_sample_rows
  39. X * and colorin_init/term.
  40. X */
  41. X
  42. X#include "jinclude.h"
  43. X
  44. X
  45. Xstatic JSAMPARRAY pixel_row;    /* Workspace for a pixel row in input format */
  46. X
  47. X
  48. X/**************** RGB -> YCbCr conversion: most common case **************/
  49. X
  50. X/*
  51. X * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
  52. X * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
  53. X * The conversion equations to be implemented are therefore
  54. X *    Y  =  0.29900 * R + 0.58700 * G + 0.11400 * B
  55. X *    Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B  + MAXJSAMPLE/2
  56. X *    Cr =  0.50000 * R - 0.41869 * G - 0.08131 * B  + MAXJSAMPLE/2
  57. X * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
  58. X *
  59. X * To avoid floating-point arithmetic, we represent the fractional constants
  60. X * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
  61. X * the products by 2^16, with appropriate rounding, to get the correct answer.
  62. X *
  63. X * For even more speed, we avoid doing any multiplications in the inner loop
  64. X * by precalculating the constants times R,G,B for all possible values.
  65. X * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
  66. X * for 12-bit samples it is still acceptable.  It's not very reasonable for
  67. X * 16-bit samples, but if you want lossless storage you shouldn't be changing
  68. X * colorspace anyway.
  69. X * The MAXJSAMPLE/2 offsets and the rounding fudge-factor of 0.5 are included
  70. X * in the tables to save adding them separately in the inner loop.
  71. X */
  72. X
  73. X#ifdef SIXTEEN_BIT_SAMPLES
  74. X#define SCALEBITS    14    /* avoid overflow */
  75. X#else
  76. X#define SCALEBITS    16    /* speedier right-shift on some machines */
  77. X#endif
  78. X#define ONE_HALF    ((INT32) 1 << (SCALEBITS-1))
  79. X#define FIX(x)        ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
  80. X
  81. X/* We allocate one big table and divide it up into eight parts, instead of
  82. X * doing eight alloc_small requests.  This lets us use a single table base
  83. X * address, which can be held in a register in the inner loops on many
  84. X * machines (more than can hold all eight addresses, anyway).
  85. X */
  86. X
  87. Xstatic INT32 * rgb_ycc_tab;    /* => table for RGB to YCbCr conversion */
  88. X#define R_Y_OFF        0            /* offset to R => Y section */
  89. X#define G_Y_OFF        (1*(MAXJSAMPLE+1))    /* offset to G => Y section */
  90. X#define B_Y_OFF        (2*(MAXJSAMPLE+1))    /* etc. */
  91. X#define R_CB_OFF    (3*(MAXJSAMPLE+1))
  92. X#define G_CB_OFF    (4*(MAXJSAMPLE+1))
  93. X#define B_CB_OFF    (5*(MAXJSAMPLE+1))
  94. X#define R_CR_OFF    B_CB_OFF        /* B=>Cb, R=>Cr are the same */
  95. X#define G_CR_OFF    (6*(MAXJSAMPLE+1))
  96. X#define B_CR_OFF    (7*(MAXJSAMPLE+1))
  97. X#define TABLE_SIZE    (8*(MAXJSAMPLE+1))
  98. X
  99. X
  100. X/*
  101. X * Initialize for colorspace conversion.
  102. X */
  103. X
  104. XMETHODDEF void
  105. Xrgb_ycc_init (compress_info_ptr cinfo)
  106. X{
  107. X  INT32 i;
  108. X
  109. X  /* Allocate a workspace for the result of get_input_row. */
  110. X  pixel_row = (*cinfo->emethods->alloc_small_sarray)
  111. X        (cinfo->image_width, (long) cinfo->input_components);
  112. X
  113. X  /* Allocate and fill in the conversion tables. */
  114. X  rgb_ycc_tab = (INT32 *) (*cinfo->emethods->alloc_small)
  115. X                (TABLE_SIZE * SIZEOF(INT32));
  116. X
  117. X  for (i = 0; i <= MAXJSAMPLE; i++) {
  118. X    rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
  119. X    rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;
  120. X    rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i     + ONE_HALF;
  121. X    rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
  122. X    rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
  123. X    rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i    + ONE_HALF*(MAXJSAMPLE+1);
  124. X/*  B=>Cb and R=>Cr tables are the same
  125. X    rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i    + ONE_HALF*(MAXJSAMPLE+1);
  126. X*/
  127. X    rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
  128. X    rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
  129. X  }
  130. X}
  131. X
  132. X
  133. X/*
  134. X * Fetch some rows of pixels from get_input_row and convert to the
  135. X * JPEG colorspace.
  136. X */
  137. X
  138. XMETHODDEF void
  139. Xget_rgb_ycc_rows (compress_info_ptr cinfo,
  140. X          int rows_to_read, JSAMPIMAGE image_data)
  141. X{
  142. X#ifdef SIXTEEN_BIT_SAMPLES
  143. X  register UINT16 r, g, b;
  144. X#else
  145. X  register int r, g, b;
  146. X#endif
  147. X  register INT32 * ctab = rgb_ycc_tab;
  148. X  register JSAMPROW inptr0, inptr1, inptr2;
  149. X  register JSAMPROW outptr0, outptr1, outptr2;
  150. X  register long col;
  151. X  long width = cinfo->image_width;
  152. X  int row;
  153. X
  154. X  for (row = 0; row < rows_to_read; row++) {
  155. X    /* Read one row from the source file */
  156. X    (*cinfo->methods->get_input_row) (cinfo, pixel_row);
  157. X    /* Convert colorspace */
  158. X    inptr0 = pixel_row[0];
  159. X    inptr1 = pixel_row[1];
  160. X    inptr2 = pixel_row[2];
  161. X    outptr0 = image_data[0][row];
  162. X    outptr1 = image_data[1][row];
  163. X    outptr2 = image_data[2][row];
  164. X    for (col = 0; col < width; col++) {
  165. X      r = GETJSAMPLE(inptr0[col]);
  166. X      g = GETJSAMPLE(inptr1[col]);
  167. X      b = GETJSAMPLE(inptr2[col]);
  168. X      /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  169. X       * must be too; we do not need an explicit range-limiting operation.
  170. X       * Hence the value being shifted is never negative, and we don't
  171. X       * need the general RIGHT_SHIFT macro.
  172. X       */
  173. X      /* Y */
  174. X      outptr0[col] = (JSAMPLE)
  175. X        ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  176. X         >> SCALEBITS);
  177. X      /* Cb */
  178. X      outptr1[col] = (JSAMPLE)
  179. X        ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  180. X         >> SCALEBITS);
  181. X      /* Cr */
  182. X      outptr2[col] = (JSAMPLE)
  183. X        ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  184. X         >> SCALEBITS);
  185. X    }
  186. X  }
  187. X}
  188. X
  189. X
  190. X/**************** Cases other than RGB -> YCbCr **************/
  191. X
  192. X
  193. X/*
  194. X * Fetch some rows of pixels from get_input_row and convert to the
  195. X * JPEG colorspace.
  196. X * This version handles RGB->grayscale conversion, which is the same
  197. X * as the RGB->Y portion of RGB->YCbCr.
  198. X * We assume rgb_ycc_init has been called (we only use the Y tables).
  199. X */
  200. X
  201. XMETHODDEF void
  202. Xget_rgb_gray_rows (compress_info_ptr cinfo,
  203. X           int rows_to_read, JSAMPIMAGE image_data)
  204. X{
  205. X#ifdef SIXTEEN_BIT_SAMPLES
  206. X  register UINT16 r, g, b;
  207. X#else
  208. X  register int r, g, b;
  209. X#endif
  210. X  register INT32 * ctab = rgb_ycc_tab;
  211. X  register JSAMPROW inptr0, inptr1, inptr2;
  212. X  register JSAMPROW outptr;
  213. X  register long col;
  214. X  long width = cinfo->image_width;
  215. X  int row;
  216. X
  217. X  for (row = 0; row < rows_to_read; row++) {
  218. X    /* Read one row from the source file */
  219. X    (*cinfo->methods->get_input_row) (cinfo, pixel_row);
  220. X    /* Convert colorspace */
  221. X    inptr0 = pixel_row[0];
  222. X    inptr1 = pixel_row[1];
  223. X    inptr2 = pixel_row[2];
  224. X    outptr = image_data[0][row];
  225. X    for (col = 0; col < width; col++) {
  226. X      r = GETJSAMPLE(inptr0[col]);
  227. X      g = GETJSAMPLE(inptr1[col]);
  228. X      b = GETJSAMPLE(inptr2[col]);
  229. X      /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  230. X       * must be too; we do not need an explicit range-limiting operation.
  231. X       * Hence the value being shifted is never negative, and we don't
  232. X       * need the general RIGHT_SHIFT macro.
  233. X       */
  234. X      /* Y */
  235. X      outptr[col] = (JSAMPLE)
  236. X        ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  237. X         >> SCALEBITS);
  238. X    }
  239. X  }
  240. X}
  241. X
  242. X
  243. X/*
  244. X * Initialize for colorspace conversion.
  245. X */
  246. X
  247. XMETHODDEF void
  248. Xcolorin_init (compress_info_ptr cinfo)
  249. X{
  250. X  /* Allocate a workspace for the result of get_input_row. */
  251. X  pixel_row = (*cinfo->emethods->alloc_small_sarray)
  252. X        (cinfo->image_width, (long) cinfo->input_components);
  253. X}
  254. X
  255. X
  256. X/*
  257. X * Fetch some rows of pixels from get_input_row and convert to the
  258. X * JPEG colorspace.
  259. X * This version handles grayscale output with no conversion.
  260. X * The source can be either plain grayscale or YCbCr (since Y == gray).
  261. X */
  262. X
  263. XMETHODDEF void
  264. Xget_grayscale_rows (compress_info_ptr cinfo,
  265. X            int rows_to_read, JSAMPIMAGE image_data)
  266. X{
  267. X  int row;
  268. X
  269. X  for (row = 0; row < rows_to_read; row++) {
  270. X    /* Read one row from the source file */
  271. X    (*cinfo->methods->get_input_row) (cinfo, pixel_row);
  272. X    /* Convert colorspace (gamma mapping needed here) */
  273. X    jcopy_sample_rows(pixel_row, 0, image_data[0], row,
  274. X              1, cinfo->image_width);
  275. X  }
  276. X}
  277. X
  278. X
  279. X/*
  280. X * Fetch some rows of pixels from get_input_row and convert to the
  281. X * JPEG colorspace.
  282. X * This version handles multi-component colorspaces without conversion.
  283. X */
  284. X
  285. XMETHODDEF void
  286. Xget_noconvert_rows (compress_info_ptr cinfo,
  287. X            int rows_to_read, JSAMPIMAGE image_data)
  288. X{
  289. X  int row, ci;
  290. X
  291. X  for (row = 0; row < rows_to_read; row++) {
  292. X    /* Read one row from the source file */
  293. X    (*cinfo->methods->get_input_row) (cinfo, pixel_row);
  294. X    /* Convert colorspace (gamma mapping needed here) */
  295. X    for (ci = 0; ci < cinfo->input_components; ci++) {
  296. X      jcopy_sample_rows(pixel_row, ci, image_data[ci], row,
  297. X            1, cinfo->image_width);
  298. X    }
  299. X  }
  300. X}
  301. X
  302. X
  303. X/*
  304. X * Finish up at the end of the file.
  305. X */
  306. X
  307. XMETHODDEF void
  308. Xcolorin_term (compress_info_ptr cinfo)
  309. X{
  310. X  /* no work (we let free_all release the workspace) */
  311. X}
  312. X
  313. X
  314. X/*
  315. X * The method selection routine for input colorspace conversion.
  316. X */
  317. X
  318. XGLOBAL void
  319. Xjselccolor (compress_info_ptr cinfo)
  320. X{
  321. X  /* Make sure input_components agrees with in_color_space */
  322. X  switch (cinfo->in_color_space) {
  323. X  case CS_GRAYSCALE:
  324. X    if (cinfo->input_components != 1)
  325. X      ERREXIT(cinfo->emethods, "Bogus input colorspace");
  326. X    break;
  327. X
  328. X  case CS_RGB:
  329. X  case CS_YCbCr:
  330. X  case CS_YIQ:
  331. X    if (cinfo->input_components != 3)
  332. X      ERREXIT(cinfo->emethods, "Bogus input colorspace");
  333. X    break;
  334. X
  335. X  case CS_CMYK:
  336. X    if (cinfo->input_components != 4)
  337. X      ERREXIT(cinfo->emethods, "Bogus input colorspace");
  338. X    break;
  339. X
  340. X  default:
  341. X    ERREXIT(cinfo->emethods, "Unsupported input colorspace");
  342. X    break;
  343. X  }
  344. X
  345. X  /* Standard init/term methods (may override below) */
  346. X  cinfo->methods->colorin_init = colorin_init;
  347. X  cinfo->methods->colorin_term = colorin_term;
  348. X
  349. X  /* Check num_components, set conversion method based on requested space */
  350. X  switch (cinfo->jpeg_color_space) {
  351. X  case CS_GRAYSCALE:
  352. X    if (cinfo->num_components != 1)
  353. X      ERREXIT(cinfo->emethods, "Bogus JPEG colorspace");
  354. X    if (cinfo->in_color_space == CS_GRAYSCALE)
  355. X      cinfo->methods->get_sample_rows = get_grayscale_rows;
  356. X    else if (cinfo->in_color_space == CS_RGB) {
  357. X      cinfo->methods->colorin_init = rgb_ycc_init;
  358. X      cinfo->methods->get_sample_rows = get_rgb_gray_rows;
  359. X    } else if (cinfo->in_color_space == CS_YCbCr)
  360. X      cinfo->methods->get_sample_rows = get_grayscale_rows;
  361. X    else
  362. X      ERREXIT(cinfo->emethods, "Unsupported color conversion request");
  363. X    break;
  364. X
  365. X  case CS_YCbCr:
  366. X    if (cinfo->num_components != 3)
  367. X      ERREXIT(cinfo->emethods, "Bogus JPEG colorspace");
  368. X    if (cinfo->in_color_space == CS_RGB) {
  369. X      cinfo->methods->colorin_init = rgb_ycc_init;
  370. X      cinfo->methods->get_sample_rows = get_rgb_ycc_rows;
  371. X    } else if (cinfo->in_color_space == CS_YCbCr)
  372. X      cinfo->methods->get_sample_rows = get_noconvert_rows;
  373. X    else
  374. X      ERREXIT(cinfo->emethods, "Unsupported color conversion request");
  375. X    break;
  376. X
  377. X  case CS_CMYK:
  378. X    if (cinfo->num_components != 4)
  379. X      ERREXIT(cinfo->emethods, "Bogus JPEG colorspace");
  380. X    if (cinfo->in_color_space == CS_CMYK)
  381. X      cinfo->methods->get_sample_rows = get_noconvert_rows;
  382. X    else
  383. X      ERREXIT(cinfo->emethods, "Unsupported color conversion request");
  384. X    break;
  385. X
  386. X  default:
  387. X    ERREXIT(cinfo->emethods, "Unsupported JPEG colorspace");
  388. X    break;
  389. X  }
  390. X}
  391. END_OF_FILE
  392.   if test 11236 -ne `wc -c <'jccolor.c'`; then
  393.     echo shar: \"'jccolor.c'\" unpacked with wrong size!
  394.   fi
  395.   # end of 'jccolor.c'
  396. fi
  397. if test -f 'jdcolor.c' -a "${1}" != "-c" ; then 
  398.   echo shar: Will not clobber existing file \"'jdcolor.c'\"
  399. else
  400.   echo shar: Extracting \"'jdcolor.c'\" \(9090 characters\)
  401.   sed "s/^X//" >'jdcolor.c' <<'END_OF_FILE'
  402. X/*
  403. X * jdcolor.c
  404. X *
  405. X * Copyright (C) 1991, 1992, Thomas G. Lane.
  406. X * This file is part of the Independent JPEG Group's software.
  407. X * For conditions of distribution and use, see the accompanying README file.
  408. X *
  409. X * This file contains output colorspace conversion routines.
  410. X * These routines are invoked via the methods color_convert
  411. X * and colorout_init/term.
  412. X */
  413. X
  414. X#include "jinclude.h"
  415. X
  416. X
  417. X/**************** YCbCr -> RGB conversion: most common case **************/
  418. X
  419. X/*
  420. X * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
  421. X * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
  422. X * The conversion equations to be implemented are therefore
  423. X *    R = Y                + 1.40200 * Cr
  424. X *    G = Y - 0.34414 * Cb - 0.71414 * Cr
  425. X *    B = Y + 1.77200 * Cb
  426. X * where Cb and Cr represent the incoming values less MAXJSAMPLE/2.
  427. X * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
  428. X *
  429. X * To avoid floating-point arithmetic, we represent the fractional constants
  430. X * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
  431. X * the products by 2^16, with appropriate rounding, to get the correct answer.
  432. X * Notice that Y, being an integral input, does not contribute any fraction
  433. X * so it need not participate in the rounding.
  434. X *
  435. X * For even more speed, we avoid doing any multiplications in the inner loop
  436. X * by precalculating the constants times Cb and Cr for all possible values.
  437. X * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
  438. X * for 12-bit samples it is still acceptable.  It's not very reasonable for
  439. X * 16-bit samples, but if you want lossless storage you shouldn't be changing
  440. X * colorspace anyway.
  441. X * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
  442. X * values for the G calculation are left scaled up, since we must add them
  443. X * together before rounding.
  444. X */
  445. X
  446. X#ifdef SIXTEEN_BIT_SAMPLES
  447. X#define SCALEBITS    14    /* avoid overflow */
  448. X#else
  449. X#define SCALEBITS    16    /* speedier right-shift on some machines */
  450. X#endif
  451. X#define ONE_HALF    ((INT32) 1 << (SCALEBITS-1))
  452. X#define FIX(x)        ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
  453. X
  454. Xstatic int * Cr_r_tab;        /* => table for Cr to R conversion */
  455. Xstatic int * Cb_b_tab;        /* => table for Cb to B conversion */
  456. Xstatic INT32 * Cr_g_tab;    /* => table for Cr to G conversion */
  457. Xstatic INT32 * Cb_g_tab;    /* => table for Cb to G conversion */
  458. X
  459. X
  460. X/*
  461. X * Initialize for colorspace conversion.
  462. X */
  463. X
  464. XMETHODDEF void
  465. Xycc_rgb_init (decompress_info_ptr cinfo)
  466. X{
  467. X  INT32 i, x2;
  468. X  SHIFT_TEMPS
  469. X
  470. X  Cr_r_tab = (int *) (*cinfo->emethods->alloc_small)
  471. X                ((MAXJSAMPLE+1) * SIZEOF(int));
  472. X  Cb_b_tab = (int *) (*cinfo->emethods->alloc_small)
  473. X                ((MAXJSAMPLE+1) * SIZEOF(int));
  474. X  Cr_g_tab = (INT32 *) (*cinfo->emethods->alloc_small)
  475. X                ((MAXJSAMPLE+1) * SIZEOF(INT32));
  476. X  Cb_g_tab = (INT32 *) (*cinfo->emethods->alloc_small)
  477. X                ((MAXJSAMPLE+1) * SIZEOF(INT32));
  478. X
  479. X  for (i = 0; i <= MAXJSAMPLE; i++) {
  480. X    /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
  481. X    /* The Cb or Cr value we are thinking of is x = i - MAXJSAMPLE/2 */
  482. X    x2 = 2*i - MAXJSAMPLE;    /* twice x */
  483. X    /* Cr=>R value is nearest int to 1.40200 * x */
  484. X    Cr_r_tab[i] = (int)
  485. X            RIGHT_SHIFT(FIX(1.40200/2) * x2 + ONE_HALF, SCALEBITS);
  486. X    /* Cb=>B value is nearest int to 1.77200 * x */
  487. X    Cb_b_tab[i] = (int)
  488. X            RIGHT_SHIFT(FIX(1.77200/2) * x2 + ONE_HALF, SCALEBITS);
  489. X    /* Cr=>G value is scaled-up -0.71414 * x */
  490. X    Cr_g_tab[i] = (- FIX(0.71414/2)) * x2;
  491. X    /* Cb=>G value is scaled-up -0.34414 * x */
  492. X    /* We also add in ONE_HALF so that need not do it in inner loop */
  493. X    Cb_g_tab[i] = (- FIX(0.34414/2)) * x2 + ONE_HALF;
  494. X  }
  495. X}
  496. X
  497. X
  498. X/*
  499. X * Convert some rows of samples to the output colorspace.
  500. X */
  501. X
  502. XMETHODDEF void
  503. Xycc_rgb_convert (decompress_info_ptr cinfo, int num_rows, long num_cols,
  504. X         JSAMPIMAGE input_data, JSAMPIMAGE output_data)
  505. X{
  506. X#ifdef SIXTEEN_BIT_SAMPLES
  507. X  register INT32 y;
  508. X  register UINT16 cb, cr;
  509. X#else
  510. X  register int y, cb, cr;
  511. X#endif
  512. X  register JSAMPROW inptr0, inptr1, inptr2;
  513. X  register JSAMPROW outptr0, outptr1, outptr2;
  514. X  register long col;
  515. X  /* copy these pointers into registers if possible */
  516. X  register JSAMPLE * range_limit = cinfo->sample_range_limit;
  517. X  register int * Crrtab = Cr_r_tab;
  518. X  register int * Cbbtab = Cb_b_tab;
  519. X  register INT32 * Crgtab = Cr_g_tab;
  520. X  register INT32 * Cbgtab = Cb_g_tab;
  521. X  int row;
  522. X  SHIFT_TEMPS
  523. X  
  524. X  for (row = 0; row < num_rows; row++) {
  525. X    inptr0 = input_data[0][row];
  526. X    inptr1 = input_data[1][row];
  527. X    inptr2 = input_data[2][row];
  528. X    outptr0 = output_data[0][row];
  529. X    outptr1 = output_data[1][row];
  530. X    outptr2 = output_data[2][row];
  531. X    for (col = 0; col < num_cols; col++) {
  532. X      y  = GETJSAMPLE(inptr0[col]);
  533. X      cb = GETJSAMPLE(inptr1[col]);
  534. X      cr = GETJSAMPLE(inptr2[col]);
  535. X      /* Note: if the inputs were computed directly from RGB values,
  536. X       * range-limiting would be unnecessary here; but due to possible
  537. X       * noise in the DCT/IDCT phase, we do need to apply range limits.
  538. X       */
  539. X      outptr0[col] = range_limit[y + Crrtab[cr]];    /* red */
  540. X      outptr1[col] = range_limit[y +            /* green */
  541. X                 ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
  542. X                            SCALEBITS))];
  543. X      outptr2[col] = range_limit[y + Cbbtab[cb]];    /* blue */
  544. X    }
  545. X  }
  546. X}
  547. X
  548. X
  549. X/*
  550. X * Finish up at the end of the file.
  551. X */
  552. X
  553. XMETHODDEF void
  554. Xycc_rgb_term (decompress_info_ptr cinfo)
  555. X{
  556. X  /* no work (we let free_all release the workspace) */
  557. X}
  558. X
  559. X
  560. X/**************** Cases other than YCbCr -> RGB **************/
  561. X
  562. X
  563. X/*
  564. X * Initialize for colorspace conversion.
  565. X */
  566. X
  567. XMETHODDEF void
  568. Xnull_init (decompress_info_ptr cinfo)
  569. X/* colorout_init for cases where no setup is needed */
  570. X{
  571. X  /* no work needed */
  572. X}
  573. X
  574. X
  575. X/*
  576. X * Color conversion for no colorspace change: just copy the data.
  577. X */
  578. X
  579. XMETHODDEF void
  580. Xnull_convert (decompress_info_ptr cinfo, int num_rows, long num_cols,
  581. X          JSAMPIMAGE input_data, JSAMPIMAGE output_data)
  582. X{
  583. X  short ci;
  584. X
  585. X  for (ci = 0; ci < cinfo->num_components; ci++) {
  586. X    jcopy_sample_rows(input_data[ci], 0, output_data[ci], 0,
  587. X              num_rows, num_cols);
  588. X  }
  589. X}
  590. X
  591. X
  592. X/*
  593. X * Color conversion for grayscale: just copy the data.
  594. X * This also works for YCbCr/YIQ -> grayscale conversion, in which
  595. X * we just copy the Y (luminance) component and ignore chrominance.
  596. X */
  597. X
  598. XMETHODDEF void
  599. Xgrayscale_convert (decompress_info_ptr cinfo, int num_rows, long num_cols,
  600. X           JSAMPIMAGE input_data, JSAMPIMAGE output_data)
  601. X{
  602. X  jcopy_sample_rows(input_data[0], 0, output_data[0], 0,
  603. X            num_rows, num_cols);
  604. X}
  605. X
  606. X
  607. X/*
  608. X * Finish up at the end of the file.
  609. X */
  610. X
  611. XMETHODDEF void
  612. Xnull_term (decompress_info_ptr cinfo)
  613. X/* colorout_term for cases where no teardown is needed */
  614. X{
  615. X  /* no work needed */
  616. X}
  617. X
  618. X
  619. X
  620. X/*
  621. X * The method selection routine for output colorspace conversion.
  622. X */
  623. X
  624. XGLOBAL void
  625. Xjseldcolor (decompress_info_ptr cinfo)
  626. X{
  627. X  /* Make sure num_components agrees with jpeg_color_space */
  628. X  switch (cinfo->jpeg_color_space) {
  629. X  case CS_GRAYSCALE:
  630. X    if (cinfo->num_components != 1)
  631. X      ERREXIT(cinfo->emethods, "Bogus JPEG colorspace");
  632. X    break;
  633. X
  634. X  case CS_RGB:
  635. X  case CS_YCbCr:
  636. X  case CS_YIQ:
  637. X    if (cinfo->num_components != 3)
  638. X      ERREXIT(cinfo->emethods, "Bogus JPEG colorspace");
  639. X    break;
  640. X
  641. X  case CS_CMYK:
  642. X    if (cinfo->num_components != 4)
  643. X      ERREXIT(cinfo->emethods, "Bogus JPEG colorspace");
  644. X    break;
  645. X
  646. X  default:
  647. X    ERREXIT(cinfo->emethods, "Unsupported JPEG colorspace");
  648. X    break;
  649. X  }
  650. X
  651. X  /* Set color_out_comps and conversion method based on requested space */
  652. X  switch (cinfo->out_color_space) {
  653. X  case CS_GRAYSCALE:
  654. X    cinfo->color_out_comps = 1;
  655. X    if (cinfo->jpeg_color_space == CS_GRAYSCALE ||
  656. X    cinfo->jpeg_color_space == CS_YCbCr ||
  657. X    cinfo->jpeg_color_space == CS_YIQ) {
  658. X      cinfo->methods->color_convert = grayscale_convert;
  659. X      cinfo->methods->colorout_init = null_init;
  660. X      cinfo->methods->colorout_term = null_term;
  661. X    } else
  662. X      ERREXIT(cinfo->emethods, "Unsupported color conversion request");
  663. X    break;
  664. X
  665. X  case CS_RGB:
  666. X    cinfo->color_out_comps = 3;
  667. X    if (cinfo->jpeg_color_space == CS_YCbCr) {
  668. X      cinfo->methods->color_convert = ycc_rgb_convert;
  669. X      cinfo->methods->colorout_init = ycc_rgb_init;
  670. X      cinfo->methods->colorout_term = ycc_rgb_term;
  671. X    } else if (cinfo->jpeg_color_space == CS_RGB) {
  672. X      cinfo->methods->color_convert = null_convert;
  673. X      cinfo->methods->colorout_init = null_init;
  674. X      cinfo->methods->colorout_term = null_term;
  675. X    } else
  676. X      ERREXIT(cinfo->emethods, "Unsupported color conversion request");
  677. X    break;
  678. X
  679. X  default:
  680. X    /* Permit null conversion from CMYK or YCbCr to same output space */
  681. X    if (cinfo->out_color_space == cinfo->jpeg_color_space) {
  682. X      cinfo->color_out_comps = cinfo->num_components;
  683. X      cinfo->methods->color_convert = null_convert;
  684. X      cinfo->methods->colorout_init = null_init;
  685. X      cinfo->methods->colorout_term = null_term;
  686. X    } else            /* unsupported non-null conversion */
  687. X      ERREXIT(cinfo->emethods, "Unsupported color conversion request");
  688. X    break;
  689. X  }
  690. X
  691. X  if (cinfo->quantize_colors)
  692. X    cinfo->final_out_comps = 1;    /* single colormapped output component */
  693. X  else
  694. X    cinfo->final_out_comps = cinfo->color_out_comps;
  695. X}
  696. END_OF_FILE
  697.   if test 9090 -ne `wc -c <'jdcolor.c'`; then
  698.     echo shar: \"'jdcolor.c'\" unpacked with wrong size!
  699.   fi
  700.   # end of 'jdcolor.c'
  701. fi
  702. if test -f 'jmemdosa.asm' -a "${1}" != "-c" ; then 
  703.   echo shar: Will not clobber existing file \"'jmemdosa.asm'\"
  704. else
  705.   echo shar: Extracting \"'jmemdosa.asm'\" \(8314 characters\)
  706.   sed "s/^X//" >'jmemdosa.asm' <<'END_OF_FILE'
  707. X;
  708. X; jmemdosa.asm
  709. X;
  710. X; Copyright (C) 1992, Thomas G. Lane.
  711. X; This file is part of the Independent JPEG Group's software.
  712. X; For conditions of distribution and use, see the accompanying README file.
  713. X;
  714. X; This file contains low-level interface routines to support the MS-DOS
  715. X; backing store manager (jmemdos.c).  Routines are provided to access disk
  716. X; files through direct DOS calls, and to access XMS and EMS drivers.
  717. X;
  718. X; This file should assemble with Microsoft's MASM or any compatible
  719. X; assembler (including Borland's Turbo Assembler).  If you haven't got
  720. X; a compatible assembler, better fall back to jmemansi.c or jmemname.c.
  721. X;
  722. X; To minimize dependence on the C compiler's register usage conventions,
  723. X; we save and restore all 8086 registers, even though most compilers only
  724. X; require SI,DI,DS to be preserved.  Also, we use only 16-bit-wide return
  725. X; values, which everybody returns in AX.
  726. X;
  727. X; Based on code contributed by Ge' Weijers.
  728. X;
  729. X
  730. XJMEMDOSA_TXT    segment byte public 'CODE'
  731. X
  732. X        assume    cs:JMEMDOSA_TXT
  733. X
  734. X        public    _jdos_open
  735. X        public    _jdos_close
  736. X        public    _jdos_seek
  737. X        public    _jdos_read
  738. X        public    _jdos_write
  739. X        public    _jxms_getdriver
  740. X        public    _jxms_calldriver
  741. X        public    _jems_available
  742. X        public    _jems_calldriver
  743. X
  744. X;
  745. X; short far jdos_open (short far * handle, char far * filename)
  746. X;
  747. X; Create and open a temporary file
  748. X;
  749. X_jdos_open    proc    far
  750. X        push    bp            ; linkage
  751. X        mov     bp,sp
  752. X        push    si            ; save all registers for safety
  753. X        push    di
  754. X        push    bx
  755. X        push    cx
  756. X        push    dx
  757. X        push    es
  758. X        push    ds
  759. X        mov    cx,0            ; normal file attributes
  760. X        lds    dx,dword ptr [bp+10]    ; get filename pointer
  761. X        mov    ah,3ch            ; create file
  762. X        int    21h
  763. X        jc    open_err        ; if failed, return error code
  764. X        lds    bx,dword ptr [bp+6]    ; get handle pointer
  765. X        mov    word ptr [bx],ax    ; save the handle
  766. X        xor    ax,ax            ; return zero for OK
  767. Xopen_err:    pop    ds            ; restore registers and exit
  768. X        pop    es
  769. X        pop    dx
  770. X        pop    cx
  771. X        pop    bx
  772. X        pop    di
  773. X        pop    si
  774. X        pop     bp
  775. X        ret
  776. X_jdos_open    endp
  777. X
  778. X
  779. X;
  780. X; short far jdos_close (short handle)
  781. X;
  782. X; Close the file handle
  783. X;
  784. X_jdos_close    proc    far
  785. X        push    bp            ; linkage
  786. X        mov     bp,sp
  787. X        push    si            ; save all registers for safety
  788. X        push    di
  789. X        push    bx
  790. X        push    cx
  791. X        push    dx
  792. X        push    es
  793. X        push    ds
  794. X        mov    bx,word ptr [bp+6]    ; file handle
  795. X        mov    ah,3eh            ; close file
  796. X        int    21h
  797. X        jc    close_err        ; if failed, return error code
  798. X        xor    ax,ax            ; return zero for OK
  799. Xclose_err:    pop    ds            ; restore registers and exit
  800. X        pop    es
  801. X        pop    dx
  802. X        pop    cx
  803. X        pop    bx
  804. X        pop    di
  805. X        pop    si
  806. X        pop     bp
  807. X        ret
  808. X_jdos_close    endp
  809. X
  810. X
  811. X;
  812. X; short far jdos_seek (short handle, long offset)
  813. X;
  814. X; Set file position
  815. X;
  816. X_jdos_seek    proc    far
  817. X        push    bp            ; linkage
  818. X        mov     bp,sp
  819. X        push    si            ; save all registers for safety
  820. X        push    di
  821. X        push    bx
  822. X        push    cx
  823. X        push    dx
  824. X        push    es
  825. X        push    ds
  826. X        mov    bx,word ptr [bp+6]    ; file handle
  827. X        mov    dx,word ptr [bp+8]    ; LS offset
  828. X        mov    cx,word ptr [bp+10]    ; MS offset
  829. X        mov    ax,4200h        ; absolute seek
  830. X        int    21h
  831. X        jc    seek_err        ; if failed, return error code
  832. X        xor    ax,ax            ; return zero for OK
  833. Xseek_err:    pop    ds            ; restore registers and exit
  834. X        pop    es
  835. X        pop    dx
  836. X        pop    cx
  837. X        pop    bx
  838. X        pop    di
  839. X        pop    si
  840. X        pop     bp
  841. X        ret
  842. X_jdos_seek    endp
  843. X
  844. X
  845. X;
  846. X; short far jdos_read (short handle, void far * buffer, unsigned short count)
  847. X;
  848. X; Read from file
  849. X;
  850. X_jdos_read    proc    far
  851. X        push    bp            ; linkage
  852. X        mov     bp,sp
  853. X        push    si            ; save all registers for safety
  854. X        push    di
  855. X        push    bx
  856. X        push    cx
  857. X        push    dx
  858. X        push    es
  859. X        push    ds
  860. X        mov    bx,word ptr [bp+6]    ; file handle
  861. X        lds    dx,dword ptr [bp+8]    ; buffer address
  862. X        mov    cx,word ptr [bp+12]    ; number of bytes
  863. X        mov    ah,3fh            ; read file
  864. X        int    21h
  865. X        jc    read_err        ; if failed, return error code
  866. X        cmp    ax,word ptr [bp+12]    ; make sure all bytes were read
  867. X        je    read_ok
  868. X        mov    ax,1            ; else return 1 for not OK
  869. X        jmp    short read_err
  870. Xread_ok:    xor    ax,ax            ; return zero for OK
  871. Xread_err:    pop    ds            ; restore registers and exit
  872. X        pop    es
  873. X        pop    dx
  874. X        pop    cx
  875. X        pop    bx
  876. X        pop    di
  877. X        pop    si
  878. X        pop     bp
  879. X        ret
  880. X_jdos_read    endp
  881. X
  882. X
  883. X;
  884. X; short far jdos_write (short handle, void far * buffer, unsigned short count)
  885. X;
  886. X; Write to file
  887. X;
  888. X_jdos_write    proc    far
  889. X        push    bp            ; linkage
  890. X        mov     bp,sp
  891. X        push    si            ; save all registers for safety
  892. X        push    di
  893. X        push    bx
  894. X        push    cx
  895. X        push    dx
  896. X        push    es
  897. X        push    ds
  898. X        mov    bx,word ptr [bp+6]    ; file handle
  899. X        lds    dx,dword ptr [bp+8]    ; buffer address
  900. X        mov    cx,word ptr [bp+12]    ; number of bytes
  901. X        mov    ah,40h            ; write file
  902. X        int    21h
  903. X        jc    write_err        ; if failed, return error code
  904. X        cmp    ax,word ptr [bp+12]    ; make sure all bytes written
  905. X        je    write_ok
  906. X        mov    ax,1            ; else return 1 for not OK
  907. X        jmp    short write_err
  908. Xwrite_ok:    xor    ax,ax            ; return zero for OK
  909. Xwrite_err:    pop    ds            ; restore registers and exit
  910. X        pop    es
  911. X        pop    dx
  912. X        pop    cx
  913. X        pop    bx
  914. X        pop    di
  915. X        pop    si
  916. X        pop     bp
  917. X        ret
  918. X_jdos_write    endp
  919. X
  920. X
  921. X;
  922. X; void far jxms_getdriver (XMSDRIVER far *)
  923. X;
  924. X; Get the address of the XMS driver, or NULL if not available
  925. X;
  926. X_jxms_getdriver    proc    far
  927. X        push    bp            ; linkage
  928. X        mov     bp,sp
  929. X        push    si            ; save all registers for safety
  930. X        push    di
  931. X        push    bx
  932. X        push    cx
  933. X        push    dx
  934. X        push    es
  935. X        push    ds
  936. X        mov     ax,4300h        ; call multiplex interrupt with
  937. X        int    2fh            ; a magic cookie, hex 4300
  938. X        cmp     al,80h            ; AL should contain hex 80
  939. X        je    xmsavail
  940. X        xor     dx,dx            ; no XMS driver available
  941. X        xor     ax,ax            ; return a nil pointer
  942. X        jmp    short xmsavail_done
  943. Xxmsavail:    mov     ax,4310h        ; fetch driver address with
  944. X        int    2fh            ; another magic cookie
  945. X        mov     dx,es            ; copy address to dx:ax
  946. X        mov     ax,bx
  947. Xxmsavail_done:    les     bx,dword ptr [bp+6]    ; get pointer to return value
  948. X        mov    word ptr es:[bx],ax
  949. X        mov    word ptr es:[bx+2],dx
  950. X        pop    ds            ; restore registers and exit
  951. X        pop    es
  952. X        pop    dx
  953. X        pop    cx
  954. X        pop    bx
  955. X        pop    di
  956. X        pop    si
  957. X        pop    bp
  958. X        ret
  959. X_jxms_getdriver    endp
  960. X
  961. X
  962. X;
  963. X; void far jxms_calldriver (XMSDRIVER, XMScontext far *)
  964. X;
  965. X; The XMScontext structure contains values for the AX,DX,BX,SI,DS registers.
  966. X; These are loaded, the XMS call is performed, and the new values of the
  967. X; AX,DX,BX registers are written back to the context structure.
  968. X;
  969. X_jxms_calldriver     proc    far
  970. X        push    bp            ; linkage
  971. X        mov     bp,sp
  972. X        push    si            ; save all registers for safety
  973. X        push    di
  974. X        push    bx
  975. X        push    cx
  976. X        push    dx
  977. X        push    es
  978. X        push    ds
  979. X        les     bx,dword ptr [bp+10]    ; get XMScontext pointer
  980. X        mov     ax,word ptr es:[bx]    ; load registers
  981. X        mov     dx,word ptr es:[bx+2]
  982. X        mov     si,word ptr es:[bx+6]
  983. X        mov     ds,word ptr es:[bx+8]
  984. X        mov     bx,word ptr es:[bx+4]
  985. X        call    dword ptr [bp+6]    ; call the driver
  986. X        mov    cx,bx            ; save returned BX for a sec
  987. X        les     bx,dword ptr [bp+10]    ; get XMScontext pointer
  988. X        mov     word ptr es:[bx],ax    ; put back ax,dx,bx
  989. X        mov     word ptr es:[bx+2],dx
  990. X        mov     word ptr es:[bx+4],cx
  991. X        pop    ds            ; restore registers and exit
  992. X        pop    es
  993. X        pop    dx
  994. X        pop    cx
  995. X        pop    bx
  996. X        pop    di
  997. X        pop    si
  998. X        pop     bp
  999. X        ret
  1000. X_jxms_calldriver     endp
  1001. X
  1002. X
  1003. X;
  1004. X; short far jems_available (void)
  1005. X;
  1006. X; Have we got an EMS driver? (this comes straight from the EMS 4.0 specs)
  1007. X;
  1008. X_jems_available    proc    far
  1009. X        push    si            ; save all registers for safety
  1010. X        push    di
  1011. X        push    bx
  1012. X        push    cx
  1013. X        push    dx
  1014. X        push    es
  1015. X        push    ds
  1016. X        mov    ax,3567h        ; get interrupt vector 67h
  1017. X        int    21h
  1018. X        push    cs
  1019. X        pop    ds
  1020. X        mov    di,000ah        ; check offs 10 in returned seg
  1021. X        lea    si,ASCII_device_name    ; against literal string
  1022. X        mov    cx,8
  1023. X        cld
  1024. X        repe cmpsb
  1025. X        jne    no_ems
  1026. X        mov    ax,1            ; match, it's there
  1027. X        jmp    short avail_done
  1028. Xno_ems:        xor    ax,ax            ; it's not there
  1029. Xavail_done:    pop    ds            ; restore registers and exit
  1030. X        pop    es
  1031. X        pop    dx
  1032. X        pop    cx
  1033. X        pop    bx
  1034. X        pop    di
  1035. X        pop    si
  1036. X        ret
  1037. X
  1038. XASCII_device_name    db    "EMMXXXX0"
  1039. X
  1040. X_jems_available    endp
  1041. X
  1042. X
  1043. X;
  1044. X; void far jems_calldriver (EMScontext far *)
  1045. X;
  1046. X; The EMScontext structure contains values for the AX,DX,BX,SI,DS registers.
  1047. X; These are loaded, the EMS trap is performed, and the new values of the
  1048. X; AX,DX,BX registers are written back to the context structure.
  1049. X;
  1050. X_jems_calldriver    proc far
  1051. X        push    bp            ; linkage
  1052. X        mov     bp,sp
  1053. X        push    si            ; save all registers for safety
  1054. X        push    di
  1055. X        push    bx
  1056. X        push    cx
  1057. X        push    dx
  1058. X        push    es
  1059. X        push    ds
  1060. X        les     bx,dword ptr [bp+6]    ; get EMScontext pointer
  1061. X        mov     ax,word ptr es:[bx]    ; load registers
  1062. X        mov     dx,word ptr es:[bx+2]
  1063. X        mov     si,word ptr es:[bx+6]
  1064. X        mov     ds,word ptr es:[bx+8]
  1065. X        mov     bx,word ptr es:[bx+4]
  1066. X        int    67h            ; call the EMS driver
  1067. X        mov    cx,bx            ; save returned BX for a sec
  1068. X        les     bx,dword ptr [bp+6]    ; get EMScontext pointer
  1069. X        mov     word ptr es:[bx],ax    ; put back ax,dx,bx
  1070. X        mov     word ptr es:[bx+2],dx
  1071. X        mov     word ptr es:[bx+4],cx
  1072. X        pop    ds            ; restore registers and exit
  1073. X        pop    es
  1074. X        pop    dx
  1075. X        pop    cx
  1076. X        pop    bx
  1077. X        pop    di
  1078. X        pop    si
  1079. X        pop     bp
  1080. X        ret
  1081. X_jems_calldriver    endp
  1082. X
  1083. XJMEMDOSA_TXT    ends
  1084. X
  1085. X        end
  1086. END_OF_FILE
  1087.   if test 8314 -ne `wc -c <'jmemdosa.asm'`; then
  1088.     echo shar: \"'jmemdosa.asm'\" unpacked with wrong size!
  1089.   fi
  1090.   # end of 'jmemdosa.asm'
  1091. fi
  1092. if test -f 'jwrppm.c' -a "${1}" != "-c" ; then 
  1093.   echo shar: Will not clobber existing file \"'jwrppm.c'\"
  1094. else
  1095.   echo shar: Extracting \"'jwrppm.c'\" \(9091 characters\)
  1096.   sed "s/^X//" >'jwrppm.c' <<'END_OF_FILE'
  1097. X/*
  1098. X * jwrppm.c
  1099. X *
  1100. X * Copyright (C) 1991, 1992, Thomas G. Lane.
  1101. X * This file is part of the Independent JPEG Group's software.
  1102. X * For conditions of distribution and use, see the accompanying README file.
  1103. X *
  1104. X * This file contains routines to write output images in PPM/PGM format.
  1105. X *
  1106. X * These routines may need modification for non-Unix environments or
  1107. X * specialized applications.  As they stand, they assume output to
  1108. X * an ordinary stdio stream.
  1109. X *
  1110. X * These routines are invoked via the methods put_pixel_rows, put_color_map,
  1111. X * and output_init/term.
  1112. X */
  1113. X
  1114. X#include "jinclude.h"
  1115. X
  1116. X#ifdef PPM_SUPPORTED
  1117. X
  1118. X
  1119. X/*
  1120. X * Haven't yet got around to making this work with text-format output,
  1121. X * hence cannot handle pixels wider than 8 bits.
  1122. X */
  1123. X
  1124. X#ifndef EIGHT_BIT_SAMPLES
  1125. X  Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
  1126. X#endif
  1127. X
  1128. X
  1129. X/*
  1130. X * On most systems, writing individual bytes with putc() is drastically less
  1131. X * efficient than buffering a row at a time for fwrite().  But we must
  1132. X * allocate the row buffer in near data space on PCs, because we are assuming
  1133. X * small-data memory model, wherein fwrite() can't reach far memory.  If you
  1134. X * need to process very wide images on a PC, you may have to use the putc()
  1135. X * approach.  Also, there are still a few systems around wherein fwrite() is
  1136. X * actually implemented as a putc() loop, in which case this buffer is a waste
  1137. X * of space.  So the putc() method can be used by defining USE_PUTC_OUTPUT.
  1138. X */
  1139. X
  1140. X#ifndef USE_PUTC_OUTPUT
  1141. Xstatic char * row_buffer;    /* holds 1 pixel row's worth of output */
  1142. X#endif
  1143. X
  1144. X
  1145. X/*
  1146. X * Write the file header.
  1147. X */
  1148. X
  1149. XMETHODDEF void
  1150. Xoutput_init (decompress_info_ptr cinfo)
  1151. X{
  1152. X  if (cinfo->out_color_space == CS_GRAYSCALE) {
  1153. X    /* emit header for raw PGM format */
  1154. X    fprintf(cinfo->output_file, "P5\n%ld %ld\n%d\n",
  1155. X        cinfo->image_width, cinfo->image_height, 255);
  1156. X#ifndef USE_PUTC_OUTPUT
  1157. X    /* allocate space for row buffer: 1 byte/pixel */
  1158. X    row_buffer = (char *) (*cinfo->emethods->alloc_small)
  1159. X            ((size_t) (SIZEOF(char) * cinfo->image_width));
  1160. X#endif
  1161. X  } else if (cinfo->out_color_space == CS_RGB) {
  1162. X    /* emit header for raw PPM format */
  1163. X    fprintf(cinfo->output_file, "P6\n%ld %ld\n%d\n",
  1164. X        cinfo->image_width, cinfo->image_height, 255);
  1165. X#ifndef USE_PUTC_OUTPUT
  1166. X    /* allocate space for row buffer: 3 bytes/pixel */
  1167. X    row_buffer = (char *) (*cinfo->emethods->alloc_small)
  1168. X            ((size_t) (3 * SIZEOF(char) * cinfo->image_width));
  1169. X#endif
  1170. X  } else {
  1171. X    ERREXIT(cinfo->emethods, "PPM output must be grayscale or RGB");
  1172. X  }
  1173. X}
  1174. X
  1175. X
  1176. X/*
  1177. X * Write some pixel data.
  1178. X */
  1179. X
  1180. X#ifdef USE_PUTC_OUTPUT
  1181. X
  1182. XMETHODDEF void
  1183. Xput_pixel_rows (decompress_info_ptr cinfo, int num_rows,
  1184. X        JSAMPIMAGE pixel_data)
  1185. X{
  1186. X  register FILE * outfile = cinfo->output_file;
  1187. X  register JSAMPROW ptr0, ptr1, ptr2;
  1188. X  register long col;
  1189. X  long width = cinfo->image_width;
  1190. X  int row;
  1191. X  
  1192. X  for (row = 0; row < num_rows; row++) {
  1193. X    ptr0 = pixel_data[0][row];
  1194. X    ptr1 = pixel_data[1][row];
  1195. X    ptr2 = pixel_data[2][row];
  1196. X    for (col = width; col > 0; col--) {
  1197. X      putc(GETJSAMPLE(*ptr0), outfile);
  1198. X      ptr0++;
  1199. X      putc(GETJSAMPLE(*ptr1), outfile);
  1200. X      ptr1++;
  1201. X      putc(GETJSAMPLE(*ptr2), outfile);
  1202. X      ptr2++;
  1203. X    }
  1204. X  }
  1205. X}
  1206. X
  1207. XMETHODDEF void
  1208. Xput_gray_rows (decompress_info_ptr cinfo, int num_rows,
  1209. X           JSAMPIMAGE pixel_data)
  1210. X{
  1211. X  register FILE * outfile = cinfo->output_file;
  1212. X  register JSAMPROW ptr0;
  1213. X  register long col;
  1214. X  long width = cinfo->image_width;
  1215. X  int row;
  1216. X  
  1217. X  for (row = 0; row < num_rows; row++) {
  1218. X    ptr0 = pixel_data[0][row];
  1219. X    for (col = width; col > 0; col--) {
  1220. X      putc(GETJSAMPLE(*ptr0), outfile);
  1221. X      ptr0++;
  1222. X    }
  1223. X  }
  1224. X}
  1225. X
  1226. X#else /* use row buffering */
  1227. X
  1228. XMETHODDEF void
  1229. Xput_pixel_rows (decompress_info_ptr cinfo, int num_rows,
  1230. X        JSAMPIMAGE pixel_data)
  1231. X{
  1232. X  FILE * outfile = cinfo->output_file;
  1233. X  register JSAMPROW ptr0, ptr1, ptr2;
  1234. X  register char * row_bufferptr;
  1235. X  register long col;
  1236. X  long width = cinfo->image_width;
  1237. X  int row;
  1238. X  
  1239. X  for (row = 0; row < num_rows; row++) {
  1240. X    ptr0 = pixel_data[0][row];
  1241. X    ptr1 = pixel_data[1][row];
  1242. X    ptr2 = pixel_data[2][row];
  1243. X    row_bufferptr = row_buffer;
  1244. X    for (col = width; col > 0; col--) {
  1245. X      *row_bufferptr++ = (char) GETJSAMPLE(*ptr0++);
  1246. X      *row_bufferptr++ = (char) GETJSAMPLE(*ptr1++);
  1247. X      *row_bufferptr++ = (char) GETJSAMPLE(*ptr2++);
  1248. X    }
  1249. X    (void) JFWRITE(outfile, row_buffer, 3*width);
  1250. X  }
  1251. X}
  1252. X
  1253. XMETHODDEF void
  1254. Xput_gray_rows (decompress_info_ptr cinfo, int num_rows,
  1255. X           JSAMPIMAGE pixel_data)
  1256. X{
  1257. X  FILE * outfile = cinfo->output_file;
  1258. X  register JSAMPROW ptr0;
  1259. X  register char * row_bufferptr;
  1260. X  register long col;
  1261. X  long width = cinfo->image_width;
  1262. X  int row;
  1263. X  
  1264. X  for (row = 0; row < num_rows; row++) {
  1265. X    ptr0 = pixel_data[0][row];
  1266. X    row_bufferptr = row_buffer;
  1267. X    for (col = width; col > 0; col--) {
  1268. X      *row_bufferptr++ = (char) GETJSAMPLE(*ptr0++);
  1269. X    }
  1270. X    (void) JFWRITE(outfile, row_buffer, width);
  1271. X  }
  1272. X}
  1273. X
  1274. X#endif /* USE_PUTC_OUTPUT */
  1275. X
  1276. X
  1277. X/*
  1278. X * Write some pixel data when color quantization is in effect.
  1279. X */
  1280. X
  1281. X#ifdef USE_PUTC_OUTPUT
  1282. X
  1283. XMETHODDEF void
  1284. Xput_demapped_rgb (decompress_info_ptr cinfo, int num_rows,
  1285. X          JSAMPIMAGE pixel_data)
  1286. X{
  1287. X  register FILE * outfile = cinfo->output_file;
  1288. X  register JSAMPROW ptr;
  1289. X  register JSAMPROW color_map0 = cinfo->colormap[0];
  1290. X  register JSAMPROW color_map1 = cinfo->colormap[1];
  1291. X  register JSAMPROW color_map2 = cinfo->colormap[2];
  1292. X  register int pixval;
  1293. X  register long col;
  1294. X  long width = cinfo->image_width;
  1295. X  int row;
  1296. X  
  1297. X  for (row = 0; row < num_rows; row++) {
  1298. X    ptr = pixel_data[0][row];
  1299. X    for (col = width; col > 0; col--) {
  1300. X      pixval = GETJSAMPLE(*ptr++);
  1301. X      putc(GETJSAMPLE(color_map0[pixval]), outfile);
  1302. X      putc(GETJSAMPLE(color_map1[pixval]), outfile);
  1303. X      putc(GETJSAMPLE(color_map2[pixval]), outfile);
  1304. X    }
  1305. X  }
  1306. X}
  1307. X
  1308. XMETHODDEF void
  1309. Xput_demapped_gray (decompress_info_ptr cinfo, int num_rows,
  1310. X           JSAMPIMAGE pixel_data)
  1311. X{
  1312. X  register FILE * outfile = cinfo->output_file;
  1313. X  register JSAMPROW ptr;
  1314. X  register JSAMPROW color_map0 = cinfo->colormap[0];
  1315. X  register int pixval;
  1316. X  register long col;
  1317. X  long width = cinfo->image_width;
  1318. X  int row;
  1319. X  
  1320. X  for (row = 0; row < num_rows; row++) {
  1321. X    ptr = pixel_data[0][row];
  1322. X    for (col = width; col > 0; col--) {
  1323. X      pixval = GETJSAMPLE(*ptr++);
  1324. X      putc(GETJSAMPLE(color_map0[pixval]), outfile);
  1325. X    }
  1326. X  }
  1327. X}
  1328. X
  1329. X#else /* use row buffering */
  1330. X
  1331. XMETHODDEF void
  1332. Xput_demapped_rgb (decompress_info_ptr cinfo, int num_rows,
  1333. X          JSAMPIMAGE pixel_data)
  1334. X{
  1335. X  FILE * outfile = cinfo->output_file;
  1336. X  register JSAMPROW ptr;
  1337. X  register char * row_bufferptr;
  1338. X  register JSAMPROW color_map0 = cinfo->colormap[0];
  1339. X  register JSAMPROW color_map1 = cinfo->colormap[1];
  1340. X  register JSAMPROW color_map2 = cinfo->colormap[2];
  1341. X  register int pixval;
  1342. X  register long col;
  1343. X  long width = cinfo->image_width;
  1344. X  int row;
  1345. X  
  1346. X  for (row = 0; row < num_rows; row++) {
  1347. X    ptr = pixel_data[0][row];
  1348. X    row_bufferptr = row_buffer;
  1349. X    for (col = width; col > 0; col--) {
  1350. X      pixval = GETJSAMPLE(*ptr++);
  1351. X      *row_bufferptr++ = (char) GETJSAMPLE(color_map0[pixval]);
  1352. X      *row_bufferptr++ = (char) GETJSAMPLE(color_map1[pixval]);
  1353. X      *row_bufferptr++ = (char) GETJSAMPLE(color_map2[pixval]);
  1354. X    }
  1355. X    (void) JFWRITE(outfile, row_buffer, 3*width);
  1356. X  }
  1357. X}
  1358. X
  1359. XMETHODDEF void
  1360. Xput_demapped_gray (decompress_info_ptr cinfo, int num_rows,
  1361. X           JSAMPIMAGE pixel_data)
  1362. X{
  1363. X  FILE * outfile = cinfo->output_file;
  1364. X  register JSAMPROW ptr;
  1365. X  register char * row_bufferptr;
  1366. X  register JSAMPROW color_map0 = cinfo->colormap[0];
  1367. X  register int pixval;
  1368. X  register long col;
  1369. X  long width = cinfo->image_width;
  1370. X  int row;
  1371. X  
  1372. X  for (row = 0; row < num_rows; row++) {
  1373. X    ptr = pixel_data[0][row];
  1374. X    row_bufferptr = row_buffer;
  1375. X    for (col = width; col > 0; col--) {
  1376. X      pixval = GETJSAMPLE(*ptr++);
  1377. X      *row_bufferptr++ = (char) GETJSAMPLE(color_map0[pixval]);
  1378. X    }
  1379. X    (void) JFWRITE(outfile, row_buffer, width);
  1380. X  }
  1381. X}
  1382. X
  1383. X#endif /* USE_PUTC_OUTPUT */
  1384. X
  1385. X
  1386. X/*
  1387. X * Write the color map.
  1388. X * For PPM output, we just remember to demap the output data!
  1389. X */
  1390. X
  1391. XMETHODDEF void
  1392. Xput_color_map (decompress_info_ptr cinfo, int num_colors, JSAMPARRAY colormap)
  1393. X{
  1394. X  if (cinfo->out_color_space == CS_RGB)
  1395. X    cinfo->methods->put_pixel_rows = put_demapped_rgb;
  1396. X  else
  1397. X    cinfo->methods->put_pixel_rows = put_demapped_gray;
  1398. X}
  1399. X
  1400. X
  1401. X/*
  1402. X * Finish up at the end of the file.
  1403. X */
  1404. X
  1405. XMETHODDEF void
  1406. Xoutput_term (decompress_info_ptr cinfo)
  1407. X{
  1408. X  /* No work except to make sure we wrote the output file OK; */
  1409. X  /* we let free_all release any workspace */
  1410. X  fflush(cinfo->output_file);
  1411. X  if (ferror(cinfo->output_file))
  1412. X    ERREXIT(cinfo->emethods, "Output file write error");
  1413. X}
  1414. X
  1415. X
  1416. X/*
  1417. X * The method selection routine for PPM format output.
  1418. X * This should be called from d_ui_method_selection if PPM output is wanted.
  1419. X */
  1420. X
  1421. XGLOBAL void
  1422. Xjselwppm (decompress_info_ptr cinfo)
  1423. X{
  1424. X  cinfo->methods->output_init = output_init;
  1425. X  cinfo->methods->put_color_map = put_color_map;
  1426. X  if (cinfo->out_color_space == CS_RGB)
  1427. X    cinfo->methods->put_pixel_rows = put_pixel_rows;
  1428. X  else
  1429. X    cinfo->methods->put_pixel_rows = put_gray_rows;
  1430. X  cinfo->methods->output_term = output_term;
  1431. X}
  1432. X
  1433. X#endif /* PPM_SUPPORTED */
  1434. END_OF_FILE
  1435.   if test 9091 -ne `wc -c <'jwrppm.c'`; then
  1436.     echo shar: \"'jwrppm.c'\" unpacked with wrong size!
  1437.   fi
  1438.   # end of 'jwrppm.c'
  1439. fi
  1440. if test -f 'jwrrle.c' -a "${1}" != "-c" ; then 
  1441.   echo shar: Will not clobber existing file \"'jwrrle.c'\"
  1442. else
  1443.   echo shar: Extracting \"'jwrrle.c'\" \(6740 characters\)
  1444.   sed "s/^X//" >'jwrrle.c' <<'END_OF_FILE'
  1445. X/*
  1446. X * jwrrle.c
  1447. X *
  1448. X * Copyright (C) 1991, 1992, Thomas G. Lane.
  1449. X * This file is part of the Independent JPEG Group's software.
  1450. X * For conditions of distribution and use, see the accompanying README file.
  1451. X *
  1452. X * This file contains routines to write output images in RLE format.
  1453. X * The Utah Raster Toolkit library is required (version 3.0).
  1454. X *
  1455. X * These routines may need modification for non-Unix environments or
  1456. X * specialized applications.  As they stand, they assume output to
  1457. X * an ordinary stdio stream.
  1458. X *
  1459. X * These routines are invoked via the methods put_pixel_rows, put_color_map,
  1460. X * and output_init/term.
  1461. X *
  1462. X * Based on code contributed by Mike Lijewski.
  1463. X */
  1464. X
  1465. X#include "jinclude.h"
  1466. X
  1467. X#ifdef RLE_SUPPORTED
  1468. X
  1469. X/* rle.h is provided by the Utah Raster Toolkit. */
  1470. X
  1471. X#include <rle.h>
  1472. X
  1473. X
  1474. X/*
  1475. X * output_term assumes that JSAMPLE has the same representation as rle_pixel,
  1476. X * to wit, "unsigned char".  Hence we can't cope with 12- or 16-bit samples.
  1477. X */
  1478. X
  1479. X#ifndef EIGHT_BIT_SAMPLES
  1480. X  Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
  1481. X#endif
  1482. X
  1483. X
  1484. X/*
  1485. X * Since RLE stores scanlines bottom-to-top, we have to invert the image
  1486. X * from JPEG's top-to-bottom order.  To do this, we save the outgoing data
  1487. X * in virtual array(s) during put_pixel_row calls, then actually emit the
  1488. X * RLE file during output_term.  We use one virtual array if the output is
  1489. X * grayscale or colormapped, more if it is full color.
  1490. X */
  1491. X
  1492. X#define MAX_CHANS    4    /* allow up to four color components */
  1493. Xstatic big_sarray_ptr channels[MAX_CHANS]; /* Virtual arrays for saved data */
  1494. X
  1495. Xstatic long cur_output_row;    /* next row# to write to virtual array(s) */
  1496. X
  1497. X
  1498. X/*
  1499. X * For now, if we emit an RLE color map then it is always 256 entries long,
  1500. X * though not all of the entries need be used.
  1501. X */
  1502. X
  1503. X#define CMAPBITS    8
  1504. X#define CMAPLENGTH    (1<<(CMAPBITS))
  1505. X
  1506. Xstatic rle_map *output_colormap; /* RLE-style color map, or NULL if none */
  1507. Xstatic int number_colors;    /* Number of colors actually used */
  1508. X
  1509. X
  1510. X/*
  1511. X * Write the file header.
  1512. X *
  1513. X * In this module it's easier to wait till output_term to actually write
  1514. X * anything; here we just request the big arrays we'll need.
  1515. X */
  1516. X
  1517. XMETHODDEF void
  1518. Xoutput_init (decompress_info_ptr cinfo)
  1519. X{
  1520. X  short ci;
  1521. X  
  1522. X  if (cinfo->final_out_comps > MAX_CHANS)
  1523. X    ERREXIT1(cinfo->emethods, "Cannot handle %d output channels for RLE",
  1524. X         cinfo->final_out_comps);
  1525. X  
  1526. X  for (ci = 0; ci < cinfo->final_out_comps; ci++) {
  1527. X    channels[ci] = (*cinfo->emethods->request_big_sarray)
  1528. X            (cinfo->image_width, cinfo->image_height, 1L);
  1529. X  }
  1530. X  
  1531. X  output_colormap = NULL;    /* No output colormap as yet */
  1532. X  number_colors = 0;
  1533. X  cur_output_row = 0;        /* Start filling virtual arrays at row 0 */
  1534. X
  1535. X  cinfo->total_passes++;    /* count file writing as separate pass */
  1536. X}
  1537. X
  1538. X
  1539. X/*
  1540. X * Write some pixel data.
  1541. X *
  1542. X * This routine just saves the data away in virtual arrays.
  1543. X */
  1544. X
  1545. XMETHODDEF void
  1546. Xput_pixel_rows (decompress_info_ptr cinfo, int num_rows,
  1547. X        JSAMPIMAGE pixel_data)
  1548. X{
  1549. X  JSAMPROW outputrow[1];    /* a pseudo JSAMPARRAY structure */
  1550. X  int row;
  1551. X  short ci;
  1552. X  
  1553. X  for (row = 0; row < num_rows; row++) {
  1554. X    for (ci = 0; ci < cinfo->final_out_comps; ci++) {
  1555. X      outputrow[0] = *((*cinfo->emethods->access_big_sarray)
  1556. X            (channels[ci], cur_output_row, TRUE));
  1557. X      jcopy_sample_rows(pixel_data[ci], row, outputrow, 0,
  1558. X            1, cinfo->image_width);
  1559. X    }
  1560. X    cur_output_row++;
  1561. X  }
  1562. X}
  1563. X
  1564. X
  1565. X/*
  1566. X * Write the color map.
  1567. X *
  1568. X *  For RLE output we just save the colormap for the output stage.
  1569. X */
  1570. X
  1571. XMETHODDEF void
  1572. Xput_color_map (decompress_info_ptr cinfo, int num_colors, JSAMPARRAY colormap)
  1573. X{
  1574. X  size_t cmapsize;
  1575. X  short ci;
  1576. X  int i;
  1577. X
  1578. X  if (num_colors > CMAPLENGTH)
  1579. X    ERREXIT1(cinfo->emethods, "Cannot handle %d colormap entries for RLE",
  1580. X         num_colors);
  1581. X
  1582. X  /* Allocate storage for RLE-style cmap, zero any extra entries */
  1583. X  cmapsize = cinfo->color_out_comps * CMAPLENGTH * SIZEOF(rle_map);
  1584. X  output_colormap = (rle_map *) (*cinfo->emethods->alloc_small) (cmapsize);
  1585. X  MEMZERO(output_colormap, cmapsize);
  1586. X
  1587. X  /* Save away data in RLE format --- note 8-bit left shift! */
  1588. X  /* Shifting would need adjustment for JSAMPLEs wider than 8 bits. */
  1589. X  for (ci = 0; ci < cinfo->color_out_comps; ci++) {
  1590. X    for (i = 0; i < num_colors; i++) {
  1591. X      output_colormap[ci * CMAPLENGTH + i] = GETJSAMPLE(colormap[ci][i]) << 8;
  1592. X    }
  1593. X  }
  1594. X  number_colors = num_colors;
  1595. X}
  1596. X
  1597. X
  1598. X/*
  1599. X * Finish up at the end of the file.
  1600. X *
  1601. X * Here is where we really output the RLE file.
  1602. X */
  1603. X
  1604. XMETHODDEF void
  1605. Xoutput_term (decompress_info_ptr cinfo)
  1606. X{
  1607. X  rle_hdr header;        /* Output file information */
  1608. X  rle_pixel *output_rows[MAX_CHANS];
  1609. X  char cmapcomment[80];
  1610. X  short ci;
  1611. X  long row;
  1612. X
  1613. X  /* Initialize the header info */
  1614. X  MEMZERO(&header, SIZEOF(rle_hdr)); /* make sure all bits are 0 */
  1615. X  header.rle_file = cinfo->output_file;
  1616. X  header.xmin     = 0;
  1617. X  header.xmax     = cinfo->image_width  - 1;
  1618. X  header.ymin     = 0;
  1619. X  header.ymax     = cinfo->image_height - 1;
  1620. X  header.alpha    = 0;
  1621. X  header.ncolors  = cinfo->final_out_comps;
  1622. X  for (ci = 0; ci < cinfo->final_out_comps; ci++) {
  1623. X    RLE_SET_BIT(header, ci);
  1624. X  }
  1625. X  if (number_colors > 0) {
  1626. X    header.ncmap   = cinfo->color_out_comps;
  1627. X    header.cmaplen = CMAPBITS;
  1628. X    header.cmap    = output_colormap;
  1629. X    /* Add a comment to the output image with the true colormap length. */
  1630. X    sprintf(cmapcomment, "color_map_length=%d", number_colors);
  1631. X    rle_putcom(cmapcomment, &header);
  1632. X  }
  1633. X  /* Emit the RLE header and color map (if any) */
  1634. X  rle_put_setup(&header);
  1635. X
  1636. X  /* Now output the RLE data from our virtual array(s).
  1637. X   * We assume here that (a) rle_pixel is represented the same as JSAMPLE,
  1638. X   * and (b) we are not on a machine where FAR pointers differ from regular.
  1639. X   */
  1640. X  for (row = cinfo->image_height-1; row >= 0; row--) {
  1641. X    (*cinfo->methods->progress_monitor) (cinfo, cinfo->image_height-row-1,
  1642. X                     cinfo->image_height);
  1643. X    for (ci = 0; ci < cinfo->final_out_comps; ci++) {
  1644. X      output_rows[ci] = (rle_pixel *) *((*cinfo->emethods->access_big_sarray)
  1645. X                    (channels[ci], row, FALSE));
  1646. X    }
  1647. X    rle_putrow(output_rows, (int) cinfo->image_width, &header);
  1648. X  }
  1649. X  cinfo->completed_passes++;
  1650. X
  1651. X  /* Emit file trailer */
  1652. X  rle_puteof(&header);
  1653. X  fflush(cinfo->output_file);
  1654. X  if (ferror(cinfo->output_file))
  1655. X    ERREXIT(cinfo->emethods, "Output file write error");
  1656. X
  1657. X  /* Release memory */
  1658. X  /* no work (we let free_all release the workspace) */
  1659. X}
  1660. X
  1661. X
  1662. X/*
  1663. X * The method selection routine for RLE format output.
  1664. X * This should be called from d_ui_method_selection if RLE output is wanted.
  1665. X */
  1666. X
  1667. XGLOBAL void
  1668. Xjselwrle (decompress_info_ptr cinfo)
  1669. X{
  1670. X  cinfo->methods->output_init    = output_init;
  1671. X  cinfo->methods->put_color_map  = put_color_map;
  1672. X  cinfo->methods->put_pixel_rows = put_pixel_rows;
  1673. X  cinfo->methods->output_term    = output_term;
  1674. X}
  1675. X
  1676. X#endif /* RLE_SUPPORTED */
  1677. END_OF_FILE
  1678.   if test 6740 -ne `wc -c <'jwrrle.c'`; then
  1679.     echo shar: \"'jwrrle.c'\" unpacked with wrong size!
  1680.   fi
  1681.   # end of 'jwrrle.c'
  1682. fi
  1683. if test -f 'jwrtarga.c' -a "${1}" != "-c" ; then 
  1684.   echo shar: Will not clobber existing file \"'jwrtarga.c'\"
  1685. else
  1686.   echo shar: Extracting \"'jwrtarga.c'\" \(9524 characters\)
  1687.   sed "s/^X//" >'jwrtarga.c' <<'END_OF_FILE'
  1688. X/*
  1689. X * jwrtarga.c
  1690. X *
  1691. X * Copyright (C) 1991, 1992, Thomas G. Lane.
  1692. X * This file is part of the Independent JPEG Group's software.
  1693. X * For conditions of distribution and use, see the accompanying README file.
  1694. X *
  1695. X * This file contains routines to write output images in Targa format.
  1696. X *
  1697. X * These routines may need modification for non-Unix environments or
  1698. X * specialized applications.  As they stand, they assume output to
  1699. X * an ordinary stdio stream.
  1700. X *
  1701. X * These routines are invoked via the methods put_pixel_rows, put_color_map,
  1702. X * and output_init/term.
  1703. X *
  1704. X * Based on code contributed by Lee Daniel Crocker.
  1705. X */
  1706. X
  1707. X#include "jinclude.h"
  1708. X
  1709. X#ifdef TARGA_SUPPORTED
  1710. X
  1711. X
  1712. X/*
  1713. X * To support 12-bit JPEG data, we'd have to scale output down to 8 bits.
  1714. X * This is not yet implemented.
  1715. X */
  1716. X
  1717. X#ifndef EIGHT_BIT_SAMPLES
  1718. X  Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
  1719. X#endif
  1720. X
  1721. X
  1722. X/*
  1723. X * On most systems, writing individual bytes with putc() is drastically less
  1724. X * efficient than buffering a row at a time for fwrite().  But we must
  1725. X * allocate the row buffer in near data space on PCs, because we are assuming
  1726. X * small-data memory model, wherein fwrite() can't reach far memory.  If you
  1727. X * need to process very wide images on a PC, you may have to use the putc()
  1728. X * approach.  Also, there are still a few systems around wherein fwrite() is
  1729. X * actually implemented as a putc() loop, in which case this buffer is a waste
  1730. X * of space.  So the putc() method can be used by defining USE_PUTC_OUTPUT.
  1731. X */
  1732. X
  1733. X#ifndef USE_PUTC_OUTPUT
  1734. Xstatic char * row_buffer;    /* holds 1 pixel row's worth of output */
  1735. X#endif
  1736. X
  1737. X
  1738. XLOCAL void
  1739. Xwrite_header (decompress_info_ptr cinfo, int num_colors)
  1740. X/* Create and write a Targa header */
  1741. X{
  1742. X  char targaheader[18];
  1743. X
  1744. X  /* Set unused fields of header to 0 */
  1745. X  MEMZERO(targaheader, SIZEOF(targaheader));
  1746. X
  1747. X  if (num_colors > 0) {
  1748. X    targaheader[1] = 1;        /* color map type 1 */
  1749. X    targaheader[5] = (char) (num_colors & 0xFF);
  1750. X    targaheader[6] = (char) (num_colors >> 8);
  1751. X    targaheader[7] = 24;    /* 24 bits per cmap entry */
  1752. X  }
  1753. X
  1754. X  targaheader[12] = (char) (cinfo->image_width & 0xFF);
  1755. X  targaheader[13] = (char) (cinfo->image_width >> 8);
  1756. X  targaheader[14] = (char) (cinfo->image_height & 0xFF);
  1757. X  targaheader[15] = (char) (cinfo->image_height >> 8);
  1758. X  targaheader[17] = 0x20;    /* Top-down, non-interlaced */
  1759. X
  1760. X  if (cinfo->out_color_space == CS_GRAYSCALE) {
  1761. X    targaheader[2] = 3;        /* image type = uncompressed gray-scale */
  1762. X    targaheader[16] = 8;    /* bits per pixel */
  1763. X  } else {            /* must be RGB */
  1764. X    if (num_colors > 0) {
  1765. X      targaheader[2] = 1;    /* image type = colormapped RGB */
  1766. X      targaheader[16] = 8;
  1767. X    } else {
  1768. X      targaheader[2] = 2;    /* image type = uncompressed RGB */
  1769. X      targaheader[16] = 24;
  1770. X    }
  1771. X  }
  1772. X
  1773. X  if (JFWRITE(cinfo->output_file, targaheader, 18) != (size_t) 18)
  1774. X    ERREXIT(cinfo->emethods, "Could not write Targa header");
  1775. X}
  1776. X
  1777. X
  1778. X/*
  1779. X * Write the file header.
  1780. X */
  1781. X
  1782. XMETHODDEF void
  1783. Xoutput_init (decompress_info_ptr cinfo)
  1784. X{
  1785. X  if (cinfo->out_color_space == CS_GRAYSCALE) {
  1786. X    /* Targa doesn't have a mapped grayscale format, so we will */
  1787. X    /* demap quantized gray output.  Never emit a colormap. */
  1788. X    write_header(cinfo, 0);
  1789. X#ifndef USE_PUTC_OUTPUT
  1790. X    /* allocate space for row buffer: 1 byte/pixel */
  1791. X    row_buffer = (char *) (*cinfo->emethods->alloc_small)
  1792. X            ((size_t) (SIZEOF(char) * cinfo->image_width));
  1793. X#endif
  1794. X  } else if (cinfo->out_color_space == CS_RGB) {
  1795. X    /* For quantized output, defer writing header until put_color_map time. */
  1796. X    if (! cinfo->quantize_colors)
  1797. X      write_header(cinfo, 0);
  1798. X#ifndef USE_PUTC_OUTPUT
  1799. X    /* allocate space for row buffer: 3 bytes/pixel */
  1800. X    row_buffer = (char *) (*cinfo->emethods->alloc_small)
  1801. X            ((size_t) (3 * SIZEOF(char) * cinfo->image_width));
  1802. X#endif
  1803. X  } else {
  1804. X    ERREXIT(cinfo->emethods, "Targa output must be grayscale or RGB");
  1805. X  }
  1806. X}
  1807. X
  1808. X
  1809. X/*
  1810. X * Write some pixel data.
  1811. X */
  1812. X
  1813. X#ifdef USE_PUTC_OUTPUT
  1814. X
  1815. XMETHODDEF void
  1816. Xput_pixel_rows (decompress_info_ptr cinfo, int num_rows,
  1817. X        JSAMPIMAGE pixel_data)
  1818. X/* used for unquantized full-color output */
  1819. X{
  1820. X  register FILE * outfile = cinfo->output_file;
  1821. X  register JSAMPROW ptr0, ptr1, ptr2;
  1822. X  register long col;
  1823. X  long width = cinfo->image_width;
  1824. X  int row;
  1825. X  
  1826. X  for (row = 0; row < num_rows; row++) {
  1827. X    ptr0 = pixel_data[0][row];
  1828. X    ptr1 = pixel_data[1][row];
  1829. X    ptr2 = pixel_data[2][row];
  1830. X    for (col = width; col > 0; col--) {
  1831. X      putc(GETJSAMPLE(*ptr2), outfile); /* write in BGR order */
  1832. X      ptr2++;
  1833. X      putc(GETJSAMPLE(*ptr1), outfile);
  1834. X      ptr1++;
  1835. X      putc(GETJSAMPLE(*ptr0), outfile);
  1836. X      ptr0++;
  1837. X    }
  1838. X  }
  1839. X}
  1840. X
  1841. XMETHODDEF void
  1842. Xput_gray_rows (decompress_info_ptr cinfo, int num_rows,
  1843. X           JSAMPIMAGE pixel_data)
  1844. X/* used for grayscale OR quantized color output */
  1845. X{
  1846. X  register FILE * outfile = cinfo->output_file;
  1847. X  register JSAMPROW ptr0;
  1848. X  register long col;
  1849. X  long width = cinfo->image_width;
  1850. X  int row;
  1851. X  
  1852. X  for (row = 0; row < num_rows; row++) {
  1853. X    ptr0 = pixel_data[0][row];
  1854. X    for (col = width; col > 0; col--) {
  1855. X      putc(GETJSAMPLE(*ptr0), outfile);
  1856. X      ptr0++;
  1857. X    }
  1858. X  }
  1859. X}
  1860. X
  1861. X#else /* use row buffering */
  1862. X
  1863. XMETHODDEF void
  1864. Xput_pixel_rows (decompress_info_ptr cinfo, int num_rows,
  1865. X        JSAMPIMAGE pixel_data)
  1866. X/* used for unquantized full-color output */
  1867. X{
  1868. X  FILE * outfile = cinfo->output_file;
  1869. X  register JSAMPROW ptr0, ptr1, ptr2;
  1870. X  register char * row_bufferptr;
  1871. X  register long col;
  1872. X  long width = cinfo->image_width;
  1873. X  int row;
  1874. X  
  1875. X  for (row = 0; row < num_rows; row++) {
  1876. X    ptr0 = pixel_data[0][row];
  1877. X    ptr1 = pixel_data[1][row];
  1878. X    ptr2 = pixel_data[2][row];
  1879. X    row_bufferptr = row_buffer;
  1880. X    for (col = width; col > 0; col--) {
  1881. X      *row_bufferptr++ = (char) GETJSAMPLE(*ptr2++); /* BGR order */
  1882. X      *row_bufferptr++ = (char) GETJSAMPLE(*ptr1++);
  1883. X      *row_bufferptr++ = (char) GETJSAMPLE(*ptr0++);
  1884. X    }
  1885. X    (void) JFWRITE(outfile, row_buffer, 3*width);
  1886. X  }
  1887. X}
  1888. X
  1889. XMETHODDEF void
  1890. Xput_gray_rows (decompress_info_ptr cinfo, int num_rows,
  1891. X           JSAMPIMAGE pixel_data)
  1892. X/* used for grayscale OR quantized color output */
  1893. X{
  1894. X  FILE * outfile = cinfo->output_file;
  1895. X  register JSAMPROW ptr0;
  1896. X  register char * row_bufferptr;
  1897. X  register long col;
  1898. X  long width = cinfo->image_width;
  1899. X  int row;
  1900. X  
  1901. X  for (row = 0; row < num_rows; row++) {
  1902. X    ptr0 = pixel_data[0][row];
  1903. X    row_bufferptr = row_buffer;
  1904. X    for (col = width; col > 0; col--) {
  1905. X      *row_bufferptr++ = (char) GETJSAMPLE(*ptr0++);
  1906. X    }
  1907. X    (void) JFWRITE(outfile, row_buffer, width);
  1908. X  }
  1909. X}
  1910. X
  1911. X#endif /* USE_PUTC_OUTPUT */
  1912. X
  1913. X
  1914. X/*
  1915. X * Write some demapped pixel data when color quantization is in effect.
  1916. X * For Targa, this is only applied to grayscale data.
  1917. X */
  1918. X
  1919. X#ifdef USE_PUTC_OUTPUT
  1920. X
  1921. XMETHODDEF void
  1922. Xput_demapped_gray (decompress_info_ptr cinfo, int num_rows,
  1923. X           JSAMPIMAGE pixel_data)
  1924. X{
  1925. X  register FILE * outfile = cinfo->output_file;
  1926. X  register JSAMPROW ptr;
  1927. X  register JSAMPROW color_map0 = cinfo->colormap[0];
  1928. X  register int pixval;
  1929. X  register long col;
  1930. X  long width = cinfo->image_width;
  1931. X  int row;
  1932. X  
  1933. X  for (row = 0; row < num_rows; row++) {
  1934. X    ptr = pixel_data[0][row];
  1935. X    for (col = width; col > 0; col--) {
  1936. X      pixval = GETJSAMPLE(*ptr++);
  1937. X      putc(GETJSAMPLE(color_map0[pixval]), outfile);
  1938. X    }
  1939. X  }
  1940. X}
  1941. X
  1942. X#else /* use row buffering */
  1943. X
  1944. XMETHODDEF void
  1945. Xput_demapped_gray (decompress_info_ptr cinfo, int num_rows,
  1946. X           JSAMPIMAGE pixel_data)
  1947. X{
  1948. X  FILE * outfile = cinfo->output_file;
  1949. X  register JSAMPROW ptr;
  1950. X  register char * row_bufferptr;
  1951. X  register JSAMPROW color_map0 = cinfo->colormap[0];
  1952. X  register int pixval;
  1953. X  register long col;
  1954. X  long width = cinfo->image_width;
  1955. X  int row;
  1956. X  
  1957. X  for (row = 0; row < num_rows; row++) {
  1958. X    ptr = pixel_data[0][row];
  1959. X    row_bufferptr = row_buffer;
  1960. X    for (col = width; col > 0; col--) {
  1961. X      pixval = GETJSAMPLE(*ptr++);
  1962. X      *row_bufferptr++ = (char) GETJSAMPLE(color_map0[pixval]);
  1963. X    }
  1964. X    (void) JFWRITE(outfile, row_buffer, width);
  1965. X  }
  1966. X}
  1967. X
  1968. X#endif /* USE_PUTC_OUTPUT */
  1969. X
  1970. X
  1971. X/*
  1972. X * Write the color map.
  1973. X */
  1974. X
  1975. XMETHODDEF void
  1976. Xput_color_map (decompress_info_ptr cinfo, int num_colors, JSAMPARRAY colormap)
  1977. X{
  1978. X  register FILE * outfile = cinfo->output_file;
  1979. X  int i;
  1980. X
  1981. X  if (cinfo->out_color_space == CS_RGB) {
  1982. X    /* We only support 8-bit colormap indexes, so only 256 colors */
  1983. X    if (num_colors > 256)
  1984. X      ERREXIT(cinfo->emethods, "Too many colors for Targa output");
  1985. X    /* Time to write the header */
  1986. X    write_header(cinfo, num_colors);
  1987. X    /* Write the colormap.  Note Targa uses BGR byte order */
  1988. X    for (i = 0; i < num_colors; i++) {
  1989. X      putc(GETJSAMPLE(colormap[2][i]), outfile);
  1990. X      putc(GETJSAMPLE(colormap[1][i]), outfile);
  1991. X      putc(GETJSAMPLE(colormap[0][i]), outfile);
  1992. X    }
  1993. X  } else {
  1994. X    cinfo->methods->put_pixel_rows = put_demapped_gray;
  1995. X  }
  1996. X}
  1997. X
  1998. X
  1999. X/*
  2000. X * Finish up at the end of the file.
  2001. X */
  2002. X
  2003. XMETHODDEF void
  2004. Xoutput_term (decompress_info_ptr cinfo)
  2005. X{
  2006. X  /* No work except to make sure we wrote the output file OK */
  2007. X  fflush(cinfo->output_file);
  2008. X  if (ferror(cinfo->output_file))
  2009. X    ERREXIT(cinfo->emethods, "Output file write error");
  2010. X}
  2011. X
  2012. X
  2013. X/*
  2014. X * The method selection routine for Targa format output.
  2015. X * This should be called from d_ui_method_selection if Targa output is wanted.
  2016. X */
  2017. X
  2018. XGLOBAL void
  2019. Xjselwtarga (decompress_info_ptr cinfo)
  2020. X{
  2021. X  cinfo->methods->output_init = output_init;
  2022. X  cinfo->methods->put_color_map = put_color_map;
  2023. X  if (cinfo->out_color_space == CS_GRAYSCALE || cinfo->quantize_colors)
  2024. X    cinfo->methods->put_pixel_rows = put_gray_rows;
  2025. X  else
  2026. X    cinfo->methods->put_pixel_rows = put_pixel_rows;
  2027. X  cinfo->methods->output_term = output_term;
  2028. X}
  2029. X
  2030. X#endif /* TARGA_SUPPORTED */
  2031. END_OF_FILE
  2032.   if test 9524 -ne `wc -c <'jwrtarga.c'`; then
  2033.     echo shar: \"'jwrtarga.c'\" unpacked with wrong size!
  2034.   fi
  2035.   # end of 'jwrtarga.c'
  2036. fi
  2037. echo shar: End of archive 15 \(of 18\).
  2038. cp /dev/null ark15isdone
  2039. MISSING=""
  2040. for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ; do
  2041.     if test ! -f ark${I}isdone ; then
  2042.     MISSING="${MISSING} ${I}"
  2043.     fi
  2044. done
  2045. if test "${MISSING}" = "" ; then
  2046.     echo You have unpacked all 18 archives.
  2047.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  2048. else
  2049.     echo You still must unpack the following archives:
  2050.     echo "        " ${MISSING}
  2051. fi
  2052. exit 0
  2053. exit 0 # Just in case...
  2054.