-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix error handling in decode jpeg (#155) * Fix error handling in decode jpeg * Update internal/decodejpeg/decode.go Co-authored-by: Joakim Möller <[email protected]> * Missed a dot Co-authored-by: Joakim Möller <[email protected]> * Preemptive bump of raclamdba version Co-authored-by: Joakim Möller <[email protected]>
- Loading branch information
1 parent
dfe2f71
commit b7c1192
Showing
4 changed files
with
84 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,70 @@ | ||
#include <stdio.h> | ||
#include <setjmp.h> | ||
|
||
#include "decode.h" | ||
|
||
char jpeg_last_error_message[JMSG_LENGTH_MAX]; | ||
|
||
void jpeg_error_exit (j_common_ptr cinfo) | ||
{ | ||
/* cinfo->err actually points to a jpeg_error_manager struct */ | ||
JpegErrorManager* myerr = (JpegErrorManager*) cinfo->err; | ||
|
||
/* output_message is a method to print an error message */ | ||
( *(cinfo->err->output_message) ) (cinfo); | ||
|
||
/* Create the message */ | ||
( *(cinfo->err->format_message) ) (cinfo, jpeg_last_error_message); | ||
|
||
/* Jump to the setjmp point */ | ||
longjmp(myerr->setjmp_buffer, 1); | ||
} | ||
|
||
GLOBAL(struct Image) | ||
read_JPEG_file(char *inbuffer, size_t size) | ||
read_JPEG_file(char* inbuffer, size_t size, char* error) | ||
{ | ||
struct Image result; | ||
struct jpeg_decompress_struct cinfo; | ||
struct jpeg_error_mgr jerr; | ||
struct Image result; | ||
struct jpeg_decompress_struct cinfo; | ||
struct JpegErrorManager jerr; | ||
|
||
const int BYTES_PER_SAMPLE = sizeof(JSAMPLE); | ||
JSAMPARRAY buffer; /* Output row buffer */ | ||
const int BYTES_PER_SAMPLE = sizeof(JSAMPLE); | ||
JSAMPARRAY buffer; /* Output row buffer */ | ||
|
||
int row_stride; /* physical row width in output buffer */ | ||
int row_stride; /* physical row width in output buffer */ | ||
|
||
cinfo.err = jpeg_std_error(&jerr); | ||
cinfo.err = jpeg_std_error(&jerr.pub); | ||
jerr.pub.error_exit = jpeg_error_exit; | ||
if (setjmp(jerr.setjmp_buffer)) { | ||
/* If we get here, the JPEG code has signaled an error. */ | ||
jpeg_destroy_decompress(&cinfo); | ||
strcpy(error, jpeg_last_error_message); | ||
return result; | ||
} | ||
|
||
jpeg_create_decompress(&cinfo); | ||
jpeg_create_decompress(&cinfo); | ||
|
||
jpeg_mem_src(&cinfo, inbuffer, size); | ||
jpeg_mem_src(&cinfo, inbuffer, size); | ||
|
||
(void)jpeg_read_header(&cinfo, TRUE); | ||
(void)jpeg_read_header(&cinfo, TRUE); | ||
|
||
(void)jpeg_start_decompress(&cinfo); | ||
result.pix = (char *)malloc(cinfo.output_height * cinfo.output_width | ||
* cinfo.output_components * BYTES_PER_SAMPLE); | ||
result.width = cinfo.output_width; | ||
result.height = cinfo.output_height; | ||
(void)jpeg_start_decompress(&cinfo); | ||
row_stride = cinfo.output_width * cinfo.output_components * BYTES_PER_SAMPLE; | ||
result.pix = (char *)malloc(cinfo.output_height * row_stride); | ||
result.width = cinfo.output_width; | ||
result.height = cinfo.output_height; | ||
|
||
row_stride = cinfo.output_width * cinfo.output_components * BYTES_PER_SAMPLE; | ||
buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo, JPOOL_IMAGE, row_stride, 1); | ||
buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo, JPOOL_IMAGE, row_stride, 1); | ||
|
||
while (cinfo.output_scanline < cinfo.output_height) | ||
{ | ||
(void)jpeg_read_scanlines(&cinfo, buffer, 1); | ||
memcpy(&(result.pix)[(cinfo.output_scanline - 1) * row_stride], | ||
buffer[0], | ||
row_stride); | ||
} | ||
(void)jpeg_finish_decompress(&cinfo); | ||
while (cinfo.output_scanline < cinfo.output_height) | ||
{ | ||
(void)jpeg_read_scanlines(&cinfo, buffer, 1); | ||
memcpy(&(result.pix)[(cinfo.output_scanline - 1) * row_stride], | ||
buffer[0], | ||
row_stride); | ||
} | ||
(void)jpeg_finish_decompress(&cinfo); | ||
|
||
jpeg_destroy_decompress(&cinfo); | ||
jpeg_destroy_decompress(&cinfo); | ||
|
||
return result; | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,21 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <setjmp.h> | ||
#include "jpeglib.h" | ||
|
||
typedef struct Image | ||
{ | ||
char *pix; | ||
char* pix; | ||
JDIMENSION width; | ||
JDIMENSION height; | ||
} Image; | ||
|
||
struct Image read_JPEG_file(char *, size_t); | ||
typedef struct JpegErrorManager { | ||
/* "public" fields */ | ||
struct jpeg_error_mgr pub; | ||
/* for return to caller */ | ||
jmp_buf setjmp_buffer; | ||
} JpegErrorManager; | ||
|
||
struct Image read_JPEG_file(char*, size_t, char*); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters