Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
dtschump committed Aug 17, 2023
2 parents dedd613 + 4e74663 commit 6583d2f
Showing 1 changed file with 41 additions and 16 deletions.
57 changes: 41 additions & 16 deletions CImg.h
Original file line number Diff line number Diff line change
Expand Up @@ -7385,11 +7385,18 @@ namespace cimg_library {
\param filename Specified filename to get size from.
\return File size or '-1' if file does not exist.
**/
inline cimg_int64 fsize(const char *const filename) {
std::FILE *const file = cimg::std_fopen(filename,"rb");
inline cimg_int64 fsize(std::FILE *const file) {
if (!file) return (cimg_int64)-1;
const long pos = std::ftell(file);
std::fseek(file,0,SEEK_END);
const cimg_int64 siz = (cimg_int64)std::ftell(file);
std::fseek(file,pos,SEEK_SET);
return siz;
}

inline cimg_int64 fsize(const char *const filename) {
std::FILE *const file = cimg::std_fopen(filename,"rb");
const cimg_int64 siz = fsize(file);
cimg::fclose(file);
return siz;
}
Expand Down Expand Up @@ -54417,7 +54424,7 @@ namespace cimg_library {
"load_bmp(): Specified filename is (null).",
cimg_instance);

const ulongT fsiz = file?(ulongT)cimg_max_buf_size:(ulongT)cimg::fsize(filename);
const ulongT fsiz = (ulongT)(file?cimg::fsize(file):cimg::fsize(filename));
std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
CImg<ucharT> header(54);
cimg::fread(header._data,54,nfile);
Expand All @@ -54440,32 +54447,50 @@ namespace cimg_library {
nb_colors = header[0x2E] + (header[0x2F]<<8) + (header[0x30]<<16) + (header[0x31]<<24),
bpp = header[0x1C] + (header[0x1D]<<8);

if (!file_size || file_size==offset) {
cimg::fseek(nfile,0,SEEK_END);
file_size = (int)cimg::ftell(nfile);
cimg::fseek(nfile,54,SEEK_SET);
}
if (header_size>40) cimg::fseek(nfile,header_size - 40,SEEK_CUR);
if ((ulongT)file_size!=fsiz)
throw CImgIOException(_cimg_instance
"load_bmp(): Invalid file_size %d specified in filename '%s' (expected %lu).",
cimg_instance,
file_size,filename?filename:"(FILE*)",fsiz);

if (header_size<0 || header_size>=file_size)
throw CImgIOException(_cimg_instance
"load_bmp(): Invalid header size %d specified in filename '%s'.",
cimg_instance,
header_size,filename?filename:"(FILE*)");

if (offset<0 || offset>=file_size)
throw CImgIOException(_cimg_instance
"load_bmp(): Invalid offset %d specified in filename '%s'.",
cimg_instance,
offset,filename?filename:"(FILE*)");

if (header_size>40) cimg::fseek(nfile,header_size - 40,SEEK_CUR);
const int
dx_bytes = (bpp==1)?(dx/8 + (dx%8?1:0)):((bpp==4)?(dx/2 + (dx%2)):(int)((longT)dx*bpp/8)),
align_bytes = (4 - dx_bytes%4)%4;
const ulongT
cimg_iobuffer = (ulongT)24*1024*1024,
buf_size = (ulongT)cimg::abs(dy)*(dx_bytes + align_bytes);

if (buf_size>fsiz)
throw CImgIOException(_cimg_instance
"load_bmp(): File size %lu for filename '%s' does not match "
"encoded image dimensions (%d,%d).",
cimg_instance,
(long)fsiz,filename?filename:"(FILE*)",dx,dy);
if (buf_size>=fsiz)
throw CImgIOException(_cimg_instance
"load_bmp(): File size %lu for filename '%s' does not match "
"encoded image dimensions (%d,%d).",
cimg_instance,
(long)fsiz,filename?filename:"(FILE*)",dx,dy);

CImg<intT> colormap;
if (bpp<16) { if (!nb_colors) nb_colors = 1<<bpp; } else nb_colors = 0;
if (nb_colors) { colormap.assign(nb_colors); cimg::fread(colormap._data,nb_colors,nfile); }

const int xoffset = offset - 14 - header_size - 4*nb_colors;
if (xoffset>0) cimg::fseek(nfile,xoffset,SEEK_CUR);
if (xoffset<0 || xoffset>=file_size)
throw CImgIOException(_cimg_instance
"load_bmp(): Malformed header in filename '%s'.",
cimg_instance,
filename?filename:"(FILE*)");
cimg::fseek(nfile,xoffset,SEEK_CUR);

CImg<ucharT> buffer;
if (buf_size<cimg_iobuffer) {
Expand Down

0 comments on commit 6583d2f

Please sign in to comment.