set linesize in WriteBuffer which also sets colours, width, etc. Fixes segfault when streaming from jpegs

This commit is contained in:
Isaac Connor 2020-07-23 17:14:56 -04:00
parent 4f3b6a063e
commit cceb010048
2 changed files with 28 additions and 19 deletions

View File

@ -504,6 +504,7 @@ uint8_t* Image::WriteBuffer(
} }
if ( p_width != width || p_height != height || p_colours != colours || p_subpixelorder != subpixelorder ) { if ( p_width != width || p_height != height || p_colours != colours || p_subpixelorder != subpixelorder ) {
unsigned int newsize = (p_width * p_height) * p_colours; unsigned int newsize = (p_width * p_height) * p_colours;
if ( buffer == NULL ) { if ( buffer == NULL ) {
@ -524,6 +525,7 @@ uint8_t* Image::WriteBuffer(
width = p_width; width = p_width;
height = p_height; height = p_height;
colours = p_colours; colours = p_colours;
linesize = p_width * p_colours;
subpixelorder = p_subpixelorder; subpixelorder = p_subpixelorder;
pixels = height*width; pixels = height*width;
size = newsize; size = newsize;
@ -897,16 +899,13 @@ bool Image::ReadJpeg(const char *filename, unsigned int p_colours, unsigned int
Debug(9, "Image dimensions differ. Old: %ux%u New: %ux%u", width, height, new_width, new_height); Debug(9, "Image dimensions differ. Old: %ux%u New: %ux%u", width, height, new_width, new_height);
} }
switch (p_colours) { switch ( p_colours ) {
case ZM_COLOUR_GRAY8: case ZM_COLOUR_GRAY8:
{
cinfo->out_color_space = JCS_GRAYSCALE; cinfo->out_color_space = JCS_GRAYSCALE;
new_colours = ZM_COLOUR_GRAY8; new_colours = ZM_COLOUR_GRAY8;
new_subpixelorder = ZM_SUBPIX_ORDER_NONE; new_subpixelorder = ZM_SUBPIX_ORDER_NONE;
break; break;
}
case ZM_COLOUR_RGB32: case ZM_COLOUR_RGB32:
{
#ifdef JCS_EXTENSIONS #ifdef JCS_EXTENSIONS
new_colours = ZM_COLOUR_RGB32; new_colours = ZM_COLOUR_RGB32;
if ( p_subpixelorder == ZM_SUBPIX_ORDER_BGRA ) { if ( p_subpixelorder == ZM_SUBPIX_ORDER_BGRA ) {
@ -927,10 +926,8 @@ bool Image::ReadJpeg(const char *filename, unsigned int p_colours, unsigned int
#else #else
Warning("libjpeg-turbo is required for reading a JPEG directly into a RGB32 buffer, reading into a RGB24 buffer instead."); Warning("libjpeg-turbo is required for reading a JPEG directly into a RGB32 buffer, reading into a RGB24 buffer instead.");
#endif #endif
}
case ZM_COLOUR_RGB24: case ZM_COLOUR_RGB24:
default: default:
{
new_colours = ZM_COLOUR_RGB24; new_colours = ZM_COLOUR_RGB24;
if ( p_subpixelorder == ZM_SUBPIX_ORDER_BGR ) { if ( p_subpixelorder == ZM_SUBPIX_ORDER_BGR ) {
#ifdef JCS_EXTENSIONS #ifdef JCS_EXTENSIONS
@ -954,8 +951,7 @@ cinfo->out_color_space = JCS_RGB;
new_subpixelorder = ZM_SUBPIX_ORDER_RGB; new_subpixelorder = ZM_SUBPIX_ORDER_RGB;
} }
break; break;
} } // end switch p_colours
}
if ( WriteBuffer(new_width, new_height, new_colours, new_subpixelorder) == NULL ) { if ( WriteBuffer(new_width, new_height, new_colours, new_subpixelorder) == NULL ) {
Error("Failed requesting writeable buffer for reading JPEG image."); Error("Failed requesting writeable buffer for reading JPEG image.");
@ -2727,7 +2723,7 @@ void Image::Flip( bool leftright ) {
} }
void Image::Scale( unsigned int factor ) { void Image::Scale(unsigned int factor) {
if ( !factor ) { if ( !factor ) {
Error("Bogus scale factor %d found", factor); Error("Bogus scale factor %d found", factor);
return; return;
@ -2787,7 +2783,7 @@ void Image::Scale( unsigned int factor ) {
unsigned int last_h_index = 0; unsigned int last_h_index = 0;
unsigned int last_w_index = 0; unsigned int last_w_index = 0;
unsigned int h_index; unsigned int h_index;
for ( unsigned int y = 0; y < (unsigned int)height; y++ ) { for ( unsigned int y = 0; y < height; y++ ) {
h_count += factor; h_count += factor;
h_index = h_count/ZM_SCALE_BASE; h_index = h_count/ZM_SCALE_BASE;
if ( h_index > last_h_index ) { if ( h_index > last_h_index ) {
@ -2796,7 +2792,7 @@ void Image::Scale( unsigned int factor ) {
last_w_index = 0; last_w_index = 0;
unsigned char *ps = &buffer[y*wc]; unsigned char *ps = &buffer[y*wc];
for ( unsigned int x = 0; x < (unsigned int)width; x++ ) { for ( unsigned int x = 0; x < width; x++ ) {
w_count += factor; w_count += factor;
w_index = w_count/ZM_SCALE_BASE; w_index = w_count/ZM_SCALE_BASE;
@ -2815,7 +2811,7 @@ void Image::Scale( unsigned int factor ) {
new_width = last_w_index; new_width = last_w_index;
new_height = last_h_index; new_height = last_h_index;
} // end foreach line } // end foreach line
AssignDirect( new_width, new_height, colours, subpixelorder, scale_buffer, scale_buffer_size, ZM_BUFTYPE_ZM); AssignDirect(new_width, new_height, colours, subpixelorder, scale_buffer, scale_buffer_size, ZM_BUFTYPE_ZM);
} }
void Image::Deinterlace_Discard() { void Image::Deinterlace_Discard() {

View File

@ -167,7 +167,7 @@ protected:
public: public:
Image(); Image();
explicit Image( const char *filename ); explicit Image(const char *filename);
Image(int p_width, int p_height, int p_colours, int p_subpixelorder, uint8_t *p_buffer=0, unsigned int padding=0); Image(int p_width, int p_height, int p_colours, int p_subpixelorder, uint8_t *p_buffer=0, unsigned int padding=0);
Image(int p_width, int p_linesize, int p_height, int p_colours, int p_subpixelorder, uint8_t *p_buffer=0, unsigned int padding=0); Image(int p_width, int p_linesize, int p_height, int p_colours, int p_subpixelorder, uint8_t *p_buffer=0, unsigned int padding=0);
explicit Image( const Image &p_image ); explicit Image( const Image &p_image );
@ -201,18 +201,31 @@ public:
width = linesize = height = colours = size = pixels = subpixelorder = 0; width = linesize = height = colours = size = pixels = subpixelorder = 0;
} }
void Assign( unsigned int p_width, unsigned int p_height, unsigned int p_colours, unsigned int p_subpixelorder, const uint8_t* new_buffer, const size_t buffer_size); void Assign(
void Assign( const Image &image ); unsigned int p_width,
void AssignDirect( const unsigned int p_width, const unsigned int p_height, const unsigned int p_colours, const unsigned int p_subpixelorder, uint8_t *new_buffer, const size_t buffer_size, const int p_buffertype); unsigned int p_height,
unsigned int p_colours,
unsigned int p_subpixelorder,
const uint8_t* new_buffer,
const size_t buffer_size);
void Assign(const Image &image);
void AssignDirect(
const unsigned int p_width,
const unsigned int p_height,
const unsigned int p_colours,
const unsigned int p_subpixelorder,
uint8_t *new_buffer,
const size_t buffer_size,
const int p_buffertype);
inline void CopyBuffer( const Image &image ) { inline void CopyBuffer(const Image &image) {
Assign(image); Assign(image);
} }
inline Image &operator=( const Image &image ) { inline Image &operator=(const Image &image) {
Assign(image); Assign(image);
return *this; return *this;
} }
inline Image &operator=( const unsigned char *new_buffer ) { inline Image &operator=(const unsigned char *new_buffer) {
(*fptr_imgbufcpy)(buffer, new_buffer, size); (*fptr_imgbufcpy)(buffer, new_buffer, size);
return *this; return *this;
} }