| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include "cdjpeg.h" |
| 23 |
|
| 24 |
#ifdef RLE_SUPPORTED |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
#include <rle.h> |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#if BITS_IN_JSAMPLE != 8 |
| 36 |
Sorry, this code only copes with 8-bit JSAMPLEs. |
| 37 |
#endif |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
typedef enum |
| 52 |
{ GRAYSCALE, MAPPEDGRAY, PSEUDOCOLOR, TRUECOLOR, DIRECTCOLOR } rle_kind; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
typedef struct _rle_source_struct * rle_source_ptr; |
| 63 |
|
| 64 |
typedef struct _rle_source_struct { |
| 65 |
struct cjpeg_source_struct pub; |
| 66 |
|
| 67 |
rle_kind visual; |
| 68 |
jvirt_sarray_ptr image; |
| 69 |
JDIMENSION row; |
| 70 |
rle_hdr header; |
| 71 |
rle_pixel** rle_row; |
| 72 |
|
| 73 |
} rle_source_struct; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
METHODDEF(void) |
| 81 |
start_input_rle (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 82 |
{ |
| 83 |
rle_source_ptr source = (rle_source_ptr) sinfo; |
| 84 |
JDIMENSION width, height; |
| 85 |
#ifdef PROGRESS_REPORT |
| 86 |
cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress; |
| 87 |
#endif |
| 88 |
|
| 89 |
|
| 90 |
source->header = *rle_hdr_init(NULL); |
| 91 |
source->header.rle_file = source->pub.input_file; |
| 92 |
switch (rle_get_setup(&(source->header))) { |
| 93 |
case RLE_SUCCESS: |
| 94 |
|
| 95 |
break; |
| 96 |
case RLE_NOT_RLE: |
| 97 |
ERREXIT(cinfo, JERR_RLE_NOT); |
| 98 |
break; |
| 99 |
case RLE_NO_SPACE: |
| 100 |
ERREXIT(cinfo, JERR_RLE_MEM); |
| 101 |
break; |
| 102 |
case RLE_EMPTY: |
| 103 |
ERREXIT(cinfo, JERR_RLE_EMPTY); |
| 104 |
break; |
| 105 |
case RLE_EOF: |
| 106 |
ERREXIT(cinfo, JERR_RLE_EOF); |
| 107 |
break; |
| 108 |
default: |
| 109 |
ERREXIT(cinfo, JERR_RLE_BADERROR); |
| 110 |
break; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
width = source->header.xmax - source->header.xmin + 1; |
| 116 |
height = source->header.ymax - source->header.ymin + 1; |
| 117 |
source->header.xmin = 0; |
| 118 |
source->header.xmax = width-1; |
| 119 |
|
| 120 |
cinfo->image_width = width; |
| 121 |
cinfo->image_height = height; |
| 122 |
cinfo->data_precision = 8; |
| 123 |
|
| 124 |
if (source->header.ncolors == 1 && source->header.ncmap == 0) { |
| 125 |
source->visual = GRAYSCALE; |
| 126 |
TRACEMS2(cinfo, 1, JTRC_RLE_GRAY, width, height); |
| 127 |
} else if (source->header.ncolors == 1 && source->header.ncmap == 1) { |
| 128 |
source->visual = MAPPEDGRAY; |
| 129 |
TRACEMS3(cinfo, 1, JTRC_RLE_MAPGRAY, width, height, |
| 130 |
1 << source->header.cmaplen); |
| 131 |
} else if (source->header.ncolors == 1 && source->header.ncmap == 3) { |
| 132 |
source->visual = PSEUDOCOLOR; |
| 133 |
TRACEMS3(cinfo, 1, JTRC_RLE_MAPPED, width, height, |
| 134 |
1 << source->header.cmaplen); |
| 135 |
} else if (source->header.ncolors == 3 && source->header.ncmap == 3) { |
| 136 |
source->visual = TRUECOLOR; |
| 137 |
TRACEMS3(cinfo, 1, JTRC_RLE_FULLMAP, width, height, |
| 138 |
1 << source->header.cmaplen); |
| 139 |
} else if (source->header.ncolors == 3 && source->header.ncmap == 0) { |
| 140 |
source->visual = DIRECTCOLOR; |
| 141 |
TRACEMS2(cinfo, 1, JTRC_RLE, width, height); |
| 142 |
} else |
| 143 |
ERREXIT(cinfo, JERR_RLE_UNSUPPORTED); |
| 144 |
|
| 145 |
if (source->visual == GRAYSCALE || source->visual == MAPPEDGRAY) { |
| 146 |
cinfo->in_color_space = JCS_GRAYSCALE; |
| 147 |
cinfo->input_components = 1; |
| 148 |
} else { |
| 149 |
cinfo->in_color_space = JCS_RGB; |
| 150 |
cinfo->input_components = 3; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
if (source->visual != GRAYSCALE) { |
| 158 |
source->rle_row = (rle_pixel**) (*cinfo->mem->alloc_sarray) |
| 159 |
((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 160 |
(JDIMENSION) width, (JDIMENSION) cinfo->input_components); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
source->image = (*cinfo->mem->request_virt_sarray) |
| 165 |
((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE, |
| 166 |
(JDIMENSION) (width * source->header.ncolors), |
| 167 |
(JDIMENSION) height, (JDIMENSION) 1); |
| 168 |
|
| 169 |
#ifdef PROGRESS_REPORT |
| 170 |
if (progress != NULL) { |
| 171 |
|
| 172 |
progress->total_extra_passes++; |
| 173 |
} |
| 174 |
#endif |
| 175 |
|
| 176 |
source->pub.buffer_height = 1; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
METHODDEF(JDIMENSION) |
| 187 |
get_rle_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 188 |
{ |
| 189 |
rle_source_ptr source = (rle_source_ptr) sinfo; |
| 190 |
|
| 191 |
source->row--; |
| 192 |
source->pub.buffer = (*cinfo->mem->access_virt_sarray) |
| 193 |
((j_common_ptr) cinfo, source->image, source->row, (JDIMENSION) 1, FALSE); |
| 194 |
|
| 195 |
return 1; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
METHODDEF(JDIMENSION) |
| 205 |
get_pseudocolor_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 206 |
{ |
| 207 |
rle_source_ptr source = (rle_source_ptr) sinfo; |
| 208 |
JSAMPROW src_row, dest_row; |
| 209 |
JDIMENSION col; |
| 210 |
rle_map *colormap; |
| 211 |
int val; |
| 212 |
|
| 213 |
colormap = source->header.cmap; |
| 214 |
dest_row = source->pub.buffer[0]; |
| 215 |
source->row--; |
| 216 |
src_row = * (*cinfo->mem->access_virt_sarray) |
| 217 |
((j_common_ptr) cinfo, source->image, source->row, (JDIMENSION) 1, FALSE); |
| 218 |
|
| 219 |
for (col = cinfo->image_width; col > 0; col--) { |
| 220 |
val = GETJSAMPLE(*src_row++); |
| 221 |
*dest_row++ = (JSAMPLE) (colormap[val ] >> 8); |
| 222 |
*dest_row++ = (JSAMPLE) (colormap[val + 256] >> 8); |
| 223 |
*dest_row++ = (JSAMPLE) (colormap[val + 512] >> 8); |
| 224 |
} |
| 225 |
|
| 226 |
return 1; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
METHODDEF(JDIMENSION) |
| 241 |
load_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 242 |
{ |
| 243 |
rle_source_ptr source = (rle_source_ptr) sinfo; |
| 244 |
JDIMENSION row, col; |
| 245 |
JSAMPROW scanline, red_ptr, green_ptr, blue_ptr; |
| 246 |
rle_pixel **rle_row; |
| 247 |
rle_map *colormap; |
| 248 |
char channel; |
| 249 |
#ifdef PROGRESS_REPORT |
| 250 |
cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress; |
| 251 |
#endif |
| 252 |
|
| 253 |
colormap = source->header.cmap; |
| 254 |
rle_row = source->rle_row; |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
RLE_CLR_BIT(source->header, RLE_ALPHA); |
| 261 |
|
| 262 |
#ifdef PROGRESS_REPORT |
| 263 |
if (progress != NULL) { |
| 264 |
progress->pub.pass_limit = cinfo->image_height; |
| 265 |
progress->pub.pass_counter = 0; |
| 266 |
(*progress->pub.progress_monitor) ((j_common_ptr) cinfo); |
| 267 |
} |
| 268 |
#endif |
| 269 |
|
| 270 |
switch (source->visual) { |
| 271 |
|
| 272 |
case GRAYSCALE: |
| 273 |
case PSEUDOCOLOR: |
| 274 |
for (row = 0; row < cinfo->image_height; row++) { |
| 275 |
rle_row = (rle_pixel **) (*cinfo->mem->access_virt_sarray) |
| 276 |
((j_common_ptr) cinfo, source->image, row, (JDIMENSION) 1, TRUE); |
| 277 |
rle_getrow(&source->header, rle_row); |
| 278 |
#ifdef PROGRESS_REPORT |
| 279 |
if (progress != NULL) { |
| 280 |
progress->pub.pass_counter++; |
| 281 |
(*progress->pub.progress_monitor) ((j_common_ptr) cinfo); |
| 282 |
} |
| 283 |
#endif |
| 284 |
} |
| 285 |
break; |
| 286 |
|
| 287 |
case MAPPEDGRAY: |
| 288 |
case TRUECOLOR: |
| 289 |
for (row = 0; row < cinfo->image_height; row++) { |
| 290 |
scanline = * (*cinfo->mem->access_virt_sarray) |
| 291 |
((j_common_ptr) cinfo, source->image, row, (JDIMENSION) 1, TRUE); |
| 292 |
rle_row = source->rle_row; |
| 293 |
rle_getrow(&source->header, rle_row); |
| 294 |
|
| 295 |
for (col = 0; col < cinfo->image_width; col++) { |
| 296 |
for (channel = 0; channel < source->header.ncolors; channel++) { |
| 297 |
*scanline++ = (JSAMPLE) |
| 298 |
(colormap[GETJSAMPLE(rle_row[channel][col]) + 256 * channel] >> 8); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
#ifdef PROGRESS_REPORT |
| 303 |
if (progress != NULL) { |
| 304 |
progress->pub.pass_counter++; |
| 305 |
(*progress->pub.progress_monitor) ((j_common_ptr) cinfo); |
| 306 |
} |
| 307 |
#endif |
| 308 |
} |
| 309 |
break; |
| 310 |
|
| 311 |
case DIRECTCOLOR: |
| 312 |
for (row = 0; row < cinfo->image_height; row++) { |
| 313 |
scanline = * (*cinfo->mem->access_virt_sarray) |
| 314 |
((j_common_ptr) cinfo, source->image, row, (JDIMENSION) 1, TRUE); |
| 315 |
rle_getrow(&source->header, rle_row); |
| 316 |
|
| 317 |
red_ptr = rle_row[0]; |
| 318 |
green_ptr = rle_row[1]; |
| 319 |
blue_ptr = rle_row[2]; |
| 320 |
|
| 321 |
for (col = cinfo->image_width; col > 0; col--) { |
| 322 |
*scanline++ = *red_ptr++; |
| 323 |
*scanline++ = *green_ptr++; |
| 324 |
*scanline++ = *blue_ptr++; |
| 325 |
} |
| 326 |
|
| 327 |
#ifdef PROGRESS_REPORT |
| 328 |
if (progress != NULL) { |
| 329 |
progress->pub.pass_counter++; |
| 330 |
(*progress->pub.progress_monitor) ((j_common_ptr) cinfo); |
| 331 |
} |
| 332 |
#endif |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
#ifdef PROGRESS_REPORT |
| 337 |
if (progress != NULL) |
| 338 |
progress->completed_extra_passes++; |
| 339 |
#endif |
| 340 |
|
| 341 |
|
| 342 |
if (source->visual == PSEUDOCOLOR) { |
| 343 |
source->pub.buffer = source->rle_row; |
| 344 |
source->pub.get_pixel_rows = get_pseudocolor_row; |
| 345 |
} else { |
| 346 |
source->pub.get_pixel_rows = get_rle_row; |
| 347 |
} |
| 348 |
source->row = cinfo->image_height; |
| 349 |
|
| 350 |
|
| 351 |
return (*source->pub.get_pixel_rows) (cinfo, sinfo); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
METHODDEF(void) |
| 360 |
finish_input_rle (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 361 |
{ |
| 362 |
|
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
GLOBAL(cjpeg_source_ptr) |
| 371 |
jinit_read_rle (j_compress_ptr cinfo) |
| 372 |
{ |
| 373 |
rle_source_ptr source; |
| 374 |
|
| 375 |
|
| 376 |
source = (rle_source_ptr) |
| 377 |
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 378 |
SIZEOF(rle_source_struct)); |
| 379 |
|
| 380 |
source->pub.start_input = start_input_rle; |
| 381 |
source->pub.finish_input = finish_input_rle; |
| 382 |
source->pub.get_pixel_rows = load_image; |
| 383 |
|
| 384 |
return (cjpeg_source_ptr) source; |
| 385 |
} |
| 386 |
|
| 387 |
#endif |