diff options
author | 2023-12-06 06:05:40 +1100 | |
---|---|---|
committer | 2023-12-06 06:05:40 +1100 | |
commit | 73751bcea9a5365e5f4d427986ba4f75fe0300ef (patch) | |
tree | 40f937b5d5e541a8ac68a80154bf30762e42c048 /3rdparty/libjpeg/rdbmp.c | |
parent | 8b2ea383f5f4dffd15c9684e94b7a0d6868d2711 (diff) |
3rdparty/libjpeg: Updated to version 9e.
Diffstat (limited to '3rdparty/libjpeg/rdbmp.c')
-rw-r--r-- | 3rdparty/libjpeg/rdbmp.c | 131 |
1 files changed, 60 insertions, 71 deletions
diff --git a/3rdparty/libjpeg/rdbmp.c b/3rdparty/libjpeg/rdbmp.c index fd773d4bb5e..6749ea0146f 100644 --- a/3rdparty/libjpeg/rdbmp.c +++ b/3rdparty/libjpeg/rdbmp.c @@ -2,13 +2,13 @@ * rdbmp.c * * Copyright (C) 1994-1996, Thomas G. Lane. - * Modified 2009-2010 by Guido Vollbeding. + * Modified 2009-2019 by Guido Vollbeding. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains routines to read input images in Microsoft "BMP" * format (MS Windows 3.x, OS/2 1.x, and OS/2 2.x flavors). - * Currently, only 8-bit and 24-bit images are supported, not 1-bit or + * Currently, only 8-, 24-, and 32-bit images are supported, not 1-bit or * 4-bit (feeding such low-depth images into JPEG would be silly anyway). * Also, we don't support RLE-compressed files. * @@ -33,11 +33,10 @@ typedef unsigned char U_CHAR; #define UCH(x) ((int) (x)) #else /* !HAVE_UNSIGNED_CHAR */ -#ifdef CHAR_IS_UNSIGNED typedef char U_CHAR; +#ifdef CHAR_IS_UNSIGNED #define UCH(x) ((int) (x)) #else -typedef char U_CHAR; #define UCH(x) ((int) (x) & 0xFF) #endif #endif /* HAVE_UNSIGNED_CHAR */ @@ -61,7 +60,8 @@ typedef struct _bmp_source_struct { JDIMENSION source_row; /* Current source row number */ JDIMENSION row_width; /* Physical width of scanlines in file */ - int bits_per_pixel; /* remembers 8- or 24-bit format */ + int bits_per_pixel; /* remembers 8-, 24-, or 32-bit format */ + int cmap_length; /* colormap length */ } bmp_source_struct; @@ -104,7 +104,6 @@ read_colormap (bmp_source_ptr sinfo, int cmaplen, int mapentrysize) break; default: ERREXIT(sinfo->cinfo, JERR_BMP_BADCMAP); - break; } } @@ -121,23 +120,25 @@ get_8bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) /* This version is for reading 8-bit colormap indexes */ { bmp_source_ptr source = (bmp_source_ptr) sinfo; - register JSAMPARRAY colormap = source->colormap; - JSAMPARRAY image_ptr; - register int t; register JSAMPROW inptr, outptr; + register JSAMPARRAY colormap; register JDIMENSION col; + register int t; + int cmaplen; /* Fetch next row from virtual array */ source->source_row--; - image_ptr = (*cinfo->mem->access_virt_sarray) - ((j_common_ptr) cinfo, source->whole_image, - source->source_row, (JDIMENSION) 1, FALSE); + inptr = * (*cinfo->mem->access_virt_sarray) ((j_common_ptr) cinfo, + source->whole_image, source->source_row, (JDIMENSION) 1, FALSE); /* Expand the colormap indexes to real data */ - inptr = image_ptr[0]; outptr = source->pub.buffer[0]; + colormap = source->colormap; + cmaplen = source->cmap_length; for (col = cinfo->image_width; col > 0; col--) { t = GETJSAMPLE(*inptr++); + if (t >= cmaplen) + ERREXIT(cinfo, JERR_BMP_OUTOFRANGE); *outptr++ = colormap[0][t]; /* can omit GETJSAMPLE() safely */ *outptr++ = colormap[1][t]; *outptr++ = colormap[2][t]; @@ -146,26 +147,22 @@ get_8bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) return 1; } - METHODDEF(JDIMENSION) get_24bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) /* This version is for reading 24-bit pixels */ { bmp_source_ptr source = (bmp_source_ptr) sinfo; - JSAMPARRAY image_ptr; register JSAMPROW inptr, outptr; register JDIMENSION col; /* Fetch next row from virtual array */ source->source_row--; - image_ptr = (*cinfo->mem->access_virt_sarray) - ((j_common_ptr) cinfo, source->whole_image, - source->source_row, (JDIMENSION) 1, FALSE); + inptr = * (*cinfo->mem->access_virt_sarray) ((j_common_ptr) cinfo, + source->whole_image, source->source_row, (JDIMENSION) 1, FALSE); /* Transfer data. Note source values are in BGR order * (even though Microsoft's own documents say the opposite). */ - inptr = image_ptr[0]; outptr = source->pub.buffer[0]; for (col = cinfo->image_width; col > 0; col--) { outptr[2] = *inptr++; /* can omit GETJSAMPLE() safely */ @@ -177,25 +174,22 @@ get_24bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) return 1; } - METHODDEF(JDIMENSION) get_32bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) /* This version is for reading 32-bit pixels */ { bmp_source_ptr source = (bmp_source_ptr) sinfo; - JSAMPARRAY image_ptr; register JSAMPROW inptr, outptr; register JDIMENSION col; /* Fetch next row from virtual array */ source->source_row--; - image_ptr = (*cinfo->mem->access_virt_sarray) - ((j_common_ptr) cinfo, source->whole_image, - source->source_row, (JDIMENSION) 1, FALSE); + inptr = * (*cinfo->mem->access_virt_sarray) ((j_common_ptr) cinfo, + source->whole_image, source->source_row, (JDIMENSION) 1, FALSE); + /* Transfer data. Note source values are in BGR order * (even though Microsoft's own documents say the opposite). */ - inptr = image_ptr[0]; outptr = source->pub.buffer[0]; for (col = cinfo->image_width; col > 0; col--) { outptr[2] = *inptr++; /* can omit GETJSAMPLE() safely */ @@ -222,7 +216,6 @@ preload_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) register FILE *infile = source->pub.input_file; register int c; register JSAMPROW out_ptr; - JSAMPARRAY image_ptr; JDIMENSION row, col; cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress; @@ -233,10 +226,8 @@ preload_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) progress->pub.pass_limit = (long) cinfo->image_height; (*progress->pub.progress_monitor) ((j_common_ptr) cinfo); } - image_ptr = (*cinfo->mem->access_virt_sarray) - ((j_common_ptr) cinfo, source->whole_image, - row, (JDIMENSION) 1, TRUE); - out_ptr = image_ptr[0]; + out_ptr = * (*cinfo->mem->access_virt_sarray) ((j_common_ptr) cinfo, + source->whole_image, row, (JDIMENSION) 1, TRUE); for (col = source->row_width; col > 0; col--) { /* inline copy of read_byte() for speed */ if ((c = getc(infile)) == EOF) @@ -278,12 +269,12 @@ start_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) bmp_source_ptr source = (bmp_source_ptr) sinfo; U_CHAR bmpfileheader[14]; U_CHAR bmpinfoheader[64]; -#define GET_2B(array,offset) ((unsigned int) UCH(array[offset]) + \ - (((unsigned int) UCH(array[offset+1])) << 8)) -#define GET_4B(array,offset) ((INT32) UCH(array[offset]) + \ - (((INT32) UCH(array[offset+1])) << 8) + \ - (((INT32) UCH(array[offset+2])) << 16) + \ - (((INT32) UCH(array[offset+3])) << 24)) +#define GET_2B(array, offset) ((unsigned int) UCH(array[offset]) + \ + (((unsigned int) UCH(array[offset+1])) << 8)) +#define GET_4B(array, offset) ((INT32) UCH(array[offset]) + \ + (((INT32) UCH(array[offset+1])) << 8) + \ + (((INT32) UCH(array[offset+2])) << 16) + \ + (((INT32) UCH(array[offset+3])) << 24)) INT32 bfOffBits; INT32 headerSize; INT32 biWidth; @@ -299,9 +290,9 @@ start_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) /* Read and verify the bitmap file header */ if (! ReadOK(source->pub.input_file, bmpfileheader, 14)) ERREXIT(cinfo, JERR_INPUT_EOF); - if (GET_2B(bmpfileheader,0) != 0x4D42) /* 'BM' */ + if (GET_2B(bmpfileheader, 0) != 0x4D42) /* 'BM' */ ERREXIT(cinfo, JERR_BMP_NOT); - bfOffBits = (INT32) GET_4B(bmpfileheader,10); + bfOffBits = GET_4B(bmpfileheader, 10); /* We ignore the remaining fileheader fields */ /* The infoheader might be 12 bytes (OS/2 1.x), 40 bytes (Windows), @@ -309,19 +300,19 @@ start_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) */ if (! ReadOK(source->pub.input_file, bmpinfoheader, 4)) ERREXIT(cinfo, JERR_INPUT_EOF); - headerSize = (INT32) GET_4B(bmpinfoheader,0); + headerSize = GET_4B(bmpinfoheader, 0); if (headerSize < 12 || headerSize > 64) ERREXIT(cinfo, JERR_BMP_BADHEADER); - if (! ReadOK(source->pub.input_file, bmpinfoheader+4, headerSize-4)) + if (! ReadOK(source->pub.input_file, bmpinfoheader + 4, headerSize - 4)) ERREXIT(cinfo, JERR_INPUT_EOF); switch ((int) headerSize) { case 12: /* Decode OS/2 1.x header (Microsoft calls this a BITMAPCOREHEADER) */ - biWidth = (INT32) GET_2B(bmpinfoheader,4); - biHeight = (INT32) GET_2B(bmpinfoheader,6); - biPlanes = GET_2B(bmpinfoheader,8); - source->bits_per_pixel = (int) GET_2B(bmpinfoheader,10); + biWidth = (INT32) GET_2B(bmpinfoheader, 4); + biHeight = (INT32) GET_2B(bmpinfoheader, 6); + biPlanes = GET_2B(bmpinfoheader, 8); + source->bits_per_pixel = (int) GET_2B(bmpinfoheader, 10); switch (source->bits_per_pixel) { case 8: /* colormapped image */ @@ -329,25 +320,26 @@ start_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) TRACEMS2(cinfo, 1, JTRC_BMP_OS2_MAPPED, (int) biWidth, (int) biHeight); break; case 24: /* RGB image */ - TRACEMS2(cinfo, 1, JTRC_BMP_OS2, (int) biWidth, (int) biHeight); + case 32: /* RGB image + Alpha channel */ + TRACEMS3(cinfo, 1, JTRC_BMP_OS2, (int) biWidth, (int) biHeight, + source->bits_per_pixel); break; default: ERREXIT(cinfo, JERR_BMP_BADDEPTH); - break; } break; case 40: case 64: /* Decode Windows 3.x header (Microsoft calls this a BITMAPINFOHEADER) */ /* or OS/2 2.x header, which has additional fields that we ignore */ - biWidth = GET_4B(bmpinfoheader,4); - biHeight = GET_4B(bmpinfoheader,8); - biPlanes = GET_2B(bmpinfoheader,12); - source->bits_per_pixel = (int) GET_2B(bmpinfoheader,14); - biCompression = GET_4B(bmpinfoheader,16); - biXPelsPerMeter = GET_4B(bmpinfoheader,24); - biYPelsPerMeter = GET_4B(bmpinfoheader,28); - biClrUsed = GET_4B(bmpinfoheader,32); + biWidth = GET_4B(bmpinfoheader, 4); + biHeight = GET_4B(bmpinfoheader, 8); + biPlanes = GET_2B(bmpinfoheader, 12); + source->bits_per_pixel = (int) GET_2B(bmpinfoheader, 14); + biCompression = GET_4B(bmpinfoheader, 16); + biXPelsPerMeter = GET_4B(bmpinfoheader, 24); + biYPelsPerMeter = GET_4B(bmpinfoheader, 28); + biClrUsed = GET_4B(bmpinfoheader, 32); /* biSizeImage, biClrImportant fields are ignored */ switch (source->bits_per_pixel) { @@ -356,14 +348,12 @@ start_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) TRACEMS2(cinfo, 1, JTRC_BMP_MAPPED, (int) biWidth, (int) biHeight); break; case 24: /* RGB image */ - TRACEMS2(cinfo, 1, JTRC_BMP, (int) biWidth, (int) biHeight); - break; case 32: /* RGB image + Alpha channel */ - TRACEMS2(cinfo, 1, JTRC_BMP, (int) biWidth, (int) biHeight); + TRACEMS3(cinfo, 1, JTRC_BMP, (int) biWidth, (int) biHeight, + source->bits_per_pixel); break; default: ERREXIT(cinfo, JERR_BMP_BADDEPTH); - break; } if (biCompression != 0) ERREXIT(cinfo, JERR_BMP_COMPRESSED); @@ -377,13 +367,14 @@ start_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) break; default: ERREXIT(cinfo, JERR_BMP_BADHEADER); - return; + return; /* avoid compiler warnings for uninitialized variables */ } - if (biWidth <= 0 || biHeight <= 0) - ERREXIT(cinfo, JERR_BMP_EMPTY); if (biPlanes != 1) ERREXIT(cinfo, JERR_BMP_BADPLANES); + /* Sanity check for buffer allocation below */ + if (biWidth <= 0 || biHeight <= 0 || (biWidth >> 24) || (biHeight >> 24)) + ERREXIT(cinfo, JERR_BMP_OUTOFRANGE); /* Compute distance to bitmap data --- will adjust for colormap below */ bPad = bfOffBits - (headerSize + 14); @@ -395,9 +386,9 @@ start_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) else if (biClrUsed > 256) ERREXIT(cinfo, JERR_BMP_BADCMAP); /* Allocate space to store the colormap */ - source->colormap = (*cinfo->mem->alloc_sarray) - ((j_common_ptr) cinfo, JPOOL_IMAGE, - (JDIMENSION) biClrUsed, (JDIMENSION) 3); + source->colormap = (*cinfo->mem->alloc_sarray) ((j_common_ptr) cinfo, + JPOOL_IMAGE, (JDIMENSION) biClrUsed, (JDIMENSION) 3); + source->cmap_length = (int) biClrUsed; /* and read it from the file */ read_colormap(source, (int) biClrUsed, mapentrysize); /* account for size of colormap */ @@ -432,9 +423,8 @@ start_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) } /* Allocate one-row buffer for returned data */ - source->pub.buffer = (*cinfo->mem->alloc_sarray) - ((j_common_ptr) cinfo, JPOOL_IMAGE, - (JDIMENSION) (biWidth * 3), (JDIMENSION) 1); + source->pub.buffer = (*cinfo->mem->alloc_sarray) ((j_common_ptr) cinfo, + JPOOL_IMAGE, (JDIMENSION) (biWidth * 3), (JDIMENSION) 1); source->pub.buffer_height = 1; cinfo->in_color_space = JCS_RGB; @@ -466,15 +456,14 @@ jinit_read_bmp (j_compress_ptr cinfo) bmp_source_ptr source; /* Create module interface object */ - source = (bmp_source_ptr) - (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, - SIZEOF(bmp_source_struct)); + source = (bmp_source_ptr) (*cinfo->mem->alloc_small) + ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(bmp_source_struct)); source->cinfo = cinfo; /* make back link for subroutines */ /* Fill in method ptrs, except get_pixel_rows which start_input sets */ source->pub.start_input = start_input_bmp; source->pub.finish_input = finish_input_bmp; - return (cjpeg_source_ptr) source; + return &source->pub; } #endif /* BMP_SUPPORTED */ |