Skip to content

Commit 91f680a

Browse files
committed
Merge pull request opencv#9448 from alalek:issue_9443
2 parents 3202bbe + aacae20 commit 91f680a

File tree

8 files changed

+50
-37
lines changed

8 files changed

+50
-37
lines changed

modules/imgcodecs/src/grfmt_bmp.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ bool BmpDecoder::readHeader()
193193
bool BmpDecoder::readData( Mat& img )
194194
{
195195
uchar* data = img.ptr();
196-
int step = (int)img.step;
196+
int step = validateToInt(img.step);
197197
bool color = img.channels() > 1;
198198
uchar gray_palette[256] = {0};
199199
bool result = false;
@@ -206,7 +206,7 @@ bool BmpDecoder::readData( Mat& img )
206206

207207
if( m_origin == IPL_ORIGIN_BL )
208208
{
209-
data += (m_height - 1)*step;
209+
data += (m_height - 1)*(size_t)step;
210210
step = -step;
211211
}
212212

@@ -530,7 +530,7 @@ bool BmpEncoder::write( const Mat& img, const std::vector<int>& )
530530
int bitmapHeaderSize = 40;
531531
int paletteSize = channels > 1 ? 0 : 1024;
532532
int headerSize = 14 /* fileheader */ + bitmapHeaderSize + paletteSize;
533-
int fileSize = fileStep*height + headerSize;
533+
size_t fileSize = (size_t)fileStep*height + headerSize;
534534
PaletteEntry palette[256];
535535

536536
if( m_buf )
@@ -540,7 +540,7 @@ bool BmpEncoder::write( const Mat& img, const std::vector<int>& )
540540
strm.putBytes( fmtSignBmp, (int)strlen(fmtSignBmp) );
541541

542542
// write file header
543-
strm.putDWord( fileSize ); // file size
543+
strm.putDWord( validateToInt(fileSize) ); // file size
544544
strm.putDWord( 0 );
545545
strm.putDWord( headerSize );
546546

modules/imgcodecs/src/grfmt_exr.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,16 +195,16 @@ bool ExrDecoder::readData( Mat& img )
195195
bool color = img.channels() > 1;
196196

197197
uchar* data = img.ptr();
198-
int step = img.step;
198+
size_t step = img.step;
199199
bool justcopy = m_native_depth;
200200
bool chromatorgb = false;
201201
bool rgbtogray = false;
202202
bool result = true;
203203
FrameBuffer frame;
204204
int xsample[3] = {1, 1, 1};
205205
char *buffer;
206-
int xstep;
207-
int ystep;
206+
size_t xstep = 0;
207+
size_t ystep = 0;
208208

209209
xstep = m_native_depth ? 4 : 1;
210210

@@ -593,7 +593,7 @@ bool ExrEncoder::write( const Mat& img, const std::vector<int>& )
593593
bool issigned = depth == CV_8S || depth == CV_16S || depth == CV_32S;
594594
bool isfloat = depth == CV_32F || depth == CV_64F;
595595
depth = CV_ELEM_SIZE1(depth)*8;
596-
const int step = img.step;
596+
const size_t step = img.step;
597597

598598
Header header( width, height );
599599
Imf::PixelType type;
@@ -623,7 +623,7 @@ bool ExrEncoder::write( const Mat& img, const std::vector<int>& )
623623
FrameBuffer frame;
624624

625625
char *buffer;
626-
int bufferstep;
626+
size_t bufferstep;
627627
int size;
628628
if( type == FLOAT && depth == 32 )
629629
{

modules/imgcodecs/src/grfmt_jpeg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ int my_jpeg_load_dht (struct jpeg_decompress_struct *info, unsigned char *dht,
396396
bool JpegDecoder::readData( Mat& img )
397397
{
398398
volatile bool result = false;
399-
int step = (int)img.step;
399+
size_t step = img.step;
400400
bool color = img.channels() > 1;
401401

402402
if( m_state && m_width && m_height )

modules/imgcodecs/src/grfmt_jpeg2000.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ bool Jpeg2KDecoder::readData( Mat& img )
156156
bool result = false;
157157
int color = img.channels() > 1;
158158
uchar* data = img.ptr();
159-
int step = (int)img.step;
159+
size_t step = img.step;
160160
jas_stream_t* stream = (jas_stream_t*)m_stream;
161161
jas_image_t* image = (jas_image_t*)m_image;
162162

@@ -252,9 +252,9 @@ bool Jpeg2KDecoder::readData( Mat& img )
252252
if( !jas_image_readcmpt( image, cmptlut[i], 0, 0, xend / xstep, yend / ystep, buffer ))
253253
{
254254
if( img.depth() == CV_8U )
255-
result = readComponent8u( data + i, buffer, step, cmptlut[i], maxval, offset, ncmpts );
255+
result = readComponent8u( data + i, buffer, validateToInt(step), cmptlut[i], maxval, offset, ncmpts );
256256
else
257-
result = readComponent16u( ((unsigned short *)data) + i, buffer, step / 2, cmptlut[i], maxval, offset, ncmpts );
257+
result = readComponent16u( ((unsigned short *)data) + i, buffer, validateToInt(step / 2), cmptlut[i], maxval, offset, ncmpts );
258258
if( !result )
259259
{
260260
i = ncmpts;

modules/imgcodecs/src/grfmt_pam.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ bool PAMDecoder::readData( Mat& img )
479479
{
480480
uchar* data = img.ptr();
481481
int target_channels = img.channels();
482-
int imp_stride = (int)img.step;
482+
size_t imp_stride = img.step;
483483
int sample_depth = CV_ELEM_SIZE1(m_type);
484484
int src_elems_per_row = m_width*m_channels;
485485
int src_stride = src_elems_per_row*sample_depth;

modules/imgcodecs/src/grfmt_sunras.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ bool SunRasterDecoder::readData( Mat& img )
160160
{
161161
int color = img.channels() > 1;
162162
uchar* data = img.ptr();
163-
int step = (int)img.step;
163+
size_t step = img.step;
164164
uchar gray_palette[256] = {0};
165165
bool result = false;
166166
int src_pitch = ((m_width*m_bpp + 7)/8 + 1) & -2;
@@ -308,11 +308,11 @@ bool SunRasterDecoder::readData( Mat& img )
308308
code = m_strm.getByte();
309309

310310
if( color )
311-
data = FillUniColor( data, line_end, step, width3,
311+
data = FillUniColor( data, line_end, validateToInt(step), width3,
312312
y, m_height, len,
313313
m_palette[code] );
314314
else
315-
data = FillUniGray( data, line_end, step, width3,
315+
data = FillUniGray( data, line_end, validateToInt(step), width3,
316316
y, m_height, len,
317317
gray_palette[code] );
318318
if( y >= m_height )

modules/imgcodecs/src/utils.cpp

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@
4242
#include "precomp.hpp"
4343
#include "utils.hpp"
4444

45+
int validateToInt(size_t sz)
46+
{
47+
int valueInt = (int)sz;
48+
CV_Assert((size_t)valueInt == sz);
49+
return valueInt;
50+
}
51+
4552
#define SCALE 14
4653
#define cR (int)(0.299*(1 << SCALE) + 0.5)
4754
#define cG (int)(0.587*(1 << SCALE) + 0.5)
@@ -537,23 +544,25 @@ uchar* FillColorRow1( uchar* data, uchar* indices, int len, PaletteEntry* palett
537544
{
538545
uchar* end = data + len*3;
539546

547+
const PaletteEntry p0 = palette[0], p1 = palette[1];
548+
540549
while( (data += 24) < end )
541550
{
542551
int idx = *indices++;
543-
*((PaletteEntry*)(data - 24)) = palette[(idx & 128) != 0];
544-
*((PaletteEntry*)(data - 21)) = palette[(idx & 64) != 0];
545-
*((PaletteEntry*)(data - 18)) = palette[(idx & 32) != 0];
546-
*((PaletteEntry*)(data - 15)) = palette[(idx & 16) != 0];
547-
*((PaletteEntry*)(data - 12)) = palette[(idx & 8) != 0];
548-
*((PaletteEntry*)(data - 9)) = palette[(idx & 4) != 0];
549-
*((PaletteEntry*)(data - 6)) = palette[(idx & 2) != 0];
550-
*((PaletteEntry*)(data - 3)) = palette[(idx & 1) != 0];
552+
*((PaletteEntry*)(data - 24)) = (idx & 128) ? p1 : p0;
553+
*((PaletteEntry*)(data - 21)) = (idx & 64) ? p1 : p0;
554+
*((PaletteEntry*)(data - 18)) = (idx & 32) ? p1 : p0;
555+
*((PaletteEntry*)(data - 15)) = (idx & 16) ? p1 : p0;
556+
*((PaletteEntry*)(data - 12)) = (idx & 8) ? p1 : p0;
557+
*((PaletteEntry*)(data - 9)) = (idx & 4) ? p1 : p0;
558+
*((PaletteEntry*)(data - 6)) = (idx & 2) ? p1 : p0;
559+
*((PaletteEntry*)(data - 3)) = (idx & 1) ? p1 : p0;
551560
}
552561

553-
int idx = indices[0] << 24;
562+
int idx = indices[0];
554563
for( data -= 24; data < end; data += 3, idx += idx )
555564
{
556-
PaletteEntry clr = palette[idx < 0];
565+
const PaletteEntry clr = (idx & 128) ? p1 : p0;
557566
WRITE_PIX( data, clr );
558567
}
559568

@@ -565,23 +574,25 @@ uchar* FillGrayRow1( uchar* data, uchar* indices, int len, uchar* palette )
565574
{
566575
uchar* end = data + len;
567576

577+
const uchar p0 = palette[0], p1 = palette[1];
578+
568579
while( (data += 8) < end )
569580
{
570581
int idx = *indices++;
571-
*((uchar*)(data - 8)) = palette[(idx & 128) != 0];
572-
*((uchar*)(data - 7)) = palette[(idx & 64) != 0];
573-
*((uchar*)(data - 6)) = palette[(idx & 32) != 0];
574-
*((uchar*)(data - 5)) = palette[(idx & 16) != 0];
575-
*((uchar*)(data - 4)) = palette[(idx & 8) != 0];
576-
*((uchar*)(data - 3)) = palette[(idx & 4) != 0];
577-
*((uchar*)(data - 2)) = palette[(idx & 2) != 0];
578-
*((uchar*)(data - 1)) = palette[(idx & 1) != 0];
582+
*((uchar*)(data - 8)) = (idx & 128) ? p1 : p0;
583+
*((uchar*)(data - 7)) = (idx & 64) ? p1 : p0;
584+
*((uchar*)(data - 6)) = (idx & 32) ? p1 : p0;
585+
*((uchar*)(data - 5)) = (idx & 16) ? p1 : p0;
586+
*((uchar*)(data - 4)) = (idx & 8) ? p1 : p0;
587+
*((uchar*)(data - 3)) = (idx & 4) ? p1 : p0;
588+
*((uchar*)(data - 2)) = (idx & 2) ? p1 : p0;
589+
*((uchar*)(data - 1)) = (idx & 1) ? p1 : p0;
579590
}
580591

581-
int idx = indices[0] << 24;
592+
int idx = indices[0];
582593
for( data -= 8; data < end; data++, idx += idx )
583594
{
584-
data[0] = palette[idx < 0];
595+
data[0] = (idx & 128) ? p1 : p0;
585596
}
586597

587598
return data;

modules/imgcodecs/src/utils.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
#ifndef _UTILS_H_
4343
#define _UTILS_H_
4444

45+
int validateToInt(size_t step);
46+
4547
struct PaletteEntry
4648
{
4749
unsigned char b, g, r, a;

0 commit comments

Comments
 (0)