rdrle.c 11 KB

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