rdrle.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * rdrle.c
  3. *
  4. * Copyright (C) 1991-1996, Thomas G. Lane.
  5. * This file is part of the Independent JPEG Group's software.
  6. * For conditions of distribution and use, see the accompanying README file.
  7. *
  8. * This file contains routines to read input images in Utah RLE format.
  9. * The Utah Raster Toolkit library is required (version 3.1 or later).
  10. *
  11. * These routines may need modification for non-Unix environments or
  12. * specialized applications. As they stand, they assume input from
  13. * an ordinary stdio stream. They further assume that reading begins
  14. * at the start of the file; start_input may need work if the
  15. * user interface has already read some data (e.g., to determine that
  16. * the file is indeed RLE format).
  17. *
  18. * Based on code contributed by Mike Lijewski,
  19. * with updates from Robert Hutchinson.
  20. */
  21. #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
  22. #ifdef RLE_SUPPORTED
  23. /* rle.h is provided by the Utah Raster Toolkit. */
  24. #include <rle.h>
  25. /*
  26. * We assume that JSAMPLE has the same representation as rle_pixel,
  27. * to wit, "unsigned char". Hence we can't cope with 12- or 16-bit samples.
  28. */
  29. #if BITS_IN_JSAMPLE != 8
  30. Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
  31. #endif
  32. /*
  33. * We support the following types of RLE files:
  34. *
  35. * GRAYSCALE - 8 bits, no colormap
  36. * MAPPEDGRAY - 8 bits, 1 channel colomap
  37. * PSEUDOCOLOR - 8 bits, 3 channel colormap
  38. * TRUECOLOR - 24 bits, 3 channel colormap
  39. * DIRECTCOLOR - 24 bits, no colormap
  40. *
  41. * For now, we ignore any alpha channel in the image.
  42. */
  43. typedef enum
  44. { GRAYSCALE, MAPPEDGRAY, PSEUDOCOLOR, TRUECOLOR, DIRECTCOLOR } rle_kind;
  45. /*
  46. * Since RLE stores scanlines bottom-to-top, we have to invert the image
  47. * to conform to JPEG's top-to-bottom order. To do this, we read the
  48. * incoming image into a virtual array on the first get_pixel_rows call,
  49. * then fetch the required row from the virtual array on subsequent calls.
  50. */
  51. typedef struct _rle_source_struct * rle_source_ptr;
  52. typedef struct _rle_source_struct {
  53. struct cjpeg_source_struct pub; /* public fields */
  54. rle_kind visual; /* actual type of input file */
  55. jvirt_sarray_ptr image; /* virtual array to hold the image */
  56. JDIMENSION row; /* current row # in the virtual array */
  57. rle_hdr header; /* Input file information */
  58. rle_pixel** rle_row; /* holds a row returned by rle_getrow() */
  59. } rle_source_struct;
  60. /*
  61. * Read the file header; return image size and component count.
  62. */
  63. METHODDEF(void)
  64. start_input_rle (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  65. {
  66. rle_source_ptr source = (rle_source_ptr) sinfo;
  67. JDIMENSION width, height;
  68. #ifdef PROGRESS_REPORT
  69. cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
  70. #endif
  71. /* Use RLE library routine to get the header info */
  72. source->header = *rle_hdr_init(NULL);
  73. source->header.rle_file = source->pub.input_file;
  74. switch (rle_get_setup(&(source->header))) {
  75. case RLE_SUCCESS:
  76. /* A-OK */
  77. break;
  78. case RLE_NOT_RLE:
  79. ERREXIT(cinfo, JERR_RLE_NOT);
  80. break;
  81. case RLE_NO_SPACE:
  82. ERREXIT(cinfo, JERR_RLE_MEM);
  83. break;
  84. case RLE_EMPTY:
  85. ERREXIT(cinfo, JERR_RLE_EMPTY);
  86. break;
  87. case RLE_EOF:
  88. ERREXIT(cinfo, JERR_RLE_EOF);
  89. break;
  90. default:
  91. ERREXIT(cinfo, JERR_RLE_BADERROR);
  92. break;
  93. }
  94. /* Figure out what we have, set private vars and return values accordingly */
  95. width = source->header.xmax - source->header.xmin + 1;
  96. height = source->header.ymax - source->header.ymin + 1;
  97. source->header.xmin = 0; /* realign horizontally */
  98. source->header.xmax = width-1;
  99. cinfo->image_width = width;
  100. cinfo->image_height = height;
  101. cinfo->data_precision = 8; /* we can only handle 8 bit data */
  102. if (source->header.ncolors == 1 && source->header.ncmap == 0) {
  103. source->visual = GRAYSCALE;
  104. TRACEMS2(cinfo, 1, JTRC_RLE_GRAY, width, height);
  105. } else if (source->header.ncolors == 1 && source->header.ncmap == 1) {
  106. source->visual = MAPPEDGRAY;
  107. TRACEMS3(cinfo, 1, JTRC_RLE_MAPGRAY, width, height,
  108. 1 << source->header.cmaplen);
  109. } else if (source->header.ncolors == 1 && source->header.ncmap == 3) {
  110. source->visual = PSEUDOCOLOR;
  111. TRACEMS3(cinfo, 1, JTRC_RLE_MAPPED, width, height,
  112. 1 << source->header.cmaplen);
  113. } else if (source->header.ncolors == 3 && source->header.ncmap == 3) {
  114. source->visual = TRUECOLOR;
  115. TRACEMS3(cinfo, 1, JTRC_RLE_FULLMAP, width, height,
  116. 1 << source->header.cmaplen);
  117. } else if (source->header.ncolors == 3 && source->header.ncmap == 0) {
  118. source->visual = DIRECTCOLOR;
  119. TRACEMS2(cinfo, 1, JTRC_RLE, width, height);
  120. } else
  121. ERREXIT(cinfo, JERR_RLE_UNSUPPORTED);
  122. if (source->visual == GRAYSCALE || source->visual == MAPPEDGRAY) {
  123. cinfo->in_color_space = JCS_GRAYSCALE;
  124. cinfo->input_components = 1;
  125. } else {
  126. cinfo->in_color_space = JCS_RGB;
  127. cinfo->input_components = 3;
  128. }
  129. /*
  130. * A place to hold each scanline while it's converted.
  131. * (GRAYSCALE scanlines don't need converting)
  132. */
  133. if (source->visual != GRAYSCALE) {
  134. source->rle_row = (rle_pixel**) (*cinfo->mem->alloc_sarray)
  135. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  136. (JDIMENSION) width, (JDIMENSION) cinfo->input_components);
  137. }
  138. /* request a virtual array to hold the image */
  139. source->image = (*cinfo->mem->request_virt_sarray)
  140. ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
  141. (JDIMENSION) (width * source->header.ncolors),
  142. (JDIMENSION) height, (JDIMENSION) 1);
  143. #ifdef PROGRESS_REPORT
  144. if (progress != NULL) {
  145. /* count file input as separate pass */
  146. progress->total_extra_passes++;
  147. }
  148. #endif
  149. source->pub.buffer_height = 1;
  150. }
  151. /*
  152. * Read one row of pixels.
  153. * Called only after load_image has read the image into the virtual array.
  154. * Used for GRAYSCALE, MAPPEDGRAY, TRUECOLOR, and DIRECTCOLOR images.
  155. */
  156. METHODDEF(JDIMENSION)
  157. get_rle_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  158. {
  159. rle_source_ptr source = (rle_source_ptr) sinfo;
  160. source->row--;
  161. source->pub.buffer = (*cinfo->mem->access_virt_sarray)
  162. ((j_common_ptr) cinfo, source->image, source->row, (JDIMENSION) 1, FALSE);
  163. return 1;
  164. }
  165. /*
  166. * Read one row of pixels.
  167. * Called only after load_image has read the image into the virtual array.
  168. * Used for PSEUDOCOLOR images.
  169. */
  170. METHODDEF(JDIMENSION)
  171. get_pseudocolor_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  172. {
  173. rle_source_ptr source = (rle_source_ptr) sinfo;
  174. JSAMPROW src_row, dest_row;
  175. JDIMENSION col;
  176. rle_map *colormap;
  177. int val;
  178. colormap = source->header.cmap;
  179. dest_row = source->pub.buffer[0];
  180. source->row--;
  181. src_row = * (*cinfo->mem->access_virt_sarray)
  182. ((j_common_ptr) cinfo, source->image, source->row, (JDIMENSION) 1, FALSE);
  183. for (col = cinfo->image_width; col > 0; col--) {
  184. val = GETJSAMPLE(*src_row++);
  185. *dest_row++ = (JSAMPLE) (colormap[val ] >> 8);
  186. *dest_row++ = (JSAMPLE) (colormap[val + 256] >> 8);
  187. *dest_row++ = (JSAMPLE) (colormap[val + 512] >> 8);
  188. }
  189. return 1;
  190. }
  191. /*
  192. * Load the image into a virtual array. We have to do this because RLE
  193. * files start at the lower left while the JPEG standard has them starting
  194. * in the upper left. This is called the first time we want to get a row
  195. * of input. What we do is load the RLE data into the array and then call
  196. * the appropriate routine to read one row from the array. Before returning,
  197. * we set source->pub.get_pixel_rows so that subsequent calls go straight to
  198. * the appropriate row-reading routine.
  199. */
  200. METHODDEF(JDIMENSION)
  201. load_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  202. {
  203. rle_source_ptr source = (rle_source_ptr) sinfo;
  204. JDIMENSION row, col;
  205. JSAMPROW scanline, red_ptr, green_ptr, blue_ptr;
  206. rle_pixel **rle_row;
  207. rle_map *colormap;
  208. char channel;
  209. #ifdef PROGRESS_REPORT
  210. cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
  211. #endif
  212. colormap = source->header.cmap;
  213. rle_row = source->rle_row;
  214. /* Read the RLE data into our virtual array.
  215. * We assume here that (a) rle_pixel is represented the same as JSAMPLE,
  216. * and (b) we are not on a machine where FAR pointers differ from regular.
  217. */
  218. RLE_CLR_BIT(source->header, RLE_ALPHA); /* don't read the alpha channel */
  219. #ifdef PROGRESS_REPORT
  220. if (progress != NULL) {
  221. progress->pub.pass_limit = cinfo->image_height;
  222. progress->pub.pass_counter = 0;
  223. (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
  224. }
  225. #endif
  226. switch (source->visual) {
  227. case GRAYSCALE:
  228. case PSEUDOCOLOR:
  229. for (row = 0; row < cinfo->image_height; row++) {
  230. rle_row = (rle_pixel **) (*cinfo->mem->access_virt_sarray)
  231. ((j_common_ptr) cinfo, source->image, row, (JDIMENSION) 1, TRUE);
  232. rle_getrow(&source->header, rle_row);
  233. #ifdef PROGRESS_REPORT
  234. if (progress != NULL) {
  235. progress->pub.pass_counter++;
  236. (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
  237. }
  238. #endif
  239. }
  240. break;
  241. case MAPPEDGRAY:
  242. case TRUECOLOR:
  243. for (row = 0; row < cinfo->image_height; row++) {
  244. scanline = * (*cinfo->mem->access_virt_sarray)
  245. ((j_common_ptr) cinfo, source->image, row, (JDIMENSION) 1, TRUE);
  246. rle_row = source->rle_row;
  247. rle_getrow(&source->header, rle_row);
  248. for (col = 0; col < cinfo->image_width; col++) {
  249. for (channel = 0; channel < source->header.ncolors; channel++) {
  250. *scanline++ = (JSAMPLE)
  251. (colormap[GETJSAMPLE(rle_row[channel][col]) + 256 * channel] >> 8);
  252. }
  253. }
  254. #ifdef PROGRESS_REPORT
  255. if (progress != NULL) {
  256. progress->pub.pass_counter++;
  257. (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
  258. }
  259. #endif
  260. }
  261. break;
  262. case DIRECTCOLOR:
  263. for (row = 0; row < cinfo->image_height; row++) {
  264. scanline = * (*cinfo->mem->access_virt_sarray)
  265. ((j_common_ptr) cinfo, source->image, row, (JDIMENSION) 1, TRUE);
  266. rle_getrow(&source->header, rle_row);
  267. red_ptr = rle_row[0];
  268. green_ptr = rle_row[1];
  269. blue_ptr = rle_row[2];
  270. for (col = cinfo->image_width; col > 0; col--) {
  271. *scanline++ = *red_ptr++;
  272. *scanline++ = *green_ptr++;
  273. *scanline++ = *blue_ptr++;
  274. }
  275. #ifdef PROGRESS_REPORT
  276. if (progress != NULL) {
  277. progress->pub.pass_counter++;
  278. (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
  279. }
  280. #endif
  281. }
  282. }
  283. #ifdef PROGRESS_REPORT
  284. if (progress != NULL)
  285. progress->completed_extra_passes++;
  286. #endif
  287. /* Set up to call proper row-extraction routine in future */
  288. if (source->visual == PSEUDOCOLOR) {
  289. source->pub.buffer = source->rle_row;
  290. source->pub.get_pixel_rows = get_pseudocolor_row;
  291. } else {
  292. source->pub.get_pixel_rows = get_rle_row;
  293. }
  294. source->row = cinfo->image_height;
  295. /* And fetch the topmost (bottommost) row */
  296. return (*source->pub.get_pixel_rows) (cinfo, sinfo);
  297. }
  298. /*
  299. * Finish up at the end of the file.
  300. */
  301. METHODDEF(void)
  302. finish_input_rle (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  303. {
  304. /* no work */
  305. }
  306. /*
  307. * The module selection routine for RLE format input.
  308. */
  309. GLOBAL(cjpeg_source_ptr)
  310. jinit_read_rle (j_compress_ptr cinfo)
  311. {
  312. rle_source_ptr source;
  313. /* Create module interface object */
  314. source = (rle_source_ptr)
  315. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  316. SIZEOF(rle_source_struct));
  317. /* Fill in method ptrs */
  318. source->pub.start_input = start_input_rle;
  319. source->pub.finish_input = finish_input_rle;
  320. source->pub.get_pixel_rows = load_image;
  321. return (cjpeg_source_ptr) source;
  322. }
  323. #endif /* RLE_SUPPORTED */