63 #define INPUT_BUF_SIZE 4096 65 #define GetColourIndex(r, g, b, a) \ 66 OSD_FindNearestColourIndex((a << 24) | (r << 16) | (g << 8) | b ) 79 struct jpeg_error_mgr pub;
80 jmp_buf setjmp_buffer;
89 struct jpeg_source_mgr pub;
93 boolean start_of_file;
101 static void my_error_exit( j_common_ptr cinfo )
104 my_error_ptr myerr = (my_error_ptr) cinfo->err;
108 (*cinfo->err->output_message)(cinfo);
111 longjmp(myerr->setjmp_buffer, 1);
114 #if JPEG_LIB_VERSION == 62 116 size_t pseudo_fread(
void *buf,
size_t size,
size_t count, FILE *file )
130 init_source(j_decompress_ptr cinfo)
138 src->start_of_file = TRUE;
175 fill_input_buffer(j_decompress_ptr cinfo)
178 myfile_t *pseudo_file = src->infile;
179 size_t nbytes = INPUT_BUF_SIZE;
181 if (pseudo_file->left < INPUT_BUF_SIZE)
183 nbytes = pseudo_file->left;
186 if (src->start_of_file)
188 ERREXIT(cinfo, JERR_INPUT_EMPTY);
190 WARNMS(cinfo, JWRN_JPEG_EOF);
192 src->buffer[0] = (UINT8) 0xFF;
193 src->buffer[1] = (UINT8) JPEG_EOI;
197 memcpy( src->buffer, pseudo_file->data + pseudo_file->posn, nbytes );
198 pseudo_file->posn += nbytes;
199 pseudo_file->left -= nbytes;
201 src->pub.next_input_byte = src->buffer;
202 src->pub.bytes_in_buffer = nbytes;
203 src->start_of_file = FALSE;
222 skip_input_data(j_decompress_ptr cinfo,
long num_bytes)
232 while (num_bytes > (
long) src->pub.bytes_in_buffer)
234 num_bytes -= (long) src->pub.bytes_in_buffer;
235 (
void) fill_input_buffer(cinfo);
240 src->pub.next_input_byte += (size_t) num_bytes;
241 src->pub.bytes_in_buffer -= (size_t) num_bytes;
265 term_source(j_decompress_ptr cinfo)
278 jobs_stdio_src(j_decompress_ptr cinfo,
myfile_t * infile)
289 if (cinfo->src == NULL)
291 cinfo->src = (
struct jpeg_source_mgr *)
292 (*cinfo->mem->alloc_small)((j_common_ptr) cinfo, JPOOL_PERMANENT,
295 src->buffer = (UINT8 *)
296 (*cinfo->mem->alloc_small)((j_common_ptr) cinfo, JPOOL_PERMANENT,
297 INPUT_BUF_SIZE *
sizeof(UINT8));
301 src->pub.init_source = init_source;
302 src->pub.fill_input_buffer = fill_input_buffer;
303 src->pub.skip_input_data = skip_input_data;
304 src->pub.resync_to_restart = jpeg_resync_to_restart;
305 src->pub.term_source = term_source;
306 src->infile = infile;
307 src->pub.bytes_in_buffer = 0;
308 src->pub.next_input_byte = NULL;
328 struct jpeg_decompress_struct cinfo;
336 pseudo_file.data = data;
337 pseudo_file.posn = 0;
338 pseudo_file.left = len;
339 cinfo.err = jpeg_std_error(&jerr.pub);
340 jerr.pub.error_exit = my_error_exit;
341 if (setjmp(jerr.setjmp_buffer))
346 jpeg_destroy_decompress(&cinfo);
352 jpeg_create_decompress(&cinfo);
353 jobs_stdio_src( &cinfo, &pseudo_file );
354 (void)jpeg_read_header(&cinfo, TRUE);
357 cinfo.out_color_space = JCS_RGB;
359 (void)jpeg_start_decompress(&cinfo);
360 assert(cinfo.output_components == 3);
362 IF_COL_DEPTH( COLOUR_FORMAT_PALETTE )
368 U8BIT *s_buff = OSD_MemAlloc( cinfo.output_width * cinfo.output_height );
371 r_bytes = cinfo.output_width * cinfo.output_components;
372 row_ptr = OSD_MemAlloc( r_bytes );
376 j_bmp->col_buff = s_buff;
377 j_bmp->width = cinfo.output_width;
378 j_bmp->height = cinfo.output_height;
381 j_bmp->y_resolution = SD_METRIC_RESOLUTION;
383 j_bmp->pix_bytes = 1;
384 j_bmp->opaque = COMPLETELY_OPAQUE;
385 while (cinfo.output_scanline < cinfo.output_height)
387 (void)jpeg_read_scanlines( &cinfo, &row_ptr, 1 );
388 for (r_ndx = 0; r_ndx != r_bytes; r_ndx += cinfo.output_components)
390 *s_buff++ = GetColourIndex(row_ptr[r_ndx], row_ptr[r_ndx + 1], row_ptr[r_ndx + 2], 0xff);
393 OSD_MemFree(row_ptr);
401 WARNMS1(&cinfo, JERR_OUT_OF_MEMORY, 0);
414 #if defined(OSD_16_BIT) || defined(OSD_31_BIT) || defined(OSD_32_BIT) 416 r_bytes = cinfo.output_width * cinfo.output_components;
422 if (!cinfo.saw_JFIF_marker)
424 resolution = SD_METRIC_RESOLUTION;
428 density_unit = cinfo.density_unit;
429 if ((cinfo.Y_density != cinfo.X_density) ||
430 (density_unit < 1) || (density_unit > 2))
436 resolution = cinfo.Y_density;
438 if (density_unit == 1)
441 resolution = (resolution * 5000 + 63) / 127;
443 else if (density_unit == 2)
446 switch (cinfo.Y_density)
448 case 53: resolution = 5315;
break;
449 case 35: resolution = 3543;
break;
450 case 28: resolution = 2835;
break;
451 default: resolution = resolution * 100;
457 resolution = SD_METRIC_RESOLUTION;
462 row_ptr = OSD_MemAlloc( r_bytes * cinfo.output_height );
468 WARNMS1(&cinfo, JERR_OUT_OF_MEMORY, 0);
472 j_bmp->y_resolution = (U16BIT)resolution;
473 j_bmp->width = cinfo.output_width;
474 j_bmp->height = cinfo.output_height;
475 j_bmp->col_buff = row_ptr;
476 j_bmp->pix_bytes = 3;
477 j_bmp->opaque = COMPLETELY_OPAQUE;
478 while (cinfo.output_scanline < cinfo.output_height)
480 (void)jpeg_read_scanlines( &cinfo, &row_ptr, 1 );
488 (void)jpeg_finish_decompress(&cinfo);
489 jpeg_destroy_decompress(&cinfo);
497 OSD_MemFree(data->col_buff);
void DEC_FreeJpg(S_IMAGE *data)
Free memory allocated for JPEG bitmap.
S_IMAGE * DEC_DecodeJpg(U8BIT *data, U32BIT len)
Allocate memory for JPG and decode bitmap.
The functions in this file are OPTIONALLY provided by Receiver Platform *.