Skip to content

Commit bb75934

Browse files
committed
STYLE: updated error message, improved size check
Several minor changes as requested. Improved check for overflow to usually avoid 2 multiplications.
1 parent a9c38a0 commit bb75934

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

Modules/IO/JPEG/src/itkJPEGImageIO.cxx

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,17 @@ JPEGImageIO::CanReadFile(const char * file)
140140
jerr.pub.error_exit = itk_jpeg_error_exit;
141141
// for any output message call itk_jpeg_output_message
142142
jerr.pub.output_message = itk_jpeg_output_message;
143-
// set the jump point, if there is a jpeg error or warning
144-
// this will evaluate to true
143+
// set the jump point
145144
if (setjmp(jerr.setjmp_buffer))
146145
{
147-
// clean up
148146
jpeg_destroy_decompress(&cinfo);
149-
// this is not a valid jpeg file
150147
return false;
151148
}
152-
/* Now we can initialize the JPEG decompression object. */
149+
// initialize the JPEG decompression object
153150
jpeg_create_decompress(&cinfo);
154-
/* Step 2: specify data source (eg, a file) */
151+
// specify data source
155152
jpeg_stdio_src(&cinfo, JPEGfp.m_FilePointer);
156-
/* Step 3: read file parameters with jpeg_read_header() */
153+
// read file parameters
157154
jpeg_read_header(&cinfo, TRUE);
158155

159156
// if no errors have occurred yet, then it must be jpeg
@@ -191,10 +188,8 @@ JPEGImageIO::Read(void * buffer)
191188
jerr.pub.output_message = itk_jpeg_output_message;
192189
if (setjmp(jerr.setjmp_buffer))
193190
{
194-
// clean up
195191
jpeg_destroy_decompress(&cinfo);
196192
itkExceptionMacro("libjpeg could not read file: " << this->GetFileName());
197-
// this is not a valid jpeg file
198193
}
199194

200195
jpeg_create_decompress(&cinfo);
@@ -205,8 +200,8 @@ JPEGImageIO::Read(void * buffer)
205200
// read the header
206201
jpeg_read_header(&cinfo, TRUE);
207202

208-
// jpeg_calc_output_dimensions was used in ReadImageInformation,
209-
// has to be used here too to ensure same parameters
203+
// jpeg_calc_output_dimensions used in ReadImageInformation,
204+
// so has to be used here too
210205
jpeg_calc_output_dimensions(&cinfo);
211206

212207
// prepare to read the bulk data
@@ -251,7 +246,7 @@ JPEGImageIO::JPEGImageIO()
251246
#if BITS_IN_JSAMPLE == 8
252247
m_ComponentType = IOComponentEnum::UCHAR;
253248
#else
254-
# error
249+
# error "JPEG files with more than 8 bits per sample are not supported"
255250
#endif
256251
m_UseCompression = false;
257252
this->Self::SetQuality(95);
@@ -308,9 +303,7 @@ JPEGImageIO::ReadImageInformation()
308303
jerr.pub.error_exit = itk_jpeg_error_exit;
309304
if (setjmp(jerr.setjmp_buffer))
310305
{
311-
// clean up
312306
jpeg_destroy_decompress(&cinfo);
313-
// this is not a valid jpeg file
314307
itkExceptionMacro("Error JPEGImageIO could not open file: " << this->GetFileName());
315308
}
316309
jpeg_create_decompress(&cinfo);
@@ -321,11 +314,10 @@ JPEGImageIO::ReadImageInformation()
321314
// read the header
322315
jpeg_read_header(&cinfo, TRUE);
323316

324-
// calculate cinfo.output_components
317+
// jpeg_calc_output_dimensions to calculate cinfo.output_components
325318
jpeg_calc_output_dimensions(&cinfo);
326-
if ((static_cast<unsigned long long>(cinfo.output_width) * cinfo.output_height * cinfo.output_components) >
327-
0xffffffff &&
328-
sizeof(size_t) < 8)
319+
if (sizeof(void *) < 8 && (static_cast<unsigned long long>(cinfo.output_width) * cinfo.output_height *
320+
cinfo.output_components) > 0xffffffff)
329321
{
330322
jpeg_destroy_decompress(&cinfo);
331323
itkExceptionMacro(<< "JPEG image is too big " << this->GetFileName());

0 commit comments

Comments
 (0)