rdrle.c 11 KB

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