From cceb010048b30f3a11ae8808ecda688085e265a5 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Thu, 23 Jul 2020 17:14:56 -0400 Subject: [PATCH] set linesize in WriteBuffer which also sets colours, width, etc. Fixes segfault when streaming from jpegs --- src/zm_image.cpp | 20 ++++++++------------ src/zm_image.h | 27 ++++++++++++++++++++------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/zm_image.cpp b/src/zm_image.cpp index e9c389e1c..cd6260913 100644 --- a/src/zm_image.cpp +++ b/src/zm_image.cpp @@ -504,6 +504,7 @@ uint8_t* Image::WriteBuffer( } if ( p_width != width || p_height != height || p_colours != colours || p_subpixelorder != subpixelorder ) { + unsigned int newsize = (p_width * p_height) * p_colours; if ( buffer == NULL ) { @@ -524,6 +525,7 @@ uint8_t* Image::WriteBuffer( width = p_width; height = p_height; colours = p_colours; + linesize = p_width * p_colours; subpixelorder = p_subpixelorder; pixels = height*width; 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); } - switch (p_colours) { + switch ( p_colours ) { case ZM_COLOUR_GRAY8: - { cinfo->out_color_space = JCS_GRAYSCALE; new_colours = ZM_COLOUR_GRAY8; new_subpixelorder = ZM_SUBPIX_ORDER_NONE; break; - } case ZM_COLOUR_RGB32: - { #ifdef JCS_EXTENSIONS new_colours = ZM_COLOUR_RGB32; if ( p_subpixelorder == ZM_SUBPIX_ORDER_BGRA ) { @@ -927,10 +926,8 @@ bool Image::ReadJpeg(const char *filename, unsigned int p_colours, unsigned int #else Warning("libjpeg-turbo is required for reading a JPEG directly into a RGB32 buffer, reading into a RGB24 buffer instead."); #endif - } case ZM_COLOUR_RGB24: default: - { new_colours = ZM_COLOUR_RGB24; if ( p_subpixelorder == ZM_SUBPIX_ORDER_BGR ) { #ifdef JCS_EXTENSIONS @@ -954,8 +951,7 @@ cinfo->out_color_space = JCS_RGB; new_subpixelorder = ZM_SUBPIX_ORDER_RGB; } break; - } - } + } // end switch p_colours if ( WriteBuffer(new_width, new_height, new_colours, new_subpixelorder) == NULL ) { 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 ) { Error("Bogus scale factor %d found", factor); return; @@ -2787,7 +2783,7 @@ void Image::Scale( unsigned int factor ) { unsigned int last_h_index = 0; unsigned int last_w_index = 0; 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_index = h_count/ZM_SCALE_BASE; if ( h_index > last_h_index ) { @@ -2796,7 +2792,7 @@ void Image::Scale( unsigned int factor ) { last_w_index = 0; 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_index = w_count/ZM_SCALE_BASE; @@ -2815,7 +2811,7 @@ void Image::Scale( unsigned int factor ) { new_width = last_w_index; new_height = last_h_index; } // 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() { diff --git a/src/zm_image.h b/src/zm_image.h index 7cfe5735b..1b8f614b9 100644 --- a/src/zm_image.h +++ b/src/zm_image.h @@ -167,7 +167,7 @@ protected: public: 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_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 ); @@ -201,18 +201,31 @@ public: 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( 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); + 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(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); } - inline Image &operator=( const Image &image ) { + inline Image &operator=(const Image &image) { Assign(image); return *this; } - inline Image &operator=( const unsigned char *new_buffer ) { + inline Image &operator=(const unsigned char *new_buffer) { (*fptr_imgbufcpy)(buffer, new_buffer, size); return *this; }