boolean jpeg_cf_fill_input_buffer(j_decompress_ptr cinfo) { cfile_src_ptr src = (cfile_src_ptr)cinfo->src; size_t nbytes; nbytes = cfread(src->buffer, 1, INPUT_BUF_SIZE, src->infile); if (nbytes <= 0) { if (src->start_of_file) { // Treat empty input file as fatal error Jpeg_Set_Error(JPEG_ERROR_READING); } // Insert a fake EOI marker src->buffer[0] = (JOCTET)0xFF; src->buffer[1] = (JOCTET)JPEG_EOI; nbytes = 2; return FALSE; } src->pub.next_input_byte = src->buffer; src->pub.bytes_in_buffer = nbytes; src->start_of_file = FALSE; return TRUE; }
// Reads header information from the JPEG file into the bitmap pointer // // filename - name of the JPEG bitmap file // w - (output) width of the bitmap // h - (output) height of the bitmap // bpp - (output) bits per pixel of the bitmap // // returns - JPEG_ERROR_NONE if successful, otherwise error code // int jpeg_read_header(const char *real_filename, CFILE *img_cfp, int *w, int *h, int *bpp, ubyte *palette) { CFILE *jpeg_file = NULL; char filename[MAX_FILENAME_LEN]; if (img_cfp == NULL) { strcpy_s( filename, real_filename ); char *p = strchr( filename, '.' ); if ( p ) *p = 0; strcat_s( filename, ".jpg" ); jpeg_file = cfopen( filename , "rb" ); if ( !jpeg_file ) { return JPEG_ERROR_READING; } } else { jpeg_file = img_cfp; } Assert( jpeg_file != NULL ); if (jpeg_file == NULL) return JPEG_ERROR_READING; // set the basic/default error code Jpeg_Set_Error(JPEG_ERROR_NONE); // initialize error message handler and decompression struct jpeg_info.err = jpeg_std_error(&jpeg_err); jpeg_err.error_exit = jpg_error_exit; jpeg_err.output_message = jpg_output_message; jpeg_create_decompress(&jpeg_info); // setup to read data via CFILE jpeg_cfile_src(&jpeg_info, jpeg_file); jpeg_read_header(&jpeg_info, TRUE); // send the info back out if (w) *w = jpeg_info.image_width; if (h) *h = jpeg_info.image_height; if (bpp) *bpp = (jpeg_info.num_components * 8); // cleanup jpeg_destroy_decompress(&jpeg_info); if (img_cfp == NULL) { cfclose(jpeg_file); jpeg_file = NULL; } return jpeg_error_code; }
// error (exit) handler void jpg_error_exit(j_common_ptr cinfo) { // give an error message that isn't just generic (*cinfo->err->output_message)(cinfo); // do cleanup since we won't get the chance otherwise jpeg_destroy(cinfo); // set the error code, pretty much always going to be a read error Jpeg_Set_Error(JPEG_ERROR_READING); // bail longjmp(FSJpegError, 1); }
// Loads a JPEG image // // filename - name of the targa file to load // image_data - allocated storage for the bitmap // // returns - true if succesful, false otherwise // int jpeg_read_bitmap(char* real_filename, ubyte* image_data, ubyte* palette, int dest_size, int cf_type) { char filename[MAX_FILENAME_LEN]; CFILE* img_cfp = NULL; JSAMPARRAY buffer = NULL; int rc = 0; strcpy_s(filename, real_filename); char* p = strchr(filename, '.'); if (p) *p = 0; strcat_s(filename, ".jpg"); img_cfp = cfopen(filename, "rb", CFILE_NORMAL, cf_type); if (img_cfp == NULL) return JPEG_ERROR_READING; // set the basic error code Jpeg_Set_Error(JPEG_ERROR_NONE); // initialize error message handler jpeg_info.err = jpeg_std_error(&jpeg_err); jpeg_err.error_exit = jpg_error_exit; jpeg_err.output_message = jpg_output_message; // SPECIAL NOTE: we've already allocated memory on the basis of original height, width and bpp // of the image from the header. DO NOT change any settings that affect output variables here // or we risk needed more memory than we have available in "image_data". The only exception is // bpp since 'dest_size' should already indicate what we want to end up with. if ((rc = setjmp(FSJpegError) == 0) != FALSE) { // initialize decompression struct jpeg_create_decompress(&jpeg_info); // setup to read data via CFILE jpeg_cfile_src(&jpeg_info, img_cfp); jpeg_read_header(&jpeg_info, TRUE); // memory for the storage of each scanline jpeg_calc_output_dimensions(&jpeg_info); // set the output components to be 'dest_size' (so we can support 16/24/32-bit images with this one function) // NOTE: only 24-bit is actually supported right now, we don't currently up/down sample at all jpeg_info.output_components = dest_size; jpeg_info.out_color_components = dest_size; // may need/have to match above // multiplying by rec_outbuf_height isn't required but is more efficient int size = jpeg_info.output_width * jpeg_info.output_components * jpeg_info.rec_outbuf_height; // a standard malloc doesn't appear to work properly here (debug vm_malloc], crashes in lib buffer = (*jpeg_info.mem->alloc_sarray)((j_common_ptr)&jpeg_info, JPOOL_IMAGE, size, 1); // DON'T free() THIS! jpeg lib does it // begin decompression process -- jpeg_start_decompress(&jpeg_info); // read each scanline and output to previously allocated 'image_data' while (jpeg_info.output_scanline < jpeg_info.output_height) { jpeg_read_scanlines(&jpeg_info, buffer, 1); memcpy(image_data, *buffer, size); image_data += size; } // -- done with decompression process jpeg_finish_decompress(&jpeg_info); // cleanup jpeg_destroy_decompress(&jpeg_info); } cfclose(img_cfp); return jpeg_error_code; }